1
0

ChannelState.cs 28 KB

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