Преглед изворни кода

Fix how CommandContexts work.

The highest level command parser now generates the context, regardless of
who executes it. So a global command executed in a channel window will get
a ChannelCommandContext, whereas before it got a plain CommandContext.

Commands that don't expect other contexts will work as before, because the
inheritence tree of contexts mirorrs the command parser (e.g., if a
ServerCommandParser executes a command, you can guarantee its context will
be a sublcass of ServerCommandContext).

This fixes the /set command never allowing --channel.

Change-Id: I86e3c9f0bfedecb5c6b538e2309528c9e71f5eb3
Fixes-issue: CLIENT-424
Reviewed-on: http://gerrit.dmdirc.com/3085
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.8rc1
Chris Smith пре 10 година
родитељ
комит
158d3bd350

+ 17
- 6
src/com/dmdirc/commandparser/parsers/ChannelCommandParser.java Прегледај датотеку

@@ -30,6 +30,7 @@ import com.dmdirc.commandparser.CommandInfo;
30 30
 import com.dmdirc.commandparser.CommandType;
31 31
 import com.dmdirc.commandparser.commands.Command;
32 32
 import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
33
+import com.dmdirc.commandparser.commands.context.CommandContext;
33 34
 import com.dmdirc.interfaces.CommandController;
34 35
 
35 36
 /**
@@ -76,16 +77,26 @@ public class ChannelCommandParser extends ChatCommandParser {
76 77
                 CommandType.TYPE_SERVER, CommandType.TYPE_CHANNEL);
77 78
     }
78 79
 
79
-    /** {@inheritDoc} */
80 80
     @Override
