From: David ‘Bombe’ Roden Date: Thu, 11 Apr 2013 20:49:21 +0000 (+0200) Subject: Remove bots when they leave the channel; handle our own leaving of channels. X-Git-Url: https://git.pterodactylus.net/?p=xudocci.git;a=commitdiff_plain;h=aa82fb99f45b296163464bff96bd130b7e02ce75 Remove bots when they leave the channel; handle our own leaving of channels. --- 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 diff --git a/src/main/java/net/pterodactylus/xdcc/core/event/GenericMessage.java b/src/main/java/net/pterodactylus/xdcc/core/event/GenericMessage.java new file mode 100644 index 0000000..6efb446 --- /dev/null +++ b/src/main/java/net/pterodactylus/xdcc/core/event/GenericMessage.java @@ -0,0 +1,53 @@ +/* + * XdccDownloader - Information.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.xdcc.core.event; + +/** + * Generic message for all consoles. + * + * @author David ‘Bombe’ Roden + */ +public class GenericMessage { + + /** The message. */ + private final String message; + + /** + * Creates a new generic message event. + * + * @param message + * The message + */ + public GenericMessage(String message) { + this.message = message; + } + + // + // ACCESSORS + // + + /** + * Returns the message. + * + * @return The message + */ + public String message() { + return message; + } + +}