GroupsModule.cs 59 KB

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