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

📄 commandset.java

📁 JAVA平台下优秀的CHART开源代码,可以实现类似EXCEL中的二维或三维的饼图/椎图功能.
💻 JAVA
字号:
/**
 * 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.
 * 
 * Created on 28.09.2003
 */

package freecs.commands;
import freecs.interfaces.ICommand;
import freecs.interfaces.IReloadable;
import freecs.interfaces.IUserRights;
import freecs.layout.TemplateSet;
import freecs.util.FileMonitor;
import freecs.content.MessageState;
import freecs.core.Group;
import freecs.core.MessageRenderer;
import freecs.core.User;
import freecs.core.UserManager;
import freecs.Server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;

/**
 * @author Manfred Andres
 *
 * freecs.commands
 */
public class CommandSet implements IReloadable {
	public static final byte UNKNOWN_COMMAND=-1;
	public static final byte TRUE=1;
	public static final byte FALSE=0;
	public static final byte INTERRUPTED = Byte.MIN_VALUE;
	private static final CommandSet cs = new CommandSet();
	private HashMap allCmds, availableCmds;
	private Properties props;	
	private File 	cfgFile = null;
	private boolean cfgFilePresent = false;
	private long	cfgFileLastModified;

	private CommandSet () {
		props = new Properties();
		allCmds 		= initAllCommands();
		availableCmds 	= initAvailableCommands (allCmds);
	}
	
	// FIXME: implement possibility to disable commands
	private HashMap initAvailableCommands (HashMap all) {
		cfgFile = new File (Server.BASE_PATH + "/config", "command.properties");
		HashMap available = checkActivatedCommands ();
		FileMonitor.getFileMonitor().addReloadable (this);
		return available;
	}
	
	private HashMap checkActivatedCommands () {
		if (cfgFile.exists() && cfgFile.isFile()) try {
			cfgFilePresent = true;
			cfgFileLastModified = cfgFile.lastModified();
			FileInputStream in = new FileInputStream(cfgFile);
			props.load(in);
			in.close();
		} catch (FileNotFoundException fnfe) {
			// never
		} catch (IOException ioe) {
			Server.log("Unable to read command.properties", Server.MSG_ERROR, Server.LVL_MAJOR);
		} else {
			cfgFilePresent = false;
		}
		if (props == null)
			return allCmds;
		HashMap available = new HashMap();
		for (Iterator i= allCmds.keySet ().iterator (); i.hasNext() ;) {
			String curr = (String) i.next();
			String key = curr.substring(1).toLowerCase();
			String value = curr = props.getProperty (key);
			if (curr != null 
				&& (curr.equals("off") 
					|| curr.equals ("false")))
				continue;
			key = "/" + key;
			ICommand cmd = (ICommand) allCmds.get(key);
			available.put(key, cmd);
		}
		return available;
	}
	
	// FIXME: should be done automatically (placing a class which name
	// starts with Cmd into freecs.commands should automatically load
	// this class on startup)
	private HashMap initAllCommands() {
		HashMap all = new HashMap ();
		all.put ("/ack",	CmdAck.getInstance(this));
		all.put ("/a", 		CmdAccept.getInstance(this));
		all.put ("/me", 	CmdAct.getInstance(this));
		all.put ("/away", 	CmdAway.getInstance(this));
		all.put ("/bk", 	CmdBanKick.getInstance(this));
		all.put ("/ban", 	CmdBan.getInstance(this));
		all.put ("/wban",	CmdListBan.getInstance(this));
		all.put ("/col", 	CmdChangeColor.getInstance(this));
		all.put ("/c", 		CmdClear.getInstance(this));
		all.put ("/td", 	CmdHitDice.getInstance(this));
		all.put ("/ig", 	CmdIgnore.getInstance(this));
		all.put ("/rig", 	CmdRespectUser.getInstance(this));
		all.put ("/i", 		CmdInvite.getInstance(this));
		all.put ("/ia", 	CmdInviteAll.getInstance(this));
		all.put ("/j", 		CmdJoin.getInstance(this));
		all.put ("/ju", 	CmdJoinUser.getInstance(this));
		all.put ("/k", 		CmdKick.getInstance(this));
		all.put ("/kh", 	CmdKickHard.getInstance(this));
		all.put ("/kc", 	CmdKickToRoom.getInstance(this));
		all.put ("/fl", 	CmdListAllFriends.getInstance(this));
		all.put ("/f", 		CmdListOnlineFriends.getInstance(this));
		all.put ("/wc", 	CmdListUsers.getInstance(this));
		all.put ("/vip", 	CmdListVips.getInstance(this));
		all.put ("/l", 		CmdLock.getInstance(this));
		all.put ("/mycol", 	CmdMyCol.getInstance(this));
		all.put ("/m", 		CmdPrivateMessage.getInstance(this));
		all.put ("/gag", 	CmdPunish.getInstance(this));
		all.put ("/aq", 	CmdQuestion.getInstance(this));
		all.put ("/q", 		CmdQuit.getInstance(this));
		all.put ("/", 		CmdRepeatedPrivateMessage.getInstance(this));
		all.put ("/r", 		CmdReplyMessage.getInstance(this));
		all.put ("/ugag", 	CmdRespectUser.getInstance(this));
		all.put ("/rsu", 	CmdRSu.getInstance(this));
		all.put ("/sepa", 	CmdSepa.getInstance(this));
		all.put ("/t", 		CmdSetTheme.getInstance(this));
		all.put ("/s", 		CmdShout.getInstance(this));
		all.put ("/ip", 	CmdShowIp.getInstance(this));
		all.put ("/time", 	CmdShowTime.getInstance(this));
		all.put ("/w", 		CmdShowUserDetail.getInstance(this));
		all.put ("/su", 	CmdSu.getInstance(this));
		all.put ("/th", 	CmdThink.getInstance(this));
		all.put ("/uban", 	CmdUnBan.getInstance(this));
		all.put ("/ul", 	CmdUnlock.getInstance(this));
		all.put ("/rgag", 	CmdUnPunish.getInstance(this));
		return all;
	}
	
