diff options
author | Dr Scofield | 2009-02-10 13:10:57 +0000 |
---|---|---|
committer | Dr Scofield | 2009-02-10 13:10:57 +0000 |
commit | 180be7de07014aa33bc6066f12a0819b731c1c9d (patch) | |
tree | 3aa13af3cda4b808fa9453655875327699b61311 /OpenSim/Region/OptionalModules/Avatar/Chat/IRCConnector.cs | |
parent | Stopgap measure: To use gridlaunch, or GUI, start opensim with (diff) | |
download | opensim-SC-180be7de07014aa33bc6066f12a0819b731c1c9d.zip opensim-SC-180be7de07014aa33bc6066f12a0819b731c1c9d.tar.gz opensim-SC-180be7de07014aa33bc6066f12a0819b731c1c9d.tar.bz2 opensim-SC-180be7de07014aa33bc6066f12a0819b731c1c9d.tar.xz |
this is step 2 of 2 of the OpenSim.Region.Environment refactor.
NOTHING has been deleted or moved off to forge at this point. what
has happened is that OpenSim.Region.Environment.Modules has been split
in two:
- OpenSim.Region.CoreModules: all those modules that are either
directly or indirectly referenced from other OpenSim packages, or
that provide functionality that the OpenSim developer community
considers core functionality:
CoreModules/Agent/AssetTransaction
CoreModules/Agent/Capabilities
CoreModules/Agent/TextureDownload
CoreModules/Agent/TextureSender
CoreModules/Agent/TextureSender/Tests
CoreModules/Agent/Xfer
CoreModules/Avatar/AvatarFactory
CoreModules/Avatar/Chat/ChatModule
CoreModules/Avatar/Combat
CoreModules/Avatar/Currency/SampleMoney
CoreModules/Avatar/Dialog
CoreModules/Avatar/Friends
CoreModules/Avatar/Gestures
CoreModules/Avatar/Groups
CoreModules/Avatar/InstantMessage
CoreModules/Avatar/Inventory
CoreModules/Avatar/Inventory/Archiver
CoreModules/Avatar/Inventory/Transfer
CoreModules/Avatar/Lure
CoreModules/Avatar/ObjectCaps
CoreModules/Avatar/Profiles
CoreModules/Communications/Local
CoreModules/Communications/REST
CoreModules/Framework/EventQueue
CoreModules/Framework/InterfaceCommander
CoreModules/Hypergrid
CoreModules/InterGrid
CoreModules/Scripting/DynamicTexture
CoreModules/Scripting/EMailModules
CoreModules/Scripting/HttpRequest
CoreModules/Scripting/LoadImageURL
CoreModules/Scripting/VectorRender
CoreModules/Scripting/WorldComm
CoreModules/Scripting/XMLRPC
CoreModules/World/Archiver
CoreModules/World/Archiver/Tests
CoreModules/World/Estate
CoreModules/World/Land
CoreModules/World/Permissions
CoreModules/World/Serialiser
CoreModules/World/Sound
CoreModules/World/Sun
CoreModules/World/Terrain
CoreModules/World/Terrain/DefaultEffects
CoreModules/World/Terrain/DefaultEffects/bin
CoreModules/World/Terrain/DefaultEffects/bin/Debug
CoreModules/World/Terrain/Effects
CoreModules/World/Terrain/FileLoaders
CoreModules/World/Terrain/FloodBrushes
CoreModules/World/Terrain/PaintBrushes
CoreModules/World/Terrain/Tests
CoreModules/World/Vegetation
CoreModules/World/Wind
CoreModules/World/WorldMap
- OpenSim.Region.OptionalModules: all those modules that are not core
modules:
OptionalModules/Avatar/Chat/IRC-stuff
OptionalModules/Avatar/Concierge
OptionalModules/Avatar/Voice/AsterixVoice
OptionalModules/Avatar/Voice/SIPVoice
OptionalModules/ContentManagementSystem
OptionalModules/Grid/Interregion
OptionalModules/Python
OptionalModules/SvnSerialiser
OptionalModules/World/NPC
OptionalModules/World/TreePopulator
Diffstat (limited to 'OpenSim/Region/OptionalModules/Avatar/Chat/IRCConnector.cs')
-rw-r--r-- | OpenSim/Region/OptionalModules/Avatar/Chat/IRCConnector.cs | 887 |
1 files changed, 887 insertions, 0 deletions
diff --git a/OpenSim/Region/OptionalModules/Avatar/Chat/IRCConnector.cs b/OpenSim/Region/OptionalModules/Avatar/Chat/IRCConnector.cs new file mode 100644 index 0000000..f5c324d --- /dev/null +++ b/OpenSim/Region/OptionalModules/Avatar/Chat/IRCConnector.cs | |||
@@ -0,0 +1,887 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Timers; | ||
30 | using System.Collections.Generic; | ||
31 | using System.IO; | ||
32 | using System.Net.Sockets; | ||
33 | using System.Reflection; | ||
34 | using System.Text.RegularExpressions; | ||
35 | using System.Threading; | ||
36 | using OpenMetaverse; | ||
37 | using log4net; | ||
38 | using Nini.Config; | ||
39 | using OpenSim.Framework; | ||
40 | using OpenSim.Region.Framework.Interfaces; | ||
41 | using OpenSim.Region.Framework.Scenes; | ||
42 | |||
43 | namespace OpenSim.Region.OptionalModules.Avatar.Chat | ||
44 | { | ||
45 | public class IRCConnector | ||
46 | { | ||
47 | |||
48 | #region Global (static) state | ||
49 | |||
50 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
51 | |||
52 | // Local constants | ||
53 | |||
54 | private static readonly Vector3 CenterOfRegion = new Vector3(128, 128, 20); | ||
55 | private static readonly char[] CS_SPACE = { ' ' }; | ||
56 | |||
57 | private const int WD_INTERVAL = 1000; // base watchdog interval | ||
58 | private static int PING_PERIOD = 15; // WD intervals per PING | ||
59 | private static int ICCD_PERIOD = 10; // WD intervals between Connects | ||
60 | private static int L_TIMEOUT = 25; // Login time out interval | ||
61 | |||
62 | private static int _idk_ = 0; // core connector identifier | ||
63 | private static int _pdk_ = 0; // ping interval counter | ||
64 | private static int _icc_ = 0; // IRC connect counter | ||
65 | |||
66 | // List of configured connectors | ||
67 | |||
68 | private static List<IRCConnector> m_connectors = new List<IRCConnector>(); | ||
69 | |||
70 | // Watchdog state | ||
71 | |||
72 | private static System.Timers.Timer m_watchdog = null; | ||
73 | |||
74 | static IRCConnector() | ||
75 | { | ||
76 | m_log.DebugFormat("[IRC-Connector]: Static initialization started"); | ||
77 | m_watchdog = new System.Timers.Timer(WD_INTERVAL); | ||
78 | m_watchdog.Elapsed += new ElapsedEventHandler(WatchdogHandler); | ||
79 | m_watchdog.AutoReset = true; | ||
80 | m_watchdog.Start(); | ||
81 | m_log.DebugFormat("[IRC-Connector]: Static initialization complete"); | ||
82 | } | ||
83 | |||
84 | #endregion | ||
85 | |||
86 | #region Instance state | ||
87 | |||
88 | // Connector identity | ||
89 | |||
90 | internal int idn = _idk_++; | ||
91 | |||
92 | // How many regions depend upon this connection | ||
93 | // This count is updated by the ChannelState object and reflects the sum | ||
94 | // of the region clients associated with the set of associated channel | ||
95 | // state instances. That's why it cannot be managed here. | ||
96 | |||
97 | internal int depends = 0; | ||
98 | |||
99 | // Working threads | ||
100 | |||
101 | private Thread m_listener = null; | ||
102 | |||
103 | private Object msyncConnect = new Object(); | ||
104 | |||
105 | internal bool m_randomizeNick = true; // add random suffix | ||
106 | internal string m_baseNick = null; // base name for randomizing | ||
107 | internal string m_nick = null; // effective nickname | ||
108 | |||
109 | public string Nick // Public property | ||
110 | { | ||
111 | get { return m_nick; } | ||
112 | set { m_nick = value; } | ||
113 | } | ||
114 | |||
115 | private bool m_enabled = false; // connector enablement | ||
116 | public bool Enabled | ||
117 | { | ||
118 | get { return m_enabled; } | ||
119 | } | ||
120 | |||
121 | private bool m_connected = false; // connection status | ||
122 | private bool m_pending = false; // login disposition | ||
123 | private int m_timeout = L_TIMEOUT; // login timeout counter | ||
124 | public bool Connected | ||
125 | { | ||
126 | get { return m_connected; } | ||
127 | } | ||
128 | |||
129 | private string m_ircChannel; // associated channel id | ||
130 | public string IrcChannel | ||
131 | { | ||
132 | get { return m_ircChannel; } | ||
133 | set { m_ircChannel = value; } | ||
134 | } | ||
135 | |||
136 | private uint m_port = 6667; // session port | ||
137 | public uint Port | ||
138 | { | ||
139 | get { return m_port; } | ||
140 | set { m_port = value; } | ||
141 | } | ||
142 | |||
143 | private string m_server = null; // IRC server name | ||
144 | public string Server | ||
145 | { | ||
146 | get { return m_server; } | ||
147 | set { m_server = value; } | ||
148 | } | ||
149 | private string m_password = null; | ||
150 | public string Password | ||
151 | { | ||
152 | get { return m_password; } | ||
153 | set { m_password = value; } | ||
154 | } | ||
155 | |||
156 | private string m_user = "USER OpenSimBot 8 * :I'm an OpenSim to IRC bot"; | ||
157 | public string User | ||
158 | { | ||
159 | get { return m_user; } | ||
160 | } | ||
161 | |||
162 | // Network interface | ||
163 | |||
164 | private TcpClient m_tcp; | ||
165 | private NetworkStream m_stream = null; | ||
166 | private StreamReader m_reader; | ||
167 | private StreamWriter m_writer; | ||
168 | |||
169 | // Channel characteristic info (if available) | ||
170 | |||
171 | internal string usermod = String.Empty; | ||
172 | internal string chanmod = String.Empty; | ||
173 | internal string version = String.Empty; | ||
174 | internal bool motd = false; | ||
175 | |||
176 | #endregion | ||
177 | |||
178 | #region connector instance management | ||
179 | |||
180 | internal IRCConnector(ChannelState cs) | ||
181 | { | ||
182 | |||
183 | // Prepare network interface | ||
184 | |||
185 | m_tcp = null; | ||
186 | m_writer = null; | ||
187 | m_reader = null; | ||
188 | |||
189 | // Setup IRC session parameters | ||
190 | |||
191 | m_server = cs.Server; | ||
192 | m_password = cs.Password; | ||
193 | m_baseNick = cs.BaseNickname; | ||
194 | m_randomizeNick = cs.RandomizeNickname; | ||
195 | m_ircChannel = cs.IrcChannel; | ||
196 | m_port = cs.Port; | ||
197 | m_user = cs.User; | ||
198 | |||
199 | if (m_watchdog == null) | ||
200 | { | ||
201 | // Non-differentiating | ||
202 | |||
203 | ICCD_PERIOD = cs.ConnectDelay; | ||
204 | PING_PERIOD = cs.PingDelay; | ||
205 | |||
206 | // Smaller values are not reasonable | ||
207 | |||
208 | if (ICCD_PERIOD < 5) | ||
209 | ICCD_PERIOD = 5; | ||
210 | |||
211 | if (PING_PERIOD < 5) | ||
212 | PING_PERIOD = 5; | ||
213 | |||
214 | _icc_ = ICCD_PERIOD; // get started right away! | ||
215 | |||
216 | } | ||
217 | |||
218 | // The last line of defense | ||
219 | |||
220 | if (m_server == null || m_baseNick == null || m_ircChannel == null || m_user == null) | ||
221 | throw new Exception("Invalid connector configuration"); | ||
222 | |||
223 | // Generate an initial nickname if randomizing is enabled | ||
224 | |||
225 | if (m_randomizeNick) | ||
226 | { | ||
227 | m_nick = m_baseNick + Util.RandomClass.Next(1, 99); | ||
228 | } | ||
229 | |||
230 | // Add the newly created connector to the known connectors list | ||
231 | |||
232 | m_connectors.Add(this); | ||
233 | |||
234 | m_log.InfoFormat("[IRC-Connector-{0}]: Initialization complete", idn); | ||
235 | |||
236 | } | ||
237 | |||
238 | ~IRCConnector() | ||
239 | { | ||
240 | m_watchdog.Stop(); | ||
241 | Close(); | ||
242 | } | ||
243 | |||
244 | // Mark the connector as connectable. Harmless if already enabled. | ||
245 | |||
246 | public void Open() | ||
247 | { | ||
248 | if (!m_enabled) | ||
249 | { | ||
250 | |||
251 | m_connectors.Add(this); | ||
252 | m_enabled = true; | ||
253 | |||
254 | if (!Connected) | ||
255 | { | ||
256 | Connect(); | ||
257 | } | ||
258 | |||
259 | } | ||
260 | } | ||
261 | |||
262 | // Only close the connector if the dependency count is zero. | ||
263 | |||
264 | public void Close() | ||
265 | { | ||
266 | |||
267 | m_log.InfoFormat("[IRC-Connector-{0}] Closing", idn); | ||
268 | |||
269 | lock (msyncConnect) | ||
270 | { | ||
271 | |||
272 | if ((depends == 0) && Enabled) | ||
273 | { | ||
274 | |||
275 | m_enabled = false; | ||
276 | |||
277 | if (Connected) | ||
278 | { | ||
279 | m_log.DebugFormat("[IRC-Connector-{0}] Closing interface", idn); | ||
280 | |||
281 | // Cleanup the IRC session | ||
282 | |||
283 | try | ||
284 | { | ||
285 | m_writer.WriteLine(String.Format("QUIT :{0} to {1} wormhole to {2} closing", | ||
286 | m_nick, m_ircChannel, m_server)); | ||
287 | m_writer.Flush(); | ||
288 | } | ||
289 | catch (Exception) {} | ||
290 | |||
291 | |||
292 | m_connected = false; | ||
293 | |||
294 | try { m_writer.Close(); } catch (Exception) {} | ||
295 | try { m_reader.Close(); } catch (Exception) {} | ||
296 | try { m_stream.Close(); } catch (Exception) {} | ||
297 | try { m_tcp.Close(); } catch (Exception) {} | ||
298 | |||
299 | } | ||
300 | |||
301 | m_connectors.Remove(this); | ||
302 | |||
303 | } | ||
304 | } | ||
305 | |||
306 | m_log.InfoFormat("[IRC-Connector-{0}] Closed", idn); | ||
307 | |||
308 | } | ||
309 | |||
310 | #endregion | ||
311 | |||
312 | #region session management | ||
313 | |||
314 | // Connect to the IRC server. A connector should always be connected, once enabled | ||
315 | |||
316 | public void Connect() | ||
317 | { | ||
318 | |||
319 | if (!m_enabled) | ||
320 | return; | ||
321 | |||
322 | // Delay until next WD cycle if this is too close to the last start attempt | ||
323 | |||
324 | while (_icc_ < ICCD_PERIOD) | ||
325 | return; | ||
326 | |||
327 | m_log.DebugFormat("[IRC-Connector-{0}]: Connection request for {1} on {2}:{3}", idn, m_nick, m_server, m_ircChannel); | ||
328 | |||
329 | lock (msyncConnect) | ||
330 | { | ||
331 | |||
332 | _icc_ = 0; | ||
333 | |||
334 | try | ||
335 | { | ||
336 | if (m_connected) return; | ||
337 | |||
338 | m_connected = true; | ||
339 | m_pending = true; | ||
340 | m_timeout = L_TIMEOUT; | ||
341 | |||
342 | m_tcp = new TcpClient(m_server, (int)m_port); | ||
343 | m_stream = m_tcp.GetStream(); | ||
344 | m_reader = new StreamReader(m_stream); | ||
345 | m_writer = new StreamWriter(m_stream); | ||
346 | |||
347 | m_log.InfoFormat("[IRC-Connector-{0}]: Connected to {1}:{2}", idn, m_server, m_port); | ||
348 | |||
349 | m_listener = new Thread(new ThreadStart(ListenerRun)); | ||
350 | m_listener.Name = "IRCConnectorListenerThread"; | ||
351 | m_listener.IsBackground = true; | ||
352 | m_listener.Start(); | ||
353 | ThreadTracker.Add(m_listener); | ||
354 | |||
355 | // This is the message order recommended by RFC 2812 | ||
356 | if (m_password != null) | ||
357 | m_writer.WriteLine(String.Format("PASS {0}", m_password)); | ||
358 | m_writer.WriteLine(String.Format("NICK {0}", m_nick)); | ||
359 | m_writer.Flush(); | ||
360 | m_writer.WriteLine(m_user); | ||
361 | m_writer.Flush(); | ||
362 | m_writer.WriteLine(String.Format("JOIN {0}", m_ircChannel)); | ||
363 | m_writer.Flush(); | ||
364 | |||
365 | m_log.InfoFormat("[IRC-Connector-{0}]: {1} has asked to join {2}", idn, m_nick, m_ircChannel); | ||
366 | |||
367 | } | ||
368 | catch (Exception e) | ||
369 | { | ||
370 | m_log.ErrorFormat("[IRC-Connector-{0}] cannot connect {1} to {2}:{3}: {4}", | ||
371 | idn, m_nick, m_server, m_port, e.Message); | ||
372 | m_connected = false; | ||
373 | m_pending = false; | ||
374 | } | ||
375 | |||
376 | } | ||
377 | |||
378 | return; | ||
379 | |||
380 | } | ||
381 | |||
382 | // Reconnect is used to force a re-cycle of the IRC connection. Should generally | ||
383 | // be a transparent event | ||
384 | |||
385 | public void Reconnect() | ||
386 | { | ||
387 | m_log.DebugFormat("[IRC-Connector-{0}]: Reconnect request for {1} on {2}:{3}", idn, m_nick, m_server, m_ircChannel); | ||
388 | |||
389 | // Don't do this if a Connect is in progress... | ||
390 | |||
391 | lock (msyncConnect) | ||
392 | { | ||
393 | |||
394 | if (m_connected) | ||
395 | { | ||
396 | m_log.InfoFormat("[IRC-Connector-{0}] Resetting connector", idn); | ||
397 | |||
398 | // Mark as disconnected. This will allow the listener thread | ||
399 | // to exit if still in-flight. | ||
400 | |||
401 | |||
402 | // The listener thread is not aborted - it *might* actually be | ||
403 | // the thread that is running the Reconnect! Instead just close | ||
404 | // the socket and it will disappear of its own accord, once this | ||
405 | // processing is completed. | ||
406 | |||
407 | try { m_writer.Close(); } catch (Exception) {} | ||
408 | try { m_reader.Close(); } catch (Exception) {} | ||
409 | try { m_tcp.Close(); } catch (Exception) {} | ||
410 | |||
411 | m_connected = false; | ||
412 | m_pending = false; | ||
413 | |||
414 | } | ||
415 | |||
416 | } | ||
417 | |||
418 | Connect(); | ||
419 | |||
420 | } | ||
421 | |||
422 | #endregion | ||
423 | |||
424 | #region Outbound (to-IRC) message handlers | ||
425 | |||
426 | public void PrivMsg(string pattern, string from, string region, string msg) | ||
427 | { | ||
428 | |||
429 | m_log.DebugFormat("[IRC-Connector-{0}] PrivMsg to IRC from {1}: <{2}>", idn, from, | ||
430 | String.Format(pattern, m_ircChannel, from, region, msg)); | ||
431 | |||
432 | // One message to the IRC server | ||
433 | |||
434 | try | ||
435 | { | ||
436 | m_writer.WriteLine(pattern, m_ircChannel, from, region, msg); | ||
437 | m_writer.Flush(); | ||
438 | m_log.DebugFormat("[IRC-Connector-{0}]: PrivMsg from {1} in {2}: {3}", idn, from, region, msg); | ||
439 | } | ||
440 | catch (IOException) | ||
441 | { | ||
442 | m_log.ErrorFormat("[IRC-Connector-{0}]: PrivMsg I/O Error: disconnected from IRC server", idn); | ||
443 | Reconnect(); | ||
444 | } | ||
445 | catch (Exception ex) | ||
446 | { | ||
447 | m_log.ErrorFormat("[IRC-Connector-{0}]: PrivMsg exception : {1}", idn, ex.Message); | ||
448 | m_log.Debug(ex); | ||
449 | } | ||
450 | |||
451 | } | ||
452 | |||
453 | public void Send(string msg) | ||
454 | { | ||
455 | |||
456 | m_log.DebugFormat("[IRC-Connector-{0}] Send to IRC : <{1}>", idn, msg); | ||
457 | |||
458 | try | ||
459 | { | ||
460 | m_writer.WriteLine(msg); | ||
461 | m_writer.Flush(); | ||
462 | m_log.DebugFormat("[IRC-Connector-{0}] Sent command string: {1}", idn, msg); | ||
463 | } | ||
464 | catch (IOException) | ||
465 | { | ||
466 | m_log.ErrorFormat("[IRC-Connector-{0}] Disconnected from IRC server.(Send)", idn); | ||
467 | Reconnect(); | ||
468 | } | ||
469 | catch (Exception ex) | ||
470 | { | ||
471 | m_log.ErrorFormat("[IRC-Connector-{0}] Send exception trap: {0}", idn, ex.Message); | ||
472 | m_log.Debug(ex); | ||
473 | } | ||
474 | |||
475 | } | ||
476 | |||
477 | #endregion | ||
478 | |||
479 | public void ListenerRun() | ||
480 | { | ||
481 | string inputLine; | ||
482 | |||
483 | try | ||
484 | { | ||
485 | while (m_enabled && m_connected) | ||
486 | { | ||
487 | |||
488 | if ((inputLine = m_reader.ReadLine()) == null) | ||
489 | throw new Exception("Listener input socket closed"); | ||
490 | |||
491 | // m_log.Info("[IRCConnector]: " + inputLine); | ||
492 | |||
493 | if (inputLine.Contains("PRIVMSG")) | ||
494 | { | ||
495 | |||
496 | Dictionary<string, string> data = ExtractMsg(inputLine); | ||
497 | |||
498 | // Any chat ??? | ||
499 | if (data != null) | ||
500 | { | ||
501 | |||
502 | OSChatMessage c = new OSChatMessage(); | ||
503 | c.Message = data["msg"]; | ||
504 | c.Type = ChatTypeEnum.Region; | ||
505 | c.Position = CenterOfRegion; | ||
506 | c.From = data["nick"]; | ||
507 | c.Sender = null; | ||
508 | c.SenderUUID = UUID.Zero; | ||
509 | |||
510 | // Is message "\001ACTION foo bar\001"? | ||
511 | // Then change to: "/me foo bar" | ||
512 | |||
513 | if ((1 == c.Message[0]) && c.Message.Substring(1).StartsWith("ACTION")) | ||
514 | c.Message = String.Format("/me {0}", c.Message.Substring(8, c.Message.Length - 9)); | ||
515 | |||
516 | ChannelState.OSChat(this, c, false); | ||
517 | |||
518 | } | ||
519 | |||
520 | } | ||
521 | else | ||
522 | { | ||
523 | ProcessIRCCommand(inputLine); | ||
524 | } | ||
525 | } | ||
526 | } | ||
527 | catch (Exception /*e*/) | ||
528 | { | ||
529 | // m_log.ErrorFormat("[IRC-Connector-{0}]: ListenerRun exception trap: {1}", idn, e.Message); | ||
530 | // m_log.Debug(e); | ||
531 | } | ||
532 | |||
533 | // This is potentially circular, but harmless if so. | ||
534 | // The connection is marked as not connected the first time | ||
535 | // through reconnect. | ||
536 | |||
537 | if (m_enabled) Reconnect(); | ||
538 | |||
539 | } | ||
540 | |||
541 | private Regex RE = new Regex(@":(?<nick>[\w-]*)!(?<user>\S*) PRIVMSG (?<channel>\S+) :(?<msg>.*)", | ||
542 | RegexOptions.Multiline); | ||
543 | |||
544 | private Dictionary<string, string> ExtractMsg(string input) | ||
545 | { | ||
546 | //examines IRC commands and extracts any private messages | ||
547 | // which will then be reboadcast in the Sim | ||
548 | |||
549 | // m_log.InfoFormat("[IRC-Connector-{0}]: ExtractMsg: {1}", idn, input); | ||
550 | |||
551 | Dictionary<string, string> result = null; | ||
552 | MatchCollection matches = RE.Matches(input); | ||
553 | |||
554 | // Get some direct matches $1 $4 is a | ||
555 | if ((matches.Count == 0) || (matches.Count != 1) || (matches[0].Groups.Count != 5)) | ||
556 | { | ||
557 | // m_log.Info("[IRCConnector]: Number of matches: " + matches.Count); | ||
558 | // if (matches.Count > 0) | ||
559 | // { | ||
560 | // m_log.Info("[IRCConnector]: Number of groups: " + matches[0].Groups.Count); | ||
561 | // } | ||
562 | return null; | ||
563 | } | ||
564 | |||
565 | result = new Dictionary<string, string>(); | ||
566 | result.Add("nick", matches[0].Groups[1].Value); | ||
567 | result.Add("user", matches[0].Groups[2].Value); | ||
568 | result.Add("channel", matches[0].Groups[3].Value); | ||
569 | result.Add("msg", matches[0].Groups[4].Value); | ||
570 | |||
571 | return result; | ||
572 | } | ||
573 | |||
574 | public void BroadcastSim(string sender, string format, params string[] args) | ||
575 | { | ||
576 | try | ||
577 | { | ||
578 | OSChatMessage c = new OSChatMessage(); | ||
579 | c.From = sender; | ||
580 | c.Message = String.Format(format, args); | ||
581 | c.Type = ChatTypeEnum.Region; // ChatTypeEnum.Say; | ||
582 | c.Position = CenterOfRegion; | ||
583 | c.Sender = null; | ||
584 | c.SenderUUID = UUID.Zero; | ||
585 | |||
586 | ChannelState.OSChat(this, c, true); | ||
587 | |||
588 | } | ||
589 | catch (Exception ex) // IRC gate should not crash Sim | ||
590 | { | ||
591 | m_log.ErrorFormat("[IRC-Connector-{0}]: BroadcastSim Exception Trap: {1}\n{2}", idn, ex.Message, ex.StackTrace); | ||
592 | } | ||
593 | } | ||
594 | |||
595 | #region IRC Command Handlers | ||
596 | |||
597 | public void ProcessIRCCommand(string command) | ||
598 | { | ||
599 | |||
600 | string[] commArgs; | ||
601 | string c_server = m_server; | ||
602 | |||
603 | string pfx = String.Empty; | ||
604 | string cmd = String.Empty; | ||
605 | string parms = String.Empty; | ||
606 | |||
607 | // ":" indicates that a prefix is present | ||
608 | // There are NEVER more than 17 real | ||
609 | // fields. A parameter that starts with | ||
610 | // ":" indicates that the remainder of the | ||
611 | // line is a single parameter value. | ||
612 | |||
613 | commArgs = command.Split(CS_SPACE,2); | ||
614 | |||
615 | if (commArgs[0].StartsWith(":")) | ||
616 | { | ||
617 | pfx = commArgs[0].Substring(1); | ||
618 | commArgs = commArgs[1].Split(CS_SPACE,2); | ||
619 | } | ||
620 | |||
621 | cmd = commArgs[0]; | ||
622 | parms = commArgs[1]; | ||
623 | |||
624 | // m_log.DebugFormat("[IRC-Connector-{0}] prefix = <{1}> cmd = <{2}>", idn, pfx, cmd); | ||
625 | |||
626 | switch (cmd) | ||
627 | { | ||
628 | |||
629 | // Messages 001-004 are always sent | ||
630 | // following signon. | ||
631 | |||
632 | case "001" : // Welcome ... | ||
633 | case "002" : // Server information | ||
634 | case "003" : // Welcome ... | ||
635 | break; | ||
636 | case "004" : // Server information | ||
637 | m_log.DebugFormat("[IRC-Connector-{0}] [{1}] parms = <{2}>", idn, cmd, parms); | ||
638 | commArgs = parms.Split(CS_SPACE); | ||
639 | c_server = commArgs[1]; | ||
640 | m_server = c_server; | ||
641 | version = commArgs[2]; | ||
642 | usermod = commArgs[3]; | ||
643 | chanmod = commArgs[4]; | ||
644 | break; | ||
645 | case "005" : // Server information | ||
646 | break; | ||
647 | case "042" : | ||
648 | case "250" : | ||
649 | case "251" : | ||
650 | case "252" : | ||
651 | case "254" : | ||
652 | case "255" : | ||
653 | case "265" : | ||
654 | case "266" : | ||
655 | case "332" : // Subject | ||
656 | case "333" : // Subject owner (?) | ||
657 | case "353" : // Name list | ||
658 | case "366" : // End-of-Name list marker | ||
659 | case "372" : // MOTD body | ||
660 | case "375" : // MOTD start | ||
661 | m_log.InfoFormat("[IRC-Connector-{0}] [{1}] {2}", idn, cmd, parms.Split(CS_SPACE,2)[1]); | ||
662 | break; | ||
663 | case "376" : // MOTD end | ||
664 | m_log.InfoFormat("[IRC-Connector-{0}] [{1}] {2}", idn, cmd, parms.Split(CS_SPACE,2)[1]); | ||
665 | motd = true; | ||
666 | break; | ||
667 | case "451" : // Not registered | ||
668 | break; | ||
669 | case "433" : // Nickname in use | ||
670 | // Gen a new name | ||
671 | m_nick = m_baseNick + Util.RandomClass.Next(1, 99); | ||
672 | m_log.ErrorFormat("[IRC-Connector-{0}]: [{1}] IRC SERVER reports NicknameInUse, trying {2}", idn, cmd, m_nick); | ||
673 | // Retry | ||
674 | m_writer.WriteLine(String.Format("NICK {0}", m_nick)); | ||
675 | m_writer.Flush(); | ||
676 | m_writer.WriteLine(m_user); | ||
677 | m_writer.Flush(); | ||
678 | m_writer.WriteLine(String.Format("JOIN {0}", m_ircChannel)); | ||
679 | m_writer.Flush(); | ||
680 | break; | ||
681 | case "479" : // Bad channel name, etc. This will never work, so disable the connection | ||
682 | m_log.ErrorFormat("[IRC-Connector-{0}] [{1}] {2}", idn, cmd, parms.Split(CS_SPACE,2)[1]); | ||
683 | m_log.ErrorFormat("[IRC-Connector-{0}] [{1}] Connector disabled", idn, cmd); | ||
684 | m_enabled = false; | ||
685 | m_connected = false; | ||
686 | m_pending = false; | ||
687 | break; | ||
688 | case "NOTICE" : | ||
689 | m_log.WarnFormat("[IRC-Connector-{0}] [{1}] {2}", idn, cmd, parms.Split(CS_SPACE,2)[1]); | ||
690 | break; | ||
691 | case "ERROR" : | ||
692 | m_log.ErrorFormat("[IRC-Connector-{0}] [{1}] {2}", idn, cmd, parms.Split(CS_SPACE,2)[1]); | ||
693 | if (parms.Contains("reconnect too fast")) | ||
694 | ICCD_PERIOD++; | ||
695 | m_pending = false; | ||
696 | Reconnect(); | ||
697 | break; | ||
698 | case "PING" : | ||
699 | m_log.DebugFormat("[IRC-Connector-{0}] [{1}] parms = <{2}>", idn, cmd, parms); | ||
700 | m_writer.WriteLine(String.Format("PONG {0}", parms)); | ||
701 | m_writer.Flush(); | ||
702 | break; | ||
703 | case "PONG" : | ||
704 | break; | ||
705 | case "JOIN": | ||
706 | if (m_pending) | ||
707 | { | ||
708 | m_log.InfoFormat("[IRC-Connector-{0}] [{1}] Connected", idn, cmd); | ||
709 | m_pending = false; | ||
710 | } | ||
711 | m_log.DebugFormat("[IRC-Connector-{0}] [{1}] parms = <{2}>", idn, cmd, parms); | ||
712 | eventIrcJoin(pfx, cmd, parms); | ||
713 | break; | ||
714 | case "PART": | ||
715 | m_log.DebugFormat("[IRC-Connector-{0}] [{1}] parms = <{2}>", idn, cmd, parms); | ||
716 | eventIrcPart(pfx, cmd, parms); | ||
717 | break; | ||
718 | case "MODE": | ||
719 | m_log.DebugFormat("[IRC-Connector-{0}] [{1}] parms = <{2}>", idn, cmd, parms); | ||
720 | eventIrcMode(pfx, cmd, parms); | ||
721 | break; | ||
722 | case "NICK": | ||
723 | m_log.DebugFormat("[IRC-Connector-{0}] [{1}] parms = <{2}>", idn, cmd, parms); | ||
724 | eventIrcNickChange(pfx, cmd, parms); | ||
725 | break; | ||
726 | case "KICK": | ||
727 | m_log.DebugFormat("[IRC-Connector-{0}] [{1}] parms = <{2}>", idn, cmd, parms); | ||
728 | eventIrcKick(pfx, cmd, parms); | ||
729 | break; | ||
730 | case "QUIT": | ||
731 | m_log.DebugFormat("[IRC-Connector-{0}] [{1}] parms = <{2}>", idn, cmd, parms); | ||
732 | eventIrcQuit(pfx, cmd, parms); | ||
733 | break; | ||
734 | default : | ||
735 | m_log.DebugFormat("[IRC-Connector-{0}] Command '{1}' ignored, parms = {2}", idn, cmd, parms); | ||
736 | break; | ||
737 | } | ||
738 | |||
739 | // m_log.DebugFormat("[IRC-Connector-{0}] prefix = <{1}> cmd = <{2}> complete", idn, pfx, cmd); | ||
740 | |||
741 | } | ||
742 | |||
743 | public void eventIrcJoin(string prefix, string command, string parms) | ||
744 | { | ||
745 | string[] args = parms.Split(CS_SPACE,2); | ||
746 | string IrcUser = prefix.Split('!')[0]; | ||
747 | string IrcChannel = args[0]; | ||
748 | |||
749 | if (IrcChannel.StartsWith(":")) | ||
750 | IrcChannel = IrcChannel.Substring(1); | ||
751 | |||
752 | m_log.DebugFormat("[IRC-Connector-{0}] Event: IRCJoin {1}:{2}", idn, m_server, m_ircChannel); | ||
753 | BroadcastSim(IrcUser, "/me joins {0}", IrcChannel); | ||
754 | } | ||
755 | |||
756 | public void eventIrcPart(string prefix, string command, string parms) | ||
757 | { | ||
758 | string[] args = parms.Split(CS_SPACE,2); | ||
759 | string IrcUser = prefix.Split('!')[0]; | ||
760 | string IrcChannel = args[0]; | ||
761 | |||
762 | m_log.DebugFormat("[IRC-Connector-{0}] Event: IRCPart {1}:{2}", idn, m_server, m_ircChannel); | ||
763 | BroadcastSim(IrcUser, "/me parts {0}", IrcChannel); | ||
764 | } | ||
765 | |||
766 | public void eventIrcMode(string prefix, string command, string parms) | ||
767 | { | ||
768 | string[] args = parms.Split(CS_SPACE,2); | ||
769 | string UserMode = args[1]; | ||
770 | |||
771 | m_log.DebugFormat("[IRC-Connector-{0}] Event: IRCMode {1}:{2}", idn, m_server, m_ircChannel); | ||
772 | if (UserMode.Substring(0, 1) == ":") | ||
773 | { | ||
774 | UserMode = UserMode.Remove(0, 1); | ||
775 | } | ||
776 | } | ||
777 | |||
778 | public void eventIrcNickChange(string prefix, string command, string parms) | ||
779 | { | ||
780 | string[] args = parms.Split(CS_SPACE,2); | ||
781 | string UserOldNick = prefix.Split('!')[0]; | ||
782 | string UserNewNick = args[0].Remove(0, 1); | ||
783 | |||
784 | m_log.DebugFormat("[IRC-Connector-{0}] Event: IRCNickChange {1}:{2}", idn, m_server, m_ircChannel); | ||
785 | BroadcastSim(UserOldNick, "/me is now known as {0}", UserNewNick); | ||
786 | } | ||
787 | |||
788 | public void eventIrcKick(string prefix, string command, string parms) | ||
789 | { | ||
790 | string[] args = parms.Split(CS_SPACE,3); | ||
791 | string UserKicker = prefix.Split('!')[0]; | ||
792 | string IrcChannel = args[0]; | ||
793 | string UserKicked = args[1]; | ||
794 | string KickMessage = args[2]; | ||
795 | |||
796 | m_log.DebugFormat("[IRC-Connector-{0}] Event: IRCKick {1}:{2}", idn, m_server, m_ircChannel); | ||
797 | BroadcastSim(UserKicker, "/me kicks kicks {0} off {1} saying \"{2}\"", UserKicked, IrcChannel, KickMessage); | ||
798 | |||
799 | if (UserKicked == m_nick) | ||
800 | { | ||
801 | BroadcastSim(m_nick, "Hey, that was me!!!"); | ||
802 | } | ||
803 | |||
804 | } | ||
805 | |||
806 | public void eventIrcQuit(string prefix, string command, string parms) | ||
807 | { | ||
808 | string IrcUser = prefix.Split('!')[0]; | ||
809 | string QuitMessage = parms; | ||
810 | |||
811 | m_log.DebugFormat("[IRC-Connector-{0}] Event: IRCQuit {1}:{2}", idn, m_server, m_ircChannel); | ||
812 | BroadcastSim(IrcUser, "/me quits saying \"{0}\"", QuitMessage); | ||
813 | } | ||
814 | |||
815 | #endregion | ||
816 | |||
817 | #region Connector Watch Dog | ||
818 | |||
819 | // A single watch dog monitors extant connectors and makes sure that they | ||
820 | // are re-connected as necessary. If a connector IS connected, then it is | ||
821 | // pinged, but only if a PING period has elapsed. | ||
822 | |||
823 | protected static void WatchdogHandler(Object source, ElapsedEventArgs args) | ||
824 | { | ||
825 | |||
826 | // m_log.InfoFormat("[IRC-Watchdog] Status scan"); | ||
827 | |||
828 | _pdk_ = (_pdk_+1)%PING_PERIOD; // cycle the ping trigger | ||
829 | _icc_++; // increment the inter-consecutive-connect-delay counter | ||
830 | |||
831 | foreach (IRCConnector connector in m_connectors) | ||
832 | { | ||
833 | if (connector.Enabled) | ||
834 | { | ||
835 | if (!connector.Connected) | ||
836 | { | ||
837 | try | ||
838 | { | ||
839 | // m_log.DebugFormat("[IRC-Watchdog] Connecting {1}:{2}", connector.idn, connector.m_server, connector.m_ircChannel); | ||
840 | connector.Connect(); | ||
841 | } | ||
842 | catch (Exception e) | ||
843 | { | ||
844 | m_log.ErrorFormat("[IRC-Watchdog] Exception on connector {0}: {1} ", connector.idn, e.Message); | ||
845 | } | ||
846 | } | ||
847 | else | ||
848 | { | ||
849 | |||
850 | if (connector.m_pending) | ||
851 | { | ||
852 | if (connector.m_timeout == 0) | ||
853 | { | ||
854 | m_log.ErrorFormat("[IRC-Watchdog] Login timed-out for connector {0}, reconnecting", connector.idn); | ||
855 | connector.Reconnect(); | ||
856 | } | ||
857 | else | ||
858 | connector.m_timeout--; | ||
859 | } | ||
860 | |||
861 | if (_pdk_ == 0) | ||
862 | { | ||
863 | try | ||
864 | { | ||
865 | connector.m_writer.WriteLine(String.Format("PING :{0}", connector.m_server)); | ||
866 | connector.m_writer.Flush(); | ||
867 | } | ||
868 | catch (Exception /*e*/) | ||
869 | { | ||
870 | // m_log.ErrorFormat("[IRC-PingRun] Exception on connector {0}: {1} ", connector.idn, e.Message); | ||
871 | // m_log.Debug(e); | ||
872 | connector.Reconnect(); | ||
873 | } | ||
874 | } | ||
875 | |||
876 | } | ||
877 | } | ||
878 | } | ||
879 | |||
880 | // m_log.InfoFormat("[IRC-Watchdog] Status scan completed"); | ||
881 | |||
882 | } | ||
883 | |||
884 | #endregion | ||
885 | |||
886 | } | ||
887 | } | ||