MuteListModule.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.Text;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Servers;
  36. using OpenSim.Framework.Client;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using Mono.Addins;
  40. using OpenSim.Server.Base;
  41. using OpenSim.Services.Interfaces;
  42. namespace OpenSim.Region.CoreModules.Avatar.InstantMessage
  43. {
  44. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "MuteListModule")]
  45. public class MuteListModule : ISharedRegionModule
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(
  48. MethodBase.GetCurrentMethod().DeclaringType);
  49. protected bool m_Enabled = false;
  50. protected List<Scene> m_SceneList = new List<Scene>();
  51. protected IMuteListService m_service = null;
  52. private IUserManagement m_userManagementModule;
  53. public void Initialise(IConfigSource config)
  54. {
  55. IConfig cnf = config.Configs["Messaging"];
  56. if (cnf == null)
  57. return;
  58. if (cnf.GetString("MuteListModule", "None") != "MuteListModule")
  59. return;
  60. m_Enabled = true;
  61. }
  62. public void AddRegion(Scene scene)
  63. {
  64. }
  65. public void RegionLoaded(Scene scene)
  66. {
  67. if (!m_Enabled)
  68. return;
  69. IXfer xfer = scene.RequestModuleInterface<IXfer>();
  70. if (xfer == null)
  71. {
  72. m_log.ErrorFormat("[MuteListModule]: Xfer not available in region {0}. Module Disabled", scene.Name);
  73. m_Enabled = false;
  74. return;
  75. }
  76. IMuteListService srv = scene.RequestModuleInterface<IMuteListService>();
  77. if(srv == null)
  78. {
  79. m_log.ErrorFormat("[MuteListModule]: MuteListService not available in region {0}. Module Disabled", scene.Name);
  80. m_Enabled = false;
  81. return;
  82. }
  83. lock (m_SceneList)
  84. {
  85. if(m_service == null)
  86. m_service = srv;
  87. if(m_userManagementModule == null)
  88. m_userManagementModule = scene.RequestModuleInterface<IUserManagement>();
  89. m_SceneList.Add(scene);
  90. scene.EventManager.OnNewClient += OnNewClient;
  91. }
  92. }
  93. public void RemoveRegion(Scene scene)
  94. {
  95. lock (m_SceneList)
  96. {
  97. if(m_SceneList.Contains(scene))
  98. {
  99. m_SceneList.Remove(scene);
  100. scene.EventManager.OnNewClient -= OnNewClient;
  101. }
  102. }
  103. }
  104. public void PostInitialise()
  105. {
  106. if (!m_Enabled)
  107. return;
  108. m_log.Debug("[MuteListModule]: enabled");
  109. }
  110. public string Name
  111. {
  112. get { return "MuteListModule"; }
  113. }
  114. public Type ReplaceableInterface
  115. {
  116. get { return null; }
  117. }
  118. public void Close()
  119. {
  120. }
  121. private bool IsForeign(IClientAPI client)
  122. {
  123. if(m_userManagementModule == null)
  124. return false; // we can't check
  125. return !m_userManagementModule.IsLocalGridUser(client.AgentId);
  126. }
  127. private void OnNewClient(IClientAPI client)
  128. {
  129. client.OnMuteListRequest += OnMuteListRequest;
  130. client.OnUpdateMuteListEntry += OnUpdateMuteListEntry;
  131. client.OnRemoveMuteListEntry += OnRemoveMuteListEntry;
  132. }
  133. private void OnMuteListRequest(IClientAPI client, uint crc)
  134. {
  135. if (!m_Enabled || IsForeign(client))
  136. {
  137. if(crc == 0)
  138. client.SendEmpytMuteList();
  139. else
  140. client.SendUseCachedMuteList();
  141. return;
  142. }
  143. IXfer xfer = client.Scene.RequestModuleInterface<IXfer>();
  144. if (xfer == null)
  145. {
  146. if(crc == 0)
  147. client.SendEmpytMuteList();
  148. else
  149. client.SendUseCachedMuteList();
  150. return;
  151. }
  152. Byte[] data = m_service.MuteListRequest(client.AgentId, crc);
  153. if (data == null)
  154. {
  155. if(crc == 0)
  156. client.SendEmpytMuteList();
  157. else
  158. client.SendUseCachedMuteList();
  159. return;
  160. }
  161. if (data.Length == 0)
  162. {
  163. client.SendEmpytMuteList();
  164. return;
  165. }
  166. if (data.Length == 1)
  167. {
  168. if(crc == 0)
  169. client.SendEmpytMuteList();
  170. else
  171. client.SendUseCachedMuteList();
  172. return;
  173. }
  174. string filename = "mutes" + client.AgentId.ToString();
  175. xfer.AddNewFile(filename, data);
  176. client.SendMuteListUpdate(filename);
  177. }
  178. private void OnUpdateMuteListEntry(IClientAPI client, UUID muteID, string muteName, int muteType, uint muteFlags)
  179. {
  180. if (!m_Enabled || IsForeign(client))
  181. return;
  182. UUID agentID = client.AgentId;
  183. if(muteType == 1) // agent
  184. {
  185. if(agentID == muteID)
  186. return;
  187. if(m_SceneList[0].Permissions.IsAdministrator(muteID))
  188. {
  189. OnMuteListRequest(client, 0);
  190. return;
  191. }
  192. }
  193. MuteData mute = new MuteData();
  194. mute.AgentID = agentID;
  195. mute.MuteID = muteID;
  196. mute.MuteName = muteName;
  197. mute.MuteType = muteType;
  198. mute.MuteFlags = (int)muteFlags;
  199. mute.Stamp = Util.UnixTimeSinceEpoch();
  200. m_service.UpdateMute(mute);
  201. }
  202. private void OnRemoveMuteListEntry(IClientAPI client, UUID muteID, string muteName)
  203. {
  204. if (!m_Enabled || IsForeign(client))
  205. return;
  206. m_service.RemoveMute(client.AgentId, muteID, muteName);
  207. }
  208. }
  209. }