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