GodsModule.cs 14 KB

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