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