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;
Set<Character> channelTypes = Sets.newHashSet();
ConnectionEstablishHandler connectionEstablishHandler = new ConnectionEstablishHandler(eventBus, this);
+ ChannelNotJoinedHandler channelNotJoinedHandler = new ChannelNotJoinedHandler(eventBus, this);
while (connected) {
Reply reply = connectionHandler.readReply();
} 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")) {
--- /dev/null
+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);
+ }
+
+}