GroupsModule.cs 60 KB

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