1
0

IRCBridgeModule.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. m_config = null;
  73. return;
  74. }
  75. if (config.Configs["RemoteAdmin"] != null)
  76. {
  77. m_password = config.Configs["RemoteAdmin"].GetString("access_password", m_password);
  78. }
  79. Enabled = true;
  80. m_log.InfoFormat("[IRC-Bridge]: Module is enabled");
  81. }
  82. public void AddRegion(Scene scene)
  83. {
  84. if (Enabled)
  85. {
  86. try
  87. {
  88. m_log.InfoFormat("[IRC-Bridge] Connecting region {0}", scene.RegionInfo.RegionName);
  89. if (!String.IsNullOrEmpty(m_password))
  90. MainServer.Instance.AddXmlRPCHandler("irc_admin", XmlRpcAdminMethod, false);
  91. m_region = new RegionState(scene, m_config);
  92. lock (m_regions)
  93. m_regions.Add(m_region);
  94. m_region.Open();
  95. }
  96. catch (Exception e)
  97. {
  98. m_log.WarnFormat("[IRC-Bridge] Region {0} not connected to IRC : {1}", scene.RegionInfo.RegionName, e.Message);
  99. m_log.Debug(e);
  100. }
  101. }
  102. else
  103. {
  104. //m_log.DebugFormat("[IRC-Bridge] Not enabled. Connect for region {0} ignored", scene.RegionInfo.RegionName);
  105. }
  106. }
  107. public void RegionLoaded(Scene scene)
  108. {
  109. }
  110. public void RemoveRegion(Scene scene)
  111. {
  112. if (!Enabled)
  113. return;
  114. if (m_region == null)
  115. return;
  116. if (!String.IsNullOrEmpty(m_password))
  117. MainServer.Instance.RemoveXmlRPCHandler("irc_admin");
  118. m_region.Close();
  119. if (m_regions.Contains(m_region))
  120. {
  121. lock (m_regions) m_regions.Remove(m_region);
  122. }
  123. }
  124. public void Close()
  125. {
  126. }
  127. #endregion
  128. public static XmlRpcResponse XmlRpcAdminMethod(XmlRpcRequest request, IPEndPoint remoteClient)
  129. {
  130. m_log.Debug("[IRC-Bridge]: XML RPC Admin Entry");
  131. XmlRpcResponse response = new XmlRpcResponse();
  132. Hashtable responseData = new Hashtable();
  133. try
  134. {
  135. Hashtable requestData = (Hashtable)request.Params[0];
  136. bool found = false;
  137. string region = String.Empty;
  138. if (m_password != String.Empty)
  139. {
  140. if (!requestData.ContainsKey("password"))
  141. throw new Exception("Invalid request");
  142. if ((string)requestData["password"] != m_password)
  143. throw new Exception("Invalid request");
  144. }
  145. if (!requestData.ContainsKey("region"))
  146. throw new Exception("No region name specified");
  147. region = (string)requestData["region"];
  148. foreach (RegionState rs in m_regions)
  149. {
  150. if (rs.Region == region)
  151. {
  152. responseData["server"] = rs.cs.Server;
  153. responseData["port"] = (int)rs.cs.Port;
  154. responseData["user"] = rs.cs.User;
  155. responseData["channel"] = rs.cs.IrcChannel;
  156. responseData["enabled"] = rs.cs.irc.Enabled;
  157. responseData["connected"] = rs.cs.irc.Connected;
  158. responseData["nickname"] = rs.cs.irc.Nick;
  159. found = true;
  160. break;
  161. }
  162. }
  163. if (!found) throw new Exception(String.Format("Region <{0}> not found", region));
  164. responseData["success"] = true;
  165. }
  166. catch (Exception e)
  167. {
  168. m_log.ErrorFormat("[IRC-Bridge] XML RPC Admin request failed : {0}", e.Message);
  169. responseData["success"] = "false";
  170. responseData["error"] = e.Message;
  171. }
  172. finally
  173. {
  174. response.Value = responseData;
  175. }
  176. m_log.Debug("[IRC-Bridge]: XML RPC Admin Exit");
  177. return response;
  178. }
  179. }
  180. }