opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 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.Linq; |
||
31 | using System.Reflection; |
||
32 | using log4net; |
||
33 | using Mono.Addins; |
||
34 | using Nini.Config; |
||
35 | using OpenMetaverse; |
||
36 | using OpenMetaverse.StructuredData; |
||
37 | using OpenSim.Framework; |
||
38 | using OpenSim.Region.Framework.Interfaces; |
||
39 | using OpenSim.Region.Framework.Scenes; |
||
40 | using OpenSim.Services.Interfaces; |
||
41 | using PresenceInfo = OpenSim.Services.Interfaces.PresenceInfo; |
||
42 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; |
||
43 | |||
44 | namespace OpenSim.Groups |
||
45 | { |
||
46 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "GroupsMessagingModule")] |
||
47 | public class GroupsMessagingModule : ISharedRegionModule, IGroupsMessagingModule |
||
48 | { |
||
49 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
50 | |||
51 | private List<Scene> m_sceneList = new List<Scene>(); |
||
52 | private IPresenceService m_presenceService; |
||
53 | |||
54 | private IMessageTransferModule m_msgTransferModule = null; |
||
55 | private IUserManagement m_UserManagement = null; |
||
56 | private IGroupsServicesConnector m_groupData = null; |
||
57 | |||
58 | // Config Options |
||
59 | private bool m_groupMessagingEnabled = false; |
||
60 | private bool m_debugEnabled = true; |
||
61 | |||
62 | /// <summary> |
||
63 | /// If enabled, module only tries to send group IMs to online users by querying cached presence information. |
||
64 | /// </summary> |
||
65 | private bool m_messageOnlineAgentsOnly; |
||
66 | |||
67 | /// <summary> |
||
68 | /// Cache for online users. |
||
69 | /// </summary> |
||
70 | /// <remarks> |
||
71 | /// Group ID is key, presence information for online members is value. |
||
72 | /// Will only be non-null if m_messageOnlineAgentsOnly = true |
||
73 | /// We cache here so that group messages don't constantly have to re-request the online user list to avoid |
||
74 | /// attempted expensive sending of messages to offline users. |
||
75 | /// The tradeoff is that a user that comes online will not receive messages consistently from all other users |
||
76 | /// until caches have updated. |
||
77 | /// Therefore, we set the cache expiry to just 20 seconds. |
||
78 | /// </remarks> |
||
79 | private ExpiringCache<UUID, PresenceInfo[]> m_usersOnlineCache; |
||
80 | |||
81 | private int m_usersOnlineCacheExpirySeconds = 20; |
||
82 | |||
83 | private Dictionary<UUID, List<string>> m_groupsAgentsDroppedFromChatSession = new Dictionary<UUID, List<string>>(); |
||
84 | private Dictionary<UUID, List<string>> m_groupsAgentsInvitedToChatSession = new Dictionary<UUID, List<string>>(); |
||
85 | |||
86 | |||
87 | #region Region Module interfaceBase Members |
||
88 | |||
89 | public void Initialise(IConfigSource config) |
||
90 | { |
||
91 | IConfig groupsConfig = config.Configs["Groups"]; |
||
92 | |||
93 | if (groupsConfig == null) |
||
94 | // Do not run this module by default. |
||
95 | return; |
||
96 | |||
97 | // if groups aren't enabled, we're not needed. |
||
98 | // if we're not specified as the connector to use, then we're not wanted |
||
99 | if ((groupsConfig.GetBoolean("Enabled", false) == false) |
||
100 | || (groupsConfig.GetString("MessagingModule", "") != Name)) |
||
101 | { |
||
102 | m_groupMessagingEnabled = false; |
||
103 | return; |
||
104 | } |
||
105 | |||
106 | m_groupMessagingEnabled = groupsConfig.GetBoolean("MessagingEnabled", true); |
||
107 | |||
108 | if (!m_groupMessagingEnabled) |
||
109 | return; |
||
110 | |||
111 | m_messageOnlineAgentsOnly = groupsConfig.GetBoolean("MessageOnlineUsersOnly", false); |
||
112 | |||
113 | if (m_messageOnlineAgentsOnly) |
||
114 | m_usersOnlineCache = new ExpiringCache<UUID, PresenceInfo[]>(); |
||
115 | |||
116 | m_debugEnabled = groupsConfig.GetBoolean("DebugEnabled", true); |
||
117 | |||
118 | m_log.InfoFormat( |
||
119 | "[Groups.Messaging]: GroupsMessagingModule enabled with MessageOnlineOnly = {0}, DebugEnabled = {1}", |
||
120 | m_messageOnlineAgentsOnly, m_debugEnabled); |
||
121 | } |
||
122 | |||
123 | public void AddRegion(Scene scene) |
||
124 | { |
||
125 | if (!m_groupMessagingEnabled) |
||
126 | return; |
||
127 | |||
128 | scene.RegisterModuleInterface<IGroupsMessagingModule>(this); |
||
129 | m_sceneList.Add(scene); |
||
130 | |||
131 | scene.EventManager.OnNewClient += OnNewClient; |
||
132 | scene.EventManager.OnMakeRootAgent += OnMakeRootAgent; |
||
133 | scene.EventManager.OnMakeChildAgent += OnMakeChildAgent; |
||
134 | scene.EventManager.OnIncomingInstantMessage += OnGridInstantMessage; |
||
135 | scene.EventManager.OnClientLogin += OnClientLogin; |
||
136 | } |
||
137 | |||
138 | public void RegionLoaded(Scene scene) |
||
139 | { |
||
140 | if (!m_groupMessagingEnabled) |
||
141 | return; |
||
142 | |||
143 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name); |
||
144 | |||
145 | m_groupData = scene.RequestModuleInterface<IGroupsServicesConnector>(); |
||
146 | |||
147 | // No groups module, no groups messaging |
||
148 | if (m_groupData == null) |
||
149 | { |
||
150 | m_log.Error("[Groups.Messaging]: Could not get IGroupsServicesConnector, GroupsMessagingModule is now disabled."); |
||
151 | RemoveRegion(scene); |
||
152 | return; |
||
153 | } |
||
154 | |||
155 | m_msgTransferModule = scene.RequestModuleInterface<IMessageTransferModule>(); |
||
156 | |||
157 | // No message transfer module, no groups messaging |
||
158 | if (m_msgTransferModule == null) |
||
159 | { |
||
160 | m_log.Error("[Groups.Messaging]: Could not get MessageTransferModule"); |
||
161 | RemoveRegion(scene); |
||
162 | return; |
||
163 | } |
||
164 | |||
165 | m_UserManagement = scene.RequestModuleInterface<IUserManagement>(); |
||
166 | |||
167 | // No groups module, no groups messaging |
||
168 | if (m_UserManagement == null) |
||
169 | { |
||
170 | m_log.Error("[Groups.Messaging]: Could not get IUserManagement, GroupsMessagingModule is now disabled."); |
||
171 | RemoveRegion(scene); |
||
172 | return; |
||
173 | } |
||
174 | |||
175 | |||
176 | if (m_presenceService == null) |
||
177 | m_presenceService = scene.PresenceService; |
||
178 | |||
179 | } |
||
180 | |||
181 | public void RemoveRegion(Scene scene) |
||
182 | { |
||
183 | if (!m_groupMessagingEnabled) |
||
184 | return; |
||
185 | |||
186 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name); |
||
187 | |||
188 | m_sceneList.Remove(scene); |
||
189 | scene.EventManager.OnNewClient -= OnNewClient; |
||
190 | scene.EventManager.OnIncomingInstantMessage -= OnGridInstantMessage; |
||
191 | scene.EventManager.OnClientLogin -= OnClientLogin; |
||
192 | scene.UnregisterModuleInterface<IGroupsMessagingModule>(this); |
||
193 | } |
||
194 | |||
195 | public void Close() |
||
196 | { |
||
197 | if (!m_groupMessagingEnabled) |
||
198 | return; |
||
199 | |||
200 | if (m_debugEnabled) m_log.Debug("[Groups.Messaging]: Shutting down GroupsMessagingModule module."); |
||
201 | |||
202 | m_sceneList.Clear(); |
||
203 | |||
204 | m_groupData = null; |
||
205 | m_msgTransferModule = null; |
||
206 | } |
||
207 | |||
208 | public Type ReplaceableInterface |
||
209 | { |
||
210 | get { return null; } |
||
211 | } |
||
212 | |||
213 | public string Name |
||
214 | { |
||
215 | get { return "Groups Messaging Module V2"; } |
||
216 | } |
||
217 | |||
218 | public void PostInitialise() |
||
219 | { |
||
220 | // NoOp |
||
221 | } |
||
222 | |||
223 | #endregion |
||
224 | |||
225 | |||
226 | /// <summary> |
||
227 | /// Not really needed, but does confirm that the group exists. |
||
228 | /// </summary> |
||
229 | public bool StartGroupChatSession(UUID agentID, UUID groupID) |
||
230 | { |
||
231 | if (m_debugEnabled) |
||
232 | m_log.DebugFormat("[Groups.Messaging]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name); |
||
233 | |||
234 | GroupRecord groupInfo = m_groupData.GetGroupRecord(agentID.ToString(), groupID, null); |
||
235 | |||
236 | if (groupInfo != null) |
||
237 | { |
||
238 | return true; |
||
239 | } |
||
240 | else |
||
241 | { |
||
242 | return false; |
||
243 | } |
||
244 | } |
||
245 | |||
246 | public void SendMessageToGroup(GridInstantMessage im, UUID groupID) |
||
247 | { |
||
248 | UUID fromAgentID = new UUID(im.fromAgentID); |
||
249 | List<GroupMembersData> groupMembers = m_groupData.GetGroupMembers(UUID.Zero.ToString(), groupID); |
||
250 | int groupMembersCount = groupMembers.Count; |
||
251 | PresenceInfo[] onlineAgents = null; |
||
252 | |||
253 | // In V2 we always only send to online members. |
||
254 | // Sending to offline members is not an option. |
||
255 | string[] t1 = groupMembers.ConvertAll<string>(gmd => gmd.AgentID.ToString()).ToArray(); |
||
256 | |||
257 | // We cache in order not to overwhlem the presence service on large grids with many groups. This does |
||
258 | // mean that members coming online will not see all group members until after m_usersOnlineCacheExpirySeconds has elapsed. |
||
259 | // (assuming this is the same across all grid simulators). |
||
260 | if (!m_usersOnlineCache.TryGetValue(groupID, out onlineAgents)) |
||
261 | { |
||
262 | onlineAgents = m_presenceService.GetAgents(t1); |
||
263 | m_usersOnlineCache.Add(groupID, onlineAgents, m_usersOnlineCacheExpirySeconds); |
||
264 | } |
||
265 | |||
266 | HashSet<string> onlineAgentsUuidSet = new HashSet<string>(); |
||
267 | Array.ForEach<PresenceInfo>(onlineAgents, pi => onlineAgentsUuidSet.Add(pi.UserID)); |
||
268 | |||
269 | groupMembers = groupMembers.Where(gmd => onlineAgentsUuidSet.Contains(gmd.AgentID.ToString())).ToList(); |
||
270 | |||
271 | // if (m_debugEnabled) |
||
272 | // m_log.DebugFormat( |
||
273 | // "[Groups.Messaging]: SendMessageToGroup called for group {0} with {1} visible members, {2} online", |
||
274 | // groupID, groupMembersCount, groupMembers.Count()); |
||
275 | |||
276 | int requestStartTick = Environment.TickCount; |
||
277 | |||
278 | im.imSessionID = groupID.Guid; |
||
279 | im.fromGroup = true; |
||
280 | IClientAPI thisClient = GetActiveClient(fromAgentID); |
||
281 | if (thisClient != null) |
||
282 | { |
||
283 | im.RegionID = thisClient.Scene.RegionInfo.RegionID.Guid; |
||
284 | } |
||
285 | |||
286 | // Send to self first of all |
||
287 | im.toAgentID = im.fromAgentID; |
||
288 | im.fromGroup = true; |
||
289 | ProcessMessageFromGroupSession(im); |
||
290 | |||
291 | List<UUID> regions = new List<UUID>(); |
||
292 | List<UUID> clientsAlreadySent = new List<UUID>(); |
||
293 | |||
294 | // Then send to everybody else |
||
295 | foreach (GroupMembersData member in groupMembers) |
||
296 | { |
||
297 | if (member.AgentID.Guid == im.fromAgentID) |
||
298 | continue; |
||
299 | |||
300 | if (clientsAlreadySent.Contains(member.AgentID)) |
||
301 | continue; |
||
302 | clientsAlreadySent.Add(member.AgentID); |
||
303 | |||
304 | if (hasAgentDroppedGroupChatSession(member.AgentID.ToString(), groupID)) |
||
305 | { |
||
306 | // Don't deliver messages to people who have dropped this session |
||
307 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: {0} has dropped session, not delivering to them", member.AgentID); |
||
308 | continue; |
||
309 | } |
||
310 | |||
311 | im.toAgentID = member.AgentID.Guid; |
||
312 | |||
313 | IClientAPI client = GetActiveClient(member.AgentID); |
||
314 | if (client == null) |
||
315 | { |
||
316 | // If they're not local, forward across the grid |
||
317 | // BUT do it only once per region, please! Sim would be even better! |
||
318 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: Delivering to {0} via Grid", member.AgentID); |
||
319 | |||
320 | bool reallySend = true; |
||
321 | if (onlineAgents != null) |
||
322 | { |
||
323 | PresenceInfo presence = onlineAgents.First(p => p.UserID == member.AgentID.ToString()); |
||
324 | if (regions.Contains(presence.RegionID)) |
||
325 | reallySend = false; |
||
326 | else |
||
327 | regions.Add(presence.RegionID); |
||
328 | } |
||
329 | |||
330 | if (reallySend) |
||
331 | { |
||
332 | // We have to create a new IM structure because the transfer module |
||
333 | // uses async send |
||
334 | GridInstantMessage msg = new GridInstantMessage(im, true); |
||
335 | m_msgTransferModule.SendInstantMessage(msg, delegate(bool success) { }); |
||
336 | } |
||
337 | } |
||
338 | else |
||
339 | { |
||
340 | // Deliver locally, directly |
||
341 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: Passing to ProcessMessageFromGroupSession to deliver to {0} locally", client.Name); |
||
342 | |||
343 | ProcessMessageFromGroupSession(im); |
||
344 | } |
||
345 | |||
346 | } |
||
347 | |||
348 | if (m_debugEnabled) |
||
349 | m_log.DebugFormat( |
||
350 | "[Groups.Messaging]: SendMessageToGroup for group {0} with {1} visible members, {2} online took {3}ms", |
||
351 | groupID, groupMembersCount, groupMembers.Count(), Environment.TickCount - requestStartTick); |
||
352 | } |
||
353 | |||
354 | #region SimGridEventHandlers |
||
355 | |||
356 | void OnClientLogin(IClientAPI client) |
||
357 | { |
||
358 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: OnInstantMessage registered for {0}", client.Name); |
||
359 | } |
||
360 | |||
361 | private void OnNewClient(IClientAPI client) |
||
362 | { |
||
363 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: OnInstantMessage registered for {0}", client.Name); |
||
364 | |||
365 | ResetAgentGroupChatSessions(client.AgentId.ToString()); |
||
366 | } |
||
367 | |||
368 | void OnMakeRootAgent(ScenePresence sp) |
||
369 | { |
||
370 | sp.ControllingClient.OnInstantMessage += OnInstantMessage; |
||
371 | } |
||
372 | |||
373 | void OnMakeChildAgent(ScenePresence sp) |
||
374 | { |
||
375 | sp.ControllingClient.OnInstantMessage -= OnInstantMessage; |
||
376 | } |
||
377 | |||
378 | |||
379 | private void OnGridInstantMessage(GridInstantMessage msg) |
||
380 | { |
||
381 | // The instant message module will only deliver messages of dialog types: |
||
382 | // MessageFromAgent, StartTyping, StopTyping, MessageFromObject |
||
383 | // |
||
384 | // Any other message type will not be delivered to a client by the |
||
385 | // Instant Message Module |
||
386 | |||
387 | UUID regionID = new UUID(msg.RegionID); |
||
388 | if (m_debugEnabled) |
||
389 | { |
||
390 | m_log.DebugFormat("[Groups.Messaging]: {0} called, IM from region {1}", |
||
391 | System.Reflection.MethodBase.GetCurrentMethod().Name, regionID); |
||
392 | |||
393 | DebugGridInstantMessage(msg); |
||
394 | } |
||
395 | |||
396 | // Incoming message from a group |
||
397 | if ((msg.fromGroup == true) && (msg.dialog == (byte)InstantMessageDialog.SessionSend)) |
||
398 | { |
||
399 | // We have to redistribute the message across all members of the group who are here |
||
400 | // on this sim |
||
401 | |||
402 | UUID GroupID = new UUID(msg.imSessionID); |
||
403 | |||
404 | Scene aScene = m_sceneList[0]; |
||
405 | GridRegion regionOfOrigin = aScene.GridService.GetRegionByUUID(aScene.RegionInfo.ScopeID, regionID); |
||
406 | |||
407 | List<GroupMembersData> groupMembers = m_groupData.GetGroupMembers(UUID.Zero.ToString(), GroupID); |
||
408 | |||
409 | //if (m_debugEnabled) |
||
410 | // foreach (GroupMembersData m in groupMembers) |
||
411 | // m_log.DebugFormat("[Groups.Messaging]: member {0}", m.AgentID); |
||
412 | |||
413 | foreach (Scene s in m_sceneList) |
||
414 | { |
||
415 | s.ForEachScenePresence(sp => |
||
416 | { |
||
417 | // If we got this via grid messaging, it's because the caller thinks |
||
418 | // that the root agent is here. We should only send the IM to root agents. |
||
419 | if (sp.IsChildAgent) |
||
420 | return; |
||
421 | |||
422 | GroupMembersData m = groupMembers.Find(gmd => |
||
423 | { |
||
424 | return gmd.AgentID == sp.UUID; |
||
425 | }); |
||
426 | if (m.AgentID == UUID.Zero) |
||
427 | { |
||
428 | if (m_debugEnabled) |
||
429 | m_log.DebugFormat("[Groups.Messaging]: skipping agent {0} because he is not a member of the group", sp.UUID); |
||
430 | return; |
||
431 | } |
||
432 | |||
433 | // Check if the user has an agent in the region where |
||
434 | // the IM came from, and if so, skip it, because the IM |
||
435 | // was already sent via that agent |
||
436 | if (regionOfOrigin != null) |
||
437 | { |
||
438 | AgentCircuitData aCircuit = s.AuthenticateHandler.GetAgentCircuitData(sp.UUID); |
||
439 | if (aCircuit != null) |
||
440 | { |
||
441 | if (aCircuit.ChildrenCapSeeds.Keys.Contains(regionOfOrigin.RegionHandle)) |
||
442 | { |
||
443 | if (m_debugEnabled) |
||
444 | m_log.DebugFormat("[Groups.Messaging]: skipping agent {0} because he has an agent in region of origin", sp.UUID); |
||
445 | return; |
||
446 | } |
||
447 | else |
||
448 | { |
||
449 | if (m_debugEnabled) |
||
450 | m_log.DebugFormat("[Groups.Messaging]: not skipping agent {0}", sp.UUID); |
||
451 | } |
||
452 | } |
||
453 | } |
||
454 | |||
455 | UUID AgentID = sp.UUID; |
||
456 | msg.toAgentID = AgentID.Guid; |
||
457 | |||
458 | if (!hasAgentDroppedGroupChatSession(AgentID.ToString(), GroupID)) |
||
459 | { |
||
460 | if (!hasAgentBeenInvitedToGroupChatSession(AgentID.ToString(), GroupID)) |
||
461 | AddAgentToSession(AgentID, GroupID, msg); |
||
462 | else |
||
463 | { |
||
464 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: Passing to ProcessMessageFromGroupSession to deliver to {0} locally", sp.Name); |
||
465 | |||
466 | ProcessMessageFromGroupSession(msg); |
||
467 | } |
||
468 | } |
||
469 | }); |
||
470 | |||
471 | } |
||
472 | } |
||
473 | } |
||
474 | |||
475 | private void ProcessMessageFromGroupSession(GridInstantMessage msg) |
||
476 | { |
||
477 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: Session message from {0} going to agent {1}", msg.fromAgentName, msg.toAgentID); |
||
478 | |||
479 | UUID AgentID = new UUID(msg.fromAgentID); |
||
480 | UUID GroupID = new UUID(msg.imSessionID); |
||
481 | UUID toAgentID = new UUID(msg.toAgentID); |
||
482 | |||
483 | switch (msg.dialog) |
||
484 | { |
||
485 | case (byte)InstantMessageDialog.SessionAdd: |
||
486 | AgentInvitedToGroupChatSession(AgentID.ToString(), GroupID); |
||
487 | break; |
||
488 | |||
489 | case (byte)InstantMessageDialog.SessionDrop: |
||
490 | AgentDroppedFromGroupChatSession(AgentID.ToString(), GroupID); |
||
491 | break; |
||
492 | |||
493 | case (byte)InstantMessageDialog.SessionSend: |
||
494 | // User hasn't dropped, so they're in the session, |
||
495 | // maybe we should deliver it. |
||
496 | IClientAPI client = GetActiveClient(new UUID(msg.toAgentID)); |
||
497 | if (client != null) |
||
498 | { |
||
499 | // Deliver locally, directly |
||
500 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: Delivering to {0} locally", client.Name); |
||
501 | |||
502 | if (!hasAgentDroppedGroupChatSession(toAgentID.ToString(), GroupID)) |
||
503 | { |
||
504 | if (!hasAgentBeenInvitedToGroupChatSession(toAgentID.ToString(), GroupID)) |
||
505 | // This actually sends the message too, so no need to resend it |
||
506 | // with client.SendInstantMessage |
||
507 | AddAgentToSession(toAgentID, GroupID, msg); |
||
508 | else |
||
509 | client.SendInstantMessage(msg); |
||
510 | } |
||
511 | } |
||
512 | else |
||
513 | { |
||
514 | m_log.WarnFormat("[Groups.Messaging]: Received a message over the grid for a client that isn't here: {0}", msg.toAgentID); |
||
515 | } |
||
516 | break; |
||
517 | |||
518 | default: |
||
519 | m_log.WarnFormat("[Groups.Messaging]: I don't know how to proccess a {0} message.", ((InstantMessageDialog)msg.dialog).ToString()); |
||
520 | break; |
||
521 | } |
||
522 | } |
||
523 | |||
524 | private void AddAgentToSession(UUID AgentID, UUID GroupID, GridInstantMessage msg) |
||
525 | { |
||
526 | // Agent not in session and hasn't dropped from session |
||
527 | // Add them to the session for now, and Invite them |
||
528 | AgentInvitedToGroupChatSession(AgentID.ToString(), GroupID); |
||
529 | |||
530 | IClientAPI activeClient = GetActiveClient(AgentID); |
||
531 | if (activeClient != null) |
||
532 | { |
||
533 | GroupRecord groupInfo = m_groupData.GetGroupRecord(UUID.Zero.ToString(), GroupID, null); |
||
534 | if (groupInfo != null) |
||
535 | { |
||
536 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: Sending chatterbox invite instant message"); |
||
537 | |||
538 | // Force? open the group session dialog??? |
||
539 | // and simultanously deliver the message, so we don't need to do a seperate client.SendInstantMessage(msg); |
||
540 | IEventQueue eq = activeClient.Scene.RequestModuleInterface<IEventQueue>(); |
||
541 | eq.ChatterboxInvitation( |
||
542 | GroupID |
||
543 | , groupInfo.GroupName |
||
544 | , new UUID(msg.fromAgentID) |
||
545 | , msg.message |
||
546 | , AgentID |
||
547 | , msg.fromAgentName |
||
548 | , msg.dialog |
||
549 | , msg.timestamp |
||
550 | , msg.offline == 1 |
||
551 | , (int)msg.ParentEstateID |
||
552 | , msg.Position |
||
553 | , 1 |
||
554 | , new UUID(msg.imSessionID) |
||
555 | , msg.fromGroup |
||
556 | , OpenMetaverse.Utils.StringToBytes(groupInfo.GroupName) |
||
557 | ); |
||
558 | |||
559 | eq.ChatterBoxSessionAgentListUpdates( |
||
560 | new UUID(GroupID) |
||
561 | , AgentID |
||
562 | , new UUID(msg.toAgentID) |
||
563 | , false //canVoiceChat |
||
564 | , false //isModerator |
||
565 | , false //text mute |
||
566 | ); |
||
567 | } |
||
568 | } |
||
569 | } |
||
570 | |||
571 | #endregion |
||
572 | |||
573 | |||
574 | #region ClientEvents |
||
575 | private void OnInstantMessage(IClientAPI remoteClient, GridInstantMessage im) |
||
576 | { |
||
577 | if (m_debugEnabled) |
||
578 | { |
||
579 | m_log.DebugFormat("[Groups.Messaging]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name); |
||
580 | |||
581 | DebugGridInstantMessage(im); |
||
582 | } |
||
583 | |||
584 | // Start group IM session |
||
585 | if ((im.dialog == (byte)InstantMessageDialog.SessionGroupStart)) |
||
586 | { |
||
587 | if (m_debugEnabled) m_log.InfoFormat("[Groups.Messaging]: imSessionID({0}) toAgentID({1})", im.imSessionID, im.toAgentID); |
||
588 | |||
589 | UUID GroupID = new UUID(im.imSessionID); |
||
590 | UUID AgentID = new UUID(im.fromAgentID); |
||
591 | |||
592 | GroupRecord groupInfo = m_groupData.GetGroupRecord(UUID.Zero.ToString(), GroupID, null); |
||
593 | |||
594 | if (groupInfo != null) |
||
595 | { |
||
596 | AgentInvitedToGroupChatSession(AgentID.ToString(), GroupID); |
||
597 | |||
598 | ChatterBoxSessionStartReplyViaCaps(remoteClient, groupInfo.GroupName, GroupID); |
||
599 | |||
600 | IEventQueue queue = remoteClient.Scene.RequestModuleInterface<IEventQueue>(); |
||
601 | queue.ChatterBoxSessionAgentListUpdates( |
||
602 | GroupID |
||
603 | , AgentID |
||
604 | , new UUID(im.toAgentID) |
||
605 | , false //canVoiceChat |
||
606 | , false //isModerator |
||
607 | , false //text mute |
||
608 | ); |
||
609 | } |
||
610 | } |
||
611 | |||
612 | // Send a message from locally connected client to a group |
||
613 | if ((im.dialog == (byte)InstantMessageDialog.SessionSend)) |
||
614 | { |
||
615 | UUID GroupID = new UUID(im.imSessionID); |
||
616 | UUID AgentID = new UUID(im.fromAgentID); |
||
617 | |||
618 | if (m_debugEnabled) |
||
619 | m_log.DebugFormat("[Groups.Messaging]: Send message to session for group {0} with session ID {1}", GroupID, im.imSessionID.ToString()); |
||
620 | |||
621 | //If this agent is sending a message, then they want to be in the session |
||
622 | AgentInvitedToGroupChatSession(AgentID.ToString(), GroupID); |
||
623 | |||
624 | SendMessageToGroup(im, GroupID); |
||
625 | } |
||
626 | } |
||
627 | |||
628 | #endregion |
||
629 | |||
630 | void ChatterBoxSessionStartReplyViaCaps(IClientAPI remoteClient, string groupName, UUID groupID) |
||
631 | { |
||
632 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name); |
||
633 | |||
634 | OSDMap moderatedMap = new OSDMap(4); |
||
635 | moderatedMap.Add("voice", OSD.FromBoolean(false)); |
||
636 | |||
637 | OSDMap sessionMap = new OSDMap(4); |
||
638 | sessionMap.Add("moderated_mode", moderatedMap); |
||
639 | sessionMap.Add("session_name", OSD.FromString(groupName)); |
||
640 | sessionMap.Add("type", OSD.FromInteger(0)); |
||
641 | sessionMap.Add("voice_enabled", OSD.FromBoolean(false)); |
||
642 | |||
643 | OSDMap bodyMap = new OSDMap(4); |
||
644 | bodyMap.Add("session_id", OSD.FromUUID(groupID)); |
||
645 | bodyMap.Add("temp_session_id", OSD.FromUUID(groupID)); |
||
646 | bodyMap.Add("success", OSD.FromBoolean(true)); |
||
647 | bodyMap.Add("session_info", sessionMap); |
||
648 | |||
649 | IEventQueue queue = remoteClient.Scene.RequestModuleInterface<IEventQueue>(); |
||
650 | |||
651 | if (queue != null) |
||
652 | { |
||
653 | queue.Enqueue(queue.BuildEvent("ChatterBoxSessionStartReply", bodyMap), remoteClient.AgentId); |
||
654 | } |
||
655 | } |
||
656 | |||
657 | private void DebugGridInstantMessage(GridInstantMessage im) |
||
658 | { |
||
659 | // Don't log any normal IMs (privacy!) |
||
660 | if (m_debugEnabled && im.dialog != (byte)InstantMessageDialog.MessageFromAgent) |
||
661 | { |
||
662 | m_log.WarnFormat("[Groups.Messaging]: IM: fromGroup({0})", im.fromGroup ? "True" : "False"); |
||
663 | m_log.WarnFormat("[Groups.Messaging]: IM: Dialog({0})", ((InstantMessageDialog)im.dialog).ToString()); |
||
664 | m_log.WarnFormat("[Groups.Messaging]: IM: fromAgentID({0})", im.fromAgentID.ToString()); |
||
665 | m_log.WarnFormat("[Groups.Messaging]: IM: fromAgentName({0})", im.fromAgentName.ToString()); |
||
666 | m_log.WarnFormat("[Groups.Messaging]: IM: imSessionID({0})", im.imSessionID.ToString()); |
||
667 | m_log.WarnFormat("[Groups.Messaging]: IM: message({0})", im.message.ToString()); |
||
668 | m_log.WarnFormat("[Groups.Messaging]: IM: offline({0})", im.offline.ToString()); |
||
669 | m_log.WarnFormat("[Groups.Messaging]: IM: toAgentID({0})", im.toAgentID.ToString()); |
||
670 | m_log.WarnFormat("[Groups.Messaging]: IM: binaryBucket({0})", OpenMetaverse.Utils.BytesToHexString(im.binaryBucket, "BinaryBucket")); |
||
671 | } |
||
672 | } |
||
673 | |||
674 | #region Client Tools |
||
675 | |||
676 | /// <summary> |
||
677 | /// Try to find an active IClientAPI reference for agentID giving preference to root connections |
||
678 | /// </summary> |
||
679 | private IClientAPI GetActiveClient(UUID agentID) |
||
680 | { |
||
681 | if (m_debugEnabled) m_log.WarnFormat("[Groups.Messaging]: Looking for local client {0}", agentID); |
||
682 | |||
683 | IClientAPI child = null; |
||
684 | |||
685 | // Try root avatar first |
||
686 | foreach (Scene scene in m_sceneList) |
||
687 | { |
||
688 | ScenePresence sp = scene.GetScenePresence(agentID); |
||
689 | if (sp != null) |
||
690 | { |
||
691 | if (!sp.IsChildAgent) |
||
692 | { |
||
693 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: Found root agent for client : {0}", sp.ControllingClient.Name); |
||
694 | return sp.ControllingClient; |
||
695 | } |
||
696 | else |
||
697 | { |
||
698 | if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: Found child agent for client : {0}", sp.ControllingClient.Name); |
||
699 | child = sp.ControllingClient; |
||
700 | } |
||
701 | } |
||
702 | } |
||
703 | |||
704 | // If we didn't find a root, then just return whichever child we found, or null if none |
||
705 | if (child == null) |
||
706 | { |
||
707 | if (m_debugEnabled) m_log.WarnFormat("[Groups.Messaging]: Could not find local client for agent : {0}", agentID); |
||
708 | } |
||
709 | else |
||
710 | { |
||
711 | if (m_debugEnabled) m_log.WarnFormat("[Groups.Messaging]: Returning child agent for client : {0}", child.Name); |
||
712 | } |
||
713 | return child; |
||
714 | } |
||
715 | |||
716 | #endregion |
||
717 | |||
718 | #region GroupSessionTracking |
||
719 | |||
720 | public void ResetAgentGroupChatSessions(string agentID) |
||
721 | { |
||
722 | foreach (List<string> agentList in m_groupsAgentsDroppedFromChatSession.Values) |
||
723 | agentList.Remove(agentID); |
||
724 | |||
725 | foreach (List<string> agentList in m_groupsAgentsInvitedToChatSession.Values) |
||
726 | agentList.Remove(agentID); |
||
727 | } |
||
728 | |||
729 | public bool hasAgentBeenInvitedToGroupChatSession(string agentID, UUID groupID) |
||
730 | { |
||
731 | // If we're tracking this group, and we can find them in the tracking, then they've been invited |
||
732 | return m_groupsAgentsInvitedToChatSession.ContainsKey(groupID) |
||
733 | && m_groupsAgentsInvitedToChatSession[groupID].Contains(agentID); |
||
734 | } |
||
735 | |||
736 | public bool hasAgentDroppedGroupChatSession(string agentID, UUID groupID) |
||
737 | { |
||
738 | // If we're tracking drops for this group, |
||
739 | // and we find them, well... then they've dropped |
||
740 | return m_groupsAgentsDroppedFromChatSession.ContainsKey(groupID) |
||
741 | && m_groupsAgentsDroppedFromChatSession[groupID].Contains(agentID); |
||
742 | } |
||
743 | |||
744 | public void AgentDroppedFromGroupChatSession(string agentID, UUID groupID) |
||
745 | { |
||
746 | if (m_groupsAgentsDroppedFromChatSession.ContainsKey(groupID)) |
||
747 | { |
||
748 | // If not in dropped list, add |
||
749 | if (!m_groupsAgentsDroppedFromChatSession[groupID].Contains(agentID)) |
||
750 | { |
||
751 | m_groupsAgentsDroppedFromChatSession[groupID].Add(agentID); |
||
752 | } |
||
753 | } |
||
754 | } |
||
755 | |||
756 | public void AgentInvitedToGroupChatSession(string agentID, UUID groupID) |
||
757 | { |
||
758 | // Add Session Status if it doesn't exist for this session |
||
759 | CreateGroupChatSessionTracking(groupID); |
||
760 | |||
761 | // If nessesary, remove from dropped list |
||
762 | if (m_groupsAgentsDroppedFromChatSession[groupID].Contains(agentID)) |
||
763 | { |
||
764 | m_groupsAgentsDroppedFromChatSession[groupID].Remove(agentID); |
||
765 | } |
||
766 | |||
767 | // Add to invited |
||
768 | if (!m_groupsAgentsInvitedToChatSession[groupID].Contains(agentID)) |
||
769 | m_groupsAgentsInvitedToChatSession[groupID].Add(agentID); |
||
770 | } |
||
771 | |||
772 | private void CreateGroupChatSessionTracking(UUID groupID) |
||
773 | { |
||
774 | if (!m_groupsAgentsDroppedFromChatSession.ContainsKey(groupID)) |
||
775 | { |
||
776 | m_groupsAgentsDroppedFromChatSession.Add(groupID, new List<string>()); |
||
777 | m_groupsAgentsInvitedToChatSession.Add(groupID, new List<string>()); |
||
778 | } |
||
779 | |||
780 | } |
||
781 | #endregion |
||
782 | |||
783 | } |
||
784 | } |