HGLureModule.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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")]
  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. m_log.DebugFormat("[HG LURE MODULE]: RequestTeleport sessionID={0}, regionID={1}, message={2}", im.imSessionID, im.RegionID, im.message);
  132. m_PendingLures.Add(sessionID, im, 7200); // 2 hours
  133. // Forward. We do this, because the IM module explicitly rejects
  134. // IMs of this type
  135. if (m_TransferModule != null)
  136. m_TransferModule.SendInstantMessage(im, delegate(bool success) { });
  137. }
  138. }
  139. public void OnStartLure(byte lureType, string message, UUID targetid, IClientAPI client)
  140. {
  141. if (!(client.Scene is Scene))
  142. return;
  143. Scene scene = (Scene)(client.Scene);
  144. ScenePresence presence = scene.GetScenePresence(client.AgentId);
  145. message += "@" + m_ThisGridURL;
  146. m_log.DebugFormat("[HG LURE MODULE]: TP invite with message {0}", message);
  147. GridInstantMessage m = new GridInstantMessage(scene, client.AgentId,
  148. client.FirstName+" "+client.LastName, targetid,
  149. (byte)InstantMessageDialog.RequestTeleport, false,
  150. message, UUID.Random(), false, presence.AbsolutePosition,
  151. new Byte[0]);
  152. m.RegionID = client.Scene.RegionInfo.RegionID.Guid;
  153. if (m_TransferModule != null)
  154. {
  155. m_TransferModule.SendInstantMessage(m,
  156. delegate(bool success) { });
  157. }
  158. }
  159. public void OnTeleportLureRequest(UUID lureID, uint teleportFlags, IClientAPI client)
  160. {
  161. if (!(client.Scene is Scene))
  162. return;
  163. // Scene scene = (Scene)(client.Scene);
  164. GridInstantMessage im = null;
  165. if (m_PendingLures.TryGetValue(lureID, out im))
  166. {
  167. m_PendingLures.Remove(lureID);
  168. Lure(client, teleportFlags, im);
  169. }
  170. else
  171. m_log.DebugFormat("[HG LURE MODULE]: pending lure {0} not found", lureID);
  172. }
  173. private void Lure(IClientAPI client, uint teleportflags, GridInstantMessage im)
  174. {
  175. Scene scene = (Scene)(client.Scene);
  176. GridRegion region = scene.GridService.GetRegionByUUID(scene.RegionInfo.ScopeID, new UUID(im.RegionID));
  177. if (region != null)
  178. scene.RequestTeleportLocation(client, region.RegionHandle, im.Position + new Vector3(0.5f, 0.5f, 0f), Vector3.UnitX, teleportflags);
  179. else // we don't have that region here. Check if it's HG
  180. {
  181. string[] parts = im.message.Split(new char[] { '@' });
  182. if (parts.Length > 1)
  183. {
  184. string url = parts[parts.Length - 1]; // the last part
  185. if (url.Trim(new char[] {'/'}) != m_ThisGridURL.Trim(new char[] {'/'}))
  186. {
  187. m_log.DebugFormat("[HG LURE MODULE]: Luring agent to grid {0} region {1} position {2}", url, im.RegionID, im.Position);
  188. GatekeeperServiceConnector gConn = new GatekeeperServiceConnector();
  189. GridRegion gatekeeper = new GridRegion();
  190. gatekeeper.ServerURI = url;
  191. GridRegion finalDestination = gConn.GetHyperlinkRegion(gatekeeper, new UUID(im.RegionID));
  192. if (finalDestination != null)
  193. {
  194. ScenePresence sp = scene.GetScenePresence(client.AgentId);
  195. IEntityTransferModule transferMod = scene.RequestModuleInterface<IEntityTransferModule>();
  196. IEventQueue eq = sp.Scene.RequestModuleInterface<IEventQueue>();
  197. if (transferMod != null && sp != null && eq != null)
  198. transferMod.DoTeleport(sp, gatekeeper, finalDestination, im.Position + new Vector3(0.5f, 0.5f, 0f), Vector3.UnitX, teleportflags, eq);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }