HGLureModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 = Util.GetConfigVarFromSections<string>(config, "GatekeeperURI",
  59. new string[] { "Startup", "Hypergrid", "Messaging" }, String.Empty);
  60. // Legacy. Remove soon!
  61. m_ThisGridURL = config.Configs["Messaging"].GetString("Gatekeeper", m_ThisGridURL);
  62. m_log.DebugFormat("[LURE MODULE]: {0} enabled", Name);
  63. }
  64. }
  65. }
  66. public void AddRegion(Scene scene)
  67. {
  68. if (!m_Enabled)
  69. return;
  70. lock (m_scenes)
  71. {
  72. m_scenes.Add(scene);
  73. scene.EventManager.OnIncomingInstantMessage += OnIncomingInstantMessage;
  74. scene.EventManager.OnNewClient += OnNewClient;
  75. }
  76. }
  77. public void RegionLoaded(Scene scene)
  78. {
  79. if (!m_Enabled)
  80. return;
  81. if (m_TransferModule == null)
  82. {
  83. m_TransferModule =
  84. scene.RequestModuleInterface<IMessageTransferModule>();
  85. if (m_TransferModule == null)
  86. {
  87. m_log.Error("[LURE MODULE]: No message transfer module, lures will not work!");
  88. m_Enabled = false;
  89. m_scenes.Clear();
  90. scene.EventManager.OnNewClient -= OnNewClient;
  91. scene.EventManager.OnIncomingInstantMessage -= OnIncomingInstantMessage;
  92. }
  93. }
  94. }
  95. public void RemoveRegion(Scene scene)
  96. {
  97. if (!m_Enabled)
  98. return;
  99. lock (m_scenes)
  100. {
  101. m_scenes.Remove(scene);
  102. scene.EventManager.OnNewClient -= OnNewClient;
  103. scene.EventManager.OnIncomingInstantMessage -= OnIncomingInstantMessage;
  104. }
  105. }
  106. void OnNewClient(IClientAPI client)
  107. {
  108. client.OnInstantMessage += OnInstantMessage;
  109. client.OnStartLure += OnStartLure;
  110. client.OnTeleportLureRequest += OnTeleportLureRequest;
  111. }
  112. public void PostInitialise()
  113. {
  114. }
  115. public void Close()
  116. {
  117. }
  118. public string Name
  119. {
  120. get { return "HGLureModule"; }
  121. }
  122. public Type ReplaceableInterface
  123. {
  124. get { return null; }
  125. }
  126. void OnInstantMessage(IClientAPI client, GridInstantMessage im)
  127. {
  128. if (im.dialog == (byte)InstantMessageDialog.RequestLure)
  129. {
  130. if (m_TransferModule != null)
  131. m_TransferModule.SendInstantMessage(im, delegate (bool success) { });
  132. }
  133. }
  134. void OnIncomingInstantMessage(GridInstantMessage im)
  135. {
  136. if (im.dialog == (byte)InstantMessageDialog.RequestTeleport
  137. || im.dialog == (byte)InstantMessageDialog.GodLikeRequestTeleport)
  138. {
  139. UUID sessionID = new UUID(im.imSessionID);
  140. if (!m_PendingLures.Contains(sessionID))
  141. {
  142. m_log.DebugFormat("[HG LURE MODULE]: RequestTeleport sessionID={0}, regionID={1}, message={2}", im.imSessionID, im.RegionID, im.message);
  143. m_PendingLures.Add(sessionID, im, 7200); // 2 hours
  144. }
  145. // Forward. We do this, because the IM module explicitly rejects
  146. // IMs of this type
  147. if (m_TransferModule != null)
  148. m_TransferModule.SendInstantMessage(im, delegate(bool success) { });
  149. }
  150. }
  151. public void OnStartLure(byte lureType, string message, UUID targetid, IClientAPI client)
  152. {
  153. if (!(client.Scene is Scene))
  154. return;
  155. Scene scene = (Scene)(client.Scene);
  156. ScenePresence presence = scene.GetScenePresence(client.AgentId);
  157. message += "@" + m_ThisGridURL;
  158. m_log.DebugFormat("[HG LURE MODULE]: TP invite with message {0}", message);
  159. UUID sessionID = UUID.Random();
  160. GridInstantMessage m = new GridInstantMessage(scene, client.AgentId,
  161. client.FirstName+" "+client.LastName, targetid,
  162. (byte)InstantMessageDialog.RequestTeleport, false,
  163. message, sessionID, false, presence.AbsolutePosition,
  164. new Byte[0], true);
  165. m.RegionID = client.Scene.RegionInfo.RegionID.Guid;
  166. m_log.DebugFormat("[HG LURE MODULE]: RequestTeleport sessionID={0}, regionID={1}, message={2}", m.imSessionID, m.RegionID, m.message);
  167. m_PendingLures.Add(sessionID, m, 7200); // 2 hours
  168. if (m_TransferModule != null)
  169. {
  170. m_TransferModule.SendInstantMessage(m,
  171. delegate(bool success) { });
  172. }
  173. }
  174. public void OnTeleportLureRequest(UUID lureID, uint teleportFlags, IClientAPI client)
  175. {
  176. if (!(client.Scene is Scene))
  177. return;
  178. // Scene scene = (Scene)(client.Scene);
  179. GridInstantMessage im = null;
  180. if (m_PendingLures.TryGetValue(lureID, out im))
  181. {
  182. m_PendingLures.Remove(lureID);
  183. Lure(client, teleportFlags, im);
  184. }
  185. else
  186. m_log.DebugFormat("[HG LURE MODULE]: pending lure {0} not found", lureID);
  187. }
  188. private void Lure(IClientAPI client, uint teleportflags, GridInstantMessage im)
  189. {
  190. Scene scene = (Scene)(client.Scene);
  191. GridRegion region = scene.GridService.GetRegionByUUID(scene.RegionInfo.ScopeID, new UUID(im.RegionID));
  192. if (region != null)
  193. scene.RequestTeleportLocation(client, region.RegionHandle, im.Position + new Vector3(0.5f, 0.5f, 0f), Vector3.UnitX, teleportflags);
  194. else // we don't have that region here. Check if it's HG
  195. {
  196. string[] parts = im.message.Split(new char[] { '@' });
  197. if (parts.Length > 1)
  198. {
  199. string url = parts[parts.Length - 1]; // the last part
  200. if (url.Trim(new char[] {'/'}) != m_ThisGridURL.Trim(new char[] {'/'}))
  201. {
  202. m_log.DebugFormat("[HG LURE MODULE]: Luring agent to grid {0} region {1} position {2}", url, im.RegionID, im.Position);
  203. GatekeeperServiceConnector gConn = new GatekeeperServiceConnector();
  204. GridRegion gatekeeper = new GridRegion();
  205. gatekeeper.ServerURI = url;
  206. string homeURI = scene.GetAgentHomeURI(client.AgentId);
  207. string message;
  208. GridRegion finalDestination = gConn.GetHyperlinkRegion(gatekeeper, new UUID(im.RegionID), client.AgentId, homeURI, out message);
  209. if (finalDestination != null)
  210. {
  211. ScenePresence sp = scene.GetScenePresence(client.AgentId);
  212. IEntityTransferModule transferMod = scene.RequestModuleInterface<IEntityTransferModule>();
  213. if (transferMod != null && sp != null)
  214. {
  215. if (message != null)
  216. sp.ControllingClient.SendAgentAlertMessage(message, true);
  217. transferMod.DoTeleport(
  218. sp, gatekeeper, finalDestination, im.Position + new Vector3(0.5f, 0.5f, 0f),
  219. Vector3.UnitX, teleportflags);
  220. }
  221. }
  222. else
  223. {
  224. m_log.InfoFormat("[HG LURE MODULE]: Lure failed: {0}", message);
  225. client.SendAgentAlertMessage(message, true);
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }