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