GroupsModule.cs 65 KB

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