r106114 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106113‎ | r106114 | r106115 >
Date:22:04, 13 December 2011
Author:petrb
Status:deferred
Tags:
Comment:
Added a lot of stuff
Modified paths:
  • /trunk/tools/wmib/Program.cs (modified) (history)

Diff [purge]

Index: trunk/tools/wmib/Program.cs
@@ -29,12 +29,31 @@
3030 private static void Main(string[] args)
3131 {
3232 Log("Connecting");
 33+ config.Load();
3334 irc.Connect();
3435 }
3536 }
3637
3738 public static class config
3839 {
 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+ }
3958 public class channel
4059 {
4160 public string name;
@@ -82,6 +101,7 @@
83102 public static StreamReader rd;
84103 private static StreamWriter wd;
85104 //private static List<IrcUser> User = new List<IrcUser>();
 105+ public static System.Threading.Thread check_thread;
86106
87107 public class IrcUser
88108 {
@@ -115,11 +135,11 @@
116136 _Channel = channel;
117137 foreach (string x in db)
118138 {
119 - if (x.Contains("|"))
 139+ if (x.Contains(config.separator))
120140 {
121 - string[] info = x.Split('|');
 141+ string[] info = x.Split(Char.Parse(config.separator));
122142 string level = info[1];
123 - string name = info[0];
 143+ string name = decode(info[0]);
124144 Users.Add(name, new IrcUser(level, name));
125145 }
126146 }
@@ -152,6 +172,9 @@
153173 {
154174 Message("This user is admin which can't be deleted from db, sorry", _Channel);
155175 return true;
 176+ Save();
 177+ Message("User was deleted from access list", _Channel);
 178+ return true;
156179 }
157180 Users.Remove(user);
158181 Save();
@@ -179,7 +202,7 @@
180203 {
181204 users_ok = users_ok + " " + b.Key;
182205 }
183 - Message("I trust to: " + users_ok, _Channel);
 206+ Message("I trust: " + users_ok, _Channel);
184207 }
185208
186209 public bool matchLevel(int level, string rights)
@@ -240,6 +263,7 @@
241264 }
242265
243266 public List<item> text = new List<item>();
 267+ public List<staticalias> Alias = new List<staticalias>();
244268 public string Channel;
245269
246270 public void Load(string channel)
@@ -252,6 +276,25 @@
253277 File.WriteAllText(file, "");
254278 }
255279
 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+ }
256299 }
257300
258301 public void Save()
@@ -260,6 +303,10 @@
261304 {
262305 string file = Channel + ".db";
263306 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+ }
264311 foreach (item key in text)
265312 {
266313 File.AppendAllText(file, key.key + "|" + key.text + "|" + key.locked + "|" + key.user);
@@ -271,24 +318,64 @@
272319 }
273320 }
274321
275 - public bool print(string name)
 322+ public string getValue(string key)
276323 {
 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+ {
277336 if (!name.StartsWith("!"))
278337 {
279338 return true;
280339 }
281340 name = name.Substring(1);
282 - if (name.Contains(" ") && name.Contains("|") == false)
 341+ if (name.Contains(" "))
283342 {
284343 string[] parm = name.Split(' ');
285344 if (parm[1] == "is")
286345 {
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+ }
288355 return false;
289356 }
 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+ }
290370 if (parm[1] == "del")
291371 {
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+ }
293380 return false;
294381 }
295382 }
@@ -299,43 +386,121 @@
300387 User = User.Replace("|", "");
301388 User = User.Replace(" ", "");
302389 name = name.Substring(0, name.IndexOf("|"));
303 - name = name.Replace(" ", "");
304390 }
305391 foreach (item data in text)
306392 {
307393
308394 if (data.key == name)
309395 {
 396+ keyv = keyv.Replace("$1", p1);
310397 if (User == "")
311398 {
312 - Message(name + " is: " + data.text, Channel);
 399+ Message(keyv, Channel);
313400 }
314401 else
315402 {
316 - Message(User + ":" + data.text, Channel);
 403+ Message(User + ": " + keyv, Channel);
317404 }
318405 return true;
319406 }
 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+ }
320425 }
321426 return true;
322427 }
323428
 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+
324492 public void setKey(string Text, string key, string user)
325493 {
326494 try
327495 {
328 - if (!Text.Contains("|"))
329 - {
330496 foreach (item data in text)
331497 {
332 -
333498 if (data.key == key)
334499 {
335500 Message("Key exist!", Channel);
336501 return;
337502 }
338503 }
339 - text.Add(new item(key, Text, user, "false"));
 504+ text.Add(new item(key, encode(Text), user, "false"));
340505 Message("Key was added!", Channel);
341506 }
342507 else
@@ -354,6 +519,16 @@
355520
356521 public void aliasKey(string key, string alias, string user)
357522 {
 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);
358533 Save();
359534 }
360535
@@ -406,6 +581,11 @@
407582 string[] rights_info = message.Split(' ');
408583 if (channel.Users.isApproved(user, host, "trustadd"))
409584 {
 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+ }
410590 if (!(rights_info[2] == "admin" || rights_info[2] == "trusted"))
411591 {
412592 Message("Unknown user level!", channel.name);
@@ -454,7 +634,7 @@
455635 return 0;
456636 }
457637
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)
459639 {
460640 if (!channel.logged) return;
461641 string log = "\n" + "[" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" +
@@ -462,6 +642,63 @@
463643 File.AppendAllText(channel.log, log);
464644 }
465645
 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+
466703 public static bool getMessage(string channel, string nick, string host, string message)
467704 {
468705 config.channel curr = getChannel(channel);
@@ -470,21 +707,41 @@
471708 curr.Keys.print(message);
472709 chanLog(message, curr, nick, host);
473710 modifyRights(message, curr, nick, host);
 711+ addChannel(curr, nick, host, message);
 712+ partChannel(curr, nick, host, message);
 713+ }
474714 }
475715
476716 return false;
477717 }
478718
 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+
479734 public static int Connect()
480735 {
481736 data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream();
482737 rd = new StreamReader(data, System.Text.Encoding.UTF8);
483738 wd = new StreamWriter(data);
 739+ check_thread = new System.Threading.Thread(new System.Threading.ThreadStart(Ping));
 740+ check_thread.Start();
484741
485742 wd.WriteLine("USER " + config.name + " 8 * :" + config.name);
486743 wd.WriteLine("NICK " + config.username);
487744
488 - //System.Threading.Thread.Sleep(2000);
 745+ System.Threading.Thread.Sleep(2000);
489746
490747 foreach (config.channel ch in config.channels)
491748 {
@@ -496,7 +753,7 @@
497754
498755 while (true)
499756 {
500 - while (!rd.EndOfStream)
 757+ try
501758 {
502759 string text = rd.ReadLine();
503760 if (text == null || text.StartsWith(":") || !text.Contains("PRIVMSG"))
@@ -521,6 +778,10 @@
522779 message = message.Substring(message.IndexOf(":") + 1);
523780 if (!message.Contains("ACTION"))
524781 {
 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 "));
525786
526787 }
527788 else

Status & tagging log