LureModule.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. if (im.dialog == (byte)InstantMessageDialog.RequestLure)
  127. {
  128. if (m_TransferModule != null)
  129. m_TransferModule.SendInstantMessage(im, delegate (bool success) { });
  130. }
  131. }
  132. public void OnStartLure(byte lureType, string message, UUID targetid, IClientAPI client)
  133. {
  134. if (!(client.Scene is Scene))
  135. return;
  136. Scene scene = (Scene)(client.Scene);
  137. ScenePresence presence = scene.GetScenePresence(client.AgentId);
  138. // Round up Z co-ordinate rather than round-down by casting. This stops tall avatars from being given
  139. // a teleport Z co-ordinate by short avatars that drops them through or embeds them in thin floors on
  140. // arrival.
  141. //
  142. // Ideally we would give the exact float position adjusting for the relative height of the two avatars
  143. // but it looks like a float component isn't possible with a parcel ID.
  144. UUID dest = Util.BuildFakeParcelID(
  145. scene.RegionInfo.RegionHandle,
  146. (uint)presence.AbsolutePosition.X,
  147. (uint)presence.AbsolutePosition.Y,
  148. (uint)presence.AbsolutePosition.Z + 2);
  149. m_log.DebugFormat("[LURE MODULE]: TP invite with message {0}, type {1}", message, lureType);
  150. GridInstantMessage m;
  151. if (scene.Permissions.IsAdministrator(client.AgentId) && presence.IsViewerUIGod && (!scene.Permissions.IsAdministrator(targetid)))
  152. {
  153. m = new GridInstantMessage(scene, client.AgentId,
  154. client.FirstName+" "+client.LastName, targetid,
  155. (byte)InstantMessageDialog.GodLikeRequestTeleport, false,
  156. message, dest, false, presence.AbsolutePosition,
  157. new Byte[0], true);
  158. }
  159. else
  160. {
  161. m = new GridInstantMessage(scene, client.AgentId,
  162. client.FirstName+" "+client.LastName, targetid,
  163. (byte)InstantMessageDialog.RequestTeleport, false,
  164. message, dest, false, presence.AbsolutePosition,
  165. new Byte[0], true);
  166. }
  167. if (m_TransferModule != null)
  168. {
  169. m_TransferModule.SendInstantMessage(m,
  170. delegate(bool success) { });
  171. }
  172. }
  173. public void OnTeleportLureRequest(UUID lureID, uint teleportFlags, IClientAPI client)
  174. {
  175. if (!(client.Scene is Scene))
  176. return;
  177. Scene scene = (Scene)(client.Scene);
  178. ulong handle = 0;
  179. uint x = 128;
  180. uint y = 128;
  181. uint z = 70;
  182. Util.ParseFakeParcelID(lureID, out handle, out x, out y, out z);
  183. Vector3 position = new Vector3();
  184. position.X = (float)x;
  185. position.Y = (float)y;
  186. position.Z = (float)z;
  187. scene.RequestTeleportLocation(client, handle, position,
  188. Vector3.Zero, teleportFlags);
  189. }
  190. private void OnGridInstantMessage(GridInstantMessage msg)
  191. {
  192. // Forward remote teleport requests
  193. //
  194. if (msg.dialog != (byte)InstantMessageDialog.RequestTeleport &&
  195. msg.dialog != (byte)InstantMessageDialog.GodLikeRequestTeleport &&
  196. msg.dialog != (byte)InstantMessageDialog.RequestLure)
  197. return;
  198. if (m_TransferModule != null)
  199. {
  200. m_TransferModule.SendInstantMessage(msg,
  201. delegate(bool success) { });
  202. }
  203. }
  204. }
  205. }