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