Index: trunk/tools/wmib/Infobot.cs |
— | — | @@ -0,0 +1,502 @@ |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Threading; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | +using System.IO; |
| 7 | +using System.Text; |
| 8 | + |
| 9 | +namespace wmib |
| 10 | +{ |
| 11 | + public class dictionary |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Data file |
| 15 | + /// </summary> |
| 16 | + public string datafile = ""; |
| 17 | + |
| 18 | + // if we need to update dump |
| 19 | + public bool update = true; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Locked |
| 23 | + /// </summary> |
| 24 | + public bool locked = false; |
| 25 | + |
| 26 | + public class item |
| 27 | + { |
| 28 | + /// <summary> |
| 29 | + /// Text |
| 30 | + /// </summary> |
| 31 | + public string text; |
| 32 | + /// <summary> |
| 33 | + /// Key |
| 34 | + /// </summary> |
| 35 | + public string key; |
| 36 | + |
| 37 | + public string user; |
| 38 | + |
| 39 | + public string locked; |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Constructor |
| 43 | + /// </summary> |
| 44 | + /// <param name="Key">Key</param> |
| 45 | + /// <param name="Text">Text of the key</param> |
| 46 | + /// <param name="User">User who created the key</param> |
| 47 | + /// <param name="Lock">If key is locked or not</param> |
| 48 | + public item(string Key, string Text, string User, string Lock = "false") |
| 49 | + { |
| 50 | + text = Text; |
| 51 | + key = Key; |
| 52 | + locked = Lock; |
| 53 | + user = User; |
| 54 | + } |
| 55 | + } |
| 56 | + public class staticalias |
| 57 | + { |
| 58 | + /// <summary> |
| 59 | + /// Name |
| 60 | + /// </summary> |
| 61 | + public string Name; |
| 62 | + /// <summary> |
| 63 | + /// Key |
| 64 | + /// </summary> |
| 65 | + public string Key; |
| 66 | + /// <summary> |
| 67 | + /// Constructor |
| 68 | + /// </summary> |
| 69 | + /// <param name="name">Alias</param> |
| 70 | + /// <param name="key">Key</param> |
| 71 | + public staticalias(string name, string key) |
| 72 | + { |
| 73 | + Name = name; |
| 74 | + Key = key; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// List of all items in class |
| 80 | + /// </summary> |
| 81 | + public List<item> text = new List<item>(); |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// List of all aliases we want to use |
| 85 | + /// </summary> |
| 86 | + public List<staticalias> Alias = new List<staticalias>(); |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Channel name |
| 90 | + /// </summary> |
| 91 | + public string Channel; |
| 92 | + |
| 93 | + private bool running; |
| 94 | + |
| 95 | + private string search_key; |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Load it |
| 99 | + /// </summary> |
| 100 | + public void Load() |
| 101 | + { |
| 102 | + text.Clear(); |
| 103 | + if (!File.Exists(datafile)) |
| 104 | + { |
| 105 | + // Create db |
| 106 | + File.WriteAllText(datafile, ""); |
| 107 | + } |
| 108 | + |
| 109 | + string[] db = File.ReadAllLines(datafile); |
| 110 | + foreach (string x in db) |
| 111 | + { |
| 112 | + if (x.Contains(config.separator)) |
| 113 | + { |
| 114 | + string[] info = x.Split(Char.Parse(config.separator)); |
| 115 | + string type = info[2]; |
| 116 | + string value = info[1]; |
| 117 | + string name = info[0]; |
| 118 | + if (type == "key") |
| 119 | + { |
| 120 | + string locked = info[3]; |
| 121 | + text.Add(new item(name, value, "", locked)); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + Alias.Add(new staticalias(name, value)); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Constructor |
| 133 | + /// </summary> |
| 134 | + /// <param name="database"></param> |
| 135 | + /// <param name="channel"></param> |
| 136 | + public dictionary(string database, string channel) |
| 137 | + { |
| 138 | + datafile = database; |
| 139 | + Channel = channel; |
| 140 | + Load(); |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Save to a file |
| 145 | + /// </summary> |
| 146 | + public void Save() |
| 147 | + { |
| 148 | + update = true; |
| 149 | + try |
| 150 | + { |
| 151 | + File.WriteAllText(datafile, ""); |
| 152 | + foreach (staticalias key in Alias) |
| 153 | + { |
| 154 | + File.AppendAllText(datafile, key.Name + config.separator + key.Key + config.separator + "alias" + "\n"); |
| 155 | + } |
| 156 | + foreach (item key in text) |
| 157 | + { |
| 158 | + File.AppendAllText(datafile, key.key + config.separator + key.text + config.separator + "key" + config.separator + key.locked + config.separator + key.user + "\n"); |
| 159 | + } |
| 160 | + } |
| 161 | + catch (Exception b) |
| 162 | + { |
| 163 | + irc.handleException(b, Channel); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// Get value of key |
| 169 | + /// </summary> |
| 170 | + /// <param name="key">Key</param> |
| 171 | + /// <returns></returns> |
| 172 | + public string getValue(string key) |
| 173 | + { |
| 174 | + foreach (item data in text) |
| 175 | + { |
| 176 | + if (data.key == key) |
| 177 | + { |
| 178 | + return irc.decode(data.text); |
| 179 | + } |
| 180 | + } |
| 181 | + return ""; |
| 182 | + } |
| 183 | + |
| 184 | + /// <summary> |
| 185 | + /// Print a value to channel if found this message doesn't need to be a valid command |
| 186 | + /// </summary> |
| 187 | + /// <param name="name">Name</param> |
| 188 | + /// <param name="user">User</param> |
| 189 | + /// <param name="chan">Channel</param> |
| 190 | + /// <param name="host">Host name</param> |
| 191 | + /// <returns></returns> |
| 192 | + public bool print(string name, string user, config.channel chan, string host) |
| 193 | + { |
| 194 | + if (!name.StartsWith("!")) |
| 195 | + { |
| 196 | + return true; |
| 197 | + } |
| 198 | + name = name.Substring(1); |
| 199 | + if (name.Contains(" ")) |
| 200 | + { |
| 201 | + string[] parm = name.Split(' '); |
| 202 | + if (parm[1] == "is") |
| 203 | + { |
| 204 | + config.channel _Chan = irc.getChannel(Channel); |
| 205 | + if (chan.Users.isApproved(user, host, "info")) |
| 206 | + { |
| 207 | + if (parm.Length < 3) |
| 208 | + { |
| 209 | + irc.Message("It would be cool to give me also a text of key", Channel); |
| 210 | + return true; |
| 211 | + } |
| 212 | + string key = name.Substring(name.IndexOf(" is") + 4); |
| 213 | + setKey(key, parm[0], ""); |
| 214 | + } |
| 215 | + else |
| 216 | + { |
| 217 | + irc.Message("You are not autorized to perform this, sorry", Channel); |
| 218 | + } |
| 219 | + return false; |
| 220 | + } |
| 221 | + if (parm[1] == "alias") |
| 222 | + { |
| 223 | + config.channel _Chan = irc.getChannel(Channel); |
| 224 | + if (chan.Users.isApproved(user, host, "info")) |
| 225 | + { |
| 226 | + if (parm.Length < 3) |
| 227 | + { |
| 228 | + irc.Message("It would be cool to give me also a name of key", Channel); |
| 229 | + return true; |
| 230 | + } |
| 231 | + this.aliasKey(name.Substring(name.IndexOf(" alias") + 7), parm[0], ""); |
| 232 | + } |
| 233 | + else |
| 234 | + { |
| 235 | + irc.Message("You are not autorized to perform this, sorry", Channel); |
| 236 | + } |
| 237 | + return false; |
| 238 | + } |
| 239 | + if (parm[1] == "unalias") |
| 240 | + { |
| 241 | + config.channel _Chan = irc.getChannel(Channel); |
| 242 | + if (chan.Users.isApproved(user, host, "info")) |
| 243 | + { |
| 244 | + foreach (staticalias b in Alias) |
| 245 | + { |
| 246 | + if (b.Name == parm[0]) |
| 247 | + { |
| 248 | + Alias.Remove(b); |
| 249 | + irc.Message("Alias removed", Channel); |
| 250 | + Save(); |
| 251 | + return false; |
| 252 | + } |
| 253 | + } |
| 254 | + return false; |
| 255 | + } |
| 256 | + else |
| 257 | + { |
| 258 | + irc.Message("You are not autorized to perform this, sorry", Channel); |
| 259 | + } |
| 260 | + return false; |
| 261 | + } |
| 262 | + if (parm[1] == "del") |
| 263 | + { |
| 264 | + if (chan.Users.isApproved(user, host, "info")) |
| 265 | + { |
| 266 | + rmKey(parm[0], ""); |
| 267 | + } |
| 268 | + else |
| 269 | + { |
| 270 | + irc.Message("You are not autorized to perform this, sorry", Channel); |
| 271 | + } |
| 272 | + return false; |
| 273 | + } |
| 274 | + } |
| 275 | + string User = ""; |
| 276 | + if (name.Contains("|")) |
| 277 | + { |
| 278 | + User = name.Substring(name.IndexOf("|")); |
| 279 | + User = User.Replace("|", ""); |
| 280 | + User = User.Replace(" ", ""); |
| 281 | + name = name.Substring(0, name.IndexOf("|")); |
| 282 | + } |
| 283 | + string[] p = name.Split(' '); |
| 284 | + int parameters = p.Length; |
| 285 | + string keyv = getValue(p[0]); |
| 286 | + if (keyv != "") |
| 287 | + { |
| 288 | + if (parameters > 1) |
| 289 | + { |
| 290 | + int curr = 1; |
| 291 | + while (parameters > curr) |
| 292 | + { |
| 293 | + keyv = keyv.Replace("$" + curr.ToString(), p[curr]); |
| 294 | + curr++; |
| 295 | + } |
| 296 | + } |
| 297 | + if (User == "") |
| 298 | + { |
| 299 | + irc.SlowQueue.DeliverMessage(keyv, Channel); |
| 300 | + } |
| 301 | + else |
| 302 | + { |
| 303 | + irc.SlowQueue.DeliverMessage(User + ": " + keyv, Channel); |
| 304 | + } |
| 305 | + return true; |
| 306 | + } |
| 307 | + foreach (staticalias b in Alias) |
| 308 | + { |
| 309 | + if (b.Name == p[0]) |
| 310 | + { |
| 311 | + keyv = getValue(b.Key); |
| 312 | + if (keyv != "") |
| 313 | + { |
| 314 | + if (parameters > 1) |
| 315 | + { |
| 316 | + int curr = 1; |
| 317 | + while (parameters > curr) |
| 318 | + { |
| 319 | + keyv = keyv.Replace("$" + curr.ToString(), p[curr]); |
| 320 | + curr++; |
| 321 | + } |
| 322 | + } |
| 323 | + if (User == "") |
| 324 | + { |
| 325 | + irc.SlowQueue.DeliverMessage(keyv, Channel); |
| 326 | + } |
| 327 | + else |
| 328 | + { |
| 329 | + irc.SlowQueue.DeliverMessage(User + ":" + keyv, Channel); |
| 330 | + } |
| 331 | + return true; |
| 332 | + } |
| 333 | + } |
| 334 | + } |
| 335 | + return true; |
| 336 | + } |
| 337 | + |
| 338 | + private void StartSearch() |
| 339 | + { |
| 340 | + Regex value = new Regex(search_key, RegexOptions.Compiled); |
| 341 | + string results = ""; |
| 342 | + foreach (item data in text) |
| 343 | + { |
| 344 | + if (data.key == search_key || value.Match(data.text).Success) |
| 345 | + { |
| 346 | + results = results + data.key + ", "; |
| 347 | + } |
| 348 | + } |
| 349 | + if (results == "") |
| 350 | + { |
| 351 | + irc.SlowQueue.DeliverMessage("No results found! :|", Channel); |
| 352 | + } |
| 353 | + else |
| 354 | + { |
| 355 | + irc.SlowQueue.DeliverMessage("Results: " + results, Channel); |
| 356 | + } |
| 357 | + running = false; |
| 358 | + } |
| 359 | + |
| 360 | + /// <summary> |
| 361 | + /// Search |
| 362 | + /// </summary> |
| 363 | + /// <param name="key">Key</param> |
| 364 | + /// <param name="Chan"></param> |
| 365 | + public void RSearch(string key, config.channel Chan) |
| 366 | + { |
| 367 | + if (!key.StartsWith("@regsearch")) |
| 368 | + { |
| 369 | + return; |
| 370 | + } |
| 371 | + if (!misc.IsValidRegex(key)) |
| 372 | + { |
| 373 | + irc.Message("This is pretty bad regex", Chan.name); |
| 374 | + return; |
| 375 | + } |
| 376 | + if (key.Length < 11) |
| 377 | + { |
| 378 | + irc.Message("Could you please tell me what I should search for :P", Chan.name); |
| 379 | + return; |
| 380 | + } |
| 381 | + search_key = key.Substring(11); |
| 382 | + running = true; |
| 383 | + Thread th = new Thread(StartSearch); |
| 384 | + th.Start(); |
| 385 | + int check = 1; |
| 386 | + while (running) |
| 387 | + { |
| 388 | + check++; |
| 389 | + Thread.Sleep(10); |
| 390 | + if (check > 80) |
| 391 | + { |
| 392 | + th.Abort(); |
| 393 | + irc.Message("Search took more than 800ms try a better regex", Channel); |
| 394 | + running = false; |
| 395 | + return; |
| 396 | + } |
| 397 | + } |
| 398 | + } |
| 399 | + |
| 400 | + public void Find(string key, config.channel Chan) |
| 401 | + { |
| 402 | + if (!key.StartsWith("@search")) |
| 403 | + { |
| 404 | + return; |
| 405 | + } |
| 406 | + if (key.Length < 9) |
| 407 | + { |
| 408 | + irc.Message("Could you please tell me what I should search for :P", Chan.name); |
| 409 | + return; |
| 410 | + } |
| 411 | + key = key.Substring(8); |
| 412 | + string results = ""; |
| 413 | + foreach (item data in text) |
| 414 | + { |
| 415 | + if (data.key == key || data.text.Contains(key)) |
| 416 | + { |
| 417 | + results = results + data.key + ", "; |
| 418 | + } |
| 419 | + } |
| 420 | + if (results == "") |
| 421 | + { |
| 422 | + irc.SlowQueue.DeliverMessage("No results found! :|", Chan.name); |
| 423 | + } |
| 424 | + else |
| 425 | + { |
| 426 | + irc.SlowQueue.DeliverMessage("Results: " + results, Chan.name); |
| 427 | + } |
| 428 | + } |
| 429 | + |
| 430 | + /// <summary> |
| 431 | + /// Save a new key |
| 432 | + /// </summary> |
| 433 | + /// <param name="Text">Text</param> |
| 434 | + /// <param name="key">Key</param> |
| 435 | + /// <param name="user">User who created it</param> |
| 436 | + public void setKey(string Text, string key, string user) |
| 437 | + { |
| 438 | + while (locked) |
| 439 | + { |
| 440 | + Thread.Sleep(200); |
| 441 | + } |
| 442 | + try |
| 443 | + { |
| 444 | + foreach (item data in text) |
| 445 | + { |
| 446 | + if (data.key == key) |
| 447 | + { |
| 448 | + irc.SlowQueue.DeliverMessage("Key exist!", Channel); |
| 449 | + return; |
| 450 | + } |
| 451 | + } |
| 452 | + text.Add(new item(key, irc.encode(Text), user, "false")); |
| 453 | + irc.Message("Key was added!", Channel); |
| 454 | + Save(); |
| 455 | + } |
| 456 | + catch (Exception b) |
| 457 | + { |
| 458 | + irc.handleException(b, Channel); |
| 459 | + } |
| 460 | + } |
| 461 | + |
| 462 | + /// <summary> |
| 463 | + /// Alias |
| 464 | + /// </summary> |
| 465 | + /// <param name="key">Key</param> |
| 466 | + /// <param name="al">Alias</param> |
| 467 | + /// <param name="user">User</param> |
| 468 | + public void aliasKey(string key, string al, string user) |
| 469 | + { |
| 470 | + foreach (staticalias stakey in this.Alias) |
| 471 | + { |
| 472 | + if (stakey.Name == al) |
| 473 | + { |
| 474 | + irc.SlowQueue.DeliverMessage("Alias is already existing!", Channel); |
| 475 | + return; |
| 476 | + } |
| 477 | + } |
| 478 | + this.Alias.Add(new staticalias(al, key)); |
| 479 | + irc.SlowQueue.DeliverMessage("Successfully created", Channel); |
| 480 | + Save(); |
| 481 | + } |
| 482 | + |
| 483 | + public void rmKey(string key, string user) |
| 484 | + { |
| 485 | + while (locked) |
| 486 | + { |
| 487 | + Thread.Sleep(200); |
| 488 | + } |
| 489 | + foreach (item keys in text) |
| 490 | + { |
| 491 | + if (keys.key == key) |
| 492 | + { |
| 493 | + text.Remove(keys); |
| 494 | + irc.SlowQueue.DeliverMessage("Successfully removed " + key, Channel); |
| 495 | + Save(); |
| 496 | + return; |
| 497 | + } |
| 498 | + } |
| 499 | + irc.SlowQueue.DeliverMessage("Unable to find the specified key in db", Channel); |
| 500 | + } |
| 501 | + } |
| 502 | + |
| 503 | +} |