GroupsModule.cs 66 KB

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