GroupsModule.cs 65 KB

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