GodsModule.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.Net;
  29. using System.Reflection;
  30. using log4net;
  31. using Mono.Addins;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenMetaverse.StructuredData;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Region.Framework.Scenes;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using Caps = OpenSim.Framework.Capabilities.Caps;
  40. using OSDArray = OpenMetaverse.StructuredData.OSDArray;
  41. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  42. namespace OpenSim.Region.CoreModules.Avatar.Gods
  43. {
  44. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "GodsModule")]
  45. public class GodsModule : INonSharedRegionModule, IGodsModule
  46. {
  47. private static readonly ILog m_log =
  48. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. /// <summary>Special UUID for actions that apply to all agents</summary>
  50. private static readonly UUID ALL_AGENTS = new UUID("44e87126-e794-4ded-05b3-7c42da3d5cdb");
  51. private static readonly UUID UUID_GRID_GOD = new UUID("6571e388-6218-4574-87db-f9379718315e");
  52. protected Scene m_scene;
  53. protected IDialogModule m_dialogModule;
  54. public void Initialise(IConfigSource source)
  55. {
  56. }
  57. public void AddRegion(Scene scene)
  58. {
  59. m_scene = scene;
  60. m_scene.RegisterModuleInterface<IGodsModule>(this);
  61. m_scene.EventManager.OnNewClient += SubscribeToClientEvents;
  62. m_scene.EventManager.OnRegisterCaps += OnRegisterCaps;
  63. scene.EventManager.OnIncomingInstantMessage +=
  64. OnIncomingInstantMessage;
  65. }
  66. public void RemoveRegion(Scene scene)
  67. {
  68. m_scene.UnregisterModuleInterface<IGodsModule>(this);
  69. m_scene.EventManager.OnNewClient -= SubscribeToClientEvents;
  70. m_scene = null;
  71. }
  72. public void RegionLoaded(Scene scene)
  73. {
  74. m_dialogModule = m_scene.RequestModuleInterface<IDialogModule>();
  75. }
  76. public void Close() {}
  77. public string Name { get { return "Gods Module"; } }
  78. public Type ReplaceableInterface
  79. {
  80. get { return null; }
  81. }
  82. public void SubscribeToClientEvents(IClientAPI client)
  83. {
  84. client.OnGodKickUser += KickUser;
  85. client.OnRequestGodlikePowers += RequestGodlikePowers;
  86. }
  87. public void UnsubscribeFromClientEvents(IClientAPI client)
  88. {
  89. client.OnGodKickUser -= KickUser;
  90. client.OnRequestGodlikePowers -= RequestGodlikePowers;
  91. }
  92. private void OnRegisterCaps(UUID agentID, Caps caps)
  93. {
  94. caps.RegisterSimpleHandler("UntrustedSimulatorMessage", new SimpleStreamHandler("/C" + UUID.Random(), HandleUntrustedSimulatorMessage));
  95. }
  96. private void HandleUntrustedSimulatorMessage(IOSHttpRequest request, IOSHttpResponse response)
  97. {
  98. if (request.HttpMethod != "POST")
  99. {
  100. response.StatusCode = (int)HttpStatusCode.NotFound;
  101. return;
  102. }
  103. OSDMap osd;
  104. try
  105. {
  106. osd = (OSDMap)OSDParser.DeserializeLLSDXml(request.InputStream);
  107. string message = osd["message"].AsString();
  108. if (message == "GodKickUser")
  109. {
  110. OSDMap body = (OSDMap)osd["body"];
  111. OSDArray userInfo = (OSDArray)body["UserInfo"];
  112. OSDMap userData = (OSDMap)userInfo[0];
  113. UUID agentID = userData["AgentID"].AsUUID();
  114. UUID godID = userData["GodID"].AsUUID();
  115. UUID godSessionID = userData["GodSessionID"].AsUUID();
  116. uint kickFlags = userData["KickFlags"].AsUInteger();
  117. string reason = userData["Reason"].AsString();
  118. ScenePresence god = m_scene.GetScenePresence(godID);
  119. if (god == null || god.ControllingClient.SessionId != godSessionID)
  120. {
  121. response.StatusCode = (int)HttpStatusCode.Unauthorized;
  122. return;
  123. }
  124. KickUser(godID, agentID, kickFlags, reason);
  125. }
  126. else
  127. {
  128. m_log.ErrorFormat("[GOD]: Unhandled UntrustedSimulatorMessage: {0}", message);
  129. response.StatusCode = (int)HttpStatusCode.BadRequest;
  130. return;
  131. }
  132. }
  133. catch
  134. {
  135. response.StatusCode = (int)HttpStatusCode.BadRequest;
  136. return;
  137. }
  138. response.StatusCode = (int)HttpStatusCode.OK;
  139. }
  140. public void RequestGodlikePowers(
  141. UUID agentID, UUID sessionID, UUID token, bool godLike)
  142. {
  143. ScenePresence sp = m_scene.GetScenePresence(agentID);
  144. if(sp == null || sp.IsDeleted || sp.IsNPC)
  145. return;
  146. if (sessionID != sp.ControllingClient.SessionId)
  147. return;
  148. sp.GrantGodlikePowers(token, godLike);
  149. if (godLike && !sp.IsViewerUIGod && m_dialogModule != null)
  150. m_dialogModule.SendAlertToUser(agentID, "Request for god powers denied");
  151. }
  152. public void KickUser(UUID godID, UUID agentID, uint kickflags, byte[] reason)
  153. {
  154. KickUser(godID, agentID, kickflags, Utils.BytesToString(reason));
  155. }
  156. /// <summary>
  157. /// Kicks or freezes User specified from the simulator. This logs them off of the grid
  158. /// </summary>
  159. /// <param name="godID">The person doing the kicking</param>
  160. /// <param name="agentID">the person that is being kicked</param>
  161. /// <param name="kickflags">Tells what to do to the user</param>
  162. /// <param name="reason">The message to send to the user after it's been turned into a field</param>
  163. public void KickUser(UUID godID, UUID agentID, uint kickflags, string reason)
  164. {
  165. // assuming automatic god rights on this for fast griefing reaction
  166. // this is also needed for kick via message
  167. if(!m_scene.Permissions.IsGod(godID))
  168. return;
  169. int godlevel = 200;
  170. // update level so higher gods can kick lower ones
  171. ScenePresence god = m_scene.GetScenePresence(godID);
  172. if(god != null && god.GodController.GodLevel > godlevel)
  173. godlevel = god.GodController.GodLevel;
  174. if(agentID == ALL_AGENTS)
  175. {
  176. m_scene.ForEachRootScenePresence(delegate(ScenePresence p)
  177. {
  178. if (p.UUID != godID)
  179. {
  180. if(godlevel > p.GodController.GodLevel)
  181. doKickmodes(godID, p, kickflags, reason);
  182. else if(m_dialogModule != null)
  183. m_dialogModule.SendAlertToUser(p.UUID, "Kick from " + godID.ToString() + " ignored, kick reason: " + reason);
  184. }
  185. });
  186. return;
  187. }
  188. ScenePresence sp = m_scene.GetScenePresence(agentID);
  189. if (sp == null || sp.IsChildAgent)
  190. {
  191. IMessageTransferModule transferModule =
  192. m_scene.RequestModuleInterface<IMessageTransferModule>();
  193. if (transferModule != null)
  194. {
  195. m_log.DebugFormat("[GODS]: Sending nonlocal kill for agent {0}", agentID);
  196. transferModule.SendInstantMessage(new GridInstantMessage(
  197. m_scene, godID, "God", agentID, (byte)250, false,
  198. reason, UUID.Zero, true,
  199. new Vector3(), new byte[] {(byte)kickflags}, true),
  200. delegate(bool success) {} );
  201. }
  202. return;
  203. }
  204. if (godlevel <= sp.GodController.GodLevel) // no god wars
  205. {
  206. if(m_dialogModule != null)
  207. m_dialogModule.SendAlertToUser(sp.UUID, "Kick from " + godID.ToString() + " ignored, kick reason: " + reason);
  208. return;
  209. }
  210. if(sp.UUID == godID)
  211. return;
  212. doKickmodes(godID, sp, kickflags, reason);
  213. }
  214. private void doKickmodes(UUID godID, ScenePresence sp, uint kickflags, string reason)
  215. {
  216. switch (kickflags)
  217. {
  218. case 0:
  219. KickPresence(sp, reason);
  220. break;
  221. case 1:
  222. sp.AllowMovement = false;
  223. if(m_dialogModule != null)
  224. {
  225. m_dialogModule.SendAlertToUser(sp.UUID, reason);
  226. m_dialogModule.SendAlertToUser(godID, "User Frozen");
  227. }
  228. break;
  229. case 2:
  230. sp.AllowMovement = true;
  231. if(m_dialogModule != null)
  232. {
  233. m_dialogModule.SendAlertToUser(sp.UUID, reason);
  234. m_dialogModule.SendAlertToUser(godID, "User Unfrozen");
  235. }
  236. break;
  237. default:
  238. break;
  239. }
  240. }
  241. private void KickPresence(ScenePresence sp, string reason)
  242. {
  243. if(sp.IsDeleted || sp.IsChildAgent)
  244. return;
  245. if (sp.IsNPC)
  246. {
  247. INPCModule npcmodule = sp.Scene.RequestModuleInterface<INPCModule>();
  248. if (npcmodule != null)
  249. {
  250. npcmodule.DeleteNPC(sp.UUID, sp.Scene);
  251. return;
  252. }
  253. }
  254. sp.ControllingClient.Kick(reason);
  255. sp.Scene.CloseAgent(sp.UUID, true);
  256. }
  257. public void GridKickUser(UUID agentID, string reason)
  258. {
  259. int godlevel = 240; // grid god default
  260. ScenePresence sp = m_scene.GetScenePresence(agentID);
  261. if (sp == null || sp.IsChildAgent)
  262. {
  263. IMessageTransferModule transferModule =
  264. m_scene.RequestModuleInterface<IMessageTransferModule>();
  265. if (transferModule != null)
  266. {
  267. m_log.DebugFormat("[GODS]: Sending nonlocal kill for agent {0}", agentID);
  268. transferModule.SendInstantMessage(new GridInstantMessage(
  269. m_scene, UUID_GRID_GOD, "GRID", agentID, (byte)250, false,
  270. reason, UUID.Zero, true,
  271. new Vector3(), new byte[] {0}, true),
  272. delegate(bool success) {} );
  273. }
  274. return;
  275. }
  276. if(sp.IsDeleted)
  277. return;
  278. if (godlevel <= sp.GodController.GodLevel) // no god wars
  279. {
  280. if(m_dialogModule != null)
  281. m_dialogModule.SendAlertToUser(sp.UUID, "GRID kick detected and ignored, kick reason: " + reason);
  282. return;
  283. }
  284. if (sp.IsNPC)
  285. {
  286. INPCModule npcmodule = sp.Scene.RequestModuleInterface<INPCModule>();
  287. if (npcmodule != null)
  288. {
  289. npcmodule.DeleteNPC(sp.UUID, sp.Scene);
  290. return;
  291. }
  292. }
  293. sp.ControllingClient.Kick(reason);
  294. sp.Scene.CloseAgent(sp.UUID, true);
  295. }
  296. private void OnIncomingInstantMessage(GridInstantMessage msg)
  297. {
  298. if (msg.dialog == (uint)250) // Nonlocal kick
  299. {
  300. UUID agentID = new UUID(msg.toAgentID);
  301. string reason = msg.message;
  302. UUID godID = new UUID(msg.fromAgentID);
  303. uint kickMode = (uint)msg.binaryBucket[0];
  304. if(godID == UUID_GRID_GOD)
  305. GridKickUser(agentID, reason);
  306. else
  307. KickUser(godID, agentID, kickMode, reason);
  308. }
  309. }
  310. }
  311. }