GroupsModule.cs 66 KB

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