Don’t log disconnects from networks we weren’t really connected to.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index 509d30e..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;
@@ -103,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;
@@ -368,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()) {
@@ -424,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)) {
@@ -533,6 +547,7 @@ 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()));
                }
@@ -545,9 +560,29 @@ public class Core extends AbstractExecutionThreadService {
                        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: %s", channelNotJoined.channel(),
-                                               network.get(), channelNotJoined.reason())));
+                               format("Could not join %s: %s", channelNotJoined.channel(),
+                                               channelNotJoined.reason())));
        }
 
        /**
@@ -613,8 +648,8 @@ public class Core extends AbstractExecutionThreadService {
                                channels.remove(channel.get());
                        }
                        eventBus.post(new GenericMessage(format(
-                                       "Kicked from %s/%s by %s: %s",
-                                       kickedFromChannel.channel(), network.get(),
+                                       "Kicked from %s by %s: %s",
+                                       kickedFromChannel.channel(),
                                        kickedFromChannel.kicker(),
                                        kickedFromChannel.reason().or("<unknown>")
                        )));