Remove bots when they leave the channel; handle our own leaving of channels.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2013 20:49:21 +0000 (22:49 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 11 Apr 2013 20:50:05 +0000 (22:50 +0200)
src/main/java/net/pterodactylus/xdcc/core/Core.java
src/main/java/net/pterodactylus/xdcc/core/event/GenericMessage.java [new file with mode: 0644]

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
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 (file)
index 0000000..6efb446
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.xdcc.core.event;
+
+/**
+ * Generic message for all consoles.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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;
+       }
+
+}