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;
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;
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;
try {
/* some status variables. */
boolean connected = true;
- Set<Nickname> nicks = Sets.newHashSet();
PrefixHandler prefixHandler = new PrefixHandler();
List<Handler> 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),
/* 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")) {
--- /dev/null
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ChannelNickHandler implements Handler {
+
+ private static final List<String> COMMANDS_TO_HANDLE =
+ asList("353", "366");
+ private final EventBus eventBus;
+ private final Connection connection;
+ private final PrefixHandler prefixHandler;
+ private final List<Nickname> 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<String> 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();
+ }
+ }
+
+}
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;
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(')');