IRCBridgeModule.cs 7.3 KB

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