ChannelState.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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)
  192. throw new Exception(String.Format("[IRC-Channel-{0}] Invalid configuration for region {1}: server missing", cs.idn, rs.Region));
  193. else if (cs.IrcChannel == null)
  194. throw new Exception(String.Format("[IRC-Channel-{0}] Invalid configuration for region {1}: channel missing", cs.idn, rs.Region));
  195. else if (cs.BaseNickname == null)
  196. throw new Exception(String.Format("[IRC-Channel-{0}] Invalid configuration for region {1}: nick missing", cs.idn, rs.Region));
  197. else if (cs.User == null)
  198. throw new Exception(String.Format("[IRC-Channel-{0}] Invalid configuration for region {1}: user missing", cs.idn, rs.Region));
  199. m_log.InfoFormat("[IRC-Channel-{0}] Configuration for Region {1} is valid", cs.idn, rs.Region);
  200. m_log.InfoFormat("[IRC-Channel-{0}] Server = {1}", cs.idn, cs.Server);
  201. m_log.InfoFormat("[IRC-Channel-{0}] Channel = {1}", cs.idn, cs.IrcChannel);
  202. m_log.InfoFormat("[IRC-Channel-{0}] Port = {1}", cs.idn, cs.Port);
  203. m_log.InfoFormat("[IRC-Channel-{0}] Nickname = {1}", cs.idn, cs.BaseNickname);
  204. m_log.InfoFormat("[IRC-Channel-{0}] User = {1}", cs.idn, cs.User);
  205. // Set the channel state for this region
  206. if (cs.RelayChat)
  207. {
  208. cs.ValidInWorldChannels.Add(0);
  209. cs.ValidInWorldChannels.Add(DEBUG_CHANNEL);
  210. }
  211. if (cs.RelayPrivateChannels)
  212. cs.ValidInWorldChannels.Add(cs.RelayChannelOut);
  213. rs.cs = Integrate(rs, cs);
  214. }
  215. // An initialized channel state instance is passed in. If an identical
  216. // channel state instance already exists, then the existing instance
  217. // is used to replace the supplied value.
  218. // If the instance matches with respect to IRC, then the underlying
  219. // IRCConnector is assigned to the supplied channel state and the
  220. // updated value is returned.
  221. // If there is no match, then the supplied instance is completed by
  222. // creating and assigning an instance of an IRC connector.
  223. private static ChannelState Integrate(RegionState rs, ChannelState p_cs)
  224. {
  225. ChannelState cs = p_cs;
  226. // Check to see if we have an existing server/channel setup that can be used
  227. // In the absence of variable substitution this will always resolve to the
  228. // same ChannelState instance, and the table will only contains a single
  229. // entry, so the performance considerations for the existing behavior are
  230. // zero. Only the IRC connector is shared, the ChannelState still contains
  231. // values that, while independent of the IRC connetion, do still distinguish
  232. // this region's behavior.
  233. lock (IRCBridgeModule.m_channels)
  234. {
  235. foreach (ChannelState xcs in IRCBridgeModule.m_channels)
  236. {
  237. if (cs.IsAPerfectMatchFor(xcs))
  238. {
  239. m_log.DebugFormat("[IRC-Channel-{0}] Channel state matched", cs.idn);
  240. cs = xcs;
  241. break;
  242. }
  243. if (cs.IsAConnectionMatchFor(xcs))
  244. {
  245. m_log.DebugFormat("[IRC-Channel-{0}] Channel matched", cs.idn);
  246. cs.irc = xcs.irc;
  247. break;
  248. }
  249. }
  250. }
  251. // No entry was found, so this is going to be a new entry.
  252. if (cs.irc == null)
  253. {
  254. m_log.DebugFormat("[IRC-Channel-{0}] New channel required", cs.idn);
  255. if ((cs.irc = new IRCConnector(cs)) != null)
  256. {
  257. IRCBridgeModule.m_channels.Add(cs);
  258. m_log.InfoFormat("[IRC-Channel-{0}] New channel initialized for {1}, nick: {2}, commands {3}, private channels {4}",
  259. cs.idn, rs.Region, cs.DefaultZone,
  260. cs.CommandsEnabled ? "enabled" : "not enabled",
  261. cs.RelayPrivateChannels ? "relayed" : "not relayed");
  262. }
  263. else
  264. {
  265. string txt = String.Format("[IRC-Channel-{0}] Region {1} failed to connect to channel {2} on server {3}:{4}",
  266. cs.idn, rs.Region, cs.IrcChannel, cs.Server, cs.Port);
  267. m_log.Error(txt);
  268. throw new Exception(txt);
  269. }
  270. }
  271. else
  272. {
  273. m_log.InfoFormat("[IRC-Channel-{0}] Region {1} reusing existing connection to channel {2} on server {3}:{4}",
  274. cs.idn, rs.Region, cs.IrcChannel, cs.Server, cs.Port);
  275. }
  276. m_log.InfoFormat("[IRC-Channel-{0}] Region {1} associated with channel {2} on server {3}:{4}",
  277. cs.idn, rs.Region, cs.IrcChannel, cs.Server, cs.Port);
  278. // We're finally ready to commit ourselves
  279. return cs;
  280. }
  281. // These routines allow differentiating changes to
  282. // the underlying channel state. If necessary, a
  283. // new channel state will be created.
  284. internal ChannelState UpdateServer(RegionState rs, string server)
  285. {
  286. RemoveRegion(rs);
  287. ChannelState cs = new ChannelState(this);
  288. cs.Server = server;
  289. cs = Integrate(rs, cs);
  290. cs.AddRegion(rs);
  291. return cs;
  292. }
  293. internal ChannelState UpdatePort(RegionState rs, string port)
  294. {
  295. RemoveRegion(rs);
  296. ChannelState cs = new ChannelState(this);
  297. cs.Port = Convert.ToUInt32(port);
  298. cs = Integrate(rs, cs);
  299. cs.AddRegion(rs);
  300. return cs;
  301. }
  302. internal ChannelState UpdateChannel(RegionState rs, string channel)
  303. {
  304. RemoveRegion(rs);
  305. ChannelState cs = new ChannelState(this);
  306. cs.IrcChannel = channel;
  307. cs = Integrate(rs, cs);
  308. cs.AddRegion(rs);
  309. return cs;
  310. }
  311. internal ChannelState UpdateNickname(RegionState rs, string nickname)
  312. {
  313. RemoveRegion(rs);
  314. ChannelState cs = new ChannelState(this);
  315. cs.BaseNickname = nickname;
  316. cs = Integrate(rs, cs);
  317. cs.AddRegion(rs);
  318. return cs;
  319. }
  320. internal ChannelState UpdateClientReporting(RegionState rs, string cr)
  321. {
  322. RemoveRegion(rs);
  323. ChannelState cs = new ChannelState(this);
  324. cs.ClientReporting = Convert.ToBoolean(cr);
  325. cs = Integrate(rs, cs);
  326. cs.AddRegion(rs);
  327. return cs;
  328. }
  329. internal ChannelState UpdateRelayIn(RegionState rs, string channel)
  330. {
  331. RemoveRegion(rs);
  332. ChannelState cs = new ChannelState(this);
  333. cs.RelayChannel = Convert.ToInt32(channel);
  334. cs = Integrate(rs, cs);
  335. cs.AddRegion(rs);
  336. return cs;
  337. }
  338. internal ChannelState UpdateRelayOut(RegionState rs, string channel)
  339. {
  340. RemoveRegion(rs);
  341. ChannelState cs = new ChannelState(this);
  342. cs.RelayChannelOut = Convert.ToInt32(channel);
  343. cs = Integrate(rs, cs);
  344. cs.AddRegion(rs);
  345. return cs;
  346. }
  347. // Determine whether or not this is a 'new' channel. Only those
  348. // attributes that uniquely distinguish an IRC connection should
  349. // be included here (and only those attributes should really be
  350. // in the ChannelState structure)
  351. private bool IsAConnectionMatchFor(ChannelState cs)
  352. {
  353. return (
  354. Server == cs.Server &&
  355. IrcChannel == cs.IrcChannel &&
  356. Port == cs.Port &&
  357. BaseNickname == cs.BaseNickname &&
  358. User == cs.User
  359. );
  360. }
  361. // This level of obsessive matching allows us to produce
  362. // a minimal overhead int he case of a server which does
  363. // need to differentiate IRC at a region level.
  364. private bool IsAPerfectMatchFor(ChannelState cs)
  365. {
  366. return (IsAConnectionMatchFor(cs) &&
  367. RelayChannelOut == cs.RelayChannelOut &&
  368. PrivateMessageFormat == cs.PrivateMessageFormat &&
  369. NoticeMessageFormat == cs.NoticeMessageFormat &&
  370. RandomizeNickname == cs.RandomizeNickname &&
  371. AccessPassword == cs.AccessPassword &&
  372. CommandsEnabled == cs.CommandsEnabled &&
  373. CommandChannel == cs.CommandChannel &&
  374. DefaultZone == cs.DefaultZone &&
  375. RelayPrivateChannels == cs.RelayPrivateChannels &&
  376. RelayChannel == cs.RelayChannel &&
  377. RelayChat == cs.RelayChat &&
  378. ClientReporting == cs.ClientReporting
  379. );
  380. }
  381. // This function implements the variable substitution mechanism
  382. // for the configuration values. Each string read from the
  383. // configuration file is scanned for '[...]' enclosures. Each
  384. // one that is found is replaced by either a runtime variable
  385. // (%xxx) or an existing configuration key. When no further
  386. // substitution is possible, the remaining string is returned
  387. // to the caller. This allows for arbitrarily nested
  388. // enclosures.
  389. private static string Substitute(RegionState rs, string instr)
  390. {
  391. string result = instr;
  392. if (string.IsNullOrEmpty(result))
  393. return result;
  394. // Repeatedly scan the string until all possible
  395. // substitutions have been performed.
  396. // m_log.DebugFormat("[IRC-Channel] Parse[1]: {0}", result);
  397. while (arg.IsMatch(result))
  398. {
  399. string vvar = arg.Match(result).ToString();
  400. string var = vvar.Substring(1, vvar.Length - 2).Trim();
  401. switch (var.ToLower())
  402. {
  403. case "%region":
  404. result = result.Replace(vvar, rs.Region);
  405. break;
  406. case "%host":
  407. result = result.Replace(vvar, rs.Host);
  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. // Now we unescape the literal brackets
  425. result = result.Replace(@"\[","[").Replace(@"\]","]");
  426. // m_log.DebugFormat("[IRC-Channel] Parse[3]: {0}", result);
  427. return result;
  428. }
  429. public void Close()
  430. {
  431. m_log.InfoFormat("[IRC-Channel-{0}] Closing channel <{1}> to server <{2}:{3}>",
  432. idn, IrcChannel, Server, Port);
  433. m_log.InfoFormat("[IRC-Channel-{0}] There are {1} active clients",
  434. idn, clientregions.Count);
  435. irc.Close();
  436. }
  437. public void Open()
  438. {
  439. m_log.InfoFormat("[IRC-Channel-{0}] Opening channel <{1}> to server <{2}:{3}>",
  440. idn, IrcChannel, Server, Port);
  441. irc.Open();
  442. }
  443. // These are called by each region that attaches to this channel. The call affects
  444. // only the relationship of the region with the channel. Not the channel to IRC
  445. // relationship (unless it is closed and we want it open).
  446. public void Open(RegionState rs)
  447. {
  448. AddRegion(rs);
  449. Open();
  450. }
  451. // Close is called to ensure that the IRC session is terminated if this is the
  452. // only client.
  453. public void Close(RegionState rs)
  454. {
  455. RemoveRegion(rs);
  456. lock (IRCBridgeModule.m_channels)
  457. {
  458. if (clientregions.Count == 0)
  459. {
  460. Close();
  461. IRCBridgeModule.m_channels.Remove(this);
  462. m_log.InfoFormat("[IRC-Channel-{0}] Region {1} is last user of channel <{2}> to server <{3}:{4}>",
  463. idn, rs.Region, IrcChannel, Server, Port);
  464. m_log.InfoFormat("[IRC-Channel-{0}] Removed", idn);
  465. }
  466. }
  467. }
  468. // Add a client region to this channel if it is not already known
  469. public void AddRegion(RegionState rs)
  470. {
  471. m_log.InfoFormat("[IRC-Channel-{0}] Adding region {1} to channel <{2}> to server <{3}:{4}>",
  472. idn, rs.Region, IrcChannel, Server, Port);
  473. if (!clientregions.Contains(rs))
  474. {
  475. clientregions.Add(rs);
  476. lock (irc) irc.depends++;
  477. }
  478. }
  479. // Remove a client region from the channel. If this is the last
  480. // region, then clean up the channel. The connector will clean itself
  481. // up if it finds itself about to be GC'd.
  482. public void RemoveRegion(RegionState rs)
  483. {
  484. m_log.InfoFormat("[IRC-Channel-{0}] Removing region {1} from channel <{2} to server <{3}:{4}>",
  485. idn, rs.Region, IrcChannel, Server, Port);
  486. if (clientregions.Contains(rs))
  487. {
  488. clientregions.Remove(rs);
  489. lock (irc) irc.depends--;
  490. }
  491. }
  492. // This function is lifted from the IRCConnector because it
  493. // contains information that is not differentiating from an
  494. // IRC point-of-view.
  495. public static void OSChat(IRCConnector p_irc, OSChatMessage c, bool cmsg)
  496. {
  497. // m_log.DebugFormat("[IRC-OSCHAT] from {0}:{1}", p_irc.Server, p_irc.IrcChannel);
  498. try
  499. {
  500. // Scan through the set of unique channel configuration for those
  501. // that belong to this connector. And then forward the message to
  502. // all regions known to those channels.
  503. // Note that this code is responsible for completing some of the
  504. // settings for the inbound OSChatMessage
  505. lock (IRCBridgeModule.m_channels)
  506. {
  507. foreach (ChannelState cs in IRCBridgeModule.m_channels)
  508. {
  509. if (p_irc == cs.irc)
  510. {
  511. // This non-IRC differentiator moved to here
  512. if (cmsg && !cs.ClientReporting)
  513. continue;
  514. // This non-IRC differentiator moved to here
  515. c.Channel = (cs.RelayPrivateChannels ? cs.RelayChannel : 0);
  516. foreach (RegionState region in cs.clientregions)
  517. {
  518. region.OSChat(cs.irc, c);
  519. }
  520. }
  521. }
  522. }
  523. }
  524. catch (Exception ex)
  525. {
  526. m_log.ErrorFormat("[IRC-OSCHAT]: BroadcastSim Exception: {0}", ex.Message);
  527. m_log.Debug(ex);
  528. }
  529. }
  530. }
  531. }