IRCBridgeModule.cs 7.3 KB

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