1
0

ChannelState.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 OpenSim 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.Generic;
  29. using System.Reflection;
  30. using System.Text.RegularExpressions;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. namespace OpenSim.Region.OptionalModules.Avatar.Chat
  37. {
  38. // An instance of this class exists for each unique combination of
  39. // IRC chat interface characteristics, as determined by the supplied
  40. // configuration file.
  41. internal class ChannelState
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private static Regex arg = new Regex(@"\[[^\[\]]*\]");
  46. private static int _idk_ = 0;
  47. private static int DEBUG_CHANNEL = 2147483647;
  48. // These are the IRC Connector configurable parameters with hard-wired
  49. // default values (retained for compatability).
  50. internal string Server = null;
  51. internal string Password = null;
  52. internal string IrcChannel = null;
  53. internal string BaseNickname = "OSimBot";
  54. internal uint Port = 6667;
  55. internal string User = "USER OpenSimBot 8 * :I'm an OpenSim to IRC bot";
  56. internal bool ClientReporting = true;
  57. internal bool RelayChat = true;
  58. internal bool RelayPrivateChannels = false;
  59. internal int RelayChannel = 1;
  60. internal List<int> ValidInWorldChannels = new List<int>();
  61. // Connector agnostic parameters. These values are NOT shared with the
  62. // connector and do not differentiate at an IRC level
  63. internal string PrivateMessageFormat = "PRIVMSG {0} :<{2}> {1} {3}";
  64. internal string NoticeMessageFormat = "PRIVMSG {0} :<{2}> {3}";
  65. internal int RelayChannelOut = -1;
  66. internal bool RandomizeNickname = true;
  67. internal bool CommandsEnabled = false;
  68. internal int CommandChannel = -1;
  69. internal int ConnectDelay = 10;
  70. internal int PingDelay = 15;
  71. internal string DefaultZone = "Sim";
  72. internal string _accessPassword = String.Empty;
  73. internal Regex AccessPasswordRegex = null;
  74. internal string AccessPassword
  75. {
  76. get { return _accessPassword; }
  77. set
  78. {
  79. _accessPassword = value;
  80. AccessPasswordRegex = new Regex(String.Format(@"^{0},\s*(?<avatar>[^,]+),\s*(?<message>.+)$", _accessPassword),
  81. RegexOptions.Compiled);
  82. }
  83. }
  84. // IRC connector reference
  85. internal IRCConnector irc = null;
  86. internal int idn = _idk_++;
  87. // List of regions dependent upon this connection
  88. internal List<RegionState> clientregions = new List<RegionState>();
  89. // Needed by OpenChannel
  90. internal ChannelState()
  91. {
  92. }
  93. // This constructor is used by the Update* methods. A copy of the
  94. // existing channel state is created, and distinguishing characteristics
  95. // are copied across.
  96. internal ChannelState(ChannelState model)
  97. {
  98. Server = model.Server;
  99. Password = model.Password;
  100. IrcChannel = model.IrcChannel;
  101. Port = model.Port;
  102. BaseNickname = model.BaseNickname;
  103. RandomizeNickname = model.RandomizeNickname;
  104. User = model.User;
  105. CommandsEnabled = model.CommandsEnabled;
  106. CommandChannel = model.CommandChannel;
  107. RelayChat = model.RelayChat;
  108. RelayPrivateChannels = model.RelayPrivateChannels;
  109. RelayChannelOut = model.RelayChannelOut;
  110. RelayChannel = model.RelayChannel;
  111. ValidInWorldChannels = model.ValidInWorldChannels;
  112. PrivateMessageFormat = model.PrivateMessageFormat;
  113. NoticeMessageFormat = model.NoticeMessageFormat;
  114. ClientReporting = model.ClientReporting;
  115. AccessPassword = model.AccessPassword;
  116. DefaultZone = model.DefaultZone;
  117. ConnectDelay = model.ConnectDelay;
  118. PingDelay = model.PingDelay;
  119. }
  120. // Read the configuration file, performing variable substitution and any
  121. // necessary aliasing. See accompanying documentation for how this works.
  122. // If you don't need variables, then this works exactly as before.
  123. // If either channel or server are not specified, the request fails.
  124. internal static void OpenChannel(RegionState rs, IConfig config)
  125. {
  126. // Create a new instance of a channel. This may not actually
  127. // get used if an equivalent channel already exists.
  128. ChannelState cs = new ChannelState();
  129. // Read in the configuration file and filter everything for variable
  130. // subsititution.
  131. m_log.DebugFormat("[IRC-Channel-{0}] Initial request by Region {1} to connect to IRC", cs.idn, rs.Region);
  132. cs.Server = Substitute(rs, config.GetString("server", null));
  133. m_log.DebugFormat("[IRC-Channel-{0}] Server : <{1}>", cs.idn, cs.Server);
  134. cs.Password = Substitute(rs, config.GetString("password", null));
  135. // probably not a good idea to put a password in the log file
  136. cs.IrcChannel = Substitute(rs, config.GetString("channel", null));
  137. m_log.DebugFormat("[IRC-Channel-{0}] IrcChannel : <{1}>", cs.idn, cs.IrcChannel);
  138. cs.Port = Convert.ToUInt32(Substitute(rs, config.GetString("port", Convert.ToString(cs.Port))));
  139. m_log.DebugFormat("[IRC-Channel-{0}] Port : <{1}>", cs.idn, cs.Port);
  140. cs.BaseNickname = Substitute(rs, config.GetString("nick", cs.BaseNickname));
  141. m_log.DebugFormat("[IRC-Channel-{0}] BaseNickname : <{1}>", cs.idn, cs.BaseNickname);
  142. cs.RandomizeNickname = Convert.ToBoolean(Substitute(rs, config.GetString("randomize_nick", Convert.ToString(cs.RandomizeNickname))));
  143. m_log.DebugFormat("[IRC-Channel-{0}] RandomizeNickname : <{1}>", cs.idn, cs.RandomizeNickname);
  144. cs.RandomizeNickname = Convert.ToBoolean(Substitute(rs, config.GetString("nicknum", Convert.ToString(cs.RandomizeNickname))));
  145. m_log.DebugFormat("[IRC-Channel-{0}] RandomizeNickname : <{1}>", cs.idn, cs.RandomizeNickname);
  146. cs.User = Substitute(rs, config.GetString("username", cs.User));
  147. m_log.DebugFormat("[IRC-Channel-{0}] User : <{1}>", cs.idn, cs.User);
  148. cs.CommandsEnabled = Convert.ToBoolean(Substitute(rs, config.GetString("commands_enabled", Convert.ToString(cs.CommandsEnabled))));
  149. m_log.DebugFormat("[IRC-Channel-{0}] CommandsEnabled : <{1}>", cs.idn, cs.CommandsEnabled);
  150. cs.CommandChannel = Convert.ToInt32(Substitute(rs, config.GetString("commandchannel", Convert.ToString(cs.CommandChannel))));
  151. m_log.DebugFormat("[IRC-Channel-{0}] CommandChannel : <{1}>", cs.idn, cs.CommandChannel);
  152. cs.CommandChannel = Convert.ToInt32(Substitute(rs, config.GetString("command_channel", Convert.ToString(cs.CommandChannel))));
  153. m_log.DebugFormat("[IRC-Channel-{0}] CommandChannel : <{1}>", cs.idn, cs.CommandChannel);
  154. cs.RelayChat = Convert.ToBoolean(Substitute(rs, config.GetString("relay_chat", Convert.ToString(cs.RelayChat))));
  155. m_log.DebugFormat("[IRC-Channel-{0}] RelayChat : <{1}>", cs.idn, cs.RelayChat);
  156. cs.RelayPrivateChannels = Convert.ToBoolean(Substitute(rs, config.GetString("relay_private_channels", Convert.ToString(cs.RelayPrivateChannels))));
  157. m_log.DebugFormat("[IRC-Channel-{0}] RelayPrivateChannels : <{1}>", cs.idn, cs.RelayPrivateChannels);
  158. cs.RelayPrivateChannels = Convert.ToBoolean(Substitute(rs, config.GetString("useworldcomm", Convert.ToString(cs.RelayPrivateChannels))));
  159. m_log.DebugFormat("[IRC-Channel-{0}] RelayPrivateChannels : <{1}>", cs.idn, cs.RelayPrivateChannels);
  160. cs.RelayChannelOut = Convert.ToInt32(Substitute(rs, config.GetString("relay_private_channel_out", Convert.ToString(cs.RelayChannelOut))));
  161. m_log.DebugFormat("[IRC-Channel-{0}] RelayChannelOut : <{1}>", cs.idn, cs.RelayChannelOut);
  162. cs.RelayChannel = Convert.ToInt32(Substitute(rs, config.GetString("relay_private_channel_in", Convert.ToString(cs.RelayChannel))));
  163. m_log.DebugFormat("[IRC-Channel-{0}] RelayChannel : <{1}>", cs.idn, cs.RelayChannel);
  164. cs.RelayChannel = Convert.ToInt32(Substitute(rs, config.GetString("inchannel", Convert.ToString(cs.RelayChannel))));
  165. m_log.DebugFormat("[IRC-Channel-{0}] RelayChannel : <{1}>", cs.idn, cs.RelayChannel);
  166. cs.PrivateMessageFormat = Substitute(rs, config.GetString("msgformat", cs.PrivateMessageFormat));
  167. m_log.DebugFormat("[IRC-Channel-{0}] PrivateMessageFormat : <{1}>", cs.idn, cs.PrivateMessageFormat);
  168. cs.NoticeMessageFormat = Substitute(rs, config.GetString("noticeformat", cs.NoticeMessageFormat));
  169. m_log.DebugFormat("[IRC-Channel-{0}] NoticeMessageFormat : <{1}>", cs.idn, cs.NoticeMessageFormat);
  170. cs.ClientReporting = Convert.ToInt32(Substitute(rs, config.GetString("verbosity", cs.ClientReporting?"1":"0"))) > 0;
  171. m_log.DebugFormat("[IRC-Channel-{0}] ClientReporting : <{1}>", cs.idn, cs.ClientReporting);
  172. cs.ClientReporting = Convert.ToBoolean(Substitute(rs, config.GetString("report_clients", Convert.ToString(cs.ClientReporting))));
  173. m_log.DebugFormat("[IRC-Channel-{0}] ClientReporting : <{1}>", cs.idn, cs.ClientReporting);
  174. cs.DefaultZone = Substitute(rs, config.GetString("fallback_region", cs.DefaultZone));
  175. m_log.DebugFormat("[IRC-Channel-{0}] DefaultZone : <{1}>", cs.idn, cs.DefaultZone);
  176. cs.ConnectDelay = Convert.ToInt32(Substitute(rs, config.GetString("connect_delay", Convert.ToString(cs.ConnectDelay))));
  177. m_log.DebugFormat("[IRC-Channel-{0}] ConnectDelay : <{1}>", cs.idn, cs.ConnectDelay);
  178. cs.PingDelay = Convert.ToInt32(Substitute(rs, config.GetString("ping_delay", Convert.ToString(cs.PingDelay))));
  179. m_log.DebugFormat("[IRC-Channel-{0}] PingDelay : <{1}>", cs.idn, cs.PingDelay);
  180. cs.AccessPassword = Substitute(rs, config.GetString("access_password", cs.AccessPassword));
  181. m_log.DebugFormat("[IRC-Channel-{0}] AccessPassword : <{1}>", cs.idn, cs.AccessPassword);
  182. // Fail if fundamental information is still missing
  183. if (cs.Server == null || cs.IrcChannel == null || cs.BaseNickname == null || cs.User == null)
  184. throw new Exception(String.Format("[IRC-Channel-{0}] Invalid configuration for region {1}", cs.idn, rs.Region));
  185. m_log.InfoFormat("[IRC-Channel-{0}] Configuration for Region {1} is valid", cs.idn, rs.Region);
  186. m_log.InfoFormat("[IRC-Channel-{0}] Server = {1}", cs.idn, cs.Server);
  187. m_log.InfoFormat("[IRC-Channel-{0}] Channel = {1}", cs.idn, cs.IrcChannel);
  188. m_log.InfoFormat("[IRC-Channel-{0}] Port = {1}", cs.idn, cs.Port);
  189. m_log.InfoFormat("[IRC-Channel-{0}] Nickname = {1}", cs.idn, cs.BaseNickname);
  190. m_log.InfoFormat("[IRC-Channel-{0}] User = {1}", cs.idn, cs.User);
  191. // Set the channel state for this region
  192. if (cs.RelayChat)
  193. {
  194. cs.ValidInWorldChannels.Add(0);
  195. cs.ValidInWorldChannels.Add(DEBUG_CHANNEL);
  196. }
  197. if (cs.RelayPrivateChannels)
  198. cs.ValidInWorldChannels.Add(cs.RelayChannelOut);
  199. rs.cs = Integrate(rs, cs);
  200. }
  201. // An initialized channel state instance is passed in. If an identical
  202. // channel state instance already exists, then the existing instance
  203. // is used to replace the supplied value.
  204. // If the instance matches with respect to IRC, then the underlying
  205. // IRCConnector is assigned to the supplied channel state and the
  206. // updated value is returned.
  207. // If there is no match, then the supplied instance is completed by
  208. // creating and assigning an instance of an IRC connector.
  209. private static ChannelState Integrate(RegionState rs, ChannelState p_cs)
  210. {
  211. ChannelState cs = p_cs;
  212. // Check to see if we have an existing server/channel setup that can be used
  213. // In the absence of variable substitution this will always resolve to the
  214. // same ChannelState instance, and the table will only contains a single
  215. // entry, so the performance considerations for the existing behavior are
  216. // zero. Only the IRC connector is shared, the ChannelState still contains
  217. // values that, while independent of the IRC connetion, do still distinguish
  218. // this region's behavior.
  219. lock (IRCBridgeModule.m_channels)
  220. {
  221. foreach (ChannelState xcs in IRCBridgeModule.m_channels)
  222. {
  223. if (cs.IsAPerfectMatchFor(xcs))
  224. {
  225. m_log.DebugFormat("[IRC-Channel-{0}] Channel state matched", cs.idn);
  226. cs = xcs;
  227. break;
  228. }
  229. if (cs.IsAConnectionMatchFor(xcs))
  230. {
  231. m_log.DebugFormat("[IRC-Channel-{0}] Channel matched", cs.idn);
  232. cs.irc = xcs.irc;
  233. break;
  234. }
  235. }
  236. }
  237. // No entry was found, so this is going to be a new entry.
  238. if (cs.irc == null)
  239. {
  240. m_log.DebugFormat("[IRC-Channel-{0}] New channel required", cs.idn);
  241. if ((cs.irc = new IRCConnector(cs)) != null)
  242. {
  243. IRCBridgeModule.m_channels.Add(cs);
  244. m_log.InfoFormat("[IRC-Channel-{0}] New channel initialized for {1}, nick: {2}, commands {3}, private channels {4}",
  245. cs.idn, rs.Region, cs.DefaultZone,
  246. cs.CommandsEnabled ? "enabled" : "not enabled",
  247. cs.RelayPrivateChannels ? "relayed" : "not relayed");
  248. }
  249. else
  250. {
  251. string txt = String.Format("[IRC-Channel-{0}] Region {1} failed to connect to channel {2} on server {3}:{4}",
  252. cs.idn, rs.Region, cs.IrcChannel, cs.Server, cs.Port);
  253. m_log.Error(txt);
  254. throw new Exception(txt);
  255. }
  256. }
  257. else
  258. {
  259. m_log.InfoFormat("[IRC-Channel-{0}] Region {1} reusing existing connection to channel {2} on server {3}:{4}",
  260. cs.idn, rs.Region, cs.IrcChannel, cs.Server, cs.Port);
  261. }
  262. m_log.InfoFormat("[IRC-Channel-{0}] Region {1} associated with channel {2} on server {3}:{4}",
  263. cs.idn, rs.Region, cs.IrcChannel, cs.Server, cs.Port);
  264. // We're finally ready to commit ourselves
  265. return cs;
  266. }
  267. // These routines allow differentiating changes to
  268. // the underlying channel state. If necessary, a
  269. // new channel state will be created.
  270. internal ChannelState UpdateServer(RegionState rs, string server)
  271. {
  272. RemoveRegion(rs);
  273. ChannelState cs = new ChannelState(this);
  274. cs.Server = server;
  275. cs = Integrate(rs, cs);
  276. cs.AddRegion(rs);
  277. return cs;
  278. }
  279. internal ChannelState UpdatePort(RegionState rs, string port)
  280. {
  281. RemoveRegion(rs);
  282. ChannelState cs = new ChannelState(this);
  283. cs.Port = Convert.ToUInt32(port);
  284. cs = Integrate(rs, cs);
  285. cs.AddRegion(rs);
  286. return cs;
  287. }
  288. internal ChannelState UpdateChannel(RegionState rs, string channel)
  289. {
  290. RemoveRegion(rs);
  291. ChannelState cs = new ChannelState(this);
  292. cs.IrcChannel = channel;
  293. cs = Integrate(rs, cs);
  294. cs.AddRegion(rs);
  295. return cs;
  296. }
  297. internal ChannelState UpdateNickname(RegionState rs, string nickname)
  298. {
  299. RemoveRegion(rs);
  300. ChannelState cs = new ChannelState(this);
  301. cs.BaseNickname = nickname;
  302. cs = Integrate(rs, cs);
  303. cs.AddRegion(rs);
  304. return cs;
  305. }
  306. internal ChannelState UpdateClientReporting(RegionState rs, string cr)
  307. {
  308. RemoveRegion(rs);
  309. ChannelState cs = new ChannelState(this);
  310. cs.ClientReporting = Convert.ToBoolean(cr);
  311. cs = Integrate(rs, cs);
  312. cs.AddRegion(rs);
  313. return cs;
  314. }
  315. internal ChannelState UpdateRelayIn(RegionState rs, string channel)
  316. {
  317. RemoveRegion(rs);
  318. ChannelState cs = new ChannelState(this);
  319. cs.RelayChannel = Convert.ToInt32(channel);
  320. cs = Integrate(rs, cs);
  321. cs.AddRegion(rs);
  322. return cs;
  323. }
  324. internal ChannelState UpdateRelayOut(RegionState rs, string channel)
  325. {
  326. RemoveRegion(rs);
  327. ChannelState cs = new ChannelState(this);
  328. cs.RelayChannelOut = Convert.ToInt32(channel);
  329. cs = Integrate(rs, cs);
  330. cs.AddRegion(rs);
  331. return cs;
  332. }
  333. // Determine whether or not this is a 'new' channel. Only those
  334. // attributes that uniquely distinguish an IRC connection should
  335. // be included here (and only those attributes should really be
  336. // in the ChannelState structure)
  337. private bool IsAConnectionMatchFor(ChannelState cs)
  338. {
  339. return (
  340. Server == cs.Server &&
  341. IrcChannel == cs.IrcChannel &&
  342. Port == cs.Port &&
  343. BaseNickname == cs.BaseNickname &&
  344. User == cs.User
  345. );
  346. }
  347. // This level of obsessive matching allows us to produce
  348. // a minimal overhead int he case of a server which does
  349. // need to differentiate IRC at a region level.
  350. private bool IsAPerfectMatchFor(ChannelState cs)
  351. {
  352. return ( IsAConnectionMatchFor(cs) &&
  353. RelayChannelOut == cs.RelayChannelOut &&
  354. PrivateMessageFormat == cs.PrivateMessageFormat &&
  355. NoticeMessageFormat == cs.NoticeMessageFormat &&
  356. RandomizeNickname == cs.RandomizeNickname &&
  357. AccessPassword == cs.AccessPassword &&
  358. CommandsEnabled == cs.CommandsEnabled &&
  359. CommandChannel == cs.CommandChannel &&
  360. DefaultZone == cs.DefaultZone &&
  361. RelayPrivateChannels == cs.RelayPrivateChannels &&
  362. RelayChannel == cs.RelayChannel &&
  363. RelayChat == cs.RelayChat &&
  364. ClientReporting == cs.ClientReporting
  365. );
  366. }
  367. // This function implements the variable substitution mechanism
  368. // for the configuration values. Each string read from the
  369. // configuration file is scanned for '[...]' enclosures. Each
  370. // one that is found is replaced by either a runtime variable
  371. // (%xxx) or an existing configuration key. When no further
  372. // substitution is possible, the remaining string is returned
  373. // to the caller. This allows for arbitrarily nested
  374. // enclosures.
  375. private static string Substitute(RegionState rs, string instr)
  376. {
  377. string result = instr;
  378. if (result == null || result.Length == 0)
  379. return result;
  380. // Repeatedly scan the string until all possible
  381. // substitutions have been performed.
  382. // m_log.DebugFormat("[IRC-Channel] Parse[1]: {0}", result);
  383. while (arg.IsMatch(result))
  384. {
  385. string vvar = arg.Match(result).ToString();
  386. string var = vvar.Substring(1,vvar.Length-2).Trim();
  387. switch (var.ToLower())
  388. {
  389. case "%region" :
  390. result = result.Replace(vvar, rs.Region);
  391. break;
  392. case "%host" :
  393. result = result.Replace(vvar, rs.Host);
  394. break;
  395. case "%master1" :
  396. result = result.Replace(vvar, rs.MA1);
  397. break;
  398. case "%master2" :
  399. result = result.Replace(vvar, rs.MA2);
  400. break;
  401. case "%locx" :
  402. result = result.Replace(vvar, rs.LocX);
  403. break;
  404. case "%locy" :
  405. result = result.Replace(vvar, rs.LocY);
  406. break;
  407. case "%k" :
  408. result = result.Replace(vvar, rs.IDK);
  409. break;
  410. default :
  411. result = result.Replace(vvar, rs.config.GetString(var,var));
  412. break;
  413. }
  414. // m_log.DebugFormat("[IRC-Channel] Parse[2]: {0}", result);
  415. }
  416. // m_log.DebugFormat("[IRC-Channel] Parse[3]: {0}", result);
  417. return result;
  418. }
  419. public void Close()
  420. {
  421. m_log.InfoFormat("[IRC-Channel-{0}] Closing channel <{1}> to server <{2}:{3}>",
  422. idn, IrcChannel, Server, Port);
  423. m_log.InfoFormat("[IRC-Channel-{0}] There are {1} active clients",
  424. idn, clientregions.Count);
  425. irc.Close();
  426. }
  427. public void Open()
  428. {
  429. m_log.InfoFormat("[IRC-Channel-{0}] Opening channel <{1}> to server <{2}:{3}>",
  430. idn, IrcChannel, Server, Port);
  431. irc.Open();
  432. }
  433. // These are called by each region that attaches to this channel. The call affects
  434. // only the relationship of the region with the channel. Not the channel to IRC
  435. // relationship (unless it is closed and we want it open).
  436. public void Open(RegionState rs)
  437. {
  438. AddRegion(rs);
  439. Open();
  440. }
  441. // Close is called to ensure that the IRC session is terminated if this is the
  442. // only client.
  443. public void Close(RegionState rs)
  444. {
  445. RemoveRegion(rs);
  446. lock (IRCBridgeModule.m_channels)
  447. {
  448. if (clientregions.Count == 0)
  449. {
  450. Close();
  451. IRCBridgeModule.m_channels.Remove(this);
  452. m_log.InfoFormat("[IRC-Channel-{0}] Region {1} is last user of channel <{2}> to server <{3}:{4}>",
  453. idn, rs.Region, IrcChannel, Server, Port);
  454. m_log.InfoFormat("[IRC-Channel-{0}] Removed", idn);
  455. }
  456. }
  457. }
  458. // Add a client region to this channel if it is not already known
  459. public void AddRegion(RegionState rs)
  460. {
  461. m_log.InfoFormat("[IRC-Channel-{0}] Adding region {1} to channel <{2}> to server <{3}:{4}>",
  462. idn, rs.Region, IrcChannel, Server, Port);
  463. if (!clientregions.Contains(rs))
  464. {
  465. clientregions.Add(rs);
  466. lock (irc) irc.depends++;
  467. }
  468. }
  469. // Remove a client region from the channel. If this is the last
  470. // region, then clean up the channel. The connector will clean itself
  471. // up if it finds itself about to be GC'd.
  472. public void RemoveRegion(RegionState rs)
  473. {
  474. m_log.InfoFormat("[IRC-Channel-{0}] Removing region {1} from channel <{2} to server <{3}:{4}>",
  475. idn, rs.Region, IrcChannel, Server, Port);
  476. if (clientregions.Contains(rs))
  477. {
  478. clientregions.Remove(rs);
  479. lock (irc) irc.depends--;
  480. }
  481. }
  482. // This function is lifted from the IRCConnector because it
  483. // contains information that is not differentiating from an
  484. // IRC point-of-view.
  485. public static void OSChat(IRCConnector p_irc, OSChatMessage c, bool cmsg)
  486. {
  487. // m_log.DebugFormat("[IRC-OSCHAT] from {0}:{1}", p_irc.Server, p_irc.IrcChannel);
  488. try
  489. {
  490. // Scan through the set of unique channel configuration for those
  491. // that belong to this connector. And then forward the message to
  492. // all regions known to those channels.
  493. // Note that this code is responsible for completing some of the
  494. // settings for the inbound OSChatMessage
  495. lock (IRCBridgeModule.m_channels)
  496. {
  497. foreach (ChannelState cs in IRCBridgeModule.m_channels)
  498. {
  499. if ( p_irc == cs.irc)
  500. {
  501. // This non-IRC differentiator moved to here
  502. if (cmsg && !cs.ClientReporting)
  503. continue;
  504. // This non-IRC differentiator moved to here
  505. c.Channel = (cs.RelayPrivateChannels ? cs.RelayChannel : 0);
  506. foreach (RegionState region in cs.clientregions)
  507. {
  508. region.OSChat(cs.irc, c);
  509. }
  510. }
  511. }
  512. }
  513. }
  514. catch (Exception ex)
  515. {
  516. m_log.ErrorFormat("[IRC-OSCHAT]: BroadcastSim Exception: {0}", ex.Message);
  517. m_log.Debug(ex);
  518. }
  519. }
  520. }
  521. }