diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/CoreModules/Avatar/InstantMessage/HGMessageTransferModule.cs | 342 |
1 files changed, 342 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..ed02119 --- /dev/null +++ b/OpenSim/Region/CoreModules/Avatar/InstantMessage/HGMessageTransferModule.cs | |||
@@ -0,0 +1,342 @@ | |||
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 | PresenceInfo upd; | ||
191 | if (account == null) // foreign user | ||
192 | url = UserManagementModule.GetUserServerURL(toAgentID, "IMServerURI"); | ||
193 | |||
194 | Util.FireAndForget(delegate | ||
195 | { | ||
196 | bool success = m_IMService.OutgoingInstantMessage(im, url); | ||
197 | if (!success && account == null) | ||
198 | { | ||
199 | // One last chance | ||
200 | string recipientUUI = TryGetRecipientUUI(new UUID(im.fromAgentID), toAgentID); | ||
201 | m_log.DebugFormat("[HG MESSAGE TRANSFER]: Got UUI {0}", recipientUUI); | ||
202 | if (recipientUUI != string.Empty) | ||
203 | { | ||
204 | UUID id; string u = string.Empty, first = string.Empty, last = string.Empty, secret = string.Empty; | ||
205 | if (Util.ParseUniversalUserIdentifier(recipientUUI, out id, out u, out first, out last, out secret)) | ||
206 | { | ||
207 | success = m_IMService.OutgoingInstantMessage(im, u); | ||
208 | if (success) | ||
209 | UserManagementModule.AddUser(toAgentID, u + ";" + first + " " + last); | ||
210 | } | ||
211 | } | ||
212 | } | ||
213 | result(success); | ||
214 | }); | ||
215 | |||
216 | return; | ||
217 | } | ||
218 | |||
219 | protected bool SendIMToScene(GridInstantMessage gim, UUID toAgentID) | ||
220 | { | ||
221 | bool successful = false; | ||
222 | foreach (Scene scene in m_Scenes) | ||
223 | { | ||
224 | if (scene.Entities.ContainsKey(toAgentID) && | ||
225 | scene.Entities[toAgentID] is ScenePresence) | ||
226 | { | ||
227 | ScenePresence user = | ||
228 | (ScenePresence)scene.Entities[toAgentID]; | ||
229 | |||
230 | if (!user.IsChildAgent) | ||
231 | { | ||
232 | scene.EventManager.TriggerIncomingInstantMessage(gim); | ||
233 | successful = true; | ||
234 | } | ||
235 | } | ||
236 | } | ||
237 | if (!successful) | ||
238 | { | ||
239 | // If the message can't be delivered to an agent, it | ||
240 | // is likely to be a group IM. On a group IM, the | ||
241 | // imSessionID = toAgentID = group id. Raise the | ||
242 | // unhandled IM event to give the groups module | ||
243 | // a chance to pick it up. We raise that in a random | ||
244 | // scene, since the groups module is shared. | ||
245 | // | ||
246 | m_Scenes[0].EventManager.TriggerUnhandledInstantMessage(gim); | ||
247 | } | ||
248 | |||
249 | return successful; | ||
250 | } | ||
251 | |||
252 | protected void HandleUndeliveredMessage(GridInstantMessage im, MessageResultNotification result) | ||
253 | { | ||
254 | UndeliveredMessage handlerUndeliveredMessage = OnUndeliveredMessage; | ||
255 | |||
256 | // If this event has handlers, then an IM from an agent will be | ||
257 | // considered delivered. This will suppress the error message. | ||
258 | // | ||
259 | if (handlerUndeliveredMessage != null) | ||
260 | { | ||
261 | handlerUndeliveredMessage(im); | ||
262 | if (im.dialog == (byte)InstantMessageDialog.MessageFromAgent) | ||
263 | result(true); | ||
264 | else | ||
265 | result(false); | ||
266 | return; | ||
267 | } | ||
268 | |||
269 | //m_log.DebugFormat("[INSTANT MESSAGE]: Undeliverable"); | ||
270 | result(false); | ||
271 | } | ||
272 | |||
273 | private string TryGetRecipientUUI(UUID fromAgent, UUID toAgent) | ||
274 | { | ||
275 | // Let's call back the fromAgent's user agent service | ||
276 | // Maybe that service knows about the toAgent | ||
277 | IClientAPI client = LocateClientObject(fromAgent); | ||
278 | if (client != null) | ||
279 | { | ||
280 | AgentCircuitData circuit = m_Scenes[0].AuthenticateHandler.GetAgentCircuitData(client.AgentId); | ||
281 | if (circuit != null) | ||
282 | { | ||
283 | if (circuit.ServiceURLs.ContainsKey("HomeURI")) | ||
284 | { | ||
285 | string uasURL = circuit.ServiceURLs["HomeURI"].ToString(); | ||
286 | m_log.DebugFormat("[HG MESSAGE TRANSFER]: getting UUI of user {0} from {1}", toAgent, uasURL); | ||
287 | UserAgentServiceConnector uasConn = new UserAgentServiceConnector(uasURL); | ||
288 | return uasConn.GetUUI(fromAgent, toAgent); | ||
289 | } | ||
290 | } | ||
291 | } | ||
292 | |||
293 | return string.Empty; | ||
294 | } | ||
295 | |||
296 | |||
297 | /// <summary> | ||
298 | /// Find the scene for an agent | ||
299 | /// </summary> | ||
300 | private Scene GetClientScene(UUID agentId) | ||
301 | { | ||
302 | lock (m_Scenes) | ||
303 | { | ||
304 | foreach (Scene scene in m_Scenes) | ||
305 | { | ||
306 | ScenePresence presence = scene.GetScenePresence(agentId); | ||
307 | if (presence != null && !presence.IsChildAgent) | ||
308 | return scene; | ||
309 | } | ||
310 | } | ||
311 | |||
312 | return null; | ||
313 | } | ||
314 | |||
315 | /// <summary> | ||
316 | /// Find the client for a ID | ||
317 | /// </summary> | ||
318 | public IClientAPI LocateClientObject(UUID agentID) | ||
319 | { | ||
320 | Scene scene = GetClientScene(agentID); | ||
321 | if (scene != null) | ||
322 | { | ||
323 | ScenePresence presence = scene.GetScenePresence(agentID); | ||
324 | if (presence != null) | ||
325 | return presence.ControllingClient; | ||
326 | } | ||
327 | |||
328 | return null; | ||
329 | } | ||
330 | |||
331 | #region IInstantMessageSimConnector | ||
332 | public bool SendInstantMessage(GridInstantMessage im) | ||
333 | { | ||
334 | //m_log.DebugFormat("[XXX] Hook SendInstantMessage {0}", im.message); | ||
335 | UUID agentID = new UUID(im.toAgentID); | ||
336 | return SendIMToScene(im, agentID); | ||
337 | } | ||
338 | #endregion | ||
339 | |||
340 | |||
341 | } | ||
342 | } | ||