r107812 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107811‎ | r107812 | r107813 >
Date:09:55, 2 January 2012
Author:petrb
Status:deferred
Tags:
Comment:
improved the output and formating of data
Modified paths:
  • /trunk/tools/wmib/Core.cs (modified) (history)
  • /trunk/tools/wmib/DumpHtm.cs (modified) (history)
  • /trunk/tools/wmib/RClogs.cs (modified) (history)

Diff [purge]

Index: trunk/tools/wmib/DumpHtm.cs
@@ -103,6 +103,7 @@
104104 try
105105 {
106106 string text = CreateHeader();
 107+ text = text + "<h4>Infobot</h4>\n";
107108 text = text + "<table border=1 width=100%>\n<tr><td width=10%>Key</td><td>Value</td></tr>\n";
108109 Channel.Keys.locked = true;
109110 if (Channel.Keys.text.Count > 0)
@@ -112,8 +113,19 @@
113114 text += AddLine(Key.key, Key.text);
114115 }
115116 }
 117+ text = text + "</table>\n";
 118+ text = text + "<h4>Aliases</h4>\n<table border=1 width=100%>\n";
 119+ foreach (dictionary.staticalias data in Channel.Keys.Alias)
 120+ {
 121+ text += AddLine(data.Name, data.Key);
 122+ }
 123+ text = text + "</table>\n";
116124 Channel.Keys.locked = false;
117 - text = text + "<table>\n";
 125+ if (Channel.feed)
 126+ {
 127+ text += "<h4>Recent changes</h4>";
 128+ text = text + Channel.RC.ToTable();
 129+ }
118130 text = text + CreateFooter();
119131 File.WriteAllText(dumpname, text);
120132 }
Index: trunk/tools/wmib/Core.cs
@@ -51,6 +51,7 @@
5252 public static Thread dumphtmt;
5353 public static Thread rc;
5454 public static Thread check_thread;
 55+ public static bool disabled;
5556 private static StreamReader rd;
5657 private static StreamWriter wd;
5758 private static List<user> User = new List<user>();
@@ -178,7 +179,7 @@
179180 /// List of all users in a channel
180181 /// </summary>
181182 private List<user> Users = new List<user>();
182 -
 183+
183184 /// <summary>
184185 /// Channel this class belong to
185186 /// </summary>
@@ -500,11 +501,12 @@
501502 {
502503 if (config.debugchan != null)
503504 {
504 - SlowQueue.DeliverMessage("DEBUG Exception: " + ex.Message + " I feel crushed, uh :|", config.debugchan);
 505+ SlowQueue.DeliverMessage("DEBUG Exception: " + ex.Message + " I feel crushed, uh :|", config.debugchan);
505506 }
506507 Program.Log(ex.Message + ex.Source + ex.StackTrace);
507 - } catch (Exception) // exception happened while we tried to handle another one, ignore that (probably issue with logging)
508 - { }
 508+ }
 509+ catch (Exception) // exception happened while we tried to handle another one, ignore that (probably issue with logging)
 510+ { }
509511 }
510512
511513 /// <summary>
@@ -659,7 +661,7 @@
660662 timedateToString(DateTime.Now.Second) + "] " + "<" +
661663 user + ">\t " + message + "\n";
662664 }
663 - File.AppendAllText(channel.log + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + ".txt", log);
 665+ File.AppendAllText(channel.log + DateTime.Now.Year + timedateToString(DateTime.Now.Month) + timedateToString(DateTime.Now.Day) + ".txt", log);
664666 }
665667 }
666668 catch (Exception er)
@@ -779,7 +781,7 @@
780782 File.Delete(variables.config + "/" + chan.name + ".list");
781783 }
782784 }
783 - catch (Exception) { }
 785+ catch (Exception) { }
