GroupsModule.cs 63 KB

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