⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 user.java

📁 使用工具jublider开发的一个聊天室实现基本功能,
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            lastRecievedMessage = now;
        } catch (Exception e) {
            Server.debug ("[User " + name + "]", "touch: catched exception during touch", e, Server.MSG_ERROR, Server.LVL_MAJOR);
        }
    }

   /**
    * sends the message to this user
    * @param mc the message-container
    */
	public void sendMessage (IContainer mc) {
		if (mc==null) 
			return;
		if (state!=LOGGED_IN) 
			return;
		if (mc instanceof MessageParser) {
			User sender = ((MessageParser)mc).getSender();
	      	if (sender!= null && ignoreList.contains(sender.getName().toLowerCase()) && !sender.hasRight(IUserStates.ROLE_VIP))
	         	return;
		}
		if (state!=LOGGING_IN && sk == null) {
			Server.log("[User " + name + "]", "sendMessage: selectionkey was null", Server.MSG_STATE, Server.LVL_MINOR);
			scheduleToRemove ();
			return;
		} else if (sk==null)
			return;
        implSendMessage(mc);
	}
    
    private void implSendMessage (IContainer mc) {
        if (mc instanceof PersonalizedMessage) {
            ConnectionBuffer cb = (ConnectionBuffer) sk.attachment();
            cb.addToWrite(mc.getByteBuffer());
            if (mc.closeSocket())
                cb.addToWrite(Responder.CLOSE_CONNECTION);
            lastRecievedMessage = System.currentTimeMillis ();
            return;
        }
        ((MessageParser) mc).setHTTP11 (isHTTP11 && Server.srv.USE_HTTP11);
        ConnectionBuffer cb = (ConnectionBuffer) sk.attachment();
        if (!((MessageParser) mc).addPersonalizedMessage (this, cb)) {
            Server.log ("[User " + name + "]", "sendMessage: there was nothing to send", Server.MSG_TRAFFIC, Server.LVL_VERY_VERBOSE);
            return;
        }
        lastRecievedMessage = System.currentTimeMillis ();
    }


   /**
    * return an Enumeration containing only this user
    */
   public Iterator users () {
      return new Iterator () {
         boolean userNotReturned = true;
         public boolean hasNext () {
            return userNotReturned;
         }
         public Object next () {
            userNotReturned = false;
            return this;
         }
         public void remove () { }
      };
   }

   /**
    * checks if the given user is ignored by this user
    * @param u the user to check if it is ignored by this user
    */
   public boolean userIsIgnored (User u) {
      return ignoreList.contains (u);
   }

   /**
    * this user will ignore the given user if called
    * @param u the user to be ignored by this user
    */
	public void ignoreUser (User u) {
		String uname = u.getName().toLowerCase();
        ignoreUser(uname);
	}
    
    /**
     * add the username to the ignorelist of this user
     * @param uname the username to add to this user's ignorelist
     */
    public void ignoreUser (String uname) {
        if (ignoreList.contains (uname)) 
            return;
/*      WE MUST NOT remove the user from the invitedBy-state
        This would give the anoying user possibility to reinvite this user
        if (invitedBy != null && invitedBy.equals (u)) {
            invitedBy = null;
            invitedTo = null;
        } */
        ignoreList.addElement (uname);
    }

   /**
    * this user will no longer ignore the given user
    * @param u the user to respect again
    */
	public void respectUser (User u) {
		String uname = u.getName().toLowerCase();
		while (ignoreList.contains (uname)) ignoreList.removeElement (uname);
   }

   /**
    * sets the id of this user
    * @param id the id of this user
    */
   public void setID (String id) {
      this.id = id;
   }

   /**
    * gets the id of this user
    * @return the id of this user
    */
   public String getID () {
      return id;
   }

