Show whether a download has already started.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
1 /*
2  * XdccDownloader - Core.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.xdcc.core;
19
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import java.util.concurrent.TimeUnit;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34
35 import net.pterodactylus.irc.Connection;
36 import net.pterodactylus.irc.ConnectionBuilder;
37 import net.pterodactylus.irc.DccReceiver;
38 import net.pterodactylus.irc.event.ChannelJoined;
39 import net.pterodactylus.irc.event.ChannelLeft;
40 import net.pterodactylus.irc.event.ChannelMessageReceived;
41 import net.pterodactylus.irc.event.ClientQuit;
42 import net.pterodactylus.irc.event.ConnectionClosed;
43 import net.pterodactylus.irc.event.ConnectionEstablished;
44 import net.pterodactylus.irc.event.DccAcceptReceived;
45 import net.pterodactylus.irc.event.DccDownloadFailed;
46 import net.pterodactylus.irc.event.DccDownloadFinished;
47 import net.pterodactylus.irc.event.DccSendReceived;
48 import net.pterodactylus.irc.event.NicknameChanged;
49 import net.pterodactylus.irc.event.PrivateNoticeReceived;
50 import net.pterodactylus.irc.event.PrivateMessageReceived;
51 import net.pterodactylus.irc.util.MessageCleaner;
52 import net.pterodactylus.irc.util.RandomNickname;
53 import net.pterodactylus.xdcc.core.event.BotAdded;
54 import net.pterodactylus.xdcc.core.event.CoreStarted;
55 import net.pterodactylus.xdcc.core.event.DownloadFailed;
56 import net.pterodactylus.xdcc.core.event.DownloadFinished;
57 import net.pterodactylus.xdcc.core.event.DownloadStarted;
58 import net.pterodactylus.xdcc.core.event.GenericError;
59 import net.pterodactylus.xdcc.core.event.GenericMessage;
60 import net.pterodactylus.xdcc.core.event.MessageReceived;
61 import net.pterodactylus.xdcc.data.Bot;
62 import net.pterodactylus.xdcc.data.Channel;
63 import net.pterodactylus.xdcc.data.Download;
64 import net.pterodactylus.xdcc.data.Network;
65 import net.pterodactylus.xdcc.data.Pack;
66 import net.pterodactylus.xdcc.data.Server;
67
68 import com.google.common.base.Optional;
69 import com.google.common.collect.HashBasedTable;
70 import com.google.common.collect.ImmutableList;
71 import com.google.common.collect.ImmutableSet;
72 import com.google.common.collect.Lists;
73 import com.google.common.collect.Maps;
74 import com.google.common.collect.Sets;
75 import com.google.common.collect.Table;
76 import com.google.common.eventbus.EventBus;
77 import com.google.common.eventbus.Subscribe;
78 import com.google.common.io.Closeables;
79 import com.google.common.util.concurrent.AbstractExecutionThreadService;
80 import com.google.inject.Inject;
81
82 /**
83  * The core of XDCC Downloader.
84  *
85  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
86  */
87 public class Core extends AbstractExecutionThreadService {
88
89         /** The logger. */
90         private static final Logger logger = Logger.getLogger(Core.class.getName());
91
92         /** The event bus. */
93         private final EventBus eventBus;
94
95         /** The temporary directory to download files to. */
96         private final String temporaryDirectory;
97
98         /** The directory to move finished downloads to. */
99         private final String finalDirectory;
100
101         /** The channels that should be monitored. */
102         private final Collection<Channel> channels = Sets.newHashSet();
103
104         /** The channels that are currentlymonitored. */
105         private final Collection<Channel> joinedChannels = Sets.newHashSet();
106
107         /** The channels that are joined but not configured. */
108         private final Collection<Channel> extraChannels = Sets.newHashSet();
109
110         /** The current network connections. */
111         private final Map<Network, Connection> networkConnections = Collections.synchronizedMap(Maps.<Network, Connection>newHashMap());
112
113         /** The currently known bots. */
114         private final Table<Network, String, Bot> networkBots = HashBasedTable.create();
115
116         /** The current downloads. */
117         private final Map<String, Download> downloads = Maps.newHashMap();
118
119         /** The current DCC receivers. */
120         private final Collection<DccReceiver> dccReceivers = Lists.newArrayList();
121
122         /**
123          * Creates a new core.
124          *
125          * @param eventBus
126          *              The event bus
127          * @param temporaryDirectory
128          *              The directory to download files to
129          * @param finalDirectory
130          *              The directory to move finished files to
131          */
132         @Inject
133         public Core(EventBus eventBus, String temporaryDirectory, String finalDirectory) {
134                 this.eventBus = eventBus;
135                 this.temporaryDirectory = temporaryDirectory;
136                 this.finalDirectory = finalDirectory;
137         }
138
139         //
140         // ACCESSORS
141         //
142
143         /**
144          * Returns all configured channels. Due to various circumstances, configured
145          * channels might not actually be joined.
146          *
147          * @return All configured channels
148          */
149         public Collection<Channel> channels() {
150                 return ImmutableSet.copyOf(channels);
151         }
152
153         /**
154          * Returns all currently joined channels.
155          *
156          * @return All currently joined channels
157          */
158         public Collection<Channel> joinedChannels() {
159                 return ImmutableSet.copyOf(joinedChannels);
160         }
161
162         /**
163          * Returns all currently joined channels that are not configured.
164          *
165          * @return All currently joined but not configured channels
166          */
167         public Collection<Channel> extraChannels() {
168                 return ImmutableSet.copyOf(extraChannels);
169         }
170
171         /**
172          * Returns all currently known bots.
173          *
174          * @return All currently known bots
175          */
176         public Collection<Bot> bots() {
177                 return networkBots.values();
178         }
179
180         /**
181          * Returns all currently running downloads.
182          *
183          * @return All currently running downloads
184          */
185         public Collection<Download> downloads() {
186                 return downloads.values();
187         }
188
189         //
190         // ACTIONS
191         //
192
193         /**
194          * Adds a channel to monitor.
195          *
196          * @param channel
197          *              The channel to monitor
198          */
199         public void addChannel(Channel channel) {
200                 channels.add(channel);
201         }
202
203         /**
204          * Fetches the given pack from the given bot.
205          *
206          * @param bot
207          *              The bot to fetch the pack from
208          * @param pack
209          *              The pack to fetch
210          */
211         public void fetch(Bot bot, Pack pack) {
212                 Connection connection = networkConnections.get(bot.network());
213                 if (connection == null) {
214                         return;
215                 }
216
217                 Download download = new Download(bot, pack);
218                 downloads.put(pack.name(), download);
219
220                 try {
221                         connection.sendMessage(bot.name(), "XDCC SEND " + pack.id());
222                 } catch (IOException ioe1) {
223                         logger.log(Level.WARNING, "Could not send message to bot!", ioe1);
224                 }
225         }
226
227         //
228         // ABSTRACTIDLESERVICE METHODS
229         //
230
231         @Override
232         protected void startUp() {
233                 for (Channel channel : channels) {
234                         logger.info(String.format("Connecting to Channel %s on Network %s…", channel.name(), channel.network().name()));
235                         connectNetwork(channel.network());
236                 }
237
238                 /* notify listeners. */
239                 eventBus.post(new CoreStarted(this));
240         }
241
242         @Override
243         protected void run() throws Exception {
244                 while (isRunning()) {
245                         try {
246                                 Thread.sleep(TimeUnit.MINUTES.toMillis(1));
247                         } catch (InterruptedException ie1) {
248                                 /* ignore. */
249                         }
250
251                         /* find channels that should be monitored but are not. */
252                         for (Channel channel : channels) {
253                                 if (joinedChannels.contains(channel)) {
254                                         continue;
255                                 }
256
257                                 connectNetwork(channel.network());
258                                 Connection connection = networkConnections.get(channel.network());
259                                 if (connection.established()) {
260                                         eventBus.post(new GenericMessage(String.format("Trying to join %s on %s.", channel.name(), channel.network().name())));
261                                         connection.joinChannel(channel.name());
262                                 }
263                         }
264                 }
265         }
266
267         @Override
268         protected void shutDown() {
269         }
270
271         //
272         // PRIVATE METHODS
273         //
274
275         /**
276          * Starts a new connection for the given network if no such connection exists
277          * already.
278          *
279          * @param network
280          *              The network to connect to
281          */
282         private void connectNetwork(Network network) {
283                 if (!networkConnections.containsKey(network)) {
284                                 /* select a random server. */
285                         List<Server> servers = Lists.newArrayList(network.servers());
286                         if (servers.isEmpty()) {
287                                 eventBus.post(new GenericError(String.format("Network %s does not have any servers.", network.name())));
288                                 return;
289                         }
290                         Server server = servers.get((int) (Math.random() * servers.size()));
291                         Connection connection = new ConnectionBuilder(eventBus).connect(server.hostname()).port(server.unencryptedPorts().iterator().next()).build();
292                         connection.username(RandomNickname.get()).realName(RandomNickname.get());
293                         networkConnections.put(network, connection);
294                         connection.start();
295                 }
296         }
297
298         //
299         // EVENT HANDLERS
300         //
301
302         /**
303          * If a connection to a network has been established, the channels associated
304          * with this network are joined.
305          *
306          * @param connectionEstablished
307          *              The connection established event
308          */
309         @Subscribe
310         public void connectionEstablished(ConnectionEstablished connectionEstablished) {
311
312                 /* get network for connection. */
313                 Optional<Network> network = getNetwork(connectionEstablished.connection());
314
315                 /* found network? */
316                 if (!network.isPresent()) {
317                         return;
318                 }
319
320                 /* join all channels on this network. */
321                 for (Channel channel : channels) {
322                         if (channel.network().equals(network.get())) {
323                                 try {
324                                         connectionEstablished.connection().joinChannel(channel.name());
325                                 } catch (IOException ioe1) {
326                                         logger.log(Level.WARNING, String.format("Could not join %s on %s!", channel.name(), network.get().name()), ioe1);
327                                 }
328                         }
329                 }
330         }
331
332         /**
333          * Remove all data stored for a network if the connection is closed.
334          *
335          * @param connectionClosed
336          *              The connection closed event
337          */
338         @Subscribe
339         public void connectionClosed(ConnectionClosed connectionClosed) {
340                 Optional<Network> network = getNetwork(connectionClosed.connection());
341                 if (!network.isPresent()) {
342                         return;
343                 }
344
345                 /* find all channels that need to be removed. */
346                 for (Collection channels : ImmutableList.of(joinedChannels, extraChannels)) {
347                         for (Iterator<Channel> channelIterator = channels.iterator(); channelIterator.hasNext(); ) {
348                                 Channel joinedChannel = channelIterator.next();
349                                 if (!joinedChannel.network().equals(network.get())) {
350                                         continue;
351                                 }
352
353                                 channelIterator.remove();
354                         }
355                 }
356
357                 /* now remove all bots for that network. */
358                 Map<String, Bot> bots = networkBots.row(network.get());
359                 int botCount = bots.size();
360                 int packCount = 0;
361                 for (Bot bot : bots.values()) {
362                         packCount += bot.packs().size();
363                 }
364                 bots.clear();
365                 eventBus.post(new GenericMessage(String.format("Network %s disconnected, %d bots removed, %d packs removed.", network.get().name(), botCount, packCount)));
366
367                 /* now remove the network. */
368                 networkConnections.remove(network.get());
369         }
370
371         /**
372          * Shows a message when a channel was joined by us.
373          *
374          * @param channelJoined
375          *              The channel joined event
376          */
377         @Subscribe
378         public void channelJoined(ChannelJoined channelJoined) {
379                 if (channelJoined.connection().isSource(channelJoined.client())) {
380                         Optional<Network> network = getNetwork(channelJoined.connection());
381                         if (!network.isPresent()) {
382                                 return;
383                         }
384
385                         Optional<Channel> channel = getChannel(network.get(), channelJoined.channel());
386                         if (!channel.isPresent()) {
387                                 /* it’s an extra channel. */
388                                 extraChannels.add(new Channel(network.get(), channelJoined.channel()));
389                                 logger.info(String.format("Joined extra Channel %s on %s.", channelJoined.channel(), network.get().name()));
390                                 return;
391                         }
392
393                         joinedChannels.add(channel.get());
394                         logger.info(String.format("Joined Channel %s on %s.", channelJoined.channel(), network.get().name()));
395                 }
396         }
397
398         /**
399          * Removes bots that leave a channel, or channels when it’s us that’s leaving.
400          *
401          * @param channelLeft
402          *              The channel left event
403          */
404         @Subscribe
405         public void channelLeft(ChannelLeft channelLeft) {
406                 Optional<Network> network = getNetwork(channelLeft.connection());
407                 if (!network.isPresent()) {
408                         return;
409                 }
410
411                 Bot bot = networkBots.get(network.get(), channelLeft.client().nick().get());
412                 if (bot == null) {
413                         /* maybe it was us? */
414                         if (channelLeft.connection().isSource(channelLeft.client())) {
415                                 Optional<Channel> channel = getChannel(network.get(), channelLeft.channel());
416                                 if (!channel.isPresent()) {
417                                         /* maybe it was an extra channel? */
418                                         channel = getExtraChannel(network.get(), channelLeft.channel());
419                                         if (!channel.isPresent()) {
420                                                 /* okay, whatever. */
421                                                 return;
422                                         }
423
424                                         extraChannels.remove(channel);
425                                 } else {
426                                         channels.remove(channel.get());
427                                 }
428
429                                 eventBus.post(new GenericMessage(String.format("Left Channel %s on %s.", channel.get().name(), channel.get().network().name())));
430                         }
431
432                         return;
433                 }
434
435                 Bot removedBot = networkBots.remove(network.get(), channelLeft.client().nick().get());
436                 if (removedBot != null) {
437                         eventBus.post(new GenericMessage(String.format("Bot %s (%s) was removed, %d packs removed.", removedBot.name(), removedBot.network().name(), removedBot.packs().size())));
438                 }
439         }
440
441         /**
442          * Removes a client (which may be a bot) from the table of known bots.
443          *
444          * @param clientQuit
445          *              The client quit event
446          */
447         @Subscribe
448         public void clientQuit(ClientQuit clientQuit) {
449                 Optional<Network> network = getNetwork(clientQuit.connection());
450                 if (!network.isPresent()) {
451                         return;
452                 }
453
454                 Bot removedBot = networkBots.remove(network.get(), clientQuit.client().nick().get());
455                 if (removedBot != null) {
456                         eventBus.post(new GenericMessage(String.format("Bot %s (%s) was removed, %d packs removed.", removedBot.name(), removedBot.network().name(), removedBot.packs().size())));
457                 }
458         }
459
460         /**
461          * If the nickname of a bit changes, remove it from the old name and store it
462          * under the new name.
463          *
464          * @param nicknameChanged
465          *              The nickname changed event
466          */
467         @Subscribe
468         public void nicknameChanged(NicknameChanged nicknameChanged) {
469                 Optional<Network> network = getNetwork(nicknameChanged.connection());
470                 if (!network.isPresent()) {
471                         return;
472                 }
473
474                 Bot bot = networkBots.remove(network.get(), nicknameChanged.client().nick().get());
475                 if (bot == null) {
476                         return;
477                 }
478
479                 networkBots.put(network.get(), nicknameChanged.newNickname(), bot);
480         }
481
482         /**
483          * If a message on a channel is received, it is parsed for pack information
484          * with is then added to a bot.
485          *
486          * @param channelMessageReceived
487          *              The channel message received event
488          */
489         @Subscribe
490         public void channelMessageReceived(ChannelMessageReceived channelMessageReceived) {
491                 String message = MessageCleaner.getDefaultInstance().clean(channelMessageReceived.message());
492                 if (!message.startsWith("#")) {
493                         /* most probably not a pack announcement. */
494                         return;
495                 }
496
497                 Optional<Network> network = getNetwork(channelMessageReceived.connection());
498                 if (!network.isPresent()) {
499                         /* message for unknown connection? */
500                         return;
501                 }
502
503                 /* parse pack information. */
504                 Optional<Pack> pack = parsePack(message);
505                 if (!pack.isPresent()) {
506                         return;
507                 }
508
509                 Bot bot;
510                 synchronized (networkBots) {
511                         if (!networkBots.contains(network.get(), channelMessageReceived.source().nick().get())) {
512                                 bot = new Bot(network.get()).name(channelMessageReceived.source().nick().get());
513                                 networkBots.put(network.get(), channelMessageReceived.source().nick().get(), bot);
514                                 eventBus.post(new BotAdded(bot));
515                         } else {
516                                 bot = networkBots.get(network.get(), channelMessageReceived.source().nick().get());
517                         }
518                 }
519
520                 /* add pack. */
521                 bot.addPack(pack.get());
522                 logger.fine(String.format("Bot %s now has %d packs.", bot, bot.packs().size()));
523         }
524
525         /**
526          * Forward all private messages to every console.
527          *
528          * @param privateMessageReceived
529          *              The private message recevied event
530          */
531         @Subscribe
532         public void privateMessageReceived(PrivateMessageReceived privateMessageReceived) {
533                 eventBus.post(new MessageReceived(privateMessageReceived.source(), privateMessageReceived.message()));
534         }
535
536         /**
537          * Sends a message to all console when a notice was received.
538          *
539          * @param privateNoticeReceived
540          *              The notice received event
541          */
542         @Subscribe
543         public void privateNoticeReceived(PrivateNoticeReceived privateNoticeReceived) {
544                 Optional<Network> network = getNetwork(privateNoticeReceived.connection());
545                 if (!network.isPresent()) {
546                         return;
547                 }
548
549                 eventBus.post(new GenericMessage(String.format("Notice from %s (%s): %s", privateNoticeReceived.reply().source(), network.get(), privateNoticeReceived.text())));
550         }
551
552         /**
553          * Starts a DCC download.
554          *
555          * @param dccSendReceived
556          *              The DCC SEND event
557          */
558         @Subscribe
559         public void dccSendReceived(DccSendReceived dccSendReceived) {
560                 Optional<Network> network = getNetwork(dccSendReceived.connection());
561                 if (!network.isPresent()) {
562                         return;
563                 }
564
565                 Download download = downloads.get(dccSendReceived.filename());
566                 if (download == null) {
567                         /* unknown download, ignore. */
568                         return;
569                 }
570
571                 /* check if the file already exists. */
572                 File outputFile = new File(temporaryDirectory, dccSendReceived.filename());
573                 if (outputFile.exists()) {
574                         long existingFileSize = outputFile.length();
575
576                         /* file already complete? */
577                         if ((dccSendReceived.filesize() > -1) && (existingFileSize >= dccSendReceived.filesize())) {
578                                 /* file is apparently already complete. just move it. */
579                                 if (outputFile.renameTo(new File(finalDirectory, download.pack().name()))) {
580                                         eventBus.post(new GenericMessage(String.format("File %s already downloaded.", download.pack().name())));
581                                 } else {
582                                         eventBus.post(new GenericMessage(String.format("File %s already downloaded but not moved to %s.", download.pack().name(), finalDirectory)));
583                                 }
584
585                                 /* remove download. */
586                                 downloads.remove(download.pack().name());
587                                 return;
588                         }
589
590                         /* file not complete yet, DCC resume it. */
591                         try {
592                                 download.remoteAddress(dccSendReceived.inetAddress()).filesize(dccSendReceived.filesize());
593                                 dccSendReceived.connection().sendDccResume(dccSendReceived.source().nick().get(), dccSendReceived.filename(), dccSendReceived.port(), existingFileSize);
594                         } catch (IOException ioe1) {
595                                 eventBus.post(new GenericError(String.format("Could not send DCC RESUME %s to %s (%s).", dccSendReceived.filename(), dccSendReceived.source().nick().get(), ioe1.getMessage())));
596                         }
597
598                         return;
599                 }
600
601                 /* file does not exist, start the download. */
602                 try {
603                         OutputStream fileOutputStream = new FileOutputStream(outputFile);
604                         DccReceiver dccReceiver = new DccReceiver(eventBus, dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream);
605                         download.filename(outputFile.getPath()).outputStream(fileOutputStream).dccReceiver(dccReceiver);
606                         dccReceivers.add(dccReceiver);
607                         dccReceiver.start();
608                         eventBus.post(new DownloadStarted(download));
609                 } catch (FileNotFoundException fnfe1) {
610                         eventBus.post(new GenericError(String.format("Could not start download of %s from %s (%s).", dccSendReceived.filename(), dccSendReceived.source().nick().get(), fnfe1.getMessage())));
611                 }
612         }
613
614         @Subscribe
615         public void dccAcceptReceived(DccAcceptReceived dccAcceptReceived) {
616                 Optional<Network> network = getNetwork(dccAcceptReceived.connection());
617                 if (!network.isPresent()) {
618                         return;
619                 }
620
621                 Download download = downloads.get(dccAcceptReceived.filename());
622                 if (download == null) {
623                         /* unknown download, ignore. */
624                         return;
625                 }
626
627                 try {
628                         File outputFile = new File(temporaryDirectory, dccAcceptReceived.filename());
629                         if (outputFile.length() != dccAcceptReceived.position()) {
630                                 eventBus.post(new GenericError(String.format("Download %s from %s does not start at the right position!")));
631                                 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()));
632
633                                 downloads.remove(download.pack().name());
634                                 return;
635                         }
636                         OutputStream outputStream = new FileOutputStream(outputFile, true);
637                         DccReceiver dccReceiver = new DccReceiver(eventBus, download.remoteAddress(), dccAcceptReceived.port(), dccAcceptReceived.filename(), dccAcceptReceived.position(), download.filesize(), outputStream);
638                         download.filename(outputFile.getPath()).outputStream(outputStream).dccReceiver(dccReceiver);
639                         dccReceivers.add(dccReceiver);
640                         dccReceiver.start();
641                         eventBus.post(new DownloadStarted(download));
642                 } catch (FileNotFoundException fnfe1) {
643                 }
644         }
645
646         /**
647          * Closes the output stream of the download and moves the file to the final
648          * location.
649          *
650          * @param dccDownloadFinished
651          *              The DCC download finished event
652          */
653         @Subscribe
654         public void dccDownloadFinished(DccDownloadFinished dccDownloadFinished) {
655                 Download download = downloads.get(dccDownloadFinished.dccReceiver().filename());
656                 if (download == null) {
657                         /* probably shouldn’t happen. */
658                         return;
659                 }
660
661                 try {
662                         download.outputStream().close();
663                         File file = new File(download.filename());
664                         file.renameTo(new File(finalDirectory, download.pack().name()));
665                         eventBus.post(new DownloadFinished(download));
666                         dccReceivers.remove(dccDownloadFinished.dccReceiver());
667                         downloads.remove(download.pack().name());
668                 } catch (IOException ioe1) {
669                         /* TODO - handle all the errors. */
670                         logger.log(Level.WARNING, String.format("Could not move file %s to directory %s.", download.filename(), finalDirectory), ioe1);
671                 }
672         }
673
674         /**
675          * Closes the output stream and notifies all listeners of the failure.
676          *
677          * @param dccDownloadFailed
678          *              The DCC download failed event
679          */
680         @Subscribe
681         public void dccDownloadFailed(DccDownloadFailed dccDownloadFailed) {
682                 Download download = downloads.get(dccDownloadFailed.dccReceiver().filename());
683                 if (download == null) {
684                         /* probably shouldn’t happen. */
685                         return;
686                 }
687
688                 try {
689                         Closeables.close(download.outputStream(), true);
690                         eventBus.post(new DownloadFailed(download));
691                         dccReceivers.remove(dccDownloadFailed.dccReceiver());
692                         downloads.remove(download.pack().name());
693                 } catch (IOException ioe1) {
694                         /* swallow silently. */
695                 }
696         }
697
698         //
699         // PRIVATE METHODS
700         //
701
702         /**
703          * Searches all current connections for the given connection, returning the
704          * associated network.
705          *
706          * @param connection
707          *              The connection to get the network for
708          * @return The network belonging to the connection, or {@link
709          *         Optional#absent()}
710          */
711         private Optional<Network> getNetwork(Connection connection) {
712                 for (Entry<Network, Connection> networkConnectionEntry : networkConnections.entrySet()) {
713                         if (networkConnectionEntry.getValue().equals(connection)) {
714                                 return Optional.of(networkConnectionEntry.getKey());
715                         }
716                 }
717                 return Optional.absent();
718         }
719
720         /**
721          * Returns the configured channel for the given network and name.
722          *
723          * @param network
724          *              The network the channel is located on
725          * @param channelName
726          *              The name of the channel
727          * @return The configured channel, or {@link Optional#absent()} if no
728          *         configured channel matching the given network and name was found
729          */
730         public Optional<Channel> getChannel(Network network, String channelName) {
731                 for (Channel channel : channels) {
732                         if (channel.network().equals(network) && (channel.name().equalsIgnoreCase(channelName))) {
733                                 return Optional.of(channel);
734                         }
735                 }
736                 return Optional.absent();
737         }
738
739         /**
740          * Returns the extra channel for the given network and name.
741          *
742          * @param network
743          *              The network the channel is located on
744          * @param channelName
745          *              The name of the channel
746          * @return The extra channel, or {@link Optional#absent()} if no extra channel
747          *         matching the given network and name was found
748          */
749         public Optional<Channel> getExtraChannel(Network network, String channelName) {
750                 for (Channel channel : extraChannels) {
751                         if (channel.network().equals(network) && (channel.name().equalsIgnoreCase(channelName))) {
752                                 return Optional.of(channel);
753                         }
754                 }
755                 return Optional.absent();
756         }
757
758         /**
759          * Parses {@link Pack} information from the given message.
760          *
761          * @param message
762          *              The message to parse pack information from
763          * @return The parsed pack, or {@link Optional#absent()} if the message could
764          *         not be parsed into a pack
765          */
766         private Optional<Pack> parsePack(String message) {
767                 int squareOpen = message.indexOf('[');
768                 int squareClose = message.indexOf(']', squareOpen);
769                 if ((squareOpen == -1) && (squareClose == -1)) {
770                         return Optional.absent();
771                 }
772                 String packSize = message.substring(squareOpen + 1, squareClose);
773                 String packName = message.substring(message.lastIndexOf(' ') + 1);
774                 String packIndex = message.substring(0, message.indexOf(' ')).substring(1);
775                 return Optional.of(new Pack(packIndex, packSize, packName));
776         }
777
778 }