1
0

RegionConsoleModule.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.Collections.Concurrent;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.IO;
  33. using log4net;
  34. using Nini.Config;
  35. using Mono.Addins;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Console;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Region.Framework.Interfaces;
  42. using OpenSim.Region.Framework.Scenes;
  43. using Caps = OpenSim.Framework.Capabilities.Caps;
  44. namespace OpenSim.Region.ClientStack.Linden
  45. {
  46. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "RegionConsoleModule")]
  47. public class RegionConsoleModule : INonSharedRegionModule, IRegionConsole
  48. {
  49. // private static readonly ILog m_log =
  50. // LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. private Scene m_scene;
  52. private IEventQueue m_eventQueue;
  53. private Commands m_commands = new Commands();
  54. public ICommands Commands { get { return m_commands; } }
  55. ConcurrentDictionary<UUID, OnOutputDelegate> currentConsoles = new ConcurrentDictionary<UUID, OnOutputDelegate>();
  56. public event ConsoleMessage OnConsoleMessage;
  57. public void Initialise(IConfigSource source)
  58. {
  59. m_commands.AddCommand( "Help", false, "help", "help [<item>]", "Display help on a particular command or on a list of commands in a category", Help);
  60. }
  61. public void AddRegion(Scene s)
  62. {
  63. m_scene = s;
  64. m_scene.RegisterModuleInterface<IRegionConsole>(this);
  65. }
  66. public void RemoveRegion(Scene s)
  67. {
  68. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  69. m_scene = null;
  70. }
  71. public void RegionLoaded(Scene s)
  72. {
  73. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  74. m_eventQueue = m_scene.RequestModuleInterface<IEventQueue>();
  75. }
  76. public void PostInitialise()
  77. {
  78. }
  79. public void Close() { }
  80. public string Name { get { return "RegionConsoleModule"; } }
  81. public Type ReplaceableInterface
  82. {
  83. get { return null; }
  84. }
  85. public void RegisterCaps(UUID agentID, Caps caps)
  86. {
  87. //if (!m_scene.RegionInfo.EstateSettings.IsEstateManagerOrOwner(agentID) && !m_scene.Permissions.IsGod(agentID))
  88. // return;
  89. caps.RegisterSimpleHandler("SimConsoleAsync",
  90. new ConsoleHandler("/" + UUID.Random(), "SimConsoleAsync", agentID, this, m_scene));
  91. }
  92. public void AddConsole(UUID agentID, OnOutputDelegate console)
  93. {
  94. if(currentConsoles.TryAdd(agentID, console))
  95. MainConsole.Instance.OnOutput += console;
  96. }
  97. public void RemoveConsole(UUID agentID)
  98. {
  99. if(currentConsoles.TryRemove(agentID, out OnOutputDelegate console))
  100. MainConsole.Instance.OnOutput -= console;
  101. }
  102. public void SendConsoleOutput(UUID agentID, string message)
  103. {
  104. if (!m_scene.TryGetScenePresence(agentID, out ScenePresence sp) || sp.IsChildAgent || sp.IsDeleted)
  105. {
  106. RemoveConsole(agentID);
  107. return;
  108. }
  109. m_eventQueue.Enqueue(m_eventQueue.BuildEvent("SimConsoleResponse", OSD.FromString(message)), agentID);
  110. OnConsoleMessage?.Invoke( agentID, message);
  111. }
  112. public bool RunCommand(string command, UUID invokerID)
  113. {
  114. string[] parts = Parser.Parse(command);
  115. Array.Resize(ref parts, parts.Length + 1);
  116. parts[parts.Length - 1] = invokerID.ToString();
  117. if (m_commands.Resolve(parts).Length == 0)
  118. return false;
  119. return true;
  120. }
  121. private void Help(string module, string[] cmd)
  122. {
  123. UUID agentID = new UUID(cmd[cmd.Length - 1]);
  124. Array.Resize(ref cmd, cmd.Length - 1);
  125. List<string> help = Commands.GetHelp(cmd);
  126. string reply = String.Empty;
  127. foreach (string s in help)
  128. {
  129. reply += s + "\n";
  130. }
  131. SendConsoleOutput(agentID, reply);
  132. }
  133. public void AddCommand(string module, bool shared, string command, string help, string longhelp, CommandDelegate fn)
  134. {
  135. m_commands.AddCommand(module, shared, command, help, longhelp, fn);
  136. }
  137. }
  138. public class ConsoleHandler : SimpleStreamHandler
  139. {
  140. // private static readonly ILog m_log =
  141. // LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  142. private RegionConsoleModule m_consoleModule;
  143. private UUID m_agentID;
  144. private bool m_isGod;
  145. private Scene m_scene;
  146. private bool m_consoleIsOn = false;
  147. public ConsoleHandler(string path, string name, UUID agentID, RegionConsoleModule module, Scene scene)
  148. :base(path)
  149. {
  150. m_agentID = agentID;
  151. m_consoleModule = module;
  152. m_scene = scene;
  153. m_isGod = m_scene.Permissions.IsGod(agentID);
  154. }
  155. protected override void ProcessRequest(IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  156. {
  157. httpResponse.KeepAlive = false;
  158. if(httpRequest.HttpMethod != "POST")
  159. {
  160. httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
  161. return;
  162. }
  163. httpResponse.StatusCode = (int)HttpStatusCode.OK;
  164. if (!m_scene.TryGetScenePresence(m_agentID, out ScenePresence sp) || sp.IsChildAgent || sp.IsDeleted)
  165. {
  166. return;
  167. }
  168. if (!m_scene.RegionInfo.EstateSettings.IsEstateManagerOrOwner(m_agentID) && !m_isGod)
  169. {
  170. m_consoleModule.SendConsoleOutput(m_agentID, "No access");
  171. return;
  172. }
  173. OSD osd;
  174. try
  175. {
  176. osd = OSDParser.DeserializeLLSDXml(httpRequest.InputStream);
  177. }
  178. catch
  179. {
  180. httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
  181. return;
  182. }
  183. string cmd = osd.AsString();
  184. if (cmd == "set console on")
  185. {
  186. if (m_isGod)
  187. {
  188. m_consoleModule.AddConsole(m_agentID, ConsoleSender);
  189. m_consoleIsOn = true;
  190. m_consoleModule.SendConsoleOutput(m_agentID, "Console is now on");
  191. }
  192. return;
  193. }
  194. else if (cmd == "set console off")
  195. {
  196. m_consoleModule.RemoveConsole(m_agentID);
  197. m_consoleIsOn = false;
  198. m_consoleModule.SendConsoleOutput(m_agentID, "Console is now off");
  199. return;
  200. }
  201. if (m_consoleIsOn == false && m_consoleModule.RunCommand(osd.AsString().Trim(), m_agentID))
  202. return;
  203. if (m_isGod && m_consoleIsOn)
  204. {
  205. MainConsole.Instance.RunCommand(osd.AsString().Trim());
  206. }
  207. else
  208. {
  209. m_consoleModule.SendConsoleOutput(m_agentID, "Unknown command");
  210. }
  211. }
  212. private void ConsoleSender(string text)
  213. {
  214. m_consoleModule.SendConsoleOutput(m_agentID, text);
  215. }
  216. }
  217. }