HGLureModule.cs 11 KB

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