1
0

ChannelState.cs 28 KB

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