🚧 Parse token from DCC SEND
[xudocci.git] / irc / connection / MotdHandler.java
1 package net.pterodactylus.irc.connection;
2
3 import static java.util.Arrays.asList;
4
5 import java.util.List;
6
7 import net.pterodactylus.irc.Connection;
8 import net.pterodactylus.irc.Reply;
9 import net.pterodactylus.irc.event.MotdReceived;
10
11 import com.google.common.eventbus.EventBus;
12
13 /**
14  * Handles the MOTD sent by the server.
15  *
16  * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
17  * @see MotdReceived
18  */
19 public class MotdHandler implements Handler {
20
21         private static final List<String> COMMANDS = asList("372", "375", "376");
22         private final EventBus eventBus;
23         private final Connection connection;
24         private final StringBuilder motd = new StringBuilder();
25
26         public MotdHandler(EventBus eventBus, Connection connection) {
27                 this.eventBus = eventBus;
28                 this.connection = connection;
29         }
30
31         @Override
32         public boolean willHandle(Reply reply) {
33                 return COMMANDS.contains(reply.command());
34         }
35
36         @Override
37         public void handleReply(Reply reply) {
38                 String command = reply.command();
39                 List<String> parameters = reply.parameters();
40
41                 /* 375, 372, and 376 handle the server’s MOTD. */
42                 if (command.equals("375")) {
43                         /* MOTD starts. */
44                         motd.append(parameters.get(1)).append('\n');
45                 } else if (command.equals("372")) {
46                         motd.append(parameters.get(1)).append('\n');
47                 } else if (command.equals("376")) {
48                         motd.append(parameters.get(1)).append('\n');
49                         eventBus.post(new MotdReceived(connection, motd.toString()));
50                         motd.setLength(0);
51                 }
52         }
53
54 }