GroupsModule.cs 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  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.Reflection;
  30. using System.Timers;
  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.Framework.Communications;
  38. using OpenSim.Region.CoreModules.Framework.EventQueue;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. using OpenSim.Services.Interfaces;
  42. using Caps = OpenSim.Framework.Capabilities.Caps;
  43. using DirFindFlags = OpenMetaverse.DirectoryManager.DirFindFlags;
  44. namespace OpenSim.Region.OptionalModules.Avatar.XmlRpcGroups
  45. {
  46. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
  47. public class GroupsModule : ISharedRegionModule, IGroupsModule
  48. {
  49. /// <summary>
  50. /// ; To use this module, you must specify the following in your OpenSim.ini
  51. /// [GROUPS]
  52. /// Enabled = true
  53. ///
  54. /// Module = GroupsModule
  55. /// NoticesEnabled = true
  56. /// DebugEnabled = true
  57. ///
  58. /// GroupsServicesConnectorModule = XmlRpcGroupsServicesConnector
  59. /// XmlRpcServiceURL = http://osflotsam.org/xmlrpc.php
  60. /// XmlRpcServiceReadKey = 1234
  61. /// XmlRpcServiceWriteKey = 1234
  62. ///
  63. /// MessagingModule = GroupsMessagingModule
  64. /// MessagingEnabled = true
  65. ///
  66. /// ; Disables HTTP Keep-Alive for Groups Module HTTP Requests, work around for
  67. /// ; a problem discovered on some Windows based region servers. Only disable
  68. /// ; if you see a large number (dozens) of the following Exceptions:
  69. /// ; System.Net.WebException: The request was aborted: The request was canceled.
  70. ///
  71. /// XmlRpcDisableKeepAlive = false
  72. /// </summary>
  73. private static readonly ILog m_log =
  74. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  75. private List<Scene> m_sceneList = new List<Scene>();
  76. private IMessageTransferModule m_msgTransferModule = null;
  77. private IGroupsServicesConnector m_groupData = null;
  78. // Configuration settings
  79. private bool m_groupsEnabled = false;
  80. private bool m_groupNoticesEnabled = true;
  81. private bool m_debugEnabled = true;
  82. #region IRegionModuleBase Members
  83. public void Initialise(IConfigSource config)
  84. {
  85. IConfig groupsConfig = config.Configs["Groups"];
  86. if (groupsConfig == null)
  87. {
  88. // Do not run this module by default.
  89. return;
  90. }
  91. else
  92. {
  93. m_groupsEnabled = groupsConfig.GetBoolean("Enabled", false);
  94. if (!m_groupsEnabled)
  95. {
  96. return;
  97. }
  98. if (groupsConfig.GetString("Module", "Default") != Name)
  99. {
  100. m_groupsEnabled = false;
  101. return;
  102. }
  103. m_log.InfoFormat("[GROUPS]: Initializing {0}", this.Name);
  104. m_groupNoticesEnabled = groupsConfig.GetBoolean("NoticesEnabled", true);
  105. m_debugEnabled = groupsConfig.GetBoolean("DebugEnabled", true);
  106. }
  107. }
  108. public void AddRegion(Scene scene)
  109. {
  110. if (m_groupsEnabled)
  111. scene.RegisterModuleInterface<IGroupsModule>(this);
  112. }
  113. public void RegionLoaded(Scene scene)
  114. {
  115. if (!m_groupsEnabled)
  116. return;
  117. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  118. if (m_groupData == null)
  119. {
  120. m_groupData = scene.RequestModuleInterface<IGroupsServicesConnector>();
  121. // No Groups Service Connector, then nothing works...
  122. if (m_groupData == null)
  123. {
  124. m_groupsEnabled = false;
  125. m_log.Error("[GROUPS]: Could not get IGroupsServicesConnector");
  126. Close();
  127. return;
  128. }
  129. }
  130. if (m_msgTransferModule == null)
  131. {
  132. m_msgTransferModule = scene.RequestModuleInterface<IMessageTransferModule>();
  133. // No message transfer module, no notices, group invites, rejects, ejects, etc
  134. if (m_msgTransferModule == null)
  135. {
  136. m_groupsEnabled = false;
  137. m_log.Error("[GROUPS]: Could not get MessageTransferModule");
  138. Close();
  139. return;
  140. }
  141. }
  142. lock (m_sceneList)
  143. {
  144. m_sceneList.Add(scene);
  145. }
  146. scene.EventManager.OnNewClient += OnNewClient;
  147. scene.EventManager.OnIncomingInstantMessage += OnGridInstantMessage;
  148. // The InstantMessageModule itself doesn't do this,
  149. // so lets see if things explode if we don't do it
  150. // scene.EventManager.OnClientClosed += OnClientClosed;
  151. }
  152. public void RemoveRegion(Scene scene)
  153. {
  154. if (!m_groupsEnabled)
  155. return;
  156. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  157. lock (m_sceneList)
  158. {
  159. m_sceneList.Remove(scene);
  160. }
  161. }
  162. public void Close()
  163. {
  164. if (!m_groupsEnabled)
  165. return;
  166. if (m_debugEnabled) m_log.Debug("[GROUPS]: Shutting down Groups module.");
  167. }
  168. public Type ReplaceableInterface
  169. {
  170. get { return null; }
  171. }
  172. public string Name
  173. {
  174. get { return "GroupsModule"; }
  175. }
  176. #endregion
  177. #region ISharedRegionModule Members
  178. public void PostInitialise()
  179. {
  180. // NoOp
  181. }
  182. #endregion
  183. #region EventHandlers
  184. private void OnNewClient(IClientAPI client)
  185. {
  186. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  187. client.OnUUIDGroupNameRequest += HandleUUIDGroupNameRequest;
  188. client.OnAgentDataUpdateRequest += OnAgentDataUpdateRequest;
  189. client.OnDirFindQuery += OnDirFindQuery;
  190. client.OnRequestAvatarProperties += OnRequestAvatarProperties;
  191. // Used for Notices and Group Invites/Accept/Reject
  192. client.OnInstantMessage += OnInstantMessage;
  193. // Send client thier groups information.
  194. SendAgentGroupDataUpdate(client, client.AgentId);
  195. }
  196. private void OnRequestAvatarProperties(IClientAPI remoteClient, UUID avatarID)
  197. {
  198. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  199. //GroupMembershipData[] avatarGroups = m_groupData.GetAgentGroupMemberships(GetRequestingAgentID(remoteClient), avatarID).ToArray();
  200. GroupMembershipData[] avatarGroups = GetProfileListedGroupMemberships(remoteClient, avatarID);
  201. remoteClient.SendAvatarGroupsReply(avatarID, avatarGroups);
  202. }
  203. /*
  204. * This becomes very problematic in a shared module. In a shared module you may have more then one
  205. * reference to IClientAPI's, one for 0 or 1 root connections, and 0 or more child connections.
  206. * The OnClientClosed event does not provide anything to indicate which one of those should be closed
  207. * nor does it provide what scene it was from so that the specific reference can be looked up.
  208. * The InstantMessageModule.cs does not currently worry about unregistering the handles,
  209. * and it should be an issue, since it's the client that references us not the other way around
  210. * , so as long as we don't keep a reference to the client laying around, the client can still be GC'ed
  211. private void OnClientClosed(UUID AgentId)
  212. {
  213. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  214. lock (m_ActiveClients)
  215. {
  216. if (m_ActiveClients.ContainsKey(AgentId))
  217. {
  218. IClientAPI client = m_ActiveClients[AgentId];
  219. client.OnUUIDGroupNameRequest -= HandleUUIDGroupNameRequest;
  220. client.OnAgentDataUpdateRequest -= OnAgentDataUpdateRequest;
  221. client.OnDirFindQuery -= OnDirFindQuery;
  222. client.OnInstantMessage -= OnInstantMessage;
  223. m_ActiveClients.Remove(AgentId);
  224. }
  225. else
  226. {
  227. if (m_debugEnabled) m_log.WarnFormat("[GROUPS]: Client closed that wasn't registered here.");
  228. }
  229. }
  230. }
  231. */
  232. void OnDirFindQuery(IClientAPI remoteClient, UUID queryID, string queryText, uint queryFlags, int queryStart)
  233. {
  234. if (((DirFindFlags)queryFlags & DirFindFlags.Groups) == DirFindFlags.Groups)
  235. {
  236. if (m_debugEnabled)
  237. m_log.DebugFormat(
  238. "[GROUPS]: {0} called with queryText({1}) queryFlags({2}) queryStart({3})",
  239. System.Reflection.MethodBase.GetCurrentMethod().Name, queryText, (DirFindFlags)queryFlags, queryStart);
  240. // TODO: This currently ignores pretty much all the query flags including Mature and sort order
  241. remoteClient.SendDirGroupsReply(queryID, m_groupData.FindGroups(GetRequestingAgentID(remoteClient), queryText).ToArray());
  242. }
  243. }
  244. private void OnAgentDataUpdateRequest(IClientAPI remoteClient, UUID dataForAgentID, UUID sessionID)
  245. {
  246. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  247. UUID activeGroupID = UUID.Zero;
  248. string activeGroupTitle = string.Empty;
  249. string activeGroupName = string.Empty;
  250. ulong activeGroupPowers = (ulong)GroupPowers.None;
  251. GroupMembershipData membership = m_groupData.GetAgentActiveMembership(GetRequestingAgentID(remoteClient), dataForAgentID);
  252. if (membership != null)
  253. {
  254. activeGroupID = membership.GroupID;
  255. activeGroupTitle = membership.GroupTitle;
  256. activeGroupPowers = membership.GroupPowers;
  257. }
  258. SendAgentDataUpdate(remoteClient, dataForAgentID, activeGroupID, activeGroupName, activeGroupPowers, activeGroupTitle);
  259. SendScenePresenceUpdate(dataForAgentID, activeGroupTitle);
  260. }
  261. private void HandleUUIDGroupNameRequest(UUID GroupID, IClientAPI remoteClient)
  262. {
  263. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  264. string GroupName;
  265. GroupRecord group = m_groupData.GetGroupRecord(GetRequestingAgentID(remoteClient), GroupID, null);
  266. if (group != null)
  267. {
  268. GroupName = group.GroupName;
  269. }
  270. else
  271. {
  272. GroupName = "Unknown";
  273. }
  274. remoteClient.SendGroupNameReply(GroupID, GroupName);
  275. }
  276. private void OnInstantMessage(IClientAPI remoteClient, GridInstantMessage im)
  277. {
  278. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  279. // Group invitations
  280. if ((im.dialog == (byte)InstantMessageDialog.GroupInvitationAccept) || (im.dialog == (byte)InstantMessageDialog.GroupInvitationDecline))
  281. {
  282. UUID inviteID = new UUID(im.imSessionID);
  283. GroupInviteInfo inviteInfo = m_groupData.GetAgentToGroupInvite(GetRequestingAgentID(remoteClient), inviteID);
  284. if (inviteInfo == null)
  285. {
  286. if (m_debugEnabled) m_log.WarnFormat("[GROUPS]: Received an Invite IM for an invite that does not exist {0}.", inviteID);
  287. return;
  288. }
  289. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: Invite is for Agent {0} to Group {1}.", inviteInfo.AgentID, inviteInfo.GroupID);
  290. UUID fromAgentID = new UUID(im.fromAgentID);
  291. if ((inviteInfo != null) && (fromAgentID == inviteInfo.AgentID))
  292. {
  293. // Accept
  294. if (im.dialog == (byte)InstantMessageDialog.GroupInvitationAccept)
  295. {
  296. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: Received an accept invite notice.");
  297. // and the sessionid is the role
  298. m_groupData.AddAgentToGroup(GetRequestingAgentID(remoteClient), inviteInfo.AgentID, inviteInfo.GroupID, inviteInfo.RoleID);
  299. GridInstantMessage msg = new GridInstantMessage();
  300. msg.imSessionID = UUID.Zero.Guid;
  301. msg.fromAgentID = UUID.Zero.Guid;
  302. msg.toAgentID = inviteInfo.AgentID.Guid;
  303. msg.timestamp = (uint)Util.UnixTimeSinceEpoch();
  304. msg.fromAgentName = "Groups";
  305. msg.message = string.Format("You have been added to the group.");
  306. msg.dialog = (byte)OpenMetaverse.InstantMessageDialog.MessageBox;
  307. msg.fromGroup = false;
  308. msg.offline = (byte)0;
  309. msg.ParentEstateID = 0;
  310. msg.Position = Vector3.Zero;
  311. msg.RegionID = UUID.Zero.Guid;
  312. msg.binaryBucket = new byte[0];
  313. OutgoingInstantMessage(msg, inviteInfo.AgentID);
  314. UpdateAllClientsWithGroupInfo(inviteInfo.AgentID);
  315. // TODO: If the inviter is still online, they need an agent dataupdate
  316. // and maybe group membership updates for the invitee
  317. m_groupData.RemoveAgentToGroupInvite(GetRequestingAgentID(remoteClient), inviteID);
  318. }
  319. // Reject
  320. if (im.dialog == (byte)InstantMessageDialog.GroupInvitationDecline)
  321. {
  322. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: Received a reject invite notice.");
  323. m_groupData.RemoveAgentToGroupInvite(GetRequestingAgentID(remoteClient), inviteID);
  324. }
  325. }
  326. }
  327. // Group notices
  328. if ((im.dialog == (byte)InstantMessageDialog.GroupNotice))
  329. {
  330. if (!m_groupNoticesEnabled)
  331. {
  332. return;
  333. }
  334. UUID GroupID = new UUID(im.toAgentID);
  335. if (m_groupData.GetGroupRecord(GetRequestingAgentID(remoteClient), GroupID, null) != null)
  336. {
  337. UUID NoticeID = UUID.Random();
  338. string Subject = im.message.Substring(0, im.message.IndexOf('|'));
  339. string Message = im.message.Substring(Subject.Length + 1);
  340. byte[] bucket;
  341. if ((im.binaryBucket.Length == 1) && (im.binaryBucket[0] == 0))
  342. {
  343. bucket = new byte[19];
  344. bucket[0] = 0; //dunno
  345. bucket[1] = 0; //dunno
  346. GroupID.ToBytes(bucket, 2);
  347. bucket[18] = 0; //dunno
  348. }
  349. else
  350. {
  351. string binBucket = OpenMetaverse.Utils.BytesToString(im.binaryBucket);
  352. binBucket = binBucket.Remove(0, 14).Trim();
  353. if (m_debugEnabled)
  354. {
  355. m_log.WarnFormat("I don't understand a group notice binary bucket of: {0}", binBucket);
  356. OSDMap binBucketOSD = (OSDMap)OSDParser.DeserializeLLSDXml(binBucket);
  357. foreach (string key in binBucketOSD.Keys)
  358. {
  359. if (binBucketOSD.ContainsKey(key))
  360. {
  361. m_log.WarnFormat("{0}: {1}", key, binBucketOSD[key].ToString());
  362. }
  363. }
  364. }
  365. // treat as if no attachment
  366. bucket = new byte[19];
  367. bucket[0] = 0; //dunno
  368. bucket[1] = 0; //dunno
  369. GroupID.ToBytes(bucket, 2);
  370. bucket[18] = 0; //dunno
  371. }
  372. m_groupData.AddGroupNotice(GetRequestingAgentID(remoteClient), GroupID, NoticeID, im.fromAgentName, Subject, Message, bucket);
  373. if (OnNewGroupNotice != null)
  374. {
  375. OnNewGroupNotice(GroupID, NoticeID);
  376. }
  377. // Send notice out to everyone that wants notices
  378. foreach (GroupMembersData member in m_groupData.GetGroupMembers(GetRequestingAgentID(remoteClient), GroupID))
  379. {
  380. if (m_debugEnabled)
  381. {
  382. UserAccount targetUser = m_sceneList[0].UserAccountService.GetUserAccount(remoteClient.Scene.RegionInfo.ScopeID, member.AgentID);
  383. if (targetUser != null)
  384. {
  385. m_log.DebugFormat("[GROUPS]: Prepping group notice {0} for agent: {1} who Accepts Notices ({2})", NoticeID, targetUser.FirstName + " " + targetUser.LastName, member.AcceptNotices);
  386. }
  387. else
  388. {
  389. m_log.DebugFormat("[GROUPS]: Prepping group notice {0} for agent: {1} who Accepts Notices ({2})", NoticeID, member.AgentID, member.AcceptNotices);
  390. }
  391. }
  392. if (member.AcceptNotices)
  393. {
  394. // Build notice IIM
  395. GridInstantMessage msg = CreateGroupNoticeIM(UUID.Zero, NoticeID, (byte)OpenMetaverse.InstantMessageDialog.GroupNotice);
  396. msg.toAgentID = member.AgentID.Guid;
  397. OutgoingInstantMessage(msg, member.AgentID);
  398. }
  399. }
  400. }
  401. }
  402. // Interop, received special 210 code for ejecting a group member
  403. // this only works within the comms servers domain, and won't work hypergrid
  404. // TODO:FIXME: Use a presense server of some kind to find out where the
  405. // client actually is, and try contacting that region directly to notify them,
  406. // or provide the notification via xmlrpc update queue
  407. if ((im.dialog == 210))
  408. {
  409. // This is sent from the region that the ejectee was ejected from
  410. // if it's being delivered here, then the ejectee is here
  411. // so we need to send local updates to the agent.
  412. UUID ejecteeID = new UUID(im.toAgentID);
  413. im.dialog = (byte)InstantMessageDialog.MessageFromAgent;
  414. OutgoingInstantMessage(im, ejecteeID);
  415. IClientAPI ejectee = GetActiveClient(ejecteeID);
  416. if (ejectee != null)
  417. {
  418. UUID groupID = new UUID(im.imSessionID);
  419. ejectee.SendAgentDropGroup(groupID);
  420. }
  421. }
  422. }
  423. private void OnGridInstantMessage(GridInstantMessage msg)
  424. {
  425. if (m_debugEnabled) m_log.InfoFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  426. // Trigger the above event handler
  427. OnInstantMessage(null, msg);
  428. // If a message from a group arrives here, it may need to be forwarded to a local client
  429. if (msg.fromGroup == true)
  430. {
  431. switch (msg.dialog)
  432. {
  433. case (byte)InstantMessageDialog.GroupInvitation:
  434. case (byte)InstantMessageDialog.GroupNotice:
  435. UUID toAgentID = new UUID(msg.toAgentID);
  436. IClientAPI localClient = GetActiveClient(toAgentID);
  437. if (localClient != null)
  438. {
  439. localClient.SendInstantMessage(msg);
  440. }
  441. break;
  442. }
  443. }
  444. }
  445. #endregion
  446. #region IGroupsModule Members
  447. public event NewGroupNotice OnNewGroupNotice;
  448. public GroupRecord GetGroupRecord(UUID GroupID)
  449. {
  450. return m_groupData.GetGroupRecord(UUID.Zero, GroupID, null);
  451. }
  452. public GroupRecord GetGroupRecord(string name)
  453. {
  454. return m_groupData.GetGroupRecord(UUID.Zero, UUID.Zero, name);
  455. }
  456. public void ActivateGroup(IClientAPI remoteClient, UUID groupID)
  457. {
  458. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  459. m_groupData.SetAgentActiveGroup(GetRequestingAgentID(remoteClient), GetRequestingAgentID(remoteClient), groupID);
  460. // Changing active group changes title, active powers, all kinds of things
  461. // anyone who is in any region that can see this client, should probably be
  462. // updated with new group info. At a minimum, they should get ScenePresence
  463. // updated with new title.
  464. UpdateAllClientsWithGroupInfo(GetRequestingAgentID(remoteClient));
  465. }
  466. /// <summary>
  467. /// Get the Role Titles for an Agent, for a specific group
  468. /// </summary>
  469. public List<GroupTitlesData> GroupTitlesRequest(IClientAPI remoteClient, UUID groupID)
  470. {
  471. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  472. List<GroupRolesData> agentRoles = m_groupData.GetAgentGroupRoles(GetRequestingAgentID(remoteClient), GetRequestingAgentID(remoteClient), groupID);
  473. GroupMembershipData agentMembership = m_groupData.GetAgentGroupMembership(GetRequestingAgentID(remoteClient), GetRequestingAgentID(remoteClient), groupID);
  474. List<GroupTitlesData> titles = new List<GroupTitlesData>();
  475. foreach (GroupRolesData role in agentRoles)
  476. {
  477. GroupTitlesData title = new GroupTitlesData();
  478. title.Name = role.Name;
  479. if (agentMembership != null)
  480. {
  481. title.Selected = agentMembership.ActiveRole == role.RoleID;
  482. }
  483. title.UUID = role.RoleID;
  484. titles.Add(title);
  485. }
  486. return titles;
  487. }
  488. public List<GroupMembersData> GroupMembersRequest(IClientAPI remoteClient, UUID groupID)
  489. {
  490. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  491. List<GroupMembersData> data = m_groupData.GetGroupMembers(GetRequestingAgentID(remoteClient), groupID);
  492. if (m_debugEnabled)
  493. {
  494. foreach (GroupMembersData member in data)
  495. {
  496. m_log.DebugFormat("[GROUPS]: Member({0}) - IsOwner({1})", member.AgentID, member.IsOwner);
  497. }
  498. }
  499. return data;
  500. }
  501. public List<GroupRolesData> GroupRoleDataRequest(IClientAPI remoteClient, UUID groupID)
  502. {
  503. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  504. List<GroupRolesData> data = m_groupData.GetGroupRoles(GetRequestingAgentID(remoteClient), groupID);
  505. return data;
  506. }
  507. public List<GroupRoleMembersData> GroupRoleMembersRequest(IClientAPI remoteClient, UUID groupID)
  508. {
  509. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  510. List<GroupRoleMembersData> data = m_groupData.GetGroupRoleMembers(GetRequestingAgentID(remoteClient), groupID);
  511. if (m_debugEnabled)
  512. {
  513. foreach (GroupRoleMembersData member in data)
  514. {
  515. m_log.DebugFormat("[GROUPS]: Member({0}) - Role({1})", member.MemberID, member.RoleID);
  516. }
  517. }
  518. return data;
  519. }
  520. public GroupProfileData GroupProfileRequest(IClientAPI remoteClient, UUID groupID)
  521. {
  522. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  523. GroupProfileData profile = new GroupProfileData();
  524. GroupRecord groupInfo = m_groupData.GetGroupRecord(GetRequestingAgentID(remoteClient), groupID, null);
  525. if (groupInfo != null)
  526. {
  527. profile.AllowPublish = groupInfo.AllowPublish;
  528. profile.Charter = groupInfo.Charter;
  529. profile.FounderID = groupInfo.FounderID;
  530. profile.GroupID = groupID;
  531. profile.GroupMembershipCount = m_groupData.GetGroupMembers(GetRequestingAgentID(remoteClient), groupID).Count;
  532. profile.GroupRolesCount = m_groupData.GetGroupRoles(GetRequestingAgentID(remoteClient), groupID).Count;
  533. profile.InsigniaID = groupInfo.GroupPicture;
  534. profile.MaturePublish = groupInfo.MaturePublish;
  535. profile.MembershipFee = groupInfo.MembershipFee;
  536. profile.Money = 0; // TODO: Get this from the currency server?
  537. profile.Name = groupInfo.GroupName;
  538. profile.OpenEnrollment = groupInfo.OpenEnrollment;
  539. profile.OwnerRole = groupInfo.OwnerRoleID;
  540. profile.ShowInList = groupInfo.ShowInList;
  541. }
  542. GroupMembershipData memberInfo = m_groupData.GetAgentGroupMembership(GetRequestingAgentID(remoteClient), GetRequestingAgentID(remoteClient), groupID);
  543. if (memberInfo != null)
  544. {
  545. profile.MemberTitle = memberInfo.GroupTitle;
  546. profile.PowersMask = memberInfo.GroupPowers;
  547. }
  548. return profile;
  549. }
  550. public GroupMembershipData[] GetMembershipData(UUID agentID)
  551. {
  552. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  553. return m_groupData.GetAgentGroupMemberships(UUID.Zero, agentID).ToArray();
  554. }
  555. public GroupMembershipData GetMembershipData(UUID groupID, UUID agentID)
  556. {
  557. if (m_debugEnabled)
  558. m_log.DebugFormat(
  559. "[GROUPS]: {0} called with groupID={1}, agentID={2}",
  560. System.Reflection.MethodBase.GetCurrentMethod().Name, groupID, agentID);
  561. return m_groupData.GetAgentGroupMembership(UUID.Zero, agentID, groupID);
  562. }
  563. public void UpdateGroupInfo(IClientAPI remoteClient, UUID groupID, string charter, bool showInList, UUID insigniaID, int membershipFee, bool openEnrollment, bool allowPublish, bool maturePublish)
  564. {
  565. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  566. // Note: Permissions checking for modification rights is handled by the Groups Server/Service
  567. m_groupData.UpdateGroup(GetRequestingAgentID(remoteClient), groupID, charter, showInList, insigniaID, membershipFee, openEnrollment, allowPublish, maturePublish);
  568. }
  569. public void SetGroupAcceptNotices(IClientAPI remoteClient, UUID groupID, bool acceptNotices, bool listInProfile)
  570. {
  571. // Note: Permissions checking for modification rights is handled by the Groups Server/Service
  572. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  573. m_groupData.SetAgentGroupInfo(GetRequestingAgentID(remoteClient), GetRequestingAgentID(remoteClient), groupID, acceptNotices, listInProfile);
  574. }
  575. public UUID CreateGroup(IClientAPI remoteClient, string name, string charter, bool showInList, UUID insigniaID, int membershipFee, bool openEnrollment, bool allowPublish, bool maturePublish)
  576. {
  577. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  578. if (m_groupData.GetGroupRecord(GetRequestingAgentID(remoteClient), UUID.Zero, name) != null)
  579. {
  580. remoteClient.SendCreateGroupReply(UUID.Zero, false, "A group with the same name already exists.");
  581. return UUID.Zero;
  582. }
  583. // is there is a money module present ?
  584. IMoneyModule money = remoteClient.Scene.RequestModuleInterface<IMoneyModule>();
  585. if (money != null)
  586. {
  587. // do the transaction, that is if the agent has got sufficient funds
  588. if (!money.AmountCovered(remoteClient, money.GroupCreationCharge)) {
  589. remoteClient.SendCreateGroupReply(UUID.Zero, false, "You have got issuficient funds to create a group.");
  590. return UUID.Zero;
  591. }
  592. money.ApplyCharge(GetRequestingAgentID(remoteClient), money.GroupCreationCharge, "Group Creation");
  593. }
  594. UUID groupID = m_groupData.CreateGroup(GetRequestingAgentID(remoteClient), name, charter, showInList, insigniaID, membershipFee, openEnrollment, allowPublish, maturePublish, GetRequestingAgentID(remoteClient));
  595. remoteClient.SendCreateGroupReply(groupID, true, "Group created successfullly");
  596. // Update the founder with new group information.
  597. SendAgentGroupDataUpdate(remoteClient, GetRequestingAgentID(remoteClient));
  598. return groupID;
  599. }
  600. public GroupNoticeData[] GroupNoticesListRequest(IClientAPI remoteClient, UUID groupID)
  601. {
  602. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  603. // ToDo: check if agent is a member of group and is allowed to see notices?
  604. return m_groupData.GetGroupNotices(GetRequestingAgentID(remoteClient), groupID).ToArray();
  605. }
  606. /// <summary>
  607. /// Get the title of the agent's current role.
  608. /// </summary>
  609. public string GetGroupTitle(UUID avatarID)
  610. {
  611. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  612. GroupMembershipData membership = m_groupData.GetAgentActiveMembership(UUID.Zero, avatarID);
  613. if (membership != null)
  614. {
  615. return membership.GroupTitle;
  616. }
  617. return string.Empty;
  618. }
  619. /// <summary>
  620. /// Change the current Active Group Role for Agent
  621. /// </summary>
  622. public void GroupTitleUpdate(IClientAPI remoteClient, UUID groupID, UUID titleRoleID)
  623. {
  624. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  625. m_groupData.SetAgentActiveGroupRole(GetRequestingAgentID(remoteClient), GetRequestingAgentID(remoteClient), groupID, titleRoleID);
  626. // TODO: Not sure what all is needed here, but if the active group role change is for the group
  627. // the client currently has set active, then we need to do a scene presence update too
  628. // if (m_groupData.GetAgentActiveMembership(GetRequestingAgentID(remoteClient)).GroupID == GroupID)
  629. UpdateAllClientsWithGroupInfo(GetRequestingAgentID(remoteClient));
  630. }
  631. public void GroupRoleUpdate(IClientAPI remoteClient, UUID groupID, UUID roleID, string name, string description, string title, ulong powers, byte updateType)
  632. {
  633. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  634. // Security Checks are handled in the Groups Service.
  635. switch ((OpenMetaverse.GroupRoleUpdate)updateType)
  636. {
  637. case OpenMetaverse.GroupRoleUpdate.Create:
  638. m_groupData.AddGroupRole(GetRequestingAgentID(remoteClient), groupID, UUID.Random(), name, description, title, powers);
  639. break;
  640. case OpenMetaverse.GroupRoleUpdate.Delete:
  641. m_groupData.RemoveGroupRole(GetRequestingAgentID(remoteClient), groupID, roleID);
  642. break;
  643. case OpenMetaverse.GroupRoleUpdate.UpdateAll:
  644. case OpenMetaverse.GroupRoleUpdate.UpdateData:
  645. case OpenMetaverse.GroupRoleUpdate.UpdatePowers:
  646. if (m_debugEnabled)
  647. {
  648. GroupPowers gp = (GroupPowers)powers;
  649. m_log.DebugFormat("[GROUPS]: Role ({0}) updated with Powers ({1}) ({2})", name, powers.ToString(), gp.ToString());
  650. }
  651. m_groupData.UpdateGroupRole(GetRequestingAgentID(remoteClient), groupID, roleID, name, description, title, powers);
  652. break;
  653. case OpenMetaverse.GroupRoleUpdate.NoUpdate:
  654. default:
  655. // No Op
  656. break;
  657. }
  658. // TODO: This update really should send out updates for everyone in the role that just got changed.
  659. SendAgentGroupDataUpdate(remoteClient, GetRequestingAgentID(remoteClient));
  660. }
  661. public void GroupRoleChanges(IClientAPI remoteClient, UUID groupID, UUID roleID, UUID memberID, uint changes)
  662. {
  663. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  664. // Todo: Security check
  665. switch (changes)
  666. {
  667. case 0:
  668. // Add
  669. m_groupData.AddAgentToGroupRole(GetRequestingAgentID(remoteClient), memberID, groupID, roleID);
  670. break;
  671. case 1:
  672. // Remove
  673. m_groupData.RemoveAgentFromGroupRole(GetRequestingAgentID(remoteClient), memberID, groupID, roleID);
  674. break;
  675. default:
  676. m_log.ErrorFormat("[GROUPS]: {0} does not understand changes == {1}", System.Reflection.MethodBase.GetCurrentMethod().Name, changes);
  677. break;
  678. }
  679. // TODO: This update really should send out updates for everyone in the role that just got changed.
  680. SendAgentGroupDataUpdate(remoteClient, GetRequestingAgentID(remoteClient));
  681. }
  682. public void GroupNoticeRequest(IClientAPI remoteClient, UUID groupNoticeID)
  683. {
  684. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  685. GroupNoticeInfo data = m_groupData.GetGroupNotice(GetRequestingAgentID(remoteClient), groupNoticeID);
  686. if (data != null)
  687. {
  688. GroupRecord groupInfo = m_groupData.GetGroupRecord(GetRequestingAgentID(remoteClient), data.GroupID, null);
  689. GridInstantMessage msg = new GridInstantMessage();
  690. msg.imSessionID = UUID.Zero.Guid;
  691. msg.fromAgentID = data.GroupID.Guid;
  692. msg.toAgentID = GetRequestingAgentID(remoteClient).Guid;
  693. msg.timestamp = (uint)Util.UnixTimeSinceEpoch();
  694. msg.fromAgentName = "Group Notice : " + groupInfo == null ? "Unknown" : groupInfo.GroupName;
  695. msg.message = data.noticeData.Subject + "|" + data.Message;
  696. msg.dialog = (byte)OpenMetaverse.InstantMessageDialog.GroupNoticeRequested;
  697. msg.fromGroup = true;
  698. msg.offline = (byte)0;
  699. msg.ParentEstateID = 0;
  700. msg.Position = Vector3.Zero;
  701. msg.RegionID = UUID.Zero.Guid;
  702. msg.binaryBucket = data.BinaryBucket;
  703. OutgoingInstantMessage(msg, GetRequestingAgentID(remoteClient));
  704. }
  705. }
  706. public GridInstantMessage CreateGroupNoticeIM(UUID agentID, UUID groupNoticeID, byte dialog)
  707. {
  708. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  709. GridInstantMessage msg = new GridInstantMessage();
  710. msg.imSessionID = UUID.Zero.Guid;
  711. msg.toAgentID = agentID.Guid;
  712. msg.dialog = dialog;
  713. // msg.dialog = (byte)OpenMetaverse.InstantMessageDialog.GroupNotice;
  714. msg.fromGroup = true;
  715. msg.offline = (byte)1; // Allow this message to be stored for offline use
  716. msg.ParentEstateID = 0;
  717. msg.Position = Vector3.Zero;
  718. msg.RegionID = UUID.Zero.Guid;
  719. GroupNoticeInfo info = m_groupData.GetGroupNotice(agentID, groupNoticeID);
  720. if (info != null)
  721. {
  722. msg.fromAgentID = info.GroupID.Guid;
  723. msg.timestamp = info.noticeData.Timestamp;
  724. msg.fromAgentName = info.noticeData.FromName;
  725. msg.message = info.noticeData.Subject + "|" + info.Message;
  726. msg.binaryBucket = info.BinaryBucket;
  727. }
  728. else
  729. {
  730. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: Group Notice {0} not found, composing empty message.", groupNoticeID);
  731. msg.fromAgentID = UUID.Zero.Guid;
  732. msg.timestamp = (uint)Util.UnixTimeSinceEpoch(); ;
  733. msg.fromAgentName = string.Empty;
  734. msg.message = string.Empty;
  735. msg.binaryBucket = new byte[0];
  736. }
  737. return msg;
  738. }
  739. public void SendAgentGroupDataUpdate(IClientAPI remoteClient)
  740. {
  741. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  742. // Send agent information about his groups
  743. SendAgentGroupDataUpdate(remoteClient, GetRequestingAgentID(remoteClient));
  744. }
  745. public void JoinGroupRequest(IClientAPI remoteClient, UUID groupID)
  746. {
  747. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  748. // Should check to see if OpenEnrollment, or if there's an outstanding invitation
  749. m_groupData.AddAgentToGroup(GetRequestingAgentID(remoteClient), GetRequestingAgentID(remoteClient), groupID, UUID.Zero);
  750. remoteClient.SendJoinGroupReply(groupID, true);
  751. // Should this send updates to everyone in the group?
  752. SendAgentGroupDataUpdate(remoteClient, GetRequestingAgentID(remoteClient));
  753. }
  754. public void LeaveGroupRequest(IClientAPI remoteClient, UUID groupID)
  755. {
  756. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  757. m_groupData.RemoveAgentFromGroup(GetRequestingAgentID(remoteClient), GetRequestingAgentID(remoteClient), groupID);
  758. remoteClient.SendLeaveGroupReply(groupID, true);
  759. remoteClient.SendAgentDropGroup(groupID);
  760. // SL sends out notifcations to the group messaging session that the person has left
  761. // Should this also update everyone who is in the group?
  762. SendAgentGroupDataUpdate(remoteClient, GetRequestingAgentID(remoteClient));
  763. }
  764. public void EjectGroupMemberRequest(IClientAPI remoteClient, UUID groupID, UUID ejecteeID)
  765. {
  766. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  767. // Todo: Security check?
  768. m_groupData.RemoveAgentFromGroup(GetRequestingAgentID(remoteClient), ejecteeID, groupID);
  769. remoteClient.SendEjectGroupMemberReply(GetRequestingAgentID(remoteClient), groupID, true);
  770. GroupRecord groupInfo = m_groupData.GetGroupRecord(GetRequestingAgentID(remoteClient), groupID, null);
  771. UserAccount account = m_sceneList[0].UserAccountService.GetUserAccount(remoteClient.Scene.RegionInfo.ScopeID, ejecteeID);
  772. if ((groupInfo == null) || (account == null))
  773. {
  774. return;
  775. }
  776. // Send Message to Ejectee
  777. GridInstantMessage msg = new GridInstantMessage();
  778. msg.imSessionID = UUID.Zero.Guid;
  779. msg.fromAgentID = GetRequestingAgentID(remoteClient).Guid;
  780. // msg.fromAgentID = info.GroupID;
  781. msg.toAgentID = ejecteeID.Guid;
  782. //msg.timestamp = (uint)Util.UnixTimeSinceEpoch();
  783. msg.timestamp = 0;
  784. msg.fromAgentName = remoteClient.Name;
  785. msg.message = string.Format("You have been ejected from '{1}' by {0}.", remoteClient.Name, groupInfo.GroupName);
  786. msg.dialog = (byte)OpenMetaverse.InstantMessageDialog.MessageFromAgent;
  787. msg.fromGroup = false;
  788. msg.offline = (byte)0;
  789. msg.ParentEstateID = 0;
  790. msg.Position = Vector3.Zero;
  791. msg.RegionID = remoteClient.Scene.RegionInfo.RegionID.Guid;
  792. msg.binaryBucket = new byte[0];
  793. OutgoingInstantMessage(msg, ejecteeID);
  794. // Message to ejector
  795. // Interop, received special 210 code for ejecting a group member
  796. // this only works within the comms servers domain, and won't work hypergrid
  797. // TODO:FIXME: Use a presense server of some kind to find out where the
  798. // client actually is, and try contacting that region directly to notify them,
  799. // or provide the notification via xmlrpc update queue
  800. msg = new GridInstantMessage();
  801. msg.imSessionID = UUID.Zero.Guid;
  802. msg.fromAgentID = GetRequestingAgentID(remoteClient).Guid;
  803. msg.toAgentID = GetRequestingAgentID(remoteClient).Guid;
  804. msg.timestamp = 0;
  805. msg.fromAgentName = remoteClient.Name;
  806. if (account != null)
  807. {
  808. msg.message = string.Format("{2} has been ejected from '{1}' by {0}.", remoteClient.Name, groupInfo.GroupName, account.FirstName + " " + account.LastName);
  809. }
  810. else
  811. {
  812. msg.message = string.Format("{2} has been ejected from '{1}' by {0}.", remoteClient.Name, groupInfo.GroupName, "Unknown member");
  813. }
  814. msg.dialog = (byte)210; //interop
  815. msg.fromGroup = false;
  816. msg.offline = (byte)0;
  817. msg.ParentEstateID = 0;
  818. msg.Position = Vector3.Zero;
  819. msg.RegionID = remoteClient.Scene.RegionInfo.RegionID.Guid;
  820. msg.binaryBucket = new byte[0];
  821. OutgoingInstantMessage(msg, GetRequestingAgentID(remoteClient));
  822. // SL sends out messages to everyone in the group
  823. // Who all should receive updates and what should they be updated with?
  824. UpdateAllClientsWithGroupInfo(ejecteeID);
  825. }
  826. public void InviteGroupRequest(IClientAPI remoteClient, UUID groupID, UUID invitedAgentID, UUID roleID)
  827. {
  828. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  829. // Todo: Security check, probably also want to send some kind of notification
  830. UUID InviteID = UUID.Random();
  831. m_groupData.AddAgentToGroupInvite(GetRequestingAgentID(remoteClient), InviteID, groupID, roleID, invitedAgentID);
  832. // Check to see if the invite went through, if it did not then it's possible
  833. // the remoteClient did not validate or did not have permission to invite.
  834. GroupInviteInfo inviteInfo = m_groupData.GetAgentToGroupInvite(GetRequestingAgentID(remoteClient), InviteID);
  835. if (inviteInfo != null)
  836. {
  837. if (m_msgTransferModule != null)
  838. {
  839. Guid inviteUUID = InviteID.Guid;
  840. GridInstantMessage msg = new GridInstantMessage();
  841. msg.imSessionID = inviteUUID;
  842. // msg.fromAgentID = GetRequestingAgentID(remoteClient).Guid;
  843. msg.fromAgentID = groupID.Guid;
  844. msg.toAgentID = invitedAgentID.Guid;
  845. //msg.timestamp = (uint)Util.UnixTimeSinceEpoch();
  846. msg.timestamp = 0;
  847. msg.fromAgentName = remoteClient.Name;
  848. msg.message = string.Format("{0} has invited you to join a group. There is no cost to join this group.", remoteClient.Name);
  849. msg.dialog = (byte)OpenMetaverse.InstantMessageDialog.GroupInvitation;
  850. msg.fromGroup = true;
  851. msg.offline = (byte)0;
  852. msg.ParentEstateID = 0;
  853. msg.Position = Vector3.Zero;
  854. msg.RegionID = remoteClient.Scene.RegionInfo.RegionID.Guid;
  855. msg.binaryBucket = new byte[20];
  856. OutgoingInstantMessage(msg, invitedAgentID);
  857. }
  858. }
  859. }
  860. #endregion
  861. #region Client/Update Tools
  862. /// <summary>
  863. /// Try to find an active IClientAPI reference for agentID giving preference to root connections
  864. /// </summary>
  865. private IClientAPI GetActiveClient(UUID agentID)
  866. {
  867. IClientAPI child = null;
  868. // Try root avatar first
  869. foreach (Scene scene in m_sceneList)
  870. {
  871. if (scene.Entities.ContainsKey(agentID) &&
  872. scene.Entities[agentID] is ScenePresence)
  873. {
  874. ScenePresence user = (ScenePresence)scene.Entities[agentID];
  875. if (!user.IsChildAgent)
  876. {
  877. return user.ControllingClient;
  878. }
  879. else
  880. {
  881. child = user.ControllingClient;
  882. }
  883. }
  884. }
  885. // If we didn't find a root, then just return whichever child we found, or null if none
  886. return child;
  887. }
  888. /// <summary>
  889. /// Send 'remoteClient' the group membership 'data' for agent 'dataForAgentID'.
  890. /// </summary>
  891. private void SendGroupMembershipInfoViaCaps(IClientAPI remoteClient, UUID dataForAgentID, GroupMembershipData[] data)
  892. {
  893. if (m_debugEnabled) m_log.InfoFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  894. OSDArray AgentData = new OSDArray(1);
  895. OSDMap AgentDataMap = new OSDMap(1);
  896. AgentDataMap.Add("AgentID", OSD.FromUUID(dataForAgentID));
  897. AgentData.Add(AgentDataMap);
  898. OSDArray GroupData = new OSDArray(data.Length);
  899. OSDArray NewGroupData = new OSDArray(data.Length);
  900. foreach (GroupMembershipData membership in data)
  901. {
  902. if (GetRequestingAgentID(remoteClient) != dataForAgentID)
  903. {
  904. if (!membership.ListInProfile)
  905. {
  906. // If we're sending group info to remoteclient about another agent,
  907. // filter out groups the other agent doesn't want to share.
  908. continue;
  909. }
  910. }
  911. OSDMap GroupDataMap = new OSDMap(6);
  912. OSDMap NewGroupDataMap = new OSDMap(1);
  913. GroupDataMap.Add("GroupID", OSD.FromUUID(membership.GroupID));
  914. GroupDataMap.Add("GroupPowers", OSD.FromBinary(membership.GroupPowers));
  915. GroupDataMap.Add("AcceptNotices", OSD.FromBoolean(membership.AcceptNotices));
  916. GroupDataMap.Add("GroupInsigniaID", OSD.FromUUID(membership.GroupPicture));
  917. GroupDataMap.Add("Contribution", OSD.FromInteger(membership.Contribution));
  918. GroupDataMap.Add("GroupName", OSD.FromString(membership.GroupName));
  919. NewGroupDataMap.Add("ListInProfile", OSD.FromBoolean(membership.ListInProfile));
  920. GroupData.Add(GroupDataMap);
  921. NewGroupData.Add(NewGroupDataMap);
  922. }
  923. OSDMap llDataStruct = new OSDMap(3);
  924. llDataStruct.Add("AgentData", AgentData);
  925. llDataStruct.Add("GroupData", GroupData);
  926. llDataStruct.Add("NewGroupData", NewGroupData);
  927. if (m_debugEnabled)
  928. {
  929. m_log.InfoFormat("[GROUPS]: {0}", OSDParser.SerializeJsonString(llDataStruct));
  930. }
  931. IEventQueue queue = remoteClient.Scene.RequestModuleInterface<IEventQueue>();
  932. if (queue != null)
  933. {
  934. queue.Enqueue(EventQueueHelper.buildEvent("AgentGroupDataUpdate", llDataStruct), GetRequestingAgentID(remoteClient));
  935. }
  936. }
  937. private void SendScenePresenceUpdate(UUID AgentID, string Title)
  938. {
  939. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: Updating scene title for {0} with title: {1}", AgentID, Title);
  940. ScenePresence presence = null;
  941. foreach (Scene scene in m_sceneList)
  942. {
  943. presence = scene.GetScenePresence(AgentID);
  944. if (presence != null)
  945. {
  946. presence.Grouptitle = Title;
  947. // FixMe: Ter suggests a "Schedule" method that I can't find.
  948. presence.SendFullUpdateToAllClients();
  949. }
  950. }
  951. }
  952. /// <summary>
  953. /// Send updates to all clients who might be interested in groups data for dataForClientID
  954. /// </summary>
  955. private void UpdateAllClientsWithGroupInfo(UUID dataForClientID)
  956. {
  957. if (m_debugEnabled) m_log.InfoFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  958. // TODO: Probably isn't nessesary to update every client in every scene.
  959. // Need to examine client updates and do only what's nessesary.
  960. lock (m_sceneList)
  961. {
  962. foreach (Scene scene in m_sceneList)
  963. {
  964. scene.ForEachClient(delegate(IClientAPI client) { SendAgentGroupDataUpdate(client, dataForClientID); });
  965. }
  966. }
  967. }
  968. /// <summary>
  969. /// Update remoteClient with group information about dataForAgentID
  970. /// </summary>
  971. private void SendAgentGroupDataUpdate(IClientAPI remoteClient, UUID dataForAgentID)
  972. {
  973. if (m_debugEnabled) m_log.InfoFormat("[GROUPS]: {0} called for {1}", System.Reflection.MethodBase.GetCurrentMethod().Name, remoteClient.Name);
  974. // TODO: All the client update functions need to be reexamined because most do too much and send too much stuff
  975. OnAgentDataUpdateRequest(remoteClient, dataForAgentID, UUID.Zero);
  976. // Need to send a group membership update to the client
  977. // UDP version doesn't seem to behave nicely. But we're going to send it out here
  978. // with an empty group membership to hopefully remove groups being displayed due
  979. // to the core Groups Stub
  980. remoteClient.SendGroupMembership(new GroupMembershipData[0]);
  981. GroupMembershipData[] membershipArray = GetProfileListedGroupMemberships(remoteClient, dataForAgentID);
  982. SendGroupMembershipInfoViaCaps(remoteClient, dataForAgentID, membershipArray);
  983. remoteClient.SendAvatarGroupsReply(dataForAgentID, membershipArray);
  984. }
  985. /// <summary>
  986. /// Get a list of groups memberships for the agent that are marked "ListInProfile"
  987. /// </summary>
  988. /// <param name="dataForAgentID"></param>
  989. /// <returns></returns>
  990. private GroupMembershipData[] GetProfileListedGroupMemberships(IClientAPI requestingClient, UUID dataForAgentID)
  991. {
  992. List<GroupMembershipData> membershipData = m_groupData.GetAgentGroupMemberships(requestingClient.AgentId, dataForAgentID);
  993. GroupMembershipData[] membershipArray;
  994. if (requestingClient.AgentId != dataForAgentID)
  995. {
  996. Predicate<GroupMembershipData> showInProfile = delegate(GroupMembershipData membership)
  997. {
  998. return membership.ListInProfile;
  999. };
  1000. membershipArray = membershipData.FindAll(showInProfile).ToArray();
  1001. }
  1002. else
  1003. {
  1004. membershipArray = membershipData.ToArray();
  1005. }
  1006. if (m_debugEnabled)
  1007. {
  1008. m_log.InfoFormat("[GROUPS]: Get group membership information for {0} requested by {1}", dataForAgentID, requestingClient.AgentId);
  1009. foreach (GroupMembershipData membership in membershipArray)
  1010. {
  1011. m_log.InfoFormat("[GROUPS]: {0} :: {1} - {2} - {3}", dataForAgentID, membership.GroupName, membership.GroupTitle, membership.GroupPowers);
  1012. }
  1013. }
  1014. return membershipArray;
  1015. }
  1016. private void SendAgentDataUpdate(IClientAPI remoteClient, UUID dataForAgentID, UUID activeGroupID, string activeGroupName, ulong activeGroupPowers, string activeGroupTitle)
  1017. {
  1018. if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  1019. // TODO: All the client update functions need to be reexamined because most do too much and send too much stuff
  1020. UserAccount account = m_sceneList[0].UserAccountService.GetUserAccount(remoteClient.Scene.RegionInfo.ScopeID, dataForAgentID);
  1021. string firstname, lastname;
  1022. if (account != null)
  1023. {
  1024. firstname = account.FirstName;
  1025. lastname = account.LastName;
  1026. }
  1027. else
  1028. {
  1029. firstname = "Unknown";
  1030. lastname = "Unknown";
  1031. }
  1032. remoteClient.SendAgentDataUpdate(dataForAgentID, activeGroupID, firstname,
  1033. lastname, activeGroupPowers, activeGroupName,
  1034. activeGroupTitle);
  1035. }
  1036. #endregion
  1037. #region IM Backed Processes
  1038. private void OutgoingInstantMessage(GridInstantMessage msg, UUID msgTo)
  1039. {
  1040. if (m_debugEnabled) m_log.InfoFormat("[GROUPS]: {0} called", System.Reflection.MethodBase.GetCurrentMethod().Name);
  1041. IClientAPI localClient = GetActiveClient(msgTo);
  1042. if (localClient != null)
  1043. {
  1044. if (m_debugEnabled) m_log.InfoFormat("[GROUPS]: MsgTo ({0}) is local, delivering directly", localClient.Name);
  1045. localClient.SendInstantMessage(msg);
  1046. }
  1047. else
  1048. {
  1049. if (m_debugEnabled) m_log.InfoFormat("[GROUPS]: MsgTo ({0}) is not local, delivering via TransferModule", msgTo);
  1050. m_msgTransferModule.SendInstantMessage(msg, delegate(bool success) { if (m_debugEnabled) m_log.DebugFormat("[GROUPS]: Message Sent: {0}", success?"Succeeded":"Failed"); });
  1051. }
  1052. }
  1053. public void NotifyChange(UUID groupID)
  1054. {
  1055. // Notify all group members of a chnge in group roles and/or
  1056. // permissions
  1057. //
  1058. }
  1059. #endregion
  1060. private UUID GetRequestingAgentID(IClientAPI client)
  1061. {
  1062. UUID requestingAgentID = UUID.Zero;
  1063. if (client != null)
  1064. {
  1065. requestingAgentID = client.AgentId;
  1066. }
  1067. return requestingAgentID;
  1068. }
  1069. }
  1070. public class GroupNoticeInfo
  1071. {
  1072. public GroupNoticeData noticeData = new GroupNoticeData();
  1073. public UUID GroupID = UUID.Zero;
  1074. public string Message = string.Empty;
  1075. public byte[] BinaryBucket = new byte[0];
  1076. }
  1077. }