GroupsModule.cs 61 KB

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