Add command to initiate a download from a bot.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index d5a45b1..ecf0946 100644 (file)
@@ -28,21 +28,24 @@ 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.util.MessageCleaner;
 import net.pterodactylus.irc.util.RandomNickname;
+import net.pterodactylus.xdcc.core.event.BotAdded;
+import net.pterodactylus.xdcc.core.event.CoreStarted;
 import net.pterodactylus.xdcc.data.Bot;
 import net.pterodactylus.xdcc.data.Channel;
 import net.pterodactylus.xdcc.data.Network;
 import net.pterodactylus.xdcc.data.Pack;
 import net.pterodactylus.xdcc.data.Server;
 
-import com.beust.jcommander.internal.Maps;
-import com.beust.jcommander.internal.Sets;
 import com.google.common.base.Optional;
 import com.google.common.collect.HashBasedTable;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
 import com.google.common.collect.Table;
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
@@ -71,6 +74,9 @@ public class Core extends AbstractIdleService {
        /** 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.
         *
@@ -83,6 +89,28 @@ public class Core extends AbstractIdleService {
        }
 
        //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns all currently known bots.
+        *
+        * @return All currently known bots
+        */
+       public Collection<Bot> bots() {
+               return networkBots.values();
+       }
+
+       /**
+        * Returns the currently active DCC receivers.
+        *
+        * @return The currently active DCC receivers
+        */
+       public Collection<DccReceiver> dccReceivers() {
+               return dccReceivers;
+       }
+
+       //
        // ACTIONS
        //
 
@@ -96,6 +124,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
        //
@@ -114,6 +163,9 @@ public class Core extends AbstractIdleService {
                                connection.start();
                        }
                }
+
+               /* notify listeners. */
+               eventBus.post(new CoreStarted(this));
        }
 
        @Override
@@ -144,7 +196,7 @@ public class Core extends AbstractIdleService {
 
                /* join all channels on this network. */
                for (Channel channel : channels) {
-                       if (channel.network().equals(network)) {
+                       if (channel.network().equals(network.get())) {
                                try {
                                        connectionEstablished.connection().joinChannel(channel.name());
                                } catch (IOException ioe1) {
@@ -175,20 +227,23 @@ public class Core extends AbstractIdleService {
                        return;
                }
 
-               Bot bot;
-               synchronized (networkBots) {
-                       if (!networkBots.contains(network.get(), channelMessageReceived.source().nick().get())) {
-                               networkBots.put(network.get(), channelMessageReceived.source().nick().get(), new Bot(network.get()).name(channelMessageReceived.source().nick().get()));
-                       }
-                       bot = networkBots.get(network.get(), channelMessageReceived.source().nick().get());
-               }
-
                /* parse pack information. */
                Optional<Pack> pack = parsePack(message);
                if (!pack.isPresent()) {
                        return;
                }
 
+               Bot bot;
+               synchronized (networkBots) {
+                       if (!networkBots.contains(network.get(), channelMessageReceived.source().nick().get())) {
+                               bot = new Bot(network.get()).name(channelMessageReceived.source().nick().get());
+                               networkBots.put(network.get(), channelMessageReceived.source().nick().get(), bot);
+                               eventBus.post(new BotAdded(bot));
+                       } else {
+                               bot = networkBots.get(network.get(), channelMessageReceived.source().nick().get());
+                       }
+               }
+
                /* add pack. */
                bot.addPack(pack.get());
                logger.fine(String.format("Bot %s now has %d packs.", bot, bot.packs().size()));