Index: trunk/tools/wmib/Program.cs |
— | — | @@ -1,552 +1,552 @@ |
2 | | -//This program is free software: you can redistribute it and/or modify
|
3 | | -//it under the terms of the GNU General Public License as published by
|
4 | | -//the Free Software Foundation, either version 3 of the License, or
|
5 | | -//(at your option) any later version.
|
6 | | -
|
7 | | -//This program is distributed in the hope that it will be useful,
|
8 | | -//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9 | | -//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10 | | -//GNU General Public License for more details.
|
11 | | -
|
12 | | -// Created by Petr Bena benapetr@gmail.com
|
13 | | -
|
14 | | -using System;
|
15 | | -using System.Collections.Generic;
|
16 | | -using System.Text;
|
17 | | -using System.Net;
|
18 | | -
|
19 | | -
|
20 | | -namespace wmib
|
21 | | -{
|
22 | | - class Program
|
23 | | - {
|
24 | | - public static bool Log(string msg )
|
25 | | - {
|
26 | | - Console.WriteLine("LOG: " + msg);
|
27 | | - return false;
|
28 | | - }
|
29 | | - static void Main(string[] args)
|
30 | | - {
|
31 | | - Log("Connecting");
|
32 | | - irc.Connect();
|
33 | | - }
|
34 | | - }
|
35 | | -
|
36 | | - public static class config
|
37 | | - {
|
38 | | - public class channel
|
39 | | - {
|
40 | | - public string name;
|
41 | | - public bool logged;
|
42 | | - public string log;
|
43 | | - public irc.dictionary Keys = new irc.dictionary();
|
44 | | - public irc.trust Users;
|
45 | | - public channel(string Name)
|
46 | | - {
|
47 | | - logged = true;
|
48 | | - name = Name;
|
49 | | - log = Name + ".txt";
|
50 | | - Keys.Load(name);
|
51 | | - Users = new irc.trust(name);
|
52 | | - }
|
53 | | - }
|
54 | | - /// <summary>
|
55 | | - /// Network
|
56 | | - /// </summary>
|
57 | | - public static string network = "irc.freenode.net";
|
58 | | - public static string username = "wm-bot";
|
59 | | - /// <summary>
|
60 | | - ///
|
61 | | - /// </summary>
|
62 | | - public static string version = "wikimedia bot v. 1.0.1";
|
63 | | - /// <summary>
|
64 | | - /// User name
|
65 | | - /// </summary>
|
66 | | - public static string name = "wm-bot";
|
67 | | - /// <summary>
|
68 | | - /// Channels
|
69 | | - /// </summary>
|
70 | | - public static channel[] channels = { new channel("#wikimedia-labs"), new channel( "#wikimedia-test-bots") };
|
71 | | - }
|
72 | | -
|
73 | | - public static class irc
|
74 | | - {
|
75 | | - private static System.Net.Sockets.NetworkStream data;
|
76 | | - public static System.IO.StreamReader rd;
|
77 | | - private static System.IO.StreamWriter wd;
|
78 | | - private static List<user> User = new List<user>();
|
79 | | -
|
80 | | - public class user
|
81 | | - {
|
82 | | - public user(string level, string name)
|
83 | | - {
|
84 | | - this.level = level;
|
85 | | - this.name = name;
|
86 | | - }
|
87 | | - public string name;
|
88 | | - public string level;
|
89 | | - }
|
90 | | -
|
91 | | - public class trust
|
92 | | - {
|
93 | | - private List<user> Users = new List<user>();
|
94 | | - public string _Channel;
|
95 | | - public string File;
|
96 | | - public trust(string channel)
|
97 | | - {
|
98 | | - // Load
|
99 | | - File = channel + "_user";
|
100 | | - if (!System.IO.File.Exists(File))
|
101 | | - {
|
102 | | - // Create db
|
103 | | - Program.Log("Creating user file for " + channel);
|
104 | | - System.IO.File.WriteAllText(File, "");
|
105 | | - }
|
106 | | - string[] db = System.IO.File.ReadAllLines(channel + "_user");
|
107 | | - this._Channel = channel;
|
108 | | - foreach (string x in db)
|
109 | | - {
|
110 | | - if (x.Contains("|"))
|
111 | | - {
|
112 | | - string[] info = x.Split('|');
|
113 | | - string level = info[1];
|
114 | | - string name = info[0];
|
115 | | - Users.Add(new user(level, name));
|
116 | | - }
|
117 | | - }
|
118 | | - }
|
119 | | - public bool Save()
|
120 | | - {
|
121 | | - System.IO.File.WriteAllText(File, "");
|
122 | | - foreach (user u in Users)
|
123 | | - {
|
124 | | - System.IO.File.AppendAllText(File, u.name + "|" + u.level + "\n");
|
125 | | - }
|
126 | | - return true;
|
127 | | - }
|
128 | | -
|
129 | | - public bool addUser(string level, string user)
|
130 | | - {
|
131 | | - Users.Add(new user(level, user));
|
132 | | - Save();
|
133 | | - return true;
|
134 | | - }
|
135 | | -
|
136 | | - public bool delUser(string user)
|
137 | | - {
|
138 | | - foreach (user u in Users)
|
139 | | - {
|
140 | | - if (u.name == user)
|
141 | | - {
|
142 | | - if (u.level == "admin")
|
143 | | - {
|
144 | | - Message("This user is admin which can't be deleted from db, sorry", _Channel);
|
145 | | - return true;
|
146 | | - }
|
147 | | - Users.Remove(u);
|
148 | | - }
|
149 | | - }
|
150 | | - Save();
|
151 | | - return true;
|
152 | | - }
|
153 | | -
|
154 | | - public user getUser(string user)
|
155 | | - {
|
156 | | - foreach (user b in Users)
|
157 | | - {
|
158 | | - System.Text.RegularExpressions.Regex id = new System.Text.RegularExpressions.Regex(b.name);
|
159 | | - if (id.Match(user).Success)
|
160 | | - {
|
161 | | - return b;
|
162 | | - }
|
163 | | - }
|
164 | | - return new user("null", "");
|
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 | | -
|
177 | | - public bool matchLevel(int level, string rights)
|
178 | | - {
|
179 | | - if (level == 2)
|
180 | | - {
|
181 | | - return (rights == "admin");
|
182 | | - }
|
183 | | - if (level == 1)
|
184 | | - {
|
185 | | - return (rights == "trusted" || rights == "admin");
|
186 | | -
|
187 | | - }
|
188 | | - return false;
|
189 | | - }
|
190 | | -
|
191 | | - public bool isApproved(string User, string Host, string command)
|
192 | | - {
|
193 | | - user current = getUser(User + "!@" + Host);
|
194 | | - if (current.level == "null")
|
195 | | - {
|
196 | | - return false;
|
197 | | - }
|
198 | | -
|
199 | | - if (command == "alias_key")
|
200 | | - {
|
201 | | - return matchLevel(1, current.level);
|
202 | | - }
|
203 | | - if (command == "new_key")
|
204 | | - {
|
205 | | - return matchLevel(1, current.level);
|
206 | | - }
|
207 | | - if (command == "shutdown")
|
208 | | - {
|
209 | | - return matchLevel(1, current.level);
|
210 | | - }
|
211 | | - if (command == "delete_key")
|
212 | | - {
|
213 | | - return matchLevel(1, current.level);
|
214 | | - }
|
215 | | - if (command == "trust")
|
216 | | - {
|
217 | | - return matchLevel(1, current.level);
|
218 | | - }
|
219 | | - if (command == "admin")
|
220 | | - {
|
221 | | - return matchLevel(2, current.level);
|
222 | | - }
|
223 | | - if (command == "trustadd")
|
224 | | - {
|
225 | | - return matchLevel(1, current.level);
|
226 | | - }
|
227 | | - if (command == "trustdel")
|
228 | | - {
|
229 | | - return matchLevel(1, current.level);
|
230 | | - }
|
231 | | - return false;
|
232 | | - }
|
233 | | - }
|
234 | | -
|
235 | | - public class dictionary
|
236 | | - {
|
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;
|
253 | | - public void Load(string channel)
|
254 | | - {
|
255 | | - Channel = channel;
|
256 | | - string file = Channel + ".db";
|
257 | | - if (!System.IO.File.Exists(file))
|
258 | | - {
|
259 | | - // Create db
|
260 | | - System.IO.File.WriteAllText(file, "");
|
261 | | - }
|
262 | | -
|
263 | | - }
|
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 | | - }
|
280 | | - }
|
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;
|
328 | | - }
|
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 | | - }
|
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 | | - }
|
377 | | - }
|
378 | | -
|
379 | | - public static void handleException(Exception ex, string chan)
|
380 | | - {
|
381 | | - Message("DEBUG Exception: " + ex.Message + " I feel crushed, uh :|", chan);
|
382 | | - }
|
383 | | -
|
384 | | - public static config.channel getChannel(string name)
|
385 | | - {
|
386 | | - foreach (config.channel current in config.channels)
|
387 | | - {
|
388 | | - if (current.name == name)
|
389 | | - {
|
390 | | - return current;
|
391 | | - }
|
392 | | - }
|
393 | | - return null;
|
394 | | - }
|
395 | | -
|
396 | | - public static bool Message(string message, string channel)
|
397 | | - {
|
398 | | - wd.WriteLine("PRIVMSG " + channel + " :" + message);
|
399 | | - wd.Flush();
|
400 | | - return true;
|
401 | | - }
|
402 | | -
|
403 | | - public static int modifyRights(string message, config.channel channel, string user, string host)
|
404 | | - {
|
405 | | - try
|
406 | | - {
|
407 | | - if (message.StartsWith("@trustadd"))
|
408 | | - {
|
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 | | - }
|
434 | | - }
|
435 | | - if (message.StartsWith("@trusted"))
|
436 | | - {
|
437 | | - channel.Users.listAll();
|
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 | | - }
|
452 | | - }
|
453 | | - catch (Exception b)
|
454 | | - {
|
455 | | - handleException(b, channel.name);
|
456 | | - }
|
457 | | - return 0;
|
458 | | - }
|
459 | | -
|
460 | | - public static void chanLog(string message, config.channel channel, string user, string host)
|
461 | | - {
|
462 | | - if (channel.logged)
|
463 | | - {
|
464 | | - string log = "\n" + "[" + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + System.DateTime.Now.Second + "] " + "<" + user + "> " + message;
|
465 | | - System.IO.File.AppendAllText(channel.log, log);
|
466 | | - }
|
467 | | - }
|
468 | | -
|
469 | | - public static bool getMessage(string channel, string nick, string host, string message)
|
470 | | - {
|
471 | | - config.channel curr = getChannel(channel);
|
472 | | - if (curr != null)
|
473 | | - {
|
474 | | - curr.Keys.print(message);
|
475 | | - chanLog(message, curr, nick, host);
|
476 | | - modifyRights(message, curr, nick, host);
|
477 | | - }
|
478 | | -
|
479 | | -
|
480 | | -
|
481 | | -
|
482 | | - return false;
|
483 | | - }
|
484 | | -
|
485 | | - public static int Connect()
|
486 | | - {
|
487 | | - data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream();
|
488 | | - rd = new System.IO.StreamReader(data, System.Text.Encoding.UTF8);
|
489 | | - wd = new System.IO.StreamWriter(data);
|
490 | | -
|
491 | | - wd.WriteLine("USER " + config.name + " 8 * :" + config.name);
|
492 | | - wd.WriteLine("NICK " + config.username);
|
493 | | -
|
494 | | - //System.Threading.Thread.Sleep(2000);
|
495 | | -
|
496 | | - foreach (config.channel ch in config.channels)
|
497 | | - {
|
498 | | - wd.WriteLine("JOIN " + ch.name);
|
499 | | - }
|
500 | | - wd.Flush();
|
501 | | - string text = "";
|
502 | | - string nick = "";
|
503 | | - string host = "";
|
504 | | - string message = "";
|
505 | | - string channel = "";
|
506 | | -
|
507 | | - while (true)
|
508 | | - {
|
509 | | - while (!rd.EndOfStream)
|
510 | | - {
|
511 | | - text = rd.ReadLine();
|
512 | | - if (text.StartsWith(":"))
|
513 | | - {
|
514 | | - if (text.Contains("PRIVMSG"))
|
515 | | - {
|
516 | | - // we got a message here :)
|
517 | | - if (text.Contains("!") && text.Contains("@"))
|
518 | | - {
|
519 | | - nick = text.Substring(1, text.IndexOf("!") - 1);
|
520 | | - host = text.Substring(text.IndexOf("@") + 1, text.IndexOf(" ", text.IndexOf("@")) - 1 - text.IndexOf("@"));
|
521 | | - }
|
522 | | - if (text.Substring(text.IndexOf("PRIVMSG ", text.IndexOf(" "), text.IndexOf("PRIVMSG "))).Contains("#"))
|
523 | | - {
|
524 | | - channel = text.Substring(text.IndexOf("#"), text.IndexOf(" ", text.IndexOf("#")) - text.IndexOf("#"));
|
525 | | - message = text.Substring(text.IndexOf("PRIVMSG"));
|
526 | | - message = message.Substring(message.IndexOf(":") + 1);
|
527 | | - if (message.Contains("ACTION"))
|
528 | | - {
|
529 | | -
|
530 | | - }
|
531 | | - else
|
532 | | - {
|
533 | | - getMessage(channel, nick, host, message);
|
534 | | - }
|
535 | | - }
|
536 | | - else
|
537 | | - {
|
538 | | - // private message
|
539 | | - }
|
540 | | - }
|
541 | | - }
|
542 | | - System.Threading.Thread.Sleep(50);
|
543 | | - }
|
544 | | - }
|
545 | | - return 0;
|
546 | | - }
|
547 | | - public static int Disconnect()
|
548 | | - {
|
549 | | - wd.Flush();
|
550 | | - return 0;
|
551 | | - }
|
552 | | - }
|
553 | | -}
|
| 2 | +//This program is free software: you can redistribute it and/or modify |
| 3 | +//it under the terms of the GNU General Public License as published by |
| 4 | +//the Free Software Foundation, either version 3 of the License, or |
| 5 | +//(at your option) any later version. |
| 6 | + |
| 7 | +//This program is distributed in the hope that it will be useful, |
| 8 | +//but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | +//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | +//GNU General Public License for more details. |
| 11 | + |
| 12 | +// Created by Petr Bena benapetr@gmail.com |
| 13 | + |
| 14 | +using System; |
| 15 | +using System.Collections.Generic; |
| 16 | +using System.Text; |
| 17 | +using System.Net; |
| 18 | + |
| 19 | + |
| 20 | +namespace wmib |
| 21 | +{ |
| 22 | + class Program |
| 23 | + { |
| 24 | + public static bool Log(string msg ) |
| 25 | + { |
| 26 | + Console.WriteLine("LOG: " + msg); |
| 27 | + return false; |
| 28 | + } |
| 29 | + static void Main(string[] args) |
| 30 | + { |
| 31 | + Log("Connecting"); |
| 32 | + irc.Connect(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static class config |
| 37 | + { |
| 38 | + public class channel |
| 39 | + { |
| 40 | + public string name; |
| 41 | + public bool logged; |
| 42 | + public string log; |
| 43 | + public irc.dictionary Keys = new irc.dictionary(); |
| 44 | + public irc.trust Users; |
| 45 | + public channel(string Name) |
| 46 | + { |
| 47 | + logged = true; |
| 48 | + name = Name; |
| 49 | + log = Name + ".txt"; |
| 50 | + Keys.Load(name); |
| 51 | + Users = new irc.trust(name); |
| 52 | + } |
| 53 | + } |
| 54 | + /// <summary> |
| 55 | + /// Network |
| 56 | + /// </summary> |
| 57 | + public static string network = "irc.freenode.net"; |
| 58 | + public static string username = "wm-bot"; |
| 59 | + /// <summary> |
| 60 | + /// |
| 61 | + /// </summary> |
| 62 | + public static string version = "wikimedia bot v. 1.0.1"; |
| 63 | + /// <summary> |
| 64 | + /// User name |
| 65 | + /// </summary> |
| 66 | + public static string name = "wm-bot"; |
| 67 | + /// <summary> |
| 68 | + /// Channels |
| 69 | + /// </summary> |
| 70 | + public static channel[] channels = { new channel("#wikimedia-labs"), new channel( "#wikimedia-test-bots") }; |
| 71 | + } |
| 72 | + |
| 73 | + public static class irc |
| 74 | + { |
| 75 | + private static System.Net.Sockets.NetworkStream data; |
| 76 | + public static System.IO.StreamReader rd; |
| 77 | + private static System.IO.StreamWriter wd; |
| 78 | + private static List<user> User = new List<user>(); |
| 79 | + |
| 80 | + public class user |
| 81 | + { |
| 82 | + public user(string level, string name) |
| 83 | + { |
| 84 | + this.level = level; |
| 85 | + this.name = name; |
| 86 | + } |
| 87 | + public string name; |
| 88 | + public string level; |
| 89 | + } |
| 90 | + |
| 91 | + public class trust |
| 92 | + { |
| 93 | + private List<user> Users = new List<user>(); |
| 94 | + public string _Channel; |
| 95 | + public string File; |
| 96 | + public trust(string channel) |
| 97 | + { |
| 98 | + // Load |
| 99 | + File = channel + "_user"; |
| 100 | + if (!System.IO.File.Exists(File)) |
| 101 | + { |
| 102 | + // Create db |
| 103 | + Program.Log("Creating user file for " + channel); |
| 104 | + System.IO.File.WriteAllText(File, ""); |
| 105 | + } |
| 106 | + string[] db = System.IO.File.ReadAllLines(channel + "_user"); |
| 107 | + this._Channel = channel; |
| 108 | + foreach (string x in db) |
| 109 | + { |
| 110 | + if (x.Contains("|")) |
| 111 | + { |
| 112 | + string[] info = x.Split('|'); |
| 113 | + string level = info[1]; |
| 114 | + string name = info[0]; |
| 115 | + Users.Add(new user(level, name)); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + public bool Save() |
| 120 | + { |
| 121 | + System.IO.File.WriteAllText(File, ""); |
| 122 | + foreach (user u in Users) |
| 123 | + { |
| 124 | + System.IO.File.AppendAllText(File, u.name + "|" + u.level + "\n"); |
| 125 | + } |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + public bool addUser(string level, string user) |
| 130 | + { |
| 131 | + Users.Add(new user(level, user)); |
| 132 | + Save(); |
| 133 | + return true; |
| 134 | + } |
| 135 | + |
| 136 | + public bool delUser(string user) |
| 137 | + { |
| 138 | + foreach (user u in Users) |
| 139 | + { |
| 140 | + if (u.name == user) |
| 141 | + { |
| 142 | + if (u.level == "admin") |
| 143 | + { |
| 144 | + Message("This user is admin which can't be deleted from db, sorry", _Channel); |
| 145 | + return true; |
| 146 | + } |
| 147 | + Users.Remove(u); |
| 148 | + } |
| 149 | + } |
| 150 | + Save(); |
| 151 | + return true; |
| 152 | + } |
| 153 | + |
| 154 | + public user getUser(string user) |
| 155 | + { |
| 156 | + foreach (user b in Users) |
| 157 | + { |
| 158 | + System.Text.RegularExpressions.Regex id = new System.Text.RegularExpressions.Regex(b.name); |
| 159 | + if (id.Match(user).Success) |
| 160 | + { |
| 161 | + return b; |
| 162 | + } |
| 163 | + } |
| 164 | + return new user("null", ""); |
| 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 | + |
| 177 | + public bool matchLevel(int level, string rights) |
| 178 | + { |
| 179 | + if (level == 2) |
| 180 | + { |
| 181 | + return (rights == "admin"); |
| 182 | + } |
| 183 | + if (level == 1) |
| 184 | + { |
| 185 | + return (rights == "trusted" || rights == "admin"); |
| 186 | + |
| 187 | + } |
| 188 | + return false; |
| 189 | + } |
| 190 | + |
| 191 | + public bool isApproved(string User, string Host, string command) |
| 192 | + { |
| 193 | + user current = getUser(User + "!@" + Host); |
| 194 | + if (current.level == "null") |
| 195 | + { |
| 196 | + return false; |
| 197 | + } |
| 198 | + |
| 199 | + if (command == "alias_key") |
| 200 | + { |
| 201 | + return matchLevel(1, current.level); |
| 202 | + } |
| 203 | + if (command == "new_key") |
| 204 | + { |
| 205 | + return matchLevel(1, current.level); |
| 206 | + } |
| 207 | + if (command == "shutdown") |
| 208 | + { |
| 209 | + return matchLevel(1, current.level); |
| 210 | + } |
| 211 | + if (command == "delete_key") |
| 212 | + { |
| 213 | + return matchLevel(1, current.level); |
| 214 | + } |
| 215 | + if (command == "trust") |
| 216 | + { |
| 217 | + return matchLevel(1, current.level); |
| 218 | + } |
| 219 | + if (command == "admin") |
| 220 | + { |
| 221 | + return matchLevel(2, current.level); |
| 222 | + } |
| 223 | + if (command == "trustadd") |
| 224 | + { |
| 225 | + return matchLevel(1, current.level); |
| 226 | + } |
| 227 | + if (command == "trustdel") |
| 228 | + { |
| 229 | + return matchLevel(1, current.level); |
| 230 | + } |
| 231 | + return false; |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + public class dictionary |
| 236 | + { |
| 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; |
| 253 | + public void Load(string channel) |
| 254 | + { |
| 255 | + Channel = channel; |
| 256 | + string file = Channel + ".db"; |
| 257 | + if (!System.IO.File.Exists(file)) |
| 258 | + { |
| 259 | + // Create db |
| 260 | + System.IO.File.WriteAllText(file, ""); |
| 261 | + } |
| 262 | + |
| 263 | + } |
| 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 | + } |
| 280 | + } |
| 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; |
| 328 | + } |
| 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 | + } |
| 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 | + } |
| 377 | + } |
| 378 | + |
| 379 | + public static void handleException(Exception ex, string chan) |
| 380 | + { |
| 381 | + Message("DEBUG Exception: " + ex.Message + " I feel crushed, uh :|", chan); |
| 382 | + } |
| 383 | + |
| 384 | + public static config.channel getChannel(string name) |
| 385 | + { |
| 386 | + foreach (config.channel current in config.channels) |
| 387 | + { |
| 388 | + if (current.name == name) |
| 389 | + { |
| 390 | + return current; |
| 391 | + } |
| 392 | + } |
| 393 | + return null; |
| 394 | + } |
| 395 | + |
| 396 | + public static bool Message(string message, string channel) |
| 397 | + { |
| 398 | + wd.WriteLine("PRIVMSG " + channel + " :" + message); |
| 399 | + wd.Flush(); |
| 400 | + return true; |
| 401 | + } |
| 402 | + |
| 403 | + public static int modifyRights(string message, config.channel channel, string user, string host) |
| 404 | + { |
| 405 | + try |
| 406 | + { |
| 407 | + if (message.StartsWith("@trustadd")) |
| 408 | + { |
| 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 | + } |
| 434 | + } |
| 435 | + if (message.StartsWith("@trusted")) |
| 436 | + { |
| 437 | + channel.Users.listAll(); |
| 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 | + } |
| 452 | + } |
| 453 | + catch (Exception b) |
| 454 | + { |
| 455 | + handleException(b, channel.name); |
| 456 | + } |
| 457 | + return 0; |
| 458 | + } |
| 459 | + |
| 460 | + public static void chanLog(string message, config.channel channel, string user, string host) |
| 461 | + { |
| 462 | + if (channel.logged) |
| 463 | + { |
| 464 | + string log = "\n" + "[" + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + System.DateTime.Now.Second + "] " + "<" + user + "> " + message; |
| 465 | + System.IO.File.AppendAllText(channel.log, log); |
| 466 | + } |
| 467 | + } |
| 468 | + |
| 469 | + public static bool getMessage(string channel, string nick, string host, string message) |
| 470 | + { |
| 471 | + config.channel curr = getChannel(channel); |
| 472 | + if (curr != null) |
| 473 | + { |
| 474 | + curr.Keys.print(message); |
| 475 | + chanLog(message, curr, nick, host); |
| 476 | + modifyRights(message, curr, nick, host); |
| 477 | + } |
| 478 | + |
| 479 | + |
| 480 | + |
| 481 | + |
| 482 | + return false; |
| 483 | + } |
| 484 | + |
| 485 | + public static int Connect() |
| 486 | + { |
| 487 | + data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream(); |
| 488 | + rd = new System.IO.StreamReader(data, System.Text.Encoding.UTF8); |
| 489 | + wd = new System.IO.StreamWriter(data); |
| 490 | + |
| 491 | + wd.WriteLine("USER " + config.name + " 8 * :" + config.name); |
| 492 | + wd.WriteLine("NICK " + config.username); |
| 493 | + |
| 494 | + //System.Threading.Thread.Sleep(2000); |
| 495 | + |
| 496 | + foreach (config.channel ch in config.channels) |
| 497 | + { |
| 498 | + wd.WriteLine("JOIN " + ch.name); |
| 499 | + } |
| 500 | + wd.Flush(); |
| 501 | + string text = ""; |
| 502 | + string nick = ""; |
| 503 | + string host = ""; |
| 504 | + string message = ""; |
| 505 | + string channel = ""; |
| 506 | + |
| 507 | + while (true) |
| 508 | + { |
| 509 | + while (!rd.EndOfStream) |
| 510 | + { |
| 511 | + text = rd.ReadLine(); |
| 512 | + if (text.StartsWith(":")) |
| 513 | + { |
| 514 | + if (text.Contains("PRIVMSG")) |
| 515 | + { |
| 516 | + // we got a message here :) |
| 517 | + if (text.Contains("!") && text.Contains("@")) |
| 518 | + { |
| 519 | + nick = text.Substring(1, text.IndexOf("!") - 1); |
| 520 | + host = text.Substring(text.IndexOf("@") + 1, text.IndexOf(" ", text.IndexOf("@")) - 1 - text.IndexOf("@")); |
| 521 | + } |
| 522 | + if (text.Substring(text.IndexOf("PRIVMSG ", text.IndexOf(" "), text.IndexOf("PRIVMSG "))).Contains("#")) |
| 523 | + { |
| 524 | + channel = text.Substring(text.IndexOf("#"), text.IndexOf(" ", text.IndexOf("#")) - text.IndexOf("#")); |
| 525 | + message = text.Substring(text.IndexOf("PRIVMSG")); |
| 526 | + message = message.Substring(message.IndexOf(":") + 1); |
| 527 | + if (message.Contains("ACTION")) |
| 528 | + { |
| 529 | + |
| 530 | + } |
| 531 | + else |
| 532 | + { |
| 533 | + getMessage(channel, nick, host, message); |
| 534 | + } |
| 535 | + } |
| 536 | + else |
| 537 | + { |
| 538 | + // private message |
| 539 | + } |
| 540 | + } |
| 541 | + } |
| 542 | + System.Threading.Thread.Sleep(50); |
| 543 | + } |
| 544 | + } |
| 545 | + return 0; |
| 546 | + } |
| 547 | + public static int Disconnect() |
| 548 | + { |
| 549 | + wd.Flush(); |
| 550 | + return 0; |
| 551 | + } |
| 552 | + } |
| 553 | +} |
Property changes on: trunk/tools/wmib/Program.cs |
___________________________________________________________________ |
Added: svn:eol-style |
554 | 554 | + native |