Bladeren bron

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 jaren geleden
bovenliggende
commit
a8474c9ac0

+ 47
- 0
src/com/dmdirc/commandparser/commands/CommandOptions.java Bestand weergeven

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 Bestand weergeven

23
 package com.dmdirc.commandparser.commands.server;
23
 package com.dmdirc.commandparser.commands.server;
24
 
24
 
25
 import com.dmdirc.Server;
25
 import com.dmdirc.Server;
26
-import com.dmdirc.ServerState;
27
 import com.dmdirc.commandparser.CommandArguments;
26
 import com.dmdirc.commandparser.CommandArguments;
28
 import com.dmdirc.commandparser.CommandManager;
27
 import com.dmdirc.commandparser.CommandManager;
28
+import com.dmdirc.commandparser.commands.CommandOptions;
29
 import com.dmdirc.commandparser.commands.ServerCommand;
29
 import com.dmdirc.commandparser.commands.ServerCommand;
30
 import com.dmdirc.ui.interfaces.InputWindow;
30
 import com.dmdirc.ui.interfaces.InputWindow;
31
 
31
 
33
  * The away command allows the user to set their away message.
33
  * The away command allows the user to set their away message.
34
  * @author chris
34
  * @author chris
35
  */
35
  */
36
+@CommandOptions(allowOffline=false)
36
 public final class Away extends ServerCommand {
37
 public final class Away extends ServerCommand {
37
     
38
     
38
     /**
39
     /**
48
     @Override
49
     @Override
49
     public void execute(final InputWindow origin, final Server server,
50
     public void execute(final InputWindow origin, final Server server,
50
             final boolean isSilent, final CommandArguments args) {
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 Bestand weergeven

23
 package com.dmdirc.commandparser.commands.server;
23
 package com.dmdirc.commandparser.commands.server;
24
 
24
 
25
 import com.dmdirc.Server;
25
 import com.dmdirc.Server;
26
-import com.dmdirc.ServerState;
27
 import com.dmdirc.commandparser.CommandArguments;
26
 import com.dmdirc.commandparser.CommandArguments;
28
 import com.dmdirc.commandparser.CommandManager;
27
 import com.dmdirc.commandparser.CommandManager;
28
+import com.dmdirc.commandparser.commands.CommandOptions;
29
 import com.dmdirc.commandparser.commands.IntelligentCommand;
29
 import com.dmdirc.commandparser.commands.IntelligentCommand;
30
 import com.dmdirc.commandparser.commands.ServerCommand;
30
 import com.dmdirc.commandparser.commands.ServerCommand;
31
 import com.dmdirc.ui.input.AdditionalTabTargets;
31
 import com.dmdirc.ui.input.AdditionalTabTargets;
37
  * The back command allows the user to unset their away status.
37
  * The back command allows the user to unset their away status.
38
  * @author chris
38
  * @author chris
39
  */
39
  */
40
+@CommandOptions(allowOffline=false)
40
 public final class Back extends ServerCommand implements IntelligentCommand {
41
 public final class Back extends ServerCommand implements IntelligentCommand {
41
     
42
     
42
     /**
43
     /**
57
      */
58
      */
58
     @Override
59
     @Override
59
     public void execute(final InputWindow origin, final Server server,
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
         server.getParser().sendLine("AWAY");
62
         server.getParser().sendLine("AWAY");
67
     }
63
     }
68
     
64
     

+ 2
- 6
src/com/dmdirc/commandparser/commands/server/Ctcp.java Bestand weergeven

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

+ 7
- 3
src/com/dmdirc/commandparser/commands/server/Disconnect.java Bestand weergeven

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

+ 2
- 6
src/com/dmdirc/commandparser/commands/server/Message.java Bestand weergeven

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

+ 2
- 0
src/com/dmdirc/commandparser/commands/server/Nick.java Bestand weergeven

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

+ 2
- 5
src/com/dmdirc/commandparser/commands/server/Notice.java Bestand weergeven

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

+ 2
- 0
src/com/dmdirc/commandparser/commands/server/Raw.java Bestand weergeven

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

+ 2
- 0
src/com/dmdirc/commandparser/commands/server/RawServerCommand.java Bestand weergeven

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

+ 2
- 0
src/com/dmdirc/commandparser/commands/server/Umode.java Bestand weergeven

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

+ 22
- 0
src/com/dmdirc/commandparser/parsers/CommandParser.java Bestand weergeven

30
 import com.dmdirc.commandparser.CommandManager;
30
 import com.dmdirc.commandparser.CommandManager;
31
 import com.dmdirc.commandparser.CommandType;
31
 import com.dmdirc.commandparser.CommandType;
32
 import com.dmdirc.commandparser.commands.Command;
32
 import com.dmdirc.commandparser.commands.Command;
33
+import com.dmdirc.commandparser.commands.CommandOptions;
33
 import com.dmdirc.commandparser.commands.ExternalCommand;
34
 import com.dmdirc.commandparser.commands.ExternalCommand;
34
 import com.dmdirc.commandparser.commands.PreviousCommand;
35
 import com.dmdirc.commandparser.commands.PreviousCommand;
35
 import com.dmdirc.config.IdentityManager;
36
 import com.dmdirc.config.IdentityManager;
262
      */
263
      */
263
     protected abstract void handleNonCommand(final InputWindow origin,
264
     protected abstract void handleNonCommand(final InputWindow origin,
264
             final String line);
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 Bestand weergeven

23
 package com.dmdirc.commandparser.parsers;
23
 package com.dmdirc.commandparser.parsers;
24
 
24
 
25
 import com.dmdirc.Server;
25
 import com.dmdirc.Server;
26
+import com.dmdirc.ServerState;
26
 import com.dmdirc.commandparser.CommandArguments;
27
 import com.dmdirc.commandparser.CommandArguments;
27
 import com.dmdirc.commandparser.CommandManager;
28
 import com.dmdirc.commandparser.CommandManager;
28
 import com.dmdirc.commandparser.CommandType;
29
 import com.dmdirc.commandparser.CommandType;
70
     protected void executeCommand(final InputWindow origin,
71
     protected void executeCommand(final InputWindow origin,
71
             final boolean isSilent, final Command command, final CommandArguments args) {
72
             final boolean isSilent, final Command command, final CommandArguments args) {
72
         if (command instanceof ServerCommand) {
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
         } else {
84
         } else {
75
             ((GlobalCommand) command).execute(origin, isSilent, args);
85
             ((GlobalCommand) command).execute(origin, isSilent, args);
76
         }
86
         }

Laden…
Annuleren
Opslaan