From 08f7863e3510aea16b6a856b40927c90687b067c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 16 Oct 2014 21:55:56 +0200 Subject: [PATCH] Use a handler for the MOTD. --- .../java/net/pterodactylus/irc/Connection.java | 14 +----- .../pterodactylus/irc/connection/MotdHandler.java | 54 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 src/main/java/net/pterodactylus/irc/connection/MotdHandler.java 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); + } + } + +} -- 2.7.4