ChannelState.cs 28 KB

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