Index: trunk/tools/wmib/Program.cs |
— | — | @@ -33,6 +33,63 @@ |
34 | 34 | } |
35 | 35 | } |
36 | 36 | |
| 37 | + public class HtmlDump |
| 38 | + { |
| 39 | + public config.channel Channel; |
| 40 | + public string dumpname; |
| 41 | + public static void Start() |
| 42 | + { |
| 43 | + while (true) |
| 44 | + { |
| 45 | + foreach (config.channel chan in config.channels) |
| 46 | + { |
| 47 | + HtmlDump dump = new HtmlDump(chan); |
| 48 | + dump.Make(); |
| 49 | + } |
| 50 | + System.Threading.Thread.Sleep(3600000); |
| 51 | + } |
| 52 | + } |
| 53 | + public HtmlDump(config.channel channel) |
| 54 | + { |
| 55 | + dumpname = config.DumpDir + "/" + channel.name + ".htm"; |
| 56 | + Channel = channel; |
| 57 | + } |
| 58 | + public string CreateFooter() |
| 59 | + { |
| 60 | + return "</body></html>\n"; |
| 61 | + } |
| 62 | + public string CreateHeader() |
| 63 | + { |
| 64 | + return "<html><head></head><body>\n"; |
| 65 | + } |
| 66 | + public string Encode(string text) |
| 67 | + { |
| 68 | + text = text.Replace("<", "<"); |
| 69 | + text = text.Replace(">", ">"); |
| 70 | + return text; |
| 71 | + } |
| 72 | + public string AddLine(string name, string value) |
| 73 | + { |
| 74 | + return "<tr><td>" + Encode(name) + "</td><td>" + Encode(value) + "</td></tr>\n"; |
| 75 | + } |
| 76 | + public void Make() |
| 77 | + { |
| 78 | + string text; |
| 79 | + text = CreateHeader(); |
| 80 | + text = text + "<table border=1 width=100%>\n<tr><td width=10%>Key</td><td>Value</td></tr>\n"; |
| 81 | + if (Channel.Keys.text.Count > 0) |
| 82 | + { |
| 83 | + foreach (irc.dictionary.item Key in Channel.Keys.text) |
| 84 | + { |
| 85 | + text = text + AddLine(Key.key, Key.text); |
| 86 | + } |
| 87 | + } |
| 88 | + text = text + "<table>\n"; |
| 89 | + text = text + CreateFooter(); |
| 90 | + System.IO.File.WriteAllText(dumpname, text); |
| 91 | + } |
| 92 | + } |
| 93 | + |
37 | 94 | public static class config |
38 | 95 | { |
39 | 96 | public static string text; |
— | — | @@ -44,7 +101,9 @@ |
45 | 102 | { |
46 | 103 | text =""; |
47 | 104 | AddConfig("username", username); |
| 105 | + AddConfig("password", password); |
48 | 106 | AddConfig("network", network); |
| 107 | + AddConfig("nick", login); |
49 | 108 | text = text + "\nchannels="; |
50 | 109 | foreach (channel current in channels) |
51 | 110 | { |
— | — | @@ -58,15 +117,59 @@ |
59 | 118 | public string name; |
60 | 119 | public bool logged; |
61 | 120 | public string log; |
62 | | - public irc.dictionary Keys = new irc.dictionary(); |
63 | | - public irc.trust Users; |
| 121 | + public irc.dictionary Keys; |
| 122 | + private string conf; |
| 123 | + public irc.IRCTrust Users; |
| 124 | + public string keydb = ""; |
| 125 | + private void AddConfig(string a, string b) |
| 126 | + { |
| 127 | + conf = conf + "\n" + a + "=" + b + ";"; |
| 128 | + } |
| 129 | + public void LoadConfig() |
| 130 | + { |
| 131 | + string conf_file = name + ".setting"; |
| 132 | + if (!System.IO.File.Exists(conf_file)) |
| 133 | + { |
| 134 | + System.IO.File.WriteAllText(conf_file, ""); |
| 135 | + Program.Log("Creating datafile for channel " + name); |
| 136 | + return; |
| 137 | + } |
| 138 | + conf = System.IO.File.ReadAllText(conf_file); |
| 139 | + if (config.parseConfig(conf, "keysdb") != "") |
| 140 | + { |
| 141 | + keydb = (config.parseConfig(conf, "keysdb")); |
| 142 | + } |
| 143 | + if (config.parseConfig(conf, "logged") != "") |
| 144 | + { |
| 145 | + logged = bool.Parse(config.parseConfig(conf, "logged")); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public void SaveConfig() |
| 150 | + { |
| 151 | + conf = ""; |
| 152 | + AddConfig("keysdb", keydb); |
| 153 | + AddConfig("logged", logged.ToString()); |
| 154 | + System.IO.File.WriteAllText(name + ".setting", conf); |
| 155 | + } |
64 | 156 | public channel(string Name) |
65 | 157 | { |
| 158 | + conf = ""; |
| 159 | + keydb = Name + ".db"; |
66 | 160 | logged = true; |
67 | | - name = Name; |
68 | | - log = Name + ".txt"; |
69 | | - Keys.Load(name); |
70 | | - Users = new irc.trust(name); |
| 161 | + name = Name; |
| 162 | + LoadConfig(); |
| 163 | + if (!System.IO.Directory.Exists("log")) |
| 164 | + { |
| 165 | + System.IO.Directory.CreateDirectory("log"); |
| 166 | + } |
| 167 | + if (!System.IO.Directory.Exists("log/" + Name)) |
| 168 | + { |
| 169 | + System.IO.Directory.CreateDirectory("log/" + Name); |
| 170 | + } |
| 171 | + Keys = new irc.dictionary(keydb, name); |
| 172 | + log = "log/" + Name + "/" + System.DateTime.Now.Year + System.DateTime.Now.Month + System.DateTime.Now.Day +".txt"; |
| 173 | + Users = new irc.IRCTrust(name); |
71 | 174 | } |
72 | 175 | } |
73 | 176 | |
— | — | @@ -87,21 +190,36 @@ |
88 | 191 | text = System.IO.File.ReadAllText("wmib"); |
89 | 192 | foreach (string x in parseConfig(text, "channels").Replace("\n", "").Split(',')) |
90 | 193 | { |
91 | | - channels.Add(new channel(x)); |
| 194 | + string name=x.Replace(" ", ""); |
| 195 | + if (!(name == "")) |
| 196 | + { |
| 197 | + channels.Add(new channel(name)); |
| 198 | + } |
92 | 199 | } |
93 | 200 | username = parseConfig(text, "username"); |
94 | 201 | network = parseConfig(text, "network"); |
95 | | - |
| 202 | + login = parseConfig(text, "nick"); |
| 203 | + password = parseConfig(text, "password"); |
| 204 | + if (!System.IO.Directory.Exists(config.DumpDir)) |
| 205 | + { |
| 206 | + System.IO.Directory.CreateDirectory(config.DumpDir); |
| 207 | + } |
96 | 208 | } |
97 | 209 | /// <summary> |
98 | 210 | /// Network |
99 | 211 | /// </summary> |
100 | 212 | public static string network = "irc.freenode.net"; |
| 213 | + /// <summary> |
| 214 | + /// Nick name |
| 215 | + /// </summary> |
101 | 216 | public static string username = "wm-bot"; |
| 217 | + public static string login = ""; |
| 218 | + public static string password = ""; |
| 219 | + public static string DumpDir = "dump"; |
102 | 220 | /// <summary> |
103 | | - /// |
| 221 | + /// Version |
104 | 222 | /// </summary> |
105 | | - public static string version = "wikimedia bot v. 1.0.1"; |
| 223 | + public static string version = "wikimedia bot v. 1.1.4"; |
106 | 224 | public static string separator = "|"; |
107 | 225 | /// <summary> |
108 | 226 | /// User name |
— | — | @@ -116,17 +234,18 @@ |
117 | 235 | public static class irc |
118 | 236 | { |
119 | 237 | private static System.Net.Sockets.NetworkStream data; |
| 238 | + public static System.Threading.Thread dumphtmt; |
| 239 | + public static System.Threading.Thread check_thread; |
120 | 240 | public static System.IO.StreamReader rd; |
121 | 241 | private static System.IO.StreamWriter wd; |
122 | 242 | private static List<user> User = new List<user>(); |
123 | | - public static System.Threading.Thread check_thread; |
124 | 243 | |
125 | 244 | public static void Ping() |
126 | 245 | { |
127 | 246 | while (true) |
128 | 247 | { |
129 | 248 | System.Threading.Thread.Sleep(20000); |
130 | | - wd.WriteLine("PING: " + config.network); |
| 249 | + wd.WriteLine("PING :" + config.network); |
131 | 250 | wd.Flush(); |
132 | 251 | } |
133 | 252 | } |
— | — | @@ -136,6 +255,17 @@ |
137 | 256 | return text.Replace(config.separator, "<separator>"); |
138 | 257 | } |
139 | 258 | |
| 259 | + public static bool Authenticate() |
| 260 | + { |
| 261 | + if (config.login != "") |
| 262 | + { |
| 263 | + wd.WriteLine("PRIVMSG nickserv :identify " + config.login + " " + config.password); |
| 264 | + wd.Flush(); |
| 265 | + System.Threading.Thread.Sleep(4000); |
| 266 | + } |
| 267 | + return true; |
| 268 | + } |
| 269 | + |
140 | 270 | public static string decode(string text) |
141 | 271 | { |
142 | 272 | return text.Replace("<separator>", config.separator); |
— | — | @@ -152,12 +282,12 @@ |
153 | 283 | public string level; |
154 | 284 | } |
155 | 285 | |
156 | | - public class trust |
| 286 | + public class IRCTrust |
157 | 287 | { |
158 | 288 | private List<user> Users = new List<user>(); |
159 | 289 | public string _Channel; |
160 | 290 | public string File; |
161 | | - public trust(string channel) |
| 291 | + public IRCTrust(string channel) |
162 | 292 | { |
163 | 293 | // Load |
164 | 294 | File = channel + "_user"; |
— | — | @@ -168,7 +298,7 @@ |
169 | 299 | System.IO.File.WriteAllText(File, ""); |
170 | 300 | } |
171 | 301 | string[] db = System.IO.File.ReadAllLines(channel + "_user"); |
172 | | - this._Channel = channel; |
| 302 | + _Channel = channel; |
173 | 303 | foreach (string x in db) |
174 | 304 | { |
175 | 305 | if (x.Contains(config.separator)) |
— | — | @@ -288,49 +418,26 @@ |
289 | 419 | { |
290 | 420 | return false; |
291 | 421 | } |
292 | | - |
293 | | - if (command == "alias_key") |
| 422 | + switch (command) |
294 | 423 | { |
| 424 | + case "alias_key": |
| 425 | + case "delete_key": |
| 426 | + case "trust": |
| 427 | + case "info": |
| 428 | + case "trustadd": |
| 429 | + case "trustdel": |
295 | 430 | return matchLevel(1, current.level); |
| 431 | + case "admin": |
| 432 | + case "shutdown": |
| 433 | + return matchLevel(2, current.level); |
296 | 434 | } |
297 | | - if (command == "new_key") |
298 | | - { |
299 | | - return matchLevel(1, current.level); |
300 | | - } |
301 | | - if (command == "shutdown") |
302 | | - { |
303 | | - return matchLevel(1, current.level); |
304 | | - } |
305 | | - if (command == "delete_key") |
306 | | - { |
307 | | - return matchLevel(1, current.level); |
308 | | - } |
309 | | - if (command == "trust") |
310 | | - { |
311 | | - return matchLevel(1, current.level); |
312 | | - } |
313 | | - if (command == "admin") |
314 | | - { |
315 | | - return matchLevel(2, current.level); |
316 | | - } |
317 | | - if (command == "info") |
318 | | - { |
319 | | - return matchLevel(1, current.level); |
320 | | - } |
321 | | - if (command == "trustadd") |
322 | | - { |
323 | | - return matchLevel(1, current.level); |
324 | | - } |
325 | | - if (command == "trustdel") |
326 | | - { |
327 | | - return matchLevel(1, current.level); |
328 | | - } |
329 | 435 | return false; |
330 | 436 | } |
331 | 437 | } |
332 | 438 | |
333 | 439 | public class dictionary |
334 | 440 | { |
| 441 | + public string datafile = ""; |
335 | 442 | public class item |
336 | 443 | { |
337 | 444 | public item(string Key, string Text, string User, string Lock = "false") |
— | — | @@ -358,18 +465,16 @@ |
359 | 466 | public List<item> text = new List<item>(); |
360 | 467 | public List<staticalias> Alias = new List<staticalias>(); |
361 | 468 | public string Channel; |
362 | | - public void Load(string channel) |
| 469 | + public void Load() |
363 | 470 | { |
364 | | - Channel = channel; |
365 | | - string file = Channel + ".db"; |
366 | 471 | text.Clear(); |
367 | | - if (!System.IO.File.Exists(file)) |
| 472 | + if (!System.IO.File.Exists(datafile)) |
368 | 473 | { |
369 | 474 | // Create db |
370 | | - System.IO.File.WriteAllText(file, ""); |
| 475 | + System.IO.File.WriteAllText(datafile, ""); |
371 | 476 | } |
372 | 477 | |
373 | | - string[] db = System.IO.File.ReadAllLines(file); |
| 478 | + string[] db = System.IO.File.ReadAllLines(datafile); |
374 | 479 | foreach (string x in db) |
375 | 480 | { |
376 | 481 | if (x.Contains(config.separator)) |
— | — | @@ -390,19 +495,25 @@ |
391 | 496 | } |
392 | 497 | } |
393 | 498 | |
| 499 | + public dictionary(string database, string channel) |
| 500 | + { |
| 501 | + datafile = database; |
| 502 | + Channel = channel; |
| 503 | + Load(); |
| 504 | + } |
| 505 | + |
394 | 506 | public void Save() |
395 | 507 | { |
396 | 508 | try |
397 | 509 | { |
398 | | - string file = Channel + ".db"; |
399 | | - System.IO.File.WriteAllText(file, ""); |
| 510 | + System.IO.File.WriteAllText(datafile, ""); |
400 | 511 | foreach (staticalias key in Alias) |
401 | 512 | { |
402 | | - System.IO.File.AppendAllText(file, key.Name + config.separator + key.Key + config.separator + "alias" + "\n"); |
| 513 | + System.IO.File.AppendAllText(datafile, key.Name + config.separator + key.Key + config.separator + "alias" + "\n"); |
403 | 514 | } |
404 | 515 | foreach (item key in text) |
405 | 516 | { |
406 | | - System.IO.File.AppendAllText(file, key.key + config.separator + key.text + config.separator + "key" + config.separator + key.locked + config.separator + key.user + "\n"); |
| 517 | + System.IO.File.AppendAllText(datafile, key.key + config.separator + key.text + config.separator + "key" + config.separator + key.locked + config.separator + key.user + "\n"); |
407 | 518 | } |
408 | 519 | } |
409 | 520 | catch (Exception b) |
— | — | @@ -438,7 +549,13 @@ |
439 | 550 | config.channel _Chan = getChannel(Channel); |
440 | 551 | if (chan.Users.isApproved(user, host, "info")) |
441 | 552 | { |
442 | | - setKey(name.Substring(name.IndexOf("is") + 3), parm[0], ""); |
| 553 | + if (parm.Length < 3) |
| 554 | + { |
| 555 | + Message("It would be cool to give me also a text of key", Channel); |
| 556 | + return true; |
| 557 | + } |
| 558 | + string key = name.Substring(name.IndexOf(" is") + 4); |
| 559 | + setKey(key, parm[0], ""); |
443 | 560 | } |
444 | 561 | else |
445 | 562 | { |
— | — | @@ -459,6 +576,28 @@ |
460 | 577 | } |
461 | 578 | return false; |
462 | 579 | } |
| 580 | + if (parm[1] == "unalias") |
| 581 | + { |
| 582 | + config.channel _Chan = getChannel(Channel); |
| 583 | + if (chan.Users.isApproved(user, host, "info")) |
| 584 | + { |
| 585 | + foreach (staticalias b in Alias) |
| 586 | + { |
| 587 | + if (b.Name == parm[0]) |
| 588 | + { |
| 589 | + Alias.Remove(b); |
| 590 | + Message("Alias removed", Channel); |
| 591 | + Save(); |
| 592 | + return false; |
| 593 | + } |
| 594 | + } |
| 595 | + } |
| 596 | + else |
| 597 | + { |
| 598 | + Message("You are not autorized to perform this, sorry", Channel); |
| 599 | + } |
| 600 | + return false; |
| 601 | + } |
463 | 602 | if (parm[1] == "del") |
464 | 603 | { |
465 | 604 | if (chan.Users.isApproved(user, host, "info")) |
— | — | @@ -501,11 +640,12 @@ |
502 | 641 | } |
503 | 642 | foreach (staticalias b in Alias) |
504 | 643 | { |
505 | | - if (b.Name == name) |
| 644 | + if (b.Name == p[0]) |
506 | 645 | { |
507 | 646 | keyv = getValue(b.Key); |
508 | 647 | if (keyv != "") |
509 | 648 | { |
| 649 | + keyv = keyv.Replace("$1", p1); |
510 | 650 | if (User == "") |
511 | 651 | { |
512 | 652 | Message(keyv, Channel); |
— | — | @@ -681,7 +821,7 @@ |
682 | 822 | { |
683 | 823 | if (!channel.Users.isApproved(user, host, "admin")) |
684 | 824 | { |
685 | | - Message("Permission denied!", channel.name); |
| 825 | + Message("Permission denied", channel.name); |
686 | 826 | return 2; |
687 | 827 | } |
688 | 828 | } |
— | — | @@ -702,15 +842,21 @@ |
703 | 843 | if (message.StartsWith("@trustdel")) |
704 | 844 | { |
705 | 845 | string[] rights_info = message.Split(' '); |
706 | | - string x = rights_info[1]; |
707 | | - if (channel.Users.isApproved(user, host, "trustdel")) |
| 846 | + if (rights_info.Length > 1) |
708 | 847 | { |
709 | | - channel.Users.delUser(rights_info[1]); |
| 848 | + string x = rights_info[1]; |
| 849 | + if (channel.Users.isApproved(user, host, "trustdel")) |
| 850 | + { |
| 851 | + channel.Users.delUser(rights_info[1]); |
| 852 | + return 0; |
| 853 | + } |
| 854 | + else |
| 855 | + { |
| 856 | + Message("You are not autorized to perform this, sorry", channel.name); |
| 857 | + return 0; |
| 858 | + } |
710 | 859 | } |
711 | | - else |
712 | | - { |
713 | | - Message("You are not autorized to perform this, sorry", channel.name); |
714 | | - } |
| 860 | + Message("Invalid user", channel.name); |
715 | 861 | } |
716 | 862 | } |
717 | 863 | catch (Exception b) |
— | — | @@ -722,19 +868,27 @@ |
723 | 869 | |
724 | 870 | public static void chanLog(string message, config.channel channel, string user, string host, bool noac = true) |
725 | 871 | { |
726 | | - if (channel.logged) |
| 872 | + try |
727 | 873 | { |
728 | | - string log; |
729 | | - if (!noac) |
| 874 | + if (channel.logged) |
730 | 875 | { |
731 | | - log = "\n" + "[" + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + System.DateTime.Now.Second + "] * " + user + " " + message; |
| 876 | + string log; |
| 877 | + if (!noac) |
| 878 | + { |
| 879 | + log = "[" + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + System.DateTime.Now.Second + "] * " + user + " " + message + "\n"; |
| 880 | + } |
| 881 | + else |
| 882 | + { |
| 883 | + log = "[" + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + System.DateTime.Now.Second + "] " + "<" + user + "> " + message + "\n"; |
| 884 | + } |
| 885 | + System.IO.File.AppendAllText(channel.log, log); |
732 | 886 | } |
733 | | - else |
734 | | - { |
735 | | - log = "\n" + "[" + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + System.DateTime.Now.Second + "] " + "<" + user + "> " + message; |
736 | | - } |
737 | | - System.IO.File.AppendAllText(channel.log, log); |
738 | 887 | } |
| 888 | + catch (Exception er) |
| 889 | + { |
| 890 | + // nothing |
| 891 | + Console.WriteLine(er.Message); |
| 892 | + } |
739 | 893 | } |
740 | 894 | |
741 | 895 | public static bool getAction(string message, string Channel, string host, string nick) |
— | — | @@ -758,6 +912,13 @@ |
759 | 913 | Message("Invalid name", chan.name); |
760 | 914 | return; |
761 | 915 | } |
| 916 | + foreach (config.channel cu in config.channels) |
| 917 | + { |
| 918 | + if (channel == cu.name) |
| 919 | + { |
| 920 | + return; |
| 921 | + } |
| 922 | + } |
762 | 923 | config.channels.Add(new config.channel(channel)); |
763 | 924 | config.Save(); |
764 | 925 | wd.WriteLine("JOIN " + channel); |
— | — | @@ -796,6 +957,19 @@ |
797 | 958 | |
798 | 959 | public static void admin(config.channel chan, string user, string host, string message) |
799 | 960 | { |
| 961 | + if (message.StartsWith("@reload")) |
| 962 | + { |
| 963 | + if (chan.Users.isApproved(user, host, "admin")) |
| 964 | + { |
| 965 | + chan.LoadConfig(); |
| 966 | + chan.Keys = new dictionary(chan.keydb, chan.name); |
| 967 | + Message("Channel config was reloaded", chan.name); |
| 968 | + } |
| 969 | + else |
| 970 | + { |
| 971 | + Message("Permission denied", chan.name); |
| 972 | + } |
| 973 | + } |
800 | 974 | if (message.StartsWith("@logon")) |
801 | 975 | { |
802 | 976 | if (chan.Users.isApproved(user, host, "admin")) |
— | — | @@ -808,6 +982,7 @@ |
809 | 983 | { |
810 | 984 | Message("Channel is now logged", chan.name); |
811 | 985 | chan.logged = true; |
| 986 | + chan.SaveConfig(); |
812 | 987 | config.Save(); |
813 | 988 | } |
814 | 989 | } |
— | — | @@ -822,12 +997,13 @@ |
823 | 998 | { |
824 | 999 | if (!chan.logged) |
825 | 1000 | { |
826 | | - Message("Channel is already not logged", chan.name); |
| 1001 | + Message("Channel was already not logged", chan.name); |
827 | 1002 | } |
828 | 1003 | else |
829 | 1004 | { |
830 | 1005 | chan.logged = false; |
831 | 1006 | config.Save(); |
| 1007 | + chan.SaveConfig(); |
832 | 1008 | Message("Channel is not logged", chan.name); |
833 | 1009 | } |
834 | 1010 | } |
— | — | @@ -836,6 +1012,15 @@ |
837 | 1013 | Message("Permission denied", chan.name); |
838 | 1014 | } |
839 | 1015 | } |
| 1016 | + if (message.StartsWith("@channellist")) |
| 1017 | + { |
| 1018 | + string channels = ""; |
| 1019 | + foreach (config.channel a in config.channels) |
| 1020 | + { |
| 1021 | + channels = channels + a.name + ", "; |
| 1022 | + } |
| 1023 | + Message("I am now in following channels: " + channels, chan.name); |
| 1024 | + } |
840 | 1025 | } |
841 | 1026 | |
842 | 1027 | public static bool getMessage(string channel, string nick, string host, string message) |
— | — | @@ -872,8 +1057,10 @@ |
873 | 1058 | wd = new System.IO.StreamWriter(data); |
874 | 1059 | wd.WriteLine("USER " + config.name + " 8 * :" + config.name); |
875 | 1060 | wd.WriteLine("NICK " + config.username); |
| 1061 | + Authenticate(); |
876 | 1062 | foreach (config.channel ch in config.channels) |
877 | 1063 | { |
| 1064 | + System.Threading.Thread.Sleep(2000); |
878 | 1065 | wd.WriteLine("JOIN " + ch.name); |
879 | 1066 | } |
880 | 1067 | wd.Flush(); |
— | — | @@ -885,6 +1072,9 @@ |
886 | 1073 | data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream(); |
887 | 1074 | rd = new System.IO.StreamReader(data, System.Text.Encoding.UTF8); |
888 | 1075 | wd = new System.IO.StreamWriter(data); |
| 1076 | + |
| 1077 | + dumphtmt = new System.Threading.Thread(new System.Threading.ThreadStart(HtmlDump.Start)); |
| 1078 | + dumphtmt.Start(); |
889 | 1079 | check_thread = new System.Threading.Thread(new System.Threading.ThreadStart(Ping)); |
890 | 1080 | check_thread.Start(); |
891 | 1081 | |
— | — | @@ -893,9 +1083,15 @@ |
894 | 1084 | |
895 | 1085 | System.Threading.Thread.Sleep(2000); |
896 | 1086 | |
| 1087 | + Authenticate(); |
| 1088 | + |
897 | 1089 | foreach (config.channel ch in config.channels) |
898 | 1090 | { |
899 | | - wd.WriteLine("JOIN " + ch.name); |
| 1091 | + if (ch.name != "") |
| 1092 | + { |
| 1093 | + wd.WriteLine("JOIN " + ch.name); |
| 1094 | + System.Threading.Thread.Sleep(2000); |
| 1095 | + } |
900 | 1096 | } |
901 | 1097 | wd.Flush(); |
902 | 1098 | string text = ""; |
— | — | @@ -903,6 +1099,7 @@ |
904 | 1100 | string host = ""; |
905 | 1101 | string message = ""; |
906 | 1102 | string channel = ""; |
| 1103 | + char delimiter = (char)001; |
907 | 1104 | |
908 | 1105 | while (true) |
909 | 1106 | { |
— | — | @@ -913,50 +1110,88 @@ |
914 | 1111 | text = rd.ReadLine(); |
915 | 1112 | if (text.StartsWith(":")) |
916 | 1113 | { |
917 | | - if (text.Contains("PRIVMSG")) |
| 1114 | + string check = text.Substring(text.IndexOf(" ")); |
| 1115 | + if (check.StartsWith(" 005")) |
918 | 1116 | { |
919 | | - string info = text.Substring(1, text.IndexOf(":", 2)); |
920 | | - string info_host; |
921 | | - // we got a message here :) |
922 | | - if (text.Contains("!") && text.Contains("@")) |
923 | | - { |
924 | | - nick = info.Substring(0, info.IndexOf("!")); |
925 | | - host = info.Substring(info.IndexOf("@") + 1, info.IndexOf(" ", info.IndexOf("@")) - 1 - info.IndexOf("@")); |
926 | | - } |
927 | | - info_host = info.Substring(info.IndexOf("PRIVMSG ")); |
928 | 1117 | |
929 | | - if (info_host.Contains("#")) |
| 1118 | + } |
| 1119 | + else |
| 1120 | + { |
| 1121 | + if (text.Contains("PRIVMSG")) |
930 | 1122 | { |
931 | | - channel = info_host.Substring(info_host.IndexOf("#")); |
932 | | - channel = channel.Substring(0, channel.IndexOf(" ")); |
933 | | - message = text.Replace(info, ""); |
934 | | - message = message.Substring(message.IndexOf(":") + 1); |
935 | | - if (message.Contains("ACTION")) |
| 1123 | + string info = text.Substring(1, text.IndexOf(":", 2)); |
| 1124 | + string info_host; |
| 1125 | + // we got a message here :) |
| 1126 | + if (text.Contains("!") && text.Contains("@")) |
936 | 1127 | { |
937 | | - getAction(message.Replace("", "").Replace("ACTION ", ""), channel, host, nick); |
| 1128 | + nick = info.Substring(0, info.IndexOf("!")); |
| 1129 | + host = info.Substring(info.IndexOf("@") + 1, info.IndexOf(" ", info.IndexOf("@")) - 1 - info.IndexOf("@")); |
938 | 1130 | } |
| 1131 | + info_host = info.Substring(info.IndexOf("PRIVMSG ")); |
| 1132 | + |
| 1133 | + if (info_host.Contains("#")) |
| 1134 | + { |
| 1135 | + channel = info_host.Substring(info_host.IndexOf("#")); |
| 1136 | + channel = channel.Substring(0, channel.IndexOf(" ")); |
| 1137 | + message = text.Replace(info, ""); |
| 1138 | + message = message.Substring(message.IndexOf(":") + 1); |
| 1139 | + if (message.Contains(delimiter.ToString() + "ACTION")) |
| 1140 | + { |
| 1141 | + getAction(message.Replace("", "").Replace(delimiter.ToString() +"ACTION ", ""), channel, host, nick); |
| 1142 | + continue; |
| 1143 | + } |
| 1144 | + else |
| 1145 | + { |
| 1146 | + getMessage(channel, nick, host, message); |
| 1147 | + continue; |
| 1148 | + } |
| 1149 | + } |
939 | 1150 | else |
940 | 1151 | { |
941 | | - getMessage(channel, nick, host, message); |
| 1152 | + message = text.Substring(text.IndexOf("PRIVMSG")); |
| 1153 | + message = message.Substring(message.IndexOf(":")); |
| 1154 | + // private message |
| 1155 | + if (message.StartsWith(":" + delimiter.ToString() + "FINGER")) |
| 1156 | + { |
| 1157 | + wd.WriteLine("NOTICE " + nick + " :" + delimiter.ToString() + "FINGER" + " I am a bot don't finger me"); |
| 1158 | + wd.Flush(); |
| 1159 | + continue; |
| 1160 | + } |
| 1161 | + if (message.StartsWith(":" + delimiter.ToString() + "TIME")) |
| 1162 | + { |
| 1163 | + wd.WriteLine("NOTICE " + nick + " :" + delimiter.ToString() + "TIME " + System.DateTime.Now.ToString()); |
| 1164 | + wd.Flush(); |
| 1165 | + continue; |
| 1166 | + } |
| 1167 | + if (message.StartsWith(":" + delimiter.ToString() + "PING")) |
| 1168 | + { |
| 1169 | + wd.WriteLine("NOTICE " + nick + " :" + delimiter.ToString() + "PING " + message.Substring(message.IndexOf(delimiter.ToString() + "PING" + 6))); |
| 1170 | + wd.Flush(); |
| 1171 | + continue; |
| 1172 | + } |
| 1173 | + if (message.StartsWith(":" + delimiter.ToString() + "VERSION")) |
| 1174 | + { |
| 1175 | + wd.WriteLine("NOTICE " + nick + " :" + delimiter.ToString() + "VERSION " + config.version); |
| 1176 | + wd.Flush(); |
| 1177 | + continue; |
| 1178 | + } |
942 | 1179 | } |
943 | 1180 | } |
944 | | - else |
| 1181 | + if (text.Contains("PING ")) |
945 | 1182 | { |
946 | | - // private message |
| 1183 | + wd.WriteLine("PONG " + text.Substring(text.IndexOf("PING ") + 5)); |
| 1184 | + wd.Flush(); |
947 | 1185 | } |
948 | 1186 | } |
949 | | - if (text.Contains("PING ")) |
950 | | - { |
951 | | - wd.WriteLine("PONG " + text.Substring(text.IndexOf("PING ") + 5)); |
952 | | - wd.Flush(); |
953 | | - } |
954 | 1187 | } |
955 | 1188 | System.Threading.Thread.Sleep(50); |
956 | 1189 | } |
| 1190 | + Program.Log("Reconnecting, end of data stream"); |
957 | 1191 | Reconnect(); |
958 | 1192 | } |
959 | 1193 | catch (System.IO.IOException xx) |
960 | 1194 | { |
| 1195 | + Program.Log("Reconnecting, connection failed"); |
961 | 1196 | Reconnect(); |
962 | 1197 | } |
963 | 1198 | catch (Exception xx) |