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