IRCBridgeModule.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.Net;
  31. using System.Reflection;
  32. using log4net;
  33. using Mono.Addins;
  34. using Nini.Config;
  35. using Nwc.XmlRpc;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Servers;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. namespace OpenSim.Region.OptionalModules.Avatar.Chat
  41. {
  42. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "IRCBridgeModule")]
  43. public class IRCBridgeModule : INonSharedRegionModule
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. internal static bool Enabled = false;
  47. internal static IConfig m_config = null;
  48. internal static List<ChannelState> m_channels = new List<ChannelState>();
  49. internal static List<RegionState> m_regions = new List<RegionState>();
  50. internal static string m_password = String.Empty;
  51. internal RegionState m_region = null;
  52. #region INonSharedRegionModule Members
  53. public Type ReplaceableInterface
  54. {
  55. get { return null; }
  56. }
  57. public string Name
  58. {
  59. get { return "IRCBridgeModule"; }
  60. }
  61. public void Initialise(IConfigSource config)
  62. {
  63. m_config = config.Configs["IRC"];
  64. if (m_config == null)
  65. {
  66. // m_log.InfoFormat("[IRC-Bridge] module not configured");
  67. return;
  68. }
  69. if (!m_config.GetBoolean("enabled", false))
  70. {
  71. // m_log.InfoFormat("[IRC-Bridge] module disabled in configuration");
  72. return;
  73. }
  74. if (config.Configs["RemoteAdmin"] != null)
  75. {
  76. m_password = config.Configs["RemoteAdmin"].GetString("access_password", m_password);
  77. }
  78. Enabled = true;
  79. m_log.InfoFormat("[IRC-Bridge]: Module is enabled");
  80. }
  81. public void AddRegion(Scene scene)
  82. {
  83. if (Enabled)
  84. {
  85. try
  86. {
  87. m_log.InfoFormat("[IRC-Bridge] Connecting region {0}", scene.RegionInfo.RegionName);
  88. if (!String.IsNullOrEmpty(m_password))
  89. MainServer.Instance.AddXmlRPCHandler("irc_admin", XmlRpcAdminMethod, false);
  90. m_region = new RegionState(scene, m_config);
  91. lock (m_regions) m_regions.Add(m_region);
  92. m_region.Open();
  93. }
  94. catch (Exception e)
  95. {
  96. m_log.WarnFormat("[IRC-Bridge] Region {0} not connected to IRC : {1}", scene.RegionInfo.RegionName, e.Message);
  97. m_log.Debug(e);
  98. }
  99. }
  100. else
  101. {
  102. //m_log.DebugFormat("[IRC-Bridge] Not enabled. Connect for region {0} ignored", scene.RegionInfo.RegionName);
  103. }
  104. }
  105. public void RegionLoaded(Scene scene)
  106. {
  107. }
  108. public void RemoveRegion(Scene scene)
  109. {
  110. if (!Enabled)
  111. return;
  112. if (m_region == null)
  113. return;
  114. if (!String.IsNullOrEmpty(m_password))
  115. MainServer.Instance.RemoveXmlRPCHandler("irc_admin");
  116. m_region.Close();
  117. if (m_regions.Contains(m_region))
  118. {
  119. lock (m_regions) m_regions.Remove(m_region);
  120. }
  121. }
  122. public void Close()
  123. {
  124. }
  125. #endregion
  126. public static XmlRpcResponse XmlRpcAdminMethod(XmlRpcRequest request, IPEndPoint remoteClient)
  127. {
  128. m_log.Debug("[IRC-Bridge]: XML RPC Admin Entry");
  129. XmlRpcResponse response = new XmlRpcResponse();
  130. Hashtable responseData = new Hashtable();
  131. try
  132. {
  133. Hashtable requestData = (Hashtable)request.Params[0];
  134. bool found = false;
  135. string region = String.Empty;
  136. if (m_password != String.Empty)
  137. {
  138. if (!requestData.ContainsKey("password"))
  139. throw new Exception("Invalid request");
  140. if ((string)requestData["password"] != m_password)
  141. throw new Exception("Invalid request");
  142. }
  143. if (!requestData.ContainsKey("region"))
  144. throw new Exception("No region name specified");
  145. region = (string)requestData["region"];
  146. foreach (RegionState rs in m_regions)
  147. {
  148. if (rs.Region == region)
  149. {
  150. responseData["server"] = rs.cs.Server;
  151. responseData["port"] = (int)rs.cs.Port;
  152. responseData["user"] = rs.cs.User;
  153. responseData["channel"] = rs.cs.IrcChannel;
  154. responseData["enabled"] = rs.cs.irc.Enabled;
  155. responseData["connected"] = rs.cs.irc.Connected;
  156. responseData["nickname"] = rs.cs.irc.Nick;
  157. found = true;
  158. break;
  159. }
  160. }
  161. if (!found) throw new Exception(String.Format("Region <{0}> not found", region));
  162. responseData["success"] = true;
  163. }
  164. catch (Exception e)
  165. {
  166. m_log.ErrorFormat("[IRC-Bridge] XML RPC Admin request failed : {0}", e.Message);
  167. responseData["success"] = "false";
  168. responseData["error"] = e.Message;
  169. }
  170. finally
  171. {
  172. response.Value = responseData;
  173. }
  174. m_log.Debug("[IRC-Bridge]: XML RPC Admin Exit");
  175. return response;
  176. }
  177. }
  178. }