Add handler for when a channel can not be joined.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 16 Oct 2014 05:18:07 +0000 (07:18 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 16 Oct 2014 05:18:07 +0000 (07:18 +0200)
src/main/java/net/pterodactylus/irc/Connection.java
src/main/java/net/pterodactylus/irc/connection/ChannelNotJoinedHandler.java [new file with mode: 0644]

index cc1f9c8..6354c90 100644 (file)
@@ -40,6 +40,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 
 import javax.net.SocketFactory;
 
+import net.pterodactylus.irc.connection.ChannelNotJoinedHandler;
 import net.pterodactylus.irc.connection.ConnectionEstablishHandler;
 import net.pterodactylus.irc.event.ChannelJoined;
 import net.pterodactylus.irc.event.ChannelLeft;
@@ -382,6 +383,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                        Set<Character> channelTypes = Sets.newHashSet();
 
                        ConnectionEstablishHandler connectionEstablishHandler = new ConnectionEstablishHandler(eventBus, this);
+                       ChannelNotJoinedHandler channelNotJoinedHandler = new ChannelNotJoinedHandler(eventBus, this);
 
                        while (connected) {
                                Reply reply = connectionHandler.readReply();
@@ -453,15 +455,8 @@ public class Connection extends AbstractExecutionThreadService implements Servic
                                } else if (command.equalsIgnoreCase("QUIT")) {
                                        eventBus.post(new ClientQuit(this, reply.source().get(), parameters.get(0)));
 
-                               /* common channel join errors. */
-                               } else if (command.equals("474")) {
-                                       eventBus.post(new ChannelNotJoined(this, parameters.get(1), Reason.banned));
-                               } else if (command.equals("473")) {
-                                       eventBus.post(new ChannelNotJoined(this, parameters.get(1), Reason.inviteOnly));
-                               } else if (command.equals("475")) {
-                                       eventBus.post(new ChannelNotJoined(this, parameters.get(1), Reason.badChannelKey));
-                               } else if (command.equals("477")) {
-                                       eventBus.post(new ChannelNotJoined(this, parameters.get(1), Reason.registeredNicknamesOnly));
+                               } else if (channelNotJoinedHandler.willHandle(reply)) {
+                                       channelNotJoinedHandler.handleReply(reply);
 
                                /* basic connection housekeeping. */
                                } else if (command.equalsIgnoreCase("PING")) {
diff --git a/src/main/java/net/pterodactylus/irc/connection/ChannelNotJoinedHandler.java b/src/main/java/net/pterodactylus/irc/connection/ChannelNotJoinedHandler.java
new file mode 100644 (file)
index 0000000..8ead49d
--- /dev/null
@@ -0,0 +1,61 @@
+package net.pterodactylus.irc.connection;
+
+import static java.util.Arrays.asList;
+import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.badChannelKey;
+import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.banned;
+import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.inviteOnly;
+import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.registeredNicknamesOnly;
+
+import java.util.List;
+
+import net.pterodactylus.irc.Connection;
+import net.pterodactylus.irc.Reply;
+import net.pterodactylus.irc.event.ChannelNotJoined;
+import net.pterodactylus.irc.event.ChannelNotJoined.Reason;
+
+import com.google.common.eventbus.EventBus;
+
+/**
+ * Handles the reasons why a channel could not be joined.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ * @see ChannelNotJoined
+ */
+public class ChannelNotJoinedHandler implements Handler {
+
+       private final EventBus eventBus;
+       private final Connection connection;
+
+       public ChannelNotJoinedHandler(EventBus eventBus, Connection connection) {
+               this.eventBus = eventBus;
+               this.connection = connection;
+       }
+
+       @Override
+       public boolean willHandle(Reply reply) {
+               return asList("473", "474", "475", "477").contains(reply.command());
+       }
+
+       @Override
+       public void handleReply(Reply reply) {
+               String command = reply.command();
+               List<String> parameters = reply.parameters();
+
+               /* common channel join errors. */
+               String channel = parameters.get(1);
+               if (command.equals("474")) {
+                       eventBus.post(createEvent(channel, banned));
+               } else if (command.equals("473")) {
+                       eventBus.post(createEvent(channel, inviteOnly));
+               } else if (command.equals("475")) {
+                       eventBus.post(createEvent(channel, badChannelKey));
+               } else if (command.equals("477")) {
+                       eventBus.post(createEvent(channel, registeredNicknamesOnly));
+               }
+       }
+
+       private ChannelNotJoined createEvent(String channel, Reason reason) {
+               return new ChannelNotJoined(connection, channel, reason);
+       }
+
+}