X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fxdcc%2Fcore%2FCore.java;h=4d1e1ac5aafa2f64c083fd325baa5158631fd5ab;hb=aa82fb99f45b296163464bff96bd130b7e02ce75;hp=d5a994702cff97393359de7c30964f6ee0c0eda5;hpb=5cc5fd2f2cc010e77448ca044c7679739add966f;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 d5a9947..4d1e1ac 100644 --- a/src/main/java/net/pterodactylus/xdcc/core/Core.java +++ b/src/main/java/net/pterodactylus/xdcc/core/Core.java @@ -34,6 +34,7 @@ import net.pterodactylus.irc.Connection; import net.pterodactylus.irc.ConnectionBuilder; 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.ConnectionEstablished; import net.pterodactylus.irc.event.DccDownloadFailed; @@ -47,6 +48,7 @@ import net.pterodactylus.xdcc.core.event.CoreStarted; import net.pterodactylus.xdcc.core.event.DownloadFailed; import net.pterodactylus.xdcc.core.event.DownloadFinished; import net.pterodactylus.xdcc.core.event.DownloadStarted; +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; @@ -302,6 +304,46 @@ public class Core extends AbstractIdleService { } /** + * Removes bots that leave a channel, or channels when it’s us that’s leaving. + * + * @param channelLeft + * The channel left event + */ + @Subscribe + public void channelLeft(ChannelLeft channelLeft) { + Optional network = getNetwork(channelLeft.connection()); + if (!network.isPresent()) { + return; + } + + Bot bot = networkBots.get(network.get(), channelLeft.client().nick().get()); + if (bot == null) { + /* maybe it was us? */ + if (channelLeft.connection().isSource(channelLeft.client())) { + Optional channel = getChannel(network.get(), channelLeft.channel()); + if (!channel.isPresent()) { + /* maybe it was an extra channel? */ + channel = getExtraChannel(network.get(), channelLeft.channel()); + if (!channel.isPresent()) { + /* okay, whatever. */ + return; + } + + extraChannels.remove(channel); + } else { + channels.remove(channel.get()); + } + + eventBus.post(new GenericMessage(String.format("Left Channel %s on %s.", channel.get().name(), channel.get().network().name()))); + } + + return; + } + + networkBots.remove(network.get(), channelLeft.client().nick().get()); + } + + /** * If a message on a channel is received, it is parsed for pack information * with is then added to a bot. * @@ -482,6 +524,25 @@ public class Core extends AbstractIdleService { } /** + * Returns the extra channel for the given network and name. + * + * @param network + * The network the channel is located on + * @param channelName + * The name of the channel + * @return The extra channel, or {@link Optional#absent()} if no extra channel + * matching the given network and name was found + */ + public Optional getExtraChannel(Network network, String channelName) { + for (Channel channel : extraChannels) { + if (channel.network().equals(network) && (channel.name().equalsIgnoreCase(channelName))) { + return Optional.of(channel); + } + } + return Optional.absent(); + } + + /** * Parses {@link Pack} information from the given message. * * @param message