Don’t log disconnects from networks we weren’t really connected to.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index eb0036c..c0a2288 100644 (file)
 
 package net.pterodactylus.xdcc.core;
 
+import static java.lang.String.format;
+import static java.lang.System.currentTimeMillis;
+import static java.util.concurrent.TimeUnit.HOURS;
+import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.banned;
+import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.registeredNicknamesOnly;
 import static net.pterodactylus.irc.util.MessageCleaner.getDefaultInstance;
 import static net.pterodactylus.xdcc.data.Channel.TO_NETWORK;
 import static net.pterodactylus.xdcc.data.Download.FILTER_RUNNING;
@@ -40,6 +45,7 @@ import net.pterodactylus.irc.DccReceiver;
 import net.pterodactylus.irc.event.ChannelJoined;
 import net.pterodactylus.irc.event.ChannelLeft;
 import net.pterodactylus.irc.event.ChannelMessageReceived;
+import net.pterodactylus.irc.event.ChannelNotJoined;
 import net.pterodactylus.irc.event.ClientQuit;
 import net.pterodactylus.irc.event.ConnectionClosed;
 import net.pterodactylus.irc.event.ConnectionEstablished;
@@ -48,6 +54,7 @@ import net.pterodactylus.irc.event.DccAcceptReceived;
 import net.pterodactylus.irc.event.DccDownloadFailed;
 import net.pterodactylus.irc.event.DccDownloadFinished;
 import net.pterodactylus.irc.event.DccSendReceived;
+import net.pterodactylus.irc.event.KickedFromChannel;
 import net.pterodactylus.irc.event.NicknameChanged;
 import net.pterodactylus.irc.event.PrivateMessageReceived;
 import net.pterodactylus.irc.event.PrivateNoticeReceived;
@@ -100,6 +107,8 @@ public class Core extends AbstractExecutionThreadService {
 
        /** The event bus. */
        private final EventBus eventBus;
+       private final ChannelBanManager channelBanManager =
+                       new ChannelBanManager();
 
        /** The temporary directory to download files to. */
        private final String temporaryDirectory;
@@ -365,6 +374,11 @@ public class Core extends AbstractExecutionThreadService {
                                        continue;
                                }
 
+                               /* are we banned from this channel? */
+                               if (channelBanManager.isBanned(channel)) {
+                                       continue;
+                               }
+
                                connectNetwork(channel.network());
                                Connection connection = networkConnections.get(channel.network());
                                if (connection.established()) {
@@ -421,6 +435,9 @@ public class Core extends AbstractExecutionThreadService {
                if (!network.isPresent()) {
                        return;
                }
+               if (!connection.established()) {
+                       return;
+               }
 
                /* find all channels that need to be removed. */
                for (Collection<Channel> channels : ImmutableList.of(joinedChannels, extraChannels)) {
@@ -530,11 +547,44 @@ public class Core extends AbstractExecutionThreadService {
                                return;
                        }
 
+                       channelBanManager.unban(channel.get());
                        joinedChannels.add(channel.get());
                        logger.info(String.format("Joined Channel %s on %s.", channelJoined.channel(), network.get().name()));
                }
        }
 
+       @Subscribe
+       public void channelNotJoined(ChannelNotJoined channelNotJoined) {
+               Optional<Network> network = getNetwork(channelNotJoined.connection());
+               if (!network.isPresent()) {
+                       return;
+               }
+
+               Optional<Channel> channel = getChannel(network.get(), channelNotJoined.channel());
+               if (!channel.isPresent()) {
+                       eventBus.post(new GenericMessage(format("Could not join %s but didn’t try to join, either.", channel.get())));
+                       return;
+               }
+
+               if (channelNotJoined.reason() == registeredNicknamesOnly) {
+                       channels.remove(channel.get());
+                       eventBus.post(new GenericMessage(
+                                       format("Not trying to join %s anymore.", channel.get())));
+                       return;
+               }
+               if (channelNotJoined.reason() == banned) {
+                       channelBanManager.ban(channel.get());
+                       eventBus.post(new GenericMessage(
+                                       format("Banned from %s, suspending join for  day.",
+                                                       channel.get())));
+                       return;
+               }
+
+               eventBus.post(new GenericMessage(
+                               format("Could not join %s: %s", channelNotJoined.channel(),
+                                               channelNotJoined.reason())));
+       }
+
        /**
         * Removes bots that leave a channel, or channels when it’s us that’s leaving.
         *
@@ -575,6 +625,41 @@ public class Core extends AbstractExecutionThreadService {
                networkBots.remove(network.get(), channelLeft.client().nick().get());
        }
 
+       @Subscribe
+       public void kickedFromChannel(KickedFromChannel kickedFromChannel) {
+               Optional<Network> network = getNetwork(kickedFromChannel.connection());
+               if (!network.isPresent()) {
+                       return;
+               }
+
+               /* have we been kicked? */
+               if (nicknameMatchesConnection(kickedFromChannel.connection(), kickedFromChannel.kickee())) {
+                       Optional<Channel> channel = getChannel(network.get(), kickedFromChannel.channel());
+                       if (!channel.isPresent()) {
+                               /* maybe it was an extra channel? */
+                               channel = getExtraChannel(network.get(), kickedFromChannel.channel());
+                               if (!channel.isPresent()) {
+                                       /* okay, whatever. */
+                                       return;
+                               }
+
+                               extraChannels.remove(channel);
+                       } else {
+                               channels.remove(channel.get());
+                       }
+                       eventBus.post(new GenericMessage(format(
+                                       "Kicked from %s by %s: %s",
+                                       kickedFromChannel.channel(),
+                                       kickedFromChannel.kicker(),
+                                       kickedFromChannel.reason().or("<unknown>")
+                       )));
+               }
+       }
+
+       private boolean nicknameMatchesConnection(Connection connection, String nickname) {
+               return connection.nickname().equalsIgnoreCase(nickname);
+       }
+
        /**
         * Removes a client (which may be a bot) from the table of known bots.
         *
@@ -680,7 +765,7 @@ public class Core extends AbstractExecutionThreadService {
                        return;
                }
 
-               eventBus.post(new GenericMessage(String.format("Notice from %s (%s): %s", privateNoticeReceived.reply().source().get(), network.get(), privateNoticeReceived.text())));
+               eventBus.post(new GenericMessage(String.format("Notice from %s (%s): %s", privateNoticeReceived.source(), network.get(), privateNoticeReceived.text())));
        }
 
        /**