OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
HGLureModule.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.Reflection;
31 using log4net;
32 using Nini.Config;
33 using OpenMetaverse;
34 using Mono.Addins;
35 
36 using OpenSim.Framework;
37 using OpenSim.Region.Framework.Interfaces;
38 using OpenSim.Region.Framework.Scenes;
39 using OpenSim.Services.Connectors.Hypergrid;
40 
42 
43 namespace OpenSim.Region.CoreModules.Avatar.Lure
44 {
45  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "HGLureModule")]
47  {
48  private static readonly ILog m_log = LogManager.GetLogger(
49  MethodBase.GetCurrentMethod().DeclaringType);
50 
51  private readonly List<Scene> m_scenes = new List<Scene>();
52 
53  private IMessageTransferModule m_TransferModule = null;
54  private bool m_Enabled = false;
55 
56  private string m_ThisGridURL;
57 
58  private ExpiringCache<UUID, GridInstantMessage> m_PendingLures = new ExpiringCache<UUID, GridInstantMessage>();
59 
60  public void Initialise(IConfigSource config)
61  {
62  if (config.Configs["Messaging"] != null)
63  {
64  if (config.Configs["Messaging"].GetString("LureModule", string.Empty) == "HGLureModule")
65  {
66  m_Enabled = true;
67 
68  m_ThisGridURL = Util.GetConfigVarFromSections<string>(config, "GatekeeperURI",
69  new string[] { "Startup", "Hypergrid", "Messaging" }, String.Empty);
70  // Legacy. Remove soon!
71  m_ThisGridURL = config.Configs["Messaging"].GetString("Gatekeeper", m_ThisGridURL);
72  m_log.DebugFormat("[LURE MODULE]: {0} enabled", Name);
73  }
74  }
75  }
76 
77  public void AddRegion(Scene scene)
78  {
79  if (!m_Enabled)
80  return;
81 
82  lock (m_scenes)
83  {
84  m_scenes.Add(scene);
85  scene.EventManager.OnIncomingInstantMessage += OnIncomingInstantMessage;
86  scene.EventManager.OnNewClient += OnNewClient;
87  }
88  }
89 
90  public void RegionLoaded(Scene scene)
91  {
92  if (!m_Enabled)
93  return;
94 
95  if (m_TransferModule == null)
96  {
97  m_TransferModule =
98  scene.RequestModuleInterface<IMessageTransferModule>();
99 
100  if (m_TransferModule == null)
101  {
102  m_log.Error("[LURE MODULE]: No message transfer module, lures will not work!");
103 
104  m_Enabled = false;
105  m_scenes.Clear();
106  scene.EventManager.OnNewClient -= OnNewClient;
107  scene.EventManager.OnIncomingInstantMessage -= OnIncomingInstantMessage;
108  }
109  }
110 
111  }
112 
113  public void RemoveRegion(Scene scene)
114  {
115  if (!m_Enabled)
116  return;
117 
118  lock (m_scenes)
119  {
120  m_scenes.Remove(scene);
121  scene.EventManager.OnNewClient -= OnNewClient;
122  scene.EventManager.OnIncomingInstantMessage -= OnIncomingInstantMessage;
123  }
124  }
125 
126  void OnNewClient(IClientAPI client)
127  {
128  client.OnInstantMessage += OnInstantMessage;
129  client.OnStartLure += OnStartLure;
130  client.OnTeleportLureRequest += OnTeleportLureRequest;
131  }
132 
133  public void PostInitialise()
134  {
135  }
136 
137  public void Close()
138  {
139  }
140 
141  public string Name
142  {
143  get { return "HGLureModule"; }
144  }
145 
146  public Type ReplaceableInterface
147  {
148  get { return null; }
149  }
150 
151  void OnInstantMessage(IClientAPI client, GridInstantMessage im)
152  {
153  }
154 
155  void OnIncomingInstantMessage(GridInstantMessage im)
156  {
157  if (im.dialog == (byte)InstantMessageDialog.RequestTeleport
158  || im.dialog == (byte)InstantMessageDialog.GodLikeRequestTeleport)
159  {
160  UUID sessionID = new UUID(im.imSessionID);
161 
162  if (!m_PendingLures.Contains(sessionID))
163  {
164  m_log.DebugFormat("[HG LURE MODULE]: RequestTeleport sessionID={0}, regionID={1}, message={2}", im.imSessionID, im.RegionID, im.message);
165  m_PendingLures.Add(sessionID, im, 7200); // 2 hours
166  }
167 
168  // Forward. We do this, because the IM module explicitly rejects
169  // IMs of this type
170  if (m_TransferModule != null)
171  m_TransferModule.SendInstantMessage(im, delegate(bool success) { });
172  }
173  }
174 
175  public void OnStartLure(byte lureType, string message, UUID targetid, IClientAPI client)
176  {
177  if (!(client.Scene is Scene))
178  return;
179 
180  Scene scene = (Scene)(client.Scene);
181  ScenePresence presence = scene.GetScenePresence(client.AgentId);
182 
183  message += "@" + m_ThisGridURL;
184 
185  m_log.DebugFormat("[HG LURE MODULE]: TP invite with message {0}", message);
186 
187  UUID sessionID = UUID.Random();
188 
189  GridInstantMessage m = new GridInstantMessage(scene, client.AgentId,
190  client.FirstName+" "+client.LastName, targetid,
191  (byte)InstantMessageDialog.RequestTeleport, false,
192  message, sessionID, false, presence.AbsolutePosition,
193  new Byte[0], true);
194  m.RegionID = client.Scene.RegionInfo.RegionID.Guid;
195 
196  m_log.DebugFormat("[HG LURE MODULE]: RequestTeleport sessionID={0}, regionID={1}, message={2}", m.imSessionID, m.RegionID, m.message);
197  m_PendingLures.Add(sessionID, m, 7200); // 2 hours
198 
199  if (m_TransferModule != null)
200  {
201  m_TransferModule.SendInstantMessage(m,
202  delegate(bool success) { });
203  }
204  }
205 
206  public void OnTeleportLureRequest(UUID lureID, uint teleportFlags, IClientAPI client)
207  {
208  if (!(client.Scene is Scene))
209  return;
210 
211 // Scene scene = (Scene)(client.Scene);
212 
213  GridInstantMessage im = null;
214  if (m_PendingLures.TryGetValue(lureID, out im))
215  {
216  m_PendingLures.Remove(lureID);
217  Lure(client, teleportFlags, im);
218  }
219  else
220  m_log.DebugFormat("[HG LURE MODULE]: pending lure {0} not found", lureID);
221 
222  }
223 
224  private void Lure(IClientAPI client, uint teleportflags, GridInstantMessage im)
225  {
226  Scene scene = (Scene)(client.Scene);
227  GridRegion region = scene.GridService.GetRegionByUUID(scene.RegionInfo.ScopeID, new UUID(im.RegionID));
228  if (region != null)
229  scene.RequestTeleportLocation(client, region.RegionHandle, im.Position + new Vector3(0.5f, 0.5f, 0f), Vector3.UnitX, teleportflags);
230  else // we don't have that region here. Check if it's HG
231  {
232  string[] parts = im.message.Split(new char[] { '@' });
233  if (parts.Length > 1)
234  {
235  string url = parts[parts.Length - 1]; // the last part
236  if (url.Trim(new char[] {'/'}) != m_ThisGridURL.Trim(new char[] {'/'}))
237  {
238  m_log.DebugFormat("[HG LURE MODULE]: Luring agent to grid {0} region {1} position {2}", url, im.RegionID, im.Position);
240  GridRegion gatekeeper = new GridRegion();
241  gatekeeper.ServerURI = url;
242  string homeURI = scene.GetAgentHomeURI(client.AgentId);
243 
244  string message;
245  GridRegion finalDestination = gConn.GetHyperlinkRegion(gatekeeper, new UUID(im.RegionID), client.AgentId, homeURI, out message);
246  if (finalDestination != null)
247  {
248  ScenePresence sp = scene.GetScenePresence(client.AgentId);
249  IEntityTransferModule transferMod = scene.RequestModuleInterface<IEntityTransferModule>();
250 
251  if (transferMod != null && sp != null)
252  {
253  if (message != null)
254  sp.ControllingClient.SendAgentAlertMessage(message, true);
255 
256  transferMod.DoTeleport(
257  sp, gatekeeper, finalDestination, im.Position + new Vector3(0.5f, 0.5f, 0f),
258  Vector3.UnitX, teleportflags);
259  }
260  }
261  else
262  {
263  m_log.InfoFormat("[HG LURE MODULE]: Lure failed: {0}", message);
264  client.SendAgentAlertMessage(message, true);
265  }
266  }
267  }
268  }
269  }
270  }
271 }
void PostInitialise()
This is called exactly once after all the shared region-modules have been instanciated and IRegionMod...
void OnTeleportLureRequest(UUID lureID, uint teleportFlags, IClientAPI client)
void OnStartLure(byte lureType, string message, UUID targetid, IClientAPI client)
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
Definition: HGLureModule.cs:90
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
override Vector3 AbsolutePosition
Position of this avatar relative to the region the avatar is in
Interactive OpenSim region server
Definition: OpenSim.cs:55
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
Definition: HGLureModule.cs:77
OpenSim.Services.Interfaces.GridRegion GridRegion
void Initialise(IConfigSource config)
This is called to initialize the region module. For shared modules, this is called exactly once...
Definition: HGLureModule.cs:60
OpenSim.Services.Interfaces.GridRegion GridRegion
Definition: HGLureModule.cs:41