GroupsModule.cs 63 KB

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