ca918050751462be82579ed457f6df8c11419083
[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())) {
396                                         if (networkConnections.get(channel.network()).established()) {
397                                                 missingChannels.add(channel);
398                                         }
399                                 }
400                         }
401                         Set<Network> missingNetworks = missingChannels.stream()
402                                         .map(Channel::network)
403                                         .distinct()
404                                         .filter((network) -> !networkConnections.containsKey(network))
405                                         .collect(Collectors.toSet());
406
407                         if (!missingChannels.isEmpty()) {
408                                 for (Channel missingChannel : missingChannels) {
409                                         Network network = missingChannel.network();
410                                         eventBus.post(new GenericMessage(String.format("Trying to join %s on %s...", missingChannel.name(), network)));
411                                         try {
412                                                 channelsBeingJoined.add(missingChannel);
413                                                 networkConnections.get(network).joinChannel(missingChannel.name());
414                                         } catch (IOException ioe1) {
415                                                 logger.warn(String.format("Could not join %s on %s!", missingChannel.name(), network.name()), ioe1);
416                                         }
417                                 }
418                         } else if (missingNetworks.isEmpty()) {
419                                 synchronized (syncObject) {
420                                         try {
421                                                 syncObject.wait(TimeUnit.MINUTES.toMillis(1));
422                                         } catch (InterruptedException ie1) {
423                                                 /* ignore. */
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                         Entry<Long, Network> firstNetwork = timesForNextConnects.entrySet().stream().findFirst().get();
433                         if (firstNetwork.getKey() > 0) {
434                                 eventBus.post(new GenericMessage(String.format("Waiting %d seconds to connect to %s...", TimeUnit.MILLISECONDS.toMinutes(firstNetwork.getKey()), firstNetwork.getValue().name())));
435                                 synchronized (syncObject) {
436                                         try {
437                                                 syncObject.wait(firstNetwork.getKey());
438                                         } catch (InterruptedException ie1) {
439                                                 /* ignore. */
440                                         }
441                                 }
442                                 if (!isRunning()) {
443                                         break;
444                                 }
445                         }
446
447                         connectNetwork(firstNetwork.getValue());
448                 }
449         }
450
451         @Override
452         protected void triggerShutdown() {
453                 synchronized (syncObject) {
454                         syncObject.notifyAll();
455                 }
456         }
457
458         //
459         // PRIVATE METHODS
460         //
461
462         /**
463          * Starts a new connection for the given network if no such connection exists
464          * already.
465          *
466          * @param network
467          *              The network to connect to
468          */
469         private void connectNetwork(Network network) {
470                 if (!networkConnections.containsKey(network)) {
471                                 /* select a random server. */
472                         List<Server> servers = Lists.newArrayList(network.servers());
473                         if (servers.isEmpty()) {
474                                 eventBus.post(new GenericError(String.format("Network %s does not have any servers.", network.name())));
475                                 return;
476                         }
477                         Server server = servers.get((int) (Math.random() * servers.size()));
478                         Connection connection = connectionFactory.createConnection(server.hostname(),
479                                         server.unencryptedPorts().iterator().next());
480                         connection.username(RandomNickname.get()).realName(RandomNickname.get());
481                         networkConnections.put(network, connection);
482                         connection.open();
483                 }
484         }
485
486         /**
487          * Removes the given connection and all its channels and bots.
488          *
489          * @param connection
490          *              The connection to remove
491          */
492         private void removeConnection(Connection connection) {
493                 Optional<Network> network = getNetwork(connection);
494                 if (!network.isPresent()) {
495                         return;
496                 }
497                 networkConnections.remove(network.get());
498                 if (!connection.established()) {
499                         return;
500                 }
501
502                 /* find all channels that need to be removed. */
503                 for (Collection<Channel> channels : ImmutableList.of(joinedChannels, extraChannels)) {
504                         for (Iterator<Channel> channelIterator = channels.iterator(); channelIterator.hasNext(); ) {
505                                 Channel joinedChannel = channelIterator.next();
506                                 if (!joinedChannel.network().equals(network.get())) {
507                                         continue;
508                                 }
509
510                                 channelIterator.remove();
511                         }
512                 }
513
514                 /* now remove all bots for that network. */
515                 Map<String, Bot> bots = networkBots.row(network.get());
516                 int botCount = bots.size();
517                 int packCount = 0;
518                 for (Bot bot : bots.values()) {
519                         packCount += bot.packs().size();
520                 }
521                 bots.clear();
522                 eventBus.post(new GenericMessage(String.format("Network %s disconnected, %d bots removed, %d packs removed.", network.get().name(), botCount, packCount)));
523         }
524
525         //
526         // EVENT HANDLERS
527         //
528
529         /**
530          * If a connection to a network has been established, the channels associated
531          * with this network are joined.
532          *
533          * @param connectionEstablished
534          *              The connection established event
535          */
536         @Subscribe
537         public void connectionEstablished(ConnectionEstablished connectionEstablished) {
538
539                 /* get network for connection. */
540                 Optional<Network> network = getNetwork(connectionEstablished.connection());
541
542                 /* found network? */
543                 if (!network.isPresent()) {
544                         eventBus.post(new GenericMessage(String.format("Connected to unknown network: %s", connectionEstablished.connection().hostname())));
545                         return;
546                 }
547
548                 connectionBackoff.connectionSuccessful(network.get());
549                 eventBus.post(new GenericMessage(String.format("Connected to network %s.", network.get().name())));
550
551                 /* join all channels on this network. */
552                 for (Channel channel : channels) {
553                         if (channel.network().equals(network.get())) {
554                                 try {
555                                         eventBus.post(new GenericMessage(String.format("Trying to join %s on %s...", channel.name(), network.get().name())));
556                                         connectionEstablished.connection().joinChannel(channel.name());
557                                 } catch (IOException ioe1) {
558                                         logger.warn(String.format("Could not join %s on %s!", channel.name(), network.get().name()), ioe1);
559                                 }
560                         }
561                 }
562         }
563
564         /**
565          * Remove all data stored for a network if the connection is closed.
566          *
567          * @param connectionClosed
568          *              The connection closed event
569          */
570         @Subscribe
571         public void connectionClosed(ConnectionClosed connectionClosed) {
572                 removeConnection(connectionClosed.connection());
573                 connectionBackoff.connectionFailed(getNetwork(connectionClosed.connection()).get());
574                 eventBus.post(new GenericMessage(String.format("Connection closed by %s.", connectionClosed.connection().hostname())));
575         }
576
577         /**
578          * Remove all data stored for a network if the connection fails.
579          *
580          * @param connectionFailed
581          *              The connection failed event
582          */
583         @Subscribe
584         public void connectionFailed(ConnectionFailed connectionFailed) {
585                 removeConnection(connectionFailed.connection());
586                 connectionBackoff.connectionFailed(getNetwork(connectionFailed.connection()).get());
587                 eventBus.post(new GenericMessage(String.format("Could not connect to %s: %s.", connectionFailed.connection().hostname(), connectionFailed.cause())));
588         }
589
590         /**
591          * Shows a message when a channel was joined by us.
592          *
593          * @param channelJoined
594          *              The channel joined event
595          */
596         @Subscribe
597         public void channelJoined(ChannelJoined channelJoined) {
598                 if (channelJoined.connection().isSource(channelJoined.client())) {
599                         Optional<Network> network = getNetwork(channelJoined.connection());
600                         if (!network.isPresent()) {
601                                 return;
602                         }
603
604                         Optional<Channel> channel = getChannel(network.get(), channelJoined.channel());
605                         if (!channel.isPresent()) {
606                                 /* it’s an extra channel. */
607                                 extraChannels.add(new Channel(network.get(), channelJoined.channel()));
608                                 logger.info(String.format("Joined extra Channel %s on %s.", channelJoined.channel(), network.get().name()));
609                                 return;
610                         }
611
612                         channelBanManager.unban(channel.get());
613                         joinedChannels.add(channel.get());
614                         channelsBeingJoined.remove(channel.get());
615                         logger.info(String.format("Joined Channel %s on %s.", channelJoined.channel(), network.get().name()));
616                 }
617         }
618
619         @Subscribe
620         public void channelNotJoined(ChannelNotJoined channelNotJoined) {
621                 Optional<Network> network = getNetwork(channelNotJoined.connection());
622                 if (!network.isPresent()) {
623                         return;
624                 }
625
626                 Optional<Channel> channel = getChannel(network.get(), channelNotJoined.channel());
627                 if (!channel.isPresent()) {
628                         eventBus.post(new GenericMessage(format("Could not join %s but didn’t try to join, either.", channel.get())));
629                         return;
630                 }
631
632                 channelsBeingJoined.remove(channel.get());
633
634                 /* remove all bots for this channel, we might have been kicked. */
635                 Collection<Bot> botsToRemove = networkBots.row(network.get())
636                                 .values().stream()
637                                 .filter(bot -> bot.channel()
638                                                 .equalsIgnoreCase(channel.get().name()))
639                                 .collect(Collectors.toSet());
640                 botsToRemove.stream()
641                                 .forEach(bot -> networkBots.row(network.get())
642                                                 .remove(bot.name()));
643
644                 if (channelNotJoined.reason() == registeredNicknamesOnly) {
645                         channels.remove(channel.get());
646                         eventBus.post(new GenericMessage(
647                                         format("Not trying to join %s anymore.", channel.get())));
648                         return;
649                 }
650                 if (channelNotJoined.reason() == banned) {
651                         channelBanManager.ban(channel.get());
652                         eventBus.post(new GenericMessage(
653                                         format("Banned from %s, suspending join for a day.",
654                                                         channel.get())));
655                         return;
656                 }
657
658                 eventBus.post(new GenericMessage(
659                                 format("Could not join %s: %s", channelNotJoined.channel(),
660                                                 channelNotJoined.reason())));
661         }
662
663         /**
664          * Removes bots that leave a channel, or channels when it’s us that’s leaving.
665          *
666          * @param channelLeft
667          *              The channel left event
668          */
669         @Subscribe
670         public void channelLeft(ChannelLeft channelLeft) {
671                 Optional<Network> network = getNetwork(channelLeft.connection());
672                 if (!network.isPresent()) {
673                         return;
674                 }
675
676                 Bot bot = networkBots.get(network.get(), channelLeft.client().nick().get());
677                 if (bot == null) {
678                         /* maybe it was us? */
679                         if (channelLeft.connection().isSource(channelLeft.client())) {
680                                 Optional<Channel> channel = getChannel(network.get(), channelLeft.channel());
681                                 if (!channel.isPresent()) {
682                                         /* maybe it was an extra channel? */
683                                         channel = getExtraChannel(network.get(), channelLeft.channel());
684                                         if (!channel.isPresent()) {
685                                                 /* okay, whatever. */
686                                                 return;
687                                         }
688
689                                         extraChannels.remove(channel);
690                                 } else {
691                                         channels.remove(channel.get());
692                                 }
693
694                                 eventBus.post(new GenericMessage(String.format("Left Channel %s on %s.", channel.get().name(), channel.get().network().name())));
695                         }
696
697                         return;
698                 }
699
700                 networkBots.remove(network.get(), channelLeft.client().nick().get());
701         }
702
703         @Subscribe
704         public void kickedFromChannel(KickedFromChannel kickedFromChannel) {
705                 Optional<Network> network = getNetwork(kickedFromChannel.connection());
706                 if (!network.isPresent()) {
707                         return;
708                 }
709
710                 /* have we been kicked? */
711                 if (nicknameMatchesConnection(kickedFromChannel.connection(), kickedFromChannel.kickee())) {
712                         Optional<Channel> channel = getChannel(network.get(), kickedFromChannel.channel());
713                         if (!channel.isPresent()) {
714                                 /* maybe it was an extra channel? */
715                                 channel = getExtraChannel(network.get(), kickedFromChannel.channel());
716                                 if (!channel.isPresent()) {
717                                         /* okay, whatever. */
718                                         return;
719                                 }
720
721                                 extraChannels.remove(channel.get());
722                         } else {
723                                 joinedChannels.remove(channel.get());
724                         }
725                         eventBus.post(new GenericMessage(format(
726                                         "Kicked from %s by %s: %s",
727                                         kickedFromChannel.channel(),
728                                         kickedFromChannel.kicker(),
729                                         kickedFromChannel.reason().or("<unknown>")
730                         )));
731                 }
732         }
733
734         private boolean nicknameMatchesConnection(Connection connection, String nickname) {
735                 return connection.nickname().equalsIgnoreCase(nickname);
736         }
737
738         /**
739          * Removes a client (which may be a bot) from the table of known bots.
740          *
741          * @param clientQuit
742          *              The client quit event
743          */
744         @Subscribe
745         public void clientQuit(ClientQuit clientQuit) {
746                 Optional<Network> network = getNetwork(clientQuit.connection());
747                 if (!network.isPresent()) {
748                         return;
749                 }
750
751                 networkBots.remove(network.get(), clientQuit.client().nick().get());
752         }
753
754         /**
755          * If the nickname of a bit changes, remove it from the old name and store it
756          * under the new name.
757          *
758          * @param nicknameChanged
759          *              The nickname changed event
760          */
761         @Subscribe
762         public void nicknameChanged(NicknameChanged nicknameChanged) {
763                 Optional<Network> network = getNetwork(nicknameChanged.connection());
764                 if (!network.isPresent()) {
765                         return;
766                 }
767
768                 Bot bot = networkBots.remove(network.get(), nicknameChanged.client().nick().get());
769                 if (bot == null) {
770                         return;
771                 }
772
773                 networkBots.put(network.get(), nicknameChanged.newNickname(), bot);
774         }
775
776         /**
777          * If a message on a channel is received, it is parsed for pack information
778          * with is then added to a bot.
779          *
780          * @param channelMessageReceived
781          *              The channel message received event
782          */
783         @Subscribe
784         public void channelMessageReceived(ChannelMessageReceived channelMessageReceived) {
785                 String message = getDefaultInstance().clean(channelMessageReceived.message());
786                 if (!message.startsWith("#")) {
787                         /* most probably not a pack announcement. */
788                         return;
789                 }
790
791                 Optional<Network> network = getNetwork(channelMessageReceived.connection());
792                 if (!network.isPresent()) {
793                         /* message for unknown connection? */
794                         return;
795                 }
796
797                 /* parse pack information. */
798                 Optional<Pack> pack = parsePack(message);
799                 if (!pack.isPresent()) {
800                         return;
801                 }
802
803                 Bot bot;
804                 synchronized (networkBots) {
805                         if (!networkBots.contains(network.get(), channelMessageReceived.source().nick().get())) {
806                                 bot = new Bot(network.get(), channelMessageReceived.channel(),
807                                                 channelMessageReceived.source().nick().get());
808                                 networkBots.put(network.get(), channelMessageReceived.source().nick().get(), bot);
809                                 eventBus.post(new BotAdded(bot));
810                         } else {
811                                 bot = networkBots.get(network.get(), channelMessageReceived.source().nick().get());
812                         }
813                 }
814
815                 /* add pack. */
816                 bot.addPack(pack.get());
817                 logger.debug(String.format("Bot %s now has %d packs.", bot, bot.packs().size()));
818         }
819
820         /**
821          * Forward all private messages to every console.
822          *
823          * @param privateMessageReceived
824          *              The private message recevied event
825          */
826         @Subscribe
827         public void privateMessageReceived(PrivateMessageReceived privateMessageReceived) {
828                 eventBus.post(new MessageReceived(privateMessageReceived.source(), privateMessageReceived.message()));
829         }
830
831         /**
832          * Sends a message to all console when a notice was received.
833          *
834          * @param privateNoticeReceived
835          *              The notice received event
836          */
837         @Subscribe
838         public void privateNoticeReceived(PrivateNoticeReceived privateNoticeReceived) {
839                 Optional<Network> network = getNetwork(privateNoticeReceived.connection());
840                 if (!network.isPresent()) {
841                         return;
842                 }
843
844                 eventBus.post(new GenericMessage(String.format("Notice from %s (%s): %s", privateNoticeReceived.source(), network.get(), privateNoticeReceived.text())));
845         }
846
847         /**
848          * Starts a DCC download.
849          *
850          * @param dccSendReceived
851          *              The DCC SEND event
852          */
853         @Subscribe
854         public void dccSendReceived(final DccSendReceived dccSendReceived) {
855                 final Optional<Network> network = getNetwork(dccSendReceived.connection());
856                 if (!network.isPresent()) {
857                         return;
858                 }
859
860                 Collection<Download> packDownloads = downloads.get(dccSendReceived.filename());
861                 if (packDownloads.isEmpty()) {
862                         /* unknown download, ignore. */
863                         return;
864                 }
865
866                 /* check if it’s already downloading. */
867                 Collection<Download> runningDownloads = FluentIterable.from(packDownloads).filter(FILTER_RUNNING).toSet();
868                 if (!runningDownloads.isEmpty()) {
869                         eventBus.post(new GenericMessage(String.format("Ignoring offer for %s, it’s already being downloaded.", dccSendReceived.filename())));
870                         return;
871                 }
872
873                 /* locate the correct download. */
874                 Collection<Download> requestedDownload = FluentIterable.from(packDownloads).filter(new Predicate<Download>() {
875
876                         @Override
877                         public boolean apply(Download download) {
878                                 return download.bot().network().equals(network.get()) && download.bot().name().equalsIgnoreCase(dccSendReceived.source().nick().get());
879                         }
880                 }).toSet();
881
882                 /* we did not request this download. */
883                 if (requestedDownload.isEmpty()) {
884                         return;
885                 }
886
887                 Download download = requestedDownload.iterator().next();
888
889                 /* check if the file already exists. */
890                 File outputFile = new File(temporaryDirectory, dccSendReceived.filename());
891                 if (outputFile.exists()) {
892                         long existingFileSize = outputFile.length();
893
894                         /* file already complete? */
895                         if ((dccSendReceived.filesize() > -1) && (existingFileSize >= dccSendReceived.filesize())) {
896                                 /* file is apparently already complete. just move it. */
897                                 if (outputFile.renameTo(new File(finalDirectory, download.pack().name()))) {
898                                         eventBus.post(new GenericMessage(String.format("File %s already downloaded.", download.pack().name())));
899                                 } else {
900                                         eventBus.post(new GenericMessage(String.format("File %s already downloaded but not moved to %s.", download.pack().name(), finalDirectory)));
901                                 }
902
903                                 /* remove download. */
904                                 downloads.removeAll(download.pack().name());
905                                 return;
906                         }
907
908                         /* file not complete yet, DCC resume it. */
909                         try {
910                                 download.remoteAddress(dccSendReceived.inetAddress()).filesize(dccSendReceived.filesize());
911                                 dccSendReceived.connection().sendDccResume(dccSendReceived.source().nick().get(), dccSendReceived.filename(), dccSendReceived.port(), existingFileSize);
912                         } catch (IOException ioe1) {
913                                 eventBus.post(new GenericError(String.format("Could not send DCC RESUME %s to %s (%s).", dccSendReceived.filename(), dccSendReceived.source().nick().get(), ioe1.getMessage())));
914                         }
915
916                         return;
917                 }
918
919                 /* file does not exist, start the download. */
920                 try {
921                         OutputStream fileOutputStream = new FileOutputStream(outputFile);
922                         DccReceiver dccReceiver = new DccReceiver(eventBus, dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream);
923                         download.filename(outputFile.getPath()).outputStream(fileOutputStream).dccReceiver(dccReceiver);
924                         dccReceivers.add(dccReceiver);
925                         dccReceiver.start();
926                         eventBus.post(new DownloadStarted(download));
927                 } catch (FileNotFoundException fnfe1) {
928                         eventBus.post(new GenericError(String.format("Could not start download of %s from %s (%s).", dccSendReceived.filename(), dccSendReceived.source().nick().get(), fnfe1.getMessage())));
929                 }
930         }
931
932         @Subscribe
933         public void dccAcceptReceived(final DccAcceptReceived dccAcceptReceived) {
934                 final Optional<Network> network = getNetwork(dccAcceptReceived.connection());
935                 if (!network.isPresent()) {
936                         return;
937                 }
938
939                 Collection<Download> packDownloads = downloads.get(dccAcceptReceived.filename());
940                 if (packDownloads.isEmpty()) {
941                         /* unknown download, ignore. */
942                         return;
943                 }
944
945                 /* check if it’s already downloading. */
946                 Collection<Download> runningDownloads = FluentIterable.from(packDownloads).filter(FILTER_RUNNING).toSet();
947                 if (!runningDownloads.isEmpty()) {
948                         eventBus.post(new GenericMessage(String.format("Ignoring offer for %s, it’s already being downloaded.", dccAcceptReceived.filename())));
949                         return;
950                 }
951
952                 /* locate the correct download. */
953                 Collection<Download> requestedDownload = FluentIterable.from(packDownloads).filter(new Predicate<Download>() {
954
955                         @Override
956                         public boolean apply(Download download) {
957                                 return download.bot().network().equals(network.get()) && download.bot().name().equalsIgnoreCase(dccAcceptReceived.source().nick().get());
958                         }
959                 }).toSet();
960
961                 /* we did not request this download. */
962                 if (requestedDownload.isEmpty()) {
963                         return;
964                 }
965
966                 Download download = requestedDownload.iterator().next();
967
968                 try {
969                         File outputFile = new File(temporaryDirectory, dccAcceptReceived.filename());
970                         if (outputFile.length() != dccAcceptReceived.position()) {
971                                 eventBus.post(new GenericError(String.format("Download %s from %s does not start at the right position!")));
972                                 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()));
973
974                                 downloads.removeAll(download.pack().name());
975                                 return;
976                         }
977                         OutputStream outputStream = new FileOutputStream(outputFile, true);
978                         DccReceiver dccReceiver = new DccReceiver(eventBus, download.remoteAddress(), dccAcceptReceived.port(), dccAcceptReceived.filename(), dccAcceptReceived.position(), download.filesize(), outputStream);
979                         download.filename(outputFile.getPath()).outputStream(outputStream).dccReceiver(dccReceiver);
980                         dccReceivers.add(dccReceiver);
981                         dccReceiver.start();
982                         eventBus.post(new DownloadStarted(download));
983                 } catch (FileNotFoundException fnfe1) {
984                 }
985         }
986
987         /**
988          * Closes the output stream of the download and moves the file to the final
989          * location.
990          *
991          * @param dccDownloadFinished
992          *              The DCC download finished event
993          */
994         @Subscribe
995         public void dccDownloadFinished(DccDownloadFinished dccDownloadFinished) {
996
997                 /* locate the correct download. */
998                 Collection<Download> requestedDownload = FluentIterable.from(downloads.get(dccDownloadFinished.dccReceiver().filename())).filter(FILTER_RUNNING).toSet();
999                 if (requestedDownload.isEmpty()) {
1000                         /* this seems wrong. */
1001                         logger.warn("Download finished but could not be located.");
1002                         return;
1003                 }
1004                 Download download = requestedDownload.iterator().next();
1005
1006                 try {
1007                         download.outputStream().close();
1008                         File file = new File(download.filename());
1009                         file.renameTo(new File(finalDirectory, download.pack().name()));
1010                         eventBus.post(new DownloadFinished(download));
1011                         dccReceivers.remove(dccDownloadFinished.dccReceiver());
1012                         downloads.removeAll(download.pack().name());
1013                 } catch (IOException ioe1) {
1014                         /* TODO - handle all the errors. */
1015                         logger.warn(String.format("Could not move file %s to directory %s.", download.filename(), finalDirectory), ioe1);
1016                 }
1017         }
1018
1019         /**
1020          * Closes the output stream and notifies all listeners of the failure.
1021          *
1022          * @param dccDownloadFailed
1023          *              The DCC download failed event
1024          */
1025         @Subscribe
1026         public void dccDownloadFailed(DccDownloadFailed dccDownloadFailed) {
1027
1028                 /* locate the correct download. */
1029                 Collection<Download> requestedDownload = FluentIterable.from(downloads.get(dccDownloadFailed.dccReceiver().filename())).filter(FILTER_RUNNING).toSet();
1030                 if (requestedDownload.isEmpty()) {
1031                         /* this seems wrong. */
1032                         logger.warn("Download finished but could not be located.");
1033                         return;
1034                 }
1035                 Download download = requestedDownload.iterator().next();
1036
1037                 try {
1038                         Closeables.close(download.outputStream(), true);
1039                         eventBus.post(new DownloadFailed(download));
1040                         dccReceivers.remove(dccDownloadFailed.dccReceiver());
1041                         downloads.removeAll(download.pack().name());
1042                 } catch (IOException ioe1) {
1043                         /* swallow silently. */
1044                 }
1045         }
1046
1047         @Subscribe
1048         public void replyReceived(ReplyReceived replyReceived) {
1049                 logger.trace(String.format("%s: %s", replyReceived.connection().hostname(), replyReceived.reply()));
1050         }
1051
1052         //
1053         // PRIVATE METHODS
1054         //
1055
1056         /**
1057          * Returns the download of the given pack from the given bot.
1058          *
1059          * @param pack
1060          *              The pack being downloaded
1061          * @param bot
1062          *              The bot the pack is being downloaded from
1063          * @return The download, or {@link Optional#absent()} if the download could not
1064          *         be found
1065          */
1066         private Optional<Download> getDownload(Pack pack, Bot bot) {
1067                 if (!downloads.containsKey(pack.name())) {
1068                         return Optional.absent();
1069                 }
1070                 for (Download download : Lists.newArrayList(downloads.get(pack.name()))) {
1071                         if (download.bot().equals(bot)) {
1072                                 return Optional.of(download);
1073                         }
1074                 }
1075                 return Optional.absent();
1076         }
1077
1078         /**
1079          * Searches all current connections for the given connection, returning the
1080          * associated network.
1081          *
1082          * @param connection
1083          *              The connection to get the network for
1084          * @return The network belonging to the connection, or {@link
1085          *         Optional#absent()}
1086          */
1087         private Optional<Network> getNetwork(Connection connection) {
1088                 for (Entry<Network, Connection> networkConnectionEntry : networkConnections.entrySet()) {
1089                         if (networkConnectionEntry.getValue().equals(connection)) {
1090                                 return Optional.of(networkConnectionEntry.getKey());
1091                         }
1092                 }
1093                 return Optional.absent();
1094         }
1095
1096         /**
1097          * Returns the configured channel for the given network and name.
1098          *
1099          * @param network
1100          *              The network the channel is located on
1101          * @param channelName
1102          *              The name of the channel
1103          * @return The configured channel, or {@link Optional#absent()} if no
1104          *         configured channel matching the given network and name was found
1105          */
1106         public Optional<Channel> getChannel(Network network, String channelName) {
1107                 for (Channel channel : channels) {
1108                         if (channel.network().equals(network) && (channel.name().equalsIgnoreCase(channelName))) {
1109                                 return Optional.of(channel);
1110                         }
1111                 }
1112                 return Optional.absent();
1113         }
1114
1115         /**
1116          * Returns the extra 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 extra channel, or {@link Optional#absent()} if no extra channel
1123          *         matching the given network and name was found
1124          */
1125         public Optional<Channel> getExtraChannel(Network network, String channelName) {
1126                 for (Channel channel : extraChannels) {
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          * Parses {@link Pack} information from the given message.
1136          *
1137          * @param message
1138          *              The message to parse pack information from
1139          * @return The parsed pack, or {@link Optional#absent()} if the message could
1140          *         not be parsed into a pack
1141          */
1142         private Optional<Pack> parsePack(String message) {
1143                 int squareOpen = message.indexOf('[');
1144                 int squareClose = message.indexOf(']', squareOpen);
1145                 if ((squareOpen == -1) && (squareClose == -1)) {
1146                         return Optional.absent();
1147                 }
1148                 String packSize = message.substring(squareOpen + 1, squareClose);
1149                 String packName = message.substring(message.lastIndexOf(' ') + 1);
1150                 String packIndex = message.substring(0, message.indexOf(' ')).substring(1);
1151                 return Optional.of(new Pack(packIndex, packSize, packName));
1152         }
1153
1154 }