IRCBridgeModule.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 OpenSim 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.Reflection;
  31. using log4net;
  32. using Nini.Config;
  33. using Nwc.XmlRpc;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Environment.Interfaces;
  36. using OpenSim.Region.Environment.Scenes;
  37. namespace OpenSim.Region.Environment.Modules.Avatar.Chat
  38. {
  39. public class IRCBridgeModule : IRegionModule
  40. {
  41. private static readonly ILog m_log =
  42. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. internal static bool configured = false;
  44. internal static bool enabled = false;
  45. internal static IConfig m_config = null;
  46. internal static List<ChannelState> m_channels = new List<ChannelState>();
  47. internal static List<RegionState> m_regions = new List<RegionState>();
  48. internal static string password = String.Empty;
  49. internal RegionState region = null;
  50. #region IRegionModule Members
  51. public string Name
  52. {
  53. get { return "IRCBridgeModule"; }
  54. }
  55. public bool IsSharedModule
  56. {
  57. get { return false; }
  58. }
  59. public void Initialise(Scene scene, IConfigSource config)
  60. {
  61. // Do a once-only scan of the configuration file to make
  62. // sure it's basically intact.
  63. if (!configured)
  64. {
  65. configured = true;
  66. try
  67. {
  68. if ((m_config = config.Configs["IRC"]) == null)
  69. {
  70. m_log.InfoFormat("[IRC-Bridge] module not configured");
  71. return;
  72. }
  73. if (!m_config.GetBoolean("enabled", false))
  74. {
  75. m_log.InfoFormat("[IRC-Bridge] module disabled in configuration");
  76. return;
  77. }
  78. }
  79. catch (Exception e)
  80. {
  81. m_log.ErrorFormat("[IRC-Bridge] configuration failed : {0}", e.Message);
  82. return;
  83. }
  84. enabled = true;
  85. if (config.Configs["RemoteAdmin"] != null)
  86. {
  87. password = config.Configs["RemoteAdmin"].GetString("access_password", password);
  88. scene.CommsManager.HttpServer.AddXmlRPCHandler("irc_admin", XmlRpcAdminMethod, false);
  89. }
  90. }
  91. // Iff the IRC bridge is enabled, then each new region may be
  92. // connected to IRC. But it should NOT be obligatory (and it
  93. // is not).
  94. // We have to do ALL of the startup here because PostInitialize
  95. // is not called when a region gets created in-flight from the
  96. // command line.
  97. if (enabled)
  98. {
  99. try
  100. {
  101. m_log.InfoFormat("[IRC-Bridge] Connecting region {0}", scene.RegionInfo.RegionName);
  102. region = new RegionState(scene, m_config);
  103. lock (m_regions) m_regions.Add(region);
  104. region.Open();
  105. }
  106. catch (Exception e)
  107. {
  108. m_log.WarnFormat("[IRC-Bridge] Region {0} not connected to IRC : {1}", scene.RegionInfo.RegionName, e.Message);
  109. m_log.Debug(e);
  110. }
  111. }
  112. else
  113. {
  114. m_log.WarnFormat("[IRC-Bridge] Not enabled. Connect for region {0} ignored", scene.RegionInfo.RegionName);
  115. }
  116. }
  117. // This module can be called in-flight in which case PostInitialize
  118. // is not called following Initialize. So no use is made of this
  119. // call.
  120. public void PostInitialise()
  121. {
  122. }
  123. // Called immediately before the region module is unloaded. Cleanup
  124. // the region.
  125. public void Close()
  126. {
  127. if (!enabled)
  128. return;
  129. region.Close();
  130. lock (m_regions) m_regions.Remove(region);
  131. }
  132. #endregion
  133. public static XmlRpcResponse XmlRpcAdminMethod(XmlRpcRequest request)
  134. {
  135. m_log.Info("[IRC-Bridge]: XML RPC Admin Entry");
  136. XmlRpcResponse response = new XmlRpcResponse();
  137. Hashtable responseData = new Hashtable();
  138. try
  139. {
  140. Hashtable requestData = (Hashtable)request.Params[0];
  141. bool found = false;
  142. string region = String.Empty;
  143. if (password != String.Empty)
  144. {
  145. if (!requestData.ContainsKey("password"))
  146. throw new Exception("Invalid request");
  147. if ((string)requestData["password"] != password)
  148. throw new Exception("Invalid request");
  149. }
  150. if (!requestData.ContainsKey("region"))
  151. throw new Exception("No region name specified");
  152. region = (string)requestData["region"];
  153. foreach (RegionState rs in m_regions)
  154. {
  155. if (rs.Region == region)
  156. {
  157. responseData["server"] = rs.cs.Server;
  158. responseData["port"] = (int)rs.cs.Port;
  159. responseData["user"] = rs.cs.User;
  160. responseData["channel"] = rs.cs.IrcChannel;
  161. responseData["enabled"] = rs.cs.irc.Enabled;
  162. responseData["connected"] = rs.cs.irc.Connected;
  163. responseData["nickname"] = rs.cs.irc.Nick;
  164. found = true;
  165. break;
  166. }
  167. }
  168. if (!found) throw new Exception(String.Format("Region <{0}> not found", region));
  169. responseData["success"] = true;
  170. }
  171. catch (Exception e)
  172. {
  173. m_log.InfoFormat("[IRC-Bridge] XML RPC Admin request failed : {0}", e.Message);
  174. responseData["success"] = "false";
  175. responseData["error"] = e.Message;
  176. }
  177. finally
  178. {
  179. response.Value = responseData;
  180. }
  181. m_log.Debug("[IRC-Bridge]: XML RPC Admin Exit");
  182. return response;
  183. }
  184. }
  185. }