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