GroupsModule.cs 66 KB

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