|
@@ -1,5 +1,5 @@
|
1
|
1
|
/*
|
2
|
|
- * Copyright (c) 2006-2015 DMDirc Developers
|
|
2
|
+ * Copyright (c) 2006-2016 DMDirc Developers
|
3
|
3
|
*
|
4
|
4
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
5
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,106 +22,42 @@
|
22
|
22
|
|
23
|
23
|
package com.dmdirc.commandparser.parsers;
|
24
|
24
|
|
25
|
|
-import com.dmdirc.commandparser.CommandArguments;
|
26
|
25
|
import com.dmdirc.commandparser.CommandInfo;
|
27
|
26
|
import com.dmdirc.commandparser.CommandInfoPair;
|
28
|
|
-import com.dmdirc.commandparser.CommandType;
|
29
|
27
|
import com.dmdirc.commandparser.commands.Command;
|
30
|
|
-import com.dmdirc.commandparser.commands.CommandOptions;
|
31
|
|
-import com.dmdirc.commandparser.commands.ExternalCommand;
|
32
|
|
-import com.dmdirc.commandparser.commands.PreviousCommand;
|
33
|
|
-import com.dmdirc.commandparser.commands.context.CommandContext;
|
34
|
|
-import com.dmdirc.events.CommandErrorEvent;
|
35
|
|
-import com.dmdirc.events.UnknownCommandEvent;
|
36
|
|
-import com.dmdirc.interfaces.CommandController;
|
37
|
|
-import com.dmdirc.interfaces.Connection;
|
38
|
|
-import com.dmdirc.interfaces.EventBus;
|
39
|
|
-import com.dmdirc.interfaces.GroupChat;
|
40
|
|
-import com.dmdirc.interfaces.InputModel;
|
41
|
28
|
import com.dmdirc.interfaces.WindowModel;
|
42
|
|
-import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
|
43
|
|
-import com.dmdirc.util.collections.RollingList;
|
44
|
|
-
|
45
|
|
-import java.io.Serializable;
|
46
|
|
-import java.util.HashMap;
|
47
|
|
-import java.util.Map;
|
48
|
|
-import java.util.Optional;
|
49
|
29
|
|
50
|
30
|
import javax.annotation.Nonnull;
|
51
|
31
|
import javax.annotation.Nullable;
|
52
|
|
-
|
53
|
|
-import static com.google.common.base.Preconditions.checkNotNull;
|
|
32
|
+import java.util.Map;
|
54
|
33
|
|
55
|
34
|
/**
|
56
|
|
- * Represents a generic command parser. A command parser takes a line of input from the user,
|
57
|
|
- * determines if it is an attempt at executing a command (based on the character at the start of the
|
58
|
|
- * string), and handles it appropriately.
|
|
35
|
+ * A command parser takes a line of input from the user, determines if it is an attempt at executing a command (based
|
|
36
|
+ * on the character at the start of the string), and handles it appropriately.
|
59
|
37
|
*/
|
60
|
|
-public abstract class CommandParser implements Serializable {
|
61
|
|
-
|
62
|
|
- /** A version number for this class. */
|
63
|
|
- private static final long serialVersionUID = 1;
|
64
|
|
- /** Commands that are associated with this parser. */
|
65
|
|
- private final Map<String, CommandInfoPair> commands;
|
66
|
|
- /** A history of commands that have been entered into this parser. */
|
67
|
|
- private final RollingList<PreviousCommand> history;
|
68
|
|
- /** Command manager to use. */
|
69
|
|
- protected final CommandController commandManager;
|
70
|
|
- /** Event bus to post events to. */
|
71
|
|
- private final EventBus eventBus;
|
72
|
|
-
|
73
|
|
- /**
|
74
|
|
- * Creates a new instance of CommandParser.
|
75
|
|
- *
|
76
|
|
- * @param configManager Config manager to read settings
|
77
|
|
- * @param commandManager Command manager to load plugins from
|
78
|
|
- * @param eventBus The event bus to post events to.
|
79
|
|
- */
|
80
|
|
- protected CommandParser(
|
81
|
|
- final ReadOnlyConfigProvider configManager,
|
82
|
|
- final CommandController commandManager,
|
83
|
|
- final EventBus eventBus) {
|
84
|
|
- this.eventBus = eventBus;
|
85
|
|
- commands = new HashMap<>();
|
86
|
|
- history = new RollingList<>(configManager.getOptionInt("general", "commandhistory"));
|
87
|
|
- this.commandManager = commandManager;
|
88
|
|
- loadCommands();
|
89
|
|
- }
|
90
|
|
-
|
91
|
|
- /** Loads the relevant commands into the parser. */
|
92
|
|
- protected abstract void loadCommands();
|
|
38
|
+public interface CommandParser {
|
93
|
39
|
|
94
|
40
|
/**
|
95
|
41
|
* Registers the specified command with this parser.
|
96
|
42
|
*
|
97
|
|
- * @since 0.6.3m1
|
98
|
43
|
* @param command Command to be registered
|
99
|
44
|
* @param info The information the command should be registered with
|
100
|
45
|
*/
|
101
|
|
- public final void registerCommand(final Command command, final CommandInfo info) {
|
102
|
|
- commands.put(info.getName().toLowerCase(), new CommandInfoPair(info, command));
|
103
|
|
- }
|
|
46
|
+ void registerCommand(Command command, CommandInfo info);
|
104
|
47
|
|
105
|
48
|
/**
|
106
|
49
|
* Unregisters the specified command with this parser.
|
107
|
50
|
*
|
108
|
51
|
* @param info Command information to be unregistered
|
109
|
|
- *
|
110
|
|
- * @since 0.6.3m1
|
111
|
52
|
*/
|
112
|
|
- public final void unregisterCommand(final CommandInfo info) {
|
113
|
|
- commands.remove(info.getName().toLowerCase());
|
114
|
|
- }
|
|
53
|
+ void unregisterCommand(CommandInfo info);
|
115
|
54
|
|
116
|
55
|
/**
|
117
|
56
|
* Retrieves a map of commands known by this command parser.
|
118
|
57
|
*
|
119
|
|
- * @since 0.6.3m1
|
120
|
58
|
* @return A map of commands known to this parser
|
121
|
59
|
*/
|
122
|
|
- public Map<String, CommandInfoPair> getCommands() {
|
123
|
|
- return new HashMap<>(commands);
|
124
|
|
- }
|
|
60
|
+ Map<String, CommandInfoPair> getCommands();
|
125
|
61
|
|
126
|
62
|
/**
|
127
|
63
|
* Parses the specified string as a command.
|
|
@@ -129,148 +65,16 @@ public abstract class CommandParser implements Serializable {
|
129
|
65
|
* @param origin The container which received the command
|
130
|
66
|
* @param line The line to be parsed
|
131
|
67
|
* @param parseChannel Whether or not to try and parse the first argument as a channel name
|
132
|
|
- *
|
133
|
|
- * @since 0.6.4
|
134
|
|
- */
|
135
|
|
- public final void parseCommand(@Nonnull final WindowModel origin, final String line,
|
136
|
|
- final boolean parseChannel) {
|
137
|
|
- checkNotNull(origin);
|
138
|
|
-
|
139
|
|
- final CommandArguments args = new CommandArguments(commandManager, line);
|
140
|
|
- if (args.isCommand()) {
|
141
|
|
- if (handleChannelCommand(origin, args, parseChannel)) {
|
142
|
|
- return;
|
143
|
|
- }
|
144
|
|
-
|
145
|
|
- if (commands.containsKey(args.getCommandName().toLowerCase())) {
|
146
|
|
- final CommandInfoPair pair = commands.get(args.getCommandName().toLowerCase());
|
147
|
|
- addHistory(args.getStrippedLine());
|
148
|
|
- executeCommand(origin, pair.getCommandInfo(), pair.getCommand(), args,
|
149
|
|
- getCommandContext(origin, pair.getCommandInfo(), pair.getCommand(), args));
|
150
|
|
- } else {
|
151
|
|
- handleInvalidCommand(origin, args);
|
152
|
|
- }
|
153
|
|
- } else {
|
154
|
|
- handleNonCommand(origin, line);
|
155
|
|
- }
|
156
|
|
- }
|
157
|
|
-
|
158
|
|
- /**
|
159
|
|
- * Checks to see whether the inputted command is a channel or external command, and if it is
|
160
|
|
- * whether one or more channels have been specified for its execution. If it is a channel or
|
161
|
|
- * external command, and channels are specified, this method invoke the appropriate command
|
162
|
|
- * parser methods to handle the command, and will return true. If the command is not handled,
|
163
|
|
- * the method returns false.
|
164
|
|
- *
|
165
|
|
- * @param origin The container which received the command
|
166
|
|
- * @param args The command and its arguments
|
167
|
|
- * @param parseChannel Whether or not to try parsing channel names
|
168
|
|
- *
|
169
|
|
- * @return True iff the command was handled, false otherwise
|
170
|
|
- */
|
171
|
|
- protected boolean handleChannelCommand(@Nonnull final WindowModel origin,
|
172
|
|
- final CommandArguments args, final boolean parseChannel) {
|
173
|
|
- final boolean silent = args.isSilent();
|
174
|
|
- final String command = args.getCommandName();
|
175
|
|
- final String[] cargs = args.getArguments();
|
176
|
|
- final Optional<Connection> connection = origin.getConnection();
|
177
|
|
-
|
178
|
|
- if (cargs.length == 0
|
179
|
|
- || !parseChannel
|
180
|
|
- || !connection.isPresent()
|
181
|
|
- || !commandManager.isChannelCommand(command)) {
|
182
|
|
- return false;
|
183
|
|
- }
|
184
|
|
-
|
185
|
|
- final Connection server = connection.get();
|
186
|
|
- final String[] parts = cargs[0].split(",");
|
187
|
|
- boolean someValid = false;
|
188
|
|
- for (String part : parts) {
|
189
|
|
- someValid |= server.getGroupChatManager().isValidChannelName(part);
|
190
|
|
- }
|
191
|
|
-
|
192
|
|
- if (someValid) {
|
193
|
|
- for (String channelName : parts) {
|
194
|
|
- if (!server.getGroupChatManager().isValidChannelName(channelName)) {
|
195
|
|
- origin.getEventBus().publishAsync(new CommandErrorEvent(origin,
|
196
|
|
- "Invalid channel name: " + channelName));
|
197
|
|
- continue;
|
198
|
|
- }
|
199
|
|
-
|
200
|
|
- final String newCommandString = commandManager.getCommandChar()
|
201
|
|
- + (silent ? String.valueOf(commandManager.getSilenceChar()) : "")
|
202
|
|
- + args.getCommandName()
|
203
|
|
- + (cargs.length > 1 ? ' ' + args.getArgumentsAsString(1) : "");
|
204
|
|
-
|
205
|
|
- final Optional<GroupChat> channel = server.getGroupChatManager()
|
206
|
|
- .getChannel(channelName);
|
207
|
|
- if (channel.isPresent()) {
|
208
|
|
- channel.get().getWindowModel().getInputModel().map(InputModel::getCommandParser)
|
209
|
|
- .ifPresent(cp -> cp.parseCommand(origin, newCommandString, false));
|
210
|
|
- } else {
|
211
|
|
- final Map.Entry<CommandInfo, Command> actCommand = commandManager.getCommand(
|
212
|
|
- CommandType.TYPE_CHANNEL, command);
|
213
|
|
-
|
214
|
|
- if (actCommand != null && actCommand.getValue() instanceof ExternalCommand) {
|
215
|
|
- ((ExternalCommand) actCommand.getValue()).execute(
|
216
|
|
- origin, server, channelName, silent,
|
217
|
|
- new CommandArguments(commandManager, newCommandString));
|
218
|
|
- }
|
219
|
|
- }
|
220
|
|
- }
|
221
|
|
-
|
222
|
|
- return true;
|
223
|
|
- }
|
224
|
|
-
|
225
|
|
- return false;
|
226
|
|
- }
|
227
|
|
-
|
228
|
|
- /**
|
229
|
|
- * Adds a command to this parser's history.
|
230
|
|
- *
|
231
|
|
- * @param command The command name and arguments that were used
|
232
|
|
- */
|
233
|
|
- private void addHistory(final String command) {
|
234
|
|
- synchronized (history) {
|
235
|
|
- final PreviousCommand pc = new PreviousCommand(command);
|
236
|
|
- history.remove(pc);
|
237
|
|
- history.add(pc);
|
238
|
|
- }
|
239
|
|
- }
|
240
|
|
-
|
241
|
|
- /**
|
242
|
|
- * Retrieves the most recent time that the specified command was used. Commands should not
|
243
|
|
- * include command or silence chars.
|
244
|
|
- *
|
245
|
|
- * @param command The command to search for
|
246
|
|
- *
|
247
|
|
- * @return The timestamp that the command was used, or 0 if it wasn't
|
248
|
68
|
*/
|
249
|
|
- public long getCommandTime(final String command) {
|
250
|
|
- long res = 0;
|
251
|
|
-
|
252
|
|
- synchronized (history) {
|
253
|
|
- for (PreviousCommand pc : history.getList()) {
|
254
|
|
- if (pc.getLine().matches("(?i)" + command)) {
|
255
|
|
- res = Math.max(res, pc.getTime());
|
256
|
|
- }
|
257
|
|
- }
|
258
|
|
- }
|
259
|
|
-
|
260
|
|
- return res;
|
261
|
|
- }
|
|
69
|
+ void parseCommand(@Nonnull WindowModel origin, String line, boolean parseChannel);
|
262
|
70
|
|
263
|
71
|
/**
|
264
|
72
|
* Parses the specified string as a command.
|
265
|
73
|
*
|
266
|
74
|
* @param origin The container which received the command
|
267
|
75
|
* @param line The line to be parsed
|
268
|
|
- *
|
269
|
|
- * @since 0.6.4
|
270
|
76
|
*/
|
271
|
|
- public void parseCommand(@Nonnull final WindowModel origin, final String line) {
|
272
|
|
- parseCommand(origin, line, true);
|
273
|
|
- }
|
|
77
|
+ void parseCommand(@Nonnull WindowModel origin, String line);
|
274
|
78
|
|
275
|
79
|
/**
|
276
|
80
|
* Handles the specified string as a non-command.
|
|
@@ -278,9 +82,7 @@ public abstract class CommandParser implements Serializable {
|
278
|
82
|
* @param origin The window in which the command was typed
|
279
|
83
|
* @param line The line to be parsed
|
280
|
84
|
*/
|
281
|
|
- public void parseCommandCtrl(final WindowModel origin, final String line) {
|
282
|
|
- handleNonCommand(origin, line);
|
283
|
|
- }
|
|
85
|
+ void parseCommandCtrl(WindowModel origin, String line);
|
284
|
86
|
|
285
|
87
|
/**
|
286
|
88
|
* Gets the command with the given name that was previously registered with this parser.
|
|
@@ -289,87 +91,6 @@ public abstract class CommandParser implements Serializable {
|
289
|
91
|
* @return The command info pair, or {@code null} if the command does not exist.
|
290
|
92
|
*/
|
291
|
93
|
@Nullable
|
292
|
|
- public CommandInfoPair getCommand(final String commandName) {
|
293
|
|
- return commands.get(commandName);
|
294
|
|
- }
|
295
|
|
-
|
296
|
|
- /**
|
297
|
|
- * Gets the context that the command will execute with.
|
298
|
|
- *
|
299
|
|
- * @param origin The container which received the command
|
300
|
|
- * @param commandInfo The command information object matched by the command
|
301
|
|
- * @param command The command to be executed
|
302
|
|
- * @param args The arguments to the command
|
303
|
|
- *
|
304
|
|
- * @return The context for the command.
|
305
|
|
- */
|
306
|
|
- protected abstract CommandContext getCommandContext(
|
307
|
|
- final WindowModel origin,
|
308
|
|
- final CommandInfo commandInfo,
|
309
|
|
- final Command command,
|
310
|
|
- final CommandArguments args);
|
311
|
|
-
|
312
|
|
- /**
|
313
|
|
- * Executes the specified command with the given arguments.
|
314
|
|
- *
|
315
|
|
- * @param origin The container which received the command
|
316
|
|
- * @param commandInfo The command information object matched by the command
|
317
|
|
- * @param command The command to be executed
|
318
|
|
- * @param args The arguments to the command
|
319
|
|
- * @param context The context to use when executing the command
|
320
|
|
- */
|
321
|
|
- protected abstract void executeCommand(
|
322
|
|
- @Nonnull final WindowModel origin,
|
323
|
|
- final CommandInfo commandInfo, final Command command,
|
324
|
|
- final CommandArguments args, final CommandContext context);
|
325
|
|
-
|
326
|
|
- /**
|
327
|
|
- * Called when the user attempted to issue a command (i.e., used the command character) that
|
328
|
|
- * wasn't found. It could be that the command has a different arity, or that it plain doesn't
|
329
|
|
- * exist.
|
330
|
|
- *
|
331
|
|
- * @param origin The window in which the command was typed
|
332
|
|
- * @param args The arguments passed to the command
|
333
|
|
- *
|
334
|
|
- * @since 0.6.3m1
|
335
|
|
- */
|
336
|
|
- protected void handleInvalidCommand(final WindowModel origin,
|
337
|
|
- final CommandArguments args) {
|
338
|
|
- eventBus.publishAsync(new UnknownCommandEvent(origin, args.getCommandName(),
|
339
|
|
- args.getArguments()));
|
340
|
|
- }
|
341
|
|
-
|
342
|
|
- /**
|
343
|
|
- * Called when the input was a line of text that was not a command. This normally means it is
|
344
|
|
- * sent to the server/channel/user as-is, with no further processing.
|
345
|
|
- *
|
346
|
|
- * @param origin The window in which the command was typed
|
347
|
|
- * @param line The line input by the user
|
348
|
|
- */
|
349
|
|
- protected abstract void handleNonCommand(final WindowModel origin,
|
350
|
|
- final String line);
|
351
|
|
-
|
352
|
|
- /**
|
353
|
|
- * Determines if the specified command has defined any command options.
|
354
|
|
- *
|
355
|
|
- * @param command The command to investigate
|
356
|
|
- *
|
357
|
|
- * @return True if the command defines options, false otherwise
|
358
|
|
- */
|
359
|
|
- protected boolean hasCommandOptions(final Command command) {
|
360
|
|
- return command.getClass().isAnnotationPresent(CommandOptions.class);
|
361
|
|
- }
|
362
|
|
-
|
363
|
|
- /**
|
364
|
|
- * Retrieves the command options for the specified command. If the command does not define
|
365
|
|
- * options, this method will return null.
|
366
|
|
- *
|
367
|
|
- * @param command The command whose options should be retrieved
|
368
|
|
- *
|
369
|
|
- * @return The command's options, or null if not available
|
370
|
|
- */
|
371
|
|
- protected CommandOptions getCommandOptions(final Command command) {
|
372
|
|
- return command.getClass().getAnnotation(CommandOptions.class);
|
373
|
|
- }
|
|
94
|
+ CommandInfoPair getCommand(String commandName);
|
374
|
95
|
|
375
|
96
|
}
|