Use a handler for the MOTD.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 16 Oct 2014 19:55:56 +0000 (21:55 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 16 Oct 2014 19:55:56 +0000 (21:55 +0200)
src/main/java/net/pterodactylus/irc/Connection.java
src/main/java/net/pterodactylus/irc/connection/MotdHandler.java [new file with mode: 0644]

index 4d601fd..c47144b 100644 (file)
@@ -44,6 +44,7 @@ import javax.net.SocketFactory;
 import net.pterodactylus.irc.connection.ChannelNotJoinedHandler;
 import net.pterodactylus.irc.connection.ConnectionEstablishHandler;
 import net.pterodactylus.irc.connection.Handler;
+import net.pterodactylus.irc.connection.MotdHandler;
 import net.pterodactylus.irc.event.ChannelJoined;
 import net.pterodactylus.irc.event.ChannelLeft;
 import net.pterodactylus.irc.event.ChannelMessageReceived;
@@ -375,7 +376,6 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                try {
                        /* some status variables. */
                        boolean connected = true;
-                       StringBuilder motd = new StringBuilder();
                        Set<Nickname> nicks = Sets.newHashSet();
 
                        /* server modes. */
@@ -383,6 +383,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                        Set<Character> channelTypes = Sets.newHashSet();
 
                        List<Handler> handlers = asList(
+                                       new MotdHandler(eventBus, this),
                                        new ConnectionEstablishHandler(eventBus, this),
                                        new ChannelNotJoinedHandler(eventBus, this)
                        );
@@ -490,17 +491,6 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                                }
                                        }
 
-                               /* 375, 372, and 376 handle the server’s MOTD. */
-                               } else if (command.equals("375")) {
-                                       /* MOTD starts. */
-                                       motd.append(parameters.get(1)).append('\n');
-                               } else if (command.equals("372")) {
-                                       motd.append(parameters.get(1)).append('\n');
-                               } else if (command.equals("376")) {
-                                       motd.append(parameters.get(1)).append('\n');
-                                       eventBus.post(new MotdReceived(this, motd.toString()));
-                                       motd.setLength(0);
-
                                } else if (command.equalsIgnoreCase("KICK")) {
                                        eventBus.post(new KickedFromChannel(this, parameters.get(0), reply.source().get(), parameters.get(1), getOptional(parameters, 2)));
 
diff --git a/src/main/java/net/pterodactylus/irc/connection/MotdHandler.java b/src/main/java/net/pterodactylus/irc/connection/MotdHandler.java
new file mode 100644 (file)
index 0000000..35651e7
--- /dev/null
@@ -0,0 +1,54 @@
+package net.pterodactylus.irc.connection;
+
+import static java.util.Arrays.asList;
+
+import java.util.List;
+
+import net.pterodactylus.irc.Connection;
+import net.pterodactylus.irc.Reply;
+import net.pterodactylus.irc.event.MotdReceived;
+
+import com.google.common.eventbus.EventBus;
+
+/**
+ * Handles the MOTD sent by the server.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ * @see MotdReceived
+ */
+public class MotdHandler implements Handler {
+
+       private static final List<String> COMMANDS = asList("372", "375", "376");
+       private final EventBus eventBus;
+       private final Connection connection;
+       private final StringBuilder motd = new StringBuilder();
+
+       public MotdHandler(EventBus eventBus, Connection connection) {
+               this.eventBus = eventBus;
+               this.connection = connection;
+       }
+
+       @Override
+       public boolean willHandle(Reply reply) {
+               return COMMANDS.contains(reply.command());
+       }
+
+       @Override
+       public void handleReply(Reply reply) {
+               String command = reply.command();
+               List<String> parameters = reply.parameters();
+
+               /* 375, 372, and 376 handle the server’s MOTD. */
+               if (command.equals("375")) {
+                       /* MOTD starts. */
+                       motd.append(parameters.get(1)).append('\n');
+               } else if (command.equals("372")) {
+                       motd.append(parameters.get(1)).append('\n');
+               } else if (command.equals("376")) {
+                       motd.append(parameters.get(1)).append('\n');
+                       eventBus.post(new MotdReceived(connection, motd.toString()));
+                       motd.setLength(0);
+               }
+       }
+
+}