Expose currently joined channels.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index 5df7f42..de4c842 100644 (file)
 
 package net.pterodactylus.xdcc.core;
 
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.OutputStream;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
@@ -28,8 +32,10 @@ import java.util.logging.Logger;
 
 import net.pterodactylus.irc.Connection;
 import net.pterodactylus.irc.ConnectionBuilder;
+import net.pterodactylus.irc.DccReceiver;
 import net.pterodactylus.irc.event.ChannelMessageReceived;
 import net.pterodactylus.irc.event.ConnectionEstablished;
+import net.pterodactylus.irc.event.DccSendReceived;
 import net.pterodactylus.irc.util.MessageCleaner;
 import net.pterodactylus.irc.util.RandomNickname;
 import net.pterodactylus.xdcc.core.event.BotAdded;
@@ -42,6 +48,7 @@ import net.pterodactylus.xdcc.data.Server;
 
 import com.google.common.base.Optional;
 import com.google.common.collect.HashBasedTable;
+import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
@@ -67,12 +74,18 @@ public class Core extends AbstractIdleService {
        /** The channels that should be monitored. */
        private final Collection<Channel> channels = Sets.newHashSet();
 
+       /** The channels that are currentlymonitored. */
+       private final Collection<Channel> joinedChannels = Sets.newHashSet();
+
        /** The current network connections. */
        private final Map<Network, Connection> networkConnections = Collections.synchronizedMap(Maps.<Network, Connection>newHashMap());
 
        /** The currently known bots. */
        private final Table<Network, String, Bot> networkBots = HashBasedTable.create();
 
+       /** The current DCC receivers. */
+       private final Collection<DccReceiver> dccReceivers = Sets.newHashSet();
+
        /**
         * Creates a new core.
         *
@@ -89,6 +102,25 @@ public class Core extends AbstractIdleService {
        //
 
        /**
+        * Returns all configured channels. Due to various circumstances, configured
+        * channels might not actually be joined.
+        *
+        * @return All configured channels
+        */
+       public Collection<Channel> channels() {
+               return ImmutableSet.copyOf(channels);
+       }
+
+       /**
+        * Returns all currently joined channels.
+        *
+        * @return All currently joined channels
+        */
+       public Collection<Channel> joinedChannels() {
+               return ImmutableSet.copyOf(joinedChannels);
+       }
+
+       /**
         * Returns all currently known bots.
         *
         * @return All currently known bots
@@ -97,6 +129,15 @@ public class Core extends AbstractIdleService {
                return networkBots.values();
        }
 
+       /**
+        * Returns the currently active DCC receivers.
+        *
+        * @return The currently active DCC receivers
+        */
+       public Collection<DccReceiver> dccReceivers() {
+               return dccReceivers;
+       }
+
        //
        // ACTIONS
        //
@@ -111,6 +152,27 @@ public class Core extends AbstractIdleService {
                channels.add(channel);
        }
 
+       /**
+        * Fetches the given pack from the given bot.
+        *
+        * @param bot
+        *              The bot to fetch the pack from
+        * @param pack
+        *              The pack to fetch
+        */
+       public void fetch(Bot bot, Pack pack) {
+               Connection connection = networkConnections.get(bot.network());
+               if (connection == null) {
+                       return;
+               }
+
+               try {
+                       connection.sendMessage(bot.name(), "XDCC SEND " + pack.id());
+               } catch (IOException ioe1) {
+                       logger.log(Level.WARNING, "Could not send message to bot!", ioe1);
+               }
+       }
+
        //
        // ABSTRACTIDLESERVICE METHODS
        //
@@ -215,6 +277,25 @@ public class Core extends AbstractIdleService {
                logger.fine(String.format("Bot %s now has %d packs.", bot, bot.packs().size()));
        }
 
+       /**
+        * Starts a DCC download.
+        *
+        * @param dccSendReceived
+        *              The DCC SEND event
+        */
+       @Subscribe
+       public void dccSendReceived(DccSendReceived dccSendReceived) {
+               logger.info(String.format("Starting download of %s.", dccSendReceived.filename()));
+               try {
+                       OutputStream fileOutputStream = new FileOutputStream(new File("/home/bombe/Temp", dccSendReceived.filename()));
+                       DccReceiver dccReceiver = new DccReceiver(dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream);
+                       dccReceivers.add(dccReceiver);
+                       dccReceiver.start();
+               } catch (FileNotFoundException fnfe1) {
+                       logger.log(Level.WARNING, "Could not open file for download!", fnfe1);
+               }
+       }
+
        //
        // PRIVATE METHODS
        //