Bläddra i källkod

Add CommandOptions class and make all server commands that can't be used

offline use it. Fixes issue 2317.
tags/0.6.3m1rc1
Chris Smith 15 år sedan
förälder
incheckning
a8474c9ac0

+ 47
- 0
src/com/dmdirc/commandparser/commands/CommandOptions.java Visa fil

@@ -0,0 +1,47 @@
1
+/*
2
+ * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.commandparser.commands;
24
+
25
+import java.lang.annotation.ElementType;
26
+import java.lang.annotation.Retention;
27
+import java.lang.annotation.RetentionPolicy;
28
+import java.lang.annotation.Target;
29
+
30
+/**
31
+ * Allows commands to define options that affect their behaviour.
32
+ *
33
+ * @since 0.6.3
34
+ * @author chris
35
+ */
36
+@Retention(RetentionPolicy.RUNTIME)
37
+@Target(ElementType.TYPE)
38
+public @interface CommandOptions {
39
+
40
+    /**
41
+     * Whether or not to allow use of the command while not connected.
42
+     *
43
+     * @return True if the command may be used offline, false otherwise
44
+     */
45
+    boolean allowOffline() default true;
46
+
47
+}

+ 4
- 7
src/com/dmdirc/commandparser/commands/server/Away.java Visa fil

@@ -23,9 +23,9 @@
23 23
 package com.dmdirc.commandparser.commands.server;
24 24
 
25 25
 import com.dmdirc.Server;
26
-import com.dmdirc.ServerState;
27 26
 import com.dmdirc.commandparser.CommandArguments;
28 27
 import com.dmdirc.commandparser.CommandManager;
28
+import com.dmdirc.commandparser.commands.CommandOptions;
29 29
 import com.dmdirc.commandparser.commands.ServerCommand;
30 30
 import com.dmdirc.ui.interfaces.InputWindow;
31 31
 
@@ -33,6 +33,7 @@ import com.dmdirc.ui.interfaces.InputWindow;
33 33
  * The away command allows the user to set their away message.
34 34
  * @author chris
35 35
  */
36
+@CommandOptions(allowOffline=false)
36 37
 public final class Away extends ServerCommand {
37 38
     
38 39
     /**
@@ -48,13 +49,9 @@ public final class Away extends ServerCommand {
48 49
     @Override
49 50
     public void execute(final InputWindow origin, final Server server,
50 51
             final boolean isSilent, final CommandArguments args) {
51
-        if (server.getState() == ServerState.CONNECTED) {
52
-            final String line = args.getArgumentsAsString();
52
+        final String line = args.getArgumentsAsString();
53 53
 
54
-            server.getParser().sendLine("AWAY :" + line);
55
-        } else {
56
-            sendLine(origin, isSilent, FORMAT_ERROR, "Not connected");
57
-        }
54
+        server.getParser().sendLine("AWAY :" + line);
58 55
     }
59 56
     
60 57
     

+ 3
- 7
src/com/dmdirc/commandparser/commands/server/Back.java Visa fil

@@ -23,9 +23,9 @@
23 23
 package com.dmdirc.commandparser.commands.server;
24 24
 
25 25
 import com.dmdirc.Server;
26
-import com.dmdirc.ServerState;
27 26
 import com.dmdirc.commandparser.CommandArguments;
28 27
 import com.dmdirc.commandparser.CommandManager;
28
+import com.dmdirc.commandparser.commands.CommandOptions;
29 29
 import com.dmdirc.commandparser.commands.IntelligentCommand;
30 30
 import com.dmdirc.commandparser.commands.ServerCommand;
31 31
 import com.dmdirc.ui.input.AdditionalTabTargets;
@@ -37,6 +37,7 @@ import java.util.List;
37 37
  * The back command allows the user to unset their away status.
38 38
  * @author chris
39 39
  */
40
+@CommandOptions(allowOffline=false)
40 41
 public final class Back extends ServerCommand implements IntelligentCommand {
41 42
     
42 43
     /**
@@ -57,12 +58,7 @@ public final class Back extends ServerCommand implements IntelligentCommand {
57 58
      */
58 59
     @Override
59 60
     public void execute(final InputWindow origin, final Server server,
60
-            final boolean isSilent, final CommandArguments args) {
61
-        if (server.getState() != ServerState.CONNECTED) {
62
-            sendLine(origin, isSilent, FORMAT_ERROR, "Not connected");
63
-            return;
64
-        }
65
-        
61
+            final boolean isSilent, final CommandArguments args) {        
66 62
         server.getParser().sendLine("AWAY");
67 63
     }
68 64
     

+ 2
- 6
src/com/dmdirc/commandparser/commands/server/Ctcp.java Visa fil

@@ -23,9 +23,9 @@
23 23
 package com.dmdirc.commandparser.commands.server;
24 24
 
25 25
 import com.dmdirc.Server;
26
-import com.dmdirc.ServerState;
27 26
 import com.dmdirc.commandparser.CommandArguments;
28 27
 import com.dmdirc.commandparser.CommandManager;
28
+import com.dmdirc.commandparser.commands.CommandOptions;
29 29
 import com.dmdirc.commandparser.commands.IntelligentCommand;
30 30
 import com.dmdirc.commandparser.commands.ServerCommand;
31 31
 import com.dmdirc.ui.input.AdditionalTabTargets;
@@ -38,6 +38,7 @@ import java.util.List;
38 38
  * Allows the user to send CTCP messages.
39 39
  * @author chris
40 40
  */
41
+@CommandOptions(allowOffline=false)
41 42
 public final class Ctcp extends ServerCommand implements IntelligentCommand {
42 43
     
43 44
     /**
@@ -59,11 +60,6 @@ public final class Ctcp extends ServerCommand implements IntelligentCommand {
59 60
     @Override
60 61
     public void execute(final InputWindow origin, final Server server,
61 62
             final boolean isSilent, final CommandArguments args) {
62
-        if (server.getState() != ServerState.CONNECTED) {
63
-            sendLine(origin, isSilent, FORMAT_ERROR, "Not connected");
64
-            return;
65
-        }
66
-
67 63
         if (args.getArguments().length < 2) {
68 64
             showUsage(origin, isSilent, "ctcp", "<target> <type> [arguments]");
69 65
         } else {

+ 7
- 3
src/com/dmdirc/commandparser/commands/server/Disconnect.java Visa fil

@@ -50,6 +50,7 @@ public final class Disconnect extends ServerCommand {
50 50
      * @param isSilent Whether this command is silenced or not
51 51
      * @param args The user supplied arguments
52 52
      */
53
+    @Override
53 54
     public void execute(final InputWindow origin, final Server server,
54 55
             final boolean isSilent, final CommandArguments args) {
55 56
         String line;
@@ -64,17 +65,20 @@ public final class Disconnect extends ServerCommand {
64 65
     }
65 66
     
66 67
     
67
-    /** {@inheritDoc}. */
68
+    /** {@inheritDoc} */
69
+    @Override
68 70
     public String getName() {
69 71
         return "disconnect";
70 72
     }
71 73
     
72
-    /** {@inheritDoc}. */
74
+    /** {@inheritDoc} */
75
+    @Override
73 76
     public boolean showInHelp() {
74 77
         return true;
75 78
     }
76 79
     
77
-    /** {@inheritDoc}. */
80
+    /** {@inheritDoc} */
81
+    @Override
78 82
     public String getHelp() {
79 83
         return "disconnect [reason] - disconnect from this server";
80 84
     }

+ 2
- 6
src/com/dmdirc/commandparser/commands/server/Message.java Visa fil

@@ -23,9 +23,9 @@
23 23
 package com.dmdirc.commandparser.commands.server;
24 24
 
25 25
 import com.dmdirc.Server;
26
-import com.dmdirc.ServerState;
27 26
 import com.dmdirc.commandparser.CommandArguments;
28 27
 import com.dmdirc.commandparser.CommandManager;
28
+import com.dmdirc.commandparser.commands.CommandOptions;
29 29
 import com.dmdirc.commandparser.commands.IntelligentCommand;
30 30
 import com.dmdirc.commandparser.commands.ServerCommand;
31 31
 import com.dmdirc.commandparser.commands.WrappableCommand;
@@ -39,6 +39,7 @@ import java.util.List;
39 39
  * Allows the user to send privmsgs.
40 40
  * @author chris
41 41
  */
42
+@CommandOptions(allowOffline=false)
42 43
 public final class Message extends ServerCommand implements IntelligentCommand,
43 44
         WrappableCommand {
44 45
     
@@ -55,11 +56,6 @@ public final class Message extends ServerCommand implements IntelligentCommand,
55 56
     @Override
56 57
     public void execute(final InputWindow origin, final Server server,
57 58
             final boolean isSilent, final CommandArguments args) {
58
-        if (server.getState() != ServerState.CONNECTED) {
59
-            sendLine(origin, isSilent, FORMAT_ERROR, "Not connected");
60
-            return;
61
-        }
62
-        
63 59
         if (args.getArguments().length < 2) {
64 60
             showUsage(origin, isSilent, "msg", "<target> <message>");
65 61
         } else {

+ 2
- 0
src/com/dmdirc/commandparser/commands/server/Nick.java Visa fil

@@ -25,6 +25,7 @@ package com.dmdirc.commandparser.commands.server;
25 25
 import com.dmdirc.Server;
26 26
 import com.dmdirc.commandparser.CommandArguments;
27 27
 import com.dmdirc.commandparser.CommandManager;
28
+import com.dmdirc.commandparser.commands.CommandOptions;
28 29
 import com.dmdirc.commandparser.commands.IntelligentCommand;
29 30
 import com.dmdirc.commandparser.commands.ServerCommand;
30 31
 import com.dmdirc.ui.input.AdditionalTabTargets;
@@ -37,6 +38,7 @@ import java.util.List;
37 38
  * Allows the user to change nickname.
38 39
  * @author chris
39 40
  */
41
+@CommandOptions(allowOffline=false)
40 42
 public final class Nick extends ServerCommand implements IntelligentCommand {
41 43
     
42 44
     /**

+ 2
- 5
src/com/dmdirc/commandparser/commands/server/Notice.java Visa fil

@@ -26,6 +26,7 @@ import com.dmdirc.Server;
26 26
 import com.dmdirc.ServerState;
27 27
 import com.dmdirc.commandparser.CommandArguments;
28 28
 import com.dmdirc.commandparser.CommandManager;
29
+import com.dmdirc.commandparser.commands.CommandOptions;
29 30
 import com.dmdirc.commandparser.commands.IntelligentCommand;
30 31
 import com.dmdirc.commandparser.commands.ServerCommand;
31 32
 import com.dmdirc.ui.input.AdditionalTabTargets;
@@ -38,6 +39,7 @@ import java.util.List;
38 39
  * Allows the user to send notices.
39 40
  * @author chris
40 41
  */
42
+@CommandOptions(allowOffline=false)
41 43
 public final class Notice extends ServerCommand implements IntelligentCommand {
42 44
     
43 45
     /**
@@ -53,11 +55,6 @@ public final class Notice extends ServerCommand implements IntelligentCommand {
53 55
     @Override
54 56
     public void execute(final InputWindow origin, final Server server,
55 57
             final boolean isSilent, final CommandArguments args) {
56
-        if (server.getState() != ServerState.CONNECTED) {
57
-            sendLine(origin, isSilent, FORMAT_ERROR, "Not connected");
58
-            return;
59
-        }
60
-        
61 58
         if (args.getArguments().length < 2) {
62 59
             showUsage(origin, isSilent, "notice", "<target> <message>");
63 60
         } else {

+ 2
- 0
src/com/dmdirc/commandparser/commands/server/Raw.java Visa fil

@@ -25,6 +25,7 @@ package com.dmdirc.commandparser.commands.server;
25 25
 import com.dmdirc.Server;
26 26
 import com.dmdirc.commandparser.CommandArguments;
27 27
 import com.dmdirc.commandparser.CommandManager;
28
+import com.dmdirc.commandparser.commands.CommandOptions;
28 29
 import com.dmdirc.commandparser.commands.ServerCommand;
29 30
 import com.dmdirc.ui.interfaces.InputWindow;
30 31
 
@@ -33,6 +34,7 @@ import com.dmdirc.ui.interfaces.InputWindow;
33 34
  * irc server.
34 35
  * @author chris
35 36
  */
37
+@CommandOptions(allowOffline=false)
36 38
 public final class Raw extends ServerCommand {
37 39
     
38 40
     /**

+ 2
- 0
src/com/dmdirc/commandparser/commands/server/RawServerCommand.java Visa fil

@@ -25,6 +25,7 @@ package com.dmdirc.commandparser.commands.server;
25 25
 import com.dmdirc.Server;
26 26
 import com.dmdirc.commandparser.CommandArguments;
27 27
 import com.dmdirc.commandparser.CommandManager;
28
+import com.dmdirc.commandparser.commands.CommandOptions;
28 29
 import com.dmdirc.commandparser.commands.ServerCommand;
29 30
 import com.dmdirc.ui.interfaces.InputWindow;
30 31
 
@@ -34,6 +35,7 @@ import com.dmdirc.ui.interfaces.InputWindow;
34 35
  * 
35 36
  * @author chris
36 37
  */
38
+@CommandOptions(allowOffline=false)
37 39
 public final class RawServerCommand extends ServerCommand {
38 40
     
39 41
     /** The name of this raw command. */

+ 2
- 0
src/com/dmdirc/commandparser/commands/server/Umode.java Visa fil

@@ -26,6 +26,7 @@ import com.dmdirc.Server;
26 26
 import com.dmdirc.ServerState;
27 27
 import com.dmdirc.commandparser.CommandArguments;
28 28
 import com.dmdirc.commandparser.CommandManager;
29
+import com.dmdirc.commandparser.commands.CommandOptions;
29 30
 import com.dmdirc.commandparser.commands.ServerCommand;
30 31
 import com.dmdirc.ui.interfaces.InputWindow;
31 32
 
@@ -34,6 +35,7 @@ import com.dmdirc.ui.interfaces.InputWindow;
34 35
  * 
35 36
  * @author chris
36 37
  */
38
+@CommandOptions(allowOffline=false)
37 39
 public class Umode extends ServerCommand {
38 40
     
39 41
     /**

+ 22
- 0
src/com/dmdirc/commandparser/parsers/CommandParser.java Visa fil

@@ -30,6 +30,7 @@ import com.dmdirc.commandparser.CommandInfo;
30 30
 import com.dmdirc.commandparser.CommandManager;
31 31
 import com.dmdirc.commandparser.CommandType;
32 32
 import com.dmdirc.commandparser.commands.Command;
33
+import com.dmdirc.commandparser.commands.CommandOptions;
33 34
 import com.dmdirc.commandparser.commands.ExternalCommand;
34 35
 import com.dmdirc.commandparser.commands.PreviousCommand;
35 36
 import com.dmdirc.config.IdentityManager;
@@ -262,4 +263,25 @@ public abstract class CommandParser implements Serializable {
262 263
      */
263 264
     protected abstract void handleNonCommand(final InputWindow origin,
264 265
             final String line);
266
+
267
+    /**
268
+     * Determines if the specified command has defined any command options.
269
+     *
270
+     * @param command The command to investigate
271
+     * @return True if the command defines options, false otherwise
272
+     */
273
+    protected boolean hasCommandOptions(final Command command) {
274
+        return command.getClass().isAnnotationPresent(CommandOptions.class);
275
+    }
276
+
277
+    /**
278
+     * Retrieves the command options for the specified command. If the command
279
+     * does not define options, this method will return null.
280
+     *
281
+     * @param command The command whose options should be retrieved
282
+     * @return The command's options, or null if not available
283
+     */
284
+    protected CommandOptions getCommandOptions(final Command command) {
285
+        return command.getClass().getAnnotation(CommandOptions.class);
286
+    }
265 287
 }

+ 11
- 1
src/com/dmdirc/commandparser/parsers/ServerCommandParser.java Visa fil

@@ -23,6 +23,7 @@
23 23
 package com.dmdirc.commandparser.parsers;
24 24
 
25 25
 import com.dmdirc.Server;
26
+import com.dmdirc.ServerState;
26 27
 import com.dmdirc.commandparser.CommandArguments;
27 28
 import com.dmdirc.commandparser.CommandManager;
28 29
 import com.dmdirc.commandparser.CommandType;
@@ -70,7 +71,16 @@ public final class ServerCommandParser extends CommandParser {
70 71
     protected void executeCommand(final InputWindow origin,
71 72
             final boolean isSilent, final Command command, final CommandArguments args) {
72 73
         if (command instanceof ServerCommand) {
73
-            ((ServerCommand) command).execute(origin, server, isSilent, args);
74
+            if (hasCommandOptions(command) && !getCommandOptions(command).allowOffline()
75
+                    && ((server.getState() != ServerState.CONNECTED
76
+                    && server.getState() != ServerState.CONNECTING)
77
+                    || server.getParser() == null)) {
78
+                if (!isSilent) {
79
+                    origin.addLine("commandError", "You must be connected to use this command");
80
+                }
81
+            } else {
82
+                ((ServerCommand) command).execute(origin, server, isSilent, args);
83
+            }
74 84
         } else {
75 85
             ((GlobalCommand) command).execute(origin, isSilent, args);
76 86
         }

Laddar…
Avbryt
Spara