2 * XdccDownloader - Core.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.xdcc.core;
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.Iterator;
28 import java.util.List;
30 import java.util.Map.Entry;
31 import java.util.concurrent.TimeUnit;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
35 import net.pterodactylus.irc.Connection;
36 import net.pterodactylus.irc.ConnectionBuilder;
37 import net.pterodactylus.irc.DccReceiver;
38 import net.pterodactylus.irc.event.ChannelJoined;
39 import net.pterodactylus.irc.event.ChannelLeft;
40 import net.pterodactylus.irc.event.ChannelMessageReceived;
41 import net.pterodactylus.irc.event.ClientQuit;
42 import net.pterodactylus.irc.event.ConnectionClosed;
43 import net.pterodactylus.irc.event.ConnectionEstablished;
44 import net.pterodactylus.irc.event.DccAcceptReceived;
45 import net.pterodactylus.irc.event.DccDownloadFailed;
46 import net.pterodactylus.irc.event.DccDownloadFinished;
47 import net.pterodactylus.irc.event.DccSendReceived;
48 import net.pterodactylus.irc.event.NicknameChanged;
49 import net.pterodactylus.irc.event.PrivateNoticeReceived;
50 import net.pterodactylus.irc.event.PrivateMessageReceived;
51 import net.pterodactylus.irc.util.MessageCleaner;
52 import net.pterodactylus.irc.util.RandomNickname;
53 import net.pterodactylus.xdcc.core.event.BotAdded;
54 import net.pterodactylus.xdcc.core.event.CoreStarted;
55 import net.pterodactylus.xdcc.core.event.DownloadFailed;
56 import net.pterodactylus.xdcc.core.event.DownloadFinished;
57 import net.pterodactylus.xdcc.core.event.DownloadStarted;
58 import net.pterodactylus.xdcc.core.event.GenericError;
59 import net.pterodactylus.xdcc.core.event.GenericMessage;
60 import net.pterodactylus.xdcc.core.event.MessageReceived;
61 import net.pterodactylus.xdcc.data.Bot;
62 import net.pterodactylus.xdcc.data.Channel;
63 import net.pterodactylus.xdcc.data.Download;
64 import net.pterodactylus.xdcc.data.Network;
65 import net.pterodactylus.xdcc.data.Pack;
66 import net.pterodactylus.xdcc.data.Server;
68 import com.google.common.base.Optional;
69 import com.google.common.collect.HashBasedTable;
70 import com.google.common.collect.ImmutableList;
71 import com.google.common.collect.ImmutableSet;
72 import com.google.common.collect.Lists;
73 import com.google.common.collect.Maps;
74 import com.google.common.collect.Sets;
75 import com.google.common.collect.Table;
76 import com.google.common.eventbus.EventBus;
77 import com.google.common.eventbus.Subscribe;
78 import com.google.common.io.Closeables;
79 import com.google.common.util.concurrent.AbstractExecutionThreadService;
80 import com.google.inject.Inject;
83 * The core of XDCC Downloader.
85 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
87 public class Core extends AbstractExecutionThreadService {
90 private static final Logger logger = Logger.getLogger(Core.class.getName());
93 private final EventBus eventBus;
95 /** The temporary directory to download files to. */
96 private final String temporaryDirectory;
98 /** The directory to move finished downloads to. */
99 private final String finalDirectory;
101 /** The channels that should be monitored. */
102 private final Collection<Channel> channels = Sets.newHashSet();
104 /** The channels that are currentlymonitored. */
105 private final Collection<Channel> joinedChannels = Sets.newHashSet();
107 /** The channels that are joined but not configured. */
108 private final Collection<Channel> extraChannels = Sets.newHashSet();
110 /** The current network connections. */
111 private final Map<Network, Connection> networkConnections = Collections.synchronizedMap(Maps.<Network, Connection>newHashMap());
113 /** The currently known bots. */
114 private final Table<Network, String, Bot> networkBots = HashBasedTable.create();
116 /** The current downloads. */
117 private final Map<String, Download> downloads = Maps.newHashMap();
119 /** The current DCC receivers. */
120 private final Collection<DccReceiver> dccReceivers = Lists.newArrayList();
123 * Creates a new core.
127 * @param temporaryDirectory
128 * The directory to download files to
129 * @param finalDirectory
130 * The directory to move finished files to
133 public Core(EventBus eventBus, String temporaryDirectory, String finalDirectory) {
134 this.eventBus = eventBus;
135 this.temporaryDirectory = temporaryDirectory;
136 this.finalDirectory = finalDirectory;
144 * Returns all configured channels. Due to various circumstances, configured
145 * channels might not actually be joined.
147 * @return All configured channels
149 public Collection<Channel> channels() {
150 return ImmutableSet.copyOf(channels);
154 * Returns all currently joined channels.
156 * @return All currently joined channels
158 public Collection<Channel> joinedChannels() {
159 return ImmutableSet.copyOf(joinedChannels);
163 * Returns all currently joined channels that are not configured.
165 * @return All currently joined but not configured channels
167 public Collection<Channel> extraChannels() {
168 return ImmutableSet.copyOf(extraChannels);
172 * Returns all currently known bots.
174 * @return All currently known bots
176 public Collection<Bot> bots() {
177 return networkBots.values();
181 * Returns all currently running downloads.
183 * @return All currently running downloads
185 public Collection<Download> downloads() {
186 return downloads.values();
194 * Adds a channel to monitor.
197 * The channel to monitor
199 public void addChannel(Channel channel) {
200 channels.add(channel);
204 * Fetches the given pack from the given bot.
207 * The bot to fetch the pack from
211 public void fetch(Bot bot, Pack pack) {
212 Connection connection = networkConnections.get(bot.network());
213 if (connection == null) {
217 Download download = new Download(bot, pack);
218 downloads.put(pack.name(), download);
221 connection.sendMessage(bot.name(), "XDCC SEND " + pack.id());
222 } catch (IOException ioe1) {
223 logger.log(Level.WARNING, "Could not send message to bot!", ioe1);
228 // ABSTRACTIDLESERVICE METHODS
232 protected void startUp() {
233 for (Channel channel : channels) {
234 logger.info(String.format("Connecting to Channel %s on Network %s…", channel.name(), channel.network().name()));
235 connectNetwork(channel.network());
238 /* notify listeners. */
239 eventBus.post(new CoreStarted(this));
243 protected void run() throws Exception {
244 while (isRunning()) {
246 Thread.sleep(TimeUnit.MINUTES.toMillis(1));
247 } catch (InterruptedException ie1) {
251 /* find channels that should be monitored but are not. */
252 for (Channel channel : channels) {
253 if (joinedChannels.contains(channel)) {
257 connectNetwork(channel.network());
258 Connection connection = networkConnections.get(channel.network());
259 if (connection.established()) {
260 eventBus.post(new GenericMessage(String.format("Trying to join %s on %s.", channel.name(), channel.network().name())));
261 connection.joinChannel(channel.name());
268 protected void shutDown() {
276 * Starts a new connection for the given network if no such connection exists
280 * The network to connect to
282 private void connectNetwork(Network network) {
283 if (!networkConnections.containsKey(network)) {
284 /* select a random server. */
285 List<Server> servers = Lists.newArrayList(network.servers());
286 if (servers.isEmpty()) {
287 eventBus.post(new GenericError(String.format("Network %s does not have any servers.", network.name())));
290 Server server = servers.get((int) (Math.random() * servers.size()));
291 Connection connection = new ConnectionBuilder(eventBus).connect(server.hostname()).port(server.unencryptedPorts().iterator().next()).build();
292 connection.username(RandomNickname.get()).realName(RandomNickname.get());
293 networkConnections.put(network, connection);
303 * If a connection to a network has been established, the channels associated
304 * with this network are joined.
306 * @param connectionEstablished
307 * The connection established event
310 public void connectionEstablished(ConnectionEstablished connectionEstablished) {
312 /* get network for connection. */
313 Optional<Network> network = getNetwork(connectionEstablished.connection());
316 if (!network.isPresent()) {
320 /* join all channels on this network. */
321 for (Channel channel : channels) {
322 if (channel.network().equals(network.get())) {
324 connectionEstablished.connection().joinChannel(channel.name());
325 } catch (IOException ioe1) {
326 logger.log(Level.WARNING, String.format("Could not join %s on %s!", channel.name(), network.get().name()), ioe1);
333 * Remove all data stored for a network if the connection is closed.
335 * @param connectionClosed
336 * The connection closed event
339 public void connectionClosed(ConnectionClosed connectionClosed) {
340 Optional<Network> network = getNetwork(connectionClosed.connection());
341 if (!network.isPresent()) {
345 /* find all channels that need to be removed. */
346 for (Collection channels : ImmutableList.of(joinedChannels, extraChannels)) {
347 for (Iterator<Channel> channelIterator = channels.iterator(); channelIterator.hasNext(); ) {
348 Channel joinedChannel = channelIterator.next();
349 if (!joinedChannel.network().equals(network.get())) {
353 channelIterator.remove();
357 /* now remove all bots for that network. */
358 Map<String, Bot> bots = networkBots.row(network.get());
359 int botCount = bots.size();
361 for (Bot bot : bots.values()) {
362 packCount += bot.packs().size();
365 eventBus.post(new GenericMessage(String.format("Network %s disconnected, %d bots removed, %d packs removed.", network.get().name(), botCount, packCount)));
367 /* now remove the network. */
368 networkConnections.remove(network.get());
372 * Shows a message when a channel was joined by us.
374 * @param channelJoined
375 * The channel joined event
378 public void channelJoined(ChannelJoined channelJoined) {
379 if (channelJoined.connection().isSource(channelJoined.client())) {
380 Optional<Network> network = getNetwork(channelJoined.connection());
381 if (!network.isPresent()) {
385 Optional<Channel> channel = getChannel(network.get(), channelJoined.channel());
386 if (!channel.isPresent()) {
387 /* it’s an extra channel. */
388 extraChannels.add(new Channel(network.get(), channelJoined.channel()));
389 logger.info(String.format("Joined extra Channel %s on %s.", channelJoined.channel(), network.get().name()));
393 joinedChannels.add(channel.get());
394 logger.info(String.format("Joined Channel %s on %s.", channelJoined.channel(), network.get().name()));
399 * Removes bots that leave a channel, or channels when it’s us that’s leaving.
402 * The channel left event
405 public void channelLeft(ChannelLeft channelLeft) {
406 Optional<Network> network = getNetwork(channelLeft.connection());
407 if (!network.isPresent()) {
411 Bot bot = networkBots.get(network.get(), channelLeft.client().nick().get());
413 /* maybe it was us? */
414 if (channelLeft.connection().isSource(channelLeft.client())) {
415 Optional<Channel> channel = getChannel(network.get(), channelLeft.channel());
416 if (!channel.isPresent()) {
417 /* maybe it was an extra channel? */
418 channel = getExtraChannel(network.get(), channelLeft.channel());
419 if (!channel.isPresent()) {
420 /* okay, whatever. */
424 extraChannels.remove(channel);
426 channels.remove(channel.get());
429 eventBus.post(new GenericMessage(String.format("Left Channel %s on %s.", channel.get().name(), channel.get().network().name())));
435 Bot removedBot = networkBots.remove(network.get(), channelLeft.client().nick().get());
436 if (removedBot != null) {
437 eventBus.post(new GenericMessage(String.format("Bot %s (%s) was removed, %d packs removed.", removedBot.name(), removedBot.network().name(), removedBot.packs().size())));
442 * Removes a client (which may be a bot) from the table of known bots.
445 * The client quit event
448 public void clientQuit(ClientQuit clientQuit) {
449 Optional<Network> network = getNetwork(clientQuit.connection());
450 if (!network.isPresent()) {
454 Bot removedBot = networkBots.remove(network.get(), clientQuit.client().nick().get());
455 if (removedBot != null) {
456 eventBus.post(new GenericMessage(String.format("Bot %s (%s) was removed, %d packs removed.", removedBot.name(), removedBot.network().name(), removedBot.packs().size())));
461 * If the nickname of a bit changes, remove it from the old name and store it
462 * under the new name.
464 * @param nicknameChanged
465 * The nickname changed event
468 public void nicknameChanged(NicknameChanged nicknameChanged) {
469 Optional<Network> network = getNetwork(nicknameChanged.connection());
470 if (!network.isPresent()) {
474 Bot bot = networkBots.remove(network.get(), nicknameChanged.client().nick().get());
479 networkBots.put(network.get(), nicknameChanged.newNickname(), bot);
483 * If a message on a channel is received, it is parsed for pack information
484 * with is then added to a bot.
486 * @param channelMessageReceived
487 * The channel message received event
490 public void channelMessageReceived(ChannelMessageReceived channelMessageReceived) {
491 String message = MessageCleaner.getDefaultInstance().clean(channelMessageReceived.message());
492 if (!message.startsWith("#")) {
493 /* most probably not a pack announcement. */
497 Optional<Network> network = getNetwork(channelMessageReceived.connection());
498 if (!network.isPresent()) {
499 /* message for unknown connection? */
503 /* parse pack information. */
504 Optional<Pack> pack = parsePack(message);
505 if (!pack.isPresent()) {
510 synchronized (networkBots) {
511 if (!networkBots.contains(network.get(), channelMessageReceived.source().nick().get())) {
512 bot = new Bot(network.get()).name(channelMessageReceived.source().nick().get());
513 networkBots.put(network.get(), channelMessageReceived.source().nick().get(), bot);
514 eventBus.post(new BotAdded(bot));
516 bot = networkBots.get(network.get(), channelMessageReceived.source().nick().get());
521 bot.addPack(pack.get());
522 logger.fine(String.format("Bot %s now has %d packs.", bot, bot.packs().size()));
526 * Forward all private messages to every console.
528 * @param privateMessageReceived
529 * The private message recevied event
532 public void privateMessageReceived(PrivateMessageReceived privateMessageReceived) {
533 eventBus.post(new MessageReceived(privateMessageReceived.source(), privateMessageReceived.message()));
537 * Sends a message to all console when a notice was received.
539 * @param privateNoticeReceived
540 * The notice received event
543 public void privateNoticeReceived(PrivateNoticeReceived privateNoticeReceived) {
544 Optional<Network> network = getNetwork(privateNoticeReceived.connection());
545 if (!network.isPresent()) {
549 eventBus.post(new GenericMessage(String.format("Notice from %s (%s): %s", privateNoticeReceived.reply().source(), network.get(), privateNoticeReceived.text())));
553 * Starts a DCC download.
555 * @param dccSendReceived
559 public void dccSendReceived(DccSendReceived dccSendReceived) {
560 Optional<Network> network = getNetwork(dccSendReceived.connection());
561 if (!network.isPresent()) {
565 Download download = downloads.get(dccSendReceived.filename());
566 if (download == null) {
567 /* unknown download, ignore. */
571 /* check if the file already exists. */
572 File outputFile = new File(temporaryDirectory, dccSendReceived.filename());
573 if (outputFile.exists()) {
574 long existingFileSize = outputFile.length();
576 /* file already complete? */
577 if ((dccSendReceived.filesize() > -1) && (existingFileSize >= dccSendReceived.filesize())) {
578 /* file is apparently already complete. just move it. */
579 if (outputFile.renameTo(new File(finalDirectory, download.pack().name()))) {
580 eventBus.post(new GenericMessage(String.format("File %s already downloaded.", download.pack().name())));
582 eventBus.post(new GenericMessage(String.format("File %s already downloaded but not moved to %s.", download.pack().name(), finalDirectory)));
585 /* remove download. */
586 downloads.remove(download.pack().name());
590 /* file not complete yet, DCC resume it. */
592 download.remoteAddress(dccSendReceived.inetAddress()).filesize(dccSendReceived.filesize());
593 dccSendReceived.connection().sendDccResume(dccSendReceived.source().nick().get(), dccSendReceived.filename(), dccSendReceived.port(), existingFileSize);
594 } catch (IOException ioe1) {
595 eventBus.post(new GenericError(String.format("Could not send DCC RESUME %s to %s (%s).", dccSendReceived.filename(), dccSendReceived.source().nick().get(), ioe1.getMessage())));
601 /* file does not exist, start the download. */
603 OutputStream fileOutputStream = new FileOutputStream(outputFile);
604 DccReceiver dccReceiver = new DccReceiver(eventBus, dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream);
605 download.filename(outputFile.getPath()).outputStream(fileOutputStream).dccReceiver(dccReceiver);
606 dccReceivers.add(dccReceiver);
608 eventBus.post(new DownloadStarted(download));
609 } catch (FileNotFoundException fnfe1) {
610 eventBus.post(new GenericError(String.format("Could not start download of %s from %s (%s).", dccSendReceived.filename(), dccSendReceived.source().nick().get(), fnfe1.getMessage())));
615 public void dccAcceptReceived(DccAcceptReceived dccAcceptReceived) {
616 Optional<Network> network = getNetwork(dccAcceptReceived.connection());
617 if (!network.isPresent()) {
621 Download download = downloads.get(dccAcceptReceived.filename());
622 if (download == null) {
623 /* unknown download, ignore. */
628 File outputFile = new File(temporaryDirectory, dccAcceptReceived.filename());
629 if (outputFile.length() != dccAcceptReceived.position()) {
630 eventBus.post(new GenericError(String.format("Download %s from %s does not start at the right position!")));
631 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()));
633 downloads.remove(download.pack().name());
636 OutputStream outputStream = new FileOutputStream(outputFile, true);
637 DccReceiver dccReceiver = new DccReceiver(eventBus, download.remoteAddress(), dccAcceptReceived.port(), dccAcceptReceived.filename(), dccAcceptReceived.position(), download.filesize(), outputStream);
638 download.filename(outputFile.getPath()).outputStream(outputStream).dccReceiver(dccReceiver);
639 dccReceivers.add(dccReceiver);
641 eventBus.post(new DownloadStarted(download));
642 } catch (FileNotFoundException fnfe1) {
647 * Closes the output stream of the download and moves the file to the final
650 * @param dccDownloadFinished
651 * The DCC download finished event
654 public void dccDownloadFinished(DccDownloadFinished dccDownloadFinished) {
655 Download download = downloads.get(dccDownloadFinished.dccReceiver().filename());
656 if (download == null) {
657 /* probably shouldn’t happen. */
662 download.outputStream().close();
663 File file = new File(download.filename());
664 file.renameTo(new File(finalDirectory, download.pack().name()));
665 eventBus.post(new DownloadFinished(download));
666 dccReceivers.remove(dccDownloadFinished.dccReceiver());
667 downloads.remove(download.pack().name());
668 } catch (IOException ioe1) {
669 /* TODO - handle all the errors. */
670 logger.log(Level.WARNING, String.format("Could not move file %s to directory %s.", download.filename(), finalDirectory), ioe1);
675 * Closes the output stream and notifies all listeners of the failure.
677 * @param dccDownloadFailed
678 * The DCC download failed event
681 public void dccDownloadFailed(DccDownloadFailed dccDownloadFailed) {
682 Download download = downloads.get(dccDownloadFailed.dccReceiver().filename());
683 if (download == null) {
684 /* probably shouldn’t happen. */
689 Closeables.close(download.outputStream(), true);
690 eventBus.post(new DownloadFailed(download));
691 dccReceivers.remove(dccDownloadFailed.dccReceiver());
692 downloads.remove(download.pack().name());
693 } catch (IOException ioe1) {
694 /* swallow silently. */
703 * Searches all current connections for the given connection, returning the
704 * associated network.
707 * The connection to get the network for
708 * @return The network belonging to the connection, or {@link
711 private Optional<Network> getNetwork(Connection connection) {
712 for (Entry<Network, Connection> networkConnectionEntry : networkConnections.entrySet()) {
713 if (networkConnectionEntry.getValue().equals(connection)) {
714 return Optional.of(networkConnectionEntry.getKey());
717 return Optional.absent();
721 * Returns the configured channel for the given network and name.
724 * The network the channel is located on
726 * The name of the channel
727 * @return The configured channel, or {@link Optional#absent()} if no
728 * configured channel matching the given network and name was found
730 public Optional<Channel> getChannel(Network network, String channelName) {
731 for (Channel channel : channels) {
732 if (channel.network().equals(network) && (channel.name().equalsIgnoreCase(channelName))) {
733 return Optional.of(channel);
736 return Optional.absent();
740 * Returns the extra channel for the given network and name.
743 * The network the channel is located on
745 * The name of the channel
746 * @return The extra channel, or {@link Optional#absent()} if no extra channel
747 * matching the given network and name was found
749 public Optional<Channel> getExtraChannel(Network network, String channelName) {
750 for (Channel channel : extraChannels) {
751 if (channel.network().equals(network) && (channel.name().equalsIgnoreCase(channelName))) {
752 return Optional.of(channel);
755 return Optional.absent();
759 * Parses {@link Pack} information from the given message.
762 * The message to parse pack information from
763 * @return The parsed pack, or {@link Optional#absent()} if the message could
764 * not be parsed into a pack
766 private Optional<Pack> parsePack(String message) {
767 int squareOpen = message.indexOf('[');
768 int squareClose = message.indexOf(']', squareOpen);
769 if ((squareOpen == -1) && (squareClose == -1)) {
770 return Optional.absent();
772 String packSize = message.substring(squareOpen + 1, squareClose);
773 String packName = message.substring(message.lastIndexOf(' ') + 1);
774 String packIndex = message.substring(0, message.indexOf(' ')).substring(1);
775 return Optional.of(new Pack(packIndex, packSize, packName));