Browse Source

Tidy up icky logic.

Change-Id: I2d7b5dcfe37d3648c3827cd305d0cc7ae4dacef9
Reviewed-on: http://gerrit.dmdirc.com/3975
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
pull/1/head
Chris Smith 9 years ago
parent
commit
020beee50b
1 changed files with 21 additions and 24 deletions
  1. 21
    24
      src/com/dmdirc/commandparser/auto/AutoCommandHandler.java

+ 21
- 24
src/com/dmdirc/commandparser/auto/AutoCommandHandler.java View File

@@ -30,6 +30,8 @@ import com.dmdirc.events.ClientOpenedEvent;
30 30
 import com.dmdirc.events.ServerConnectedEvent;
31 31
 import com.dmdirc.interfaces.CommandController;
32 32
 
33
+import com.google.common.base.Optional;
34
+
33 35
 import net.engio.mbassy.listener.Handler;
34 36
 
35 37
 /**
@@ -60,7 +62,7 @@ public class AutoCommandHandler {
60 62
      */
61 63
     @Handler
62 64
     public void checkAutoCommand(final ClientOpenedEvent event) {
63
-        if (!autoCommand.getServer().isPresent() && !autoCommand.getNetwork().isPresent()) {
65
+        if (isGlobalCommand()) {
64 66
             execute(globalWindow, globalCommandParser);
65 67
         }
66 68
     }
@@ -72,33 +74,28 @@ public class AutoCommandHandler {
72 74
      */
73 75
     @Handler
74 76
     public void checkAutoCommand(final ServerConnectedEvent event) {
75
-        if (!autoCommand.getServer().isPresent() && !autoCommand.getNetwork().isPresent()) {
76
-            // This is a global auto command, shouldn't be executed for servers.
77
-            return;
78
-        }
79
-
80
-        if (autoCommand.getProfile().isPresent() && !event.getConnection().getProfile().getName()
81
-                .equalsIgnoreCase(autoCommand.getProfile().get())) {
82
-            // There's a profile specified in the command that isn't matched
83
-            return;
84
-        }
85
-
86
-        if (autoCommand.getServer().isPresent() && !event.getConnection().getAddress()
87
-                .equalsIgnoreCase(autoCommand.getServer().get())) {
88
-            // There's a server specified in the command that isn't matched
89
-            return;
77
+        if (appliesToServer(event.getConnection().getNetwork(),
78
+                event.getConnection().getAddress(), event.getConnection().getProfile().getName())) {
79
+            final FrameContainer container = event.getConnection().getWindowModel();
80
+            final CommandParser parser = container.getCommandParser();
81
+            execute(container, parser);
90 82
         }
83
+    }
91 84
 
85
+    private boolean appliesToServer(final String network, final String server,
86
+            final String profile) {
87
+        return !isGlobalCommand()
88
+                && matchesIfPreset(autoCommand.getNetwork(), network)
89
+                && matchesIfPreset(autoCommand.getServer(), server)
90
+                && matchesIfPreset(autoCommand.getProfile(), profile);
91
+    }
92 92
 
93
-        if (autoCommand.getNetwork().isPresent() && !event.getConnection().getNetwork()
94
-                .equalsIgnoreCase(autoCommand.getNetwork().get())) {
95
-            // There's a network specified in the command that isn't matched
96
-            return;
97
-        }
93
+    private boolean isGlobalCommand() {
94
+        return !autoCommand.getServer().isPresent() && !autoCommand.getNetwork().isPresent();
95
+    }
98 96
 
99
-        final FrameContainer container = event.getConnection().getWindowModel();
100
-        final CommandParser parser = container.getCommandParser();
101
-        execute(container, parser);
97
+    private boolean matchesIfPreset(final Optional<String> target, final String value) {
98
+        return !target.isPresent() || target.get().equalsIgnoreCase(value);
102 99
     }
103 100
 
104 101
     private void execute(final FrameContainer origin, final CommandParser parser) {

Loading…
Cancel
Save