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

📄 group.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.Server;
import freecs.interfaces.*;
import freecs.util.EntityDecoder;

import java.util.List;
import java.util.Vector;
import java.util.Iterator;

public class Group implements IGroupState, IMessageDestination {
   private IGroupPlugin[] plugins;
   private String       suForbiddenMembership;
   private String       name, key;
   private String       theme;
   private String       saveName, saveTheme;
   private int          state;
   private int          minRight=IUserStates.MAY_JOIN_GROUP;
   private int          questionCntr = 0, joinpunishedCntr = 0;
   private int			timelockSec = 60;
   private int          minSetSuRole = IUserStates.ROLE_USER;
   private User[]       usrArr;
   private Vector       usr;
   private Vector       susers;
   private Vector       banList;
   private Vector       autoSuList;
   private Membership[] memberRoom;
   private volatile boolean valid;

   public Group (String name, String theme) {
      this.name   = name;
      this.saveName = EntityDecoder.charToHtml(name);
      this.key = name.toLowerCase ().trim();
      this.theme  = theme;
      this.saveTheme = EntityDecoder.charToHtml(theme);
      this.state  = OPEN | AUTO_SU_FIRST | ALLOW_SU;
      this.usr  = new Vector ();
      this.susers = new Vector ();
      this.banList = new Vector ();
      this.valid = true;
      if (Server.TRACE_CREATE_AND_FINALIZE)
          Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
   }

   public Group (String name, String theme, int state) {
      this.name   = name;
      this.saveName = EntityDecoder.charToHtml(name);
      this.key = name.toLowerCase ().trim();
      this.theme  = theme;
      this.saveTheme = EntityDecoder.charToHtml(theme);
      this.state  = state;
      this.usr  = new Vector ();
      this.susers = new Vector ();
      this.banList = new Vector ();
      this.valid = true;
      if (Server.TRACE_CREATE_AND_FINALIZE)
          Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
   }

   /**
    * returns the name of this group
    * @return the name of this group
    */
   public String getName () {
      return saveName;
   }
   
    public String getRawName() {
   	    return name;
    }
   
   /**
    * @return the key of this group
    */
   public String getKey() {
       return key;
   }


   public void setPlugins (IGroupPlugin[] plugins) {
       this.plugins = plugins;
   }
   public IGroupPlugin[] getPlugins () {
       return plugins;
   }
	/**
	 * set the theme of this group
	 * @param t the theme to use for this group
	 */
   public void setTheme (String t) {
      	theme = t;
      	saveTheme = EntityDecoder.charToHtml(t);
   }
   /**
    * returns the theme of this group
    * @return the theme of this group
    */
   public String getTheme () {
      return saveTheme;
   }

    public void setMemberRoom (Membership[] memberRoom) {
        this.memberRoom = memberRoom;
    }
    
    public Membership[] getMemberships () {
        return memberRoom;
    }
    /**
     * adds this user to this group
     * @param u The user joining this Group
     */
    public boolean addUser (User u) {
        GroupManager.mgr.updateGroupListLastModified();
   		return (addUser (u, u, false));
    }
   
