GroupsModule.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 log4net;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. using Mono.Addins;
  37. namespace OpenSim.Region.CoreModules.Avatar.Groups
  38. {
  39. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "GroupsModule")]
  40. public class GroupsModule : ISharedRegionModule
  41. {
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private Dictionary<UUID, GroupMembershipData> m_GroupMap =
  45. new Dictionary<UUID, GroupMembershipData>();
  46. private Dictionary<UUID, IClientAPI> m_ClientMap =
  47. new Dictionary<UUID, IClientAPI>();
  48. private UUID opensimulatorGroupID =
  49. new UUID("00000000-68f9-1111-024e-222222111123");
  50. private List<Scene> m_SceneList = new List<Scene>();
  51. private static GroupMembershipData osGroup =
  52. new GroupMembershipData();
  53. private bool m_Enabled = false;
  54. #region ISharedRegionModule Members
  55. public void Initialise(IConfigSource config)
  56. {
  57. IConfig groupsConfig = config.Configs["Groups"];
  58. if (groupsConfig == null)
  59. {
  60. m_log.Info("[GROUPS]: No configuration found. Using defaults");
  61. }
  62. else
  63. {
  64. m_Enabled = groupsConfig.GetBoolean("Enabled", false);
  65. if (!m_Enabled)
  66. {
  67. m_log.Info("[GROUPS]: Groups disabled in configuration");
  68. return;
  69. }
  70. if (groupsConfig.GetString("Module", "Default") != "Default")
  71. {
  72. m_Enabled = false;
  73. return;
  74. }
  75. }
  76. }
  77. public void AddRegion(Scene scene)
  78. {
  79. if (!m_Enabled)
  80. return;
  81. lock (m_SceneList)
  82. {
  83. if (!m_SceneList.Contains(scene))
  84. {
  85. if (m_SceneList.Count == 0)
  86. {
  87. osGroup.GroupID = opensimulatorGroupID;
  88. osGroup.GroupName = "OpenSimulator Testing";
  89. osGroup.GroupPowers =
  90. (uint)(GroupPowers.AllowLandmark |
  91. GroupPowers.AllowSetHome);
  92. m_GroupMap[opensimulatorGroupID] = osGroup;
  93. }
  94. m_SceneList.Add(scene);
  95. }
  96. }
  97. scene.EventManager.OnNewClient += OnNewClient;
  98. scene.EventManager.OnClientClosed += OnClientClosed;
  99. // scene.EventManager.OnIncomingInstantMessage += OnGridInstantMessage;
  100. }
  101. public void RemoveRegion(Scene scene)
  102. {
  103. if (!m_Enabled)
  104. return;
  105. lock (m_SceneList)
  106. {
  107. if (m_SceneList.Contains(scene))
  108. m_SceneList.Remove(scene);
  109. }
  110. scene.EventManager.OnNewClient -= OnNewClient;
  111. scene.EventManager.OnClientClosed -= OnClientClosed;
  112. }
  113. public void RegionLoaded(Scene scene)
  114. {
  115. }
  116. public void PostInitialise()
  117. {
  118. }
  119. public void Close()
  120. {
  121. if (!m_Enabled)
  122. return;
  123. // m_log.Debug("[GROUPS]: Shutting down group module.");
  124. lock (m_ClientMap)
  125. {
  126. m_ClientMap.Clear();
  127. }
  128. lock (m_GroupMap)
  129. {
  130. m_GroupMap.Clear();
  131. }
  132. }
  133. public string Name
  134. {
  135. get { return "GroupsModule"; }
  136. }
  137. public Type ReplaceableInterface
  138. {
  139. get { return null; }
  140. }
  141. #endregion
  142. private void OnNewClient(IClientAPI client)
  143. {
  144. // Subscribe to instant messages
  145. // client.OnInstantMessage += OnInstantMessage;
  146. client.OnAgentDataUpdateRequest += OnAgentDataUpdateRequest;
  147. client.OnUUIDGroupNameRequest += HandleUUIDGroupNameRequest;
  148. lock (m_ClientMap)
  149. {
  150. if (!m_ClientMap.ContainsKey(client.AgentId))
  151. {
  152. m_ClientMap.Add(client.AgentId, client);
  153. }
  154. }
  155. }
  156. private void OnAgentDataUpdateRequest(IClientAPI remoteClient,
  157. UUID AgentID, UUID SessionID)
  158. {
  159. UUID ActiveGroupID;
  160. string ActiveGroupName;
  161. ulong ActiveGroupPowers;
  162. string firstname = remoteClient.FirstName;
  163. string lastname = remoteClient.LastName;
  164. string ActiveGroupTitle = "I IZ N0T";
  165. ActiveGroupID = osGroup.GroupID;
  166. ActiveGroupName = osGroup.GroupName;
  167. ActiveGroupPowers = osGroup.GroupPowers;
  168. remoteClient.SendAgentDataUpdate(AgentID, ActiveGroupID, firstname,
  169. lastname, ActiveGroupPowers, ActiveGroupName,
  170. ActiveGroupTitle);
  171. }
  172. // private void OnInstantMessage(IClientAPI client, GridInstantMessage im)
  173. // {
  174. // }
  175. // private void OnGridInstantMessage(GridInstantMessage msg)
  176. // {
  177. // // Trigger the above event handler
  178. // OnInstantMessage(null, msg);
  179. // }
  180. private void HandleUUIDGroupNameRequest(UUID id,IClientAPI remote_client)
  181. {
  182. string groupnamereply = "Unknown";
  183. UUID groupUUID = UUID.Zero;
  184. lock (m_GroupMap)
  185. {
  186. if (m_GroupMap.ContainsKey(id))
  187. {
  188. GroupMembershipData grp = m_GroupMap[id];
  189. groupnamereply = grp.GroupName;
  190. groupUUID = grp.GroupID;
  191. }
  192. }
  193. remote_client.SendGroupNameReply(groupUUID, groupnamereply);
  194. }
  195. public GroupMembershipData[] GetMembershipData(UUID agentID)
  196. {
  197. GroupMembershipData[] updateGroups = new GroupMembershipData[1];
  198. updateGroups[0] = osGroup;
  199. return updateGroups;
  200. }
  201. public GroupMembershipData GetActiveMembershipData(UUID agentID)
  202. {
  203. return osGroup;
  204. }
  205. private void OnClientClosed(UUID agentID, Scene scene)
  206. {
  207. lock (m_ClientMap)
  208. {
  209. if (m_ClientMap.ContainsKey(agentID))
  210. {
  211. // IClientAPI cli = m_ClientMap[agentID];
  212. // if (cli != null)
  213. // {
  214. // //m_log.Info("[GROUPS]: Removing all reference to groups for " + cli.Name);
  215. // }
  216. // else
  217. // {
  218. // //m_log.Info("[GROUPS]: Removing all reference to groups for " + agentID.ToString());
  219. // }
  220. m_ClientMap.Remove(agentID);
  221. }
  222. }
  223. }
  224. }
  225. }