GroupsModule.cs 66 KB

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