HGLureModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using log4net;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using Mono.Addins;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Services.Connectors.Hypergrid;
  38. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  39. namespace OpenSim.Region.CoreModules.Avatar.Lure
  40. {
  41. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "HGLureModule")]
  42. public class HGLureModule : ISharedRegionModule
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. private readonly List<Scene> m_scenes = new List<Scene>();
  47. private IMessageTransferModule m_TransferModule = null;
  48. private bool m_Enabled = false;
  49. private GridInfo m_thisGridInfo;
  50. private readonly ExpiringCacheOS<UUID, GridInstantMessage> m_PendingLures = new ExpiringCacheOS<UUID, GridInstantMessage>(3600000);
  51. public void Initialise(IConfigSource config)
  52. {
  53. if (config.Configs["Messaging"] != null)
  54. {
  55. if (config.Configs["Messaging"].GetString("LureModule", string.Empty) == "HGLureModule")
  56. {
  57. m_Enabled = true;
  58. }
  59. }
  60. }
  61. public void AddRegion(Scene scene)
  62. {
  63. if (!m_Enabled)
  64. return;
  65. lock (m_scenes)
  66. {
  67. m_scenes.Add(scene);
  68. if(m_thisGridInfo == null)
  69. m_thisGridInfo = scene.SceneGridInfo;
  70. scene.EventManager.OnIncomingInstantMessage += OnIncomingInstantMessage;
  71. scene.EventManager.OnNewClient += OnNewClient;
  72. }
  73. }
  74. public void RegionLoaded(Scene scene)
  75. {
  76. if (!m_Enabled)
  77. return;
  78. if (m_TransferModule == null)
  79. {
  80. m_TransferModule =
  81. scene.RequestModuleInterface<IMessageTransferModule>();
  82. if (m_TransferModule == null)
  83. {
  84. m_log.Error("[LURE MODULE]: No message transfer module, lures will not work!");
  85. m_Enabled = false;
  86. m_scenes.Clear();
  87. scene.EventManager.OnNewClient -= OnNewClient;
  88. scene.EventManager.OnIncomingInstantMessage -= OnIncomingInstantMessage;
  89. }
  90. }
  91. }
  92. public void RemoveRegion(Scene scene)
  93. {
  94. if (!m_Enabled)
  95. return;
  96. lock (m_scenes)
  97. {
  98. m_scenes.Remove(scene);
  99. scene.EventManager.OnNewClient -= OnNewClient;
  100. scene.EventManager.OnIncomingInstantMessage -= OnIncomingInstantMessage;
  101. }
  102. }
  103. void OnNewClient(IClientAPI client)
  104. {
  105. client.OnInstantMessage += OnInstantMessage;
  106. client.OnStartLure += OnStartLure;
  107. client.OnTeleportLureRequest += OnTeleportLureRequest;
  108. }
  109. public void PostInitialise()
  110. {
  111. }
  112. public void Close()
  113. {
  114. m_thisGridInfo = null;
  115. }
  116. public string Name
  117. {
  118. get { return "HGLureModule"; }
  119. }
  120. public Type ReplaceableInterface
  121. {
  122. get { return null; }
  123. }
  124. void OnInstantMessage(IClientAPI client, GridInstantMessage im)
  125. {
  126. if (im.dialog == (byte)InstantMessageDialog.RequestLure)
  127. {
  128. if (m_TransferModule != null)
  129. m_TransferModule.SendInstantMessage(im, delegate (bool success) { });
  130. }
  131. }
  132. void OnIncomingInstantMessage(GridInstantMessage im)
  133. {
  134. if (im.dialog == (byte)InstantMessageDialog.RequestTeleport
  135. || im.dialog == (byte)InstantMessageDialog.GodLikeRequestTeleport)
  136. {
  137. UUID sessionID = new UUID(im.imSessionID);
  138. if (!m_PendingLures.Contains(sessionID))
  139. {
  140. m_log.DebugFormat("[HG LURE MODULE]: RequestTeleport sessionID={0}, regionID={1}, message={2}", im.imSessionID, im.RegionID, im.message);
  141. m_PendingLures.Add(sessionID, im, 7200000); // 2 hours
  142. }
  143. // Forward. We do this, because the IM module explicitly rejects
  144. // IMs of this type
  145. if (m_TransferModule != null)
  146. m_TransferModule.SendInstantMessage(im, delegate(bool success) { });
  147. }
  148. else if (im.dialog == (byte)InstantMessageDialog.RequestLure)
  149. {
  150. if (m_TransferModule != null)
  151. m_TransferModule.SendInstantMessage(im, delegate (bool success) { });
  152. }
  153. }
  154. public void OnStartLure(byte lureType, string message, UUID targetid, IClientAPI client)
  155. {
  156. if (!(client.Scene is Scene))
  157. return;
  158. Scene scene = (Scene)(client.Scene);
  159. ScenePresence presence = scene.GetScenePresence(client.AgentId);
  160. message += "@" + m_thisGridInfo.GateKeeperURLNoEndSlash;
  161. m_log.DebugFormat("[HG LURE MODULE]: TP invite with message {0}", message);
  162. UUID sessionID = UUID.Random();
  163. GridInstantMessage m = new GridInstantMessage(scene, client.AgentId,
  164. client.FirstName+" "+client.LastName, targetid,
  165. (byte)InstantMessageDialog.RequestTeleport, false,
  166. message, sessionID, false, presence.AbsolutePosition,
  167. Array.Empty<byte>(), true);
  168. m.RegionID = client.Scene.RegionInfo.RegionID.Guid;
  169. m_log.DebugFormat("[HG LURE MODULE]: RequestTeleport sessionID={0}, regionID={1}, message={2}", m.imSessionID, m.RegionID, m.message);
  170. m_PendingLures.Add(sessionID, m, 7200000); // 2 hours
  171. if (m_TransferModule != null)
  172. {
  173. m_TransferModule.SendInstantMessage(m,
  174. delegate(bool success) { });
  175. }
  176. }
  177. public void OnTeleportLureRequest(UUID lureID, uint teleportFlags, IClientAPI client)
  178. {
  179. if (!(client.Scene is Scene))
  180. return;
  181. //Scene scene = (Scene)(client.Scene);
  182. if (m_PendingLures.TryGetValue(lureID, out GridInstantMessage im))
  183. {
  184. m_PendingLures.Remove(lureID);
  185. Lure(client, teleportFlags, im);
  186. }
  187. else
  188. m_log.DebugFormat("[HG LURE MODULE]: pending lure {0} not found", lureID);
  189. }
  190. private void Lure(IClientAPI client, uint teleportflags, GridInstantMessage im)
  191. {
  192. Scene scene = client.Scene as Scene;
  193. UUID regionID = new UUID(im.RegionID);
  194. GridRegion region = scene.GridService.GetRegionByUUID(scene.RegionInfo.ScopeID, regionID);
  195. if (region != null)
  196. scene.RequestTeleportLocation(client, region.RegionHandle, im.Position + new Vector3(0.5f, 0.5f, 0f), Vector3.UnitX, teleportflags);
  197. else // we don't have that region here. Check if it's HG
  198. {
  199. string[] parts = im.message.Split(new char[] { '@' });
  200. if (parts.Length > 1)
  201. {
  202. string url = parts[parts.Length - 1]; // the last part
  203. if (m_thisGridInfo.IsLocalGrid(url, true) == 0)
  204. {
  205. m_log.DebugFormat("[HG LURE MODULE]: Luring agent to grid {0} region {1} position {2}", url, im.RegionID, im.Position);
  206. GatekeeperServiceConnector gConn = new GatekeeperServiceConnector();
  207. GridRegion gatekeeper = new GridRegion();
  208. gatekeeper.ServerURI = url;
  209. string homeURI = scene.GetAgentHomeURI(client.AgentId);
  210. string message;
  211. GridRegion finalDestination = gConn.GetHyperlinkRegion(gatekeeper, regionID, client.AgentId, homeURI, out message);
  212. if (finalDestination != null)
  213. {
  214. ScenePresence sp = scene.GetScenePresence(client.AgentId);
  215. IEntityTransferModule transferMod = scene.RequestModuleInterface<IEntityTransferModule>();
  216. if (transferMod != null && sp != null)
  217. {
  218. if (message != null)
  219. sp.ControllingClient.SendAgentAlertMessage(message, true);
  220. transferMod.DoTeleport(
  221. sp, gatekeeper, finalDestination, im.Position + new Vector3(0.5f, 0.5f, 0f),
  222. Vector3.UnitX, teleportflags);
  223. }
  224. }
  225. else
  226. {
  227. m_log.InfoFormat("[HG LURE MODULE]: Lure failed: {0}", message);
  228. client.SendAgentAlertMessage(message, true);
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }