KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CommandSet


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Created on 28.09.2003
19  */

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 JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Properties JavaDoc;
35
36 /**
37  * @author Manfred Andres
38  *
39  * freecs.commands
40  */

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 JavaDoc allCmds, availableCmds;
48     private Properties JavaDoc props;
49     private File JavaDoc cfgFile = null;
50     private boolean cfgFilePresent = false;
51     private long cfgFileLastModified;
52
53     private CommandSet () {
54         props = new Properties JavaDoc();
55         allCmds = initAllCommands();
56         availableCmds = initAvailableCommands (allCmds);
57     }
58     
59     private HashMap JavaDoc initAvailableCommands (HashMap JavaDoc all) {
60         cfgFile = new File JavaDoc (Server.BASE_PATH + "/config", "command.properties");
61         HashMap JavaDoc available = checkActivatedCommands ();
62         FileMonitor.getFileMonitor().addReloadable (this);
63         return available;
64     }
65     
66     private HashMap JavaDoc checkActivatedCommands () {
67         if (cfgFile.exists() && cfgFile.isFile()) try {
68             cfgFilePresent = true;
69             cfgFileLastModified = cfgFile.lastModified();
70             FileInputStream JavaDoc in = new FileInputStream JavaDoc(cfgFile);
71             props.load(in);
72             in.close();
73         } catch (FileNotFoundException JavaDoc fnfe) {
74             // never
75
} catch (IOException JavaDoc 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 JavaDoc available = new HashMap JavaDoc();
83         for (Iterator JavaDoc i= allCmds.keySet ().iterator (); i.hasNext() ;) {
84             String JavaDoc curr = (String JavaDoc) i.next();
85             String JavaDoc key = curr.substring(1).toLowerCase();
86             String JavaDoc 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     // FIXME: should be done automatically (placing a class which name
99
// starts with Cmd into freecs.commands should automatically load
100
// this class on startup)
101
private HashMap JavaDoc initAllCommands() {
102         HashMap JavaDoc all = new HashMap JavaDoc ();
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 JavaDoc cmd) {
158         return (ICommand) allCmds.get(cmd);
159     }
160
161     public byte evaluate (String JavaDoc cmd, MessageState msgState, String JavaDoc param) {
162         try {
163             return evaluate (cmd, msgState, param, false);
164         } catch (Exception JavaDoc 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 JavaDoc cmd, MessageState msgState, String JavaDoc 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 JavaDoc e) {
178             Server.debug (cmd, "evaluation caused exception", e, Server.MSG_ERROR, Server.LVL_MAJOR);
179         }
180         return -1;
181     }
182
183     /**
184      * Interface IReloadable's methods are deffined below here
185      */

186     public boolean filePresent() {
187         return cfgFilePresent;
188     }
189
190     public File JavaDoc 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 JavaDoc toString () {
213         return ("[CommandSet]");
214     }
215 }
216
Popular Tags