LureModule.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 Mono.Addins;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. namespace OpenSim.Region.CoreModules.Avatar.Lure
  38. {
  39. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "LureModule")]
  40. public class LureModule : ISharedRegionModule
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(
  43. MethodBase.GetCurrentMethod().DeclaringType);
  44. private readonly List<Scene> m_scenes = new List<Scene>();
  45. private IMessageTransferModule m_TransferModule = null;
  46. private bool m_Enabled = false;
  47. public void Initialise(IConfigSource config)
  48. {
  49. if (config.Configs["Messaging"] != null)
  50. {
  51. if (config.Configs["Messaging"].GetString(
  52. "LureModule", "LureModule") ==
  53. "LureModule")
  54. {
  55. m_Enabled = true;
  56. m_log.DebugFormat("[LURE MODULE]: {0} enabled", Name);
  57. }
  58. }
  59. }
  60. public void AddRegion(Scene scene)
  61. {
  62. if (!m_Enabled)
  63. return;
  64. lock (m_scenes)
  65. {
  66. m_scenes.Add(scene);
  67. scene.EventManager.OnNewClient += OnNewClient;
  68. scene.EventManager.OnIncomingInstantMessage +=
  69. OnGridInstantMessage;
  70. }
  71. }
  72. public void RegionLoaded(Scene scene)
  73. {
  74. if (!m_Enabled)
  75. return;
  76. if (m_TransferModule == null)
  77. {
  78. m_TransferModule =
  79. scene.RequestModuleInterface<IMessageTransferModule>();
  80. if (m_TransferModule == null)
  81. {
  82. m_log.Error("[INSTANT MESSAGE]: No message transfer module, "+
  83. "lures will not work!");
  84. m_Enabled = false;
  85. m_scenes.Clear();
  86. scene.EventManager.OnNewClient -= OnNewClient;
  87. scene.EventManager.OnIncomingInstantMessage -=
  88. OnGridInstantMessage;
  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 -=
  101. OnGridInstantMessage;
  102. }
  103. }
  104. void OnNewClient(IClientAPI client)
  105. {
  106. client.OnInstantMessage += OnInstantMessage;
  107. client.OnStartLure += OnStartLure;
  108. client.OnTeleportLureRequest += OnTeleportLureRequest;
  109. }
  110. public void PostInitialise()
  111. {
  112. }
  113. public void Close()
  114. {
  115. }
  116. public string Name
  117. {
  118. get { return "LureModule"; }
  119. }
  120. public Type ReplaceableInterface
  121. {
  122. get { return null; }
  123. }
  124. public void OnInstantMessage(IClientAPI client, GridInstantMessage im)
  125. {
  126. }
  127. public void OnStartLure(byte lureType, string message, UUID targetid, IClientAPI client)
  128. {
  129. if (!(client.Scene is Scene))
  130. return;
  131. Scene scene = (Scene)(client.Scene);
  132. ScenePresence presence = scene.GetScenePresence(client.AgentId);
  133. // Round up Z co-ordinate rather than round-down by casting. This stops tall avatars from being given
  134. // a teleport Z co-ordinate by short avatars that drops them through or embeds them in thin floors on
  135. // arrival.
  136. //
  137. // Ideally we would give the exact float position adjusting for the relative height of the two avatars
  138. // but it looks like a float component isn't possible with a parcel ID.
  139. UUID dest = Util.BuildFakeParcelID(
  140. scene.RegionInfo.RegionHandle,
  141. (uint)presence.AbsolutePosition.X,
  142. (uint)presence.AbsolutePosition.Y,
  143. (uint)presence.AbsolutePosition.Z + 2);
  144. m_log.DebugFormat("[LURE MODULE]: TP invite with message {0}, type {1}", message, lureType);
  145. GridInstantMessage m;
  146. if (scene.Permissions.IsAdministrator(client.AgentId) && presence.IsViewerUIGod && (!scene.Permissions.IsAdministrator(targetid)))
  147. {
  148. m = new GridInstantMessage(scene, client.AgentId,
  149. client.FirstName+" "+client.LastName, targetid,
  150. (byte)InstantMessageDialog.GodLikeRequestTeleport, false,
  151. message, dest, false, presence.AbsolutePosition,
  152. new Byte[0], true);
  153. }
  154. else
  155. {
  156. m = new GridInstantMessage(scene, client.AgentId,
  157. client.FirstName+" "+client.LastName, targetid,
  158. (byte)InstantMessageDialog.RequestTeleport, false,
  159. message, dest, false, presence.AbsolutePosition,
  160. new Byte[0], true);
  161. }
  162. if (m_TransferModule != null)
  163. {
  164. m_TransferModule.SendInstantMessage(m,
  165. delegate(bool success) { });
  166. }
  167. }
  168. public void OnTeleportLureRequest(UUID lureID, uint teleportFlags, IClientAPI client)
  169. {
  170. if (!(client.Scene is Scene))
  171. return;
  172. Scene scene = (Scene)(client.Scene);
  173. ulong handle = 0;
  174. uint x = 128;
  175. uint y = 128;
  176. uint z = 70;
  177. Util.ParseFakeParcelID(lureID, out handle, out x, out y, out z);
  178. Vector3 position = new Vector3();
  179. position.X = (float)x;
  180. position.Y = (float)y;
  181. position.Z = (float)z;
  182. scene.RequestTeleportLocation(client, handle, position,
  183. Vector3.Zero, teleportFlags);
  184. }
  185. private void OnGridInstantMessage(GridInstantMessage msg)
  186. {
  187. // Forward remote teleport requests
  188. //
  189. if (msg.dialog != (byte)InstantMessageDialog.RequestTeleport &&
  190. msg.dialog != (byte)InstantMessageDialog.GodLikeRequestTeleport)
  191. return;
  192. if (m_TransferModule != null)
  193. {
  194. m_TransferModule.SendInstantMessage(msg,
  195. delegate(bool success) { });
  196. }
  197. }
  198. }
  199. }