r106011 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106010‎ | r106011 | r106012 >
Date:11:16, 13 December 2011
Author:reedy
Status:deferred
Tags:
Comment:
Use Dictionar<Key, Value> instead of lists, to prevent foreach loop checkign values

Collapse a mass of if statements

Reduce nesting
Modified paths:
  • /trunk/tools/wmib/Program.cs (modified) (history)

Diff [purge]

Index: trunk/tools/wmib/Program.cs
@@ -11,14 +11,14 @@
1212 // Created by Petr Bena benapetr@gmail.com
1313
1414 using System;
 15+using System.IO;
1516 using System.Collections.Generic;
16 -using System.Text;
17 -using System.Net;
 17+using System.Text.RegularExpressions;
1818
1919
2020 namespace wmib
2121 {
22 - class Program
 22+ internal class Program
2323 {
2424 public static bool Log(string msg)
2525 {
@@ -41,7 +41,7 @@
4242 public bool logged;
4343 public string log;
4444 public irc.dictionary Keys = new irc.dictionary();
45 - public irc.trust Users;
 45+ public irc.IrcTrust Users;
4646
4747 public channel(string Name)
4848 {
@@ -49,7 +49,7 @@
5050 name = Name;
5151 log = Name + ".txt";
5252 Keys.Load(name);
53 - Users = new irc.trust(name);
 53+ Users = new irc.IrcTrust(name);
5454 }
5555 }
5656
@@ -79,13 +79,13 @@
8080 public static class irc
8181 {
8282 private static System.Net.Sockets.NetworkStream data;
83 - public static System.IO.StreamReader rd;
84 - private static System.IO.StreamWriter wd;
85 - private static List<user> User = new List<user>();
 83+ public static StreamReader rd;
 84+ private static StreamWriter wd;
 85+ //private static List<IrcUser> User = new List<IrcUser>();
8686
87 - public class user
 87+ public class IrcUser
8888 {
89 - public user(string level, string name)
 89+ public IrcUser(string level, string name)
9090 {
9191 this.level = level;
9292 this.name = name;
@@ -95,13 +95,13 @@
9696 public string level;
9797 }
9898
99 - public class trust
 99+ public class IrcTrust
100100 {
101 - private List<user> Users = new List<user>();
 101+ private readonly Dictionary<string, IrcUser> Users = new Dictionary<string, IrcUser>();
102102 public string _Channel;
103103 public string File;
104104
105 - public trust(string channel)
 105+ public IrcTrust(string channel)
106106 {
107107 // Load
108108 File = channel + "_user";
@@ -112,7 +112,7 @@
113113 System.IO.File.WriteAllText(File, "");
114114 }
115115 string[] db = System.IO.File.ReadAllLines(channel + "_user");
116 - this._Channel = channel;
 116+ _Channel = channel;
117117 foreach (string x in db)
118118 {
119119 if (x.Contains("|"))
@@ -120,7 +120,7 @@
121121 string[] info = x.Split('|');
122122 string level = info[1];
123123 string name = info[0];
124 - Users.Add(new user(level, name));
 124+ Users.Add(name, new IrcUser(level, name));
125125 }
126126 }
127127 }
@@ -128,57 +128,56 @@
129129 public bool Save()
130130 {
131131 System.IO.File.WriteAllText(File, "");
132 - foreach (user u in Users)
 132+ foreach (KeyValuePair<string, IrcUser> u in Users)
133133 {
134 - System.IO.File.AppendAllText(File, u.name + "|" + u.level + "\n");
 134+ System.IO.File.AppendAllText(File, u.Key + "|" + u.Value.level + "\n");
135135 }
136136 return true;
137137 }
138138
139139 public bool addUser(string level, string user)
140140 {
141 - Users.Add(new user(level, user));
 141+ Users.Add(user, new IrcUser(level, user));
142142 Save();
143143 return true;
144144 }
145145
146146 public bool delUser(string user)
147147 {
148 - foreach (user u in Users)
 148+ IrcUser ircuser;
 149+ Users.TryGetValue(user, out ircuser);
 150+ if (ircuser != null)
149151 {
150 - if (u.name == user)
 152+ if (ircuser.level == "admin")
151153 {
152 - if (u.level == "admin")
153 - {
154 - Message("This user is admin which can't be deleted from db, sorry", _Channel);
155 - return true;
156 - }
157 - Users.Remove(u);
 154+ Message("This user is admin which can't be deleted from db, sorry", _Channel);
 155+ return true;
158156 }
 157+ Users.Remove(user);
 158+ Save();
159159 }
160 - Save();
 160+
161161 return true;
162162 }
163163
164 - public user getUser(string user)
 164+ public IrcUser getUser(string user)
165165 {
166 - foreach (user b in Users)
 166+ foreach (KeyValuePair<string, IrcUser> b in Users)
167167 {
168 - System.Text.RegularExpressions.Regex id = new System.Text.RegularExpressions.Regex(b.name);
169 - if (id.Match(user).Success)
 168+ if (new Regex(b.Key).Match(user).Success)
170169 {
171 - return b;
 170+ return b.Value;
172171 }
173172 }
174 - return new user("null", "");
 173+ return null;
175174 }
176175
177176 public void listAll()
178177 {
179178 string users_ok = "";
180 - foreach (user b in Users)
 179+ foreach (KeyValuePair<string, IrcUser> b in Users)
181180 {
182 - users_ok = users_ok + " " + b.name;
 181+ users_ok = users_ok + " " + b.Key;
183182 }
184183 Message("I trust to: " + users_ok, _Channel);
185184 }
@@ -199,44 +198,25 @@
200199
201200 public bool isApproved(string User, string Host, string command)
202201 {
203 - user current = getUser(User + "!@" + Host);
204 - if (current.level == "null")
 202+ IrcUser current = getUser(User + "!@" + Host);
 203+ if (current == null)
205204 {
206205 return false;
207206 }
208207
209 - if (command == "alias_key")
 208+ switch (command)
210209 {
211 - return matchLevel(1, current.level);
 210+ case "alias_key":
 211+ case "new_key":
 212+ case "shutdown":
 213+ case "delete_key":
 214+ case "trust":
 215+ case "trustadd":
 216+ case "trustdel":
 217+ return matchLevel(1, current.level);
 218+ case "admin":
 219+ return matchLevel(2, current.level);
212220 }
213 - if (command == "new_key")
214 - {
215 - return matchLevel(1, current.level);
216 - }
217 - if (command == "shutdown")
218 - {
219 - return matchLevel(1, current.level);
220 - }
221 - if (command == "delete_key")
222 - {
223 - return matchLevel(1, current.level);
224 - }
225 - if (command == "trust")
226 - {
227 - return matchLevel(1, current.level);
228 - }
229 - if (command == "admin")
230 - {
231 - return matchLevel(2, current.level);
232 - }
233 - if (command == "trustadd")
234 - {
235 - return matchLevel(1, current.level);
236 - }
237 - if (command == "trustdel")
238 - {
239 - return matchLevel(1, current.level);
240 - }
241221 return false;
242222 }
243223 }
@@ -266,10 +246,10 @@
267247 {
268248 Channel = channel;
269249 string file = Channel + ".db";
270 - if (!System.IO.File.Exists(file))
 250+ if (!File.Exists(file))
271251 {
272252 // Create db
273 - System.IO.File.WriteAllText(file, "");
 253+ File.WriteAllText(file, "");
274254 }
275255
276256 }
@@ -279,10 +259,10 @@
280260 try
281261 {
282262 string file = Channel + ".db";
283 - System.IO.File.WriteAllText(file, "");
 263+ File.WriteAllText(file, "");
284264 foreach (item key in text)
285265 {
286 - System.IO.File.AppendAllText(file, key.key + "|" + key.text + "|" + key.locked + "|" + key.user);
 266+ File.AppendAllText(file, key.key + "|" + key.text + "|" + key.locked + "|" + key.user);
287267 }
288268 }
289269 catch (Exception b)
@@ -456,7 +436,7 @@
457437 if (message.StartsWith("@trustdel"))
458438 {
459439 string[] rights_info = message.Split(' ');
460 - string x = rights_info[1];
 440+
461441 if (channel.Users.isApproved(user, host, "trustdel"))
462442 {
463443 channel.Users.delUser(rights_info[1]);
@@ -476,12 +456,10 @@
477457
478458 public static void chanLog(string message, config.channel channel, string user, string host)
479459 {
480 - if (channel.logged)
481 - {
482 - string log = "\n" + "[" + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" +
483 - System.DateTime.Now.Second + "] " + "<" + user + "> " + message;
484 - System.IO.File.AppendAllText(channel.log, log);
485 - }
 460+ if (!channel.logged) return;
 461+ string log = "\n" + "[" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" +
 462+ DateTime.Now.Second + "] " + "<" + user + "> " + message;
 463+ File.AppendAllText(channel.log, log);
486464 }
487465
488466 public static bool getMessage(string channel, string nick, string host, string message)
@@ -494,17 +472,14 @@
495473 modifyRights(message, curr, nick, host);
496474 }
497475
498 -
499 -
500 -
501476 return false;
502477 }
503478
504479 public static int Connect()
505480 {
506481 data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream();
507 - rd = new System.IO.StreamReader(data, System.Text.Encoding.UTF8);
508 - wd = new System.IO.StreamWriter(data);
 482+ rd = new StreamReader(data, System.Text.Encoding.UTF8);
 483+ wd = new StreamWriter(data);
509484
510485 wd.WriteLine("USER " + config.name + " 8 * :" + config.name);
511486 wd.WriteLine("NICK " + config.username);
@@ -516,55 +491,52 @@
517492 wd.WriteLine("JOIN " + ch.name);
518493 }
519494 wd.Flush();
520 - string text = "";
521495 string nick = "";
522496 string host = "";
523 - string message = "";
524 - string channel = "";
525497
526498 while (true)
527499 {
528500 while (!rd.EndOfStream)
529501 {
530 - text = rd.ReadLine();
531 - if (text.StartsWith(":"))
 502+ string text = rd.ReadLine();
 503+ if (text == null || text.StartsWith(":") || !text.Contains("PRIVMSG"))
532504 {
533 - if (text.Contains("PRIVMSG"))
 505+ continue;
 506+ }
 507+
 508+ // we got a message here :)
 509+ if (text.Contains("!") && text.Contains("@"))
 510+ {
 511+ nick = text.Substring(1, text.IndexOf("!") - 1);
 512+ host = text.Substring(text.IndexOf("@") + 1,
 513+ text.IndexOf(" ", text.IndexOf("@")) - 1 - text.IndexOf("@"));
 514+ }
 515+ if (
 516+ text.Substring(text.IndexOf("PRIVMSG ", text.IndexOf(" "), text.IndexOf("PRIVMSG "))).Contains(
 517+ "#"))
 518+ {
 519+ string channel = text.Substring(text.IndexOf("#"),
 520+ text.IndexOf(" ", text.IndexOf("#")) - text.IndexOf("#"));
 521+ string message = text.Substring(text.IndexOf("PRIVMSG"));
 522+ message = message.Substring(message.IndexOf(":") + 1);
 523+ if (!message.Contains("ACTION"))
534524 {
535 - // we got a message here :)
536 - if (text.Contains("!") && text.Contains("@"))
537 - {
538 - nick = text.Substring(1, text.IndexOf("!") - 1);
539 - host = text.Substring(text.IndexOf("@") + 1,
540 - text.IndexOf(" ", text.IndexOf("@")) - 1 - text.IndexOf("@"));
541 - }
542 - if (
543 - text.Substring(text.IndexOf("PRIVMSG ", text.IndexOf(" "), text.IndexOf("PRIVMSG "))).
544 - Contains("#"))
545 - {
546 - channel = text.Substring(text.IndexOf("#"),
547 - text.IndexOf(" ", text.IndexOf("#")) - text.IndexOf("#"));
548 - message = text.Substring(text.IndexOf("PRIVMSG"));
549 - message = message.Substring(message.IndexOf(":") + 1);
550 - if (message.Contains("ACTION"))
551 - {
552525
553 - }
554 - else
555 - {
556 - getMessage(channel, nick, host, message);
557 - }
558 - }
559 - else
560 - {
561 - // private message
562 - }
563526 }
 527+ else
 528+ {
 529+ getMessage(channel, nick, host, message);
 530+ }
564531 }
 532+ else
 533+ {
 534+ // private message
 535+ }
 536+
565537 System.Threading.Thread.Sleep(50);
566538 }
567539 }
568 - return 0;
 540+ return 0; // FIXME: Unreachable
569541 }
570542
571543 public static int Disconnect()
@@ -574,3 +546,4 @@
575547 }
576548 }
577549 }
 550+

Status & tagging log