LureModule.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 OpenSim.Framework;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. namespace OpenSim.Region.CoreModules.Avatar.Lure
  37. {
  38. public class LureModule : ISharedRegionModule
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(
  41. MethodBase.GetCurrentMethod().DeclaringType);
  42. private readonly List<Scene> m_scenes = new List<Scene>();
  43. private IMessageTransferModule m_TransferModule = null;
  44. private bool m_Enabled = false;
  45. public void Initialise(IConfigSource config)
  46. {
  47. if (config.Configs["Messaging"] != null)
  48. {
  49. if (config.Configs["Messaging"].GetString(
  50. "LureModule", "LureModule") ==
  51. "LureModule")
  52. {
  53. m_Enabled = true;
  54. m_log.DebugFormat("[LURE MODULE]: {0} enabled", Name);
  55. }
  56. }
  57. }
  58. public void AddRegion(Scene scene)
  59. {
  60. if (!m_Enabled)
  61. return;
  62. lock (m_scenes)
  63. {
  64. m_scenes.Add(scene);
  65. scene.EventManager.OnNewClient += OnNewClient;
  66. scene.EventManager.OnIncomingInstantMessage +=
  67. OnGridInstantMessage;
  68. }
  69. }
  70. public void RegionLoaded(Scene scene)
  71. {
  72. if (!m_Enabled)
  73. return;
  74. if (m_TransferModule == null)
  75. {
  76. m_TransferModule =
  77. scene.RequestModuleInterface<IMessageTransferModule>();
  78. if (m_TransferModule == null)
  79. {
  80. m_log.Error("[INSTANT MESSAGE]: No message transfer module, "+
  81. "lures will not work!");
  82. m_Enabled = false;
  83. m_scenes.Clear();
  84. scene.EventManager.OnNewClient -= OnNewClient;
  85. scene.EventManager.OnIncomingInstantMessage -=
  86. OnGridInstantMessage;
  87. }
  88. }
  89. }
  90. public void RemoveRegion(Scene scene)
  91. {
  92. if (!m_Enabled)
  93. return;
  94. lock (m_scenes)
  95. {
  96. m_scenes.Remove(scene);
  97. scene.EventManager.OnNewClient -= OnNewClient;
  98. scene.EventManager.OnIncomingInstantMessage -=
  99. OnGridInstantMessage;
  100. }
  101. }
  102. void OnNewClient(IClientAPI client)
  103. {
  104. client.OnInstantMessage += OnInstantMessage;
  105. client.OnStartLure += OnStartLure;
  106. client.OnTeleportLureRequest += OnTeleportLureRequest;
  107. }
  108. public void PostInitialise()
  109. {
  110. }
  111. public void Close()
  112. {
  113. }
  114. public string Name
  115. {
  116. get { return "LureModule"; }
  117. }
  118. public Type ReplaceableInterface
  119. {
  120. get { return null; }
  121. }
  122. public void OnInstantMessage(IClientAPI client, GridInstantMessage im)
  123. {
  124. }
  125. public void OnStartLure(byte lureType, string message, UUID targetid, IClientAPI client)
  126. {
  127. if (!(client.Scene is Scene))
  128. return;
  129. Scene scene = (Scene)(client.Scene);
  130. ScenePresence presence = scene.GetScenePresence(client.AgentId);
  131. UUID dest = Util.BuildFakeParcelID(
  132. scene.RegionInfo.RegionHandle,
  133. (uint)presence.AbsolutePosition.X,
  134. (uint)presence.AbsolutePosition.Y,
  135. (uint)presence.AbsolutePosition.Z);
  136. m_log.DebugFormat("TP invite with message {0}", message);
  137. GridInstantMessage m = new GridInstantMessage(scene, client.AgentId,
  138. client.FirstName+" "+client.LastName, targetid,
  139. (byte)InstantMessageDialog.RequestTeleport, false,
  140. message, dest, false, presence.AbsolutePosition,
  141. new Byte[0]);
  142. if (m_TransferModule != null)
  143. {
  144. m_TransferModule.SendInstantMessage(m,
  145. delegate(bool success) { });
  146. }
  147. }
  148. public void OnTeleportLureRequest(UUID lureID, uint teleportFlags, IClientAPI client)
  149. {
  150. if (!(client.Scene is Scene))
  151. return;
  152. Scene scene = (Scene)(client.Scene);
  153. ulong handle = 0;
  154. uint x = 128;
  155. uint y = 128;
  156. uint z = 70;
  157. Util.ParseFakeParcelID(lureID, out handle, out x, out y, out z);
  158. Vector3 position = new Vector3();
  159. position.X = (float)x;
  160. position.Y = (float)y;
  161. position.Z = (float)z;
  162. scene.RequestTeleportLocation(client, handle, position,
  163. Vector3.Zero, teleportFlags);
  164. }
  165. private void OnGridInstantMessage(GridInstantMessage msg)
  166. {
  167. // Forward remote teleport requests
  168. //
  169. if (msg.dialog != 22)
  170. return;
  171. if (m_TransferModule != null)
  172. {
  173. m_TransferModule.SendInstantMessage(msg,
  174. delegate(bool success) { });
  175. }
  176. }
  177. }
  178. }