RegionConsoleModule.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 void Initialise(IConfigSource source)
  63. {
  64. m_commands.AddCommand( "Help", false, "help", "help [<item>]", "Display help on a particular command or on a list of commands in a category", Help);
  65. }
  66. public void AddRegion(Scene s)
  67. {
  68. m_scene = s;
  69. m_scene.RegisterModuleInterface<IRegionConsole>(this);
  70. }
  71. public void RemoveRegion(Scene s)
  72. {
  73. m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
  74. m_scene = null;
  75. }
  76. public void RegionLoaded(Scene s)
  77. {
  78. m_scene.EventManager.OnRegisterCaps += RegisterCaps;
  79. m_eventQueue = m_scene.RequestModuleInterface<IEventQueue>();
  80. }
  81. public void PostInitialise()
  82. {
  83. }
  84. public void Close() { }
  85. public string Name { get { return "RegionConsoleModule"; } }
  86. public Type ReplaceableInterface
  87. {
  88. get { return null; }
  89. }
  90. public void RegisterCaps(UUID agentID, Caps caps)
  91. {
  92. if (!m_scene.RegionInfo.EstateSettings.IsEstateManagerOrOwner(agentID))
  93. return;
  94. UUID capID = UUID.Random();
  95. // m_log.DebugFormat("[REGION CONSOLE]: /CAPS/{0} in region {1}", capID, m_scene.RegionInfo.RegionName);
  96. caps.RegisterHandler(
  97. "SimConsoleAsync",
  98. new ConsoleHandler("/CAPS/" + capID + "/", "SimConsoleAsync", agentID, this, m_scene));
  99. }
  100. public void SendConsoleOutput(UUID agentID, string message)
  101. {
  102. OSD osd = OSD.FromString(message);
  103. m_eventQueue.Enqueue(EventQueueHelper.BuildEvent("SimConsoleResponse", osd), agentID);
  104. }
  105. public bool RunCommand(string command, UUID invokerID)
  106. {
  107. string[] parts = Parser.Parse(command);
  108. Array.Resize(ref parts, parts.Length + 1);
  109. parts[parts.Length - 1] = invokerID.ToString();
  110. if (m_commands.Resolve(parts).Length == 0)
  111. return false;
  112. return true;
  113. }
  114. private void Help(string module, string[] cmd)
  115. {
  116. UUID agentID = new UUID(cmd[cmd.Length - 1]);
  117. Array.Resize(ref cmd, cmd.Length - 1);
  118. List<string> help = Commands.GetHelp(cmd);
  119. string reply = String.Empty;
  120. foreach (string s in help)
  121. {
  122. reply += s + "\n";
  123. }
  124. SendConsoleOutput(agentID, reply);
  125. }
  126. public void AddCommand(string module, bool shared, string command, string help, string longhelp, CommandDelegate fn)
  127. {
  128. m_commands.AddCommand(module, shared, command, help, longhelp, fn);
  129. }
  130. }
  131. public class ConsoleHandler : BaseStreamHandler
  132. {
  133. private static readonly ILog m_log =
  134. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  135. private RegionConsoleModule m_consoleModule;
  136. private UUID m_agentID;
  137. private bool m_isGod;
  138. private Scene m_scene;
  139. private bool m_consoleIsOn = false;
  140. public ConsoleHandler(string path, string name, UUID agentID, RegionConsoleModule module, Scene scene)
  141. :base("POST", path, name, agentID.ToString())
  142. {
  143. m_agentID = agentID;
  144. m_consoleModule = module;
  145. m_scene = scene;
  146. m_isGod = m_scene.Permissions.IsGod(agentID);
  147. }
  148. public override byte[] Handle(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  149. {
  150. StreamReader reader = new StreamReader(request);
  151. string message = reader.ReadToEnd();
  152. OSD osd = OSDParser.DeserializeLLSDXml(message);
  153. string cmd = osd.AsString();
  154. if (cmd == "set console on")
  155. {
  156. if (m_isGod)
  157. {
  158. MainConsole.Instance.OnOutput += ConsoleSender;
  159. m_consoleIsOn = true;
  160. m_consoleModule.SendConsoleOutput(m_agentID, "Console is now on");
  161. }
  162. return new byte[0];
  163. }
  164. else if (cmd == "set console off")
  165. {
  166. MainConsole.Instance.OnOutput -= ConsoleSender;
  167. m_consoleIsOn = false;
  168. m_consoleModule.SendConsoleOutput(m_agentID, "Console is now off");
  169. return new byte[0];
  170. }
  171. if (m_consoleIsOn == false && m_consoleModule.RunCommand(osd.AsString().Trim(), m_agentID))
  172. return new byte[0];
  173. if (m_isGod && m_consoleIsOn)
  174. {
  175. MainConsole.Instance.RunCommand(osd.AsString().Trim());
  176. }
  177. else
  178. {
  179. m_consoleModule.SendConsoleOutput(m_agentID, "Unknown command");
  180. }
  181. return new byte[0];
  182. }
  183. private void ConsoleSender(string text)
  184. {
  185. m_consoleModule.SendConsoleOutput(m_agentID, text);
  186. }
  187. private void OnMakeChildAgent(ScenePresence presence)
  188. {
  189. if (presence.UUID == m_agentID)
  190. {
  191. MainConsole.Instance.OnOutput -= ConsoleSender;
  192. m_consoleIsOn = false;
  193. }
  194. }
  195. }
  196. }