+ logger.debug(String.format("Bot %s now has %d packs.", bot, bot.packs().size()));
+ }
+
+ /**
+ * Forward all private messages to every console.
+ *
+ * @param privateMessageReceived
+ * The private message recevied event
+ */
+ @Subscribe
+ public void privateMessageReceived(PrivateMessageReceived privateMessageReceived) {
+ eventBus.post(new MessageReceived(privateMessageReceived.source(), privateMessageReceived.message()));
+ }
+
+ /**
+ * Sends a message to all console when a notice was received.
+ *
+ * @param privateNoticeReceived
+ * The notice received event
+ */
+ @Subscribe
+ public void privateNoticeReceived(PrivateNoticeReceived privateNoticeReceived) {
+ Optional<Network> network = getNetwork(privateNoticeReceived.connection());
+ if (!network.isPresent()) {
+ return;
+ }
+
+ eventBus.post(new GenericMessage(String.format("Notice from %s (%s): %s", privateNoticeReceived.source(), network.get(), privateNoticeReceived.text())));
+ }
+
+ /**
+ * Starts a DCC download.
+ *
+ * @param dccSendReceived
+ * The DCC SEND event
+ */
+ @Subscribe
+ public void dccSendReceived(final DccSendReceived dccSendReceived) {
+ final Optional<Network> network = getNetwork(dccSendReceived.connection());
+ if (!network.isPresent()) {
+ return;
+ }
+
+ Collection<Download> packDownloads = downloads.get(dccSendReceived.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.", dccSendReceived.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(dccSendReceived.source().nick().get());
+ }
+ }).toSet();
+
+ /* we did not request this download. */
+ if (requestedDownload.isEmpty()) {
+ return;
+ }
+
+ Download download = requestedDownload.iterator().next();
+
+ /* check if the file already exists. */
+ File outputFile = new File(temporaryDirectory, dccSendReceived.filename());
+ if (outputFile.exists()) {
+ long existingFileSize = outputFile.length();
+
+ /* file already complete? */
+ if ((dccSendReceived.filesize() > -1) && (existingFileSize >= dccSendReceived.filesize())) {
+ /* file is apparently already complete. just move it. */
+ if (outputFile.renameTo(new File(finalDirectory, download.pack().name()))) {
+ eventBus.post(new GenericMessage(String.format("File %s already downloaded.", download.pack().name())));
+ } else {
+ eventBus.post(new GenericMessage(String.format("File %s already downloaded but not moved to %s.", download.pack().name(), finalDirectory)));
+ }
+
+ /* remove download. */
+ downloads.removeAll(download.pack().name());
+ return;
+ }
+
+ /* file not complete yet, DCC resume it. */
+ try {
+ download.remoteAddress(dccSendReceived.inetAddress()).filesize(dccSendReceived.filesize());
+ dccSendReceived.connection().sendDccResume(dccSendReceived.source().nick().get(), dccSendReceived.filename(), dccSendReceived.port(), existingFileSize);
+ } catch (IOException ioe1) {
+ eventBus.post(new GenericError(String.format("Could not send DCC RESUME %s to %s (%s).", dccSendReceived.filename(), dccSendReceived.source().nick().get(), ioe1.getMessage())));
+ }
+
+ return;
+ }
+
+ /* file does not exist, start the download. */
+ try {
+ OutputStream fileOutputStream = new FileOutputStream(outputFile);
+ DccReceiver dccReceiver = new DccReceiver(eventBus, dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream);
+ download.filename(outputFile.getPath()).outputStream(fileOutputStream).dccReceiver(dccReceiver);
+ dccReceivers.add(dccReceiver);
+ dccReceiver.start();
+ eventBus.post(new DownloadStarted(download));
+ } catch (FileNotFoundException fnfe1) {
+ eventBus.post(new GenericError(String.format("Could not start download of %s from %s (%s).", dccSendReceived.filename(), dccSendReceived.source().nick().get(), fnfe1.getMessage())));
+ }
+ }
+
+ @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.warn(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.warn("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.warn(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.warn("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.trace(String.format("%s: %s", replyReceived.connection().hostname(), replyReceived.reply()));