HGLureModule.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 string m_ThisGridURL;
  50. private ExpiringCache<UUID, GridInstantMessage> m_PendingLures = new ExpiringCache<UUID, GridInstantMessage>();
  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. m_ThisGridURL = config.Configs["Messaging"].GetString("Gatekeeper", string.Empty);
  59. m_log.DebugFormat("[LURE MODULE]: {0} enabled", Name);
  60. }
  61. }
  62. }
  63. public void AddRegion(Scene scene)
  64. {
  65. if (!m_Enabled)
  66. return;
  67. lock (m_scenes)
  68. {
  69. m_scenes.Add(scene);
  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. }
  115. public string Name
  116. {
  117. get { return "HGLureModule"; }
  118. }
  119. public Type ReplaceableInterface
  120. {
  121. get { return null; }
  122. }
  123. void OnInstantMessage(IClientAPI client, GridInstantMessage im)
  124. {
  125. }
  126. void OnIncomingInstantMessage(GridInstantMessage im)
  127. {
  128. if (im.dialog == (byte)InstantMessageDialog.RequestTeleport)
  129. {
  130. UUID sessionID = new UUID(im.imSessionID);
  131. if (!m_PendingLures.Contains(sessionID))
  132. {
  133. m_log.DebugFormat("[HG LURE MODULE]: RequestTeleport sessionID={0}, regionID={1}, message={2}", im.imSessionID, im.RegionID, im.message);
  134. m_PendingLures.Add(sessionID, im, 7200); // 2 hours
  135. }
  136. // Forward. We do this, because the IM module explicitly rejects
  137. // IMs of this type
  138. if (m_TransferModule != null)
  139. m_TransferModule.SendInstantMessage(im, delegate(bool success) { });
  140. }
  141. }
  142. public void OnStartLure(byte lureType, string message, UUID targetid, IClientAPI client)
  143. {
  144. if (!(client.Scene is Scene))
  145. return;
  146. Scene scene = (Scene)(client.Scene);
  147. ScenePresence presence = scene.GetScenePresence(client.AgentId);
  148. message += "@" + m_ThisGridURL;
  149. m_log.DebugFormat("[HG LURE MODULE]: TP invite with message {0}", message);
  150. UUID sessionID = UUID.Random();
  151. GridInstantMessage m = new GridInstantMessage(scene, client.AgentId,
  152. client.FirstName+" "+client.LastName, targetid,
  153. (byte)InstantMessageDialog.RequestTeleport, false,
  154. message, sessionID, false, presence.AbsolutePosition,
  155. new Byte[0], true);
  156. m.RegionID = client.Scene.RegionInfo.RegionID.Guid;
  157. m_log.DebugFormat("[HG LURE MODULE]: RequestTeleport sessionID={0}, regionID={1}, message={2}", m.imSessionID, m.RegionID, m.message);
  158. m_PendingLures.Add(sessionID, m, 7200); // 2 hours
  159. if (m_TransferModule != null)
  160. {
  161. m_TransferModule.SendInstantMessage(m,
  162. delegate(bool success) { });
  163. }
  164. }
  165. public void OnTeleportLureRequest(UUID lureID, uint teleportFlags, IClientAPI client)
  166. {
  167. if (!(client.Scene is Scene))
  168. return;
  169. // Scene scene = (Scene)(client.Scene);
  170. GridInstantMessage im = null;
  171. if (m_PendingLures.TryGetValue(lureID, out im))
  172. {
  173. m_PendingLures.Remove(lureID);
  174. Lure(client, teleportFlags, im);
  175. }
  176. else
  177. m_log.DebugFormat("[HG LURE MODULE]: pending lure {0} not found", lureID);
  178. }
  179. private void Lure(IClientAPI client, uint teleportflags, GridInstantMessage im)
  180. {
  181. Scene scene = (Scene)(client.Scene);
  182. GridRegion region = scene.GridService.GetRegionByUUID(scene.RegionInfo.ScopeID, new UUID(im.RegionID));
  183. if (region != null)
  184. scene.RequestTeleportLocation(client, region.RegionHandle, im.Position + new Vector3(0.5f, 0.5f, 0f), Vector3.UnitX, teleportflags);
  185. else // we don't have that region here. Check if it's HG
  186. {
  187. string[] parts = im.message.Split(new char[] { '@' });
  188. if (parts.Length > 1)
  189. {
  190. string url = parts[parts.Length - 1]; // the last part
  191. if (url.Trim(new char[] {'/'}) != m_ThisGridURL.Trim(new char[] {'/'}))
  192. {
  193. m_log.DebugFormat("[HG LURE MODULE]: Luring agent to grid {0} region {1} position {2}", url, im.RegionID, im.Position);
  194. GatekeeperServiceConnector gConn = new GatekeeperServiceConnector();
  195. GridRegion gatekeeper = new GridRegion();
  196. gatekeeper.ServerURI = url;
  197. GridRegion finalDestination = gConn.GetHyperlinkRegion(gatekeeper, new UUID(im.RegionID));
  198. if (finalDestination != null)
  199. {
  200. ScenePresence sp = scene.GetScenePresence(client.AgentId);
  201. IEntityTransferModule transferMod = scene.RequestModuleInterface<IEntityTransferModule>();
  202. if (transferMod != null && sp != null)
  203. transferMod.DoTeleport(
  204. sp, gatekeeper, finalDestination, im.Position + new Vector3(0.5f, 0.5f, 0f),
  205. Vector3.UnitX, teleportflags);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }