瀏覽代碼

Add unknown command event.

Depends-On: I06ee87cf6f7795eb3ee07828fb43f4381a465ac3
Change-Id: Ibcfc059d943a64494842eab68428ea8351206a09
Reviewed-on: http://gerrit.dmdirc.com/3488
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes 10 年之前
父節點
當前提交
cb25fc6edc

+ 1
- 1
src/com/dmdirc/Channel.java 查看文件

@@ -117,7 +117,7 @@ public class Channel extends MessageTarget implements ConfigChangeListener, Grou
117 117
         super(newServer, "channel-inactive", newChannelInfo.getName(),
118 118
                 Styliser.stipControlCodes(newChannelInfo.getName()),
119 119
                 configMigrator.getConfigProvider(),
120
-                new ChannelCommandParser(newServer, commandController),
120
+                new ChannelCommandParser(newServer, commandController, eventBus),
121 121
                 tabCompleterFactory.getTabCompleter(newServer.getTabCompleter(),
122 122
                         configMigrator.getConfigProvider(), CommandType.TYPE_CHANNEL,
123 123
                         CommandType.TYPE_CHAT),

+ 1
- 1
src/com/dmdirc/Query.java 查看文件

@@ -96,7 +96,7 @@ public class Query extends MessageTarget implements PrivateActionListener,
96 96
         super(newServer, "query", newServer.parseHostmask(newHost)[0],
97 97
                 newServer.parseHostmask(newHost)[0],
98 98
                 newServer.getConfigManager(),
99
-                new QueryCommandParser(newServer, commandController),
99
+                new QueryCommandParser(newServer, commandController, newServer.getEventBus()),
100 100
                 tabCompleterFactory.getTabCompleter(newServer.getTabCompleter(),
101 101
                         newServer.getConfigManager(),
102 102
                         CommandType.TYPE_QUERY, CommandType.TYPE_CHAT),

+ 2
- 1
src/com/dmdirc/Raw.java 查看文件

@@ -64,7 +64,8 @@ public class Raw extends FrameContainer implements DataInListener, DataOutListen
64 64
             final MessageSinkManager messageSinkManager,
65 65
             final URLBuilder urlBuilder) {
66 66
         super(newServer, "raw", "Raw", "(Raw log)", newServer.getConfigManager(), urlBuilder,
67
-                new ServerCommandParser(newServer.getConfigManager(), commandController),
67
+                new ServerCommandParser(newServer.getConfigManager(), commandController, newServer.
68
+                        getEventBus()),
68 69
                 newServer.getTabCompleter(),
69 70
                 messageSinkManager,
70 71
                 newServer.getEventBus(),

+ 9
- 2
src/com/dmdirc/ServerManager.java 查看文件

@@ -34,6 +34,7 @@ import com.dmdirc.logger.Logger;
34 34
 import com.dmdirc.parser.common.ChannelJoinRequest;
35 35
 import com.dmdirc.ui.WindowManager;
36 36
 
37
+import com.google.common.eventbus.EventBus;
37 38
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
38 39
 
39 40
 import java.net.URI;
@@ -69,6 +70,8 @@ public class ServerManager implements ServerFactory {
69 70
     private final WindowManager windowManager;
70 71
     /** Concrete server factory to use. */
71 72
     private final ServerFactoryImpl serverFactoryImpl;
73
+    /** Event bus for servers. */
74
+    private final EventBus eventBus;
72 75
 
73 76
     /**
74 77
      * Creates a new instance of ServerManager.
@@ -78,6 +81,7 @@ public class ServerManager implements ServerFactory {
78 81
      * @param commandController  A provider of {@link CommandController}s to pass to servers.
79 82
      * @param windowManager      Window manager to add new servers to.
80 83
      * @param serverFactory      The factory to use to create servers.
84
+     * @param eventBus           The event bus to pass to servers.
81 85
      */
82 86
     @Inject
83 87
     public ServerManager(
@@ -85,12 +89,14 @@ public class ServerManager implements ServerFactory {
85 89
             final IdentityFactory identityFactory,
86 90
             final Provider<CommandController> commandController,
87 91
             final WindowManager windowManager,
88
-            final ServerFactoryImpl serverFactory) {
92
+            final ServerFactoryImpl serverFactory,
93
+            final EventBus eventBus) {
89 94
         this.identityController = identityController;
90 95
         this.identityFactory = identityFactory;
91 96
         this.commandController = commandController;
92 97
         this.windowManager = windowManager;
93 98
         this.serverFactoryImpl = serverFactory;
99
+        this.eventBus = eventBus;
94 100
     }
95 101
 
96 102
     @Override
@@ -100,7 +106,8 @@ public class ServerManager implements ServerFactory {
100 106
 
101 107
         final Server server = serverFactoryImpl.getServer(
102 108
                 configProvider,
103
-                new ServerCommandParser(configProvider.getConfigProvider(), commandController.get()),
109
+                new ServerCommandParser(configProvider.getConfigProvider(), commandController.get(),
110
+                        eventBus),
104 111
                 Executors.newScheduledThreadPool(1,
105 112
                         new ThreadFactoryBuilder().setNameFormat("server-timer-%d").build()),
106 113
                 uri,

+ 1
- 0
src/com/dmdirc/actions/CoreActionType.java 查看文件

@@ -83,6 +83,7 @@ public enum CoreActionType implements ActionType {
83 83
     @Deprecated
84 84
     CLIENT_PREFS_CLOSED(ClientEvents.CLIENT_EVENT, "Preferences dialog opened"),
85 85
     /** Unknown command. */
86
+    @Deprecated
86 87
     UNKNOWN_COMMAND(ClientEvents.UNKNOWN_COMMAND, "Unknown command entered"),
87 88
     /** Server numeric received. */
88 89
     @Deprecated

+ 6
- 2
src/com/dmdirc/commandparser/parsers/ChannelCommandParser.java 查看文件

@@ -33,6 +33,8 @@ import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
33 33
 import com.dmdirc.commandparser.commands.context.CommandContext;
34 34
 import com.dmdirc.interfaces.CommandController;
35 35
 
36
+import com.google.common.eventbus.EventBus;
37
+
36 38
 import javax.annotation.Nonnull;
37 39
 
38 40
 /**
@@ -52,9 +54,11 @@ public class ChannelCommandParser extends ChatCommandParser {
52 54
      *
53 55
      * @param server            The server this parser's query belongs to
54 56
      * @param commandController The controller to load commands from.
57
+     * @param eventBus          Event bus to post events on
55 58
      */
56
-    public ChannelCommandParser(final Server server, final CommandController commandController) {
57
-        super(server, commandController);
59
+    public ChannelCommandParser(final Server server, final CommandController commandController,
60
+            final EventBus eventBus) {
61
+        super(server, commandController, eventBus);
58 62
     }
59 63
 
60 64
     @Override

+ 6
- 2
src/com/dmdirc/commandparser/parsers/ChatCommandParser.java 查看文件

@@ -33,6 +33,8 @@ import com.dmdirc.commandparser.commands.context.ChatCommandContext;
33 33
 import com.dmdirc.commandparser.commands.context.CommandContext;
34 34
 import com.dmdirc.interfaces.CommandController;
35 35
 
36
+import com.google.common.eventbus.EventBus;
37
+
36 38
 import javax.annotation.Nonnull;
37 39
 
38 40
 /**
@@ -52,9 +54,11 @@ public class ChatCommandParser extends ServerCommandParser {
52 54
      *
53 55
      * @param server            The server which owns this parser's container
54 56
      * @param commandController The controller to load commands from.
57
+     * @param eventBus          Event but to post events on
55 58
      */
56
-    public ChatCommandParser(final Server server, final CommandController commandController) {
57
-        super(server.getConfigManager(), commandController);
59
+    public ChatCommandParser(final Server server, final CommandController commandController,
60
+            final EventBus eventBus) {
61
+        super(server.getConfigManager(), commandController, eventBus);
58 62
         super.setOwner(server);
59 63
     }
60 64
 

+ 16
- 12
src/com/dmdirc/commandparser/parsers/CommandParser.java 查看文件

@@ -24,8 +24,6 @@ package com.dmdirc.commandparser.parsers;
24 24
 
25 25
 import com.dmdirc.FrameContainer;
26 26
 import com.dmdirc.Server;
27
-import com.dmdirc.actions.ActionManager;
28
-import com.dmdirc.actions.CoreActionType;
29 27
 import com.dmdirc.commandparser.CommandArguments;
30 28
 import com.dmdirc.commandparser.CommandInfo;
31 29
 import com.dmdirc.commandparser.CommandInfoPair;
@@ -35,11 +33,16 @@ import com.dmdirc.commandparser.commands.CommandOptions;
35 33
 import com.dmdirc.commandparser.commands.ExternalCommand;
36 34
 import com.dmdirc.commandparser.commands.PreviousCommand;
37 35
 import com.dmdirc.commandparser.commands.context.CommandContext;
36
+import com.dmdirc.events.DisplayableEvent;
37
+import com.dmdirc.events.EventUtils;
38
+import com.dmdirc.events.UnknownCommandEvent;
38 39
 import com.dmdirc.interfaces.CommandController;
39 40
 import com.dmdirc.interfaces.Connection;
40 41
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
41 42
 import com.dmdirc.util.collections.RollingList;
42 43
 
44
+import com.google.common.eventbus.EventBus;
45
+
43 46
 import java.io.Serializable;
44 47
 import java.util.HashMap;
45 48
 import java.util.Map;
@@ -63,15 +66,20 @@ public abstract class CommandParser implements Serializable {
63 66
     private final RollingList<PreviousCommand> history;
64 67
     /** Command manager to use. */
65 68
     protected final CommandController commandManager;
69
+    /** Event bus to post events to. */
70
+    private final EventBus eventBus;
66 71
 
67 72
     /**
68 73
      * Creates a new instance of CommandParser.
69 74
      *
70 75
      * @param configManager  Config manager to read settings
71 76
      * @param commandManager Command manager to load plugins from
77
+     * @param eventBus       The event bus to post events to.
72 78
      */
73 79
     protected CommandParser(final AggregateConfigProvider configManager,
74
-            final CommandController commandManager) {
80
+            final CommandController commandManager,
81
+            final EventBus eventBus) {
82
+        this.eventBus = eventBus;
75 83
         commands = new HashMap<>();
76 84
         history = new RollingList<>(configManager.getOptionInt("general", "commandhistory"));
77 85
         this.commandManager = commandManager;
@@ -327,17 +335,13 @@ public abstract class CommandParser implements Serializable {
327 335
     protected void handleInvalidCommand(final FrameContainer origin,
328 336
             final CommandArguments args) {
329 337
         if (origin == null) {
330
-            ActionManager.getActionManager().triggerEvent(
331
-                    CoreActionType.UNKNOWN_COMMAND, null, null,
332
-                    args.getCommandName(), args.getArguments());
338
+            eventBus.post(new UnknownCommandEvent(null, args.getCommandName(), args.getArguments()));
333 339
         } else {
334
-            final StringBuffer buff = new StringBuffer("unknownCommand");
335
-
336
-            ActionManager.getActionManager().triggerEvent(
337
-                    CoreActionType.UNKNOWN_COMMAND, buff, origin,
338
-                    args.getCommandName(), args.getArguments());
340
+            final DisplayableEvent event = new UnknownCommandEvent(origin, args.getCommandName(),
341
+                    args.getArguments());
342
+            final String format = EventUtils.postDisplayable(eventBus, event, "unknownCommand");
339 343
 
340
-            origin.addLine(buff, args.getCommandName());
344
+            origin.addLine(format, args.getCommandName());
341 345
         }
342 346
     }
343 347
 

+ 6
- 2
src/com/dmdirc/commandparser/parsers/GlobalCommandParser.java 查看文件

@@ -34,6 +34,8 @@ import com.dmdirc.interfaces.config.AggregateConfigProvider;
34 34
 import com.dmdirc.logger.ErrorLevel;
35 35
 import com.dmdirc.logger.Logger;
36 36
 
37
+import com.google.common.eventbus.EventBus;
38
+
37 39
 import javax.annotation.Nonnull;
38 40
 import javax.inject.Inject;
39 41
 import javax.inject.Singleton;
@@ -52,12 +54,14 @@ public class GlobalCommandParser extends CommandParser {
52 54
      *
53 55
      * @param configManager  Config manager to read settings from
54 56
      * @param commandManager Command manager to load commands from
57
+     * @param eventBus       eventBus
55 58
      */
56 59
     @Inject
57 60
     public GlobalCommandParser(
58 61
             @ClientModule.GlobalConfig final AggregateConfigProvider configManager,
59
-            final CommandController commandManager) {
60
-        super(configManager, commandManager);
62
+            final CommandController commandManager,
63
+            final EventBus eventBus) {
64
+        super(configManager, commandManager, eventBus);
61 65
     }
62 66
 
63 67
     @Override

+ 6
- 2
src/com/dmdirc/commandparser/parsers/QueryCommandParser.java 查看文件

@@ -33,6 +33,8 @@ import com.dmdirc.commandparser.commands.context.CommandContext;
33 33
 import com.dmdirc.commandparser.commands.context.QueryCommandContext;
34 34
 import com.dmdirc.interfaces.CommandController;
35 35
 
36
+import com.google.common.eventbus.EventBus;
37
+
36 38
 import javax.annotation.Nonnull;
37 39
 
38 40
 /**
@@ -53,9 +55,11 @@ public class QueryCommandParser extends ChatCommandParser {
53 55
      *
54 56
      * @param server            The server this parser's query belongs to
55 57
      * @param commandController The controller to load commands from.
58
+     * @param eventBus          Event bus to post events on
56 59
      */
57
-    public QueryCommandParser(final Server server, final CommandController commandController) {
58
-        super(server, commandController);
60
+    public QueryCommandParser(final Server server, final CommandController commandController,
61
+            final EventBus eventBus) {
62
+        super(server, commandController, eventBus);
59 63
     }
60 64
 
61 65
     @Override

+ 6
- 2
src/com/dmdirc/commandparser/parsers/ServerCommandParser.java 查看文件

@@ -34,6 +34,8 @@ import com.dmdirc.commandparser.commands.context.ServerCommandContext;
34 34
 import com.dmdirc.interfaces.CommandController;
35 35
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
36 36
 
37
+import com.google.common.eventbus.EventBus;
38
+
37 39
 import javax.annotation.Nonnull;
38 40
 
39 41
 /**
@@ -49,11 +51,13 @@ public class ServerCommandParser extends GlobalCommandParser {
49 51
      *
50 52
      * @param configManager     Config manager to read settings from
51 53
      * @param commandController The controller to load commands from.
54
+     * @param eventBus          Event bus to post events on
52 55
      */
53 56
     public ServerCommandParser(
54 57
             final AggregateConfigProvider configManager,
55
-            final CommandController commandController) {
56
-        super(configManager, commandController);
58
+            final CommandController commandController,
59
+            final EventBus eventBus) {
60
+        super(configManager, commandController, eventBus);
57 61
     }
58 62
     /**
59 63
      * The server instance that this parser is attached to.

+ 79
- 0
src/com/dmdirc/events/UnknownCommandEvent.java 查看文件

@@ -0,0 +1,79 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.events;
24
+
25
+import com.dmdirc.FrameContainer;
26
+
27
+import java.util.concurrent.atomic.AtomicReference;
28
+
29
+import javax.annotation.Nullable;
30
+
31
+/**
32
+ * Fired when an unknown command is used.
33
+ */
34
+public class UnknownCommandEvent extends DMDircEvent implements DisplayableEvent {
35
+
36
+    /** The display format to use for this event. */
37
+    private final AtomicReference<String> displayFormatRef = new AtomicReference<>("");
38
+    @Nullable private final FrameContainer source;
39
+    private final String command;
40
+    private final String[] arguments;
41
+
42
+    public UnknownCommandEvent(final long timestamp, @Nullable final FrameContainer source,
43
+            final String command, final String[] arguments) {
44
+        super(timestamp);
45
+        this.source = source;
46
+        this.command = command;
47
+        this.arguments = arguments;
48
+    }
49
+
50
+    public UnknownCommandEvent(@Nullable final FrameContainer source, final String command,
51
+            final String[] arguments) {
52
+        this.source = source;
53
+        this.command = command;
54
+        this.arguments = arguments;
55
+    }
56
+
57
+    public FrameContainer getSource() {
58
+        return source;
59
+    }
60
+
61
+    public String getCommand() {
62
+        return command;
63
+    }
64
+
65
+    public String[] getArguments() {
66
+        return arguments;
67
+    }
68
+
69
+    @Override
70
+    public String getDisplayFormat() {
71
+        return displayFormatRef.get();
72
+    }
73
+
74
+    @Override
75
+    public void setDisplayFormat(String format) {
76
+        this.displayFormatRef.set(format);
77
+    }
78
+
79
+}

+ 4
- 1
test/com/dmdirc/ServerManagerTest.java 查看文件

@@ -32,6 +32,8 @@ import com.dmdirc.interfaces.config.IdentityFactory;
32 32
 import com.dmdirc.parser.common.ChannelJoinRequest;
33 33
 import com.dmdirc.ui.WindowManager;
34 34
 
35
+import com.google.common.eventbus.EventBus;
36
+
35 37
 import java.net.URI;
36 38
 import java.util.Arrays;
37 39
 import java.util.concurrent.ScheduledExecutorService;
@@ -68,6 +70,7 @@ public class ServerManagerTest {
68 70
     @Mock private WindowManager windowManager;
69 71
     @Mock private ServerFactoryImpl serverFactoryImpl;
70 72
     @Mock private Server server;
73
+    @Mock private EventBus eventBus;
71 74
 
72 75
     @Captor private ArgumentCaptor<URI> uriCaptor;
73 76
 
@@ -76,7 +79,7 @@ public class ServerManagerTest {
76 79
     @Before
77 80
     public void setUp() throws Exception {
78 81
         serverManager = new ServerManager(identityController, identityFactory,
79
-                commandControllerProvider, windowManager, serverFactoryImpl);
82
+                commandControllerProvider, windowManager, serverFactoryImpl, eventBus);
80 83
 
81 84
         when(commandControllerProvider.get()).thenReturn(commandController);
82 85
 

+ 5
- 2
test/com/dmdirc/commandparser/parsers/CommandParserTest.java 查看文件

@@ -31,6 +31,8 @@ import com.dmdirc.interfaces.CommandController;
31 31
 import com.dmdirc.interfaces.Connection;
32 32
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
33 33
 
34
+import com.google.common.eventbus.EventBus;
35
+
34 36
 import org.junit.Before;
35 37
 import org.junit.Test;
36 38
 import org.junit.runner.RunWith;
@@ -56,6 +58,7 @@ public class CommandParserTest {
56 58
     @Mock private FrameContainer container;
57 59
     @Mock private Channel channel;
58 60
     @Mock private Connection connection;
61
+    @Mock private EventBus eventBus;
59 62
     private TestCommandParser commandParser;
60 63
     private TestCommandParser channelCommandParser;
61 64
 
@@ -76,11 +79,11 @@ public class CommandParserTest {
76 79
         when(connection.hasChannel("#channel1")).thenReturn(true);
77 80
         when(connection.getChannel("#channel1")).thenReturn(channel);
78 81
 
79
-        commandParser = new TestCommandParser(configProvider, commandController);
82
+        commandParser = new TestCommandParser(configProvider, commandController, eventBus);
80 83
         commandParser.registerCommand(command, commandInfo);
81 84
         commandParser.registerCommand(channelCommand, channelCommandInfo);
82 85
 
83
-        channelCommandParser = new TestCommandParser(configProvider, commandController);
86
+        channelCommandParser = new TestCommandParser(configProvider, commandController, eventBus);
84 87
         channelCommandParser.registerCommand(channelCommand, channelCommandInfo);
85 88
 
86 89
         when(channel.getCommandParser()).thenReturn(channelCommandParser);

+ 4
- 2
test/com/dmdirc/harness/TestCommandParser.java 查看文件

@@ -31,6 +31,8 @@ import com.dmdirc.commandparser.parsers.CommandParser;
31 31
 import com.dmdirc.interfaces.CommandController;
32 32
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
33 33
 
34
+import com.google.common.eventbus.EventBus;
35
+
34 36
 public class TestCommandParser extends CommandParser {
35 37
 
36 38
     private static final long serialVersionUID = 7073002401375438532L;
@@ -46,8 +48,8 @@ public class TestCommandParser extends CommandParser {
46 48
     public String invalidCommand;
47 49
 
48 50
     public TestCommandParser(final AggregateConfigProvider configManager,
49
-            final CommandController commandManager) {
50
-        super(configManager, commandManager);
51
+            final CommandController commandManager, final EventBus eventBus) {
52
+        super(configManager, commandManager, eventBus);
51 53
     }
52 54
 
53 55
     @Override

+ 1
- 1
test/com/dmdirc/harness/TestWritableFrameContainer.java 查看文件

@@ -47,7 +47,7 @@ public class TestWritableFrameContainer extends FrameContainer {
47 47
             final MessageSinkManager messageSinkManager,
48 48
             final URLBuilder urlBuilder, final EventBus eventBus) {
49 49
         super(null, "raw", "Raw", "(Raw)", cm, urlBuilder,
50
-                new GlobalCommandParser(cm, commandManager),
50
+                new GlobalCommandParser(cm, commandManager, eventBus),
51 51
                 new TabCompleter(mock(CommandController.class), cm),
52 52
                 messageSinkManager,
53 53
                 eventBus,

Loading…
取消
儲存