GroupsModule.cs 59 KB

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