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

📄 groupmanager.java

📁 使用工具jublider开发的一个聊天室实现基本功能,
💻 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 java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Iterator;
/**
 * The GroupManager manages all groups. It is a single instantiable Thread
 *
 * freecs.core
 */
public class GroupManager implements IGroupState {
   public static final GroupManager mgr = new GroupManager ();
   public static final HashMap pluginStore = new HashMap();
   private Map grps = new HashMap ();
   private Group[] grpsArr = new Group[0];
   private int highWaterMark=0;
   private Vector moderatedGroups=new Vector(), startGroups=new Vector();
   private volatile long lastModified=0;
   private volatile long groupListLastChange=0;
   public HashMap startGroupThemes=new HashMap();

	private GroupManager () {
	}

   /**
    * adds a group to the map of available groups
    * @param g the Group to add
    */
	private synchronized void addGroup (Group g) {
		grps.put (g.getKey (), g);
        lastModified=System.currentTimeMillis();
        grpsArr=null;
		if (grps.size () > highWaterMark)
			highWaterMark = grps.size();
	}
	
	public int getHighWaterMark () {
		return highWaterMark;
	}

   /**
    * removes a group
    * @param g the Group-Object to be removed
    */
    public synchronized void removeGroup (Group g) {
        if (g == null)
            return;
        lastModified=System.currentTimeMillis();
        g.invalidate();
        grps.remove (g.getKey ());
        IGroupPlugin[] plugins = g.getPlugins();
        if (plugins!=null) {
            for (int i = 0; i<plugins.length; i++) {
                try {
                    plugins[i].remove();
                } catch (Exception e) {
                    Server.debug(plugins[i], "catched exception from extension while removing: ", e, Server.MSG_ERROR, Server.LVL_MAJOR);
                }
            }
        }
        grpsArr=null;
    }

   /**
    * Return the group identified by gName
    * @param gName the name of this group
    * @return the group identified by gName || null if none existent
    */
	public Group getGroup (String gName) {
		if (gName==null) 
			return null;
		return (Group) grps.get (gName.toLowerCase ().trim());
	}

	/**
	 * Return the startinggroup identified by gName
	 * @param gName
	 * @return the group identified by gName || null if none existent
	 */
   public Group getStartingGroup (String gName) {
   	  Group g;
      g = (Group) grps.get (gName.toLowerCase ().trim());
      if (g == null) return null;
      if (!g.hasState(IGroupState.ENTRANCE)) {
         return null;
      }
      return g;
   }

	/**
	 * Opens a new group
	 * @param groupname The name of the group
	 * @param topic The theme to use for this group (possible null)
	 * @param opener The opener of this group
	 * @return the newly created group or the group this user has joined
	 */
   public Group openGroup (String groupname, String topic, User opener) {
      return openGroup (groupname, topic, opener, opener);
   }

	/**
	 * Opens a new group checking the rights of another user
	 * @param groupname The name of the group
	 * @param topic The theme to use for this group (possible null)
	 * @param opener The opener of this group
	 * @param rUser The user to check the rights for opening this group
	 * @return the newly created group or the group this user has joined
	 */
    public synchronized Group openGroup (String groupname, String topic, User opener, User rUser) {
        if (Server.srv.MAX_GROUPNAME_LENGTH > 0 && groupname.length() > Server.srv.MAX_GROUPNAME_LENGTH)
            return null;
        if (isStartingGroup(groupname)
                && (!opener.hasRight(IUserStates.ROLE_VIP)
                        || topic==null))
            topic = (String) startGroupThemes.get(groupname.trim().toLowerCase());
        boolean moderated = moderatedGroups.contains(groupname.trim().toLowerCase());
		if (moderated && !rUser.hasRight(IUserStates.MAY_OPEN_MODERATED_GROUP))
				return null;
		Group g = getGroup(groupname);
		// if the group is already opened, try to add 
        // the given user and return the group on success
        if (g != null) {
            return g.addUser(opener, rUser) ? g : null;
        }
		if (!rUser.hasRight (IUserStates.MAY_OPEN_GROUP)) 
			return null;
		g = new Group (groupname, topic);
        if (isStartingGroup(g.getRawName()))
            g.setState(IGroupState.ENTRANCE);
		if (moderated)
			g.setState(IGroupState.MODERATED);
        StringBuffer sb = new StringBuffer (Server.BASE_PATH);
        sb.append ("/grouppolicies/");
        sb.append (groupname.toLowerCase());
        sb.append (".properties");
        File f = new File (sb.toString());
        if (f.exists()) {
            g = checkProperties (f, g, rUser);
            if (g == null)
                return null;
        }
		if (!g.addUser (opener))
            return null;
		addGroup (g);
		return g;
    }
    
    private Group checkProperties (File f, Group g, User rUser) {
        Properties props = new Properties();
        try {
            FileInputStream in = new FileInputStream(f);
            props.load(in);
            in.close();
        } catch (FileNotFoundException fnfe) {
            return g;
        } catch (IOException ioe) {
            Server.debug(this, "crateByProperties:", ioe, Server.MSG_ERROR, Server.LVL_HALT);
            return g;
        }
        HashMap map = new HashMap();
        for (Iterator i = props.keySet().iterator(); i.hasNext(); ) {
            String key = i.next().toString();
            String low = key.toLowerCase();
            String val = props.getProperty(key);
            if ("moderated".equals(low) 
                && "true".equals(val)) {
                if (!rUser.hasRight(IUserStates.MAY_OPEN_MODERATED_GROUP))
                    return null;
                g.setState(IGroupState.MODERATED);
            } else if ("timelock".equals(low)) {
                if (!g.hasState(IGroupState.MODERATED))
                    return null;
                try {
                    int sec = Integer.parseInt(val);
                    g.setTimelockSec(sec);
                } catch (NumberFormatException nfe) {
                    Server.log(g, "Timelock value isn't a number", Server.MSG_ERROR, Server.LVL_MINOR);
                }
            } else if ("minuserrole".equals(low)) {
                int r = 0;
                if ("vip".equals(val))
                    r = IUserStates.ROLE_VIP;
                else if ("moderator".equals(val))
                    r = IUserStates.IS_MODERATOR;

⌨️ 快捷键说明

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