GroupsMessagingModule.cs 35 KB

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