1 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 ; 25 import java.util.Enumeration ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 import java.util.Vector ; 29 import java.nio.ByteBuffer ; 30 import java.nio.channels.SelectionKey ; 31 32 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 name, cookie, colCode, awayMessage, id; 46 private String customTitle; 47 private volatile int permissionMap, defaultPermissionMap, defaultMembershipPermissionMap, hashCode=Integer.MIN_VALUE, 48 flooded=0, questionCounter=0, tooled=0; 49 private HashMap 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 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 schedMsgs; 62 private volatile SelectionKey sk; 63 private TemplateSet ts; 64 private volatile transient User ownPrivateUser = null, foreignPrivateUser = null; public Connection conn; 67 68 private HashMap memberships = new HashMap (); 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 78 public User(String name, String cookie) { 79 this.colCode = null; 80 this.name = name; 81 this.cookie = cookie; 82 this.userProps = new HashMap (); 83 this.sessionStart = System.currentTimeMillis (); 84 this.lastRecievedMessage = this.sessionStart; 85 this.lastActive = this.sessionStart; 86 this.ignoreList = new Vector (); 87 this.friendsList = new Vector (); 88 this.schedMsgs = new Vector (); 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 100 public void scheduleMessage (MessageParser mpr) { 101 if (mpr == null) return; 102 synchronized (this) { 103 if (schedMsgs == null) 104 schedMsgs = new Vector (); 105 schedMsgs.addElement (mpr); 106 } 107 } 108 109 112 public void sendScheduledMessages() { 113 if (schedMsgs == null) return; 114 for (Iterator 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 130 public boolean isRemoving () { 131 return this.state==SCHEDULED_FOR_REMOVAL; 132 } 133 134 138 public void scheduleToRemove () { 139 if (this.state>=SCHEDULED_FOR_REMOVAL) 140 return; 141 this.state=SCHEDULED_FOR_REMOVAL; 142 StringBuffer tsb = new StringBuffer ("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 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 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 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 i = memberships.entrySet().iterator(); i.hasNext(); ) { 206 Map.Entry entry = (Map.Entry ) i.next(); 207 Membership m = (Membership) entry.getValue(); 208 m.remove(this); 209 } 210 211 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 try { 225 Server.srv.auth.logoutUser (this); 226 } catch (Exception e) { 227 Server.debug ("[User " + name + "]", "removeNow: Exception during logout", e, Server.MSG_ERROR, Server.LVL_MAJOR); 228 } 229 for (Enumeration e = friendsList.elements (); e.hasMoreElements (); ) { 230 String fname = (String ) e.nextElement (); 231 UserManager.mgr.removeFriendship (this, fname); 232 } 233 StringBuffer tsb = new StringBuffer ("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 247 public long getRemoveWhen () { 248 return removeWhen; 249 } 250 251 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 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 310 public Group getGroup () { 311 return grp; 312 } 313 314 318 public String getCookie () { 319 return cookie; 320 } 321 322 326 public long lastActive () { 327 return lastActive; 328 } 329 330 334 public synchronized void setKey (SelectionKey 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 351 public SelectionKey getKey () { 352 return sk; 353 } 354 355 356 359 360 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 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 e) { 387 Server.debug ("[User " + name + "]", "touch: catched exception during touch", e, Server.MSG_ERROR, Server.LVL_MAJOR); 388 } 389 } 390 391 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 436 public Iterator users () { 437 return new Iterator () { 438 boolean userNotReturned = true; 439 public boolean hasNext () { 440 return userNotReturned; 441 } 442 public Object next () { 443 userNotReturned = false; 444 return this; 445 } 446 public void remove () { } 447 }; 448 } 449 450 454 public boolean userIsIgnored (User u) { 455 return ignoreList.contains (u); 456 } 457 458 462 public void ignoreUser (User u) { 463 String uname = u.getName().toLowerCase(); 464 ignoreUser(uname); 465 } 466 467 471 public void ignoreUser (String uname) { 472 if (ignoreList.contains (uname)) 473 return; 474 480 ignoreList.addElement (uname); 481 } 482 483 487 public void respectUser (User u) { 488 String uname = u.getName().toLowerCase(); 489 while (ignoreList.contains (uname)) ignoreList.removeElement (uname); 490 } 491 492 496 public void setID (String id) { 497 this.id = id; 498 } 499 500 504 public String getID () { 505 return id; 506 } 507 508 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 key, Membership m) { 543 if (defaultMembership == null) 544 defaultMembership = m; 545 memberships.put(key, m); 546 } 547 548 public Membership getMembership (String key) { 549 return (Membership) memberships.get(key); 550 } 551 552 public Membership getDefaultMembership () { 553 return defaultMembership; 554 } 555 556 public synchronized void removeMembership (String key) { 557 memberships.remove(key); 558 rebuildMemberships(); 559 } 560 561 public synchronized void rebuildMemberships () { 562 for (Iterator i = memberships.entrySet().iterator(); i.hasNext(); ) { 563 Map.Entry entry = (Map.Entry )i.next(); 564 Membership m = (Membership) entry.getValue(); 565 m.add(this); 566 } 567 } 568 569 574 public void givePermission (int right) { 575 this.permissionMap = this.permissionMap | (right - (this.permissionMap & right)); 576 } 577 578 583 public void takePermission (int right) { 584 this.permissionMap = this.permissionMap - (this.permissionMap & right); 585 } 586 587 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 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 621 public boolean isAway () { 622 return away; 623 } 624 625 629 public long awayTime () { 630 return awayTime; 631 } 632 633 637 public boolean isPunished () { 638 return isPunished; 639 } 640 641 645 public void setPunish (boolean p) { 646 isPunished = p; 647 } 648 649 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 664 public void unsetInvitedTo () { 665 invitedBy = null; 666 invitedTo = null; 667 } 668 669 673 public User invitedBy () { 674 return invitedBy; 675 } 676 677 681 public Group invitedTo () { 682 return invitedTo; 683 } 684 685 689 public String getColCode () { 690 return colCode; 691 } 692 693 699 public void setColCode (String cCode) { 700 colCode = cCode; 701 } 702 703 708 public boolean changeColCode (String 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 721 public TemplateSet getTemplateSet () { 722 if (this.ts == null) ts = Server.srv.templatemanager.getTemplateSet ("default"); 723 return ts; 724 } 725 726 730 public void setTemplateSet (TemplateSet ts) { 731 this.ts = ts; 732 } 733 734 740 public Object getProperty (String k) { 741 if (k.equals ("isaway")) { 742 return (away ? "away" : ""); 743 } else if (k.equals("awaymessage")) { 744 if (!away) 745 return (""); 746 String 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 tsb = new StringBuffer (); 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 787 public void setProperty (String k, Object v) { 788 userProps.put (k, v); 789 } 790 791 795 public long getSessionStart () { 796 return sessionStart; 797 } 798 799 803 public void setHTTP11 (boolean b) { 804 isHTTP11 = b; 805 } 806 807 811 public String getName () { 812 return (name); 813 } 814 815 819 public void addFriend (String fname) { 820 if (friendsList.contains (fname)) return; 821 UserManager.mgr.addFriendship (this, fname); 822 friendsList.addElement (fname); 823 } 824 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 name) { 836 String n = name.trim().toLowerCase(); 837 return this.friendsList.contains(n); 838 } 839 840 844 public Enumeration friends () { 845 return friendsList.elements (); 846 } 847 848 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 876 public int getQuestionCounter () { 877 return questionCounter; 878 } 879 882 public void incrementQuestionCounter () { 883 questionCounter++; 884 } 885 886 890 public boolean isLoggedOut () { 891 return state==LOGGED_OUT; 892 } 893 894 898 public boolean isLoggedIn () { 899 return state==LOGGED_IN; 900 } 901 902 906 public void setPrivateUser (User pu) { 907 this.ownPrivateUser = pu; 908 } 909 910 914 public User getPrivateUser () { 915 return ownPrivateUser; 916 } 917 918 922 public void setForeignPrivateUser (User pu) { 923 this.foreignPrivateUser = pu; 924 } 925 926 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 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 978 public void setAwayMessage(String param) { 979 awayMessage = param; 980 } 981 985 public String getAwayMessage () { 986 return awayMessage == null ? "" : awayMessage; 987 } 988 989 993 public long getChattime () { 994 Object ct = getProperty ("chattime"); 995 if (ct == null) { 996 return ((lastActive () - getSessionStart ()) - awayTime ()) / 1000; 997 } 998 return (((lastActive () - getSessionStart ()) - awayTime ()) / 1000) + ((Long ) 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 1013 public void setCustomTitle (String et) { 1014 customTitle = et; 1015 } 1016 1017 1021 public String 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 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 toString() { 1082 StringBuffer tsb = new StringBuffer ("[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 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 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 |