// ***************************** STATE-QUERY and SET
   /**
    * sets right for this user
    * @param right the rightset for this user
    * @see freecs.interfaces.IUserStates
    */
    public void setPermission (int right) {
       this.permissionMap = right;
       this.defaultPermissionMap = right;
    }
    public void setDefaultPermissionMap (int right) {
          this.defaultPermissionMap = right;
    }
    public void setDefaultMembershipPermission (int right) {
        this.defaultMembershipPermissionMap = right;
    }
    public int getPermissionMap () {
        return this.permissionMap;
    }
    public int getDefaultMembershipPermissionMap () {
        return this.defaultMembershipPermissionMap;
    }
    public int getDefaultPermissionMap () {
        return this.defaultPermissionMap;
    }

    public void setNewPermission (int right) {
        this.permissionMap = right;
    }
    
    public void resetPermission () {
    	this.permissionMap = this.defaultPermissionMap;
    }

    public synchronized void addMembership (String key, Membership m) {
        if (defaultMembership == null)
            defaultMembership = m;
        memberships.put(key, m);
    }
    
    public Membership getMembership (String key) {
        return (Membership) memberships.get(key);
    }
    
    public Membership getDefaultMembership () {
        return defaultMembership;
    }

    public synchronized void removeMembership (String key) {
        memberships.remove(key);
        rebuildMemberships();
    }
    
    public synchronized void rebuildMemberships () {
        for (Iterator i = memberships.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry entry = (Map.Entry)i.next();
            Membership m = (Membership) entry.getValue();
            m.add(this);
        }
    }

   /**
    * grants a specific right to this user additional to previous ones
    * @param right
    * @see freecs.interfaces.IUserStates
    */
   public void givePermission (int right) {
      this.permissionMap = this.permissionMap | (right - (this.permissionMap & right));
   }

   /**
    * removes right of this user
    * @param right
    * @see freecs.interfaces.IUserStates
    */
   public void takePermission (int right) {
      this.permissionMap = this.permissionMap - (this.permissionMap & right);
   }

   /**
    * determines if this user has the given right
    * @param right the right this user will be queried for
    * @return true if all flaggs in right are set for this user
    */
   public boolean hasRight (int right) {
      return ((this.permissionMap & right) == right);
   }

   public boolean hasRole (int role) {
       int clean = this.permissionMap - (this.permissionMap & (IUserStates.IS_GUEST | IUserStates.IS_MODERATOR));
       return clean == role;
   }

   public boolean hasDefaultRight (int right) {
       return ((this.defaultPermissionMap & right) == right);
   }
   /**
    * set away-state
    * @param b true if user is away; false otherwhise
    */
   public void setAway (boolean b) {
   	  if (b) { 
          awayStart = System.currentTimeMillis ();
      } else {
          awayTime += System.currentTimeMillis () - awayStart;
      }
      away = b;
   }

   /**
    * set away-state
    * @return away true if user is away; false otherwhise
    */
   public boolean isAway () {
      return away;
   }
   
   /**
    * returns the time this user switched to the away-state
    * @return the away-time of this user
    */
   public long awayTime () {
   	  return awayTime;
   }

	/**
	 * checks if this user is punished
	 * @return true if the user is punished
	 */
   public boolean isPunished () {
      return isPunished;
   }

	/**
	 * sets the punishment-state of this user
	 * @param p if true, the user will be switched into punished mode
	 */
   public void setPunish (boolean p) {
      isPunished = p;
   }

	/**
	 * sets the invitation of a user for this user
	 * @param u the user inviting this user
	 * @return true if the user doesn't ignore the other user
	 */
   public boolean invitedFrom (User u) {
      if (userIsIgnored(u)) return false;
      this.invitedTo = u.getGroup ();
      this.invitedBy = u;
      return true;
   }

	/**
	 * unsets the invitation-state of this user
	 */
   public void unsetInvitedTo () {
      invitedBy = null;
      invitedTo = null;
   }

	/**
	 * returns the user which has invited this user
	 * @return the inviter of this user
	 */
   public User invitedBy () {
      return invitedBy;
   }

	/**
	 * returns the group to which this user was invited to
	 * @return the group this user is invited to
	 */
   public Group invitedTo () {
      return invitedTo;
   }

	/**
	 * get the color-code for this user
	 * @return the color-code for this user
	 */
   public String getColCode () {
      return colCode;
   }

	/**
	 * Set the colorcode for this user. This method is only for internal
	 * use (e.g. assigning a colorcode automatically from the database). A
	 * colorchange triggered by the user must be done by changeColCode 
	 * @param cCode the colorcode this user will recieve
	 */
   public void setColCode (String cCode) {
      colCode = cCode;
   }
   
   /**
    * This is used for user-triggered colorchanges.
    * @param cCode the color-code wanted
    * @return true if allowed, if not false
    */
   public boolean changeColCode (String cCode) {
        long now = System.currentTimeMillis ();
        if ((now - lastColChange) < Server.srv.COLOR_CHANGE_INTERVAL) 
            return false;
        colCode = cCode;
        lastColChange = now;
        return true;
    }

	/**
	 * returns the templateset this user has choosen
	 * @return the templateset this user has choosen
	 */
   public TemplateSet getTemplateSet () {
      if (this.ts == null) ts = Server.srv.templatemanager.getTemplateSet ("default");
      return ts;
   }

	/**
	 * set the templateset to use for this user
	 * @param ts the templateset to use
	 */
   public void setTemplateSet (TemplateSet ts) {
      this.ts = ts;
   }

	/**
	 * return the value of the given property of this user
	 * @param k the name of the property
	 * FIXME: this should also use the templateset of this user
	 * @return the value of the property of this user
	 */
   public Object getProperty (String k) {
      if (k.equals ("isaway")) {
         return (away ? "away" : "");
      } else if (k.equals("awaymessage")) {
      	 if (!away) 
      	 	return ("");
      	 String msg = getAwayMessage();
      	 if (msg.equals(""))
      	 	return (Boolean.FALSE);
		 return (msg);
      } else if (k.equals ("sessionstart.hour")) {
         return Server.formatTimeStamp (sessionStart, Server.hourSDF);
      } else if (k.equals ("sessionstart.minute")) {
         return Server.formatTimeStamp (sessionStart, Server.minuteSDF);
      } else if (k.equals ("idletime")) {
         long diff = System.currentTimeMillis () - lastActive;
         diff = Math.round (diff / 1000);
         return (String.valueOf (diff));
      } else if (k.equals ("sessiontime")) {
         long l = (System.currentTimeMillis () - sessionStart) / 1000 / 60;
         StringBuffer tsb = new StringBuffer ();
         if (l > 60) {
            long stdn = l / 60;
            if (stdn == 1)
               tsb.append ("einer Stunde ");
            else {
               tsb.append (stdn);
               tsb.append (" Stunden ");
            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -