1 20 21 package freecs.commands; 22 import freecs.interfaces.ICommand; 23 import freecs.interfaces.IReloadable; 24 import freecs.util.FileMonitor; 25 import freecs.content.MessageState; 26 import freecs.Server; 27 28 import java.io.File ; 29 import java.io.FileInputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.IOException ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Properties ; 35 36 41 public class CommandSet implements IReloadable { 42 public static final byte UNKNOWN_COMMAND=-1; 43 public static final byte TRUE=1; 44 public static final byte FALSE=0; 45 public static final byte INTERRUPTED = Byte.MIN_VALUE; 46 private static final CommandSet cs = new CommandSet(); 47 private HashMap allCmds, availableCmds; 48 private Properties props; 49 private File cfgFile = null; 50 private boolean cfgFilePresent = false; 51 private long cfgFileLastModified; 52 53 private CommandSet () { 54 props = new Properties (); 55 allCmds = initAllCommands(); 56 availableCmds = initAvailableCommands (allCmds); 57 } 58 59 private HashMap initAvailableCommands (HashMap all) { 60 cfgFile = new File (Server.BASE_PATH + "/config", "command.properties"); 61 HashMap available = checkActivatedCommands (); 62 FileMonitor.getFileMonitor().addReloadable (this); 63 return available; 64 } 65 66 private HashMap checkActivatedCommands () { 67 if (cfgFile.exists() && cfgFile.isFile()) try { 68 cfgFilePresent = true; 69 cfgFileLastModified = cfgFile.lastModified(); 70 FileInputStream in = new FileInputStream (cfgFile); 71 props.load(in); 72 in.close(); 73 } catch (FileNotFoundException fnfe) { 74 } catch (IOException ioe) { 76 Server.log(this, "Unable to read command.properties", Server.MSG_ERROR, Server.LVL_MAJOR); 77 } else { 78 cfgFilePresent = false; 79 } 80 if (props == null) 81 return allCmds; 82 HashMap available = new HashMap (); 83 for (Iterator i= allCmds.keySet ().iterator (); i.hasNext() ;) { 84 String curr = (String ) i.next(); 85 String key = curr.substring(1).toLowerCase(); 86 String value = curr = props.getProperty (key); 87 if (curr != null 88 && (curr.equals("off") 89 || curr.equals ("false"))) 90 continue; 91 key = "/" + key; 92 ICommand cmd = (ICommand) allCmds.get(key); 93 available.put(key, cmd); 94 } 95 return available; 96 } 97 98 private HashMap initAllCommands() { 102 HashMap all = new HashMap (); 103 all.put ("/ack", CmdAck.getInstance()); 104 all.put ("/a", CmdAccept.getInstance()); 105 all.put ("/me", CmdAct.getInstance()); 106 all.put ("/away", CmdAway.getInstance()); 107 all.put ("/ban", CmdBan.getInstance()); 108 all.put ("/wban", CmdListBan.getInstance()); 109 all.put ("/col", CmdChangeColor.getInstance()); 110 all.put ("/fcol", CmdChangeForeignColor.getInstance()); 111 all.put ("/c", CmdClear.getInstance()); 112 all.put ("/td", CmdHitDice.getInstance()); 113 all.put ("/ig", CmdIgnore.getInstance()); 114 all.put ("/rig", CmdRespectUser.getInstance()); 115 all.put ("/i", CmdInvite.getInstance()); 116 all.put ("/ia", CmdInviteAll.getInstance()); 117 all.put ("/j", CmdJoin.getInstance()); 118 all.put ("/jclosed",CmdJoinClosed.getInstance()); 119 all.put ("/ju", CmdJoinUser.getInstance()); 120 all.put ("/k", CmdKick.getInstance()); 121 all.put ("/kh", CmdKickHard.getInstance()); 122 all.put ("/kc", CmdKickToRoom.getInstance()); 123 all.put ("/fl", CmdListAllFriends.getInstance()); 124 all.put ("/f", CmdListOnlineFriends.getInstance()); 125 all.put ("/wc", CmdListUsers.getInstance()); 126 all.put ("/vip", CmdListVips.getInstance()); 127 all.put ("/l", CmdLock.getInstance()); 128 all.put ("/mycol", CmdMyCol.getInstance()); 129 all.put ("/m", CmdPrivateMessage.getInstance()); 130 all.put ("/gag", CmdPunish.getInstance()); 131 all.put ("/aq", CmdQuestion.getInstance()); 132 all.put ("/raq", CmdResetQuestioncounter.getInstance()); 133 all.put ("/q", CmdQuit.getInstance()); 134 all.put ("/", CmdRepeatedPrivateMessage.getInstance()); 135 all.put ("/r", CmdReplyMessage.getInstance()); 136 all.put ("/sys", CmdSys.getInstance()); 137 all.put ("/rsu", CmdRSu.getInstance()); 138 all.put ("/sepa", CmdSepa.getInstance()); 139 all.put ("/t", CmdSetTheme.getInstance()); 140 all.put ("/s", CmdShout.getInstance()); 141 all.put ("/ip", CmdShowIp.getInstance()); 142 all.put ("/time", CmdShowTime.getInstance()); 143 all.put ("/w", CmdShowUserDetail.getInstance()); 144 all.put ("/su", CmdSu.getInstance()); 145 all.put ("/th", CmdThink.getInstance()); 146 all.put ("/uban", CmdUnBan.getInstance()); 147 all.put ("/ul", CmdUnlock.getInstance()); 148 all.put ("/rgag", CmdUnPunish.getInstance()); 149 all.put ("/rc", CmdRightChange.getInstance()); 150 return all; 151 } 152 153 public static CommandSet getCommandSet () { 154 return cs; 155 } 156 157 public ICommand getCommand (String cmd) { 158 return (ICommand) allCmds.get(cmd); 159 } 160 161 public byte evaluate (String cmd, MessageState msgState, String param) { 162 try { 163 return evaluate (cmd, msgState, param, false); 164 } catch (Exception e) { 165 Server.debug(cmd, "evaluation caused exception", e, Server.MSG_ERROR, Server.LVL_MAJOR); 166 } 167 return -1; 168 } 169 public byte evaluate (String cmd, MessageState msgState, String param, boolean moderated) { 170 try { 171 if (!msgState.cb.isValid()) 172 return (INTERRUPTED); 173 ICommand cmdObj = (ICommand) availableCmds.get(cmd); 174 if (cmdObj == null) 175 return (UNKNOWN_COMMAND); 176 return (cmdObj.execute(msgState, param) ? TRUE : FALSE); 177 } catch (Exception e) { 178 Server.debug (cmd, "evaluation caused exception", e, Server.MSG_ERROR, Server.LVL_MAJOR); 179 } 180 return -1; 181 } 182 183 186 public boolean filePresent() { 187 return cfgFilePresent; 188 } 189 190 public File getFile() { 191 return cfgFile; 192 } 193 194 public long lastModified() { 195 return cfgFileLastModified; 196 } 197 198 public void changed() { 199 Server.log (this, "changed: reloaded commandset", Server.MSG_STATE, Server.LVL_MINOR); 200 availableCmds=checkActivatedCommands (); 201 } 202 203 public void removed() { 204 Server.log (this, "removed: removed commandset", Server.MSG_STATE, Server.LVL_MINOR); 205 availableCmds=checkActivatedCommands (); 206 } 207 208 public void created() { 209 Server.log (this, "created: loaded commandset", Server.MSG_STATE, Server.LVL_MINOR); 210 availableCmds=checkActivatedCommands (); 211 } 212 public String toString () { 213 return ("[CommandSet]"); 214 } 215 } 216 | Popular Tags |