diff options
Diffstat (limited to 'OpenSim/Region/CoreModules/Avatar/InstantMessage/HGMessageTransferModule.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/Avatar/InstantMessage/HGMessageTransferModule.cs | 350 |
1 files changed, 350 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/Avatar/InstantMessage/HGMessageTransferModule.cs b/OpenSim/Region/CoreModules/Avatar/InstantMessage/HGMessageTransferModule.cs new file mode 100644 index 0000000..7753c25 --- /dev/null +++ b/OpenSim/Region/CoreModules/Avatar/InstantMessage/HGMessageTransferModule.cs | |||
@@ -0,0 +1,350 @@ | |||
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; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Net; | ||
31 | using System.Reflection; | ||
32 | using log4net; | ||
33 | using Nini.Config; | ||
34 | using Nwc.XmlRpc; | ||
35 | using Mono.Addins; | ||
36 | using OpenMetaverse; | ||
37 | using OpenSim.Framework; | ||
38 | using OpenSim.Region.Framework.Interfaces; | ||
39 | using OpenSim.Region.Framework.Scenes; | ||
40 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
41 | using PresenceInfo = OpenSim.Services.Interfaces.PresenceInfo; | ||
42 | using OpenSim.Services.Interfaces; | ||
43 | using OpenSim.Services.Connectors.InstantMessage; | ||
44 | using OpenSim.Services.Connectors.Hypergrid; | ||
45 | using OpenSim.Server.Handlers.Hypergrid; | ||
46 | |||
47 | namespace OpenSim.Region.CoreModules.Avatar.InstantMessage | ||
48 | { | ||
49 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")] | ||
50 | public class HGMessageTransferModule : ISharedRegionModule, IMessageTransferModule, IInstantMessageSimConnector | ||
51 | { | ||
52 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
53 | |||
54 | protected bool m_Enabled = false; | ||
55 | protected List<Scene> m_Scenes = new List<Scene>(); | ||
56 | |||
57 | protected IInstantMessage m_IMService; | ||
58 | protected Dictionary<UUID, object> m_UserLocationMap = new Dictionary<UUID, object>(); | ||
59 | |||
60 | public event UndeliveredMessage OnUndeliveredMessage; | ||
61 | |||
62 | IUserManagement m_uMan; | ||
63 | IUserManagement UserManagementModule | ||
64 | { | ||
65 | get | ||
66 | { | ||
67 | if (m_uMan == null) | ||
68 | m_uMan = m_Scenes[0].RequestModuleInterface<IUserManagement>(); | ||
69 | return m_uMan; | ||
70 | } | ||
71 | } | ||
72 | |||
73 | public virtual void Initialise(IConfigSource config) | ||
74 | { | ||
75 | IConfig cnf = config.Configs["Messaging"]; | ||
76 | if (cnf != null && cnf.GetString( | ||
77 | "MessageTransferModule", "MessageTransferModule") != Name) | ||
78 | { | ||
79 | m_log.Debug("[HG MESSAGE TRANSFER]: Disabled by configuration"); | ||
80 | return; | ||
81 | } | ||
82 | |||
83 | InstantMessageServerConnector imServer = new InstantMessageServerConnector(config, MainServer.Instance, this); | ||
84 | m_IMService = imServer.GetService(); | ||
85 | m_Enabled = true; | ||
86 | } | ||
87 | |||
88 | public virtual void AddRegion(Scene scene) | ||
89 | { | ||
90 | if (!m_Enabled) | ||
91 | return; | ||
92 | |||
93 | lock (m_Scenes) | ||
94 | { | ||
95 | m_log.DebugFormat("[HG MESSAGE TRANSFER]: Message transfer module {0} active", Name); | ||
96 | scene.RegisterModuleInterface<IMessageTransferModule>(this); | ||
97 | m_Scenes.Add(scene); | ||
98 | } | ||
99 | } | ||
100 | |||
101 | public virtual void PostInitialise() | ||
102 | { | ||
103 | if (!m_Enabled) | ||
104 | return; | ||
105 | |||
106 | } | ||
107 | |||
108 | public virtual void RegionLoaded(Scene scene) | ||
109 | { | ||
110 | } | ||
111 | |||
112 | public virtual void RemoveRegion(Scene scene) | ||
113 | { | ||
114 | if (!m_Enabled) | ||
115 | return; | ||
116 | |||
117 | lock (m_Scenes) | ||
118 | { | ||
119 | m_Scenes.Remove(scene); | ||
120 | } | ||
121 | } | ||
122 | |||
123 | public virtual void Close() | ||
124 | { | ||
125 | } | ||
126 | |||
127 | public virtual string Name | ||
128 | { | ||
129 | get { return "HGMessageTransferModule"; } | ||
130 | } | ||
131 | |||
132 | public virtual Type ReplaceableInterface | ||
133 | { | ||
134 | get { return null; } | ||
135 | } | ||
136 | |||
137 | public void SendInstantMessage(GridInstantMessage im, MessageResultNotification result) | ||
138 | { | ||
139 | UUID toAgentID = new UUID(im.toAgentID); | ||
140 | |||
141 | // Try root avatar only first | ||
142 | foreach (Scene scene in m_Scenes) | ||
143 | { | ||
144 | if (scene.Entities.ContainsKey(toAgentID) && | ||
145 | scene.Entities[toAgentID] is ScenePresence) | ||
146 | { | ||
147 | // m_log.DebugFormat( | ||
148 | // "[INSTANT MESSAGE]: Looking for root agent {0} in {1}", | ||
149 | // toAgentID.ToString(), scene.RegionInfo.RegionName); | ||
150 | |||
151 | ScenePresence user = (ScenePresence) scene.Entities[toAgentID]; | ||
152 | if (!user.IsChildAgent) | ||
153 | { | ||
154 | // Local message | ||
155 | // m_log.DebugFormat("[INSTANT MESSAGE]: Delivering IM to root agent {0} {1}", user.Name, toAgentID); | ||
156 | user.ControllingClient.SendInstantMessage(im); | ||
157 | |||
158 | // Message sent | ||
159 | result(true); | ||
160 | return; | ||
161 | } | ||
162 | } | ||
163 | } | ||
164 | |||
165 | // try child avatar second | ||
166 | foreach (Scene scene in m_Scenes) | ||
167 | { | ||
168 | // m_log.DebugFormat( | ||
169 | // "[INSTANT MESSAGE]: Looking for child of {0} in {1}", toAgentID, scene.RegionInfo.RegionName); | ||
170 | |||
171 | if (scene.Entities.ContainsKey(toAgentID) && | ||
172 | scene.Entities[toAgentID] is ScenePresence) | ||
173 | { | ||
174 | // Local message | ||
175 | ScenePresence user = (ScenePresence) scene.Entities[toAgentID]; | ||
176 | |||
177 | // m_log.DebugFormat("[INSTANT MESSAGE]: Delivering IM to child agent {0} {1}", user.Name, toAgentID); | ||
178 | user.ControllingClient.SendInstantMessage(im); | ||
179 | |||
180 | // Message sent | ||
181 | result(true); | ||
182 | return; | ||
183 | } | ||
184 | } | ||
185 | |||
186 | // m_log.DebugFormat("[INSTANT MESSAGE]: Delivering IM to {0} via XMLRPC", im.toAgentID); | ||
187 | // Is the user a local user? | ||
188 | UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, toAgentID); | ||
189 | string url = string.Empty; | ||
190 | bool foreigner = false; | ||
191 | if (account == null) // foreign user | ||
192 | { | ||
193 | url = UserManagementModule.GetUserServerURL(toAgentID, "IMServerURI"); | ||
194 | foreigner = true; | ||
195 | } | ||
196 | |||
197 | Util.FireAndForget(delegate | ||
198 | { | ||
199 | bool success = false; | ||
200 | if (foreigner && url == string.Empty) // we don't know about this user | ||
201 | { | ||
202 | string recipientUUI = TryGetRecipientUUI(new UUID(im.fromAgentID), toAgentID); | ||
203 | m_log.DebugFormat("[HG MESSAGE TRANSFER]: Got UUI {0}", recipientUUI); | ||
204 | if (recipientUUI != string.Empty) | ||
205 | { | ||
206 | UUID id; string u = string.Empty, first = string.Empty, last = string.Empty, secret = string.Empty; | ||
207 | if (Util.ParseUniversalUserIdentifier(recipientUUI, out id, out u, out first, out last, out secret)) | ||
208 | { | ||
209 | success = m_IMService.OutgoingInstantMessage(im, u, true); | ||
210 | if (success) | ||
211 | UserManagementModule.AddUser(toAgentID, u + ";" + first + " " + last); | ||
212 | } | ||
213 | } | ||
214 | } | ||
215 | else | ||
216 | success = m_IMService.OutgoingInstantMessage(im, url, foreigner); | ||
217 | |||
218 | if (!success && !foreigner) | ||
219 | HandleUndeliveredMessage(im, result); | ||
220 | else | ||
221 | result(success); | ||
222 | }); | ||
223 | |||
224 | return; | ||
225 | } | ||
226 | |||
227 | protected bool SendIMToScene(GridInstantMessage gim, UUID toAgentID) | ||
228 | { | ||
229 | bool successful = false; | ||
230 | foreach (Scene scene in m_Scenes) | ||
231 | { | ||
232 | if (scene.Entities.ContainsKey(toAgentID) && | ||
233 | scene.Entities[toAgentID] is ScenePresence) | ||
234 | { | ||
235 | ScenePresence user = | ||
236 | (ScenePresence)scene.Entities[toAgentID]; | ||
237 | |||
238 | if (!user.IsChildAgent) | ||
239 | { | ||
240 | scene.EventManager.TriggerIncomingInstantMessage(gim); | ||
241 | successful = true; | ||
242 | } | ||
243 | } | ||
244 | } | ||
245 | if (!successful) | ||
246 | { | ||
247 | // If the message can't be delivered to an agent, it | ||
248 | // is likely to be a group IM. On a group IM, the | ||
249 | // imSessionID = toAgentID = group id. Raise the | ||
250 | // unhandled IM event to give the groups module | ||
251 | // a chance to pick it up. We raise that in a random | ||
252 | // scene, since the groups module is shared. | ||
253 | // | ||
254 | m_Scenes[0].EventManager.TriggerUnhandledInstantMessage(gim); | ||
255 | } | ||
256 | |||
257 | return successful; | ||
258 | } | ||
259 | |||
260 | protected void HandleUndeliveredMessage(GridInstantMessage im, MessageResultNotification result) | ||
261 | { | ||
262 | UndeliveredMessage handlerUndeliveredMessage = OnUndeliveredMessage; | ||
263 | |||
264 | // If this event has handlers, then an IM from an agent will be | ||
265 | // considered delivered. This will suppress the error message. | ||
266 | // | ||
267 | if (handlerUndeliveredMessage != null) | ||
268 | { | ||
269 | handlerUndeliveredMessage(im); | ||
270 | if (im.dialog == (byte)InstantMessageDialog.MessageFromAgent) | ||
271 | result(true); | ||
272 | else | ||
273 | result(false); | ||
274 | return; | ||
275 | } | ||
276 | |||
277 | //m_log.DebugFormat("[INSTANT MESSAGE]: Undeliverable"); | ||
278 | result(false); | ||
279 | } | ||
280 | |||
281 | private string TryGetRecipientUUI(UUID fromAgent, UUID toAgent) | ||
282 | { | ||
283 | // Let's call back the fromAgent's user agent service | ||
284 | // Maybe that service knows about the toAgent | ||
285 | IClientAPI client = LocateClientObject(fromAgent); | ||
286 | if (client != null) | ||
287 | { | ||
288 | AgentCircuitData circuit = m_Scenes[0].AuthenticateHandler.GetAgentCircuitData(client.AgentId); | ||
289 | if (circuit != null) | ||
290 | { | ||
291 | if (circuit.ServiceURLs.ContainsKey("HomeURI")) | ||
292 | { | ||
293 | string uasURL = circuit.ServiceURLs["HomeURI"].ToString(); | ||
294 | m_log.DebugFormat("[HG MESSAGE TRANSFER]: getting UUI of user {0} from {1}", toAgent, uasURL); | ||
295 | UserAgentServiceConnector uasConn = new UserAgentServiceConnector(uasURL); | ||
296 | return uasConn.GetUUI(fromAgent, toAgent); | ||
297 | } | ||
298 | } | ||
299 | } | ||
300 | |||
301 | return string.Empty; | ||
302 | } | ||
303 | |||
304 | |||
305 | /// <summary> | ||
306 | /// Find the scene for an agent | ||
307 | /// </summary> | ||
308 | private Scene GetClientScene(UUID agentId) | ||
309 | { | ||
310 | lock (m_Scenes) | ||
311 | { | ||
312 | foreach (Scene scene in m_Scenes) | ||
313 | { | ||
314 | ScenePresence presence = scene.GetScenePresence(agentId); | ||
315 | if (presence != null && !presence.IsChildAgent) | ||
316 | return scene; | ||
317 | } | ||
318 | } | ||
319 | |||
320 | return null; | ||
321 | } | ||
322 | |||
323 | /// <summary> | ||
324 | /// Find the client for a ID | ||
325 | /// </summary> | ||
326 | public IClientAPI LocateClientObject(UUID agentID) | ||
327 | { | ||
328 | Scene scene = GetClientScene(agentID); | ||
329 | if (scene != null) | ||
330 | { | ||
331 | ScenePresence presence = scene.GetScenePresence(agentID); | ||
332 | if (presence != null) | ||
333 | return presence.ControllingClient; | ||
334 | } | ||
335 | |||
336 | return null; | ||
337 | } | ||
338 | |||
339 | #region IInstantMessageSimConnector | ||
340 | public bool SendInstantMessage(GridInstantMessage im) | ||
341 | { | ||
342 | //m_log.DebugFormat("[XXX] Hook SendInstantMessage {0}", im.message); | ||
343 | UUID agentID = new UUID(im.toAgentID); | ||
344 | return SendIMToScene(im, agentID); | ||
345 | } | ||
346 | #endregion | ||
347 | |||
348 | |||
349 | } | ||
350 | } | ||