784786 config.channels.Remove(chan);
785787 config.Save();
786788 return;
@@ -965,7 +967,7 @@
966968 }
967969 else
968970 {
969 - SlowQueue.DeliverMessage ("Type @commands for list of commands. This bot is running http://meta.wikimedia.org/wiki/WM-Bot version " + config.version + " source code licensed under GPL and located in wikimedia svn", chan.name);
 971+ SlowQueue.DeliverMessage("Type @commands for list of commands. This bot is running http://meta.wikimedia.org/wiki/WM-Bot version " + config.version + " source code licensed under GPL and located in wikimedia svn", chan.name);
970972 return;
971973 }
972974 }
@@ -1173,7 +1175,7 @@
11741176 private static bool ShowHelp(string parameter, string channel)
11751177 {
11761178 switch (parameter.ToLower())
1177 - {
 1179+ {
11781180 case "trustdel":
11791181 showInfo("trustdel", "Remove an entry from access list, example @trustdel regex", channel);
11801182 return false;
@@ -1272,140 +1274,148 @@
12731275 /// <returns></returns>
12741276 public static void Connect()
12751277 {
1276 - data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream();
1277 - rd = new System.IO.StreamReader(data, System.Text.Encoding.UTF8);
1278 - wd = new System.IO.StreamWriter(data);
 1278+ try
 1279+ {
 1280+ data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream();
 1281+ rd = new System.IO.StreamReader(data, System.Text.Encoding.UTF8);
 1282+ wd = new System.IO.StreamWriter(data);
12791283
1280 - _Queue = new Thread(SlowQueue.Run);
1281 - dumphtmt = new Thread(HtmlDump.Start);
1282 - dumphtmt.Start();
1283 - rc = new Thread(RecentChanges.Start);
1284 - rc.Start();
1285 - check_thread = new Thread(Ping);
1286 - check_thread.Start();
 1284+ _Queue = new Thread(SlowQueue.Run);
 1285+ dumphtmt = new Thread(HtmlDump.Start);
 1286+ dumphtmt.Start();
 1287+ rc = new Thread(RecentChanges.Start);
 1288+ rc.Start();
 1289+ check_thread = new Thread(Ping);
 1290+ check_thread.Start();
12871291
1288 - wd.WriteLine("USER " + config.name + " 8 * :" + config.name);
1289 - wd.WriteLine("NICK " + config.username);
 1292+ wd.WriteLine("USER " + config.name + " 8 * :" + config.name);
 1293+ wd.WriteLine("NICK " + config.username);
12901294
1291 - _Queue.Start();
1292 - Thread.Sleep(2000);
 1295+ _Queue.Start();
 1296+ Thread.Sleep(2000);
12931297
1294 - Authenticate();
 1298+ Authenticate();
12951299
1296 - foreach (config.channel ch in config.channels)
1297 - {
1298 - if (ch.name != "")
 1300+ foreach (config.channel ch in config.channels)
12991301 {
1300 - wd.Flush();
1301 - wd.WriteLine("JOIN " + ch.name);
1302 - Thread.Sleep(2000);
 1302+ if (ch.name != "")
 1303+ {
 1304+ wd.Flush();
 1305+ wd.WriteLine("JOIN " + ch.name);
 1306+ Thread.Sleep(2000);
 1307+ }
13031308 }
1304 - }
1305 - wd.Flush();
1306 - string text = "";
1307 - string nick = "";
1308 - string host = "";
1309 - string message = "";
1310 - string channel = "";
1311 - char delimiter = (char)001;
 1309+ wd.Flush();
 1310+ string text = "";
 1311+ string nick = "";
 1312+ string host = "";
 1313+ string message = "";
 1314+ string channel = "";
 1315+ char delimiter = (char)001;
13121316
1313 - while (true)
1314 - {
1315 - try
 1317+ while (!disabled)
13161318 {
1317 - while (!rd.EndOfStream)
 1319+ try
13181320 {
1319 - text = rd.ReadLine();
1320 - if (text.StartsWith(":"))
 1321+ while (!rd.EndOfStream)
13211322 {
1322 - string check = text.Substring(text.IndexOf(" "));
1323 - if (check.StartsWith(" 005"))
 1323+ text = rd.ReadLine();
 1324+ if (text.StartsWith(":"))
13241325 {
 1326+ string check = text.Substring(text.IndexOf(" "));
 1327+ if (check.StartsWith(" 005"))
 1328+ {
13251329
1326 - }
1327 - else
1328 - {
1329 - if (text.Contains("PRIVMSG"))
 1330+ }
 1331+ else
13301332 {
1331 - string info = text.Substring(1, text.IndexOf(" :", 1) - 1);
1332 - string info_host;
1333 - // we got a message here :)
1334 - if (text.Contains("!") && text.Contains("@"))
 1333+ if (text.Contains("PRIVMSG"))
13351334 {
1336 - nick = info.Substring(0, info.IndexOf("!"));
1337 - host = info.Substring(info.IndexOf("@") + 1, info.IndexOf(" ", info.IndexOf("@")) - 1 - info.IndexOf("@"));
1338 - }
1339 - info_host = info.Substring(info.IndexOf("PRIVMSG "));
 1335+ string info = text.Substring(1, text.IndexOf(" :", 1) - 1);
 1336+ string info_host;
 1337+ // we got a message here :)
 1338+ if (text.Contains("!") && text.Contains("@"))
 1339+ {
 1340+ nick = info.Substring(0, info.IndexOf("!"));
 1341+ host = info.Substring(info.IndexOf("@") + 1, info.IndexOf(" ", info.IndexOf("@")) - 1 - info.IndexOf("@"));
 1342+ }
 1343+ info_host = info.Substring(info.IndexOf("PRIVMSG "));
13401344
1341 - if (info_host.Contains("#"))
1342 - {
1343 - channel = info_host.Substring(info_host.IndexOf("#"));
1344 - message = text.Replace(info, "");
1345 - message = message.Substring(message.IndexOf(" :") + 2);
1346 - if (message.Contains(delimiter.ToString() + "ACTION"))
 1345+ if (info_host.Contains("#"))
13471346 {
1348 - getAction(message.Replace(delimiter.ToString() + "ACTION", ""), channel, host, nick);
1349 - continue;
 1347+ channel = info_host.Substring(info_host.IndexOf("#"));
 1348+ message = text.Replace(info, "");
 1349+ message = message.Substring(message.IndexOf(" :") + 2);
 1350+ if (message.Contains(delimiter.ToString() + "ACTION"))
 1351+ {
 1352+ getAction(message.Replace(delimiter.ToString() + "ACTION", ""), channel, host, nick);
 1353+ continue;
 1354+ }
 1355+ else
 1356+ {
 1357+ getMessage(channel, nick, host, message);
 1358+ continue;
 1359+ }
13501360 }
13511361 else
13521362 {
1353 - getMessage(channel, nick, host, message);
1354 - continue;
 1363+ message = text.Substring(text.IndexOf("PRIVMSG"));
 1364+ message = message.Substring(message.IndexOf(":"));
 1365+ // private message
 1366+ if (message.StartsWith(":" + delimiter.ToString() + "FINGER"))
 1367+ {
 1368+ wd.WriteLine("NOTICE " + nick + " :" + delimiter.ToString() + "FINGER" + " I am a bot don't finger me");
 1369+ wd.Flush();
 1370+ continue;
 1371+ }
 1372+ if (message.StartsWith(":" + delimiter.ToString() + "TIME"))
 1373+ {
 1374+ wd.WriteLine("NOTICE " + nick + " :" + delimiter.ToString() + "TIME " + System.DateTime.Now.ToString());
 1375+ wd.Flush();
 1376+ continue;
 1377+ }
 1378+ if (message.StartsWith(":" + delimiter.ToString() + "PING"))
 1379+ {
 1380+ wd.WriteLine("NOTICE " + nick + " :" + delimiter.ToString() + "PING" + message.Substring(message.IndexOf(delimiter.ToString() + "PING") + 5));
 1381+ wd.Flush();
 1382+ continue;
 1383+ }
 1384+ if (message.StartsWith(":" + delimiter.ToString() + "VERSION"))
 1385+ {
 1386+ wd.WriteLine("NOTICE " + nick + " :" + delimiter.ToString() + "VERSION " + config.version);
 1387+ wd.Flush();
 1388+ continue;
 1389+ }
13551390 }
13561391 }
1357 - else
 1392+ if (text.Contains("PING "))
13581393 {
1359 - message = text.Substring(text.IndexOf("PRIVMSG"));
1360 - message = message.Substring(message.IndexOf(":"));
1361 - // private message
1362 - if (message.StartsWith(":" + delimiter.ToString() + "FINGER"))
1363 - {
1364 - wd.WriteLine("NOTICE " + nick + " :" + delimiter.ToString() + "FINGER" + " I am a bot don't finger me");
1365 - wd.Flush();
1366 - continue;
1367 - }
1368 - if (message.StartsWith(":" + delimiter.ToString() + "TIME"))
1369 - {
1370 - wd.WriteLine("NOTICE " + nick + " :" + delimiter.ToString() + "TIME " + System.DateTime.Now.ToString());
1371 - wd.Flush();
1372 - continue;
1373 - }
1374 - if (message.StartsWith(":" + delimiter.ToString() + "PING"))
1375 - {
1376 - wd.WriteLine("NOTICE " + nick + " :" + delimiter.ToString() + "PING" + message.Substring(message.IndexOf(delimiter.ToString() + "PING") + 5));
1377 - wd.Flush();
1378 - continue;
1379 - }
1380 - if (message.StartsWith(":" + delimiter.ToString() + "VERSION"))
1381 - {
1382 - wd.WriteLine("NOTICE " + nick + " :" + delimiter.ToString() + "VERSION " + config.version);
1383 - wd.Flush();
1384 - continue;
1385 - }
 1394+ wd.WriteLine("PONG " + text.Substring(text.IndexOf("PING ") + 5));
 1395+ wd.Flush();
13861396 }
13871397 }
1388 - if (text.Contains("PING "))
1389 - {
1390 - wd.WriteLine("PONG " + text.Substring(text.IndexOf("PING ") + 5));
1391 - wd.Flush();
1392 - }
13931398 }
 1399+ Thread.Sleep(50);
13941400 }
1395 - Thread.Sleep(50);
 1401+ Program.Log("Reconnecting, end of data stream");
 1402+ Reconnect();
13961403 }
1397 - Program.Log("Reconnecting, end of data stream");
1398 - Reconnect();
 1404+ catch (System.IO.IOException xx)
 1405+ {
 1406+ Program.Log("Reconnecting, connection failed " + xx.Message + xx.StackTrace);
 1407+ Reconnect();
 1408+ }
 1409+ catch (Exception xx)
 1410+ {
 1411+ handleException(xx, channel);
 1412+ }
13991413 }
1400 - catch (System.IO.IOException xx)
1401 - {
1402 - Program.Log("Reconnecting, connection failed " + xx.Message + xx.StackTrace);
1403 - Reconnect();
1404 - }
1405 - catch (Exception xx)
1406 - {
1407 - handleException(xx, channel);
1408 - }
14091414 }
 1415+ catch (Exception)
 1416+ {
 1417+ Console.WriteLine("RecentChanges disabled, no connection");
 1418+ disabled = true;
 1419+ }
14101420 }
14111421 public static int Disconnect()
14121422 {
Index: trunk/tools/wmib/RClogs.cs
@@ -60,6 +60,8 @@
6161 /// Channels
6262 /// </summary>
6363 private static List<string> channels;
 64+ public bool changed;
 65+ private bool writable = true;
6466
6567 private static List<RecentChanges> rc = new List<RecentChanges>();
6668
@@ -89,9 +91,31 @@
9092 }
9193 }
9294
 95+ public string ToTable()
 96+ {
 97+ string output = "<table>";
 98+ try
 99+ {
 100+ writable = false;
 101+ foreach (IWatch b in pages)
 102+ {
 103+ output = output + "<tr><td>" + b.Channel + "</td><td>" + b.Page + "</td></tr>\n";
 104+ }
 105+ output = output + "</table>";
 106+ writable = true;
 107+ return output;
 108+ }
 109+ catch (Exception)
 110+ {
 111+ writable = true;
 112+ return "";
 113+ }
 114+ }
 115+
93116 public RecentChanges(config.channel _channel)
94117 {
95118 channel = _channel;
 119+ changed = false;
96120 Load();
97121 rc.Add(this);
98122 }
@@ -225,6 +249,7 @@
226250 public void Load()
227251 {
228252 string name = variables.config + "/" + channel.name + ".list";
 253+ writable = false;
229254 if (File.Exists(name))
230255 {
231256 string[] content = File.ReadAllLines(name);
@@ -238,6 +263,7 @@
239264 }
240265 }
241266 }
 267+ writable = true;
242268 }
243269
244270 /// <summary>
@@ -301,7 +327,12 @@
302328 }
303329 if (pages.Contains(currpage))
304330 {
 331+ while (!writable)
 332+ {
 333+ System.Threading.Thread.Sleep(100);
 334+ }
305335 pages.Remove(currpage);
 336+ channel.Keys.update = true;
306337 Save();
307338 irc.SlowQueue.DeliverMessage("Deleted item from feed", channel.name);
308339 return true;
@@ -314,13 +345,25 @@
315346 return false;
316347 }
317348 irc.SlowQueue.DeliverMessage(
318 - "Unable to delete the string from the list because there is no such wiki site known by a bot",
 349+ "Unable to delete the string from the list because there is no such a wiki site known by a bot",
319350 channel.name);
320351 return false;
321352 }
322353
323354 public static int InsertSite()
324355 {
 356+ if (File.Exists("sites"))
 357+ {
 358+ string[] content = File.ReadAllLines("sites");
 359+ foreach (string a in content)
 360+ {
 361+ string[] values = a.Split('|');
 362+ if (values.Length == 3)
 363+ {
 364+ wikiinfo.Add(new wiki(values[0], values[1], values[2]));
 365+ }
 366+ }
 367+ }
325368 wikiinfo.Add(new wiki("#cs.wikinews", "https://cs.wikipedia.org/w/index.php", "cs_wikinews"));
326369 wikiinfo.Add(new wiki("#en.wikinews", "https://en.wikipedia.org/w/index.php", "en_wikinews"));
327370 wikiinfo.Add(new wiki("#de.wikinews", "https://de.wikipedia.org/w/index.php", "de_wikinews"));
@@ -389,8 +432,13 @@
390433 channel.name);
391434 return true;
392435 }
 436+ while (!writable)
 437+ {
 438+ System.Threading.Thread.Sleep(100);
 439+ }
