GroupsMessagingModule.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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}", 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. // We cache in order not to overwhelm the presence service on large grids with many groups. This does
  244. // mean that members coming online will not see all group members until after m_usersOnlineCacheExpirySeconds has elapsed.
  245. // (assuming this is the same across all grid simulators).
  246. if (!m_usersOnlineCache.TryGetValue(groupID, out onlineAgents))
  247. {
  248. string[] t1 = groupMembers.ConvertAll<string>(gmd => gmd.AgentID.ToString()).ToArray();
  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 is 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.IsZero())
  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. UUID fromAgent = new UUID(msg.fromAgentID);
  494. // Force? open the group session dialog???
  495. // and simultanously deliver the message, so we don't need to do a seperate client.SendInstantMessage(msg);
  496. IEventQueue eq = activeClient.Scene.RequestModuleInterface<IEventQueue>();
  497. if (eq != null)
  498. {
  499. eq.ChatterboxInvitation(
  500. GroupID
  501. , groupInfo.GroupName
  502. , fromAgent
  503. , msg.message
  504. , AgentID
  505. , msg.fromAgentName
  506. , msg.dialog
  507. , msg.timestamp
  508. , msg.offline == 1
  509. , (int)msg.ParentEstateID
  510. , msg.Position
  511. , 1
  512. , new UUID(msg.imSessionID)
  513. , msg.fromGroup
  514. , OpenMetaverse.Utils.StringToBytes(groupInfo.GroupName)
  515. );
  516. var update = new GroupChatListAgentUpdateData(AgentID);
  517. var updates = new List<GroupChatListAgentUpdateData> { update };
  518. eq.ChatterBoxSessionAgentListUpdates(GroupID, new UUID(msg.toAgentID), updates);
  519. }
  520. }
  521. }
  522. }
  523. #endregion
  524. #region ClientEvents
  525. private void OnInstantMessage(IClientAPI remoteClient, GridInstantMessage im)
  526. {
  527. if (m_debugEnabled)
  528. {
  529. m_log.DebugFormat("[Groups.Messaging]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  530. DebugGridInstantMessage(im);
  531. }
  532. // Start group IM session
  533. if ((im.dialog == (byte)InstantMessageDialog.SessionGroupStart))
  534. {
  535. if (m_debugEnabled) m_log.InfoFormat("[Groups.Messaging]: imSessionID({0}) toAgentID({1})", im.imSessionID, im.toAgentID);
  536. UUID GroupID = new UUID(im.imSessionID);
  537. UUID AgentID = new UUID(im.fromAgentID);
  538. GroupRecord groupInfo = m_groupData.GetGroupRecord(UUID.Zero.ToString(), GroupID, null);
  539. if (groupInfo != null)
  540. {
  541. AgentInvitedToGroupChatSession(AgentID.ToString(), GroupID);
  542. ChatterBoxSessionStartReplyViaCaps(remoteClient, groupInfo.GroupName, GroupID);
  543. IEventQueue queue = remoteClient.Scene.RequestModuleInterface<IEventQueue>();
  544. if (queue != null)
  545. {
  546. var update = new GroupChatListAgentUpdateData(AgentID);
  547. var updates = new List<GroupChatListAgentUpdateData> { update };
  548. queue.ChatterBoxSessionAgentListUpdates(GroupID, remoteClient.AgentId, updates);
  549. }
  550. }
  551. }
  552. // Send a message from locally connected client to a group
  553. if ((im.dialog == (byte)InstantMessageDialog.SessionSend))
  554. {
  555. UUID GroupID = new UUID(im.imSessionID);
  556. UUID AgentID = new UUID(im.fromAgentID);
  557. if (m_debugEnabled)
  558. m_log.DebugFormat("[Groups.Messaging]: Send message to session for group {0} with session ID {1}", GroupID, im.imSessionID.ToString());
  559. //If this agent is sending a message, then they want to be in the session
  560. AgentInvitedToGroupChatSession(AgentID.ToString(), GroupID);
  561. SendMessageToGroup(im, GroupID);
  562. }
  563. }
  564. #endregion
  565. void ChatterBoxSessionStartReplyViaCaps(IClientAPI remoteClient, string groupName, UUID groupID)
  566. {
  567. if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  568. OSDMap moderatedMap = new OSDMap(4);
  569. moderatedMap.Add("voice", OSD.FromBoolean(false));
  570. OSDMap sessionMap = new OSDMap(4);
  571. sessionMap.Add("moderated_mode", moderatedMap);
  572. sessionMap.Add("session_name", OSD.FromString(groupName));
  573. sessionMap.Add("type", OSD.FromInteger(0));
  574. sessionMap.Add("voice_enabled", OSD.FromBoolean(false));
  575. OSDMap bodyMap = new OSDMap(4);
  576. bodyMap.Add("session_id", OSD.FromUUID(groupID));
  577. bodyMap.Add("temp_session_id", OSD.FromUUID(groupID));
  578. bodyMap.Add("success", OSD.FromBoolean(true));
  579. bodyMap.Add("session_info", sessionMap);
  580. IEventQueue queue = remoteClient.Scene.RequestModuleInterface<IEventQueue>();
  581. queue?.Enqueue(queue.BuildEvent("ChatterBoxSessionStartReply", bodyMap), remoteClient.AgentId);
  582. }
  583. private void DebugGridInstantMessage(GridInstantMessage im)
  584. {
  585. // Don't log any normal IMs (privacy!)
  586. if (m_debugEnabled && im.dialog != (byte)InstantMessageDialog.MessageFromAgent)
  587. {
  588. m_log.WarnFormat("[Groups.Messaging]: IM: fromGroup({0})", im.fromGroup ? "True" : "False");
  589. m_log.WarnFormat("[Groups.Messaging]: IM: Dialog({0})", ((InstantMessageDialog)im.dialog).ToString());
  590. m_log.WarnFormat("[Groups.Messaging]: IM: fromAgentID({0})", im.fromAgentID.ToString());
  591. m_log.WarnFormat("[Groups.Messaging]: IM: fromAgentName({0})", im.fromAgentName.ToString());
  592. m_log.WarnFormat("[Groups.Messaging]: IM: imSessionID({0})", im.imSessionID.ToString());
  593. m_log.WarnFormat("[Groups.Messaging]: IM: message({0})", im.message.ToString());
  594. m_log.WarnFormat("[Groups.Messaging]: IM: offline({0})", im.offline.ToString());
  595. m_log.WarnFormat("[Groups.Messaging]: IM: toAgentID({0})", im.toAgentID.ToString());
  596. m_log.WarnFormat("[Groups.Messaging]: IM: binaryBucket({0})", OpenMetaverse.Utils.BytesToHexString(im.binaryBucket, "BinaryBucket"));
  597. }
  598. }
  599. #region Client Tools
  600. /// <summary>
  601. /// Try to find an active IClientAPI reference for agentID giving preference to root connections
  602. /// </summary>
  603. private IClientAPI GetActiveClient(UUID agentID)
  604. {
  605. if (m_debugEnabled) m_log.WarnFormat("[Groups.Messaging]: Looking for local client {0}", agentID);
  606. IClientAPI child = null;
  607. // Try root avatar first
  608. foreach (Scene scene in m_sceneList)
  609. {
  610. ScenePresence sp = scene.GetScenePresence(agentID);
  611. if (sp != null)
  612. {
  613. if (!sp.IsChildAgent)
  614. {
  615. if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: Found root agent for client : {0}", sp.ControllingClient.Name);
  616. return sp.ControllingClient;
  617. }
  618. else
  619. {
  620. if (m_debugEnabled) m_log.DebugFormat("[Groups.Messaging]: Found child agent for client : {0}", sp.ControllingClient.Name);
  621. child = sp.ControllingClient;
  622. }
  623. }
  624. }
  625. // If we didn't find a root, then just return whichever child we found, or null if none
  626. if (child == null)
  627. {
  628. if (m_debugEnabled) m_log.WarnFormat("[Groups.Messaging]: Could not find local client for agent : {0}", agentID);
  629. }
  630. else
  631. {
  632. if (m_debugEnabled) m_log.WarnFormat("[Groups.Messaging]: Returning child agent for client : {0}", child.Name);
  633. }
  634. return child;
  635. }
  636. #endregion
  637. #region GroupSessionTracking
  638. public void ResetAgentGroupChatSessions(string agentID)
  639. {
  640. foreach (List<string> agentList in m_groupsAgentsDroppedFromChatSession.Values)
  641. agentList.Remove(agentID);
  642. foreach (List<string> agentList in m_groupsAgentsInvitedToChatSession.Values)
  643. agentList.Remove(agentID);
  644. }
  645. public bool hasAgentBeenInvitedToGroupChatSession(string agentID, UUID groupID)
  646. {
  647. // If we're tracking this group, and we can find them in the tracking, then they've been invited
  648. return m_groupsAgentsInvitedToChatSession.ContainsKey(groupID)
  649. && m_groupsAgentsInvitedToChatSession[groupID].Contains(agentID);
  650. }
  651. public bool hasAgentDroppedGroupChatSession(string agentID, UUID groupID)
  652. {
  653. // If we're tracking drops for this group,
  654. // and we find them, well... then they've dropped
  655. return m_groupsAgentsDroppedFromChatSession.ContainsKey(groupID)
  656. && m_groupsAgentsDroppedFromChatSession[groupID].Contains(agentID);
  657. }
  658. public void AgentDroppedFromGroupChatSession(string agentID, UUID groupID)
  659. {
  660. if (m_groupsAgentsDroppedFromChatSession.ContainsKey(groupID))
  661. {
  662. // If not in dropped list, add
  663. if (!m_groupsAgentsDroppedFromChatSession[groupID].Contains(agentID))
  664. {
  665. m_groupsAgentsDroppedFromChatSession[groupID].Add(agentID);
  666. }
  667. }
  668. }
  669. public void AgentInvitedToGroupChatSession(string agentID, UUID groupID)
  670. {
  671. // Add Session Status if it doesn't exist for this session
  672. CreateGroupChatSessionTracking(groupID);
  673. // If nessesary, remove from dropped list
  674. if (m_groupsAgentsDroppedFromChatSession[groupID].Contains(agentID))
  675. {
  676. m_groupsAgentsDroppedFromChatSession[groupID].Remove(agentID);
  677. }
  678. // Add to invited
  679. if (!m_groupsAgentsInvitedToChatSession[groupID].Contains(agentID))
  680. m_groupsAgentsInvitedToChatSession[groupID].Add(agentID);
  681. }
  682. private void CreateGroupChatSessionTracking(UUID groupID)
  683. {
  684. if (!m_groupsAgentsDroppedFromChatSession.ContainsKey(groupID))
  685. {
  686. m_groupsAgentsDroppedFromChatSession.Add(groupID, new List<string>());
  687. m_groupsAgentsInvitedToChatSession.Add(groupID, new List<string>());
  688. }
  689. }
  690. #endregion
  691. }
  692. }