Post messages when connecting to a network.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index 2da9c73..b02398d 100644 (file)
@@ -51,6 +51,7 @@ import net.pterodactylus.irc.event.DccSendReceived;
 import net.pterodactylus.irc.event.NicknameChanged;
 import net.pterodactylus.irc.event.PrivateMessageReceived;
 import net.pterodactylus.irc.event.PrivateNoticeReceived;
+import net.pterodactylus.irc.event.ReplyReceived;
 import net.pterodactylus.irc.util.MessageCleaner;
 import net.pterodactylus.irc.util.RandomNickname;
 import net.pterodactylus.xdcc.core.event.BotAdded;
@@ -260,6 +261,43 @@ public class Core extends AbstractExecutionThreadService {
        }
 
        /**
+        * Cancels the download of the given pack from the given bot.
+        *
+        * @param bot
+        *              The bot the pack is being downloaded from
+        * @param pack
+        *              The pack being downloaded
+        */
+       public void cancelDownload(Bot bot, Pack pack) {
+               Optional<Download> download = getDownload(pack, bot);
+               if (!download.isPresent()) {
+                       return;
+               }
+
+               /* get connection. */
+               Connection connection = networkConnections.get(bot.network());
+               if (connection == null) {
+                       /* request for unknown network? */
+                       return;
+               }
+
+               /* remove download. */
+               downloads.remove(pack.name(), download.get());
+
+               /* stop the DCC receiver. */
+               if (download.get().dccReceiver() != null) {
+                       download.get().dccReceiver().stop();
+               }
+
+               /* remove the request from the bot, too. */
+               try {
+                       connection.sendMessage(bot.name(), String.format("XDCC %s", (download.get().dccReceiver() != null) ? "CANCEL" : "REMOVE"));
+               } catch (IOException ioe1) {
+                       logger.log(Level.WARNING, String.format("Could not cancel DCC from %s (%s)!", bot.name(), bot.network().name()), ioe1);
+               }
+       }
+
+       /**
         * Closes the given connection.
         *
         * @param connection
@@ -307,7 +345,11 @@ public class Core extends AbstractExecutionThreadService {
                                Connection connection = networkConnections.get(channel.network());
                                if (connection.established()) {
                                        eventBus.post(new GenericMessage(String.format("Trying to join %s on %s.", channel.name(), channel.network().name())));
-                                       connection.joinChannel(channel.name());
+                                       try {
+                                               connection.joinChannel(channel.name());
+                                       } catch (IOException ioe1) {
+                                               eventBus.post(new GenericMessage(String.format("Could not join %s on %s.", channel.name(), channel.network().name())));
+                                       }
                                }
                        }
                }
@@ -401,13 +443,17 @@ public class Core extends AbstractExecutionThreadService {
 
                /* found network? */
                if (!network.isPresent()) {
+                       eventBus.post(new GenericMessage(String.format("Connected to unknown network: %s", connectionEstablished.connection().hostname())));
                        return;
                }
 
+               eventBus.post(new GenericMessage(String.format("Connected to network %s.", network.get().name())));
+
                /* join all channels on this network. */
                for (Channel channel : channels) {
                        if (channel.network().equals(network.get())) {
                                try {
+                                       eventBus.post(new GenericMessage(String.format("Trying to join %s on %s...", channel.name(), network.get().name())));
                                        connectionEstablished.connection().joinChannel(channel.name());
                                } catch (IOException ioe1) {
                                        logger.log(Level.WARNING, String.format("Could not join %s on %s!", channel.name(), network.get().name()), ioe1);
@@ -818,11 +864,38 @@ public class Core extends AbstractExecutionThreadService {
                }
        }
 
+       @Subscribe
+       public void replyReceived(ReplyReceived replyReceived) {
+               logger.log(Level.FINEST, String.format("%s: %s", replyReceived.connection().hostname(), replyReceived.reply()));
+       }
+
        //
        // PRIVATE METHODS
        //
 
        /**
+        * Returns the download of the given pack from the given bot.
+        *
+        * @param pack
+        *              The pack being downloaded
+        * @param bot
+        *              The bot the pack is being downloaded from
+        * @return The download, or {@link Optional#absent()} if the download could not
+        *         be found
+        */
+       private Optional<Download> getDownload(Pack pack, Bot bot) {
+               if (!downloads.containsKey(pack.name())) {
+                       return Optional.absent();
+               }
+               for (Download download : Lists.newArrayList(downloads.get(pack.name()))) {
+                       if (download.bot().equals(bot)) {
+                               return Optional.of(download);
+                       }
+               }
+               return Optional.absent();
+       }
+
+       /**
         * Searches all current connections for the given connection, returning the
         * associated network.
         *