]> jfr.im git - irc/rizon/acid.git/blob - acid/src/main/java/net/rizon/acid/core/User.java
5d3ef64439a7339fadb47394e6de3f5a1dd13359
[irc/rizon/acid.git] / acid / src / main / java / net / rizon / acid / core / User.java
1 package net.rizon.acid.core;
2
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.Hashtable;
11 import java.util.Iterator;
12 import java.util.Set;
13 import java.util.logging.Level;
14
15 import net.rizon.acid.conf.AccessPreset;
16 import net.rizon.acid.util.CloakGenerator;
17
18 public class User implements Comparable<User>
19 {
20 private static final Logger log = Logger.getLogger(User.class.getName());
21 private String nick, user, host, vhost, name, identnick, modes, UID, ip,
22 certfp, authflags, su;
23 private String cloakedIp, cloakedHost;
24 private int nickTS, conTS;
25 private Server server;
26 private String flags; // Access flags
27 private ArrayList<String> oldNicks;
28 private Hashtable<String, ArrayList<Integer>> chans;
29 private HashSet<Channel> chanList;
30
31 public User(String nick, String user, String host, String vhost, String name,
32 Server server, int nickTS, int conTS, String modes, String UID, String ip)
33 {
34 this.nick = nick;
35 this.user = user;
36 this.host = host;
37 this.vhost = vhost;
38 this.host = host;
39 this.name = name;
40 this.server = server;
41 this.identnick = "";
42 this.nickTS = nickTS;
43 this.conTS = conTS;
44 this.modes = modes;
45 this.UID = UID;
46 this.ip = ip;
47 // Check if this IP isn't spoofed.
48 if (!this.ip.equals("255.255.255.255") && !this.ip.equals("0"))
49 {
50 // Cloak IP if IP is visible host, else cloak hostname.
51 this.cloakedIp = Acidictive.cloakGenerator.cloak(this.ip);
52 if (this.ip.equals(this.host))
53 {
54 this.cloakedHost = this.cloakedIp;
55 }
56 else
57 {
58 this.cloakedHost = Acidictive.cloakGenerator.cloak(this.host);
59 }
60 }
61 else
62 {
63 this.cloakedIp = this.ip;
64 this.cloakedHost = this.host;
65 }
66 this.flags = "";
67 this.certfp = "";
68 this.authflags = "";
69 this.su = "";
70 oldNicks = new ArrayList<String>();
71 chans = new Hashtable<String, ArrayList<Integer>>();
72 chanList = new HashSet<Channel>();
73
74 uidMap.put(UID, this);
75 nickMap.put(nick.toLowerCase(), this);
76
77 this.getServer().incUsers();
78 }
79
80 public void onQuit()
81 {
82 for (Iterator<Channel> it = chanList.iterator(); it.hasNext();)
83 {
84 Channel chan = it.next();
85 chan.removeUser(this);
86 it.remove();
87 }
88
89 this.getServer().decUsers();
90 if (!(this instanceof AcidUser))
91 UserList.decreaseHost(this.ip);
92
93 uidMap.remove(UID);
94 nickMap.remove(nick.toLowerCase());
95 }
96
97 public void flush()
98 {
99 oldNicks.clear();
100 chans.clear();
101 }
102
103 public void clearFlood()
104 {
105 if (chans != null)
106 chans.clear();
107 }
108
109 public ArrayList<Integer> addChanJoinPart(String chan, int TS)
110 {
111 ArrayList<Integer> x = chans.get(chan.toLowerCase());
112 if (x == null)
113 {
114 ArrayList<Integer> tmp = new ArrayList<Integer>();
115 tmp.add(new Integer(TS));
116 chans.put(chan.toLowerCase(), tmp);
117 return tmp;
118 }
119 else
120 {
121 x.add(new Integer(TS));
122 if (x.size() > 4)
123 x.remove(0);
124 return x;
125 }
126
127 }
128
129 public boolean isOnChan(Channel chan)
130 {
131 return chanList.contains(chan);
132 }
133
134 public void addChan(Channel chan)
135 {
136 chanList.add(chan);
137 }
138
139 public void remChan(Channel chan)
140 {
141 chanList.remove(chan);
142 }
143
144 public Set<Channel> getChannels()
145 {
146 return this.chanList;
147 }
148
149 public String getChanStr()
150 {
151 String s = "";
152
153 for (Channel c : chanList)
154 {
155 if (s.isEmpty())
156 s = c.getName();
157 else
158 s += " " + c.getName();
159 }
160
161 if (s.isEmpty())
162 s = "none";
163
164 return s;
165 }
166
167 public void setRealhost(String ip, String host)
168 {
169 this.ip = ip;
170 this.host = host;
171 }
172
173 public void setFlags(String flags)
174 {
175 this.flags = flags;
176 }
177
178 public void setUID(String uid)
179 {
180 this.UID = uid;
181 }
182
183 public void changeNick(String newNick)
184 {
185 String oldnick = this.getNick();
186
187 nickMap.remove(this.getNick().toLowerCase());
188 oldNicks.add(this.getNick());
189 if (oldNicks.size() > 20)
190 oldNicks.remove(0);
191 this.nick = newNick;
192 nickMap.put(this.getNick().toLowerCase(), this);
193
194 this.setMode("-r");
195
196 for (Channel chan : chanList)
197 {
198 chan.onNick(oldnick, this.getNick());
199 }
200 }
201
202 // accessor methods
203 public String getIdentNick()
204 {
205 return identnick;
206 }
207
208 public String getIP()
209 {
210 return ip;
211 }
212
213 public String getNick()
214 {
215 return nick;
216 }
217
218 public boolean hasMode(String mode)
219 {
220 return modes.contains(mode);
221 }
222
223 public void setMode(String mode)
224 {
225 if (mode.equals("") || mode == null)
226 return;
227
228 String old = modes;
229
230 boolean plus = true;
231 String chr;
232 for (int x = 0; x < mode.length(); x++)
233 {
234 chr = mode.substring(x, x + 1);
235 if (chr.equals("+"))
236 {
237 plus = true;
238 }
239 else if (chr.equals("-"))
240 {
241 plus = false;
242 }
243 else if (plus && modes.indexOf(chr) == -1)
244 {
245 modes += chr;
246 }
247 else if (!plus && modes.indexOf(chr) >= 0)
248 {
249 if (chr.equals("x"))
250 this.vhost = this.host;
251 modes = modes.replaceAll(chr, "");
252 }
253 }
254
255 for (Event e : Event.getEvents())
256 e.onUserMode(this, old, modes);
257 }
258
259 public String getUser()
260 {
261 return user;
262 }
263
264 public String getUID()
265 {
266 return UID;
267 }
268
269 public String getHost()
270 {
271 return host;
272 }
273
274 public String getVhost()
275 {
276 return vhost;
277 }
278
279 public void setVhost(String vhost)
280 {
281 this.vhost = vhost;
282 }
283
284 public String getRealName()
285 {
286 return name;
287 }
288
289 public int getNickTS()
290 {
291 return nickTS;
292 }
293
294 public int getConTS()
295 {
296 return conTS;
297 }
298
299 public void changeNickTS(int nickTS)
300 {
301 this.nickTS = nickTS;
302 }
303
304 public Server getServer()
305 {
306 return server;
307 }
308
309 /* Does this user have all of these flags? Can also take a type (SRA/SA/SO/whatever) */
310 public boolean hasFlags(String flags)
311 {
312 if (flags.isEmpty())
313 return false;
314
315 String my_flags = this.flags;
316
317 for (AccessPreset p : Acidictive.conf.access_preset)
318 {
319 for (String s : p.name)
320 {
321 my_flags = my_flags.replace(s, p.privileges);
322 flags = flags.replace(s, p.privileges);
323 }
324 }
325
326 for (int i = 0; i < flags.length(); ++i)
327 if (my_flags.indexOf(flags.charAt(i)) == -1)
328 return false;
329
330 return true;
331 }
332
333 public String getCloakedIp()
334 {
335 return this.cloakedIp;
336 }
337
338 public String getCloakedHost()
339 {
340 return this.cloakedHost;
341 }
342
343 public String getFlags()
344 {
345 return this.flags;
346 }
347
348 public String getModes()
349 {
350 return modes;
351 }
352
353 public String getCertFP()
354 {
355 return certfp;
356 }
357
358 public void setCertFP(String certfp)
359 {
360 this.certfp = certfp;
361 }
362
363 public String getAuthFlags()
364 {
365 return authflags;
366 }
367
368 public void setAuthFlags(String authflags)
369 {
370 this.authflags = authflags;
371 }
372
373 public String getSU()
374 {
375 return su;
376 }
377
378 public void setSU(String su)
379 {
380 this.su = su;
381 }
382
383 @Override
384 public int compareTo(User user)
385 {
386 return nick.compareTo(user.getNick());
387 }
388
389 @Override
390 public String toString()
391 {
392 return nick + "!" + user + "@" + host + "/" + ip + "/" + this.cloakedIp + "/" + this.cloakedHost + "/" + "(" + name + ")[" + server + "] / " + oldNicksList();
393 }
394
395 public String getNickString()
396 {
397 return nick + "!" + user + "@" + host + "/" + ip + "/" + this.cloakedIp + "/" + this.cloakedHost + "/" + vhost + "(" + name + ")";
398 }
399
400 public String getSNString()
401 {
402 return nick + "!" + user + "@" + host;
403 }
404
405 public String getNString()
406 {
407 return nick + "!" + user + "@" + host + "/" + ip + "/" + this.cloakedIp + "/" + this.cloakedHost + "/" + vhost + "(" + name + ")(" + modes + ")[" + server + "]";
408 }
409
410 public String getNStringON()
411 {
412 return nick + "!" + user + "@" + host + "/" + ip + "/" + this.cloakedIp + "/" + this.cloakedHost + "/" + vhost + "(" + name + ")(" + modes + ")[" + server + "] / " + oldNicksList();
413 }
414
415 public String getNickLine()
416 {
417 return getNick() + "!" + getUser() + "@" + getHost() + "/" + getRealName();
418 }
419
420 public ArrayList<String> oldNicks()
421 {
422 return oldNicks == null ? new ArrayList<String>() : oldNicks;
423 }
424
425 public String oldNicksList()
426 {
427 if (oldNicks == null)
428 return "No nick changes.";
429 if (oldNicks.size() > 0)
430 {
431 StringBuffer sb = new StringBuffer();
432 for (int i = 0; i < oldNicks.size(); i++)
433 {
434 sb.append(oldNicks.get(i) + " -> ");
435 }
436 sb.append(nick);
437 return sb.toString();
438 }
439 return "No nick changes.";
440 }
441
442 // TODO: Fixme to make this either properly static or a "real" method
443 // XXX ??????????????????????????????????? wtf is this
444 public void loadAccess(final User user)
445 {
446 try
447 {
448 PreparedStatement ps = Acidictive.acidcore_sql.prepare("SELECT `user`, `flags` FROM `access` WHERE `user` = ?");
449 ps.setString(1, user.getSU());
450 ResultSet rs = Acidictive.acidcore_sql.executeQuery(ps);
451 if (rs.next())
452 {
453 String oldflags = this.flags;
454
455 this.flags = rs.getString("flags");
456 this.identnick = user.getSU();
457
458 if (oldflags.equals(this.flags) == false)
459 Acidictive.notice(this, "You have been logged in with flags " + this.flags);
460 }
461 }
462 catch (SQLException ex)
463 {
464 log.log(Level.WARNING, "Unable to load access for user", ex);
465 }
466 }
467
468 private static HashMap<String, User> uidMap = new HashMap<String, User>();
469 private static HashMap<String, User> nickMap = new HashMap<String, User>();
470
471 public static User findUser(final String nick)
472 {
473 if (nick != null && nick.isEmpty() == false && Character.isDigit(nick.charAt(0)))
474 return uidMap.get(nick);
475 return nickMap.get(nick.toLowerCase());
476 }
477
478 public static final Set<String> getUsers()
479 {
480 return nickMap.keySet();
481 }
482
483 public static Collection<User> getUsersC() { return nickMap.values(); }
484
485 public static final String toName(final String uid)
486 {
487 User u = findUser(uid);
488 if (u != null)
489 return u.getNick();
490 return uid;
491 }
492
493 public static int userCount()
494 {
495 return nickMap.size();
496 }
497
498 private static char[] currentUid = new char[] { 'A', 'A', 'A', 'A', 'A', '@' };
499
500 public static String generateUID()
501 {
502 for (int i = 5; i >= 0; --i)
503 {
504 if (currentUid[i] == 'Z')
505 {
506 currentUid[i] = '0';
507 break;
508 }
509 else if (currentUid[i] != '9')
510 {
511 currentUid[i]++;
512 break;
513 }
514 else
515 currentUid[i] = 'A';
516 }
517
518 return AcidCore.me.getSID() + new String(currentUid);
519 }
520 }