OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
IRCBridgeModule.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.Net;
32 using System.Reflection;
33 using log4net;
34 using Mono.Addins;
35 using Nini.Config;
36 using Nwc.XmlRpc;
37 using OpenSim.Framework;
38 using OpenSim.Framework.Servers;
39 using OpenSim.Region.Framework.Interfaces;
40 using OpenSim.Region.Framework.Scenes;
41 
42 namespace OpenSim.Region.OptionalModules.Avatar.Chat
43 {
44  [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "IRCBridgeModule")]
46  {
47  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48 
49  internal static bool Enabled = false;
50  internal static IConfig m_config = null;
51 
52  internal static List<ChannelState> m_channels = new List<ChannelState>();
53  internal static List<RegionState> m_regions = new List<RegionState>();
54 
55  internal static string m_password = String.Empty;
56  internal RegionState m_region = null;
57 
58  #region INonSharedRegionModule Members
59 
60  public Type ReplaceableInterface
61  {
62  get { return null; }
63  }
64 
65  public string Name
66  {
67  get { return "IRCBridgeModule"; }
68  }
69 
70  public void Initialise(IConfigSource config)
71  {
72  m_config = config.Configs["IRC"];
73  if (m_config == null)
74  {
75  // m_log.InfoFormat("[IRC-Bridge] module not configured");
76  return;
77  }
78 
79  if (!m_config.GetBoolean("enabled", false))
80  {
81  // m_log.InfoFormat("[IRC-Bridge] module disabled in configuration");
82  m_config = null;
83  return;
84  }
85 
86  if (config.Configs["RemoteAdmin"] != null)
87  {
88  m_password = config.Configs["RemoteAdmin"].GetString("access_password", m_password);
89  }
90 
91  Enabled = true;
92 
93  m_log.InfoFormat("[IRC-Bridge]: Module is enabled");
94  }
95 
96  public void AddRegion(Scene scene)
97  {
98  if (Enabled)
99  {
100  try
101  {
102  m_log.InfoFormat("[IRC-Bridge] Connecting region {0}", scene.RegionInfo.RegionName);
103 
104  if (!String.IsNullOrEmpty(m_password))
105  MainServer.Instance.AddXmlRPCHandler("irc_admin", XmlRpcAdminMethod, false);
106 
107  m_region = new RegionState(scene, m_config);
108  lock (m_regions)
109  m_regions.Add(m_region);
110  m_region.Open();
111  }
112  catch (Exception e)
113  {
114  m_log.WarnFormat("[IRC-Bridge] Region {0} not connected to IRC : {1}", scene.RegionInfo.RegionName, e.Message);
115  m_log.Debug(e);
116  }
117  }
118  else
119  {
120  //m_log.DebugFormat("[IRC-Bridge] Not enabled. Connect for region {0} ignored", scene.RegionInfo.RegionName);
121  }
122  }
123 
124 
125  public void RegionLoaded(Scene scene)
126  {
127  }
128 
129  public void RemoveRegion(Scene scene)
130  {
131  if (!Enabled)
132  return;
133 
134  if (m_region == null)
135  return;
136 
137  if (!String.IsNullOrEmpty(m_password))
138  MainServer.Instance.RemoveXmlRPCHandler("irc_admin");
139 
140  m_region.Close();
141 
142  if (m_regions.Contains(m_region))
143  {
144  lock (m_regions) m_regions.Remove(m_region);
145  }
146  }
147 
148  public void Close()
149  {
150  }
151  #endregion
152 
153  public static XmlRpcResponse XmlRpcAdminMethod(XmlRpcRequest request, IPEndPoint remoteClient)
154  {
155  m_log.Debug("[IRC-Bridge]: XML RPC Admin Entry");
156 
157  XmlRpcResponse response = new XmlRpcResponse();
158  Hashtable responseData = new Hashtable();
159 
160  try
161  {
162  Hashtable requestData = (Hashtable)request.Params[0];
163  bool found = false;
164  string region = String.Empty;
165 
166  if (m_password != String.Empty)
167  {
168  if (!requestData.ContainsKey("password"))
169  throw new Exception("Invalid request");
170  if ((string)requestData["password"] != m_password)
171  throw new Exception("Invalid request");
172  }
173 
174  if (!requestData.ContainsKey("region"))
175  throw new Exception("No region name specified");
176  region = (string)requestData["region"];
177 
178  foreach (RegionState rs in m_regions)
179  {
180  if (rs.Region == region)
181  {
182  responseData["server"] = rs.cs.Server;
183  responseData["port"] = (int)rs.cs.Port;
184  responseData["user"] = rs.cs.User;
185  responseData["channel"] = rs.cs.IrcChannel;
186  responseData["enabled"] = rs.cs.irc.Enabled;
187  responseData["connected"] = rs.cs.irc.Connected;
188  responseData["nickname"] = rs.cs.irc.Nick;
189  found = true;
190  break;
191  }
192  }
193 
194  if (!found) throw new Exception(String.Format("Region <{0}> not found", region));
195 
196  responseData["success"] = true;
197  }
198  catch (Exception e)
199  {
200  m_log.ErrorFormat("[IRC-Bridge] XML RPC Admin request failed : {0}", e.Message);
201 
202  responseData["success"] = "false";
203  responseData["error"] = e.Message;
204  }
205  finally
206  {
207  response.Value = responseData;
208  }
209 
210  m_log.Debug("[IRC-Bridge]: XML RPC Admin Exit");
211 
212  return response;
213  }
214  }
215 }
static XmlRpcResponse XmlRpcAdminMethod(XmlRpcRequest request, IPEndPoint remoteClient)
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
static BaseHttpServer Instance
Set the main HTTP server instance.
Definition: MainServer.cs:83
void Initialise(IConfigSource config)
This is called to initialize the region module. For shared modules, this is called exactly once...
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...