+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);
+ }
+ }
+
+}