🚧 Parse token from DCC SEND
[xudocci.git] / src / main / java / net / pterodactylus / irc / connection / ChannelNotJoinedHandler.java
1 package net.pterodactylus.irc.connection;
2
3 import static java.util.Arrays.asList;
4 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.badChannelKey;
5 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.banned;
6 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.inviteOnly;
7 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.registeredNicknamesOnly;
8
9 import java.util.List;
10
11 import net.pterodactylus.irc.Connection;
12 import net.pterodactylus.irc.Reply;
13 import net.pterodactylus.irc.event.ChannelNotJoined;
14 import net.pterodactylus.irc.event.ChannelNotJoined.Reason;
15
16 import com.google.common.eventbus.EventBus;
17
18 /**
19  * Handles the reasons why a channel could not be joined.
20  *
21  * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
22  * @see ChannelNotJoined
23  */
24 public class ChannelNotJoinedHandler implements Handler {
25
26         private final EventBus eventBus;
27         private final Connection connection;
28
29         public ChannelNotJoinedHandler(EventBus eventBus, Connection connection) {
30                 this.eventBus = eventBus;
31                 this.connection = connection;
32         }
33
34         @Override
35         public boolean willHandle(Reply reply) {
36                 return asList("473", "474", "475", "477").contains(reply.command());
37         }
38
39         @Override
40         public void handleReply(Reply reply) {
41                 String command = reply.command();
42                 List<String> parameters = reply.parameters();
43
44                 /* common channel join errors. */
45                 String channel = parameters.get(1);
46                 if (command.equals("474")) {
47                         eventBus.post(createEvent(channel, banned));
48                 } else if (command.equals("473")) {
49                         eventBus.post(createEvent(channel, inviteOnly));
50                 } else if (command.equals("475")) {
51                         eventBus.post(createEvent(channel, badChannelKey));
52                 } else if (command.equals("477")) {
53                         eventBus.post(createEvent(channel, registeredNicknamesOnly));
54                 }
55         }
56
57         private ChannelNotJoined createEvent(String channel, Reason reason) {
58                 return new ChannelNotJoined(connection, channel, reason);
59         }
60
61 }