Initiate the download when being offered a file.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 10 Apr 2013 04:52:00 +0000 (06:52 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 10 Apr 2013 04:52:00 +0000 (06:52 +0200)
src/main/java/net/pterodactylus/xdcc/core/Core.java

index ecf0946..e95cf0e 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;
@@ -31,6 +35,7 @@ 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;
@@ -249,6 +254,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
        //