GroupsModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 OpenSim 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 libsecondlife;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Environment.Interfaces;
  35. using OpenSim.Region.Environment.Scenes;
  36. namespace OpenSim.Region.Environment.Modules.Avatar.Groups
  37. {
  38. public class GroupsModule : IRegionModule
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. private Dictionary<LLUUID, GroupList> m_grouplistmap = new Dictionary<LLUUID, GroupList>();
  42. private Dictionary<LLUUID, GroupData> m_groupmap = new Dictionary<LLUUID, GroupData>();
  43. private Dictionary<LLUUID, IClientAPI> m_iclientmap = new Dictionary<LLUUID, IClientAPI>();
  44. private Dictionary<LLUUID, GroupData> m_groupUUIDGroup = new Dictionary<LLUUID, GroupData>();
  45. private LLUUID opensimulatorGroupID = new LLUUID("00000000-68f9-1111-024e-222222111123");
  46. private List<Scene> m_scene = new List<Scene>();
  47. #region IRegionModule Members
  48. public void Initialise(Scene scene, IConfigSource config)
  49. {
  50. lock (m_scene)
  51. {
  52. m_scene.Add(scene);
  53. }
  54. scene.EventManager.OnNewClient += OnNewClient;
  55. scene.EventManager.OnClientClosed += OnClientClosed;
  56. scene.EventManager.OnGridInstantMessageToGroupsModule += OnGridInstantMessage;
  57. lock (m_groupUUIDGroup)
  58. {
  59. GroupData OpenSimulatorGroup = new GroupData();
  60. OpenSimulatorGroup.ActiveGroupTitle = "OpenSimulator Tester";
  61. OpenSimulatorGroup.GroupID = opensimulatorGroupID;
  62. OpenSimulatorGroup.groupName = "OpenSimulator Testing";
  63. OpenSimulatorGroup.ActiveGroupPowers = GroupPowers.LandAllowSetHome;
  64. OpenSimulatorGroup.GroupTitles.Add("OpenSimulator Tester");
  65. if (!m_groupUUIDGroup.ContainsKey(opensimulatorGroupID))
  66. m_groupUUIDGroup.Add(opensimulatorGroupID, OpenSimulatorGroup);
  67. }
  68. //scene.EventManager.
  69. }
  70. public void PostInitialise()
  71. {
  72. }
  73. public void Close()
  74. {
  75. m_log.Info("[GROUP]: Shutting down group module.");
  76. lock (m_iclientmap)
  77. {
  78. m_iclientmap.Clear();
  79. }
  80. lock (m_groupmap)
  81. {
  82. m_groupmap.Clear();
  83. }
  84. lock (m_grouplistmap)
  85. {
  86. m_grouplistmap.Clear();
  87. }
  88. GC.Collect();
  89. }
  90. public string Name
  91. {
  92. get { return "GroupsModule"; }
  93. }
  94. public bool IsSharedModule
  95. {
  96. get { return true; }
  97. }
  98. #endregion
  99. private void OnNewClient(IClientAPI client)
  100. {
  101. // Subscribe to instant messages
  102. client.OnInstantMessage += OnInstantMessage;
  103. client.OnAgentDataUpdateRequest += OnAgentDataUpdateRequest;
  104. client.OnUUIDGroupNameRequest += HandleUUIDGroupNameRequest;
  105. lock (m_iclientmap)
  106. {
  107. if (!m_iclientmap.ContainsKey(client.AgentId))
  108. {
  109. m_iclientmap.Add(client.AgentId, client);
  110. }
  111. }
  112. GroupData OpenSimulatorGroup = null;
  113. lock (m_groupUUIDGroup)
  114. {
  115. OpenSimulatorGroup = m_groupUUIDGroup[opensimulatorGroupID];
  116. if (!OpenSimulatorGroup.GroupMembers.Contains(client.AgentId))
  117. {
  118. OpenSimulatorGroup.GroupMembers.Add(client.AgentId);
  119. m_groupUUIDGroup[opensimulatorGroupID] = OpenSimulatorGroup;
  120. }
  121. }
  122. lock (m_groupmap)
  123. {
  124. if (!m_groupmap.ContainsKey(client.AgentId))
  125. {
  126. m_groupmap.Add(client.AgentId, OpenSimulatorGroup);
  127. }
  128. }
  129. GroupList testGroupList = new GroupList();
  130. testGroupList.m_GroupList.Add(OpenSimulatorGroup.GroupID);
  131. lock (m_grouplistmap)
  132. {
  133. if (!m_grouplistmap.ContainsKey(client.AgentId))
  134. {
  135. m_grouplistmap.Add(client.AgentId, testGroupList);
  136. }
  137. }
  138. m_log.Info("[GROUP]: Adding " + client.Name + " to " + OpenSimulatorGroup.groupName + " ");
  139. GroupData[] updateGroups = new GroupData[1];
  140. updateGroups[0] = OpenSimulatorGroup;
  141. client.SendGroupMembership(updateGroups);
  142. }
  143. private void OnAgentDataUpdateRequest(IClientAPI remoteClient, LLUUID AgentID, LLUUID SessionID)
  144. {
  145. // Adam, this is one of those impossible to refactor items without resorting to .Split hackery
  146. string firstname = remoteClient.FirstName;
  147. string lastname = remoteClient.LastName;
  148. LLUUID ActiveGroupID = LLUUID.Zero;
  149. uint ActiveGroupPowers = 0;
  150. string ActiveGroupName = "OpenSimulator Tester";
  151. string ActiveGroupTitle = "I IZ N0T";
  152. bool foundUser = false;
  153. lock (m_iclientmap)
  154. {
  155. if (m_iclientmap.ContainsKey(remoteClient.AgentId))
  156. {
  157. foundUser = true;
  158. }
  159. }
  160. if (foundUser)
  161. {
  162. lock (m_groupmap)
  163. {
  164. if (m_groupmap.ContainsKey(remoteClient.AgentId))
  165. {
  166. GroupData grp = m_groupmap[remoteClient.AgentId];
  167. if (grp != null)
  168. {
  169. ActiveGroupID = grp.GroupID;
  170. ActiveGroupName = grp.groupName;
  171. ActiveGroupPowers = grp.groupPowers;
  172. ActiveGroupTitle = grp.ActiveGroupTitle;
  173. }
  174. remoteClient.SendAgentDataUpdate(AgentID, ActiveGroupID, firstname, lastname, ActiveGroupPowers, ActiveGroupName, ActiveGroupTitle);
  175. }
  176. }
  177. }
  178. }
  179. private void OnInstantMessage(IClientAPI client, LLUUID fromAgentID,
  180. LLUUID fromAgentSession, LLUUID toAgentID,
  181. LLUUID imSessionID, uint timestamp, string fromAgentName,
  182. string message, byte dialog, bool fromGroup, byte offline,
  183. uint ParentEstateID, LLVector3 Position, LLUUID RegionID,
  184. byte[] binaryBucket)
  185. {
  186. }
  187. private void OnGridInstantMessage(GridInstantMessage msg)
  188. {
  189. // Trigger the above event handler
  190. OnInstantMessage(null, new LLUUID(msg.fromAgentID), new LLUUID(msg.fromAgentSession),
  191. new LLUUID(msg.toAgentID), new LLUUID(msg.imSessionID), msg.timestamp, msg.fromAgentName,
  192. msg.message, msg.dialog, msg.fromGroup, msg.offline, msg.ParentEstateID,
  193. new LLVector3(msg.Position.x, msg.Position.y, msg.Position.z), new LLUUID(msg.RegionID),
  194. msg.binaryBucket);
  195. }
  196. private void HandleUUIDGroupNameRequest(LLUUID id,IClientAPI remote_client)
  197. {
  198. string groupnamereply = "Unknown";
  199. LLUUID groupUUID = LLUUID.Zero;
  200. lock (m_groupUUIDGroup)
  201. {
  202. if (m_groupUUIDGroup.ContainsKey(id))
  203. {
  204. GroupData grp = m_groupUUIDGroup[id];
  205. groupnamereply = grp.groupName;
  206. groupUUID = grp.GroupID;
  207. }
  208. }
  209. remote_client.SendGroupNameReply(groupUUID, groupnamereply);
  210. }
  211. private void OnClientClosed(LLUUID agentID)
  212. {
  213. lock (m_iclientmap)
  214. {
  215. if (m_iclientmap.ContainsKey(agentID))
  216. {
  217. IClientAPI cli = m_iclientmap[agentID];
  218. if (cli != null)
  219. {
  220. m_log.Info("[GROUP]: Removing all reference to groups for " + cli.Name);
  221. }
  222. else
  223. {
  224. m_log.Info("[GROUP]: Removing all reference to groups for " + agentID.ToString());
  225. }
  226. m_iclientmap.Remove(agentID);
  227. }
  228. }
  229. lock (m_groupmap)
  230. {
  231. if (m_groupmap.ContainsKey(agentID))
  232. {
  233. m_groupmap.Remove(agentID);
  234. }
  235. }
  236. lock (m_grouplistmap)
  237. {
  238. if (m_grouplistmap.ContainsKey(agentID))
  239. {
  240. m_grouplistmap.Remove(agentID);
  241. }
  242. }
  243. GC.Collect();
  244. }
  245. }
  246. }