From: David ‘Bombe’ Roden Date: Thu, 16 Oct 2014 05:18:07 +0000 (+0200) Subject: Add handler for when a channel can not be joined. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=c4867f883cce2d99972bb14dd5937c6348ff29f5;p=xudocci.git Add handler for when a channel can not be joined. --- diff --git a/src/main/java/net/pterodactylus/irc/Connection.java b/src/main/java/net/pterodactylus/irc/Connection.java index cc1f9c8..6354c90 100644 --- a/src/main/java/net/pterodactylus/irc/Connection.java +++ b/src/main/java/net/pterodactylus/irc/Connection.java @@ -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 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 index 0000000..8ead49d --- /dev/null +++ b/src/main/java/net/pterodactylus/irc/connection/ChannelNotJoinedHandler.java @@ -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 David ‘Bombe’ Roden + * @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 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); + } + +}