r106537 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106536‎ | r106537 | r106538 >
Date:00:33, 18 December 2011
Author:reedy
Status:deferred (Comments)
Tags:
Comment:
Add a few more using statements

Fix some variable collisions
Modified paths:
  • /trunk/tools/wmib/Config.cs (modified) (history)
  • /trunk/tools/wmib/Core.cs (modified) (history)
  • /trunk/tools/wmib/DumpHtm.cs (modified) (history)
  • /trunk/tools/wmib/Program.cs (modified) (history)

Diff [purge]

Index: trunk/tools/wmib/DumpHtm.cs
@@ -8,14 +8,8 @@
99 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1010 //GNU General Public License for more details.
1111
12 -
13 -
1412 using System;
15 -using System.Collections.Generic;
16 -using System.Text;
17 -using System.Net;
1813
19 -
2014 namespace wmib
2115 {
2216 public class HtmlDump
@@ -35,12 +29,10 @@
3630 {
3731 foreach (config.channel chan in config.channels)
3832 {
39 - if (chan.Keys.update)
40 - {
41 - HtmlDump dump = new HtmlDump(chan);
42 - dump.Make();
43 - chan.Keys.update = false;
44 - }
 33+ if (!chan.Keys.update) continue;
 34+ HtmlDump dump = new HtmlDump(chan);
 35+ dump.Make();
 36+ chan.Keys.update = false;
4537 }
4638 System.Threading.Thread.Sleep(320000);
4739 }
@@ -103,9 +95,8 @@
10496 {
10597 try
10698 {
107 - string text;
108 - text = CreateHeader();
109 - text = text + "<table border=1 width=100%>\n<tr><td width=10%>Key</td><td>Value</td></tr>\n";
 99+ string text = CreateHeader();
 100+ text += "<table border=1 width=100%>\n<tr><td width=10%>Key</td><td>Value</td></tr>\n";
110101 Channel.Keys.locked = true;
111102 if (Channel.Keys.text.Count > 0)
112103 {
Index: trunk/tools/wmib/Config.cs
@@ -10,12 +10,9 @@
1111
1212 // Created by Petr Bena benapetr@gmail.com
1313
14 -using System;
1514 using System.Collections.Generic;
16 -using System.Text;
17 -using System.Net;
 15+using System.IO;
1816
19 -
2017 namespace wmib
2118 {
2219 public static class config
@@ -65,24 +62,24 @@
6663 public void LoadConfig()
6764 {
6865 string conf_file = name + ".setting";
69 - if (!System.IO.File.Exists(conf_file))
 66+ if (!File.Exists(conf_file))
7067 {
71 - System.IO.File.WriteAllText(conf_file, "");
 68+ File.WriteAllText(conf_file, "");
7269 Program.Log("Creating datafile for channel " + name);
7370 return;
7471 }
75 - conf = System.IO.File.ReadAllText(conf_file);
76 - if (config.parseConfig(conf, "keysdb") != "")
 72+ conf = File.ReadAllText(conf_file);
 73+ if (parseConfig(conf, "keysdb") != "")
7774 {
78 - keydb = (config.parseConfig(conf, "keysdb"));
 75+ keydb = (parseConfig(conf, "keysdb"));
7976 }
80 - if (config.parseConfig(conf, "logged") != "")
 77+ if (parseConfig(conf, "logged") != "")
8178 {
82 - logged = bool.Parse(config.parseConfig(conf, "logged"));
 79+ logged = bool.Parse(parseConfig(conf, "logged"));
8380 }
84 - if (config.parseConfig(conf, "infodb") != "")
 81+ if (parseConfig(conf, "infodb") != "")
8582 {
86 - info = bool.Parse(config.parseConfig(conf, "infodb"));
 83+ info = bool.Parse(parseConfig(conf, "infodb"));
8784 }
8885 }
8986
@@ -95,7 +92,7 @@
9693 AddConfig("infodb", info.ToString());
9794 AddConfig("logged", logged.ToString());
9895 AddConfig("keysdb", keydb);
99 - System.IO.File.WriteAllText(name + ".setting", conf);
 96+ File.WriteAllText(name + ".setting", conf);
10097 }
10198
10299 /// <summary>
@@ -110,13 +107,13 @@
111108 logged = true;
112109 name = Name;
113110 LoadConfig();
114 - if (!System.IO.Directory.Exists("log"))
 111+ if (!Directory.Exists("log"))
115112 {
116 - System.IO.Directory.CreateDirectory("log");
 113+ Directory.CreateDirectory("log");
117114 }
118 - if (!System.IO.Directory.Exists("log/" + Name))
 115+ if (!Directory.Exists("log/" + Name))
119116 {
120 - System.IO.Directory.CreateDirectory("log/" + Name);
 117+ Directory.CreateDirectory("log/" + Name);
121118 }
122119 Keys = new irc.dictionary(keydb, name);
123120 log = "log/" + Name + "/";
@@ -148,7 +145,7 @@
149146 text = text + current.name + ",\n";
150147 }
151148 text = text + ";";
152 - System.IO.File.WriteAllText("wmib", text);
 149+ File.WriteAllText("wmib", text);
153150 }
154151
155152 /// <summary>
@@ -174,13 +171,13 @@
175172 /// </summary>
176173 public static void Load()
177174 {
178 - text = System.IO.File.ReadAllText("wmib");
 175+ text = File.ReadAllText("wmib");
179176 foreach (string x in parseConfig(text, "channels").Replace("\n", "").Split(','))
180177 {
181 - string name=x.Replace(" ", "");
182 - if (!(name == ""))
 178+ string config =x.Replace(" ", "");
 179+ if (config != "")
183180 {
184 - channels.Add(new channel(name));
 181+ channels.Add(new channel(config));
185182 }
186183 }
187184 username = parseConfig(text, "username");
@@ -188,12 +185,14 @@
189186 login = parseConfig(text, "nick");
190187 debugchan = parseConfig(text, "debug");
191188 password = parseConfig(text, "password");
192 - if (!System.IO.Directory.Exists(config.DumpDir))
 189+ if (!Directory.Exists(DumpDir))
193190 {
194 - System.IO.Directory.CreateDirectory(config.DumpDir);
 191+ Directory.CreateDirectory(DumpDir);
195192 }
196193 }
 194+
197195 public static string text;
 196+
198197 /// <summary>
199198 /// Network
200199 /// </summary>
@@ -202,31 +201,39 @@
203202 /// Nick name
204203 /// </summary>
205204 public static string username = "wm-bot";
 205+
206206 public static string debugchan = "";
 207+
207208 /// <summary>
208209 /// Login name
209210 /// </summary>
210211 public static string login = "";
 212+
211213 /// <summary>
212214 /// Login pw
213215 /// </summary>
214216 public static string password = "";
 217+
215218 /// <summary>
216219 /// Dump
217220 /// </summary>
218221 public static string DumpDir = "dump";
 222+
219223 /// <summary>
220224 /// Version
221225 /// </summary>
222226 public static string version = "wikimedia bot v. 1.1.4";
 227+
223228 /// <summary>
224229 /// Separator
225230 /// </summary>
226231 public static string separator = "|";
 232+
227233 /// <summary>
228234 /// User name
229235 /// </summary>
230236 public static string name = "wm-bot";
 237+
231238 /// <summary>
232239 /// Channels
233240 /// </summary>
Index: trunk/tools/wmib/Program.cs
@@ -11,11 +11,7 @@
1212 // Created by Petr Bena benapetr@gmail.com
1313
1414 using System;
15 -using System.Collections.Generic;
16 -using System.Text;
17 -using System.Net;
1815
19 -
2016 namespace wmib
2117 {
2218 class Program
@@ -25,6 +21,7 @@
2622 Console.WriteLine("LOG: " + msg);
2723 return false;
2824 }
 25+
2926 static void Main(string[] args)
3027 {
3128 Log("Connecting");
Index: trunk/tools/wmib/Core.cs
@@ -10,10 +10,8 @@
1111
1212 using System;
1313 using System.Collections.Generic;
14 -using System.Text;
15 -using System.Net;
 14+using System.IO;
1615
17 -
1816 namespace wmib
1917 {
2018 public class misc
@@ -39,8 +37,8 @@
4038 private static System.Net.Sockets.NetworkStream data;
4139 public static System.Threading.Thread dumphtmt;
4240 public static System.Threading.Thread check_thread;
43 - public static System.IO.StreamReader rd;
44 - private static System.IO.StreamWriter wd;
 41+ public static StreamReader rd;
 42+ private static StreamWriter wd;
4543 private static List<user> User = new List<user>();
4644
4745 public class messages
@@ -76,22 +74,25 @@
7775 public string value;
7876 public string regex;
7977 public bool searching;
80 - public bool result = false;
 78+ public bool result;
 79+
8180 public RegexCheck(string Regex, string Data)
8281 {
8382 result = false;
8483 value = Data;
8584 regex = Regex;
8685 }
 86+
8787 private void Run()
8888 {
8989 System.Text.RegularExpressions.Regex c = new System.Text.RegularExpressions.Regex(regex);
9090 result = c.Match(value).Success;
9191 searching = false;
9292 }
 93+
9394 public int IsMatch()
9495 {
95 - System.Threading.Thread quick = new System.Threading.Thread(new System.Threading.ThreadStart(Run));
 96+ System.Threading.Thread quick = new System.Threading.Thread(Run);
9697 searching = true;
9798 quick.Start();
9899 int check = 0;
@@ -99,17 +100,11 @@
100101 {
101102 check++;
102103 System.Threading.Thread.Sleep(10);
103 - if (check > 50)
104 - {
105 - quick.Abort();
106 - return 2;
107 - }
 104+ if (check <= 50) continue;
 105+ quick.Abort();
 106+ return 2;
108107 }
109 - if (result)
110 - {
111 - return 1;
112 - }
113 - return 0;
 108+ return result ? 1 : 0;
114109 }
115110 }
116111
@@ -126,7 +121,7 @@
127122 /// <summary>
128123 /// File where data are stored
129124 /// </summary>
130 - public string File;
 125+ public string DataFile;
131126
132127 /// <summary>
133128 /// Constructor
@@ -135,14 +130,14 @@
136131 public IRCTrust(string channel)
137132 {
138133 // Load
139 - File = channel + "_user";
140 - if (!System.IO.File.Exists(File))
 134+ DataFile = channel + "_user";
 135+ if (!File.Exists(DataFile))
141136 {
142137 // Create db
143138 Program.Log("Creating user file for " + channel);
144 - System.IO.File.WriteAllText(File, "");
 139+ File.WriteAllText(DataFile, "");
145140 }
146 - string[] db = System.IO.File.ReadAllLines(channel + "_user");
 141+ string[] db = File.ReadAllLines(channel + "_user");
147142 _Channel = channel;
148143 foreach (string x in db)
149144 {
@@ -162,10 +157,10 @@
163158 /// <returns></returns>
164159 public bool Save()
165160 {
166 - System.IO.File.WriteAllText(File, "");
 161+ File.WriteAllText(DataFile, "");
167162 foreach (user u in Users)
168163 {
169 - System.IO.File.AppendAllText(File, encode(u.name) + config.separator + u.level + "\n");
 164+ File.AppendAllText(DataFile, encode(u.name) + config.separator + u.level + "\n");
170165 }
171166 return true;
172167 }
@@ -412,13 +407,13 @@
413408 public void Load()
414409 {
415410 text.Clear();
416 - if (!System.IO.File.Exists(datafile))
 411+ if (!File.Exists(datafile))
417412 {
418413 // Create db
419 - System.IO.File.WriteAllText(datafile, "");
 414+ File.WriteAllText(datafile, "");
420415 }
421416
422 - string[] db = System.IO.File.ReadAllLines(datafile);
 417+ string[] db = File.ReadAllLines(datafile);
423418 foreach (string x in db)
424419 {
425420 if (x.Contains(config.separator))
@@ -460,14 +455,14 @@
461456 update = true;
462457 try
463458 {
464 - System.IO.File.WriteAllText(datafile, "");
 459+ File.WriteAllText(datafile, "");
465460 foreach (staticalias key in Alias)
466461 {
467 - System.IO.File.AppendAllText(datafile, key.Name + config.separator + key.Key + config.separator + "alias" + "\n");
 462+ File.AppendAllText(datafile, key.Name + config.separator + key.Key + config.separator + "alias" + "\n");
468463 }
469464 foreach (item key in text)
470465 {
471 - System.IO.File.AppendAllText(datafile, key.key + config.separator + key.text + config.separator + "key" + config.separator + key.locked + config.separator + key.user + "\n");
 466+ File.AppendAllText(datafile, key.key + config.separator + key.text + config.separator + "key" + config.separator + key.locked + config.separator + key.user + "\n");
472467 }
473468 }
474469 catch (Exception b)
@@ -1016,7 +1011,7 @@
10171012 {
10181013 log = "[" + timedateToString( System.DateTime.Now.Hour) + ":" + timedateToString( System.DateTime.Now.Minute ) + ":" + timedateToString( System.DateTime.Now.Second) + "] " + "<" + user + ">\t " + message + "\n";
10191014 }
1020 - System.IO.File.AppendAllText(channel.log + System.DateTime.Now.Year + System.DateTime.Now.Month + System.DateTime.Now.Day +".txt", log);
 1015+ File.AppendAllText(channel.log + System.DateTime.Now.Year + System.DateTime.Now.Month + System.DateTime.Now.Day +".txt", log);
10211016 }
10221017 }
10231018 catch (Exception er)
@@ -1119,12 +1114,12 @@
11201115 wd.WriteLine("PART " + chan.name);
11211116 System.Threading.Thread.Sleep(100);
11221117 wd.Flush();
1123 - if (!System.IO.Directory.Exists(chan.log))
 1118+ if (!Directory.Exists(chan.log))
11241119 {
1125 - System.IO.Directory.Delete(chan.log, true);
 1120+ Directory.Delete(chan.log, true);
11261121 }
1127 - System.IO.File.Delete(chan.name + ".setting");
1128 - System.IO.File.Delete(chan.Users.File);
 1122+ File.Delete(chan.name + ".setting");
 1123+ File.Delete(chan.Users.DataFile);
11291124 config.channels.Remove(chan);
11301125 config.Save();
11311126 return;
@@ -1295,11 +1290,8 @@
12961291 return;
12971292 }
12981293 }
1299 - else
1300 - {
1301 - Message(messages.PermissionDenied, chan.name);
1302 - return;
1303 - }
 1294+ Message(messages.PermissionDenied, chan.name);
 1295+ return;
13041296 }
13051297 if (message == "@commands")
13061298 {
@@ -1350,8 +1342,8 @@
13511343 public static bool Reconnect()
13521344 {
13531345 data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream();
1354 - rd = new System.IO.StreamReader(data, System.Text.Encoding.UTF8);
1355 - wd = new System.IO.StreamWriter(data);
 1346+ rd = new StreamReader(data, System.Text.Encoding.UTF8);
 1347+ wd = new StreamWriter(data);
13561348 wd.WriteLine("USER " + config.name + " 8 * :" + config.name);
13571349 wd.WriteLine("NICK " + config.username);
13581350 Authenticate();
@@ -1371,12 +1363,12 @@
13721364 public static int Connect()
13731365 {
13741366 data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream();
1375 - rd = new System.IO.StreamReader(data, System.Text.Encoding.UTF8);
1376 - wd = new System.IO.StreamWriter(data);
 1367+ rd = new StreamReader(data, System.Text.Encoding.UTF8);
 1368+ wd = new StreamWriter(data);
13771369
1378 - dumphtmt = new System.Threading.Thread(new System.Threading.ThreadStart(HtmlDump.Start));
 1370+ dumphtmt = new System.Threading.Thread(HtmlDump.Start);
13791371 dumphtmt.Start();
1380 - check_thread = new System.Threading.Thread(new System.Threading.ThreadStart(Ping));
 1372+ check_thread = new System.Threading.Thread(Ping);
13811373 check_thread.Start();
13821374
13831375 wd.WriteLine("USER " + config.name + " 8 * :" + config.name);
@@ -1440,11 +1432,8 @@
14411433 getAction(message.Replace(delimiter.ToString() +"ACTION", ""), channel, host, nick);
14421434 continue;
14431435 }
1444 - else
1445 - {
1446 - getMessage(channel, nick, host, message);
1447 - continue;
1448 - }
 1436+ getMessage(channel, nick, host, message);
 1437+ continue;
14491438 }
14501439 else
14511440 {
@@ -1489,7 +1478,7 @@
14901479 Program.Log("Reconnecting, end of data stream");
14911480 Reconnect();
14921481 }
1493 - catch (System.IO.IOException xx)
 1482+ catch (IOException xx)
14941483 {
14951484 Program.Log("Reconnecting, connection failed " + xx.Message + xx.StackTrace);
14961485 Reconnect();
@@ -1501,6 +1490,7 @@
15021491 }
15031492 return 0;
15041493 }
 1494+
15051495 public static int Disconnect()
15061496 {
15071497 wd.Flush();

Comments

#Comment by Johnduhart (talk | contribs)   00:39, 18 December 2011
+                    if (!chan.Keys.update) continue;

Are we following MediaWiki standards with this?

#Comment by Reedy (talk | contribs)   00:50, 18 December 2011

No

Status & tagging log