Index: trunk/tools/wmib/Program.cs |
— | — | @@ -11,14 +11,14 @@ |
12 | 12 | // Created by Petr Bena benapetr@gmail.com |
13 | 13 | |
14 | 14 | using System; |
| 15 | +using System.IO; |
15 | 16 | using System.Collections.Generic; |
16 | | -using System.Text; |
17 | | -using System.Net; |
| 17 | +using System.Text.RegularExpressions; |
18 | 18 | |
19 | 19 | |
20 | 20 | namespace wmib |
21 | 21 | { |
22 | | - class Program |
| 22 | + internal class Program |
23 | 23 | { |
24 | 24 | public static bool Log(string msg) |
25 | 25 | { |
— | — | @@ -41,7 +41,7 @@ |
42 | 42 | public bool logged; |
43 | 43 | public string log; |
44 | 44 | public irc.dictionary Keys = new irc.dictionary(); |
45 | | - public irc.trust Users; |
| 45 | + public irc.IrcTrust Users; |
46 | 46 | |
47 | 47 | public channel(string Name) |
48 | 48 | { |
— | — | @@ -49,7 +49,7 @@ |
50 | 50 | name = Name; |
51 | 51 | log = Name + ".txt"; |
52 | 52 | Keys.Load(name); |
53 | | - Users = new irc.trust(name); |
| 53 | + Users = new irc.IrcTrust(name); |
54 | 54 | } |
55 | 55 | } |
56 | 56 | |
— | — | @@ -79,13 +79,13 @@ |
80 | 80 | public static class irc |
81 | 81 | { |
82 | 82 | private static System.Net.Sockets.NetworkStream data; |
83 | | - public static System.IO.StreamReader rd; |
84 | | - private static System.IO.StreamWriter wd; |
85 | | - private static List<user> User = new List<user>(); |
| 83 | + public static StreamReader rd; |
| 84 | + private static StreamWriter wd; |
| 85 | + //private static List<IrcUser> User = new List<IrcUser>(); |
86 | 86 | |
87 | | - public class user |
| 87 | + public class IrcUser |
88 | 88 | { |
89 | | - public user(string level, string name) |
| 89 | + public IrcUser(string level, string name) |
90 | 90 | { |
91 | 91 | this.level = level; |
92 | 92 | this.name = name; |
— | — | @@ -95,13 +95,13 @@ |
96 | 96 | public string level; |
97 | 97 | } |
98 | 98 | |
99 | | - public class trust |
| 99 | + public class IrcTrust |
100 | 100 | { |
101 | | - private List<user> Users = new List<user>(); |
| 101 | + private readonly Dictionary<string, IrcUser> Users = new Dictionary<string, IrcUser>(); |
102 | 102 | public string _Channel; |
103 | 103 | public string File; |
104 | 104 | |
105 | | - public trust(string channel) |
| 105 | + public IrcTrust(string channel) |
106 | 106 | { |
107 | 107 | // Load |
108 | 108 | File = channel + "_user"; |
— | — | @@ -112,7 +112,7 @@ |
113 | 113 | System.IO.File.WriteAllText(File, ""); |
114 | 114 | } |
115 | 115 | string[] db = System.IO.File.ReadAllLines(channel + "_user"); |
116 | | - this._Channel = channel; |
| 116 | + _Channel = channel; |
117 | 117 | foreach (string x in db) |
118 | 118 | { |
119 | 119 | if (x.Contains("|")) |
— | — | @@ -120,7 +120,7 @@ |
121 | 121 | string[] info = x.Split('|'); |
122 | 122 | string level = info[1]; |
123 | 123 | string name = info[0]; |
124 | | - Users.Add(new user(level, name)); |
| 124 | + Users.Add(name, new IrcUser(level, name)); |
125 | 125 | } |
126 | 126 | } |
127 | 127 | } |
— | — | @@ -128,57 +128,56 @@ |
129 | 129 | public bool Save() |
130 | 130 | { |
131 | 131 | System.IO.File.WriteAllText(File, ""); |
132 | | - foreach (user u in Users) |
| 132 | + foreach (KeyValuePair<string, IrcUser> u in Users) |
133 | 133 | { |
134 | | - System.IO.File.AppendAllText(File, u.name + "|" + u.level + "\n"); |
| 134 | + System.IO.File.AppendAllText(File, u.Key + "|" + u.Value.level + "\n"); |
135 | 135 | } |
136 | 136 | return true; |
137 | 137 | } |
138 | 138 | |
139 | 139 | public bool addUser(string level, string user) |
140 | 140 | { |
141 | | - Users.Add(new user(level, user)); |
| 141 | + Users.Add(user, new IrcUser(level, user)); |
142 | 142 | Save(); |
143 | 143 | return true; |
144 | 144 | } |
145 | 145 | |
146 | 146 | public bool delUser(string user) |
147 | 147 | { |
148 | | - foreach (user u in Users) |
| 148 | + IrcUser ircuser; |
| 149 | + Users.TryGetValue(user, out ircuser); |
| 150 | + if (ircuser != null) |
149 | 151 | { |
150 | | - if (u.name == user) |
| 152 | + if (ircuser.level == "admin") |
151 | 153 | { |
152 | | - if (u.level == "admin") |
153 | | - { |
154 | | - Message("This user is admin which can't be deleted from db, sorry", _Channel); |
155 | | - return true; |
156 | | - } |
157 | | - Users.Remove(u); |
| 154 | + Message("This user is admin which can't be deleted from db, sorry", _Channel); |
| 155 | + return true; |
158 | 156 | } |
| 157 | + Users.Remove(user); |
| 158 | + Save(); |
159 | 159 | } |
160 | | - Save(); |
| 160 | + |
161 | 161 | return true; |
162 | 162 | } |
163 | 163 | |
164 | | - public user getUser(string user) |
| 164 | + public IrcUser getUser(string user) |
165 | 165 | { |
166 | | - foreach (user b in Users) |
| 166 | + foreach (KeyValuePair<string, IrcUser> b in Users) |
167 | 167 | { |
168 | | - System.Text.RegularExpressions.Regex id = new System.Text.RegularExpressions.Regex(b.name); |
169 | | - if (id.Match(user).Success) |
| 168 | + if (new Regex(b.Key).Match(user).Success) |
170 | 169 | { |
171 | | - return b; |
| 170 | + return b.Value; |
172 | 171 | } |
173 | 172 | } |
174 | | - return new user("null", ""); |
| 173 | + return null; |
175 | 174 | } |
176 | 175 | |
177 | 176 | public void listAll() |
178 | 177 | { |
179 | 178 | string users_ok = ""; |
180 | | - foreach (user b in Users) |
| 179 | + foreach (KeyValuePair<string, IrcUser> b in Users) |
181 | 180 | { |
182 | | - users_ok = users_ok + " " + b.name; |
| 181 | + users_ok = users_ok + " " + b.Key; |
183 | 182 | } |
184 | 183 | Message("I trust to: " + users_ok, _Channel); |
185 | 184 | } |
— | — | @@ -199,44 +198,25 @@ |
200 | 199 | |
201 | 200 | public bool isApproved(string User, string Host, string command) |
202 | 201 | { |
203 | | - user current = getUser(User + "!@" + Host); |
204 | | - if (current.level == "null") |
| 202 | + IrcUser current = getUser(User + "!@" + Host); |
| 203 | + if (current == null) |
205 | 204 | { |
206 | 205 | return false; |
207 | 206 | } |
208 | 207 | |
209 | | - if (command == "alias_key") |
| 208 | + switch (command) |
210 | 209 | { |
211 | | - return matchLevel(1, current.level); |
| 210 | + case "alias_key": |
| 211 | + case "new_key": |
| 212 | + case "shutdown": |
| 213 | + case "delete_key": |
| 214 | + case "trust": |
| 215 | + case "trustadd": |
| 216 | + case "trustdel": |
| 217 | + return matchLevel(1, current.level); |
| 218 | + case "admin": |
| 219 | + return matchLevel(2, current.level); |
212 | 220 | } |
213 | | - if (command == "new_key") |
214 | | - { |
215 | | - return matchLevel(1, current.level); |
216 | | - } |
217 | | - if (command == "shutdown") |
218 | | - { |
219 | | - return matchLevel(1, current.level); |
220 | | - } |
221 | | - if (command == "delete_key") |
222 | | - { |
223 | | - return matchLevel(1, current.level); |
224 | | - } |
225 | | - if (command == "trust") |
226 | | - { |
227 | | - return matchLevel(1, current.level); |
228 | | - } |
229 | | - if (command == "admin") |
230 | | - { |
231 | | - return matchLevel(2, current.level); |
232 | | - } |
233 | | - if (command == "trustadd") |
234 | | - { |
235 | | - return matchLevel(1, current.level); |
236 | | - } |
237 | | - if (command == "trustdel") |
238 | | - { |
239 | | - return matchLevel(1, current.level); |
240 | | - } |
241 | 221 | return false; |
242 | 222 | } |
243 | 223 | } |
— | — | @@ -266,10 +246,10 @@ |
267 | 247 | { |
268 | 248 | Channel = channel; |
269 | 249 | string file = Channel + ".db"; |
270 | | - if (!System.IO.File.Exists(file)) |
| 250 | + if (!File.Exists(file)) |
271 | 251 | { |
272 | 252 | // Create db |
273 | | - System.IO.File.WriteAllText(file, ""); |
| 253 | + File.WriteAllText(file, ""); |
274 | 254 | } |
275 | 255 | |
276 | 256 | } |
— | — | @@ -279,10 +259,10 @@ |
280 | 260 | try |
281 | 261 | { |
282 | 262 | string file = Channel + ".db"; |
283 | | - System.IO.File.WriteAllText(file, ""); |
| 263 | + File.WriteAllText(file, ""); |
284 | 264 | foreach (item key in text) |
285 | 265 | { |
286 | | - System.IO.File.AppendAllText(file, key.key + "|" + key.text + "|" + key.locked + "|" + key.user); |
| 266 | + File.AppendAllText(file, key.key + "|" + key.text + "|" + key.locked + "|" + key.user); |
287 | 267 | } |
288 | 268 | } |
289 | 269 | catch (Exception b) |
— | — | @@ -456,7 +436,7 @@ |
457 | 437 | if (message.StartsWith("@trustdel")) |
458 | 438 | { |
459 | 439 | string[] rights_info = message.Split(' '); |
460 | | - string x = rights_info[1]; |
| 440 | + |
461 | 441 | if (channel.Users.isApproved(user, host, "trustdel")) |
462 | 442 | { |
463 | 443 | channel.Users.delUser(rights_info[1]); |
— | — | @@ -476,12 +456,10 @@ |
477 | 457 | |
478 | 458 | public static void chanLog(string message, config.channel channel, string user, string host) |
479 | 459 | { |
480 | | - if (channel.logged) |
481 | | - { |
482 | | - string log = "\n" + "[" + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + |
483 | | - System.DateTime.Now.Second + "] " + "<" + user + "> " + message; |
484 | | - System.IO.File.AppendAllText(channel.log, log); |
485 | | - } |
| 460 | + if (!channel.logged) return; |
| 461 | + string log = "\n" + "[" + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + |
| 462 | + DateTime.Now.Second + "] " + "<" + user + "> " + message; |
| 463 | + File.AppendAllText(channel.log, log); |
486 | 464 | } |
487 | 465 | |
488 | 466 | public static bool getMessage(string channel, string nick, string host, string message) |
— | — | @@ -494,17 +472,14 @@ |
495 | 473 | modifyRights(message, curr, nick, host); |
496 | 474 | } |
497 | 475 | |
498 | | - |
499 | | - |
500 | | - |
501 | 476 | return false; |
502 | 477 | } |
503 | 478 | |
504 | 479 | public static int Connect() |
505 | 480 | { |
506 | 481 | data = new System.Net.Sockets.TcpClient(config.network, 6667).GetStream(); |
507 | | - rd = new System.IO.StreamReader(data, System.Text.Encoding.UTF8); |
508 | | - wd = new System.IO.StreamWriter(data); |
| 482 | + rd = new StreamReader(data, System.Text.Encoding.UTF8); |
| 483 | + wd = new StreamWriter(data); |
509 | 484 | |
510 | 485 | wd.WriteLine("USER " + config.name + " 8 * :" + config.name); |
511 | 486 | wd.WriteLine("NICK " + config.username); |
— | — | @@ -516,55 +491,52 @@ |
517 | 492 | wd.WriteLine("JOIN " + ch.name); |
518 | 493 | } |
519 | 494 | wd.Flush(); |
520 | | - string text = ""; |
521 | 495 | string nick = ""; |
522 | 496 | string host = ""; |
523 | | - string message = ""; |
524 | | - string channel = ""; |
525 | 497 | |
526 | 498 | while (true) |
527 | 499 | { |
528 | 500 | while (!rd.EndOfStream) |
529 | 501 | { |
530 | | - text = rd.ReadLine(); |
531 | | - if (text.StartsWith(":")) |
| 502 | + string text = rd.ReadLine(); |
| 503 | + if (text == null || text.StartsWith(":") || !text.Contains("PRIVMSG")) |
532 | 504 | { |
533 | | - if (text.Contains("PRIVMSG")) |
| 505 | + continue; |
| 506 | + } |
| 507 | + |
| 508 | + // we got a message here :) |
| 509 | + if (text.Contains("!") && text.Contains("@")) |
| 510 | + { |
| 511 | + nick = text.Substring(1, text.IndexOf("!") - 1); |
| 512 | + host = text.Substring(text.IndexOf("@") + 1, |
| 513 | + text.IndexOf(" ", text.IndexOf("@")) - 1 - text.IndexOf("@")); |
| 514 | + } |
| 515 | + if ( |
| 516 | + text.Substring(text.IndexOf("PRIVMSG ", text.IndexOf(" "), text.IndexOf("PRIVMSG "))).Contains( |
| 517 | + "#")) |
| 518 | + { |
| 519 | + string channel = text.Substring(text.IndexOf("#"), |
| 520 | + text.IndexOf(" ", text.IndexOf("#")) - text.IndexOf("#")); |
| 521 | + string message = text.Substring(text.IndexOf("PRIVMSG")); |
| 522 | + message = message.Substring(message.IndexOf(":") + 1); |
| 523 | + if (!message.Contains("ACTION")) |
534 | 524 | { |
535 | | - // we got a message here :) |
536 | | - if (text.Contains("!") && text.Contains("@")) |
537 | | - { |
538 | | - nick = text.Substring(1, text.IndexOf("!") - 1); |
539 | | - host = text.Substring(text.IndexOf("@") + 1, |
540 | | - text.IndexOf(" ", text.IndexOf("@")) - 1 - text.IndexOf("@")); |
541 | | - } |
542 | | - if ( |
543 | | - text.Substring(text.IndexOf("PRIVMSG ", text.IndexOf(" "), text.IndexOf("PRIVMSG "))). |
544 | | - Contains("#")) |
545 | | - { |
546 | | - channel = text.Substring(text.IndexOf("#"), |
547 | | - text.IndexOf(" ", text.IndexOf("#")) - text.IndexOf("#")); |
548 | | - message = text.Substring(text.IndexOf("PRIVMSG")); |
549 | | - message = message.Substring(message.IndexOf(":") + 1); |
550 | | - if (message.Contains("ACTION")) |
551 | | - { |
552 | 525 | |
553 | | - } |
554 | | - else |
555 | | - { |
556 | | - getMessage(channel, nick, host, message); |
557 | | - } |
558 | | - } |
559 | | - else |
560 | | - { |
561 | | - // private message |
562 | | - } |
563 | 526 | } |
| 527 | + else |
| 528 | + { |
| 529 | + getMessage(channel, nick, host, message); |
| 530 | + } |
564 | 531 | } |
| 532 | + else |
| 533 | + { |
| 534 | + // private message |
| 535 | + } |
| 536 | + |
565 | 537 | System.Threading.Thread.Sleep(50); |
566 | 538 | } |
567 | 539 | } |
568 | | - return 0; |
| 540 | + return 0; // FIXME: Unreachable |
569 | 541 | } |
570 | 542 | |
571 | 543 | public static int Disconnect() |
— | — | @@ -574,3 +546,4 @@ |
575 | 547 | } |
576 | 548 | } |
577 | 549 | } |
| 550 | + |