Show a message when a bot has left a channel.
[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 java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
32
33 import net.pterodactylus.irc.Connection;
34 import net.pterodactylus.irc.ConnectionBuilder;
35 import net.pterodactylus.irc.DccReceiver;
36 import net.pterodactylus.irc.event.ChannelJoined;
37 import net.pterodactylus.irc.event.ChannelLeft;
38 import net.pterodactylus.irc.event.ChannelMessageReceived;
39 import net.pterodactylus.irc.event.ClientQuit;
40 import net.pterodactylus.irc.event.ConnectionEstablished;
41 import net.pterodactylus.irc.event.DccAcceptReceived;
42 import net.pterodactylus.irc.event.DccDownloadFailed;
43 import net.pterodactylus.irc.event.DccDownloadFinished;
44 import net.pterodactylus.irc.event.DccSendReceived;
45 import net.pterodactylus.irc.event.PrivateMessageReceived;
46 import net.pterodactylus.irc.util.MessageCleaner;
47 import net.pterodactylus.irc.util.RandomNickname;
48 import net.pterodactylus.xdcc.core.event.BotAdded;
49 import net.pterodactylus.xdcc.core.event.CoreStarted;
50 import net.pterodactylus.xdcc.core.event.DownloadFailed;
51 import net.pterodactylus.xdcc.core.event.DownloadFinished;
52 import net.pterodactylus.xdcc.core.event.DownloadStarted;
53 import net.pterodactylus.xdcc.core.event.GenericError;
54 import net.pterodactylus.xdcc.core.event.GenericMessage;
55 import net.pterodactylus.xdcc.core.event.MessageReceived;
56 import net.pterodactylus.xdcc.data.Bot;
57 import net.pterodactylus.xdcc.data.Channel;
58 import net.pterodactylus.xdcc.data.Download;
59 import net.pterodactylus.xdcc.data.Network;
60 import net.pterodactylus.xdcc.data.Pack;
61 import net.pterodactylus.xdcc.data.Server;
62
63 import com.google.common.base.Optional;
64 import com.google.common.collect.HashBasedTable;
65 import com.google.common.collect.ImmutableSet;
66 import com.google.common.collect.Lists;
67 import com.google.common.collect.Maps;
68 import com.google.common.collect.Sets;
69 import com.google.common.collect.Table;
70 import com.google.common.eventbus.EventBus;
71 import com.google.common.eventbus.Subscribe;
72 import com.google.common.io.Closeables;
73 import com.google.common.util.concurrent.AbstractIdleService;
74 import com.google.inject.Inject;
75
76 /**
77  * The core of XDCC Downloader.
78  *
79  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
80  */
81 public class Core extends AbstractIdleService {
82
83         /** The logger. */
84         private static final Logger logger = Logger.getLogger(Core.class.getName());
85
86         /** The event bus. */
87         private final EventBus eventBus;
88
89         /** The temporary directory to download files to. */
90         private final String temporaryDirectory;
91
92         /** The directory to move finished downloads to. */
93         private final String finalDirectory;
94
95         /** The channels that should be monitored. */
96         private final Collection<Channel> channels = Sets.newHashSet();
97
98         /** The channels that are currentlymonitored. */
99         private final Collection<Channel> joinedChannels = Sets.newHashSet();
100
101         /** The channels that are joined but not configured. */
102         private final Collection<Channel> extraChannels = Sets.newHashSet();
103
104         /** The current network connections. */
105         private final Map<Network, Connection> networkConnections = Collections.synchronizedMap(Maps.<Network, Connection>newHashMap());
106
107         /** The currently known bots. */
108         private final Table<Network, String, Bot> networkBots = HashBasedTable.create();
109
110         /** The current downloads. */
111         private final Map<String, Download> downloads = Maps.newHashMap();
112
113         /** The current DCC receivers. */
114         private final Collection<DccReceiver> dccReceivers = Lists.newArrayList();
115
116         /**
117          * Creates a new core.
118          *
119          * @param eventBus
120          *              The event bus
121          * @param temporaryDirectory
122          *              The directory to download files to
123          * @param finalDirectory
124          *              The directory to move finished files to
125          */
126         @Inject
127         public Core(EventBus eventBus, String temporaryDirectory, String finalDirectory) {
128                 this.eventBus = eventBus;
129                 this.temporaryDirectory = temporaryDirectory;
130                 this.finalDirectory = finalDirectory;
131         }
132
133         //
134         // ACCESSORS
135         //
136
137         /**
138          * Returns all configured channels. Due to various circumstances, configured
139          * channels might not actually be joined.
140          *
141          * @return All configured channels
142          */
143         public Collection<Channel> channels() {
144                 return ImmutableSet.copyOf(channels);
145         }
146
147         /**
148          * Returns all currently joined channels.
149          *
150          * @return All currently joined channels
151          */
152         public Collection<Channel> joinedChannels() {
153                 return ImmutableSet.copyOf(joinedChannels);
154         }
155
156         /**
157          * Returns all currently joined channels that are not configured.
158          *
159          * @return All currently joined but not configured channels
160          */
161         public Collection<Channel> extraChannels() {
162                 return ImmutableSet.copyOf(extraChannels);
163         }
164
165         /**
166          * Returns all currently known bots.
167          *
168          * @return All currently known bots
169          */
170         public Collection<Bot> bots() {
171                 return networkBots.values();
172         }
173
174         /**
175          * Returns the currently active DCC receivers.
176          *
177          * @return The currently active DCC receivers
178          */
179         public Collection<DccReceiver> dccReceivers() {
180                 return dccReceivers;
181         }
182
183         //
184         // ACTIONS
185         //
186
187         /**
188          * Adds a channel to monitor.
189          *
190          * @param channel
191          *              The channel to monitor
192          */
193         public void addChannel(Channel channel) {
194                 channels.add(channel);
195         }
196
197         /**
198          * Fetches the given pack from the given bot.
199          *
200          * @param bot
201          *              The bot to fetch the pack from
202          * @param pack
203          *              The pack to fetch
204          */
205         public void fetch(Bot bot, Pack pack) {
206                 Connection connection = networkConnections.get(bot.network());
207                 if (connection == null) {
208                         return;
209                 }
210
211                 Download download = new Download(bot, pack);
212                 downloads.put(pack.name(), download);
213
214                 try {
215                         connection.sendMessage(bot.name(), "XDCC SEND " + pack.id());
216                 } catch (IOException ioe1) {
217                         logger.log(Level.WARNING, "Could not send message to bot!", ioe1);
218                 }
219         }
220
221         //
222         // ABSTRACTIDLESERVICE METHODS
223         //
224
225         @Override
226         protected void startUp() {
227                 for (Channel channel : channels) {
228                         logger.info(String.format("Connecting to Channel %s on Network %s…", channel.name(), channel.network().name()));
229                         if (!networkConnections.containsKey(channel.network())) {
230                                 /* select a random server. */
231                                 List<Server> servers = Lists.newArrayList(channel.network().servers());
232                                 Server server = servers.get((int) (Math.random() * servers.size()));
233                                 Connection connection = new ConnectionBuilder(eventBus).connect(server.hostname()).port(server.unencryptedPorts().iterator().next()).build();
234                                 connection.username(RandomNickname.get()).realName(RandomNickname.get());
235                                 networkConnections.put(channel.network(), connection);
236                                 connection.start();
237                         }
238                 }
239
240                 /* notify listeners. */
241                 eventBus.post(new CoreStarted(this));
242         }
243
244         @Override
245         protected void shutDown() {
246         }
247
248         //
249         // EVENT HANDLERS
250         //
251
252         /**
253          * If a connection to a network has been established, the channels associated
254          * with this network are joined.
255          *
256          * @param connectionEstablished
257          *              The connection established event
258          */
259         @Subscribe
260         public void connectionEstablished(ConnectionEstablished connectionEstablished) {
261
262                 /* get network for connection. */
263                 Optional<Network> network = getNetwork(connectionEstablished.connection());
264
265                 /* found network? */
266                 if (!network.isPresent()) {
267                         return;
268                 }
269
270                 /* join all channels on this network. */
271                 for (Channel channel : channels) {
272                         if (channel.network().equals(network.get())) {
273                                 try {
274                                         connectionEstablished.connection().joinChannel(channel.name());
275                                 } catch (IOException ioe1) {
276                                         logger.log(Level.WARNING, String.format("Could not join %s on %s!", channel.name(), network.get().name()), ioe1);
277                                 }
278                         }
279                 }
280         }
281
282         /**
283          * Shows a message when a channel was joined by us.
284          *
285          * @param channelJoined
286          *              The channel joined event
287          */
288         @Subscribe
289         public void channelJoined(ChannelJoined channelJoined) {
290                 if (channelJoined.connection().isSource(channelJoined.client())) {
291                         Optional<Network> network = getNetwork(channelJoined.connection());
292                         if (!network.isPresent()) {
293                                 return;
294                         }
295
296                         Optional<Channel> channel = getChannel(network.get(), channelJoined.channel());
297                         if (!channel.isPresent()) {
298                                 /* it’s an extra channel. */
299                                 extraChannels.add(new Channel(network.get(), channelJoined.channel()));
300                                 logger.info(String.format("Joined extra Channel %s on %s.", channelJoined.channel(), network.get().name()));
301                                 return;
302                         }
303
304                         joinedChannels.add(channel.get());
305                         logger.info(String.format("Joined Channel %s on %s.", channelJoined.channel(), network.get().name()));
306                 }
307         }
308
309         /**
310          * Removes bots that leave a channel, or channels when it’s us that’s leaving.
311          *
312          * @param channelLeft
313          *              The channel left event
314          */
315         @Subscribe
316         public void channelLeft(ChannelLeft channelLeft) {
317                 Optional<Network> network = getNetwork(channelLeft.connection());
318                 if (!network.isPresent()) {
319                         return;
320                 }
321
322                 Bot bot = networkBots.get(network.get(), channelLeft.client().nick().get());
323                 if (bot == null) {
324                         /* maybe it was us? */
325                         if (channelLeft.connection().isSource(channelLeft.client())) {
326                                 Optional<Channel> channel = getChannel(network.get(), channelLeft.channel());
327                                 if (!channel.isPresent()) {
328                                         /* maybe it was an extra channel? */
329                                         channel = getExtraChannel(network.get(), channelLeft.channel());
330                                         if (!channel.isPresent()) {
331                                                 /* okay, whatever. */
332                                                 return;
333                                         }
334
335                                         extraChannels.remove(channel);
336                                 } else {
337                                         channels.remove(channel.get());
338                                 }
339
340                                 eventBus.post(new GenericMessage(String.format("Left Channel %s on %s.", channel.get().name(), channel.get().network().name())));
341                         }
342
343                         return;
344                 }
345
346                 Bot removedBot = networkBots.remove(network.get(), channelLeft.client().nick().get());
347                 if (removedBot != null) {
348                         eventBus.post(new GenericMessage(String.format("Bot %s (%s) was removed, %d packs removed.", removedBot.name(), removedBot.network().name(), removedBot.packs().size())));
349                 }
350         }
351
352         /**
353          * Removes a client (which may be a bot) from the table of known bots.
354          *
355          * @param clientQuit
356          *              The client quit event
357          */
358         @Subscribe
359         public void clientQuit(ClientQuit clientQuit) {
360                 Optional<Network> network = getNetwork(clientQuit.connection());
361                 if (!network.isPresent()) {
362                         return;
363                 }
364
365                 Bot removedBot = networkBots.remove(network.get(), clientQuit.client().nick().get());
366                 if (removedBot != null) {
367                         eventBus.post(new GenericMessage(String.format("Bot %s (%s) was removed, %d packs removed.", removedBot.name(), removedBot.network().name(), removedBot.packs().size())));
368                 }
369         }
370
371         /**
372          * If a message on a channel is received, it is parsed for pack information
373          * with is then added to a bot.
374          *
375          * @param channelMessageReceived
376          *              The channel message received event
377          */
378         @Subscribe
379         public void channelMessageReceived(ChannelMessageReceived channelMessageReceived) {
380                 String message = MessageCleaner.getDefaultInstance().clean(channelMessageReceived.message());
381                 if (!message.startsWith("#")) {
382                         /* most probably not a pack announcement. */
383                         return;
384                 }
385
386                 Optional<Network> network = getNetwork(channelMessageReceived.connection());
387                 if (!network.isPresent()) {
388                         /* message for unknown connection? */
389                         return;
390                 }
391
392                 /* parse pack information. */
393                 Optional<Pack> pack = parsePack(message);
394                 if (!pack.isPresent()) {
395                         return;
396                 }
397
398                 Bot bot;
399                 synchronized (networkBots) {
400                         if (!networkBots.contains(network.get(), channelMessageReceived.source().nick().get())) {
401                                 bot = new Bot(network.get()).name(channelMessageReceived.source().nick().get());
402                                 networkBots.put(network.get(), channelMessageReceived.source().nick().get(), bot);
403                                 eventBus.post(new BotAdded(bot));
404                         } else {
405                                 bot = networkBots.get(network.get(), channelMessageReceived.source().nick().get());
406                         }
407                 }
408
409                 /* add pack. */
410                 bot.addPack(pack.get());
411                 logger.fine(String.format("Bot %s now has %d packs.", bot, bot.packs().size()));
412         }
413
414         /**
415          * Forward all private messages to every console.
416          *
417          * @param privateMessageReceived
418          *              The private message recevied event
419          */
420         @Subscribe
421         public void privateMessageReceived(PrivateMessageReceived privateMessageReceived) {
422                 eventBus.post(new MessageReceived(privateMessageReceived.source(), privateMessageReceived.message()));
423         }
424
425         /**
426          * Starts a DCC download.
427          *
428          * @param dccSendReceived
429          *              The DCC SEND event
430          */
431         @Subscribe
432         public void dccSendReceived(DccSendReceived dccSendReceived) {
433                 Optional<Network> network = getNetwork(dccSendReceived.connection());
434                 if (!network.isPresent()) {
435                         return;
436                 }
437
438                 Download download = downloads.get(dccSendReceived.filename());
439                 if (download == null) {
440                         /* unknown download, ignore. */
441                         return;
442                 }
443
444                 /* check if the file already exists. */
445                 File outputFile = new File(temporaryDirectory, dccSendReceived.filename());
446                 if (outputFile.exists()) {
447                         long existingFileSize = outputFile.length();
448
449                         /* file already complete? */
450                         if ((dccSendReceived.filesize() > -1) && (existingFileSize >= dccSendReceived.filesize())) {
451                                 /* file is apparently already complete. just move it. */
452                                 if (outputFile.renameTo(new File(finalDirectory, download.pack().name()))) {
453                                         eventBus.post(new GenericMessage(String.format("File %s already downloaded.", download.pack().name())));
454                                 } else {
455                                         eventBus.post(new GenericMessage(String.format("File %s already downloaded but not moved to %s.", download.pack().name(), finalDirectory)));
456                                 }
457
458                                 /* remove download. */
459                                 downloads.remove(download);
460                                 return;
461                         }
462
463                         /* file not complete yet, DCC resume it. */
464                         try {
465                                 download.remoteAddress(dccSendReceived.inetAddress()).filesize(dccSendReceived.filesize());
466                                 dccSendReceived.connection().sendDccResume(dccSendReceived.source().nick().get(), dccSendReceived.filename(), dccSendReceived.port(), existingFileSize);
467                         } catch (IOException ioe1) {
468                                 eventBus.post(new GenericError(String.format("Could not send DCC RESUME %s to %s (%s).", dccSendReceived.filename(), dccSendReceived.source().nick().get(), ioe1.getMessage())));
469                         }
470
471                         return;
472                 }
473
474                 /* file does not exist, start the download. */
475                 try {
476                         OutputStream fileOutputStream = new FileOutputStream(outputFile);
477                         DccReceiver dccReceiver = new DccReceiver(eventBus, dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream);
478                         download.filename(outputFile.getPath()).outputStream(fileOutputStream).dccReceiver(dccReceiver);
479                         dccReceivers.add(dccReceiver);
480                         dccReceiver.start();
481                         eventBus.post(new DownloadStarted(download));
482                 } catch (FileNotFoundException fnfe1) {
483                         eventBus.post(new GenericError(String.format("Could not start download of %s from %s (%s).", dccSendReceived.filename(), dccSendReceived.source().nick().get(), fnfe1.getMessage())));
484                 }
485         }
486
487         @Subscribe
488         public void dccAcceptReceived(DccAcceptReceived dccAcceptReceived) {
489                 Optional<Network> network = getNetwork(dccAcceptReceived.connection());
490                 if (!network.isPresent()) {
491                         return;
492                 }
493
494                 Download download = downloads.get(dccAcceptReceived.filename());
495                 if (download == null) {
496                         /* unknown download, ignore. */
497                         return;
498                 }
499
500                 try {
501                         File outputFile = new File(temporaryDirectory, dccAcceptReceived.filename());
502                         if (outputFile.length() != dccAcceptReceived.position()) {
503                                 eventBus.post(new GenericError(String.format("Download %s from %s does not start at the right position!")));
504                                 logger.log(Level.WARNING, String.format("Download %s from %s: have %d bytes but wants to resume from %d!", dccAcceptReceived.filename(), dccAcceptReceived.source(), outputFile.length(), dccAcceptReceived.position()));
505
506                                 downloads.remove(download);
507                                 return;
508                         }
509                         OutputStream outputStream = new FileOutputStream(outputFile, true);
510                         DccReceiver dccReceiver = new DccReceiver(eventBus, download.remoteAddress(), dccAcceptReceived.port(), dccAcceptReceived.filename(), dccAcceptReceived.position(), download.filesize(), outputStream);
511                         download.filename(outputFile.getPath()).outputStream(outputStream).dccReceiver(dccReceiver);
512                         dccReceivers.add(dccReceiver);
513                         dccReceiver.start();
514                         eventBus.post(new DownloadStarted(download));
515                 } catch (FileNotFoundException fnfe1) {
516                 }
517         }
518
519         /**
520          * Closes the output stream of the download and moves the file to the final
521          * location.
522          *
523          * @param dccDownloadFinished
524          *              The DCC download finished event
525          */
526         @Subscribe
527         public void dccDownloadFinished(DccDownloadFinished dccDownloadFinished) {
528                 Download download = downloads.get(dccDownloadFinished.dccReceiver().filename());
529                 if (download == null) {
530                         /* probably shouldn’t happen. */
531                         return;
532                 }
533
534                 try {
535                         download.outputStream().close();
536                         File file = new File(download.filename());
537                         file.renameTo(new File(finalDirectory, download.pack().name()));
538                         eventBus.post(new DownloadFinished(download));
539                         dccReceivers.remove(dccDownloadFinished.dccReceiver());
540                         downloads.remove(download);
541                 } catch (IOException ioe1) {
542                         /* TODO - handle all the errors. */
543                         logger.log(Level.WARNING, String.format("Could not move file %s to directory %s.", download.filename(), finalDirectory), ioe1);
544                 }
545         }
546
547         /**
548          * Closes the output stream and notifies all listeners of the failure.
549          *
550          * @param dccDownloadFailed
551          *              The DCC download failed event
552          */
553         @Subscribe
554         public void dccDownloadFailed(DccDownloadFailed dccDownloadFailed) {
555                 Download download = downloads.get(dccDownloadFailed.dccReceiver().filename());
556                 if (download == null) {
557                         /* probably shouldn’t happen. */
558                         return;
559                 }
560
561                 try {
562                         Closeables.close(download.outputStream(), true);
563                         eventBus.post(new DownloadFailed(download));
564                         dccReceivers.remove(dccDownloadFailed.dccReceiver());
565                         downloads.remove(download);
566                 } catch (IOException ioe1) {
567                         /* swallow silently. */
568                 }
569         }
570
571         //
572         // PRIVATE METHODS
573         //
574
575         /**
576          * Searches all current connections for the given connection, returning the
577          * associated network.
578          *
579          * @param connection
580          *              The connection to get the network for
581          * @return The network belonging to the connection, or {@link
582          *         Optional#absent()}
583          */
584         private Optional<Network> getNetwork(Connection connection) {
585                 for (Entry<Network, Connection> networkConnectionEntry : networkConnections.entrySet()) {
586                         if (networkConnectionEntry.getValue().equals(connection)) {
587                                 return Optional.of(networkConnectionEntry.getKey());
588                         }
589                 }
590                 return Optional.absent();
591         }
592
593         /**
594          * Returns the configured channel for the given network and name.
595          *
596          * @param network
597          *              The network the channel is located on
598          * @param channelName
599          *              The name of the channel
600          * @return The configured channel, or {@link Optional#absent()} if no
601          *         configured channel matching the given network and name was found
602          */
603         public Optional<Channel> getChannel(Network network, String channelName) {
604                 for (Channel channel : channels) {
605                         if (channel.network().equals(network) && (channel.name().equalsIgnoreCase(channelName))) {
606                                 return Optional.of(channel);
607                         }
608                 }
609                 return Optional.absent();
610         }
611
612         /**
613          * Returns the extra channel for the given network and name.
614          *
615          * @param network
616          *              The network the channel is located on
617          * @param channelName
618          *              The name of the channel
619          * @return The extra channel, or {@link Optional#absent()} if no extra channel
620          *         matching the given network and name was found
621          */
622         public Optional<Channel> getExtraChannel(Network network, String channelName) {
623                 for (Channel channel : extraChannels) {
624                         if (channel.network().equals(network) && (channel.name().equalsIgnoreCase(channelName))) {
625                                 return Optional.of(channel);
626                         }
627                 }
628                 return Optional.absent();
629         }
630
631         /**
632          * Parses {@link Pack} information from the given message.
633          *
634          * @param message
635          *              The message to parse pack information from
636          * @return The parsed pack, or {@link Optional#absent()} if the message could
637          *         not be parsed into a pack
638          */
639         private Optional<Pack> parsePack(String message) {
640                 int squareOpen = message.indexOf('[');
641                 int squareClose = message.indexOf(']', squareOpen);
642                 if ((squareOpen == -1) && (squareClose == -1)) {
643                         return Optional.absent();
644                 }
645                 String packSize = message.substring(squareOpen + 1, squareClose);
646                 String packName = message.substring(message.lastIndexOf(' ') + 1);
647                 String packIndex = message.substring(0, message.indexOf(' ')).substring(1);
648                 return Optional.of(new Pack(packIndex, packSize, packName));
649         }
650
651 }