From: David ‘Bombe’ Roden Date: Fri, 17 Oct 2014 07:56:10 +0000 (+0200) Subject: Only try to re-join banned channels once a day. X-Git-Url: https://git.pterodactylus.net/?p=xudocci.git;a=commitdiff_plain;h=b6c081057ba5fd76c5eed0ce76675a3170d80830 Only try to re-join banned channels once a day. --- diff --git a/src/main/java/net/pterodactylus/xdcc/core/ChannelBanManager.java b/src/main/java/net/pterodactylus/xdcc/core/ChannelBanManager.java new file mode 100644 index 0000000..6998b18 --- /dev/null +++ b/src/main/java/net/pterodactylus/xdcc/core/ChannelBanManager.java @@ -0,0 +1,35 @@ +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 David ‘Bombe’ Roden + */ +public class ChannelBanManager { + + private static final long BAN_TIME = HOURS.toMillis(24); + private final Map 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); + } + +} diff --git a/src/main/java/net/pterodactylus/xdcc/core/Core.java b/src/main/java/net/pterodactylus/xdcc/core/Core.java index 1ea95a9..5bcccf3 100644 --- a/src/main/java/net/pterodactylus/xdcc/core/Core.java +++ b/src/main/java/net/pterodactylus/xdcc/core/Core.java @@ -18,6 +18,9 @@ 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; @@ -104,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; @@ -369,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()) { @@ -534,6 +544,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())); } @@ -554,6 +565,13 @@ public class Core extends AbstractExecutionThreadService { } 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(),