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

📄 usermanager.java

📁 JAVA平台下优秀的CHART开源代码,可以实现类似EXCEL中的二维或三维的饼图/椎图功能.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * Copyright (C) 2003  Manfred Andres
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package freecs.core;

import freecs.*;
import freecs.interfaces.*;
import freecs.layout.TemplateSet;

import java.util.HashMap;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Iterator;
import java.nio.channels.SelectionKey;
import java.util.ConcurrentModificationException;

public class UserManager extends Thread implements IUserRights, IMessageDestination {
   public static final UserManager mgr = new UserManager();
   public static int highWaterMark = 0;

   private Vector    removableUsers;
   private HashMap usrCookie;
   private HashMap usrName;
   private HashMap fshipList;
   private int       usrCount=0;
   private Vector    schedule, onlineVips, moderators, vips, loggingIn;

   private UserManager () {
      fshipList = new HashMap ();
      usrCookie = new HashMap ();
      usrName = new HashMap ();
      onlineVips = new Vector();
      vips=new Vector();
      moderators=new Vector();
      removableUsers = new Vector ();
      this.setName ("UserManager");
      schedule = new Vector ();
      loggingIn = new Vector();
   }
   
	public static void startUserManager() {
   		mgr.start();
	}

	/**
	 * returns the number of active users
	 */
	public int getActiveUserCount () {
		return Math.min(usrCookie.size(), usrName.size()); 
	}

   public static final short  LOGIN_OK                = 0,
                              LOGIN_MISSING           = 1,
                              LOGIN_COOKIE_MISSING    = 2,
                              LOGIN_GROUP_MISSING     = 3,
                              LOGIN_GROUP_NOSTART     = 4,
                              LOGIN_GROUP_BAN		  = 5,
                              LOGIN_GROUP_LOCK		  = 6,
                              LOGIN_COOKIE_DUPLICATE  = 7,
                              LOGIN_PRESENT           = 8,
                              LOGIN_RELOAD            = 9,
                              LOGIN_FAILED            =10,
                              TECHNICAL_ERROR         =11,
                              MAX_USERS               =12;
   /**
    * try to login this user. returns integer containing 0 for ok or any other number (treated as error-code)
    * @return short 0 = ok, erverything else is an errorcode
    */
	public short tryLogin (String uname, String pwd, String grp, TemplateSet ts, String cookie, RequestReader req) {
		synchronized (loggingIn) {
			if (loggingIn.contains(uname.toLowerCase()))
				return LOGIN_RELOAD;
			loggingIn.add(uname.toLowerCase());
		}
		short result = execTryLogin (uname, pwd, grp, ts, cookie, req);
		synchronized (loggingIn) {
			while (loggingIn.contains(uname.toLowerCase()))
				loggingIn.remove(uname.toLowerCase());
		}
		return result;
	}
   private short execTryLogin (String uname, String pwd, String grp, TemplateSet ts, String cookie, RequestReader req) {
   		req.currPosition = RequestReader.TRYLOGIN;
		if (usrCookie.size () >= Server.srv.MAX_USERS)
			return MAX_USERS;
		if (uname == null || uname.length () < 1)
			return LOGIN_MISSING;
		if (cookie == null || cookie.length () < 1)
			return LOGIN_COOKIE_MISSING;
		if (grp == null || grp.length () < 1)
			return LOGIN_GROUP_MISSING;
		req.currPosition = RequestReader.TRYLOGIN_CHECK4PRESENCE;
		User un, uc;
		un = (User) usrName.get (uname.toLowerCase ());
		uc = (User) usrCookie.get (cookie);
		if (un != null && uc == null)
			return LOGIN_PRESENT;
		if (un == null && uc != null)
			return LOGIN_COOKIE_DUPLICATE;
		if (un != null && uc != null && un.equals (uc))
			return LOGIN_RELOAD;
		if (un != null && uc != null)
			return LOGIN_FAILED;
		User nu;
		req.currPosition = RequestReader.TRYLOGIN_AUTHENTICATE;
		try {
			nu = Server.srv.auth.loginUser (uname, pwd, cookie);
		} catch (Exception e) {
			Server.debug ("UserManager.tryLogin:", e, Server.MSG_ERROR, Server.LVL_MAJOR);
			return TECHNICAL_ERROR;
		}
		if (nu == null)
			return LOGIN_FAILED;
		req.currPosition = RequestReader.TRYLOGIN_CORRECT_PERMISSION;
		
		// check user-rights here. If user-rights equal 0, the rights will
		// be corrected to the IUserRights.ROLE_USER.
		if (nu.hasRole(0) || nu.hasRole(Integer.MIN_VALUE))
			nu.setPermission(IUserRights.ROLE_USER);
		// if the user has IUserRights.ROLE_USER, the config will be checked
		// for the user-rights
		if (nu.hasRole(IUserRights.ROLE_USER)) {
			String tname = nu.getName().trim().toLowerCase();
			if (moderators.contains (tname)) {
				nu.setPermission (IUserRights.ROLE_MODERATOR);
			} else if (vips.contains (tname)) {
				nu.setPermission (IUserRights.ROLE_VIP);
			}
		}
		req.currPosition = RequestReader.TRYLOGIN_SET_GROUP;
		Group g = GroupManager.mgr.getStartingGroup (grp);
		if (g == null)
			return LOGIN_GROUP_NOSTART;
		if (g.usrIsBaned(uname))
			return LOGIN_GROUP_BAN;
		if (!g.hasState(IGroupState.OPEN) && !nu.hasRight(IUserRights.MAY_JOIN_LOCKED_GROUP))
			return LOGIN_GROUP_LOCK;
		addUser (nu);
		req.currPosition = RequestReader.TRYLOGIN_SEND_LOGINMSG;
		MessageParser mp = new MessageParser ();
		mp.setSender (nu);
		mp.setMessageTemplate ("message.user.join.server");
		g.sendMessage (mp);
		g.addLoginUser (nu);
		req.currPosition = RequestReader.TRYLOGIN_CHECK_FRIENDS;
		Vector f = (Vector) fshipList.get (nu.getName ());
		if (f != null) {
			MessageParser mpr = new MessageParser ();
			mpr.setUsercontext (nu);
			mpr.setSource (nu.getName ());
			mpr.setTarget (g.getName ());
			mpr.setMessageTemplate ("message.f.joined");
			for (Enumeration e = f.elements (); e.hasMoreElements (); ) {
				User cu = (User) e.nextElement ();
				cu.sendMessage (mpr);
			}
		}
		req.currPosition = RequestReader.TRYLOGIN_SET_PERMISSION;
		nu.setTemplateSet (ts);
		req.currPosition = RequestReader.TRYLOGIN_SCHEDULE_VIPMSG;
		if (nu.hasRight(IUserRights.ROLE_VIP)) {
			MessageParser mpr = new MessageParser ();
			mpr.setMessageTemplate ("message.user.vip");
			nu.scheduleMessage (mpr);
			onlineVips.add(nu);
		}
		req.currPosition = RequestReader.TRYLOGIN_SCHEDULE_FRIENDMSGS;
		for (Enumeration e = nu.friends (); e.hasMoreElements (); ) {
			String fname = (String) e.nextElement ();
			User cu = getUserByName (fname);
			if (cu == null) 
				continue;
			MessageParser mpr = new MessageParser ();
			mpr.setUsercontext (cu);
			mpr.setSource (cu.getName ());
			Group gg = cu.getGroup ();
			mpr.setTarget (gg == null ? "undef" : gg.getName ());
			mpr.setMessageTemplate ("message.f.isOnline");
			nu.scheduleMessage (mpr);
		}
		return LOGIN_OK;
	}

