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;
try {
/* some status variables. */
boolean connected = true;
- StringBuilder motd = new StringBuilder();
Set<Nickname> nicks = Sets.newHashSet();
/* server modes. */
Set<Character> channelTypes = Sets.newHashSet();
List<Handler> handlers = asList(
+ new MotdHandler(eventBus, this),
new ConnectionEstablishHandler(eventBus, this),
new ChannelNotJoinedHandler(eventBus, this)
);
}
}
- /* 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)));
--- /dev/null
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ * @see MotdReceived
+ */
+public class MotdHandler implements Handler {
+
+ private static final List<String> 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<String> 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);
+ }
+ }
+
+}