81
-    protected void executeCommand(final FrameContainer origin,
82
-            final CommandInfo commandInfo, final Command command,
81
+    protected CommandContext getCommandContext(
82
+            final FrameContainer origin,
83
+            final CommandInfo commandInfo,
84
+            final Command command,
83 85
             final CommandArguments args) {
86
+        return new ChannelCommandContext(origin, commandInfo, channel);
87
+    }
88
+
89
+    @Override
90
+    protected void executeCommand(
91
+            final FrameContainer origin,
92
+            final CommandInfo commandInfo,
93
+            final Command command,
94
+            final CommandArguments args,
95
+            final CommandContext context) {
84 96
         if (commandInfo.getType() == CommandType.TYPE_CHANNEL) {
85
-            command.execute(origin, args, new ChannelCommandContext(origin,
86
-                    commandInfo, channel));
97
+            command.execute(origin, args, context);
87 98
         } else {
88
-            super.executeCommand(origin, commandInfo, command, args);
99
+            super.executeCommand(origin, commandInfo, command, args, context);
89 100
         }
90 101
     }
91 102
 

+ 17
- 6
src/com/dmdirc/commandparser/parsers/ChatCommandParser.java Прегледај датотеку

@@ -30,6 +30,7 @@ import com.dmdirc.commandparser.CommandInfo;
30 30
 import com.dmdirc.commandparser.CommandType;
31 31
 import com.dmdirc.commandparser.commands.Command;
32 32
 import com.dmdirc.commandparser.commands.context.ChatCommandContext;
33
+import com.dmdirc.commandparser.commands.context.CommandContext;
33 34
 import com.dmdirc.interfaces.CommandController;
34 35
 
35 36
 /**
@@ -70,16 +71,26 @@ public class ChatCommandParser extends ServerCommandParser {
70 71
         }
71 72
     }
72 73
 
73
-    /** {@inheritDoc} */
74 74
     @Override
75
-    protected void executeCommand(final FrameContainer origin,
76
-            final CommandInfo commandInfo, final Command command,
75
+    protected CommandContext getCommandContext(
76
+            final FrameContainer origin,
77
+            final CommandInfo commandInfo,
78
+            final Command command,
77 79
             final CommandArguments args) {
80
+        return new ChatCommandContext(origin, commandInfo, owner);
81
+    }
82
+
83
+    @Override
84
+    protected void executeCommand(
85
+            final FrameContainer origin,
86
+            final CommandInfo commandInfo,
87
+            final Command command,
88
+            final CommandArguments args,
89
+            final CommandContext context) {
78 90
         if (commandInfo.getType() == CommandType.TYPE_CHAT) {
79
-            command.execute(origin, args, new ChatCommandContext(origin,
80
-                    commandInfo, owner));
91
+            command.execute(origin, args, context);
81 92
         } else {
82
-            super.executeCommand(origin, commandInfo, command, args);
93
+            super.executeCommand(origin, commandInfo, command, args, context);
83 94
         }
84 95
     }
85 96
 

+ 20
- 3
src/com/dmdirc/commandparser/parsers/CommandParser.java Прегледај датотеку

@@ -34,6 +34,7 @@ import com.dmdirc.commandparser.commands.Command;
34 34
 import com.dmdirc.commandparser.commands.CommandOptions;
35 35
 import com.dmdirc.commandparser.commands.ExternalCommand;
36 36
 import com.dmdirc.commandparser.commands.PreviousCommand;
37
+import com.dmdirc.commandparser.commands.context.CommandContext;
37 38
 import com.dmdirc.interfaces.CommandController;
38 39
 import com.dmdirc.interfaces.Connection;
39 40
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
@@ -151,7 +152,8 @@ public abstract class CommandParser implements Serializable {
151 152
             if (commands.containsKey(args.getCommandName().toLowerCase())) {
152 153
                 final CommandInfoPair pair = commands.get(args.getCommandName().toLowerCase());
153 154
                 addHistory(args.getStrippedLine());
154
-                executeCommand(origin, pair.getCommandInfo(), pair.getCommand(), args);
155
+                executeCommand(origin, pair.getCommandInfo(), pair.getCommand(), args,
156
+                        getCommandContext(origin, pair.getCommandInfo(), pair.getCommand(), args));
155 157
             } else {
156 158
                 handleInvalidCommand(origin, args);
157 159
             }
@@ -277,6 +279,21 @@ public abstract class CommandParser implements Serializable {
277 279
         handleNonCommand(origin, line);
278 280
     }
279 281
 
282
+    /**
283
+     * Gets the context that the command will execute with.
284
+     *
285
+     * @param origin The container which received the command
286
+     * @param commandInfo The command information object matched by the command
287
+     * @param command The command to be executed
288
+     * @param args The arguments to the command
289
+     * @return The context for the command.
290
+     */
291
+    protected abstract CommandContext getCommandContext(
292
+            final FrameContainer origin,
293
+            final CommandInfo commandInfo,
294
+            final Command command,
295
+            final CommandArguments args);
296
+
280 297
     /**
281 298
      * Executes the specified command with the given arguments.
282 299
      *
@@ -284,11 +301,11 @@ public abstract class CommandParser implements Serializable {
284 301
      * @param commandInfo The command information object matched by the command
285 302
      * @param command The command to be executed
286 303
      * @param args The arguments to the command
287
-     * @since 0.6.4
304
+     * @param context The context to use when executing the command
288 305
      */
289 306
     protected abstract void executeCommand(final FrameContainer origin,
290 307
             final CommandInfo commandInfo, final Command command,
291
-            final CommandArguments args);
308
+            final CommandArguments args, final CommandContext context);
292 309
 
293 310
     /**
294 311
      * Called when the user attempted to issue a command (i.e., used the command

+ 15
- 4
src/com/dmdirc/commandparser/parsers/GlobalCommandParser.java Прегледај датотеку

@@ -75,12 +75,23 @@ public class GlobalCommandParser extends CommandParser {
75 75
         commandManager.loadCommands(this, CommandType.TYPE_GLOBAL);
76 76
     }
77 77
 
78
-    /** {@inheritDoc} */
79 78
     @Override
80
-    protected void executeCommand(final FrameContainer origin,
81
-            final CommandInfo commandInfo, final Command command,
79
+    protected CommandContext getCommandContext(
80
+            final FrameContainer origin,
81
+            final CommandInfo commandInfo,
82
+            final Command command,
82 83
             final CommandArguments args) {
83
-        command.execute(origin, args, new CommandContext(origin, commandInfo));
84
+        return new CommandContext(origin, commandInfo);
85
+    }
86
+
87
+    @Override
88
+    protected void executeCommand(
89
+            final FrameContainer origin,
90
+            final CommandInfo commandInfo,
91
+            final Command command,
92
+            final CommandArguments args,
93
+            final CommandContext context) {
94
+        command.execute(origin, args, context);
84 95
     }
85 96
 
86 97
     /**

+ 17
- 6
src/com/dmdirc/commandparser/parsers/QueryCommandParser.java Прегледај датотеку

@@ -29,6 +29,7 @@ import com.dmdirc.commandparser.CommandArguments;
29 29
 import com.dmdirc.commandparser.CommandInfo;
30 30
 import com.dmdirc.commandparser.CommandType;
31 31
 import com.dmdirc.commandparser.commands.Command;
32
+import com.dmdirc.commandparser.commands.context.CommandContext;
32 33
 import com.dmdirc.commandparser.commands.context.QueryCommandContext;
33 34
 import com.dmdirc.interfaces.CommandController;
34 35
 
@@ -77,16 +78,26 @@ public class QueryCommandParser extends ChatCommandParser {
77 78
                 CommandType.TYPE_SERVER, CommandType.TYPE_QUERY);
78 79
     }
79 80
 
80
-    /** {@inheritDoc} */
81 81
     @Override
82
-    protected void executeCommand(final FrameContainer origin,
83
-            final CommandInfo commandInfo, final Command command,
82
+    protected CommandContext getCommandContext(
83
+            final FrameContainer origin,
84
+            final CommandInfo commandInfo,
85
+            final Command command,
84 86
             final CommandArguments args) {
87
+        return new QueryCommandContext(origin, commandInfo, query);
88
+    }
89
+
90
+    @Override
91
+    protected void executeCommand(
92
+            final FrameContainer origin,
93
+            final CommandInfo commandInfo,
94
+            final Command command,
95
+            final CommandArguments args,
96
+            final CommandContext context) {
85 97
         if (commandInfo.getType() == CommandType.TYPE_QUERY) {
86
-            command.execute(origin, args, new QueryCommandContext(origin,
87
-                    commandInfo, query));
98
+            command.execute(origin, args, context);
88 99
         } else {
89
-            super.executeCommand(origin, commandInfo, command, args);
100
+            super.executeCommand(origin, commandInfo, command, args, context);
90 101
         }
91 102
     }
92 103
 

+ 17
- 5
src/com/dmdirc/commandparser/parsers/ServerCommandParser.java Прегледај датотеку

@@ -29,6 +29,7 @@ import com.dmdirc.commandparser.CommandArguments;
29 29
 import com.dmdirc.commandparser.CommandInfo;
30 30
 import com.dmdirc.commandparser.CommandType;
31 31
 import com.dmdirc.commandparser.commands.Command;
32
+import com.dmdirc.commandparser.commands.context.CommandContext;
32 33
 import com.dmdirc.commandparser.commands.context.ServerCommandContext;
33 34
 import com.dmdirc.interfaces.CommandController;
34 35
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
@@ -78,11 +79,22 @@ public class ServerCommandParser extends GlobalCommandParser {
78 79
         commandManager.loadCommands(this, CommandType.TYPE_GLOBAL, CommandType.TYPE_SERVER);
79 80
     }
80 81
 
81
-    /** {@inheritDoc} */
82 82
     @Override
83
-    protected void executeCommand(final FrameContainer origin,
84
-            final CommandInfo commandInfo, final Command command,
83
+    protected CommandContext getCommandContext(
84
+            final FrameContainer origin,
85
+            final CommandInfo commandInfo,
86
+            final Command command,
85 87
             final CommandArguments args) {
88
+        return new ServerCommandContext(origin, commandInfo, server);
89
+    }
90
+
91
+    @Override
92
+    protected void executeCommand(
93
+            final FrameContainer origin,
94
+            final CommandInfo commandInfo,
95
+            final Command command,
96
+            final CommandArguments args,
97
+            final CommandContext context) {
86 98
         if (commandInfo.getType() == CommandType.TYPE_SERVER) {
87 99
             if (hasCommandOptions(command) && !getCommandOptions(command).allowOffline()
88 100
                     && (server == null || (server.getState() != ServerState.CONNECTED
@@ -92,10 +104,10 @@ public class ServerCommandParser extends GlobalCommandParser {
92 104
                     origin.addLine("commandError", "You must be connected to use this command");
93 105
                 }
94 106
             } else {
95
-                command.execute(origin, args, new ServerCommandContext(origin, commandInfo, server));
107
+                command.execute(origin, args, context);
96 108
             }
97 109
         } else {
98
-            super.executeCommand(origin, commandInfo, command, args);
110
+            super.executeCommand(origin, commandInfo, command, args, context);
99 111
         }
100 112
     }
101 113
 

+ 9
- 5
test/com/dmdirc/harness/TestCommandParser.java Прегледај датотеку

@@ -27,6 +27,7 @@ import com.dmdirc.commandparser.CommandArguments;
27 27
 import com.dmdirc.commandparser.CommandInfo;
28 28
 import com.dmdirc.commandparser.CommandType;
29 29
 import com.dmdirc.commandparser.commands.Command;
30
+import com.dmdirc.commandparser.commands.context.CommandContext;
30 31
 import com.dmdirc.commandparser.parsers.CommandParser;
31 32
 import com.dmdirc.interfaces.CommandController;
32 33
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
@@ -34,8 +35,6 @@ import com.dmdirc.interfaces.config.AggregateConfigProvider;
34 35
 public class TestCommandParser extends CommandParser {
35 36
     private static final long serialVersionUID = 7073002401375438532L;
36 37
 
37
-    private final AggregateConfigProvider configManager;
38
-
39 38
     public String nonCommandLine;
40 39
 
41 40
     public Command executedCommand;
@@ -49,7 +48,6 @@ public class TestCommandParser extends CommandParser {
49 48
     public TestCommandParser(final AggregateConfigProvider configManager,
50 49
             final CommandController commandManager) {
51 50
         super(configManager, commandManager);
52
-        this.configManager = configManager;
53 51
     }
54 52
 
55 53
     @Override
@@ -58,13 +56,19 @@ public class TestCommandParser extends CommandParser {
58 56
     }
59 57
 
60 58
     @Override
61
-    protected void executeCommand(FrameContainer origin,
62
-            CommandInfo commandInfo, Command command, CommandArguments args) {
59
+    protected void executeCommand(FrameContainer origin, CommandInfo commandInfo, Command command,
60
+            CommandArguments args, CommandContext context) {
63 61
         executedCommand = command;
64 62
         wasSilent = args.isSilent();
65 63
         commandArgs = args;
66 64
     }
67 65
 
66
+    @Override
67
+    protected CommandContext getCommandContext(FrameContainer origin, CommandInfo commandInfo,
68
+            Command command, CommandArguments args) {
69
+        return new CommandContext(origin, commandInfo);
70
+    }
71
+
68 72
     @Override
69 73
     protected void handleNonCommand(FrameContainer origin, String line) {
70 74
         nonCommandLine = line;

Loading…
Откажи
Сачувај