RegionConsoleModule.cs 8.6 KB

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