   /**
    * adds a user to the lists
    * @param u The user to add
    */
	public void addUser (User u) {
		usrCookie.put (u.getCookie (), u);
		usrName.put (u.getName ().toLowerCase (), u);
		usrCount++;
		if (usrCount > highWaterMark)
			highWaterMark = usrCount;
	}

   /**
    * removes a user from the userlists
    * @param u The user to remove
    */
   public void removeUser (User u) {
      removeUser (u, false);
   }
   /**
    * removes a user from the userlists. if b is true, the user was
    * kicked from the server
    * @param u the user to remove
    * @param b if true the user was kicked, if not the user just left
    */
	public void removeUser (User u, boolean b) {
		usrCookie.remove ( u.getCookie ());
		usrName.remove ( u.getName ().toLowerCase ());
		usrCount--;
		while (removableUsers.contains (u)) 
			removableUsers.removeElement (u);
		if (u.hasRight(IUserRights.ROLE_VIP)) {
      		onlineVips.removeElement(u);
		}
		u.removeNow (b);
	}

   /**
    * returns the user identifyed by the give cookie-value
    * @param c  the cookie-value
    * @return User the user identified by this cookie-value
    */
   public User getUserByCookie (String c) {
      return (User) usrCookie.get (c);
   }

   /**
    * returns the user identifyed by the give name
    * @param n  the name
    * @return User the user identified by this name
    */
   public User getUserByName (String n) {
      return ((User) usrName.get (n.toLowerCase ()));
   }

   /**
    * schedules this user to be removed
    */
   public void scheduleToRemove (User u) {
   	  if (u.getRemoveWhen() == 0) 
   	  	return;
      removableUsers.addElement (u);
   }

	/**
	 * adds a friendship-relation
	 * @param u The user which has registered fname as friend
	 * @param fname the name of the user which will be registered with u
	 */
   public void addFriendship (User u, String fname) {
      Vector f = (Vector) fshipList.get (fname);

⌨️ 快捷键说明

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