KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > core > User


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */

18 package freecs.core;
19
20 import freecs.content.*;
21 import freecs.*;
22 import freecs.interfaces.*;
23 import freecs.layout.*;
24 import java.util.HashMap JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Vector JavaDoc;
29 import java.nio.ByteBuffer JavaDoc;
30 import java.nio.channels.SelectionKey JavaDoc;
31
32 /**
33  * the user prototype storing all userdata
34  */

35 public class User implements IUserStates, IMessageDestination {
36     private static final short LOGGING_IN = 0;
37     private static final short LOGGED_IN = 1;
38     private static final short SCHEDULED_FOR_REMOVAL = 2;
39     private static final short SENDING_QUIT_MESSAGE = 3;
40     private static final short LOGGING_OUT = 4;
41     private static final short LOGGED_OUT = 5;
42     
43     private short state = -1;
44     public boolean blocked = false, activated = true, isUnregistered = true;
45     private volatile String JavaDoc name, cookie, colCode, awayMessage, id;
46     private String JavaDoc customTitle;
47     private volatile int permissionMap, defaultPermissionMap, defaultMembershipPermissionMap, hashCode=Integer.MIN_VALUE,
48                          flooded=0, questionCounter=0, tooled=0;
49     private HashMap JavaDoc userProps;
50     private long sessionStart;
51     public long lastSentMessage;
52     public long toolcontrol;
53     private volatile long lastActive, removeWhen = 0,
54                         lastColChange = 0, awayStart, awayTime=0;
55     public volatile long lastRecievedMessage;
56     private volatile transient Group grp = null;
57     private Vector JavaDoc ignoreList, friendsList;
58     private volatile transient Group invitedTo;
59     private volatile transient User invitedBy;
60     private volatile boolean away = false, isPunished = false, isHTTP11 = true;
61     private Vector JavaDoc schedMsgs;
62     private volatile SelectionKey JavaDoc sk;
63     private TemplateSet ts;
64     private volatile transient User ownPrivateUser = null, // the last user this user whispered to
65
foreignPrivateUser = null; // the last user whispering to this user
66
public Connection conn;
67
68     private HashMap JavaDoc memberships = new HashMap JavaDoc();
69     private Membership defaultMembership = null;
70     private short friendNotification = 0;
71     public static final short FN_NONE = 0;
72     public static final short FN_FRIEND_AGREEMENT = 1;
73     public static final short FN_ALL = 2;
74
75    /**
76     * constructor for user
77     */

78    public User(String JavaDoc name, String JavaDoc cookie) {
79       this.colCode = null;
80       this.name = name;
81       this.cookie = cookie;
82       this.userProps = new HashMap JavaDoc();
83       this.sessionStart = System.currentTimeMillis ();
84       this.lastRecievedMessage = this.sessionStart;
85       this.lastActive = this.sessionStart;
86       this.ignoreList = new Vector JavaDoc ();
87       this.friendsList = new Vector JavaDoc ();
88       this.schedMsgs = new Vector JavaDoc ();
89       this.state = LOGGING_IN;
90       ts = Server.srv.templatemanager.getTemplateSet("default");
91       if (Server.TRACE_CREATE_AND_FINALIZE)
92           Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
93    }
94
95     /**
96      * schedule a message, which will be displayed when the user
97      * enters the chat (e.g. you have vip-rights)
98      * @param mpr
99      */

100    public void scheduleMessage (MessageParser mpr) {
101       if (mpr == null) return;
102       synchronized (this) {
103           if (schedMsgs == null)
104               schedMsgs = new Vector JavaDoc ();
105           schedMsgs.addElement (mpr);
106       }
107    }
108
109     /**
110      * Sends all scheduled messages
111      */

112     public void sendScheduledMessages() {
113         if (schedMsgs == null) return;
114         for (Iterator JavaDoc i = schedMsgs.iterator(); i.hasNext(); ) {
115             MessageParser mpr = (MessageParser) i.next ();
116             this.implSendMessage(mpr);
117             i.remove();
118         }
119         schedMsgs=null;
120     }
121
122     public boolean isJoining () {
123         return state == LOGGING_IN;
124     }
125
126     /**
127      * check if the user is in the removing-process
128      * @return true if this user is schedulte for removal, false if not
129      */

130     public boolean isRemoving () {
131         return this.state==SCHEDULED_FOR_REMOVAL;
132     }
133
134     /**
135      * schedule this user to be removed (if user reconnects, we asume he was accidently disconnected
136      * and reuse this User-Object. After a deffined time the user get's realy logged out)
137      */

138     public void scheduleToRemove () {
139         if (this.state>=SCHEDULED_FOR_REMOVAL)
140             return;
141         this.state=SCHEDULED_FOR_REMOVAL;
142         StringBuffer JavaDoc tsb = new StringBuffer JavaDoc ("scheduleToRemove: ");
143         tsb.append (name);
144         if (conn != null) {
145             tsb.append ("@");
146             tsb.append (conn.toString());
147         }
148         Server.log ("[User " + name + "]", tsb.toString (), Server.MSG_AUTH, Server.LVL_VERY_VERBOSE);
149         this.removeWhen = System.currentTimeMillis () + Server.srv.USER_REMOVE_SCHEDULE_TIME;
150         if (sk!=null) {
151             ConnectionBuffer cb = (ConnectionBuffer) sk.attachment();
152             cb.invalidate();
153         }
154     }
155
156     public synchronized void sendQuitMessage (boolean b) {
157         if (state>=SENDING_QUIT_MESSAGE)
158             return;
159         state = SENDING_QUIT_MESSAGE;
160         // if the selectionkey is valid and the channel is open, try
161
// to send quit message
162
Group g = grp;
163         if (grp != null) {
164             grp = null;
165             g.removeUser(this);
166         }
167         if (g != null && g.size() > 0 && g.isValid()) {
168             MessageParser mp = new MessageParser ();
169             mp.setSender (this);
170             if (b) {
171                 mp.setMessageTemplate ("message.user.leaving.server.kicked");
172             } else {
173                 if (isAway()){
174                     mp.setMessageTemplate ("message.away.off");
175                     g.sendMessage (mp);
176                 }
177                 mp.setMessageTemplate ("message.user.leaving.server");
178             }
179             g.sendMessage (mp);
180         }
181         if (sk != null && sk.isValid() && sk.channel().isOpen()) try {
182             MessageParser mp = new MessageParser ();
183             mp.setSender (this);
184             mp.setMessageTemplate (b ? "message.kh.personal" : "message.q");
185             this.implSendMessage (mp);
186             Server.log ("[User " + name + "]", "removeNow: sent quit-message", Server.MSG_STATE, Server.LVL_VERBOSE);
187         } catch (Exception JavaDoc e) {
188             Server.debug ("[User " + name + "]", "removeNow: ", e, Server.MSG_ERROR, Server.LVL_MINOR);
189         } else {
190             CentralSelector.dropKey(sk);
191         }
192         removeWhen = System.currentTimeMillis() + 1000;
193     }
194
195     /**
196      * loggs out the user now. skipps scheduleToRemove or get's called
197      * because schedule-time has been reached
198      */

199     public synchronized void removeNow () {
200         if (state>=LOGGING_OUT)
201             return;
202         state=LOGGING_OUT;
203         removeWhen=System.currentTimeMillis() + Server.srv.USER_REMOVE_SCHEDULE_TIME;
204         UserManager.mgr.removeUser (this);
205         for (Iterator JavaDoc i = memberships.entrySet().iterator(); i.hasNext(); ) {
206             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
207             Membership m = (Membership) entry.getValue();
208             m.remove(this);
209         }
210
211         // if user has a group, send a message to this group to let other users know
212
// this user has left the server
213
if (grp != null) {
214             Group g = grp;
215             grp = null;
216             g.removeUser(this);
217             MessageParser mp = new MessageParser ();
218             mp.setSender (this);
219             mp.setMessageTemplate ("message.user.leaving.server");
220             g.sendMessage (mp);
221         }
222
223         // logout the user
224
try {
225             Server.srv.auth.logoutUser (this);
226         } catch (Exception JavaDoc e) {
227             Server.debug ("[User " + name + "]", "removeNow: Exception during logout", e, Server.MSG_ERROR, Server.LVL_MAJOR);
228         }
229         for (Enumeration JavaDoc e = friendsList.elements (); e.hasMoreElements (); ) {
230             String JavaDoc fname = (String JavaDoc) e.nextElement ();
231             UserManager.mgr.removeFriendship (this, fname);
232         }
233         StringBuffer JavaDoc tsb = new StringBuffer JavaDoc ("logged out: ");
234         tsb.append (name);
235         if (conn != null) {
236             tsb.append ("@");
237             tsb.append (conn.toString ());
238         }
239         Server.log ("[User " + name + "]", tsb.toString (), Server.MSG_AUTH, Server.LVL_MINOR);
240         Server.srv.removeToken(cookie);
241         this.state=LOGGED_OUT;
242     }
243     
244    /**
245     * return the time the user has to be logged out
246     */

247    public long getRemoveWhen () {
248       return removeWhen;
249    }
250
251    /**
252     * keeps the lastActive time fresh and checks for flooding
253     * @return false if the user has flooded or is finalizing
254     */

255     public synchronized boolean wasActive () {
256         if (state>=LOGGING_OUT)
257             return false;
258         if (removeWhen != 0 || state==SCHEDULED_FOR_REMOVAL) {
259             removeWhen = 0;
260             state=LOGGED_IN;
261         }
262         long currTime = System.currentTimeMillis ();
263         if ((currTime - this.lastActive) < Server.srv.FLOOD_PROTECT_MILLIS) {
264             flooded++;
265             if (flooded > Server.srv.FLOOD_PROTECT_TOLERANC) {
266                 Server.srv.banUser (this, "message.user.flooded", null, Server.srv.FLOOD_BAN_DURATION, "FloodProtection");
267                 return false;
268             }
269         } else
270             flooded = 0;
271               
272         long difference = toolcontrol- (currTime - this.lastActive);
273         if (difference <0 ) difference = difference *-1;
274         if (tooled >0)
275             Server.log("[User " + name + "]","TOK: " + toolcontrol + " <> " + (currTime - this.lastActive)+ " = " + difference + " Toleranc: " + Server.srv.TOOL_PROTECT_TOLERANC + " Counter: " + tooled , Server.MSG_STATE, Server.LVL_MINOR);
276         if ((currTime - this.lastActive) * Server.srv.TOOL_PROTECT_MINCOUNTER > Server.srv.TOOL_PROTECT_MINMILLS) {
277             Server.log("[User " + name + "]","TOK: "+ (currTime - this.lastActive) * Server.srv.TOOL_PROTECT_MINCOUNTER+ " <> " + Server.srv.TOOL_PROTECT_MINMILLS , Server.MSG_STATE, Server.LVL_MINOR);
278             if (difference <= Server.srv.TOOL_PROTECT_TOLERANC) {
279                 tooled++;
280                 if (tooled >= Server.srv.TOOL_PROTECT_COUNTER) {
281                     Server.srv.banUser (this, "message.user.tooled", null, Server.srv.TOOL_BAN_DURATION, "ToolProtection");
282                     return false;
283                 }
284             } else
285                 tooled = 0;
286         }
287         toolcontrol = currTime - this.lastActive;
288         this.lastActive = currTime;
289         return true;
290     }
291
292    /**
293     * sets the group in which this user joined
294     * @param newgrp the group this user has joined
295     */

296     public synchronized boolean setGroup (Group newgrp) {
297         if (newgrp == null)
298             return false;
299         if (this.grp != null)
300             this.grp.removeUser (this);
301         this.grp = newgrp;
302         questionCounter = 0;
303         return true;
304    }
305
306    /**
307     * gets the group of this user
308     * @return the group this user is a member from
309     */

310    public Group getGroup () {
311       return grp;
312    }
313
314    /**
315     * gets the cookie of this user
316     * @return this users cookie
317     */

318    public String JavaDoc getCookie () {
319       return cookie;
320    }
321
322    /**
323     * gets the last-active-time of this user
324     * @return last activity as long value
325     */

326    public long lastActive () {
327       return lastActive;
328    }
329
330    /**
331     * Sets the SelectionKey of the message-output-frame of this user
332     * @param sk the SelectionKey for this user's responder
333     */

334    public synchronized void setKey (SelectionKey JavaDoc sk) {
335       if (!CentralSelector.isSkValid(sk)) {
336           Server.log (this, "tryed to set invalid key", Server.MSG_STATE, Server.LVL_MINOR);
337           this.scheduleToRemove();
338           return;
339       }
340       this.sk = sk;
341       ConnectionBuffer cb = (ConnectionBuffer) sk.attachment ();
342       cb.setUser (this);
343       conn = cb.conn;
344       state=LOGGED_IN;
345    }
346    
347    /**
348     * returns the SelectionKey of this users message-output-frame
349     * @return SelectionKey of this users message-output-frame
350     */

351    public SelectionKey JavaDoc getKey () {
352       return sk;
353    }
354
355
356    /**
357     * Interface IMessageDestination
358     */

359
360     /**
361      * touches this user to keep proxies from closing the connection for this user
362      */

363     public synchronized void touch (long now) {
364         if (state!=LOGGED_IN) {
365             return;
366         }
367         if (sk == null) {
368             scheduleToRemove();
369             return;
370         }
371         if (!sk.isValid () || !sk.channel ().isOpen ()) {
372             Server.log ("[User " + name + "]", "touch: droped key", Server.MSG_STATE, Server.LVL_VERBOSE);
373             CentralSelector.dropKey (sk);
374             return;
375         }
376         long diff = now - lastRecievedMessage;
377         if (diff < Server.srv.TOUCH_USER_DELAY) {
378             return;
379         }
380         try {
381             ByteBuffer JavaDoc clone = ByteBuffer.wrap(UserManager.mgr.TOUCH_CONTENT);
382             Server.log ("[User " + name + "]", "touch", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
383             ConnectionBuffer cb = (ConnectionBuffer) sk.attachment();
384             cb.addToWrite (clone);
385             lastRecievedMessage = now;
386         } catch (Exception JavaDoc e) {
387             Server.debug ("[User " + name + "]", "touch: catched exception during touch", e, Server.MSG_ERROR, Server.LVL_MAJOR);
388         }
389     }
390
391    /**
392     * sends the message to this user
393     * @param mc the message-container
394     */

395     public void sendMessage (IContainer mc) {
396         if (mc==null)
397             return;
398         if (state!=LOGGED_IN)
399             return;
400         if (mc instanceof MessageParser) {
401             User sender = ((MessageParser)mc).getSender();
402             if (sender!= null && ignoreList.contains(sender.getName().toLowerCase()) && !sender.hasRight(IUserStates.ROLE_VIP))
403                 return;
404         }
405         if (state!=LOGGING_IN && sk == null) {
406             Server.log("[User " + name + "]", "sendMessage: selectionkey was null", Server.MSG_STATE, Server.LVL_MINOR);
407             scheduleToRemove ();
408             return;
409         } else if (sk==null)
410             return;
411         implSendMessage(mc);
412     }
413     
414     private void implSendMessage (IContainer mc) {
415         if (mc instanceof PersonalizedMessage) {
416             ConnectionBuffer cb = (ConnectionBuffer) sk.attachment();
417             cb.addToWrite(mc.getByteBuffer());
418             if (mc.closeSocket())
419                 cb.addToWrite(Responder.CLOSE_CONNECTION);
420             lastRecievedMessage = System.currentTimeMillis ();
421             return;
422         }
423         ((MessageParser) mc).setHTTP11 (isHTTP11 && Server.srv.USE_HTTP11);
424         ConnectionBuffer cb = (ConnectionBuffer) sk.attachment();
425         if (!((MessageParser) mc).addPersonalizedMessage (this, cb)) {
426             Server.log ("[User " + name + "]", "sendMessage: there was nothing to send", Server.MSG_TRAFFIC, Server.LVL_VERY_VERBOSE);
427             return;
428         }
429         lastRecievedMessage = System.currentTimeMillis ();
430     }
431
432
433    /**
434     * return an Enumeration containing only this user
435     */

436    public Iterator JavaDoc users () {
437       return new Iterator JavaDoc () {
438          boolean userNotReturned = true;
439          public boolean hasNext () {
440             return userNotReturned;
441          }
442          public Object JavaDoc next () {
443             userNotReturned = false;
444             return this;
445          }
446          public void remove () { }
447       };
448    }
449
450    /**
451     * checks if the given user is ignored by this user
452     * @param u the user to check if it is ignored by this user
453     */

454    public boolean userIsIgnored (User u) {
455       return ignoreList.contains (u);
456    }
457
458    /**
459     * this user will ignore the given user if called
460     * @param u the user to be ignored by this user
461     */

462     public void ignoreUser (User u) {
463         String JavaDoc uname = u.getName().toLowerCase();
464         ignoreUser(uname);
465     }
466     
467     /**
468      * add the username to the ignorelist of this user
469      * @param uname the username to add to this user's ignorelist
470      */

471     public void ignoreUser (String JavaDoc uname) {
472         if (ignoreList.contains (uname))
473             return;
474 /* WE MUST NOT remove the user from the invitedBy-state
475         This would give the anoying user possibility to reinvite this user
476         if (invitedBy != null && invitedBy.equals (u)) {
477             invitedBy = null;
478             invitedTo = null;
479         } */

480         ignoreList.addElement (uname);
481     }
482
483    /**
484     * this user will no longer ignore the given user
485     * @param u the user to respect again
486     */

487     public void respectUser (User u) {
488         String JavaDoc uname = u.getName().toLowerCase();
489         while (ignoreList.contains (uname)) ignoreList.removeElement (uname);
490    }
491
492    /**
493     * sets the id of this user
494     * @param id the id of this user
495     */

496    public void setID (String JavaDoc id) {
497       this.id = id;
498    }
499
500    /**
501     * gets the id of this user
502     * @return the id of this user
503     */

504    public String JavaDoc getID () {
505       return id;
506    }
507
508 // ***************************** STATE-QUERY and SET
509
/**
510     * sets right for this user
511     * @param right the rightset for this user
512     * @see freecs.interfaces.IUserStates
513     */

514     public void setPermission (int right) {
515        this.permissionMap = right;
516        this.defaultPermissionMap = right;
517     }
518     public void setDefaultPermissionMap (int right) {
519           this.defaultPermissionMap = right;
520     }
521     public void setDefaultMembershipPermission (int right) {
522         this.defaultMembershipPermissionMap = right;
523     }
524     public int getPermissionMap () {
525         return this.permissionMap;
526     }
527     public int getDefaultMembershipPermissionMap () {
528         return this.defaultMembershipPermissionMap;
529     }
530     public int getDefaultPermissionMap () {
531         return this.defaultPermissionMap;
532     }
533
534     public void setNewPermission (int right) {
535         this.permissionMap = right;
536     }
537     
538     public void resetPermission () {
539         this.permissionMap = this.defaultPermissionMap;
540     }
541
542     public synchronized void addMembership (String JavaDoc key, Membership m) {
543         if (defaultMembership == null)
544             defaultMembership = m;
545         memberships.put(key, m);
546     }
547     
548     public Membership getMembership (String JavaDoc key) {
549         return (Membership) memberships.get(key);
550     }
551     
552     public Membership getDefaultMembership () {
553         return defaultMembership;
554     }
555
556     public synchronized void removeMembership (String JavaDoc key) {
557         memberships.remove(key);
558         rebuildMemberships();
559     }
560     
561     public synchronized void rebuildMemberships () {
562         for (Iterator JavaDoc i = memberships.entrySet().iterator(); i.hasNext(); ) {
563             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
564             Membership m = (Membership) entry.getValue();
565             m.add(this);
566         }
567     }
568
569    /**
570     * grants a specific right to this user additional to previous ones
571     * @param right
572     * @see freecs.interfaces.IUserStates
573     */

574    public void givePermission (int right) {
575       this.permissionMap = this.permissionMap | (right - (this.permissionMap & right));
576    }
577
578    /**
579     * removes right of this user
580     * @param right
581     * @see freecs.interfaces.IUserStates
582     */

583    public void takePermission (int right) {
584       this.permissionMap = this.permissionMap - (this.permissionMap & right);
585    }
586
587    /**
588     * determines if this user has the given right
589     * @param right the right this user will be queried for
590     * @return true if all flaggs in right are set for this user
591     */

592    public boolean hasRight (int right) {
593       return ((this.permissionMap & right) == right);
594    }
595
596    public boolean hasRole (int role) {
597        int clean = this.permissionMap - (this.permissionMap & (IUserStates.IS_GUEST | IUserStates.IS_MODERATOR));
598        return clean == role;
599    }
600
601    public boolean hasDefaultRight (int right) {
602        return ((this.defaultPermissionMap & right) == right);
603    }
604    /**
605     * set away-state
606     * @param b true if user is away; false otherwhise
607     */

608    public void setAway (boolean b) {
609       if (b) {
610           awayStart = System.currentTimeMillis ();
611       } else {
612           awayTime += System.currentTimeMillis () - awayStart;
613       }
614       away = b;
615    }
616
617    /**
618     * set away-state
619     * @return away true if user is away; false otherwhise
620     */

621    public boolean isAway () {
622       return away;
623    }
624    
625    /**
626     * returns the time this user switched to the away-state
627     * @return the away-time of this user
628     */

629    public long awayTime () {
630       return awayTime;
631    }
632
633     /**
634      * checks if this user is punished
635      * @return true if the user is punished
636      */

637    public boolean isPunished () {
638       return isPunished;
639    }
640
641     /**
642      * sets the punishment-state of this user
643      * @param p if true, the user will be switched into punished mode
644      */

645    public void setPunish (boolean p) {
646       isPunished = p;
647    }
648
649     /**
650      * sets the invitation of a user for this user
651      * @param u the user inviting this user
652      * @return true if the user doesn't ignore the other user
653      */

654    public boolean invitedFrom (User u) {
655       if (userIsIgnored(u)) return false;
656       this.invitedTo = u.getGroup ();
657       this.invitedBy = u;
658       return true;
659    }
660
661     /**
662      * unsets the invitation-state of this user
663      */

664    public void unsetInvitedTo () {
665       invitedBy = null;
666       invitedTo = null;
667    }
668
669     /**
670      * returns the user which has invited this user
671      * @return the inviter of this user
672      */

673    public User invitedBy () {
674       return invitedBy;
675    }
676
677     /**
678      * returns the group to which this user was invited to
679      * @return the group this user is invited to
680      */

681    public Group invitedTo () {
682       return invitedTo;
683    }
684
685     /**
686      * get the color-code for this user
687      * @return the color-code for this user
688      */

689    public String JavaDoc getColCode () {
690       return colCode;
691    }
692
693     /**
694      * Set the colorcode for this user. This method is only for internal
695      * use (e.g. assigning a colorcode automatically from the database). A
696      * colorchange triggered by the user must be done by changeColCode
697      * @param cCode the colorcode this user will recieve
698      */

699    public void setColCode (String JavaDoc cCode) {
700       colCode = cCode;
701    }
702    
703    /**
704     * This is used for user-triggered colorchanges.
705     * @param cCode the color-code wanted
706     * @return true if allowed, if not false
707     */

708    public boolean changeColCode (String JavaDoc cCode) {
709         long now = System.currentTimeMillis ();
710         if ((now - lastColChange) < Server.srv.COLOR_CHANGE_INTERVAL)
711             return false;
712         colCode = cCode;
713         lastColChange = now;
714         return true;
715     }
716
717     /**
718      * returns the templateset this user has choosen
719      * @return the templateset this user has choosen
720      */

721    public TemplateSet getTemplateSet () {
722       if (this.ts == null) ts = Server.srv.templatemanager.getTemplateSet ("default");
723       return ts;
724    }
725
726     /**
727      * set the templateset to use for this user
728      * @param ts the templateset to use
729      */

730    public void setTemplateSet (TemplateSet ts) {
731       this.ts = ts;
732    }
733
734     /**
735      * return the value of the given property of this user
736      * @param k the name of the property
737      * FIXME: this should also use the templateset of this user
738      * @return the value of the property of this user
739      */

740    public Object JavaDoc getProperty (String JavaDoc k) {
741       if (k.equals ("isaway")) {
742          return (away ? "away" : "");
743       } else if (k.equals("awaymessage")) {
744          if (!away)
745             return ("");
746          String JavaDoc msg = getAwayMessage();
747          if (msg.equals(""))
748             return (Boolean.FALSE);
749          return (msg);
750       } else if (k.equals ("sessionstart.hour")) {
751          return Server.formatTimeStamp (sessionStart, Server.hourSDF);
752       } else if (k.equals ("sessionstart.minute")) {
753          return Server.formatTimeStamp (sessionStart, Server.minuteSDF);
754       } else if (k.equals ("idletime")) {
755          long diff = System.currentTimeMillis () - lastActive;
756          diff = Math.round (diff / 1000);
757          return (String.valueOf (diff));
758       } else if (k.equals ("sessiontime")) {
759          long l = (System.currentTimeMillis () - sessionStart) / 1000 / 60;
760          StringBuffer JavaDoc tsb = new StringBuffer JavaDoc ();
761          if (l > 60) {
762             long stdn = l / 60;
763             if (stdn == 1)
764                tsb.append ("einer Stunde ");
765             else {
766                tsb.append (stdn);
767                tsb.append (" Stunden ");
768             }
769             tsb.append (l % 60);
770             tsb.append (" Minuten");
771          } else if (l == 1) {
772             tsb.append ("einer Minute");
773          } else {
774             tsb.append (l);
775             tsb.append (" Minuten");
776          }
777          return tsb.toString ();
778       }
779       return userProps.get (k);
780    }
781
782     /**
783      * set the property of this user to the value given
784      * @param k the property to set for this user
785      * @param v the value for this property of this user
786      */

787    public void setProperty (String JavaDoc k, Object JavaDoc v) {
788       userProps.put (k, v);
789    }
790
791     /**
792      * retuns the timestamp this user logged in
793      * @return the timestamp
794      */

795    public long getSessionStart () {
796       return sessionStart;
797    }
798
799     /**
800      * set the HTTP/1.1 capability of this user
801      * @param b true if this users client is HTTP/1.1 capable
802      */

803    public void setHTTP11 (boolean b) {
804       isHTTP11 = b;
805    }
806
807    /**
808     * get the name of this user
809     * @return string with username
810     */

811    public String JavaDoc getName () {
812       return (name);
813    }
814
815     /**
816      * adds a freind relation for this user
817      * @param fname the name of the friend
818      */

819    public void addFriend (String JavaDoc fname) {
820       if (friendsList.contains (fname)) return;
821       UserManager.mgr.addFriendship (this, fname);
822       friendsList.addElement (fname);
823    }
824    /**
825     * returns the number of friends of this user
826     * @return the number of freinds of this user
827     */

828    public int numberOfFriends () {
829       return friendsList.size ();
830    }
831    
832    public boolean isFriend (User u) {
833        return isFriend (u.name);
834    }
835    public boolean isFriend (String JavaDoc name) {
836        String JavaDoc n = name.trim().toLowerCase();
837        return this.friendsList.contains(n);
838    }
839    
840    /**
841     * returns an Enumeration containing all friends of this user.
842     * @return Enumeration contaioning all friends of this user
843     */

844    public Enumeration JavaDoc friends () {
845       return friendsList.elements ();
846    }
847
848     /**
849      * give a message when the user-object gets finalized by the garbage-collector
850    public void finalize () {
851       Server.log ("[User " + name + "]", "got finalized **************", Server.MSG_STATE, Server.LVL_VERBOSE);
852    }
853      */

854    
855     public boolean equals (User u) {
856         if (u==null) return false;
857         if (id != null && id.equals(u.getID ())) return true;
858         if (name.equalsIgnoreCase (u.getName ())) return true;
859         return (false);
860    }
861
862    public int hashCode () {
863       if (hashCode != Integer.MIN_VALUE)
864          return hashCode;
865       if (id != null)
866          hashCode = id.hashCode();
867       else
868          hashCode = name.toLowerCase().hashCode ();
869       return (hashCode);
870    }
871
872     /**
873      * returns the number of questions this user has asked within this group
874      * @return the number of questions this user has asked within this group
875      */

876    public int getQuestionCounter () {
877       return questionCounter;
878    }
879    /**
880     * increments the questioncounter of this user
881     */

882    public void incrementQuestionCounter () {
883       questionCounter++;
884    }
885    
886    /**
887     * returns true if this userobject is logged out
888     * @return true if this userobject is logged out
889     */

890    public boolean isLoggedOut () {
891       return state==LOGGED_OUT;
892    }
893    
894    /**
895     * returns true if this userobject is logged in
896     * @return true if this userobject is logged in
897     */

898    public boolean isLoggedIn () {
899       return state==LOGGED_IN;
900    }
901
902    /**
903     * stores the last user to which this user sent a private-message
904     * @param pu the user who sent the private-message to this user
905     */

906    public void setPrivateUser (User pu) {
907       this.ownPrivateUser = pu;
908    }
909    
910    /**
911     * returns the stored user to which this user sent a private message last time
912     * @return the stored user to which this user sent a private message last time
913     */

914    public User getPrivateUser () {
915       return ownPrivateUser;
916    }
917    
918    /**
919     * stores the last user which sent a private-message to this user
920     * @param pu the user who sent the private-message to this user
921     */

922    public void setForeignPrivateUser (User pu) {
923       this.foreignPrivateUser = pu;
924    }
925
926    /**
927     * returns the stored user which sent a private message to this user last time
928     * @return the stored user which sent a private message to this user last time
929     */

930    public User getForeignPrivateUser () {
931       return foreignPrivateUser;
932    }
933  
934     public boolean usrMayWhisper(User u) {
935         if (this.grp==null)
936             return true;
937         if (this.hasRight(IUserStates.IS_GUEST)
938             && this.grp.hasState(IGroupState.SND_PRF_GUEST))
939             return false;
940         if (this.hasRight(IUserStates.ROLE_GOD)
941             && this.grp.hasState(IGroupState.SND_PRF_GOD))
942                 return false;
943         if (this.hasRight(IUserStates.IS_MODERATOR)
944             && this.grp.hasState(IGroupState.SND_PRF_MODERATOR))
945                 return false;
946         if (this.hasRight(IUserStates.ROLE_VIP)
947             && this.grp.hasState(IGroupState.SND_PRF_VIP))
948                 return false;
949         if (this.hasRight(IUserStates.ROLE_USER)
950             && this.grp.hasState(IGroupState.SND_PRF_USER))
951                 return false;
952         return true;
953     }
954    
955     /**
956      * checks the references of this user and unsets them if
957      * the user referd to is logged out
958      */

959     public void checkReferences() {
960         if (foreignPrivateUser != null && foreignPrivateUser.isLoggedOut())
961             foreignPrivateUser = null;
962         if (ownPrivateUser != null && ownPrivateUser.isLoggedOut()) {
963             User opu = UserManager.mgr.getUserByName(ownPrivateUser.name);
964             if (opu != null)
965                 ownPrivateUser = opu;
966         }
967         if (invitedBy != null && invitedBy.isLoggedOut()) {
968             invitedBy = null;
969             invitedTo = null;
970         }
971     }
972
973     /**
974      * Stores the message given by the command /away do display it, when the user
975      * returns from away-state (or when /w is called for this user...)
976      * @param param
977      */

978     public void setAwayMessage(String JavaDoc param) {
979         awayMessage = param;
980     }
981     /**
982      * returns the stored awaymessage
983      * @return the stored awaymessage
984      */

985     public String JavaDoc getAwayMessage () {
986         return awayMessage == null ? "" : awayMessage;
987     }
988     
989     /**
990      * calculate the chattime including the current session
991      * @return the calculated chattime
992      */

993     public long getChattime () {
994         Object JavaDoc ct = getProperty ("chattime");
995         if (ct == null) {
996             return ((lastActive () - getSessionStart ()) - awayTime ()) / 1000;
997         }
998         return (((lastActive () - getSessionStart ()) - awayTime ()) / 1000) + ((Long JavaDoc) ct).longValue ();
999     }
1000    
1001    public void setFriendsNotification (short mode) {
1002        this.friendNotification=mode;
1003    }
1004    public short notifyFriends() {
1005        return this.friendNotification;
1006    }
1007    
1008    
1009    /**
1010     * set the custom-title
1011     * @param et the custom-title
1012     */

1013    public void setCustomTitle (String JavaDoc et) {
1014        customTitle = et;
1015    }
1016    
1017    /**
1018     * get the custom-title which was fetched from db on login
1019     * @return custom-title
1020     */

1021    public String JavaDoc getCustomTitle () {
1022        return customTitle;
1023    }
1024
1025    public synchronized long nextCheck() {
1026        long lowestValue = lastActive + (away ? Server.srv.USER_AWAY_TIMEOUT : Server.srv.USER_TIMEOUT);
1027        if ((state==LOGGING_OUT || state==SCHEDULED_FOR_REMOVAL)
1028            && removeWhen != 0 && removeWhen < lowestValue)
1029            lowestValue = removeWhen;
1030        return lowestValue;
1031    }
1032
1033    /**
1034     * returns true, if this user is valid. Validity is determined in a "soft" way,
1035     * meaning, that users, which don't have a connection at the moment,
1036     * will also be checked for their removeWhen-Timestamp and will stay valid, until
1037     * their removeWhen-Timestamp is reached.
1038     * @return true if valid, false if not
1039     */

1040    public synchronized boolean check(long now) {
1041        switch (state) {
1042            case LOGGING_IN:
1043                long laDiff = now-this.lastActive;
1044                long seDiff = now-this.sessionStart;
1045                if (laDiff < Server.srv.USER_REMOVE_SCHEDULE_TIME*10
1046                    || seDiff < Server.srv.USER_REMOVE_SCHEDULE_TIME*10)
1047                    return true;
1048                else
1049                    return false;
1050            case SCHEDULED_FOR_REMOVAL:
1051                if (this.removeWhen==0) {
1052                    state=LOGGED_IN;
1053                    return true;
1054                }
1055                return this.removeWhen > now;
1056            case SENDING_QUIT_MESSAGE:
1057                return this.removeWhen > now;
1058            case LOGGING_OUT:
1059                return this.removeWhen > now;
1060        }
1061        long tot = lastActive + (away
1062                        ? Server.srv.USER_AWAY_TIMEOUT
1063                        : Server.srv.USER_TIMEOUT);
1064        if (this.hasRight(IUserStates.ROLE_VIP)) {
1065            if (Server.srv.VIP_TIMEOUT<0)
1066                tot = Long.MAX_VALUE;
1067            else if (Server.srv.VIP_TIMEOUT>0)
1068                tot = lastActive + Server.srv.VIP_TIMEOUT;
1069        }
1070        if (grp==null
1071            || !grp.isValid()
1072            || tot <=now) {
1073            this.sendQuitMessage(false);
1074        } else if (sk == null
1075            || !sk.isValid()
1076            || !sk.channel().isOpen())
1077            this.scheduleToRemove();
1078        return true;
1079    }
1080
1081    public String JavaDoc toString() {
1082        StringBuffer JavaDoc tsb = new StringBuffer JavaDoc ("[User ");
1083        tsb.append (name);
1084        tsb.append (" [cookie=");
1085        tsb.append (cookie);
1086        tsb.append (" / state=");
1087        tsb.append (state);
1088        tsb.append (" / grp: ");
1089        tsb.append (grp);
1090        tsb.append (" / sk: ");
1091        if (sk!=null) {
1092            tsb.append ("true valid? ");
1093            tsb.append (sk.isValid());
1094            tsb.append (" open? ");
1095            tsb.append (sk.channel().isOpen());
1096        } else {
1097            tsb.append ("false");
1098        }
1099        tsb.append ("]]");
1100        return tsb.toString();
1101   }
1102
1103    public void finalize() {
1104        if (Server.TRACE_CREATE_AND_FINALIZE)
1105            Server.log(this, "----------------------------------------FINALIZED", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
1106    }
1107    
1108    /**
1109     * Compares the Roles of two users:<br><br>
1110     * returns:<br>
1111     * 2,3 not the same user and u is lower than 'this'<br>
1112     * -1 u is lower than 'this'<br>
1113     * 0 u is equal with 'this'<br>
1114     * 1 u is higher than 'his'<br>
1115     * @param u
1116     * @return
1117     */

1118    public int compareRoleTo(User u) {
1119        int defaultright = this.getDefaultPermissionMap();
1120        if (this.hasRole(IUserStates.ROLE_GOD)) {
1121            if (u.hasRole(IUserStates.ROLE_GOD))
1122                return 0;
1123        } else if (this.hasRole(IUserStates.ROLE_VIP)) {
1124            if (u.hasRole(IUserStates.ROLE_GOD))
1125                return 1;
1126            else if (u.hasRole(IUserStates.ROLE_VIP))
1127                return 0;
1128            else if (u.hasRole(IUserStates.ROLE_USER))
1129                return 2;
1130        } else if (this.hasRole(IUserStates.ROLE_USER)) {
1131            if (!u.equals(this) && u.hasRole(IUserStates.ROLE_USER) && !u.hasRight(defaultright))
1132                return 2;
1133            if (!u.equals(this) && u.hasRole(IUserStates.ROLE_VIP) && !u.hasRight(defaultright))
1134                return 3;
1135            if (u.hasRole(IUserStates.ROLE_GOD))
1136                return 1;
1137            if (u.hasRole(IUserStates.ROLE_VIP))
1138                return 1;
1139            else if (u.hasRole(IUserStates.ROLE_USER))
1140                return 0;
1141        } else {
1142            // this must be an asshole
1143
if (u.hasRole(IUserStates.ROLE_ASSHOLE) && defaultright == IUserStates.ROLE_USER )
1144                return 2;
1145            if (u.hasRole(IUserStates.ROLE_ASSHOLE))
1146                return 0;
1147            else
1148                return 1;
1149        }
1150        return -1;
1151    }
1152}
Popular Tags