r105996 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105995‎ | r105996 | r105997 >
Date:09:31, 13 December 2011
Author:petrb
Status:deferred
Tags:
Comment:
some updates of the bot code, work in progress
Modified paths:
  • /trunk/tools/wmib/Program.cs (modified) (history)

Diff [purge]

Index: trunk/tools/wmib/Program.cs
@@ -12,7 +12,6 @@
1313
1414 using System;
1515 using System.Collections.Generic;
16 -using System.Linq;
1716 using System.Text;
1817 using System.Net;
1918
@@ -67,7 +66,7 @@
6867 /// <summary>
6968 /// Channels
7069 /// </summary>
71 - public static channel[] channels = { new channel("#bhfsjhhdhgf"), new channel( "#wikimedia-testbot") };
 70+ public static channel[] channels = { new channel("#wikimedia-labs"), new channel( "#wikimedia-test-bots") };
7271 }
7372
7473 public static class irc
@@ -96,7 +95,7 @@
9796 public trust(string channel)
9897 {
9998 // Load
100 - File = _Channel + "_user";
 99+ File = channel + "_user";
101100 if (!System.IO.File.Exists(File))
102101 {
103102 // Create db
@@ -155,13 +154,25 @@
156155 {
157156 foreach (user b in Users)
158157 {
159 - if (b.name == user)
 158+ System.Text.RegularExpressions.Regex id = new System.Text.RegularExpressions.Regex(b.name);
 159+ if (id.Match(user).Success)
160160 {
161161 return b;
162162 }
163163 }
164164 return new user("null", "");
165165 }
 166+
 167+ public void listAll()
 168+ {
 169+ string users_ok = "";
 170+ foreach (user b in Users)
 171+ {
 172+ users_ok = users_ok + " " + b.name;
 173+ }
 174+ Message("I trust to: " + users_ok, _Channel);
 175+ }
 176+
166177 public bool matchLevel(int level, string rights)
167178 {
168179 if (level == 2)
@@ -178,7 +189,7 @@
179190
180191 public bool isApproved(string User, string Host, string command)
181192 {
182 - user current = getUser(User);
 193+ user current = getUser(User + "!@" + Host);
183194 if (current.level == "null")
184195 {
185196 return false;
@@ -204,9 +215,13 @@
205216 {
206217 return matchLevel(1, current.level);
207218 }
 219+ if (command == "admin")
 220+ {
 221+ return matchLevel(2, current.level);
 222+ }
208223 if (command == "trustadd")
209224 {
210 - return matchLevel(1, current.level);
 225+ return matchLevel(1, current.level);
211226 }
212227 if (command == "trustdel")
213228 {
@@ -218,30 +233,153 @@
219234
220235 public class dictionary
221236 {
222 - public Dictionary<string, string> text = new Dictionary<string, string>();
 237+ public class item
 238+ {
 239+ public item(string Key, string Text, string User, string Lock = "false")
 240+ {
 241+ text = Text;
 242+ key = Key;
 243+ locked = Lock;
 244+ user = User;
 245+ }
 246+ public string text;
 247+ public string key;
 248+ public string user;
 249+ public string locked;
 250+ }
 251+ public List<item> text = new List<item>();
 252+ public string Channel;
223253 public void Load(string channel)
224 - {
225 - string file = channel + ".db";
 254+ {
 255+ Channel = channel;
 256+ string file = Channel + ".db";
226257 if (!System.IO.File.Exists(file))
227258 {
228259 // Create db
229260 System.IO.File.WriteAllText(file, "");
230261 }
 262+
231263 }
232 - public void setKey(string text, string key, string user, config.channel channel)
233 - {
234 -
 264+
 265+ public void Save()
 266+ {
 267+ try
 268+ {
 269+ string file = Channel + ".db";
 270+ System.IO.File.WriteAllText(file, "");
 271+ foreach (item key in text)
 272+ {
 273+ System.IO.File.AppendAllText(file, key.key + "|" + key.text + "|" + key.locked + "|" + key.user);
 274+ }
 275+ }
 276+ catch (Exception b)
 277+ {
 278+ handleException(b, Channel);
 279+ }
235280 }
236 - public void aliasKey(string key, string alias, string user, config.channel channel)
237 - {
238 -
 281+
 282+ public bool print(string name)
 283+ {
 284+ if (!name.StartsWith("!"))
 285+ {
 286+ return true;
 287+ }
 288+ name = name.Substring(1);
 289+ if (name.Contains(" ") && name.Contains("|") == false)
 290+ {
 291+ string[] parm = name.Split(' ');
 292+ if (parm[1] == "is")
 293+ {
 294+ setKey(parm[2], parm[0], "");
 295+ return false;
 296+ }
 297+ if (parm[1] == "del")
 298+ {
 299+ rmKey(parm[0], "");
 300+ return false;
 301+ }
 302+ }
 303+ string User ="";
 304+ if (name.Contains("|"))
 305+ {
 306+ User = name.Substring(name.IndexOf("|"));
 307+ User = User.Replace("|", "");
 308+ User = User.Replace(" ", "");
 309+ name = name.Substring(0, name.IndexOf("|"));
 310+ name = name.Replace(" ", "");
 311+ }
 312+ foreach (item data in text)
 313+ {
 314+
 315+ if (data.key == name)
 316+ {
 317+ if (User == "")
 318+ {
 319+ Message(name + " is: " + data.text, Channel);
 320+ } else
 321+ {
 322+ Message(User + ":" + data.text, Channel);
 323+ }
 324+ return true;
 325+ }
 326+ }
 327+ return true;
239328 }
240 - public void rmKey(string key, string user, config.channel channel)
241 - {
242 -
 329+
 330+ public void setKey(string Text, string key, string user)
 331+ {
 332+ try
 333+ {
 334+ if (!Text.Contains("|"))
 335+ {
 336+ foreach (item data in text)
 337+ {
 338+
 339+ if (data.key == key)
 340+ {
 341+ Message("Key exist!", Channel);
 342+ return;
 343+ }
 344+ }
 345+ text.Add(new item(key, Text, user, "false"));
 346+ Message("Key was added!", Channel);
 347+ }
 348+ else
 349+ {
 350+ Message("Error, it contains invalid characters, " + user + " you better not use pipes in text!", Channel);
 351+ }
 352+ Save();
 353+ }
 354+ catch (Exception b)
 355+ {
 356+ handleException(b, Channel);
 357+ }
243358 }
 359+ public void aliasKey(string key, string alias, string user)
 360+ {
 361+ Save();
 362+ }
 363+ public void rmKey(string key, string user)
 364+ {
 365+ foreach (item keys in text)
 366+ {
 367+ if (keys.key == key)
 368+ {
 369+ text.Remove(keys);
 370+ Message("Successfully removed " + key, Channel);
 371+ Save();
 372+ return;
 373+ }
 374+ }
 375+ Message("Unable to find the specified key in db", Channel);
 376+ }
244377 }
245378
 379+ public static void handleException(Exception ex, string chan)
 380+ {
 381+ Message("DEBUG Exception: " + ex.Message + " I feel crushed, uh :|", chan);
 382+ }
 383+
246384 public static config.channel getChannel(string name)
247385 {
248386 foreach (config.channel current in config.channels)
@@ -263,44 +401,66 @@
264402
265403 public static int modifyRights(string message, config.channel channel, string user, string host)
266404 {
267 - if (message.StartsWith("@trustadd"))
 405+ try
268406 {
269 - string[] rights_info = message.Split(' ');
270 - if (channel.Users.isApproved(user, host, "trustadd"))
 407+ if (message.StartsWith("@trustadd"))
271408 {
272 - channel.Users.addUser(rights_info[2], rights_info[1]);
 409+ string[] rights_info = message.Split(' ');
 410+ if (channel.Users.isApproved(user, host, "trustadd"))
 411+ {
 412+ if (!(rights_info[2] == "admin" || rights_info[2] == "trusted"))
 413+ {
 414+ Message("Unknown user level!", channel.name);
 415+ return 2;
 416+ }
 417+ if (rights_info[2] == "admin")
 418+ {
 419+ if (!channel.Users.isApproved(user, host, "admin"))
 420+ {
 421+ Message("Permission denied!", channel.name);
 422+ return 2;
 423+ }
 424+ }
 425+ if (channel.Users.addUser(rights_info[2], rights_info[1]))
 426+ {
 427+ Message("Successfuly added " + rights_info[1], channel.name);
 428+ }
 429+ }
 430+ else
 431+ {
 432+ Message("You are not autorized to perform this, sorry", channel.name);
 433+ }
273434 }
274 - else
 435+ if (message.StartsWith("@trusted"))
275436 {
276 - Message("You are not autorized to perform this, sorry", channel.name);
277 -
 437+ channel.Users.listAll();
278438 }
 439+ if (message.StartsWith("@trustdel"))
 440+ {
 441+ string[] rights_info = message.Split(' ');
 442+ string x = rights_info[1];
 443+ if (channel.Users.isApproved(user, host, "trustdel"))
 444+ {
 445+ channel.Users.delUser(rights_info[1]);
 446+ }
 447+ else
 448+ {
 449+ Message("You are not autorized to perform this, sorry", channel.name);
 450+ }
 451+ }
279452 }
280 - if (message.StartsWith("@trusted"))
 453+ catch (Exception b)
281454 {
282 -
 455+ handleException(b, channel.name);
283456 }
284 - if (message.StartsWith("@trustdel"))
285 - {
286 - string[] rights_info = message.Split(' ');
287 - string x = rights_info[1];
288 - if (channel.Users.isApproved(user, host, "trustdel"))
289 - {
290 - channel.Users.delUser(rights_info[1]);
291 - }
292 - else
293 - {
294 - Message("You are not autorized to perform this, sorry", channel.name);
295 - }
296 - }
297457 return 0;
298458 }
299459
300460 public static void chanLog(string message, config.channel channel, string user, string host)
301461 {
302462 if (channel.logged)
303 - {
304 - string log = "\n" + "[" + System.DateTime.Today.Hour + ":" + System.DateTime.Today.Minute + ":" + System.DateTime.Today.Second + "] " + "<" + user + "> " + message;
 463+ {
 464+ string log = "\n" + "[" + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + System.DateTime.Now.Second + "] " + "<" + user + "> " + message;
305465 System.IO.File.AppendAllText(channel.log, log);
306466 }
307467 }
@@ -310,11 +470,14 @@
311471 config.channel curr = getChannel(channel);
312472 if (curr != null)
313473 {
 474+ curr.Keys.print(message);
314475 chanLog(message, curr, nick, host);
315476 modifyRights(message, curr, nick, host);
316477 }
317 -
318478
 479+
 480+
 481+
319482 return false;
320483 }
321484
@@ -360,7 +523,14 @@
361524 channel = text.Substring(text.IndexOf("#"), text.IndexOf(" ", text.IndexOf("#")) - text.IndexOf("#"));
362525 message = text.Substring(text.IndexOf("PRIVMSG"));
363526 message = message.Substring(message.IndexOf(":") + 1);
364 - getMessage(channel, nick, host, message);
 527+ if (message.Contains("ACTION"))
 528+ {
 529+
 530+ }
 531+ else
 532+ {
 533+ getMessage(channel, nick, host, message);
 534+ }
365535 }
366536 else
367537 {
@@ -368,7 +538,6 @@
369539 }
370540 }
371541 }
372 - Console.Write(text + "\n\n\n");
373542 System.Threading.Thread.Sleep(50);
374543 }
375544 }

Status & tagging log