e24fa2ff3aeafae29df0b5601c2538b1fe48b64e
[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 static java.lang.String.format;
21 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.banned;
22 import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.registeredNicknamesOnly;
23 import static net.pterodactylus.irc.util.MessageCleaner.getDefaultInstance;
24 import static net.pterodactylus.xdcc.data.Channel.TO_NETWORK;
25 import static net.pterodactylus.xdcc.data.Download.FILTER_RUNNING;
26
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.HashSet;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Map.Entry;
39 import java.util.Set;
40 import java.util.TreeMap;
41 import java.util.concurrent.TimeUnit;
42 import java.util.function.Function;
43 import java.util.stream.Collectors;
44
45 import net.pterodactylus.irc.Connection;
46 import net.pterodactylus.irc.ConnectionFactory;
47 import net.pterodactylus.irc.DccReceiver;
48 import net.pterodactylus.irc.DefaultConnection;
49 import net.pterodactylus.irc.event.ChannelJoined;
50 import net.pterodactylus.irc.event.ChannelLeft;
51 import net.pterodactylus.irc.event.ChannelMessageReceived;
52 import net.pterodactylus.irc.event.ChannelNotJoined;
53 import net.pterodactylus.irc.event.ClientQuit;
54 import net.pterodactylus.irc.event.ConnectionClosed;
55 import net.pterodactylus.irc.event.ConnectionEstablished;
56 import net.pterodactylus.irc.event.ConnectionFailed;
57 import net.pterodactylus.irc.event.DccAcceptReceived;
58 import net.pterodactylus.irc.event.DccDownloadFailed;
59 import net.pterodactylus.irc.event.DccDownloadFinished;
60 import net.pterodactylus.irc.event.DccSendReceived;
61 import net.pterodactylus.irc.event.KickedFromChannel;
62 import net.pterodactylus.irc.event.NicknameChanged;
63 import net.pterodactylus.irc.event.PrivateMessageReceived;
64 import net.pterodactylus.irc.event.PrivateNoticeReceived;
65 import net.pterodactylus.irc.event.ReplyReceived;
66 import net.pterodactylus.irc.util.RandomNickname;
67 import net.pterodactylus.xdcc.core.event.BotAdded;
68 import net.pterodactylus.xdcc.core.event.CoreStarted;
69 import net.pterodactylus.xdcc.core.event.DownloadFailed;
70 import net.pterodactylus.xdcc.core.event.DownloadFinished;
71 import net.pterodactylus.xdcc.core.event.DownloadStarted;
72 import net.pterodactylus.xdcc.core.event.GenericError;
73 import net.pterodactylus.xdcc.core.event.GenericMessage;
74 import net.pterodactylus.xdcc.core.event.MessageReceived;
75 import net.pterodactylus.xdcc.data.Bot;
76 import net.pterodactylus.xdcc.data.Channel;
77 import net.pterodactylus.xdcc.data.ConnectedNetwork;
78 import net.pterodactylus.xdcc.data.Download;
79 import net.pterodactylus.xdcc.data.Network;
80 import net.pterodactylus.xdcc.data.Pack;
81 import net.pterodactylus.xdcc.data.Server;
82
83 import com.google.common.base.Optional;
84 import com.google.common.base.Predicate;
85 import com.google.common.collect.FluentIterable;
86 import com.google.common.collect.HashBasedTable;
87 import com.google.common.collect.HashMultimap;
88 import com.google.common.collect.ImmutableList;
89 import com.google.common.collect.ImmutableSet;
90 import com.google.common.collect.Lists;
91 import com.google.common.collect.Maps;
92 import com.google.common.collect.Multimap;
93 import com.google.common.collect.Sets;
94 import com.google.common.collect.Table;
95 import com.google.common.eventbus.EventBus;
96 import com.google.common.eventbus.Subscribe;
97 import com.google.common.io.Closeables;
98 import com.google.common.util.concurrent.AbstractExecutionThreadService;
99 import com.google.inject.Inject;
100 import org.apache.log4j.Logger;
101
102 /**
103  * The core of XDCC Downloader.
104  *
105  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
106  */
107 public class Core extends AbstractExecutionThreadService {
108
109         /** The logger. */
110         private static final Logger logger = Logger.getLogger(Core.class.getName());
111
112         private final Object syncObject = new Object();
113         /** The event bus. */
114         private final EventBus eventBus;
115         private final ConnectionFactory connectionFactory;
116         private final ChannelBanManager channelBanManager =
117                         new ChannelBanManager();
118         private final ConnectionBackoff connectionBackoff = new ConnectionBackoff();
119
120         /** The temporary directory to download files to. */
121         private final String temporaryDirectory;
122
123         /** The directory to move finished downloads to. */
124         private final String finalDirectory;
125
126         /** The channels that should be monitored. */
127         private final Collection<Channel> channels = Sets.newHashSet();
128
129         /** The channels that are currentlymonitored. */
130         private final Collection<Channel> joinedChannels = Sets.newHashSet();
131         private final Set<Channel> channelsBeingJoined = new HashSet<>();
132
133         /** The channels that are joined but not configured. */
134         private final Collection<Channel> extraChannels = Sets.newHashSet();
135
136         /** The current network connections. */
137         private final Map<Network, Connection> networkConnections = Collections.synchronizedMap(Maps.<Network, Connection>newHashMap());
138
139         /** The currently known bots. */
140         private final Table<Network, String, Bot> networkBots = HashBasedTable.create();
141
142         /** The current downloads. */
143         private final Multimap<String, Download> downloads = HashMultimap.create();
144
145         /** The current DCC receivers. */
146         private final Collection<DccReceiver> dccReceivers = Lists.newArrayList();
147
148         /**
149          * Creates a new core.
150          *
151          * @param eventBus
152          *              The event bus
153          * @param temporaryDirectory
154          *              The directory to download files to
155          * @param finalDirectory
156          *              The directory to move finished files to
157          */
158         @Inject
159         public Core(EventBus eventBus, ConnectionFactory connectionFactory, String temporaryDirectory, String finalDirectory) {
160                 this.eventBus = eventBus;
161                 this.connectionFactory = connectionFactory;
162                 this.temporaryDirectory = temporaryDirectory;
163                 this.finalDirectory = finalDirectory;
164         }
165
166         //
167         // ACCESSORS
168         //
169
170         /**
171          * Returns all currently known connections.
172          *
173          * @return All currently known connections
174          */
175         public Collection<Connection> connections() {
176                 return networkConnections.values();
177         }
178
179         /**
180          * Returns all defined networks.
181          *
182          * @return All defined networks
183          */
184         public Collection<Network> networks() {
185                 return FluentIterable.from(channels).transform(TO_NETWORK).toSet();
186         }
187
188         /**
189          * Returns all connected networks.
190          *
191          * @return All connected networks
192          */
193         public Collection<ConnectedNetwork> connectedNetworks() {
194                 return networkConnections.entrySet().stream().map((entry) -> {
195                         Network network = entry.getKey();
196                         Collection<Bot> bots = networkBots.row(network).values();
197                         int packCount = bots.stream().mapToInt((bot) -> bot.packs().size()).reduce((a, b) -> a + b).orElse(0);
198                         return new ConnectedNetwork(network, entry.getValue().hostname(),
199                                         entry.getValue().port(), entry.getValue().nickname(),
200                                         channels.stream()
201                                                         .filter((channel) -> channel.network()
202                                                                         .equals(network))
203                                                         .map(Channel::name)
204                                                         .collect(Collectors.<String>toList()),
205                                         extraChannels.stream()
206                                                         .filter((channel) -> channel.network()
207                                                                         .equals(network))
208                                                         .map(Channel::name)
209                                                         .collect(Collectors.<String>toList()),
210                                         bots.size(), packCount);
211                 }).collect(Collectors.<ConnectedNetwork>toList());
212         }
213
214         /**
215          * Returns all configured channels. Due to various circumstances, configured
216          * channels might not actually be joined.
217          *
218          * @return All configured channels
219          */
220         public Collection<Channel> channels() {
221                 return ImmutableSet.copyOf(channels);
222         }
223
224         /**
225          * Returns all currently joined channels.
226          *
227          * @return All currently joined channels
228          */
229         public Collection<Channel> joinedChannels() {
230                 return ImmutableSet.copyOf(joinedChannels);
231         }
232
233         /**
234          * Returns all currently joined channels that are not configured.
235          *
236          * @return All currently joined but not configured channels
237          */
238         public Collection<Channel> extraChannels() {
239                 return ImmutableSet.copyOf(extraChannels);
240         }
241
242         /**
243          * Returns all currently known bots.
244          *
245          * @return All currently known bots
246          */
247         public Collection<Bot> bots() {
248                 return networkBots.values();
249         }
250
251         /**
252          * Returns all currently running downloads.
253          *
254          * @return All currently running downloads
255          */
256         public Collection<Download> downloads() {
257                 return downloads.values();
258         }
259
260         //
261         // ACTIONS
262         //
263
264         /**
265          * Adds a channel to monitor.
266          *
267          * @param channel
268          *              The channel to monitor
269          */
270         public void addChannel(Channel channel) {
271                 channels.add(channel);
272         }
273
274         /**
275          * Fetches the given pack from the given bot.
276          *
277          * @param bot
278          *              The bot to fetch the pack from
279          * @param pack
280          *              The pack to fetch
281          */
282         public void fetch(Bot bot, Pack pack) {
283                 Connection connection = networkConnections.get(bot.network());
284                 if (connection == null) {
285                         return;
286                 }
287
288                 /* check if we are already downloading the file? */
289                 if (downloads.containsKey(pack.name())) {
290                         Collection<Download> packDownloads = downloads.get(pack.name());
291                         Collection<Download> runningDownloads = FluentIterable.from(packDownloads).filter(FILTER_RUNNING).toSet();
292                         if (!runningDownloads.isEmpty()) {
293                                 Download download = runningDownloads.iterator().next();
294                                 eventBus.post(new GenericMessage(String.format("File %s is already downloading from %s (%s).", pack.name(), download.bot().name(), download.bot().network().name())));
295                                 return;
296                         }
297                         StringBuilder bots = new StringBuilder();
298                         for (Download download : packDownloads) {
299                                 if (bots.length() > 0) {
300                                         bots.append(", ");
301                                 }
302                                 bots.append(download.bot().name()).append(" (").append(download.bot().network().name()).append(')');
303                         }
304                         eventBus.post(new GenericMessage(String.format("File %s is already requested from %d bots (%s).", pack.name(), packDownloads.size(), bots.toString())));
305                 }
306
307                 Download download = new Download(bot, pack);
308                 downloads.put(pack.name(), download);
309
310                 try {
311                         connection.sendMessage(bot.name(), "XDCC SEND " + pack.id());
312                 } catch (IOException ioe1) {
313                         logger.warn("Could not send message to bot!", ioe1);
314                 }
315         }
316
317         /**
318          * Cancels the download of the given pack from the given bot.
319          *
320          * @param bot
321          *              The bot the pack is being downloaded from
322          * @param pack
323          *              The pack being downloaded
324          */
325         public void cancelDownload(Bot bot, Pack pack) {
326                 Optional<Download> download = getDownload(pack, bot);
327                 if (!download.isPresent()) {
328                         return;
329                 }
330
331                 /* get connection. */
332                 Connection connection = networkConnections.get(bot.network());
333                 if (connection == null) {
334                         /* request for unknown network? */
335                         return;
336                 }
337
338                 /* stop the DCC receiver. */
339                 if (download.get().dccReceiver() != null) {
340                         download.get().dccReceiver().stop();
341                 } else {
342                         /* remove download if it hasn’t started yet. */
343                         downloads.remove(pack.name(), download.get());
344                 }
345
346                 /* remove the request from the bot, too. */
347                 try {
348                         connection.sendMessage(bot.name(), String.format("XDCC %s", (download.get().dccReceiver() != null) ? "CANCEL" : "REMOVE"));
349                 } catch (IOException ioe1) {
350                         logger.warn(String.format("Could not cancel DCC from %s (%s)!", bot.name(), bot.network().name()), ioe1);
351                 }
352         }
353
354         /**
355          * Closes the given connection.
356          *
357          * @param connection
358          *              The connection to close
359          */
360         public void closeConnection(Connection connection) {
361                 try {
362                         connection.close();
363                 } catch (IOException ioe1) {
364                         /* TODO */
365                 }
366         }
367
368         //
369         // ABSTRACTIDLESERVICE METHODS
370         //
371
372         @Override
373         protected void startUp() {
374                 for (Channel channel : channels) {
375                         logger.info(String.format("Connecting to Channel %s on Network %s…", channel.name(), channel.network().name()));
376                         connectNetwork(channel.network());
377                 }
378
379                 /* notify listeners. */
380                 eventBus.post(new CoreStarted(this));
381         }
382
383         @Override
384         protected void run() throws Exception {
385                 while (isRunning()) {
386
387                         Set<Channel> missingChannels = new HashSet<>();
388                         for (Channel channel : channels) {
389                                 if (joinedChannels.contains(channel) || channelsBeingJoined.contains(channel)) {
390                                         continue;
391                                 }
392                                 if (channelBanManager.isBanned(channel)) {
393                                         continue;
394                                 }
395                                 if (!networkConnections.containsKey(channel.network()) || networkConnections.get(channel.network()).established()) {
396                                         missingChannels.add(channel);
397                                 }
398                         }
399                         Set<Network> missingNetworks = missingChannels.stream()
400                                         .map(Channel::network)
401                                         .distinct()
402                                         .filter((network) -> !networkConnections.containsKey(network))
403                                         .collect(Collectors.toSet());
404
405                         if (missingNetworks.isEmpty()) {
406                                 if (!missingChannels.isEmpty()) {
407                                         for (Channel missingChannel : missingChannels) {
408                                                 Network network = missingChannel.network();
409                                                 eventBus.post(new GenericMessage(String.format("Trying to join %s on %s...", missingChannel.name(), network)));
410                                                 try {
411                                                         channelsBeingJoined.add(missingChannel);
412                                                         networkConnections.get(network).joinChannel(missingChannel.name());
413                                                 } catch (IOException ioe1) {
414                                                         logger.warn(String.format("Could not join %s on %s!", missingChannel.name(), network.name()), ioe1);
415                                                 }
416                                         }
417                                 } else {
418                                         synchronized (syncObject) {
419                                                 try {
420                                                         syncObject.wait(TimeUnit.MINUTES.toMillis(1));
421                                                 } catch (InterruptedException ie1) {
422                                                 /* ignore. */
423                                                 }
424                                         }
425                                 }
426                                 continue;
427                         }
428
429                         Map<Long, Network> timesForNextConnects = new TreeMap<>(missingNetworks.stream()
430                                         .collect(Collectors.toMap(connectionBackoff::getBackoff, Function.identity(), (network, ignore) -> network)));
431
432                         Optional<Entry<Long, Network>> firstNetwork = Optional.fromNullable(timesForNextConnects.entrySet().stream().findFirst().orElse(null));
433                         if (!firstNetwork.isPresent()) {
434                                 continue;
435                         }
436                         if (firstNetwork.get().getKey() > 0) {
437                                 eventBus.post(new GenericMessage(String.format("Waiting %d minutes to connect to %s...", TimeUnit.MILLISECONDS.toMinutes(firstNetwork.get().getKey()), firstNetwork.get().getValue().name())));
438                                 synchronized (syncObject) {
439                                         try {
440                                                 syncObject.wait(firstNetwork.get().getKey());
441                                         } catch (InterruptedException ie1) {
442                                                 /* ignore. */
443                                         }
444                                 }
445                                 if (!isRunning()) {
446                                         break;
447                                 }
448                         }
449
450                         connectNetwork(firstNetwork.get().getValue());
451                 }
452         }
453
454         @Override
455         protected void triggerShutdown() {
456                 synchronized (syncObject) {
457                         syncObject.notifyAll();
458                 }
459         }
460
461         //
462         // PRIVATE METHODS
463         //
464
465         /**
466          * Starts a new connection for the given network if no such connection exists
467          * already.
468          *
469          * @param network
470          *              The network to connect to
471          */
472         private void connectNetwork(Network network) {
473                 if (!networkConnections.containsKey(network)) {
474                                 /* select a random server. */
475                         List<Server> servers = Lists.newArrayList(network.servers());
476                         if (servers.isEmpty()) {
477                                 eventBus.post(new GenericError(String.format("Network %s does not have any servers.", network.name())));
478                                 return;
479                         }
480                         Server server = servers.get((int) (Math.random() * servers.size()));
481                         eventBus.post(new GenericMessage(String.format("Connecting to %s on %s...", network.name(), server.hostname())));
482                         Connection connection = connectionFactory.createConnection(server.hostname(),
483                                         server.unencryptedPorts().iterator().next());
484                         connection.username(RandomNickname.get()).realName(RandomNickname.get());
485                         networkConnections.put(network, connection);
486                         connection.open();
487                 }
488         }
489
490         /**
491          * Removes the given connection and all its channels and bots.
492          *
493          * @param connection
494          *              The connection to remove
495          */
496         private void removeConnection(Connection connection) {
497                 Optional<Network> network = getNetwork(connection);
498                 if (!network.isPresent()) {
499                         return;
500                 }
501                 networkConnections.remove(network.get());
502                 if (!connection.established()) {
503                         return;
504                 }
505
506                 /* find all channels that need to be removed. */
507                 for (Collection<Channel> channels : ImmutableList.of(joinedChannels, extraChannels)) {
508                         for (Iterator<Channel> channelIterator = channels.iterator(); channelIterator.hasNext(); ) {
509                                 Channel joinedChannel = channelIterator.next();
510                                 if (!joinedChannel.network().equals(network.get())) {
511                                         continue;
512                                 }
513
514                                 channelIterator.remove();
515                         }
516                 }
517
518                 /* now remove all bots for that network. */
519                 Map<String, Bot> bots = networkBots.row(network.get());
520                 int botCount = bots.size();
521                 int packCount = 0;
522                 for (Bot bot : bots.values()) {
523                         packCount += bot.packs().size();
524                 }
525                 bots.clear();
526                 eventBus.post(new GenericMessage(String.format("Network %s disconnected, %d bots removed, %d packs removed.", network.get().name(), botCount, packCount)));
527         }
528
529         //
530         // EVENT HANDLERS
531         //
532
533         /**
534          * If a connection to a network has been established, the channels associated
535          * with this network are joined.
536          *
537          * @param connectionEstablished
538          *              The connection established event
539          */
540         @Subscribe
541         public void connectionEstablished(ConnectionEstablished connectionEstablished) {
542
543                 /* get network for connection. */
544                 Optional<Network> network = getNetwork(connectionEstablished.connection());
545
546                 /* found network? */
547                 if (!network.isPresent()) {
548                         eventBus.post(new GenericMessage(String.format("Connected to unknown network: %s", connectionEstablished.connection().hostname())));
549                         return;
550                 }
551
552                 connectionBackoff.connectionSuccessful(network.get());
553                 eventBus.post(new GenericMessage(String.format("Connected to network %s.", network.get().name())));
554
555                 /* join all channels on this network. */
556                 for (Channel channel : channels) {
557                         if (channel.network().equals(network.get())) {
558                                 try {
559                                         eventBus.post(new GenericMessage(String.format("Trying to join %s on %s...", channel.name(), network.get().name())));
560                                         connectionEstablished.connection().joinChannel(channel.name());
561                                 } catch (IOException ioe1) {
562                                         logger.warn(String.format("Could not join %s on %s!", channel.name(), network.get().name()), ioe1);
563                                 }
564                         }
565                 }
566         }
567
568         /**
569          * Remove all data stored for a network if the connection is closed.
570          *
571          * @param connectionClosed
572          *              The connection closed event
573          */
574         @Subscribe
575         public void connectionClosed(ConnectionClosed connectionClosed) {
576                 connectionBackoff.connectionFailed(getNetwork(connectionClosed.connection()).get());
577                 removeConnection(connectionClosed.connection());
578                 synchronized (syncObject) {
579                         syncObject.notifyAll();
580                 }
581                 eventBus.post(new GenericMessage(String.format("Connection closed by %s.", connectionClosed.connection().hostname())));
582         }
583
584         /**
585          * Remove all data stored for a network if the connection fails.
586          *
587          * @param connectionFailed
588          *              The connection failed event
589          */
590         @Subscribe
591         public void connectionFailed(ConnectionFailed connectionFailed) {
592                 connectionBackoff.connectionFailed(getNetwork(connectionFailed.connection()).get());
593                 removeConnection(connectionFailed.connection());
594                 synchronized (syncObject) {
595                         syncObject.notifyAll();
596                 }
597                 eventBus.post(new GenericMessage(String.format("Could not connect to %s: %s.", connectionFailed.connection().hostname(), connectionFailed.cause())));
598         }
599
600         /**
601          * Shows a message when a channel was joined by us.
602          *
603          * @param channelJoined
604          *              The channel joined event
605          */
606         @Subscribe
607         public void channelJoined(ChannelJoined channelJoined) {
608                 if (channelJoined.connection().isSource(channelJoined.client())) {
609                         Optional<Network> network = getNetwork(channelJoined.connection());
610                         if (!network.isPresent()) {
611                                 return;
612                         }
613
614                         Optional<Channel> channel = getChannel(network.get(), channelJoined.channel());
615                         if (!channel.isPresent()) {
616                                 /* it’s an extra channel. */
617                                 extraChannels.add(new Channel(network.get(), channelJoined.channel()));
618                                 logger.info(String.format("Joined extra Channel %s on %s.", channelJoined.channel(), network.get().name()));
619                                 return;
620                         }
621
622                         channelBanManager.unban(channel.get());
623                         joinedChannels.add(channel.get());
624                         channelsBeingJoined.remove(channel.get());
625                         logger.info(String.format("Joined Channel %s on %s.", channelJoined.channel(), network.get().name()));
626                 }
627         }
628
629         @Subscribe
630         public void channelNotJoined(ChannelNotJoined channelNotJoined) {
631                 Optional<Network> network = getNetwork(channelNotJoined.connection());
632                 if (!network.isPresent()) {
633                         return;
634                 }
635
636                 Optional<Channel> channel = getChannel(network.get(), channelNotJoined.channel());
637                 synchronized (syncObject) {
638                         syncObject.notifyAll();
639                 }
640                 if (!channel.isPresent()) {
641                         eventBus.post(new GenericMessage(format("Could not join %s but didn’t try to join, either.", channelNotJoined.channel())));
642                         return;
643                 }
644                 channelsBeingJoined.remove(channel.get());
645
646
647                 /* remove all bots for this channel, we might have been kicked. */
648                 Collection<Bot> botsToRemove = networkBots.row(network.get())
649                                 .values().stream()
650                                 .filter(bot -> bot.channel()
651                                                 .equalsIgnoreCase(channel.get().name()))
652                                 .collect(Collectors.toSet());
653                 botsToRemove.stream()
654                                 .forEach(bot -> networkBots.row(network.get())
655                                                 .remove(bot.name()));
656
657                 if (channelNotJoined.reason() == registeredNicknamesOnly) {
658                         channels.remove(channel.get());
659                         eventBus.post(new GenericMessage(
660                                         format("Not trying to join %s anymore.", channel.get())));
661                         return;
662                 }
663                 if (channelNotJoined.reason() == banned) {
664                         channelBanManager.ban(channel.get());
665                         eventBus.post(new GenericMessage(
666                                         format("Banned from %s, suspending join for a day.",
667                                                         channel.get())));
668                         return;
669                 }
670
671                 eventBus.post(new GenericMessage(
672                                 format("Could not join %s: %s", channelNotJoined.channel(),
673                                                 channelNotJoined.reason())));
674         }
675
676         /**
677          * Removes bots that leave a channel, or channels when it’s us that’s leaving.
678          *
679          * @param channelLeft
680          *              The channel left event
681          */
682         @Subscribe
683         public void channelLeft(ChannelLeft channelLeft) {
684                 Optional<Network> network = getNetwork(channelLeft.connection());
685                 if (!network.isPresent()) {
686                         return;
687                 }
688
689                 Bot bot = networkBots.get(network.get(), channelLeft.client().nick().get());
690                 if (bot == null) {
691                         /* maybe it was us? */
692                         if (channelLeft.connection().isSource(channelLeft.client())) {
693                                 Optional<Channel> channel = getChannel(network.get(), channelLeft.channel());
694                                 if (!channel.isPresent()) {
695                                         /* maybe it was an extra channel? */
696                                         channel = getExtraChannel(network.get(), channelLeft.channel());
697                                         if (!channel.isPresent()) {
698                                                 /* okay, whatever. */
699                                                 return;
700                                         }
701
702                                         extraChannels.remove(channel);
703                                 } else {
704                                         channels.remove(channel.get());
705                                 }
706                                 synchronized (syncObject) {
707                                         syncObject.notifyAll();
708                                 }
709
710                                 eventBus.post(new GenericMessage(String.format("Left Channel %s on %s.", channel.get().name(), channel.get().network().name())));
711                         }
712
713                         return;
714                 }
715
716                 networkBots.remove(network.get(), channelLeft.client().nick().get());
717         }
718
719         @Subscribe
720         public void kickedFromChannel(KickedFromChannel kickedFromChannel) {
721                 Optional<Network> network = getNetwork(kickedFromChannel.connection());
722                 if (!network.isPresent()) {
723                         return;
724                 }
725
726                 /* have we been kicked? */
727                 if (nicknameMatchesConnection(kickedFromChannel.connection(), kickedFromChannel.kickee())) {
728                         Optional<Channel> channel = getChannel(network.get(), kickedFromChannel.channel());
729                         if (!channel.isPresent()) {
730                                 /* maybe it was an extra channel? */
731                                 channel = getExtraChannel(network.get(), kickedFromChannel.channel());
732                                 if (!channel.isPresent()) {
733                                         /* okay, whatever. */
734                                         return;
735                                 }
736
737                                 extraChannels.remove(channel.get());
738                         } else {
739                                 joinedChannels.remove(channel.get());
740                         }
741                         synchronized (syncObject) {
742                                 syncObject.notifyAll();
743                         }
744                         eventBus.post(new GenericMessage(format(
745                                         "Kicked from %s by %s: %s",
746                                         kickedFromChannel.channel(),
747                                         kickedFromChannel.kicker(),
748                                         kickedFromChannel.reason().or("<unknown>")
749                         )));
750                 }
751         }
752
753         private boolean nicknameMatchesConnection(Connection connection, String nickname) {
754                 return connection.nickname().equalsIgnoreCase(nickname);
755         }
756
757         /**
758          * Removes a client (which may be a bot) from the table of known bots.
759          *
760          * @param clientQuit
761          *              The client quit event
762          */
763         @Subscribe
764         public void clientQuit(ClientQuit clientQuit) {
765                 Optional<Network> network = getNetwork(clientQuit.connection());
766                 if (!network.isPresent()) {
767                         return;
768                 }
769
770                 networkBots.remove(network.get(), clientQuit.client().nick().get());
771         }
772
773         /**
774          * If the nickname of a bit changes, remove it from the old name and store it
775          * under the new name.
776          *
777          * @param nicknameChanged
778          *              The nickname changed event
779          */
780         @Subscribe
781         public void nicknameChanged(NicknameChanged nicknameChanged) {
782                 Optional<Network> network = getNetwork(nicknameChanged.connection());
783                 if (!network.isPresent()) {
784                         return;
785                 }
786
787                 Bot bot = networkBots.remove(network.get(), nicknameChanged.client().nick().get());
788                 if (bot == null) {
789                         return;
790                 }
791
792                 networkBots.put(network.get(), nicknameChanged.newNickname(), bot);
793         }
794
795         /**
796          * If a message on a channel is received, it is parsed for pack information
797          * with is then added to a bot.
798          *
799          * @param channelMessageReceived
800          *              The channel message received event
801          */
802         @Subscribe
803         public void channelMessageReceived(ChannelMessageReceived channelMessageReceived) {
804                 String message = getDefaultInstance().clean(channelMessageReceived.message());
805                 if (!message.startsWith("#")) {
806                         /* most probably not a pack announcement. */
807                         return;
808                 }
809
810                 Optional<Network> network = getNetwork(channelMessageReceived.connection());
811                 if (!network.isPresent()) {
812                         /* message for unknown connection? */
813                         return;
814                 }
815
816                 /* parse pack information. */
817                 Optional<Pack> pack = parsePack(message);
818                 if (!pack.isPresent()) {
819                         return;
820                 }
821
822                 Bot bot;
823                 synchronized (networkBots) {
824                         if (!networkBots.contains(network.get(), channelMessageReceived.source().nick().get())) {
825                                 bot = new Bot(network.get(), channelMessageReceived.channel(),
826                                                 channelMessageReceived.source().nick().get());
827                                 networkBots.put(network.get(), channelMessageReceived.source().nick().get(), bot);
828                                 eventBus.post(new BotAdded(bot));
829                         } else {
830                                 bot = networkBots.get(network.get(), channelMessageReceived.source().nick().get());
831                         }
832                 }
833
834                 /* add pack. */
835                 bot.addPack(pack.get());
836                 logger.debug(String.format("Bot %s now has %d packs.", bot, bot.packs().size()));
837         }
838
839         /**
840          * Forward all private messages to every console.
841          *
842          * @param privateMessageReceived
843          *              The private message recevied event
844          */
845         @Subscribe
846         public void privateMessageReceived(PrivateMessageReceived privateMessageReceived) {
847                 eventBus.post(new MessageReceived(privateMessageReceived.source(), privateMessageReceived.message()));
848         }
849
850         /**
851          * Sends a message to all console when a notice was received.
852          *
853          * @param privateNoticeReceived
854          *              The notice received event
855          */
856         @Subscribe
857         public void privateNoticeReceived(PrivateNoticeReceived privateNoticeReceived) {
858                 Optional<Network> network = getNetwork(privateNoticeReceived.connection());
859                 if (!network.isPresent()) {
860                         return;
861                 }
862
863                 eventBus.post(new GenericMessage(String.format("Notice from %s (%s): %s", privateNoticeReceived.source(), network.get(), privateNoticeReceived.text())));
864         }
865
866         /**
867          * Starts a DCC download.
868          *
869          * @param dccSendReceived
870          *              The DCC SEND event
871          */
872         @Subscribe
873         public void dccSendReceived(final DccSendReceived dccSendReceived) {
874                 final Optional<Network> network = getNetwork(dccSendReceived.connection());
875                 if (!network.isPresent()) {
876                         return;
877                 }
878
879                 Collection<Download> packDownloads = downloads.get(dccSendReceived.filename());
880                 if (packDownloads.isEmpty()) {
881                         /* unknown download, ignore. */
882                         return;
883                 }
884
885                 /* check if it’s already downloading. */
886                 Collection<Download> runningDownloads = FluentIterable.from(packDownloads).filter(FILTER_RUNNING).toSet();
887                 if (!runningDownloads.isEmpty()) {
888                         eventBus.post(new GenericMessage(String.format("Ignoring offer for %s, it’s already being downloaded.", dccSendReceived.filename())));
889                         return;
890                 }
891
892                 /* locate the correct download. */
893                 Collection<Download> requestedDownload = FluentIterable.from(packDownloads).filter(new Predicate<Download>() {
894
895                         @Override
896                         public boolean apply(Download download) {
897                                 return download.bot().network().equals(network.get()) && download.bot().name().equalsIgnoreCase(dccSendReceived.source().nick().get());
898                         }
899                 }).toSet();
900
901                 /* we did not request this download. */
902                 if (requestedDownload.isEmpty()) {
903                         return;
904                 }
905
906                 Download download = requestedDownload.iterator().next();
907
908                 /* check if the file already exists. */
909                 File outputFile = new File(temporaryDirectory, dccSendReceived.filename());
910                 if (outputFile.exists()) {
911                         long existingFileSize = outputFile.length();
912
913                         /* file already complete? */
914                         if ((dccSendReceived.filesize() > -1) && (existingFileSize >= dccSendReceived.filesize())) {
915                                 /* file is apparently already complete. just move it. */
916                                 if (outputFile.renameTo(new File(finalDirectory, download.pack().name()))) {
917                                         eventBus.post(new GenericMessage(String.format("File %s already downloaded.", download.pack().name())));
918                                 } else {
919                                         eventBus.post(new GenericMessage(String.format("File %s already downloaded but not moved to %s.", download.pack().name(), finalDirectory)));
920                                 }
921
922                                 /* remove download. */
923                                 downloads.removeAll(download.pack().name());
924                                 return;
925                         }
926
927                         /* file not complete yet, DCC resume it. */
928                         try {
929                                 download.remoteAddress(dccSendReceived.inetAddress()).filesize(dccSendReceived.filesize());
930                                 dccSendReceived.connection().sendDccResume(dccSendReceived.source().nick().get(), dccSendReceived.filename(), dccSendReceived.port(), existingFileSize);
931                         } catch (IOException ioe1) {
932                                 eventBus.post(new GenericError(String.format("Could not send DCC RESUME %s to %s (%s).", dccSendReceived.filename(), dccSendReceived.source().nick().get(), ioe1.getMessage())));
933                         }
934
935                         return;
936                 }
937
938                 /* file does not exist, start the download. */
939                 try {
940                         OutputStream fileOutputStream = new FileOutputStream(outputFile);
941                         DccReceiver dccReceiver = new DccReceiver(eventBus, dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream);
942                         download.filename(outputFile.getPath()).outputStream(fileOutputStream).dccReceiver(dccReceiver);
943                         dccReceivers.add(dccReceiver);
944                         dccReceiver.start();
945                         eventBus.post(new DownloadStarted(download));
946                 } catch (FileNotFoundException fnfe1) {
947                         eventBus.post(new GenericError(String.format("Could not start download of %s from %s (%s).", dccSendReceived.filename(), dccSendReceived.source().nick().get(), fnfe1.getMessage())));
948                 }
949         }
950
951         @Subscribe
952         public void dccAcceptReceived(final DccAcceptReceived dccAcceptReceived) {
953                 final Optional<Network> network = getNetwork(dccAcceptReceived.connection());
954                 if (!network.isPresent()) {
955                         return;
956                 }
957
958                 Collection<Download> packDownloads = downloads.get(dccAcceptReceived.filename());
959                 if (packDownloads.isEmpty()) {
960                         /* unknown download, ignore. */
961                         return;
962                 }
963
964                 /* check if it’s already downloading. */
965                 Collection<Download> runningDownloads = FluentIterable.from(packDownloads).filter(FILTER_RUNNING).toSet();
966                 if (!runningDownloads.isEmpty()) {
967                         eventBus.post(new GenericMessage(String.format("Ignoring offer for %s, it’s already being downloaded.", dccAcceptReceived.filename())));
968                         return;
969                 }
970
971                 /* locate the correct download. */
972                 Collection<Download> requestedDownload = FluentIterable.from(packDownloads).filter(new Predicate<Download>() {
973
974                         @Override
975                         public boolean apply(Download download) {
976                                 return download.bot().network().equals(network.get()) && download.bot().name().equalsIgnoreCase(dccAcceptReceived.source().nick().get());
977                         }
978                 }).toSet();
979
980                 /* we did not request this download. */
981                 if (requestedDownload.isEmpty()) {
982                         return;
983                 }
984
985                 Download download = requestedDownload.iterator().next();
986
987                 try {
988                         File outputFile = new File(temporaryDirectory, dccAcceptReceived.filename());
989                         if (outputFile.length() != dccAcceptReceived.position()) {
990                                 eventBus.post(new GenericError(String.format("Download %s from %s does not start at the right position!")));
991                                 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()));
992
993                                 downloads.removeAll(download.pack().name());
994                                 return;
995                         }
996                         OutputStream outputStream = new FileOutputStream(outputFile, true);
997                         DccReceiver dccReceiver = new DccReceiver(eventBus, download.remoteAddress(), dccAcceptReceived.port(), dccAcceptReceived.filename(), dccAcceptReceived.position(), download.filesize(), outputStream);
998                         download.filename(outputFile.getPath()).outputStream(outputStream).dccReceiver(dccReceiver);
999                         dccReceivers.add(dccReceiver);
1000                         dccReceiver.start();
1001                         eventBus.post(new DownloadStarted(download));
1002                 } catch (FileNotFoundException fnfe1) {
1003                 }
1004         }
1005
1006         /**
1007          * Closes the output stream of the download and moves the file to the final
1008          * location.
1009          *
1010          * @param dccDownloadFinished
1011          *              The DCC download finished event
1012          */
1013         @Subscribe
1014         public void dccDownloadFinished(DccDownloadFinished dccDownloadFinished) {
1015
1016                 /* locate the correct download. */
1017                 Collection<Download> requestedDownload = FluentIterable.from(downloads.get(dccDownloadFinished.dccReceiver().filename())).filter(FILTER_RUNNING).toSet();
1018                 if (requestedDownload.isEmpty()) {
1019                         /* this seems wrong. */
1020                         logger.warn("Download finished but could not be located.");
1021                         return;
1022                 }
1023                 Download download = requestedDownload.iterator().next();
1024
1025                 try {
1026                         download.outputStream().close();
1027                         File file = new File(download.filename());
1028                         file.renameTo(new File(finalDirectory, download.pack().name()));
1029                         eventBus.post(new DownloadFinished(download));
1030                         dccReceivers.remove(dccDownloadFinished.dccReceiver());
1031                         downloads.removeAll(download.pack().name());
1032                 } catch (IOException ioe1) {
1033                         /* TODO - handle all the errors. */
1034                         logger.warn(String.format("Could not move file %s to directory %s.", download.filename(), finalDirectory), ioe1);
1035                 }
1036         }
1037
1038         /**
1039          * Closes the output stream and notifies all listeners of the failure.
1040          *
1041          * @param dccDownloadFailed
1042          *              The DCC download failed event
1043          */
1044         @Subscribe
1045         public void dccDownloadFailed(DccDownloadFailed dccDownloadFailed) {
1046
1047                 /* locate the correct download. */
1048                 Collection<Download> requestedDownload = FluentIterable.from(downloads.get(dccDownloadFailed.dccReceiver().filename())).filter(FILTER_RUNNING).toSet();
1049                 if (requestedDownload.isEmpty()) {
1050                         /* this seems wrong. */
1051                         logger.warn("Download finished but could not be located.");
1052                         return;
1053                 }
1054                 Download download = requestedDownload.iterator().next();
1055
1056                 try {
1057                         Closeables.close(download.outputStream(), true);
1058                         eventBus.post(new DownloadFailed(download));
1059                         dccReceivers.remove(dccDownloadFailed.dccReceiver());
1060                         downloads.removeAll(download.pack().name());
1061                 } catch (IOException ioe1) {
1062                         /* swallow silently. */
1063                 }
1064         }
1065
1066         @Subscribe
1067         public void replyReceived(ReplyReceived replyReceived) {
1068                 logger.trace(String.format("%s: %s", replyReceived.connection().hostname(), replyReceived.reply()));
1069         }
1070
1071         //
1072         // PRIVATE METHODS
1073         //
1074
1075         /**
1076          * Returns the download of the given pack from the given bot.
1077          *
1078          * @param pack
1079          *              The pack being downloaded
1080          * @param bot
1081          *              The bot the pack is being downloaded from
1082          * @return The download, or {@link Optional#absent()} if the download could not
1083          *         be found
1084          */
1085         private Optional<Download> getDownload(Pack pack, Bot bot) {
1086                 if (!downloads.containsKey(pack.name())) {
1087                         return Optional.absent();
1088                 }
1089                 for (Download download : Lists.newArrayList(downloads.get(pack.name()))) {
1090                         if (download.bot().equals(bot)) {
1091                                 return Optional.of(download);
1092                         }
1093                 }
1094                 return Optional.absent();
1095         }
1096
1097         /**
1098          * Searches all current connections for the given connection, returning the
1099          * associated network.
1100          *
1101          * @param connection
1102          *              The connection to get the network for
1103          * @return The network belonging to the connection, or {@link
1104          *         Optional#absent()}
1105          */
1106         private Optional<Network> getNetwork(Connection connection) {
1107                 for (Entry<Network, Connection> networkConnectionEntry : networkConnections.entrySet()) {
1108                         if (networkConnectionEntry.getValue().equals(connection)) {
1109                                 return Optional.of(networkConnectionEntry.getKey());
1110                         }
1111                 }
1112                 return Optional.absent();
1113         }
1114
1115         /**
1116          * Returns the configured channel for the given network and name.
1117          *
1118          * @param network
1119          *              The network the channel is located on
1120          * @param channelName
1121          *              The name of the channel
1122          * @return The configured channel, or {@link Optional#absent()} if no
1123          *         configured channel matching the given network and name was found
1124          */
1125         public Optional<Channel> getChannel(Network network, String channelName) {
1126                 for (Channel channel : channels) {
1127                         if (channel.network().equals(network) && (channel.name().equalsIgnoreCase(channelName))) {
1128                                 return Optional.of(channel);
1129                         }
1130                 }
1131                 return Optional.absent();
1132         }
1133
1134         /**
1135          * Returns the extra channel for the given network and name.
1136          *
1137          * @param network
1138          *              The network the channel is located on
1139          * @param channelName
1140          *              The name of the channel
1141          * @return The extra channel, or {@link Optional#absent()} if no extra channel
1142          *         matching the given network and name was found
1143          */
1144         public Optional<Channel> getExtraChannel(Network network, String channelName) {
1145                 for (Channel channel : extraChannels) {
1146                         if (channel.network().equals(network) && (channel.name().equalsIgnoreCase(channelName))) {
1147                                 return Optional.of(channel);
1148                         }
1149                 }
1150                 return Optional.absent();
1151         }
1152
1153         /**
1154          * Parses {@link Pack} information from the given message.
1155          *
1156          * @param message
1157          *              The message to parse pack information from
1158          * @return The parsed pack, or {@link Optional#absent()} if the message could
1159          *         not be parsed into a pack
1160          */
1161         private Optional<Pack> parsePack(String message) {
1162                 int squareOpen = message.indexOf('[');
1163                 int squareClose = message.indexOf(']', squareOpen);
1164                 if ((squareOpen == -1) && (squareClose == -1)) {
1165                         return Optional.absent();
1166                 }
1167                 String packSize = message.substring(squareOpen + 1, squareClose);
1168                 String packName = message.substring(message.lastIndexOf(' ') + 1);
1169                 String packIndex = message.substring(0, message.indexOf(' ')).substring(1);
1170                 return Optional.of(new Pack(packIndex, packSize, packName));
1171         }
1172
1173 }