+ @Subscribe
+ public void dccAcceptReceived(final DccAcceptReceived dccAcceptReceived) {
+ final Optional<Network> network = getNetwork(dccAcceptReceived.connection());
+ if (!network.isPresent()) {
+ return;
+ }
+
+ Collection<Download> packDownloads = downloads.get(dccAcceptReceived.filename());
+ if (packDownloads.isEmpty()) {
+ /* unknown download, ignore. */
+ return;
+ }
+
+ /* check if it’s already downloading. */
+ Collection<Download> runningDownloads = FluentIterable.from(packDownloads).filter(FILTER_RUNNING).toSet();
+ if (!runningDownloads.isEmpty()) {
+ eventBus.post(new GenericMessage(String.format("Ignoring offer for %s, it’s already being downloaded.", dccAcceptReceived.filename())));
+ return;
+ }
+
+ /* locate the correct download. */
+ Collection<Download> requestedDownload = FluentIterable.from(packDownloads).filter(new Predicate<Download>() {
+
+ @Override
+ public boolean apply(Download download) {
+ return download.bot().network().equals(network.get()) && download.bot().name().equalsIgnoreCase(dccAcceptReceived.source().nick().get());
+ }
+ }).toSet();
+
+ /* we did not request this download. */
+ if (requestedDownload.isEmpty()) {
+ return;
+ }
+
+ Download download = requestedDownload.iterator().next();
+
+ try {
+ File outputFile = new File(temporaryDirectory, dccAcceptReceived.filename());
+ if (outputFile.length() != dccAcceptReceived.position()) {
+ eventBus.post(new GenericError(String.format("Download %s from %s does not start at the right position!")));
+ logger.log(Level.WARNING, String.format("Download %s from %s: have %d bytes but wants to resume from %d!", dccAcceptReceived.filename(), dccAcceptReceived.source(), outputFile.length(), dccAcceptReceived.position()));
+
+ downloads.removeAll(download.pack().name());
+ return;
+ }
+ OutputStream outputStream = new FileOutputStream(outputFile, true);
+ DccReceiver dccReceiver = new DccReceiver(eventBus, download.remoteAddress(), dccAcceptReceived.port(), dccAcceptReceived.filename(), dccAcceptReceived.position(), download.filesize(), outputStream);
+ download.filename(outputFile.getPath()).outputStream(outputStream).dccReceiver(dccReceiver);
+ dccReceivers.add(dccReceiver);
+ dccReceiver.start();
+ eventBus.post(new DownloadStarted(download));
+ } catch (FileNotFoundException fnfe1) {
+ }
+ }
+
+ /**
+ * Closes the output stream of the download and moves the file to the final
+ * location.
+ *
+ * @param dccDownloadFinished
+ * The DCC download finished event
+ */
+ @Subscribe
+ public void dccDownloadFinished(DccDownloadFinished dccDownloadFinished) {
+
+ /* locate the correct download. */
+ Collection<Download> requestedDownload = FluentIterable.from(downloads.get(dccDownloadFinished.dccReceiver().filename())).filter(FILTER_RUNNING).toSet();
+ if (requestedDownload.isEmpty()) {
+ /* this seems wrong. */
+ logger.warning("Download finished but could not be located.");
+ return;
+ }
+ Download download = requestedDownload.iterator().next();
+
+ try {
+ download.outputStream().close();
+ File file = new File(download.filename());
+ file.renameTo(new File(finalDirectory, download.pack().name()));
+ eventBus.post(new DownloadFinished(download));
+ dccReceivers.remove(dccDownloadFinished.dccReceiver());
+ downloads.removeAll(download.pack().name());
+ } catch (IOException ioe1) {
+ /* TODO - handle all the errors. */
+ logger.log(Level.WARNING, String.format("Could not move file %s to directory %s.", download.filename(), finalDirectory), ioe1);
+ }
+ }
+
+ /**
+ * Closes the output stream and notifies all listeners of the failure.
+ *
+ * @param dccDownloadFailed
+ * The DCC download failed event
+ */
+ @Subscribe
+ public void dccDownloadFailed(DccDownloadFailed dccDownloadFailed) {
+
+ /* locate the correct download. */
+ Collection<Download> requestedDownload = FluentIterable.from(downloads.get(dccDownloadFailed.dccReceiver().filename())).filter(FILTER_RUNNING).toSet();
+ if (requestedDownload.isEmpty()) {
+ /* this seems wrong. */
+ logger.warning("Download finished but could not be located.");
+ return;
+ }
+ Download download = requestedDownload.iterator().next();
+
+ try {
+ Closeables.close(download.outputStream(), true);
+ eventBus.post(new DownloadFailed(download));
+ dccReceivers.remove(dccDownloadFailed.dccReceiver());
+ downloads.removeAll(download.pack().name());
+ } catch (IOException ioe1) {
+ /* swallow silently. */
+ }
+ }
+
+ @Subscribe
+ public void replyReceived(ReplyReceived replyReceived) {
+ logger.log(Level.FINEST, String.format("%s: %s", replyReceived.connection().hostname(), replyReceived.reply()));
+ }
+