	public static CommandSet getCommandSet () {
		return cs;
	}
	
	public ICommand getCommand (String cmd) {
		return (ICommand) allCmds.get(cmd);
	} 

	public byte evaluate (String cmd, MessageState msgState, String param) {
		return evaluate (cmd, msgState, param, false);
	}
	public byte evaluate (String cmd, MessageState msgState, String param, boolean moderated) {
		if (!msgState.cb.isValid())
			return (INTERRUPTED);
		ICommand cmdObj = (ICommand) availableCmds.get(cmd);
		if (cmdObj == null) 
			return (UNKNOWN_COMMAND);
		return (cmdObj.execute(msgState, param) ? TRUE : FALSE); 
	}

	/**
	 * Utility method setting the invitation-state for one specific user
	 * @param msgState The message-state to operate on
	 * @param u The invited user
	 * @param g The Group this user was invited to
	 * @return u if invitation was successfull, msgState.sender if not
	 */
	public User setInvited (MessageState msgState, User u, Group g) {
		msgState.target = g.getName ();
		if (u.invitedFrom (msgState.sender)) {
			msgState.msgTemplate = "message.i";
			return (u);
		}
		msgState.msgTemplate = "error.i.alreadyInvited";
		return (msgState.sender);
	}

	/**
	 * Utility-method doing the hardKick
	 * @param msgState the MessageState to operate on
	 * @param cu the user to kick
	 * @param time how long this user will be banned from the server
	 * @return true on success false on failure
	 */
	public boolean kickHard (MessageState msgState, User cu, long time) {
		if (!msgState.sender.hasRight (IUserRights.MAY_KICK | IUserRights.MAY_BAN) &&
			!cu.hasRight (IUserRights.FREELY_KICKABLE | IUserRights.FREELY_BANABLE)) {
			msgState.msgTemplate = "error.kh.noRight";
			msgState.source = cu.getName();
			msgState.sender.sendMessage(msgState.mp);
			return false;
		}
		if (time > Server.srv.MAX_BAN_DURATION) 
			time = Server.srv.MAX_BAN_DURATION;
		time = time * 60 * 1000;
		msgState.source = cu.getName ();
		Server.srv.banUser (cu, msgState.message, time, msgState.sender.getName());
		return true;
	}

	public boolean generateUserList (MessageState msgState, boolean shorten) {
		Group g = msgState.room;
		StringBuffer sb = new StringBuffer ();
		TemplateSet ts = msgState.sender.getTemplateSet ();
		String tplcnt = ts.getMessageTemplate (shorten ? "message.user.short" : "message.user.overview");
		String seperator = ts.getMessageTemplate ("message.user.short.seperator");
		if (seperator != null) 
			seperator = MessageRenderer.renderTemplate (msgState, seperator, false);
		for (Iterator i = g.users (); i != null && i.hasNext (); ) {
			msgState.usercontext = (User) i.next ();
			String snippet = MessageRenderer.renderTemplate (msgState, tplcnt, false);
			sb.append (snippet == null ? msgState.usercontext.getName () : snippet);
			if (i.hasNext () && (shorten || snippet == null)) {
				if (seperator != null) 
					sb.append (seperator);
				else 
					sb.append (", ");
			}
		}
		if (sb.length () == 0) return false;
		msgState.msgTemplate = "list.users";
		msgState.target = g.getName ();
		msgState.source = g.getTheme ();
		msgState.message = sb.toString ();
		msgState.sender.sendMessage (msgState.mp);
		return true;
	}

