Remove bots when they leave the channel; handle our own leaving of channels.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index d5a9947..4d1e1ac 100644 (file)
@@ -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> 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> 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<Channel> 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