LureModule.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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)Math.Ceiling(presence.AbsolutePosition.Z));
  144. m_log.DebugFormat("[LURE MODULE]: TP invite with message {0}, type {1}", message, lureType);
  145. GridInstantMessage m = new GridInstantMessage(scene, client.AgentId,
  146. client.FirstName+" "+client.LastName, targetid,
  147. (byte)InstantMessageDialog.RequestTeleport, false,
  148. message, dest, false, presence.AbsolutePosition,
  149. new Byte[0], true);
  150. if (m_TransferModule != null)
  151. {
  152. m_TransferModule.SendInstantMessage(m,
  153. delegate(bool success) { });
  154. }
  155. }
  156. public void OnTeleportLureRequest(UUID lureID, uint teleportFlags, IClientAPI client)
  157. {
  158. if (!(client.Scene is Scene))
  159. return;
  160. Scene scene = (Scene)(client.Scene);
  161. ulong handle = 0;
  162. uint x = 128;
  163. uint y = 128;
  164. uint z = 70;
  165. Util.ParseFakeParcelID(lureID, out handle, out x, out y, out z);
  166. Vector3 position = new Vector3();
  167. position.X = (float)x;
  168. position.Y = (float)y;
  169. position.Z = (float)z;
  170. scene.RequestTeleportLocation(client, handle, position,
  171. Vector3.Zero, teleportFlags);
  172. }
  173. private void OnGridInstantMessage(GridInstantMessage msg)
  174. {
  175. // Forward remote teleport requests
  176. //
  177. if (msg.dialog != 22)
  178. return;
  179. if (m_TransferModule != null)
  180. {
  181. m_TransferModule.SendInstantMessage(msg,
  182. delegate(bool success) { });
  183. }
  184. }
  185. }
  186. }