GroupsModule.cs 58 KB

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