393440 pages.Add(new IWatch(site, Page, site.channel));
394441 irc.SlowQueue.DeliverMessage("Inserted new item to feed of changes", channel.name);
 442+ channel.Keys.update = true;
395443 Save();
396444 return true;
397445 }
@@ -439,10 +487,6 @@
440488 string link = Edit.Groups[4].Value;
441489 string summary = Edit.Groups[8].Value;
442490
443 - if (summary.Length > 20)
444 - {
445 - summary = summary.Substring(0, 16) + " ...";
446 - }
447491 foreach (RecentChanges curr in rc)
448492 {
449493 if (curr.channel.feed)
@@ -452,9 +496,9 @@
453497 if (w.Channel == _channel && page == w.Page)
454498 {
455499 irc.SlowQueue.DeliverMessage(
456 - "Change on 12" + w.URL.name + "1 a page " + page +
457 - " was modified, summary: " + summary + " changed by " + username +
458 - " link " + w.URL.url + "?diff=" + link, curr.channel.name);
 500+ "Change on 12" + w.URL.name + " a page " + page +
 501+ " was modified," + " changed by " + username +
 502+ " link " + w.URL.url + "?diff=" + link, curr.channel.name + " edit summary: " + summary);
459503 }
460504 }
461505 }

Status & tagging log