1 package net.pterodactylus.irc.connection;
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;
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;
16 import com.google.common.eventbus.EventBus;
19 * Handles the reasons why a channel could not be joined.
21 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22 * @see ChannelNotJoined
24 public class ChannelNotJoinedHandler implements Handler {
26 private final EventBus eventBus;
27 private final Connection connection;
29 public ChannelNotJoinedHandler(EventBus eventBus, Connection connection) {
30 this.eventBus = eventBus;
31 this.connection = connection;
35 public boolean willHandle(Reply reply) {
36 return asList("473", "474", "475", "477").contains(reply.command());
40 public void handleReply(Reply reply) {
41 String command = reply.command();
42 List<String> parameters = reply.parameters();
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));
57 private ChannelNotJoined createEvent(String channel, Reason reason) {
58 return new ChannelNotJoined(connection, channel, reason);