	public void addLoginUser (User u) {
        synchronized (this) {
    		usr.addElement(u);
            usrArr=(User[]) usr.toArray(new User[0]);
        }
        
		u.setGroup(this);
		if (autoSuList != null
	            && autoSuList.contains(u.getName().toLowerCase())) {
	                addToSusers(u);
	        }
        if (plugins!=null) {
            for (int i = 0; i<plugins.length; i++) {
                try {
                    plugins[i].usrJoin(u);
                } catch (Exception e) {
                    Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR);
                }
            }
        }
	}
   /**
    * adds this user to this group using the right of another user
    * @param u The user to join to this group
    * @param ru The user to check for the rights for joining ths group
    * @return ture on success, false if joining is not allowed
    */
    public boolean addUser (User u, User ru) {
        return this.addUser (u, ru, false);
    } 
	public boolean addUser (User u, User ru, boolean invited) {
        if (u == null || usr.contains (u)) 
            return true;
        if (!usrMayJoin(ru)
            && (u.equals(ru) || !invited)) 
            return false;
        boolean isFirst;
        synchronized (this) {
            isFirst = usr.isEmpty();
            usr.addElement (u);
            usrArr=(User[]) usr.toArray(new User[0]);
        }
        if ((isFirst 
                && this.hasState(IGroupState.AUTO_SU_FIRST | IGroupState.ALLOW_SU)
                && !this.hasState(IGroupState.ENTRANCE) &&  !this.hasState(NO_SU_FIRST))
            || (autoSuList != null 
                && autoSuList.contains(u.getName().toLowerCase()) && this.hasState(AUTO_SU_FIRST) )) {
            addToSusers (u);
        }
        u.setGroup(this);
        if (plugins!=null) {
            for (int i = 0; i<plugins.length; i++) {
                try {
                    plugins[i].usrJoin(u);
                } catch (Exception e) {
                    Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR);
                }
            }
        }
        return true;
    }

    /**
     * removes this user from this group
     * @param u the user to remove from this group
     */
    public void removeUser (User u) {
        synchronized (this) {
            if (usr==null) {
                return;
            }
            usr.remove (u);
            if (usr.size() <= 0) {
                GroupManager.mgr.removeGroup (this);
                return;
            }
            usrArr=(User[]) usr.toArray(new User[0]);
        }
        while (susers.contains (u)) {
            susers.remove (u);
        }
        if (susers.size()<1
                && !this.hasState(OPEN)) {
            this.setState(OPEN);
            MessageParser mp = new MessageParser ();
            mp.setSender(u);
            mp.setMessageTemplate("message.ul");
            this.sendModeratedMessage(mp);
        }
        if (plugins!=null) {
            for (int i = 0; i<plugins.length; i++) {
                try {
                    plugins[i].usrLeaving(u);
                } catch (Exception e) {
                    Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR);
                }
            }
        }
    }

   /**
    * adds user to su-user-list of this room
    * @param u The user to add to the su-user-list of this group
    */
	public boolean addToSusers (User u) {
       if (!this.hasState(AUTO_SU_FIRST))
            return false;
        if (susers.contains (u)) 
            return true;
        susers.addElement (u);
        return true;
	}

	/**
	 * removes a user from the su-user-list of this group
	 * @param u The user to remove from the su-user-list
	 */
	public void removeFromSusers (User u) {
        while (susers.contains(u))
            susers.removeElement (u);
    }

    /**
     * send an excluseive message. Message goes to everybody within this group, excluding the users contained within the given vector
     * @param mc the message
     * @param exclude users which will not recieve this message
     */
    public void exclusiveSendMessage (IContainer mc, List exclude) {
        if (usr == null || usrArr==null || usr.size () < 1)
            return;
        User[] uarr = usrArr;
        for (int i = 0; i < uarr.length; i++) {
            User cu = (User) uarr[i];
            if (exclude.contains(cu))
                continue;
            cu.sendMessage (mc);
        }
        if (mc instanceof MessageParser && plugins!=null) {
            MessageParser mp = (MessageParser) mc;
            for (int i = 0; i<plugins.length; i++) {
                try {
                    plugins[i].usrAction(mp);
                } catch (Exception e) {
                    Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR);
                }
            }
        }
    }

   /**
    * Interface IMessageDestination used for sending messages to this group
    * meaning all users of this group
    */

   /**
    * sends a message to all users of this group. If the group is moderated,
    * only the moderator will see the message. A moderator may send a users
    * message by typing /ack username theMessageOfTheUser. This will be done
    * by a template, causing the message to have a link which sends this 
    * acknowledgement.
    * @param mc the IContainer containing the content to send to all users of this group
    */
	public void sendMessage (IContainer mc) {
		if (usr==null || usrArr==null || usr.size () < 1)
			return;
		boolean messageToModerate = false;
		if (mc instanceof MessageParser) {
			User sender = ((MessageParser) mc).getSender();
			messageToModerate = !(sender != null && (sender.hasRight(IUserStates.IS_MODERATOR) || sender.hasRight(IUserStates.IS_GUEST)));
		}
		if (messageToModerate &&
			this.hasState(IGroupState.MODERATED)) {
                User[] uarr = usrArr;
                for (int i = 0; i<uarr.length; i++) {
				User cu = uarr[i];
				if (!cu.hasRight(IUserStates.IS_MODERATOR))
					continue;
				cu.sendMessage (mc);
			}
			return;
        }
		sendMsg(mc);
	}
   
	private void sendMsg (IContainer mc) {
        if (usrArr==null)
            return;
        User[] uarr = usrArr;
        for (int i = 0; i<uarr.length; i++) {
			User cu = uarr[i];
			cu.sendMessage (mc);
		}
        if (mc instanceof MessageParser && plugins!=null) {
            MessageParser mp = (MessageParser) mc;
            for (int i = 0; i<plugins.length; i++) {
                try {
                    plugins[i].usrAction(mp);
                } catch (Exception e) {
                    Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR);
                }
            }
        }
	}

⌨️ 快捷键说明

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