X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fxdcc%2Fcore%2FCore.java;h=272076c4bddd15525e67f7f0530247592448064e;hb=67092b8d7109812ba079965503bb61b11f5655c6;hp=8d983d59d17a13b7870d24084d1f67daaa2f5173;hpb=0e84cf6dbee5bc509d5e3d64c3347a3046810d9d;p=xudocci.git diff --git a/src/main/java/net/pterodactylus/xdcc/core/Core.java b/src/main/java/net/pterodactylus/xdcc/core/Core.java index 8d983d5..272076c 100644 --- a/src/main/java/net/pterodactylus/xdcc/core/Core.java +++ b/src/main/java/net/pterodactylus/xdcc/core/Core.java @@ -18,6 +18,8 @@ package net.pterodactylus.xdcc.core; import static java.lang.String.format; +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; @@ -34,6 +36,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import net.pterodactylus.irc.Connection; import net.pterodactylus.irc.ConnectionBuilder; @@ -66,12 +69,12 @@ import net.pterodactylus.xdcc.core.event.GenericMessage; import net.pterodactylus.xdcc.core.event.MessageReceived; import net.pterodactylus.xdcc.data.Bot; import net.pterodactylus.xdcc.data.Channel; +import net.pterodactylus.xdcc.data.ConnectedNetwork; import net.pterodactylus.xdcc.data.Download; import net.pterodactylus.xdcc.data.Network; import net.pterodactylus.xdcc.data.Pack; import net.pterodactylus.xdcc.data.Server; -import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.collect.FluentIterable; @@ -103,6 +106,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; @@ -175,13 +180,25 @@ public class Core extends AbstractExecutionThreadService { * * @return All connected networks */ - public Collection connectedNetworks() { - return Lists.newArrayList(Optional.presentInstances(FluentIterable.from(networkConnections.values()).transform(new Function>() { - @Override - public Optional apply(Connection connection) { - return getNetwork(connection); - } - }))); + public Collection connectedNetworks() { + return networkConnections.entrySet().stream().map((entry) -> { + Network network = entry.getKey(); + Collection bots = networkBots.row(network).values(); + int packCount = bots.stream().mapToInt((bot) -> bot.packs().size()).reduce((a, b) -> a + b).getAsInt(); + return new ConnectedNetwork(network, entry.getValue().hostname(), + entry.getValue().port(), entry.getValue().nickname(), + channels.stream() + .filter((channel) -> channel.network() + .equals(network)) + .map(Channel::name) + .collect(Collectors.toList()), + extraChannels.stream() + .filter((channel) -> channel.network() + .equals(network)) + .map(Channel::name) + .collect(Collectors.toList()), + bots.size(), packCount); + }).collect(Collectors.toList()); } /** @@ -368,6 +385,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 +446,9 @@ public class Core extends AbstractExecutionThreadService { if (!network.isPresent()) { return; } + if (!connection.established()) { + return; + } /* find all channels that need to be removed. */ for (Collection channels : ImmutableList.of(joinedChannels, extraChannels)) { @@ -533,6 +558,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 +571,29 @@ public class Core extends AbstractExecutionThreadService { return; } + Optional 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 a 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 +659,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("") ))); @@ -730,7 +776,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()))); } /**