LureModule.cs 6.8 KB

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