aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/OptionalModules/Avatar/XmlRpcGroups/GroupsMessagingModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/OptionalModules/Avatar/XmlRpcGroups/GroupsMessagingModule.cs')
-rw-r--r--OpenSim/Region/OptionalModules/Avatar/XmlRpcGroups/GroupsMessagingModule.cs548
1 files changed, 548 insertions, 0 deletions
diff --git a/OpenSim/Region/OptionalModules/Avatar/XmlRpcGroups/GroupsMessagingModule.cs b/OpenSim/Region/OptionalModules/Avatar/XmlRpcGroups/GroupsMessagingModule.cs
new file mode 100644
index 0000000..8fbda28
--- /dev/null
+++ b/OpenSim/Region/OptionalModules/Avatar/XmlRpcGroups/GroupsMessagingModule.cs
@@ -0,0 +1,548 @@
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
28using System;
29using System.Collections.Generic;
30using System.Reflection;
31
32
33using log4net;
34using Nini.Config;
35
36using OpenMetaverse;
37using OpenMetaverse.StructuredData;
38
39using OpenSim.Framework;
40using OpenSim.Region.CoreModules.Framework.EventQueue;
41using OpenSim.Region.Framework.Interfaces;
42using OpenSim.Region.Framework.Scenes;
43
44
45using Caps = OpenSim.Framework.Capabilities.Caps;
46
47namespace OpenSim.Region.OptionalModules.Avatar.XmlRpcGroups
48{
49 public class GroupsMessagingModule : ISharedRegionModule
50 {
51
52 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
53
54 private List<Scene> m_sceneList = new List<Scene>();
55
56 private IMessageTransferModule m_msgTransferModule = null;
57
58 private IGroupsModule m_groupsModule = null;
59
60 // TODO: Move this off to the xmlrpc server
61 public Dictionary<Guid, List<Guid>> m_agentsInGroupSession = new Dictionary<Guid, List<Guid>>();
62 public Dictionary<Guid, List<Guid>> m_agentsDroppedSession = new Dictionary<Guid, List<Guid>>();
63
64
65 // Config Options
66 private bool m_groupMessagingEnabled = false;
67 private bool m_debugEnabled = true;
68
69 #region IRegionModuleBase Members
70
71 public void Initialise(IConfigSource config)
72 {
73 IConfig groupsConfig = config.Configs["Groups"];
74
75 if (groupsConfig == null)
76 {
77 // Do not run this module by default.
78 return;
79 }
80 else
81 {
82 if (!groupsConfig.GetBoolean("Enabled", false))
83 {
84 return;
85 }
86
87 if (groupsConfig.GetString("Module", "Default") != "XmlRpcGroups")
88 {
89 m_groupMessagingEnabled = false;
90
91 return;
92 }
93
94 m_groupMessagingEnabled = groupsConfig.GetBoolean("XmlRpcMessagingEnabled", true);
95
96 if (!m_groupMessagingEnabled)
97 {
98 return;
99 }
100
101 m_log.Info("[GROUPS-MESSAGING]: Initializing XmlRpcGroupsMessaging");
102
103 m_debugEnabled = groupsConfig.GetBoolean("XmlRpcDebugEnabled", true);
104 }
105
106 m_log.Info("[GROUPS-MESSAGING]: XmlRpcGroupsMessaging starting up");
107
108 }
109
110 public void AddRegion(Scene scene)
111 {
112 // NoOp
113 }
114 public void RegionLoaded(Scene scene)
115 {
116 if (!m_groupMessagingEnabled)
117 return;
118
119 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
120
121 m_groupsModule = scene.RequestModuleInterface<IGroupsModule>();
122
123 // No groups module, no groups messaging
124 if (m_groupsModule == null)
125 {
126 m_log.Error("[GROUPS-MESSAGING]: Could not get IGroupsModule, XmlRpcGroupsMessaging is now disabled.");
127 Close();
128 m_groupMessagingEnabled = false;
129 return;
130 }
131
132 m_msgTransferModule = scene.RequestModuleInterface<IMessageTransferModule>();
133
134 // No message transfer module, no groups messaging
135 if (m_msgTransferModule == null)
136 {
137 m_log.Error("[GROUPS-MESSAGING]: Could not get MessageTransferModule");
138 Close();
139 m_groupMessagingEnabled = false;
140 return;
141 }
142
143
144 m_sceneList.Add(scene);
145
146 scene.EventManager.OnNewClient += OnNewClient;
147 scene.EventManager.OnIncomingInstantMessage += OnGridInstantMessage;
148
149 }
150
151 public void RemoveRegion(Scene scene)
152 {
153 if (!m_groupMessagingEnabled)
154 return;
155
156 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
157
158 m_sceneList.Remove(scene);
159 }
160
161 public void Close()
162 {
163 if (!m_groupMessagingEnabled)
164 return;
165
166 if (m_debugEnabled) m_log.Debug("[GROUPS-MESSAGING]: Shutting down XmlRpcGroupsMessaging module.");
167
168 foreach (Scene scene in m_sceneList)
169 {
170 scene.EventManager.OnNewClient -= OnNewClient;
171 scene.EventManager.OnIncomingInstantMessage -= OnGridInstantMessage;
172 }
173
174 m_sceneList.Clear();
175
176 m_groupsModule = null;
177 m_msgTransferModule = null;
178 }
179
180 public Type ReplacableInterface
181 {
182 get { return null; }
183 }
184
185 public string Name
186 {
187 get { return "XmlRpcGroupsMessaging"; }
188 }
189
190 #endregion
191
192 #region ISharedRegionModule Members
193
194 public void PostInitialise()
195 {
196 // NoOp
197 }
198
199 #endregion
200
201 #region SimGridEventHandlers
202
203 private void OnNewClient(IClientAPI client)
204 {
205 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: OnInstantMessage registered for {0}", client.Name);
206
207 client.OnInstantMessage += OnInstantMessage;
208 }
209
210 private void OnGridInstantMessage(GridInstantMessage msg)
211 {
212 // The instant message module will only deliver messages of dialog types:
213 // MessageFromAgent, StartTyping, StopTyping, MessageFromObject
214 //
215 // Any other message type will not be delivered to a client by the
216 // Instant Message Module
217
218
219 if (m_debugEnabled)
220 {
221 m_log.DebugFormat("[GROUPS-MESSAGING]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
222
223 DebugGridInstantMessage(msg);
224 }
225
226 // Incoming message from a group
227 if ((msg.fromGroup == true) &&
228 ((msg.dialog == (byte)InstantMessageDialog.SessionSend)
229 || (msg.dialog == (byte)InstantMessageDialog.SessionAdd)
230 || (msg.dialog == (byte)InstantMessageDialog.SessionDrop)))
231 {
232 ProcessMessageFromGroupSession(msg);
233 }
234 }
235
236 private void ProcessMessageFromGroupSession(GridInstantMessage msg)
237 {
238 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: Session message from {0} going to agent {1}", msg.fromAgentName, msg.toAgentID);
239
240 switch (msg.dialog)
241 {
242 case (byte)InstantMessageDialog.SessionAdd:
243 AddAgentToGroupSession(msg.fromAgentID, msg.imSessionID);
244 break;
245
246 case (byte)InstantMessageDialog.SessionDrop:
247 RemoveAgentFromGroupSession(msg.fromAgentID, msg.imSessionID);
248 break;
249
250 case (byte)InstantMessageDialog.SessionSend:
251 if (!m_agentsInGroupSession.ContainsKey(msg.toAgentID)
252 && !m_agentsDroppedSession.ContainsKey(msg.toAgentID))
253 {
254 // Agent not in session and hasn't dropped from session
255 // Add them to the session for now, and Invite them
256 AddAgentToGroupSession(msg.toAgentID, msg.imSessionID);
257
258 UUID toAgentID = new UUID(msg.toAgentID);
259 IClientAPI activeClient = GetActiveClient(toAgentID);
260 if (activeClient != null)
261 {
262 UUID groupID = new UUID(msg.fromAgentID);
263
264 GroupRecord groupInfo = m_groupsModule.GetGroupRecord(groupID);
265 if (groupInfo != null)
266 {
267 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: Sending chatterbox invite instant message");
268
269 // Force? open the group session dialog???
270 IEventQueue eq = activeClient.Scene.RequestModuleInterface<IEventQueue>();
271 eq.ChatterboxInvitation(
272 groupID
273 , groupInfo.GroupName
274 , new UUID(msg.fromAgentID)
275 , msg.message, new UUID(msg.toAgentID)
276 , msg.fromAgentName
277 , msg.dialog
278 , msg.timestamp
279 , msg.offline == 1
280 , (int)msg.ParentEstateID
281 , msg.Position
282 , 1
283 , new UUID(msg.imSessionID)
284 , msg.fromGroup
285 , Utils.StringToBytes(groupInfo.GroupName)
286 );
287
288 eq.ChatterBoxSessionAgentListUpdates(
289 new UUID(groupID)
290 , new UUID(msg.fromAgentID)
291 , new UUID(msg.toAgentID)
292 , false //canVoiceChat
293 , false //isModerator
294 , false //text mute
295 );
296 }
297 }
298 }
299 else if (!m_agentsDroppedSession.ContainsKey(msg.toAgentID))
300 {
301 // User hasn't dropped, so they're in the session,
302 // maybe we should deliver it.
303 IClientAPI client = GetActiveClient(new UUID(msg.toAgentID));
304 if (client != null)
305 {
306 // Deliver locally, directly
307 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: Delivering to {0} locally", client.Name);
308 client.SendInstantMessage(msg);
309 }
310 else
311 {
312 m_log.WarnFormat("[GROUPS-MESSAGING]: Received a message over the grid for a client that isn't here: {0}", msg.toAgentID);
313 }
314 }
315 break;
316
317 default:
318 m_log.WarnFormat("[GROUPS-MESSAGING]: I don't know how to proccess a {0} message.", ((InstantMessageDialog)msg.dialog).ToString());
319 break;
320 }
321 }
322
323 #endregion
324
325 #region ClientEvents
326
327 private void RemoveAgentFromGroupSession(Guid agentID, Guid sessionID)
328 {
329 if (m_agentsInGroupSession.ContainsKey(sessionID))
330 {
331 // If in session remove
332 if (m_agentsInGroupSession[sessionID].Contains(agentID))
333 {
334 m_agentsInGroupSession[sessionID].Remove(agentID);
335 }
336
337 // If not in dropped list, add
338 if (!m_agentsDroppedSession[sessionID].Contains(agentID))
339 {
340 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: Dropped {1} from session {0}", sessionID, agentID);
341 m_agentsDroppedSession[sessionID].Add(agentID);
342 }
343 }
344 }
345
346 private void AddAgentToGroupSession(Guid agentID, Guid sessionID)
347 {
348 // Add Session Status if it doesn't exist for this session
349 CreateGroupSessionTracking(sessionID);
350
351 // If nessesary, remove from dropped list
352 if (m_agentsDroppedSession[sessionID].Contains(agentID))
353 {
354 m_agentsDroppedSession[sessionID].Remove(agentID);
355 }
356
357 // If nessesary, add to in session list
358 if (!m_agentsInGroupSession[sessionID].Contains(agentID))
359 {
360 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: Added {1} to session {0}", sessionID, agentID);
361 m_agentsInGroupSession[sessionID].Add(agentID);
362 }
363 }
364
365 private void CreateGroupSessionTracking(Guid sessionID)
366 {
367 if (!m_agentsInGroupSession.ContainsKey(sessionID))
368 {
369 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: Creating session tracking for : {0}", sessionID);
370 m_agentsInGroupSession.Add(sessionID, new List<Guid>());
371 m_agentsDroppedSession.Add(sessionID, new List<Guid>());
372 }
373 }
374
375 private void OnInstantMessage(IClientAPI remoteClient, GridInstantMessage im)
376 {
377 if (m_debugEnabled)
378 {
379 m_log.DebugFormat("[GROUPS-MESSAGING]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
380
381 DebugGridInstantMessage(im);
382 }
383
384 // Start group IM session
385 if ((im.dialog == (byte)InstantMessageDialog.SessionGroupStart))
386 {
387 UUID groupID = new UUID(im.toAgentID);
388
389 GroupRecord groupInfo = m_groupsModule.GetGroupRecord(groupID);
390 if (groupInfo != null)
391 {
392 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: Start Group Session for {0}", groupInfo.GroupName);
393
394 AddAgentToGroupSession(im.fromAgentID, im.imSessionID);
395
396 ChatterBoxSessionStartReplyViaCaps(remoteClient, groupInfo.GroupName, groupID);
397
398 IEventQueue queue = remoteClient.Scene.RequestModuleInterface<IEventQueue>();
399 queue.ChatterBoxSessionAgentListUpdates(
400 new UUID(groupID)
401 , new UUID(im.fromAgentID)
402 , new UUID(im.toAgentID)
403 , false //canVoiceChat
404 , false //isModerator
405 , false //text mute
406 );
407 }
408 }
409
410 // Send a message from locally connected client to a group
411 if ((im.dialog == (byte)InstantMessageDialog.SessionSend))
412 {
413 UUID groupID = new UUID(im.toAgentID);
414
415 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: Send message to session for group {0} with session ID {1}", groupID, im.imSessionID.ToString());
416
417 SendMessageToGroup(im, groupID);
418 }
419 }
420
421 #endregion
422
423 private void SendMessageToGroup(GridInstantMessage im, UUID groupID)
424 {
425 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
426
427 foreach (GroupMembersData member in m_groupsModule.GroupMembersRequest(null, groupID))
428 {
429 if (m_agentsDroppedSession[im.imSessionID].Contains(member.AgentID.Guid))
430 {
431 // Don't deliver messages to people who have dropped this session
432 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: {0} has dropped session, not delivering to them", member.AgentID);
433 continue;
434 }
435
436 // Copy Message
437 GridInstantMessage msg = new GridInstantMessage();
438 msg.imSessionID = im.imSessionID;
439 msg.fromAgentName = im.fromAgentName;
440 msg.message = im.message;
441 msg.dialog = im.dialog;
442 msg.offline = im.offline;
443 msg.ParentEstateID = im.ParentEstateID;
444 msg.Position = im.Position;
445 msg.RegionID = im.RegionID;
446 msg.binaryBucket = im.binaryBucket;
447 msg.timestamp = (uint)Util.UnixTimeSinceEpoch();
448
449 // Updat Pertinate fields to make it a "group message"
450 msg.fromAgentID = groupID.Guid;
451 msg.fromGroup = true;
452
453 msg.toAgentID = member.AgentID.Guid;
454
455 IClientAPI client = GetActiveClient(member.AgentID);
456 if (client == null)
457 {
458 // If they're not local, forward across the grid
459 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: Delivering to {0} via Grid", member.AgentID);
460 m_msgTransferModule.SendInstantMessage(msg, delegate(bool success) { });
461 }
462 else
463 {
464 // Deliver locally, directly
465 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: Passing to ProcessMessageFromGroupSession to deliver to {0} locally", client.Name);
466 ProcessMessageFromGroupSession(msg);
467 }
468 }
469 }
470
471 void ChatterBoxSessionStartReplyViaCaps(IClientAPI remoteClient, string groupName, UUID groupID)
472 {
473 if (m_debugEnabled) m_log.DebugFormat("[GROUPS-MESSAGING]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
474
475 OSDMap moderatedMap = new OSDMap(4);
476 moderatedMap.Add("voice", OSD.FromBoolean(false));
477
478 OSDMap sessionMap = new OSDMap(4);
479 sessionMap.Add("moderated_mode", moderatedMap);
480 sessionMap.Add("session_name", OSD.FromString(groupName));
481 sessionMap.Add("type", OSD.FromInteger(0));
482 sessionMap.Add("voice_enabled", OSD.FromBoolean(false));
483
484 OSDMap bodyMap = new OSDMap(4);
485 bodyMap.Add("session_id", OSD.FromUUID(groupID));
486 bodyMap.Add("temp_session_id", OSD.FromUUID(groupID));
487 bodyMap.Add("success", OSD.FromBoolean(true));
488 bodyMap.Add("session_info", sessionMap);
489
490 IEventQueue queue = remoteClient.Scene.RequestModuleInterface<IEventQueue>();
491
492 if (queue != null)
493 {
494 queue.Enqueue(EventQueueHelper.buildEvent("ChatterBoxSessionStartReply", bodyMap), remoteClient.AgentId);
495 }
496 }
497
498 private void DebugGridInstantMessage(GridInstantMessage im)
499 {
500 // Don't log any normal IMs (privacy!)
501 if (m_debugEnabled && im.dialog != (byte)InstantMessageDialog.MessageFromAgent)
502 {
503 m_log.WarnFormat("[GROUPS-MESSAGING]: IM: fromGroup({0})", im.fromGroup ? "True" : "False");
504 m_log.WarnFormat("[GROUPS-MESSAGING]: IM: Dialog({0})", ((InstantMessageDialog)im.dialog).ToString());
505 m_log.WarnFormat("[GROUPS-MESSAGING]: IM: fromAgentID({0})", im.fromAgentID.ToString());
506 m_log.WarnFormat("[GROUPS-MESSAGING]: IM: fromAgentName({0})", im.fromAgentName.ToString());
507 m_log.WarnFormat("[GROUPS-MESSAGING]: IM: imSessionID({0})", im.imSessionID.ToString());
508 m_log.WarnFormat("[GROUPS-MESSAGING]: IM: message({0})", im.message.ToString());
509 m_log.WarnFormat("[GROUPS-MESSAGING]: IM: offline({0})", im.offline.ToString());
510 m_log.WarnFormat("[GROUPS-MESSAGING]: IM: toAgentID({0})", im.toAgentID.ToString());
511 m_log.WarnFormat("[GROUPS-MESSAGING]: IM: binaryBucket({0})", OpenMetaverse.Utils.BytesToHexString(im.binaryBucket, "BinaryBucket"));
512 }
513 }
514
515 #region Client Tools
516
517 /// <summary>
518 /// Try to find an active IClientAPI reference for agentID giving preference to root connections
519 /// </summary>
520 private IClientAPI GetActiveClient(UUID agentID)
521 {
522 IClientAPI child = null;
523
524 // Try root avatar first
525 foreach (Scene scene in m_sceneList)
526 {
527 if (scene.Entities.ContainsKey(agentID) &&
528 scene.Entities[agentID] is ScenePresence)
529 {
530 ScenePresence user = (ScenePresence)scene.Entities[agentID];
531 if (!user.IsChildAgent)
532 {
533 return user.ControllingClient;
534 }
535 else
536 {
537 child = user.ControllingClient;
538 }
539 }
540 }
541
542 // If we didn't find a root, then just return whichever child we found, or null if none
543 return child;
544 }
545
546 #endregion
547 }
548}