From 85ac475193f3f99d543acbcdb0f04bf3ad23369f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 19 Oct 2014 21:22:36 +0200 Subject: [PATCH] Move handling of the nicks sent on JOIN to its own handler. --- .../java/net/pterodactylus/irc/Connection.java | 17 +------ .../irc/connection/ChannelNickHandler.java | 55 ++++++++++++++++++++++ .../irc/connection/PrefixHandler.java | 9 ++++ 3 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 src/main/java/net/pterodactylus/irc/connection/ChannelNickHandler.java diff --git a/src/main/java/net/pterodactylus/irc/Connection.java b/src/main/java/net/pterodactylus/irc/Connection.java index 6bb747a..9d9ef72 100644 --- a/src/main/java/net/pterodactylus/irc/Connection.java +++ b/src/main/java/net/pterodactylus/irc/Connection.java @@ -32,12 +32,12 @@ import java.io.UnsupportedEncodingException; import java.net.Socket; import java.util.ArrayList; import java.util.List; -import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import javax.net.SocketFactory; +import net.pterodactylus.irc.connection.ChannelNickHandler; import net.pterodactylus.irc.connection.ChannelNotJoinedHandler; import net.pterodactylus.irc.connection.ConnectionEstablishHandler; import net.pterodactylus.irc.connection.CtcpHandler; @@ -47,7 +47,6 @@ import net.pterodactylus.irc.connection.MotdHandler; import net.pterodactylus.irc.connection.PrefixHandler; import net.pterodactylus.irc.event.ChannelJoined; import net.pterodactylus.irc.event.ChannelLeft; -import net.pterodactylus.irc.event.ChannelNicknames; import net.pterodactylus.irc.event.ChannelTopic; import net.pterodactylus.irc.event.ClientQuit; import net.pterodactylus.irc.event.ConnectionClosed; @@ -64,7 +63,6 @@ import net.pterodactylus.xdcc.util.io.BandwidthCountingInputStream; import net.pterodactylus.xdcc.util.io.BandwidthCountingOutputStream; import com.google.common.base.Optional; -import com.google.common.collect.Sets; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; import com.google.common.io.Closeables; @@ -364,12 +362,12 @@ public class Connection extends AbstractExecutionThreadService implements Servic try { /* some status variables. */ boolean connected = true; - Set nicks = Sets.newHashSet(); PrefixHandler prefixHandler = new PrefixHandler(); List handlers = asList( new MessageHandler(eventBus, this, prefixHandler), new CtcpHandler(eventBus, this), + new ChannelNickHandler(eventBus, this, prefixHandler), new MotdHandler(eventBus, this), new ChannelNotJoinedHandler(eventBus, this), new ConnectionEstablishHandler(eventBus, this), @@ -412,17 +410,6 @@ public class Connection extends AbstractExecutionThreadService implements Servic /* no topic is set. */ } else if (command.equals("332")) { eventBus.post(new ChannelTopic(this, parameters.get(1), parameters.get(2))); - } else if (command.equals("353")) { - for (String nickname : parameters.get(3).split(" ")) { - if (prefixHandler.isNickPrefixed(nickname)) { - nicks.add(new Nickname(nickname.substring(1), nickname.substring(0, 1))); - } else { - nicks.add(new Nickname(nickname, "")); - } - } - } else if (command.equals("366")) { - eventBus.post(new ChannelNicknames(this, parameters.get(1), nicks)); - nicks.clear(); } else if (command.equalsIgnoreCase("PART")) { eventBus.post(new ChannelLeft(this, parameters.get(0), reply.source().get(), getOptional(parameters, 1))); } else if (command.equalsIgnoreCase("QUIT")) { diff --git a/src/main/java/net/pterodactylus/irc/connection/ChannelNickHandler.java b/src/main/java/net/pterodactylus/irc/connection/ChannelNickHandler.java new file mode 100644 index 0000000..b2f19d1 --- /dev/null +++ b/src/main/java/net/pterodactylus/irc/connection/ChannelNickHandler.java @@ -0,0 +1,55 @@ +package net.pterodactylus.irc.connection; + +import static java.util.Arrays.asList; + +import java.util.ArrayList; +import java.util.List; + +import net.pterodactylus.irc.Connection; +import net.pterodactylus.irc.Nickname; +import net.pterodactylus.irc.Reply; +import net.pterodactylus.irc.event.ChannelNicknames; + +import com.google.common.eventbus.EventBus; + +/** + * Handles the list of nicknames that are sent on a {@code JOIN}. + * + * @author David ‘Bombe’ Roden + */ +public class ChannelNickHandler implements Handler { + + private static final List COMMANDS_TO_HANDLE = + asList("353", "366"); + private final EventBus eventBus; + private final Connection connection; + private final PrefixHandler prefixHandler; + private final List nicknames = new ArrayList<>(); + + public ChannelNickHandler(EventBus eventBus, Connection connection, + PrefixHandler prefixHandler) { + this.eventBus = eventBus; + this.connection = connection; + this.prefixHandler = prefixHandler; + } + + @Override + public boolean willHandle(Reply reply) { + return COMMANDS_TO_HANDLE.contains(reply.command()); + } + + @Override + public void handleReply(Reply reply) { + String command = reply.command(); + List parameters = reply.parameters(); + if (command.equals("353")) { + asList(parameters.get(3).split(" ")).stream().forEach( + (nick) -> nicknames.add(prefixHandler.getNickname(nick))); + } else if (command.equals("366")) { + eventBus.post(new ChannelNicknames(connection, parameters.get(1), + nicknames)); + nicknames.clear(); + } + } + +} diff --git a/src/main/java/net/pterodactylus/irc/connection/PrefixHandler.java b/src/main/java/net/pterodactylus/irc/connection/PrefixHandler.java index 51e0b86..fe4d1bb 100644 --- a/src/main/java/net/pterodactylus/irc/connection/PrefixHandler.java +++ b/src/main/java/net/pterodactylus/irc/connection/PrefixHandler.java @@ -11,6 +11,7 @@ import java.util.Map; import java.util.Set; import net.pterodactylus.irc.Connection; +import net.pterodactylus.irc.Nickname; import net.pterodactylus.irc.Reply; import com.google.common.eventbus.EventBus; @@ -56,6 +57,14 @@ public class PrefixHandler implements Handler { return nickPrefixes.containsKey(nick.substring(0, 1)); } + public Nickname getNickname(String nicknameWithOptionalPrefix) { + if (isNickPrefixed(nicknameWithOptionalPrefix)) { + return new Nickname(nicknameWithOptionalPrefix.substring(1), + nicknameWithOptionalPrefix.substring(0, 1)); + } + return new Nickname(nicknameWithOptionalPrefix, ""); + } + private void parseNickPrefixes(String parameter) { int openParen = parameter.indexOf('('); int closeParen = parameter.indexOf(')'); -- 2.7.4