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.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.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 SimpleCommandHandler(eventBus)
+ .addCommand("431",
+ (s, p) -> new NoNicknameGivenReceived(
+ this))
+ .addCommand("NICK",
+ (s, p) -> new NicknameChanged(this,
+ s.get(), p.get(0)))
+ .addCommand("JOIN",
+ (s, p) -> new ChannelJoined(this,
+ p.get(0), s.get()))
+ .addCommand("332",
+ (s, p) -> new ChannelTopic(this, p.get(1),
+ p.get(2)))
+ .addCommand("PART",
+ (s, p) -> new ChannelLeft(this, p.get(0),
+ s.get(), getOptional(p, 1)))
+ .addCommand("QUIT",
+ (s, p) -> new ClientQuit(this, s.get(),
+ p.get(0)))
+ .addCommand("KICK",
+ (s, p) -> new KickedFromChannel(this,
+ p.get(0), s.get(), p.get(1),
+ getOptional(p, 2))),
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, reply));
- } else if (command.equals("433")) {
+ if (command.equals("433")) {
if (!established.get()) {
nickname = nicknameChooser.getNickname();
connectionHandler.sendCommand("NICK", nickname);
eventBus.post(new NicknameInUseReceived(this, reply));
}
- /* client stuff. */
- } else if (command.equalsIgnoreCase("NICK")) {
- eventBus.post(new NicknameChanged(this, reply.source().get(), parameters.get(0)));
-
/* channel stuff. */
- } else if (command.equalsIgnoreCase("JOIN")) {
- eventBus.post(new ChannelJoined(this, parameters.get(0), reply.source().get()));
} else if (command.equals("331")) {
/* 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")) {
- eventBus.post(new ClientQuit(this, reply.source().get(), parameters.get(0)));
/* basic connection housekeeping. */
} else if (command.equalsIgnoreCase("PING")) {
connectionHandler.sendCommand("PONG", getOptional(parameters, 0), getOptional(parameters, 1));
- } else if (command.equalsIgnoreCase("KICK")) {
- eventBus.post(new KickedFromChannel(this, parameters.get(0), reply.source().get(), parameters.get(1), getOptional(parameters, 2)));
-
/* okay, everything else. */
} else {
eventBus.post(new UnknownReplyReceived(this, reply));