IRCBridgeModule.cs 7.4 KB

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