|
@@ -26,7 +26,9 @@ import com.dmdirc.Server;
|
26
|
26
|
import com.dmdirc.actions.ActionManager;
|
27
|
27
|
import com.dmdirc.actions.CoreActionType;
|
28
|
28
|
import com.dmdirc.commandparser.CommandManager;
|
|
29
|
+import com.dmdirc.commandparser.CommandType;
|
29
|
30
|
import com.dmdirc.commandparser.commands.Command;
|
|
31
|
+import com.dmdirc.commandparser.commands.ExternalCommand;
|
30
|
32
|
import com.dmdirc.commandparser.commands.PreviousCommand;
|
31
|
33
|
import com.dmdirc.config.IdentityManager;
|
32
|
34
|
import com.dmdirc.ui.interfaces.InputWindow;
|
|
@@ -40,28 +42,28 @@ import java.util.Map;
|
40
|
42
|
* Represents a generic command parser. A command parser takes a line of input
|
41
|
43
|
* from the user, determines if it is an attempt at executing a command (based
|
42
|
44
|
* on the character at the start of the string), and handles it appropriately.
|
43
|
|
- *
|
|
45
|
+ *
|
44
|
46
|
* @author chris
|
45
|
47
|
*/
|
46
|
48
|
public abstract class CommandParser implements Serializable {
|
47
|
|
-
|
|
49
|
+
|
48
|
50
|
/**
|
49
|
51
|
* A version number for this class. It should be changed whenever the class
|
50
|
52
|
* structure is changed (or anything else that would prevent serialized
|
51
|
53
|
* objects being unserialized with the new class).
|
52
|
54
|
*/
|
53
|
55
|
private static final long serialVersionUID = 1;
|
54
|
|
-
|
|
56
|
+
|
55
|
57
|
/**
|
56
|
58
|
* Commands that are associated with this parser.
|
57
|
59
|
*/
|
58
|
60
|
private final Map<String, Command> commands;
|
59
|
|
-
|
|
61
|
+
|
60
|
62
|
/**
|
61
|
63
|
* A history of commands that have been entered into this parser.
|
62
|
64
|
*/
|
63
|
65
|
private final RollingList<PreviousCommand> history;
|
64
|
|
-
|
|
66
|
+
|
65
|
67
|
/** Creates a new instance of CommandParser. */
|
66
|
68
|
public CommandParser() {
|
67
|
69
|
commands = new Hashtable<String, Command>();
|
|
@@ -70,31 +72,31 @@ public abstract class CommandParser implements Serializable {
|
70
|
72
|
"commandhistory", 10));
|
71
|
73
|
loadCommands();
|
72
|
74
|
}
|
73
|
|
-
|
|
75
|
+
|
74
|
76
|
/** Loads the relevant commands into the parser. */
|
75
|
77
|
protected abstract void loadCommands();
|
76
|
|
-
|
|
78
|
+
|
77
|
79
|
/**
|
78
|
80
|
* Registers the specified command with this parser.
|
79
|
|
- *
|
|
81
|
+ *
|
80
|
82
|
* @param command Command to be registered
|
81
|
83
|
*/
|
82
|
84
|
public final void registerCommand(final Command command) {
|
83
|
85
|
commands.put(command.getName().toLowerCase(), command);
|
84
|
86
|
}
|
85
|
|
-
|
|
87
|
+
|
86
|
88
|
/**
|
87
|
89
|
* Unregisters the specified command with this parser.
|
88
|
|
- *
|
|
90
|
+ *
|
89
|
91
|
* @param command Command to be unregistered
|
90
|
92
|
*/
|
91
|
93
|
public final void unregisterCommand(final Command command) {
|
92
|
94
|
commands.remove(command.getName().toLowerCase());
|
93
|
95
|
}
|
94
|
|
-
|
|
96
|
+
|
95
|
97
|
/**
|
96
|
98
|
* Parses the specified string as a command.
|
97
|
|
- *
|
|
99
|
+ *
|
98
|
100
|
* @param origin The window in which the command was typed
|
99
|
101
|
* @param line The line to be parsed
|
100
|
102
|
* @param parseChannel Whether or not to try and parse the first argument
|
|
@@ -105,50 +107,59 @@ public abstract class CommandParser implements Serializable {
|
105
|
107
|
if (line.isEmpty()) {
|
106
|
108
|
return;
|
107
|
109
|
}
|
108
|
|
-
|
|
110
|
+
|
109
|
111
|
if (line.charAt(0) == CommandManager.getCommandChar()) {
|
110
|
112
|
int offset = 1;
|
111
|
113
|
boolean silent = false;
|
112
|
|
-
|
|
114
|
+
|
113
|
115
|
if (line.length() > offset && line.charAt(offset) == CommandManager.getSilenceChar()) {
|
114
|
116
|
silent = true;
|
115
|
117
|
offset++;
|
116
|
118
|
}
|
117
|
|
-
|
|
119
|
+
|
118
|
120
|
final String[] args = line.split(" ");
|
119
|
121
|
final String command = args[0].substring(offset);
|
120
|
122
|
String[] comargs;
|
121
|
|
-
|
|
123
|
+
|
122
|
124
|
if (args.length >= 2 && parseChannel && origin != null
|
123
|
125
|
&& origin.getContainer() != null
|
124
|
126
|
&& origin.getContainer().getServer() != null
|
125
|
127
|
&& origin.getContainer().getServer().isValidChannelName(args[1])
|
126
|
128
|
&& CommandManager.isChannelCommand(command)) {
|
127
|
129
|
final Server server = origin.getContainer().getServer();
|
128
|
|
-
|
|
130
|
+
|
129
|
131
|
if (server.hasChannel(args[1])) {
|
130
|
|
-
|
|
132
|
+
|
131
|
133
|
final StringBuilder newLine = new StringBuilder();
|
132
|
134
|
for (int i = 0; i < args.length; i++) {
|
133
|
135
|
if (i == 1) { continue; }
|
134
|
136
|
newLine.append(" ").append(args[i]);
|
135
|
137
|
}
|
136
|
|
-
|
|
138
|
+
|
137
|
139
|
server.getChannel(args[1]).getFrame().getCommandParser()
|
138
|
140
|
.parseCommand(origin, newLine.substring(1), false);
|
139
|
|
-
|
|
141
|
+
|
140
|
142
|
return;
|
141
|
143
|
} else {
|
142
|
|
- // Do something haxy involving external commands here...
|
|
144
|
+ final Command actCommand = CommandManager.getCommand(
|
|
145
|
+ CommandType.TYPE_CHANNEL, command);
|
|
146
|
+
|
|
147
|
+ if (actCommand instanceof ExternalCommand) {
|
|
148
|
+ comargs = new String[args.length - 2];
|
|
149
|
+ System.arraycopy(args, 2, comargs, 0, args.length - 2);
|
|
150
|
+
|
|
151
|
+ ((ExternalCommand) actCommand).execute(origin, server,
|
|
152
|
+ args[1], silent, comargs);
|
|
153
|
+ return;
|
|
154
|
+ }
|
143
|
155
|
}
|
144
|
156
|
}
|
145
|
|
-
|
|
157
|
+
|
146
|
158
|
comargs = new String[args.length - 1];
|
147
|
|
-
|
148
|
159
|
System.arraycopy(args, 1, comargs, 0, args.length - 1);
|
149
|
|
-
|
|
160
|
+
|
150
|
161
|
final String signature = command;
|
151
|
|
-
|
|
162
|
+
|
152
|
163
|
if (commands.containsKey(signature.toLowerCase())) {
|
153
|
164
|
addHistory(command, comargs);
|
154
|
165
|
executeCommand(origin, silent, commands.get(signature.toLowerCase()), comargs);
|
|
@@ -159,7 +170,7 @@ public abstract class CommandParser implements Serializable {
|
159
|
170
|
handleNonCommand(origin, line);
|
160
|
171
|
}
|
161
|
172
|
}
|
162
|
|
-
|
|
173
|
+
|
163
|
174
|
/**
|
164
|
175
|
* Adds a command to this parser's history.
|
165
|
176
|
*
|
|
@@ -168,17 +179,17 @@ public abstract class CommandParser implements Serializable {
|
168
|
179
|
*/
|
169
|
180
|
private void addHistory(final String command, final String... args) {
|
170
|
181
|
final StringBuilder builder = new StringBuilder(command);
|
171
|
|
-
|
|
182
|
+
|
172
|
183
|
for (String arg : args) {
|
173
|
184
|
builder.append(' ');
|
174
|
185
|
builder.append(arg);
|
175
|
186
|
}
|
176
|
|
-
|
|
187
|
+
|
177
|
188
|
synchronized(history) {
|
178
|
189
|
history.add(new PreviousCommand(builder.toString()));
|
179
|
190
|
}
|
180
|
191
|
}
|
181
|
|
-
|
|
192
|
+
|
182
|
193
|
/**
|
183
|
194
|
* Retrieves the most recent time that the specified command was used.
|
184
|
195
|
* Commands should not include command or silence chars.
|
|
@@ -196,13 +207,13 @@ public abstract class CommandParser implements Serializable {
|
196
|
207
|
}
|
197
|
208
|
}
|
198
|
209
|
}
|
199
|
|
-
|
|
210
|
+
|
200
|
211
|
return res;
|
201
|
212
|
}
|
202
|
|
-
|
|
213
|
+
|
203
|
214
|
/**
|
204
|
215
|
* Parses the specified string as a command.
|
205
|
|
- *
|
|
216
|
+ *
|
206
|
217
|
* @param origin The window in which the command was typed
|
207
|
218
|
* @param line The line to be parsed
|
208
|
219
|
*/
|
|
@@ -210,20 +221,20 @@ public abstract class CommandParser implements Serializable {
|
210
|
221
|
final String line) {
|
211
|
222
|
parseCommand(origin, line, true);
|
212
|
223
|
}
|
213
|
|
-
|
|
224
|
+
|
214
|
225
|
/**
|
215
|
226
|
* Handles the specified string as a non-command.
|
216
|
|
- *
|
|
227
|
+ *
|
217
|
228
|
* @param origin The window in which the command was typed
|
218
|
229
|
* @param line The line to be parsed
|
219
|
230
|
*/
|
220
|
231
|
public final void parseCommandCtrl(final InputWindow origin, final String line) {
|
221
|
232
|
handleNonCommand(origin, line);
|
222
|
233
|
}
|
223
|
|
-
|
|
234
|
+
|
224
|
235
|
/**
|
225
|
236
|
* Executes the specified command with the given arguments.
|
226
|
|
- *
|
|
237
|
+ *
|
227
|
238
|
* @param origin The window in which the command was typed
|
228
|
239
|
* @param isSilent Whether the command is being silenced or not
|
229
|
240
|
* @param command The command to be executed
|
|
@@ -231,7 +242,7 @@ public abstract class CommandParser implements Serializable {
|
231
|
242
|
*/
|
232
|
243
|
protected abstract void executeCommand(final InputWindow origin,
|
233
|
244
|
final boolean isSilent, final Command command, final String... args);
|
234
|
|
-
|
|
245
|
+
|
235
|
246
|
/**
|
236
|
247
|
* Called when the user attempted to issue a command (i.e., used the command
|
237
|
248
|
* character) that wasn't found. It could be that the command has a different
|
|
@@ -247,18 +258,18 @@ public abstract class CommandParser implements Serializable {
|
247
|
258
|
null, command, args);
|
248
|
259
|
} else {
|
249
|
260
|
final StringBuffer buff = new StringBuffer("unknownCommand");
|
250
|
|
-
|
|
261
|
+
|
251
|
262
|
ActionManager.processEvent(CoreActionType.UNKNOWN_COMMAND, buff,
|
252
|
263
|
origin.getContainer(), command, args);
|
253
|
|
-
|
|
264
|
+
|
254
|
265
|
origin.addLine(buff, command);
|
255
|
266
|
}
|
256
|
267
|
}
|
257
|
|
-
|
|
268
|
+
|
258
|
269
|
/**
|
259
|
270
|
* Called when the input was a line of text that was not a command. This normally
|
260
|
271
|
* means it is sent to the server/channel/user as-is, with no further processing.
|
261
|
|
- *
|
|
272
|
+ *
|
262
|
273
|
* @param origin The window in which the command was typed
|
263
|
274
|
* @param line The line input by the user
|
264
|
275
|
*/
|