GroupsModule.cs 62 KB

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