From: David ‘Bombe’ Roden Date: Thu, 16 Oct 2014 19:55:56 +0000 (+0200) Subject: Use a handler for the MOTD. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=08f7863e3510aea16b6a856b40927c90687b067c;p=xudocci.git Use a handler for the MOTD. --- diff --git a/src/main/java/net/pterodactylus/irc/Connection.java b/src/main/java/net/pterodactylus/irc/Connection.java index 4d601fd..c47144b 100644 --- a/src/main/java/net/pterodactylus/irc/Connection.java +++ b/src/main/java/net/pterodactylus/irc/Connection.java @@ -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 nicks = Sets.newHashSet(); /* server modes. */ @@ -383,6 +383,7 @@ public class Connection extends AbstractExecutionThreadService implements Servic Set channelTypes = Sets.newHashSet(); List 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 index 0000000..35651e7 --- /dev/null +++ b/src/main/java/net/pterodactylus/irc/connection/MotdHandler.java @@ -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 David ‘Bombe’ Roden + * @see MotdReceived + */ +public class MotdHandler implements Handler { + + private static final List 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 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); + } + } + +}