--- /dev/null
+package net.pterodactylus.xdcc.core;
+
+import static java.lang.System.currentTimeMillis;
+import static java.util.concurrent.TimeUnit.HOURS;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import net.pterodactylus.xdcc.data.Channel;
+
+/**
+ * Manages banned channels and their re-join times.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ChannelBanManager {
+
+ private static final long BAN_TIME = HOURS.toMillis(24);
+ private final Map<Channel, Long> channelBanTimes = new HashMap<>();
+
+ public boolean isBanned(Channel channel) {
+ return channelBanTimes.containsKey(channel)
+ && ((currentTimeMillis() - channelBanTimes.get(channel))
+ < BAN_TIME);
+ }
+
+ public void ban(Channel channel) {
+ channelBanTimes.put(channel, currentTimeMillis());
+ }
+
+ public void unban(Channel channel) {
+ channelBanTimes.remove(channel);
+ }
+
+}
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;
/** The event bus. */
private final EventBus eventBus;
+ private final ChannelBanManager channelBanManager =
+ new ChannelBanManager();
/** The temporary directory to download files to. */
private final String temporaryDirectory;
continue;
}
+ /* are we banned from this channel? */
+ if (channelBanManager.isBanned(channel)) {
+ continue;
+ }
+
connectNetwork(channel.network());
Connection connection = networkConnections.get(channel.network());
if (connection.established()) {
return;
}
+ channelBanManager.unban(channel.get());
joinedChannels.add(channel.get());
logger.info(String.format("Joined Channel %s on %s.", channelJoined.channel(), network.get().name()));
}
}
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(),