GroupsModule.cs 7.6 KB

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