import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import net.pterodactylus.irc.connection.ConnectionEstablishHandler;
import net.pterodactylus.irc.connection.Handler;
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.ChannelMessageReceived;
import net.pterodactylus.xdcc.util.io.BandwidthCountingOutputStream;
import com.google.common.base.Optional;
-import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
boolean connected = true;
Set<Nickname> nicks = Sets.newHashSet();
- /* server modes. */
- Map<String, String> nickPrefixes = Maps.newHashMap();
- Set<Character> channelTypes = Sets.newHashSet();
-
+ PrefixHandler prefixHandler = new PrefixHandler();
List<Handler> handlers = asList(
new MotdHandler(eventBus, this),
+ new ChannelNotJoinedHandler(eventBus, this),
new ConnectionEstablishHandler(eventBus, this),
- new ChannelNotJoinedHandler(eventBus, this)
+ prefixHandler
);
while (connected) {
if (message.startsWith("\u0001") && message.endsWith("\u0001")) {
/* CTCP! */
handleCtcp(reply.source().get(), message);
- } else if (!channelTypes.contains(recipient.charAt(0))) {
+ } else if (!prefixHandler.isChannel(recipient)) {
eventBus.post(new PrivateMessageReceived(this, reply.source().get(), message));
} else {
eventBus.post(new ChannelMessageReceived(this, recipient, reply.source().get(), message));
if (message.startsWith("\u0001") && message.endsWith("\u0001")) {
/* CTCP! */
handleCtcp(reply.source().get(), message);
- } else if (!channelTypes.contains(recipient.charAt(0))) {
+ } else if (!prefixHandler.isChannel(recipient)) {
eventBus.post(new PrivateNoticeReceived(this, reply));
} else {
eventBus.post(new ChannelNoticeReceived(this, reply.source().get(), recipient, message));
eventBus.post(new ChannelTopic(this, parameters.get(1), parameters.get(2)));
} else if (command.equals("353")) {
for (String nickname : parameters.get(3).split(" ")) {
- if (nickPrefixes.containsKey(nickname.substring(0, 1))) {
+ if (prefixHandler.isNickPrefixed(nickname)) {
nicks.add(new Nickname(nickname.substring(1), nickname.substring(0, 1)));
} else {
nicks.add(new Nickname(nickname, ""));
} else if (command.equalsIgnoreCase("PING")) {
connectionHandler.sendCommand("PONG", getOptional(parameters, 0), getOptional(parameters, 1));
- /* 005 originally was a bounce message, now used to transmit useful information about the server. */
- } else if (command.equals("005")) {
- for (String parameter : parameters) {
- if (parameter.startsWith("PREFIX=")) {
- int openParen = parameter.indexOf('(');
- int closeParen = parameter.indexOf(')');
- if ((openParen != -1) && (closeParen != -1)) {
- for (int modeCharacterIndex = 1; modeCharacterIndex < (closeParen - openParen); ++modeCharacterIndex) {
- char modeCharacter = parameter.charAt(openParen + modeCharacterIndex);
- char modeSymbol = parameter.charAt(closeParen + modeCharacterIndex);
- nickPrefixes.put(String.valueOf(modeSymbol), String.valueOf(modeCharacter));
- }
- logger.debug(String.format("Parsed Prefixes: %s", nickPrefixes));
- }
- } else if (parameter.startsWith("CHANTYPES=")) {
- for (int typeIndex = 10; typeIndex < parameter.length(); ++typeIndex) {
- channelTypes.add(parameter.charAt(typeIndex));
- }
- logger.debug(String.format("Parsed Channel Types: %s", channelTypes));
- }
- }
-
} else if (command.equalsIgnoreCase("KICK")) {
eventBus.post(new KickedFromChannel(this, parameters.get(0), reply.source().get(), parameters.get(1), getOptional(parameters, 2)));
--- /dev/null
+package net.pterodactylus.irc.connection;
+
+import static java.lang.String.format;
+import static java.lang.String.valueOf;
+import static org.apache.log4j.Logger.getLogger;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import net.pterodactylus.irc.Connection;
+import net.pterodactylus.irc.Reply;
+
+import com.google.common.eventbus.EventBus;
+import org.apache.log4j.Logger;
+
+/**
+ * Parses the server’s messages about which prefixes it understands for
+ * channels and nicknames.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class PrefixHandler implements Handler {
+
+ private static final Logger logger =
+ getLogger(PrefixHandler.class);
+
+ private final Map<String, String> nickPrefixes = new HashMap<>();
+ private final Set<Character> channelTypes = new HashSet<>();
+
+ @Override
+ public boolean willHandle(Reply reply) {
+ return "005".equals(reply.command());
+ }
+
+ @Override
+ public void handleReply(Reply reply) {
+ List<String> parameters = reply.parameters();
+
+ for (String parameter : parameters) {
+ if (parameter.startsWith("PREFIX=")) {
+ parseNickPrefixes(parameter);
+ } else if (parameter.startsWith("CHANTYPES=")) {
+ parseChannelTypes(parameter);
+ }
+ }
+ }
+
+ public boolean isChannel(String receiver) {
+ return channelTypes.contains(receiver.charAt(0));
+ }
+
+ public boolean isNickPrefixed(String nick) {
+ return nickPrefixes.containsKey(nick.substring(0, 1));
+ }
+
+ private void parseNickPrefixes(String parameter) {
+ int openParen = parameter.indexOf('(');
+ int closeParen = parameter.indexOf(')');
+ if ((openParen != -1) && (closeParen != -1)) {
+ for (int modeCharacterIndex = 1;
+ modeCharacterIndex < (closeParen - openParen);
+ ++modeCharacterIndex) {
+ char modeCharacter = parameter.charAt(
+ openParen + modeCharacterIndex);
+ char modeSymbol = parameter.charAt(
+ closeParen + modeCharacterIndex);
+ nickPrefixes.put(valueOf(modeSymbol),
+ valueOf(modeCharacter));
+ }
+ logger.debug(format("Parsed Prefixes: %s", nickPrefixes));
+ }
+ }
+
+ private void parseChannelTypes(String parameter) {
+ for (char channelType : parameter.substring(10).toCharArray()) {
+ channelTypes.add(channelType);
+ }
+ logger.debug(format("Parsed Channel Types: %s", channelTypes));
+ }
+
+}