Index: trunk/tools/wmib/Program.cs |
— | — | @@ -12,7 +12,6 @@ |
13 | 13 |
|
14 | 14 | using System;
|
15 | 15 | using System.Collections.Generic;
|
16 | | -using System.Linq;
|
17 | 16 | using System.Text;
|
18 | 17 | using System.Net;
|
19 | 18 |
|
— | — | @@ -67,7 +66,7 @@ |
68 | 67 | /// <summary>
|
69 | 68 | /// Channels
|
70 | 69 | /// </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") };
|
72 | 71 | }
|
73 | 72 |
|
74 | 73 | public static class irc
|
— | — | @@ -96,7 +95,7 @@ |
97 | 96 | public trust(string channel)
|
98 | 97 | {
|
99 | 98 | // Load
|
100 | | - File = _Channel + "_user";
|
| 99 | + File = channel + "_user";
|
101 | 100 | if (!System.IO.File.Exists(File))
|
102 | 101 | {
|
103 | 102 | // Create db
|
— | — | @@ -155,13 +154,25 @@ |
156 | 155 | {
|
157 | 156 | foreach (user b in Users)
|
158 | 157 | {
|
159 | | - if (b.name == user)
|
| 158 | + System.Text.RegularExpressions.Regex id = new System.Text.RegularExpressions.Regex(b.name);
|
| 159 | + if (id.Match(user).Success)
|
160 | 160 | {
|
161 | 161 | return b;
|
162 | 162 | }
|
163 | 163 | }
|
164 | 164 | return new user("null", "");
|
165 | 165 | }
|
| 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 | +
|
166 | 177 | public bool matchLevel(int level, string rights)
|
167 | 178 | {
|
168 | 179 | if (level == 2)
|
— | — | @@ -178,7 +189,7 @@ |
179 | 190 |
|
180 | 191 | public bool isApproved(string User, string Host, string command)
|
181 | 192 | {
|
182 | | - user current = getUser(User);
|
| 193 | + user current = getUser(User + "!@" + Host);
|
183 | 194 | if (current.level == "null")
|
184 | 195 | {
|
185 | 196 | return false;
|
— | — | @@ -204,9 +215,13 @@ |
205 | 216 | {
|
206 | 217 | return matchLevel(1, current.level);
|
207 | 218 | }
|
| 219 | + if (command == "admin")
|
| 220 | + {
|
| 221 | + return matchLevel(2, current.level);
|
| 222 | + }
|
208 | 223 | if (command == "trustadd")
|
209 | 224 | {
|
210 | | - return matchLevel(1, current.level);
|
| 225 | + return matchLevel(1, current.level);
|
211 | 226 | }
|
212 | 227 | if (command == "trustdel")
|
213 | 228 | {
|
— | — | @@ -218,30 +233,153 @@ |
219 | 234 |
|
220 | 235 | public class dictionary
|
221 | 236 | {
|
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;
|
223 | 253 | public void Load(string channel)
|
224 | | - {
|
225 | | - string file = channel + ".db";
|
| 254 | + {
|
| 255 | + Channel = channel;
|
| 256 | + string file = Channel + ".db";
|
226 | 257 | if (!System.IO.File.Exists(file))
|
227 | 258 | {
|
228 | 259 | // Create db
|
229 | 260 | System.IO.File.WriteAllText(file, "");
|
230 | 261 | }
|
| 262 | +
|
231 | 263 | }
|
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 | + }
|
235 | 280 | }
|
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;
|
239 | 328 | }
|
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 | + }
|
243 | 358 | }
|
| 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 | + }
|
244 | 377 | }
|
245 | 378 |
|
| 379 | + public static void handleException(Exception ex, string chan)
|
| 380 | + {
|
| 381 | + Message("DEBUG Exception: " + ex.Message + " I feel crushed, uh :|", chan);
|
| 382 | + }
|
| 383 | +
|
246 | 384 | public static config.channel getChannel(string name)
|
247 | 385 | {
|
248 | 386 | foreach (config.channel current in config.channels)
|
— | — | @@ -263,44 +401,66 @@ |
264 | 402 |
|
265 | 403 | public static int modifyRights(string message, config.channel channel, string user, string host)
|
266 | 404 | {
|
267 | | - if (message.StartsWith("@trustadd"))
|
| 405 | + try
|
268 | 406 | {
|
269 | | - string[] rights_info = message.Split(' ');
|
270 | | - if (channel.Users.isApproved(user, host, "trustadd"))
|
| 407 | + if (message.StartsWith("@trustadd"))
|
271 | 408 | {
|
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 | + }
|
273 | 434 | }
|
274 | | - else
|
| 435 | + if (message.StartsWith("@trusted"))
|
275 | 436 | {
|
276 | | - Message("You are not autorized to perform this, sorry", channel.name);
|
277 | | -
|
| 437 | + channel.Users.listAll();
|
278 | 438 | }
|
| 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 | + }
|
279 | 452 | }
|
280 | | - if (message.StartsWith("@trusted"))
|
| 453 | + catch (Exception b)
|
281 | 454 | {
|
282 | | -
|
| 455 | + handleException(b, channel.name);
|
283 | 456 | }
|
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 | | - }
|
297 | 457 | return 0;
|
298 | 458 | }
|
299 | 459 |
|
300 | 460 | public static void chanLog(string message, config.channel channel, string user, string host)
|
301 | 461 | {
|
302 | 462 | 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;
|
305 | 465 | System.IO.File.AppendAllText(channel.log, log);
|
306 | 466 | }
|
307 | 467 | }
|
— | — | @@ -310,11 +470,14 @@ |
311 | 471 | config.channel curr = getChannel(channel);
|
312 | 472 | if (curr != null)
|
313 | 473 | {
|
| 474 | + curr.Keys.print(message);
|
314 | 475 | chanLog(message, curr, nick, host);
|
315 | 476 | modifyRights(message, curr, nick, host);
|
316 | 477 | }
|
317 | | -
|
318 | 478 |
|
| 479 | +
|
| 480 | +
|
| 481 | +
|
319 | 482 | return false;
|
320 | 483 | }
|
321 | 484 |
|
— | — | @@ -360,7 +523,14 @@ |
361 | 524 | channel = text.Substring(text.IndexOf("#"), text.IndexOf(" ", text.IndexOf("#")) - text.IndexOf("#"));
|
362 | 525 | message = text.Substring(text.IndexOf("PRIVMSG"));
|
363 | 526 | 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 | + }
|
365 | 535 | }
|
366 | 536 | else
|
367 | 537 | {
|
— | — | @@ -368,7 +538,6 @@ |
369 | 539 | }
|
370 | 540 | }
|
371 | 541 | }
|
372 | | - Console.Write(text + "\n\n\n");
|
373 | 542 | System.Threading.Thread.Sleep(50);
|
374 | 543 | }
|
375 | 544 | }
|