Index: trunk/tools/wmib/Program.cs |
— | — | @@ -29,12 +29,31 @@ |
30 | 30 | private static void Main(string[] args) |
31 | 31 | { |
32 | 32 | Log("Connecting"); |
| 33 | + config.Load(); |
33 | 34 | irc.Connect(); |
34 | 35 | } |
35 | 36 | } |
36 | 37 | |
37 | 38 | public static class config |
38 | 39 | { |
| 40 | + public static string text; |
| 41 | + private static void AddConfig(string a, string b) |
| 42 | + { |
| 43 | + text = text + "\n" + a + "=" + b + ";"; |
| 44 | + } |
| 45 | + public static void Save() |
| 46 | + { |
| 47 | + text =""; |
| 48 | + AddConfig("username", username); |
| 49 | + AddConfig("network", network); |
| 50 | + text = text + "\nchannels="; |
| 51 | + foreach (channel current in channels) |
| 52 | + { |
| 53 | + text = text + current.name + ",\n"; |
| 54 | + } |
| 55 | + text = text + ";"; |
| 56 | + System.IO.File.WriteAllText("wmib", text); |
| 57 | + } |
39 | 58 | public class channel |
40 | 59 | { |
41 | 60 | public string name; |
— | — | @@ -82,6 +101,7 @@ |
83 | 102 | public static StreamReader rd; |
84 | 103 | private static StreamWriter wd; |
85 | 104 | //private static List<IrcUser> User = new List<IrcUser>(); |
| 105 | + public static System.Threading.Thread check_thread; |
86 | 106 | |
87 | 107 | public class IrcUser |
88 | 108 | { |
— | — | @@ -115,11 +135,11 @@ |
116 | 136 | _Channel = channel; |
117 | 137 | foreach (string x in db) |
118 | 138 | { |
119 | | - if (x.Contains("|")) |
| 139 | + if (x.Contains(config.separator)) |
120 | 140 | { |
121 | | - string[] info = x.Split('|'); |
| 141 | + string[] info = x.Split(Char.Parse(config.separator)); |
122 | 142 | string level = info[1]; |
123 | | - string name = info[0]; |
| 143 | + string name = decode(info[0]); |
124 | 144 | Users.Add(name, new IrcUser(level, name)); |
125 | 145 | } |
126 | 146 | } |
— | — | @@ -152,6 +172,9 @@ |
153 | 173 | { |
154 | 174 | Message("This user is admin which can't be deleted from db, sorry", _Channel); |
155 | 175 | return true; |
| 176 | + Save(); |
| 177 | + Message("User was deleted from access list", _Channel); |
| 178 | + return true; |
156 | 179 | } |
157 | 180 | Users.Remove(user); |
158 | 181 | Save(); |
— | — | @@ -179,7 +202,7 @@ |
180 | 203 | { |
181 | 204 | users_ok = users_ok + " " + b.Key; |
182 | 205 | } |
183 | | - Message("I trust to: " + users_ok, _Channel); |
| 206 | + Message("I trust: " + users_ok, _Channel); |
184 | 207 | } |
185 | 208 | |
186 | 209 | public bool matchLevel(int level, string rights) |
— | — | @@ -240,6 +263,7 @@ |
241 | 264 | } |
242 | 265 | |
243 | 266 | public List<item> text = new List<item>(); |
| 267 | + public List<staticalias> Alias = new List<staticalias>(); |
244 | 268 | public string Channel; |
245 | 269 | |
246 | 270 | public void Load(string channel) |
— | — | @@ -252,6 +276,25 @@ |
253 | 277 | File.WriteAllText(file, ""); |
254 | 278 | } |
255 | 279 | |
| 280 | + string[] db = System.IO.File.ReadAllLines(file); |
| 281 | + foreach (string x in db) |
| 282 | + { |
| 283 | + if (x.Contains(config.separator)) |
| 284 | + { |
| 285 | + string[] info = x.Split(Char.Parse(config.separator)); |
| 286 | + string type = info[2]; |
| 287 | + string value = info[1]; |
| 288 | + string name = info[0]; |
| 289 | + if (type == "key") |
| 290 | + { |
| 291 | + text.Add(new item(name, value, "")); |
| 292 | + } |
| 293 | + else |
| 294 | + { |
| 295 | + Alias.Add(new staticalias(name, value)); |
| 296 | + } |
| 297 | + } |
| 298 | + } |
256 | 299 | } |
257 | 300 | |
258 | 301 | public void Save() |
— | — | @@ -260,6 +303,10 @@ |
261 | 304 | { |
262 | 305 | string file = Channel + ".db"; |
263 | 306 | File.WriteAllText(file, ""); |
| 307 | + foreach (staticalias key in Alias) |
| 308 | + { |
| 309 | + System.IO.File.AppendAllText(file, key.Name + config.separator + key.Key + config.separator + "alias" + "\n"); |
| 310 | + } |
264 | 311 | foreach (item key in text) |
265 | 312 | { |
266 | 313 | File.AppendAllText(file, key.key + "|" + key.text + "|" + key.locked + "|" + key.user); |
— | — | @@ -271,24 +318,64 @@ |
272 | 319 | } |
273 | 320 | } |
274 | 321 | |
275 | | - public bool print(string name) |
| 322 | + public string getValue(string key) |
276 | 323 | { |
| 324 | + foreach (item data in text) |
| 325 | + { |
| 326 | + if (data.key == key) |
| 327 | + { |
| 328 | + return decode(data.text); |
| 329 | + } |
| 330 | + } |
| 331 | + return ""; |
| 332 | + } |
| 333 | + |
| 334 | + public bool print(string name, string user, config.channel chan, string host) |
| 335 | + { |
277 | 336 | if (!name.StartsWith("!")) |
278 | 337 | { |
279 | 338 | return true; |
280 | 339 | } |
281 | 340 | name = name.Substring(1); |
282 | | - if (name.Contains(" ") && name.Contains("|") == false) |
| 341 | + if (name.Contains(" ")) |
283 | 342 | { |
284 | 343 | string[] parm = name.Split(' '); |
285 | 344 | if (parm[1] == "is") |
286 | 345 | { |
287 | | - setKey(parm[2], parm[0], ""); |
| 346 | + config.channel _Chan = getChannel(Channel); |
| 347 | + if (chan.Users.isApproved(user, host, "info")) |
| 348 | + { |
| 349 | + setKey(name.Substring(name.IndexOf("is") + 3), parm[0], ""); |
| 350 | + } |
| 351 | + else |
| 352 | + { |
| 353 | + Message("You are not autorized to perform this, sorry", Channel); |
| 354 | + } |
288 | 355 | return false; |
289 | 356 | } |
| 357 | + if (parm[1] == "alias") |
| 358 | + { |
| 359 | + config.channel _Chan = getChannel(Channel); |
| 360 | + if (chan.Users.isApproved(user, host, "info")) |
| 361 | + { |
| 362 | + this.aliasKey(name.Substring(name.IndexOf("alias") + 6), parm[0], ""); |
| 363 | + } |
| 364 | + else |
| 365 | + { |
| 366 | + Message("You are not autorized to perform this, sorry", Channel); |
| 367 | + } |
| 368 | + return false; |
| 369 | + } |
290 | 370 | if (parm[1] == "del") |
291 | 371 | { |
292 | | - rmKey(parm[0], ""); |
| 372 | + if (chan.Users.isApproved(user, host, "info")) |
| 373 | + { |
| 374 | + rmKey(parm[0], ""); |
| 375 | + } |
| 376 | + else |
| 377 | + { |
| 378 | + Message("You are not autorized to perform this, sorry", Channel); |
| 379 | + } |
293 | 380 | return false; |
294 | 381 | } |
295 | 382 | } |
— | — | @@ -299,43 +386,121 @@ |
300 | 387 | User = User.Replace("|", ""); |
301 | 388 | User = User.Replace(" ", ""); |
302 | 389 | name = name.Substring(0, name.IndexOf("|")); |
303 | | - name = name.Replace(" ", ""); |
304 | 390 | } |
305 | 391 | foreach (item data in text) |
306 | 392 | { |
307 | 393 | |
308 | 394 | if (data.key == name) |
309 | 395 | { |
| 396 | + keyv = keyv.Replace("$1", p1); |
310 | 397 | if (User == "") |
311 | 398 | { |
312 | | - Message(name + " is: " + data.text, Channel); |
| 399 | + Message(keyv, Channel); |
313 | 400 | } |
314 | 401 | else |
315 | 402 | { |
316 | | - Message(User + ":" + data.text, Channel); |
| 403 | + Message(User + ": " + keyv, Channel); |
317 | 404 | } |
318 | 405 | return true; |
319 | 406 | } |
| 407 | + foreach (staticalias b in Alias) |
| 408 | + { |
| 409 | + if (b.Name == name) |
| 410 | + { |
| 411 | + keyv = getValue(b.Key); |
| 412 | + if (keyv != "") |
| 413 | + { |
| 414 | + if (User == "") |
| 415 | + { |
| 416 | + Message(keyv, Channel); |
| 417 | + } |
| 418 | + else |
| 419 | + { |
| 420 | + Message(User + ":" + keyv, Channel); |
| 421 | + } |
| 422 | + return true; |
| 423 | + } |
| 424 | + } |
320 | 425 | } |
321 | 426 | return true; |
322 | 427 | } |
323 | 428 | |
| 429 | + public void RSearch(string key, config.channel Chan) |
| 430 | + { |
| 431 | + if (!key.StartsWith("@regsearch")) |
| 432 | + { |
| 433 | + return; |
| 434 | + } |
| 435 | + if (key.Length < 10) |
| 436 | + { |
| 437 | + Message("Could you please tell me what I should search for :P", Chan.name); |
| 438 | + return; |
| 439 | + } |
| 440 | + key = key.Substring(10); |
| 441 | + System.Text.RegularExpressions.Regex value = new System.Text.RegularExpressions.Regex(key); |
| 442 | + string results = ""; |
| 443 | + foreach (item data in text) |
| 444 | + { |
| 445 | + |
| 446 | + if (data.key == key || value.Match(data.text).Success) |
| 447 | + { |
| 448 | + results = results + data.key + ", "; |
| 449 | + } |
| 450 | + } |
| 451 | + if (results == "") |
| 452 | + { |
| 453 | + Message("No results found! :|", Chan.name); |
| 454 | + } |
| 455 | + else |
| 456 | + { |
| 457 | + Message("Results: " + results, Chan.name); |
| 458 | + } |
| 459 | + } |
| 460 | + |
| 461 | + public void Find(string key, config.channel Chan) |
| 462 | + { |
| 463 | + if (!key.StartsWith("@search")) |
| 464 | + { |
| 465 | + return; |
| 466 | + } |
| 467 | + if (key.Length < 9) |
| 468 | + { |
| 469 | + Message("Could you please tell me what I should search for :P", Chan.name); |
| 470 | + return; |
| 471 | + } |
| 472 | + key = key.Substring(8); |
| 473 | + string results = ""; |
| 474 | + foreach (item data in text) |
| 475 | + { |
| 476 | + |
| 477 | + if (data.key == key || data.text.Contains(key)) |
| 478 | + { |
| 479 | + results = results + data.key + ", "; |
| 480 | + } |
| 481 | + } |
| 482 | + if (results == "") |
| 483 | + { |
| 484 | + Message("No results found! :|", Chan.name); |
| 485 | + } |
| 486 | + else |
| 487 | + { |
| 488 | + Message("Results: " + results, Chan.name); |
| 489 | + } |
| 490 | + } |
| 491 | + |
324 | 492 | public void setKey(string Text, string key, string user) |
325 | 493 | { |
326 | 494 | try |
327 | 495 | { |
328 | | - if (!Text.Contains("|")) |
329 | | - { |
330 | 496 | foreach (item data in text) |
331 | 497 | { |
332 | | - |
333 | 498 | if (data.key == key) |
334 | 499 | { |
335 | 500 | Message("Key exist!", Channel); |
336 | 501 | return; |
337 | 502 | } |
338 | 503 | } |
339 | | - text.Add(new item(key, Text, user, "false")); |
| 504 | + text.Add(new item(key, encode(Text), user, "false")); |
340 | 505 | Message("Key was added!", Channel); |
341 | 506 | } |
342 | 507 | else |
— | — | @@ -354,6 +519,16 @@ |
355 | 520 | |
356 | 521 | public void aliasKey(string key, string alias, string user) |
357 | 522 | { |
| 523 | + foreach(staticalias stakey in this.Alias) |
| 524 | + { |
| 525 | + if (stakey.Name == al) |
| 526 | + { |
| 527 | + Message("Alias is already existing!", Channel); |
| 528 | + return; |
| 529 | + } |
| 530 | + } |
| 531 | + this.Alias.Add(new staticalias(al, key)); |
| 532 | + Message("Successfully created", Channel); |
358 | 533 | Save(); |
359 | 534 | } |
360 | 535 | |
— | — | @@ -406,6 +581,11 @@ |
407 | 582 | string[] rights_info = message.Split(' '); |
408 | 583 | if (channel.Users.isApproved(user, host, "trustadd")) |
409 | 584 | { |
| 585 | + if (rights_info.Length < 3) |
| 586 | + { |
| 587 | + Message("Wrong number of parameters, go fix it - example @trustadd regex (admin|trusted)", channel.name); |
| 588 | + return 0; |
| 589 | + } |
410 | 590 | if (!(rights_info[2] == "admin" || rights_info[2] == "trusted")) |
411 | 591 | { |
412 | 592 | Message("Unknown user level!", channel.name); |
— | — | @@ -454,7 +634,7 @@ |
455 | 635 | return 0; |
456 | 636 | } |
457 | 637 | |
458 | | - public static void chanLog(string message, config.channel channel, string user, string host) |
| 638 | + public static void chanLog(string message, config.channel channel, string user, string host, bool noac = true) |
459 | 639 | { |
460 | 640 | if (!channel.logged) return; |
461 | 641 | string log = "\n" + "[" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + |
— | — | @@ -462,6 +642,63 @@ |
463 | 643 | File.AppendAllText(channel.log, log); |
464 | 644 | } |
465 | 645 | |
| 646 | + public static bool getAction(string message, string Channel, string host, string nick) |
| 647 | + { |
| 648 | + config.channel curr = getChannel(Channel); |
| 649 | + chanLog(message, curr, nick, host, false); |
| 650 | + return false; |
| 651 | + } |
| 652 | + |
| 653 | + public static void addChannel(config.channel chan, string user, string host, string message) |
| 654 | + { |
| 655 | + if (message.StartsWith("@add")) |
| 656 | + { |
| 657 | + if (chan.Users.isApproved(user, host, "admin")) |
| 658 | + { |
| 659 | + if (message.Contains(" ")) |
| 660 | + { |
| 661 | + string channel=message.Substring(message.IndexOf(" ") + 1); |
| 662 | + if (channel.Contains(" ") || (channel.Contains("#") == false)) |
| 663 | + { |
| 664 | + Message("Invalid name", chan.name); |
| 665 | + return; |
| 666 | + } |
| 667 | + config.channels.Add(new config.channel(channel)); |
| 668 | + config.Save(); |
| 669 | + wd.WriteLine("JOIN " + channel); |
| 670 | + wd.Flush(); |
| 671 | + System.Threading.Thread.Sleep(100); |
| 672 | + config.channel Chan = getChannel(channel); |
| 673 | + Chan.Users.addUser("admin", user + ".*" + host ); |
| 674 | + } else |
| 675 | + { |
| 676 | + Message("Invalid name", chan.name); |
| 677 | + } |
| 678 | + } else |
| 679 | + { |
| 680 | + Message("Permission denied", chan.name); |
| 681 | + } |
| 682 | + } |
| 683 | + } |
| 684 | + |
| 685 | + public static void partChannel(config.channel chan, string user, string host, string message) |
| 686 | + { |
| 687 | + if (message.StartsWith("@part")) |
| 688 | + { |
| 689 | + if (chan.Users.isApproved(user, host, "admin")) |
| 690 | + { |
| 691 | + wd.WriteLine("PART " + chan.name); |
| 692 | + System.Threading.Thread.Sleep(100); |
| 693 | + wd.Flush(); |
| 694 | + config.channels.Remove(chan); |
| 695 | + config.Save(); |
| 696 | + } else |
| 697 | + { |
| 698 | + Message("Permission denied", chan.name); |
| 699 | + } |
| 700 | + } |
| 701 | + } |
| 702 | + |
466 | 703 | public static bool getMessage(string channel, string nick, string host, string message) |
467 | 704 | { |
468 | 705 | config.channel curr = getChannel(channel); |
— | — | @@ -470,21 +707,41 @@ |
471 | 708 | curr.Keys.print(message); |
472 | 709 | chanLog(message, curr, nick, host); |
473 | 710 | modifyRights(message, curr, nick, host); |
| 711 | + addChannel(curr, nick, host, message); |
| 712 | + partChannel(curr, nick, host, message); |
| 713 | + } |
474 | 714 | } |
475 | 715 | |
476 | 716 | return false; |
477 | 717 | } |
478 | 718 | |
| 719 | + public static bool Reconnect() |
| 720 | + { |
| 721 | + data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream(); |
| 722 | + rd = new System.IO.StreamReader(data, System.Text.Encoding.UTF8); |
| 723 | + wd = new System.IO.StreamWriter(data); |
| 724 | + wd.WriteLine("USER " + config.name + " 8 * :" + config.name); |
| 725 | + wd.WriteLine("NICK " + config.username); |
| 726 | + foreach (config.channel ch in config.channels) |
| 727 | + { |
| 728 | + wd.WriteLine("JOIN " + ch.name); |
| 729 | + } |
| 730 | + wd.Flush(); |
| 731 | + return false; |
| 732 | + } |
| 733 | + |
479 | 734 | public static int Connect() |
480 | 735 | { |
481 | 736 | data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream(); |
482 | 737 | rd = new StreamReader(data, System.Text.Encoding.UTF8); |
483 | 738 | wd = new StreamWriter(data); |
| 739 | + check_thread = new System.Threading.Thread(new System.Threading.ThreadStart(Ping)); |
| 740 | + check_thread.Start(); |
484 | 741 | |
485 | 742 | wd.WriteLine("USER " + config.name + " 8 * :" + config.name); |
486 | 743 | wd.WriteLine("NICK " + config.username); |
487 | 744 | |
488 | | - //System.Threading.Thread.Sleep(2000); |
| 745 | + System.Threading.Thread.Sleep(2000); |
489 | 746 | |
490 | 747 | foreach (config.channel ch in config.channels) |
491 | 748 | { |
— | — | @@ -496,7 +753,7 @@ |
497 | 754 | |
498 | 755 | while (true) |
499 | 756 | { |
500 | | - while (!rd.EndOfStream) |
| 757 | + try |
501 | 758 | { |
502 | 759 | string text = rd.ReadLine(); |
503 | 760 | if (text == null || text.StartsWith(":") || !text.Contains("PRIVMSG")) |
— | — | @@ -521,6 +778,10 @@ |
522 | 779 | message = message.Substring(message.IndexOf(":") + 1); |
523 | 780 | if (!message.Contains("ACTION")) |
524 | 781 | { |
| 782 | + nick = info.Substring(0, info.IndexOf("!")); |
| 783 | + host = info.Substring(info.IndexOf("@") + 1, info.IndexOf(" ", info.IndexOf("@")) - 1 - info.IndexOf("@")); |
| 784 | + } |
| 785 | + info_host = info.Substring(info.IndexOf("PRIVMSG ")); |
525 | 786 | |
526 | 787 | } |
527 | 788 | else |