	public void sendPrivateMessage (MessageState msgState, User cu, String message) {
		msgState.sender.setPrivateUser (cu);
		cu.setForeignPrivateUser (msgState.sender);
		msgState.target = cu.getName ();
		msgState.msgTemplate = "message.m";
		cu.sendMessage (msgState.mp);
		if (cu.isAway ()) msgState.msgTemplate = "message.m.away";
		else msgState.msgTemplate = "message.m.confirm";
		msgState.sender.sendMessage (msgState.mp);
	}

	public boolean setPunishment (MessageState msgState, User u, boolean p) {
		if ((p && isPunished(msgState)) 
			|| u == null || u.isPunished () == p) 
	   			return false;
		boolean isSu = u.getGroup ().usrIsSu(u);
		if (((u.hasRight (IUserRights.PROTECTED_FROM_PUNISH) && !msgState.sender.hasRight (IUserRights.ROLE_GOD)) 
				|| (!msgState.sender.getGroup().equals (u.getGroup ()) && !msgState.sender.hasRight (IUserRights.MAY_PUNISH))
				|| (p && isSu && !msgState.sender.hasRight (IUserRights.MAY_PUNISH)))
			&& !u.hasRight (IUserRights.FREELY_PUNISHABLE)) {
			msgState.msgTemplate = "error.gag.protected";
			msgState.target = u.getName ();
			msgState.sender.sendMessage (msgState.mp);
			return (false);
		}
		StringBuffer tsb = new StringBuffer ("CommandSet.setPunishment: ").append (msgState.sender.getName ()).append (" setting punishment for user ").append (u.getName ());
		Server.log (tsb.toString (), Server.MSG_STATE, Server.LVL_MINOR);
		u.setPunish (p);
		Group g = u.getGroup ();
		if (g != null) {
			tsb = new StringBuffer ("message.").append (p ? "gag" : "rgag");
			msgState.msgTemplate = tsb.toString ();
			msgState.target = u.getName ();
			u.getGroup ().sendModeraedMessage (msgState.mp);
		}
		return true;
	}

	public boolean setSuRight (MessageState msgState, User cu, boolean give) {
		String action = give ? "su" : "rsu";
		if ((!cu.getGroup ().equals (msgState.sender.getGroup ()) &&
			 !msgState.sender.hasRight (IUserRights.MAY_CHANGE_RIGHT)) ||
					cu.hasRight (IUserRights.PROTECTED_FROM_RIGHTCHANGE)) {
		   StringBuffer tsb = new StringBuffer ("error.").append (action).append (".noRight");
		   msgState.msgTemplate = tsb.toString ();
		   msgState.target = cu.getName ();
		   msgState.sender.sendMessage (msgState.mp);
		   return false;
		}
		Group cug = cu.getGroup ();
		if (give) {
		   cug.addToSusers (cu);
		} else {
		   cug.removeFromSusers (cu);
		}
		msgState.target = cu.getName ();
		StringBuffer tsb = new StringBuffer ("message.").append (action).append (".personal");
		msgState.msgTemplate = tsb.toString ();
		cu.sendMessage (msgState.mp);
	   return true;
	}

	public User getUser (MessageState msgState, String uname) {
	   User ru = UserManager.mgr.getUserByName (uname);
	   if (ru == null) {
		  msgState.msgTemplate = "error.user.notOnServer";
		  msgState.target = uname;
		  msgState.sender.sendMessage (msgState.mp);
	   }
	   return ru;
	}

	public boolean isPunished (MessageState msgState) {
		if (!msgState.sender.isPunished ()) return false;
		msgState.msgTemplate = "error.user.punished";
		msgState.sender.sendMessage(msgState.mp);
		return true;
	}

	/**
	 * Interface IReloadable's methods are deffined below here
	 */
	public boolean filePresent() {
		return cfgFilePresent;
	}

	public File getFile() {
		return cfgFile;
	}

	public long lastModified() {
		return cfgFileLastModified;
	}

	public void changed() {
		Server.log ("CommandSet.changed: reloaded commandset", Server.MSG_STATE, Server.LVL_MAJOR);
		availableCmds=checkActivatedCommands ();
	}

	public void removed() {
		Server.log ("CommandSet.removed: removed commandset", Server.MSG_STATE, Server.LVL_MAJOR);
		availableCmds=checkActivatedCommands ();
	}
	
	public void created() {
		Server.log ("CommandSet.created: loaded commandset", Server.MSG_STATE, Server.LVL_MAJOR);
		availableCmds=checkActivatedCommands ();
	}
	public String toString () {
		return ("[CommandSet]");
	}
}

⌨️ 快捷键说明

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