import net.pterodactylus.irc.connection.MessageHandler;
import net.pterodactylus.irc.connection.MotdHandler;
import net.pterodactylus.irc.connection.PrefixHandler;
+import net.pterodactylus.irc.connection.SimpleCommandHandler;
import net.pterodactylus.irc.event.ChannelJoined;
import net.pterodactylus.irc.event.ChannelLeft;
import net.pterodactylus.irc.event.ChannelTopic;
new MessageHandler(eventBus, this, prefixHandler),
new CtcpHandler(eventBus, this),
new ChannelNickHandler(eventBus, this, prefixHandler),
+ new SimpleCommandHandler()
+ .addCommand("431", (parameters) -> eventBus.post(
+ new NoNicknameGivenReceived(this))),
new MotdHandler(eventBus, this),
new ChannelNotJoinedHandler(eventBus, this),
new ConnectionEstablishHandler(eventBus, this),
}
/* 43x replies are for nick change errors. */
- if (command.equals("431")) {
- eventBus.post(new NoNicknameGivenReceived(this));
- } else if (command.equals("433")) {
+ if (command.equals("433")) {
if (!established.get()) {
nickname = nicknameChooser.getNickname();
connectionHandler.sendCommand("NICK", nickname);
--- /dev/null
+package net.pterodactylus.irc.connection;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import net.pterodactylus.irc.Reply;
+
+/**
+ * Handler that can process any number of events.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class SimpleCommandHandler implements Handler {
+
+ private final Map<String, EventProcessor> commandEventSenders =
+ new HashMap<>();
+
+ public SimpleCommandHandler addCommand(String command,
+ EventProcessor eventProcessor) {
+ commandEventSenders.put(command, eventProcessor);
+ return this;
+ }
+
+ @Override
+ public boolean willHandle(Reply reply) {
+ return commandEventSenders.containsKey(reply.command());
+ }
+
+ @Override
+ public void handleReply(Reply reply) {
+ EventProcessor eventProcessor =
+ commandEventSenders.get(reply.command());
+ eventProcessor.processEvent(reply.parameters());
+ }
+
+ /**
+ * Interface for event processors.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+ @FunctionalInterface
+ public static interface EventProcessor {
+
+ void processEvent(List<String> parameters);
+
+ }
+
+}