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.List;
29 import java.util.Map.Entry;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
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.ConnectionEstablished;
40 import net.pterodactylus.irc.event.DccDownloadFailed;
41 import net.pterodactylus.irc.event.DccDownloadFinished;
42 import net.pterodactylus.irc.event.DccSendReceived;
43 import net.pterodactylus.irc.event.PrivateMessageReceived;
44 import net.pterodactylus.irc.util.MessageCleaner;
45 import net.pterodactylus.irc.util.RandomNickname;
46 import net.pterodactylus.xdcc.core.event.BotAdded;
47 import net.pterodactylus.xdcc.core.event.CoreStarted;
48 import net.pterodactylus.xdcc.core.event.DownloadFailed;
49 import net.pterodactylus.xdcc.core.event.DownloadFinished;
50 import net.pterodactylus.xdcc.core.event.DownloadStarted;
51 import net.pterodactylus.xdcc.core.event.GenericMessage;
52 import net.pterodactylus.xdcc.core.event.MessageReceived;
53 import net.pterodactylus.xdcc.data.Bot;
54 import net.pterodactylus.xdcc.data.Channel;
55 import net.pterodactylus.xdcc.data.Download;
56 import net.pterodactylus.xdcc.data.Network;
57 import net.pterodactylus.xdcc.data.Pack;
58 import net.pterodactylus.xdcc.data.Server;
60 import com.google.common.base.Optional;
61 import com.google.common.collect.HashBasedTable;
62 import com.google.common.collect.ImmutableSet;
63 import com.google.common.collect.Lists;
64 import com.google.common.collect.Maps;
65 import com.google.common.collect.Sets;
66 import com.google.common.collect.Table;
67 import com.google.common.eventbus.EventBus;
68 import com.google.common.eventbus.Subscribe;
69 import com.google.common.io.Closeables;
70 import com.google.common.util.concurrent.AbstractIdleService;
71 import com.google.inject.Inject;
74 * The core of XDCC Downloader.
76 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
78 public class Core extends AbstractIdleService {
81 private static final Logger logger = Logger.getLogger(Core.class.getName());
84 private final EventBus eventBus;
86 /** The temporary directory to download files to. */
87 private final String temporaryDirectory;
89 /** The directory to move finished downloads to. */
90 private final String finalDirectory;
92 /** The channels that should be monitored. */
93 private final Collection<Channel> channels = Sets.newHashSet();
95 /** The channels that are currentlymonitored. */
96 private final Collection<Channel> joinedChannels = Sets.newHashSet();
98 /** The channels that are joined but not configured. */
99 private final Collection<Channel> extraChannels = Sets.newHashSet();
101 /** The current network connections. */
102 private final Map<Network, Connection> networkConnections = Collections.synchronizedMap(Maps.<Network, Connection>newHashMap());
104 /** The currently known bots. */
105 private final Table<Network, String, Bot> networkBots = HashBasedTable.create();
107 /** The current downloads. */
108 private final Map<String, Download> downloads = Maps.newHashMap();
110 /** The current DCC receivers. */
111 private final Collection<DccReceiver> dccReceivers = Lists.newArrayList();
114 * Creates a new core.
118 * @param temporaryDirectory
119 * The directory to download files to
120 * @param finalDirectory
121 * The directory to move finished files to
124 public Core(EventBus eventBus, String temporaryDirectory, String finalDirectory) {
125 this.eventBus = eventBus;
126 this.temporaryDirectory = temporaryDirectory;
127 this.finalDirectory = finalDirectory;
135 * Returns all configured channels. Due to various circumstances, configured
136 * channels might not actually be joined.
138 * @return All configured channels
140 public Collection<Channel> channels() {
141 return ImmutableSet.copyOf(channels);
145 * Returns all currently joined channels.
147 * @return All currently joined channels
149 public Collection<Channel> joinedChannels() {
150 return ImmutableSet.copyOf(joinedChannels);
154 * Returns all currently joined channels that are not configured.
156 * @return All currently joined but not configured channels
158 public Collection<Channel> extraChannels() {
159 return ImmutableSet.copyOf(extraChannels);
163 * Returns all currently known bots.
165 * @return All currently known bots
167 public Collection<Bot> bots() {
168 return networkBots.values();
172 * Returns the currently active DCC receivers.
174 * @return The currently active DCC receivers
176 public Collection<DccReceiver> dccReceivers() {
185 * Adds a channel to monitor.
188 * The channel to monitor
190 public void addChannel(Channel channel) {
191 channels.add(channel);
195 * Fetches the given pack from the given bot.
198 * The bot to fetch the pack from
202 public void fetch(Bot bot, Pack pack) {
203 Connection connection = networkConnections.get(bot.network());
204 if (connection == null) {
208 Download download = new Download(bot, pack);
209 downloads.put(pack.name(), download);
212 connection.sendMessage(bot.name(), "XDCC SEND " + pack.id());
213 } catch (IOException ioe1) {
214 logger.log(Level.WARNING, "Could not send message to bot!", ioe1);
219 // ABSTRACTIDLESERVICE METHODS
223 protected void startUp() {
224 for (Channel channel : channels) {
225 logger.info(String.format("Connecting to Channel %s on Network %s…", channel.name(), channel.network().name()));
226 if (!networkConnections.containsKey(channel.network())) {
227 /* select a random server. */
228 List<Server> servers = Lists.newArrayList(channel.network().servers());
229 Server server = servers.get((int) (Math.random() * servers.size()));
230 Connection connection = new ConnectionBuilder(eventBus).connect(server.hostname()).port(server.unencryptedPorts().iterator().next()).build();
231 connection.username(RandomNickname.get()).realName(RandomNickname.get());
232 networkConnections.put(channel.network(), connection);
237 /* notify listeners. */
238 eventBus.post(new CoreStarted(this));
242 protected void shutDown() {
250 * If a connection to a network has been established, the channels associated
251 * with this network are joined.
253 * @param connectionEstablished
254 * The connection established event
257 public void connectionEstablished(ConnectionEstablished connectionEstablished) {
259 /* get network for connection. */
260 Optional<Network> network = getNetwork(connectionEstablished.connection());
263 if (!network.isPresent()) {
267 /* join all channels on this network. */
268 for (Channel channel : channels) {
269 if (channel.network().equals(network.get())) {
271 connectionEstablished.connection().joinChannel(channel.name());
272 } catch (IOException ioe1) {
273 logger.log(Level.WARNING, String.format("Could not join %s on %s!", channel.name(), network.get().name()), ioe1);
280 * Shows a message when a channel was joined by us.
282 * @param channelJoined
283 * The channel joined event
286 public void channelJoined(ChannelJoined channelJoined) {
287 if (channelJoined.connection().isSource(channelJoined.client())) {
288 Optional<Network> network = getNetwork(channelJoined.connection());
289 if (!network.isPresent()) {
293 Optional<Channel> channel = getChannel(network.get(), channelJoined.channel());
294 if (!channel.isPresent()) {
295 /* it’s an extra channel. */
296 extraChannels.add(new Channel(network.get(), channelJoined.channel()));
297 logger.info(String.format("Joined extra Channel %s on %s.", channelJoined.channel(), network.get().name()));
301 joinedChannels.add(channel.get());
302 logger.info(String.format("Joined Channel %s on %s.", channelJoined.channel(), network.get().name()));
307 * Removes bots that leave a channel, or channels when it’s us that’s leaving.
310 * The channel left event
313 public void channelLeft(ChannelLeft channelLeft) {
314 Optional<Network> network = getNetwork(channelLeft.connection());
315 if (!network.isPresent()) {
319 Bot bot = networkBots.get(network.get(), channelLeft.client().nick().get());
321 /* maybe it was us? */
322 if (channelLeft.connection().isSource(channelLeft.client())) {
323 Optional<Channel> channel = getChannel(network.get(), channelLeft.channel());
324 if (!channel.isPresent()) {
325 /* maybe it was an extra channel? */
326 channel = getExtraChannel(network.get(), channelLeft.channel());
327 if (!channel.isPresent()) {
328 /* okay, whatever. */
332 extraChannels.remove(channel);
334 channels.remove(channel.get());
337 eventBus.post(new GenericMessage(String.format("Left Channel %s on %s.", channel.get().name(), channel.get().network().name())));
343 networkBots.remove(network.get(), channelLeft.client().nick().get());
347 * If a message on a channel is received, it is parsed for pack information
348 * with is then added to a bot.
350 * @param channelMessageReceived
351 * The channel message received event
354 public void channelMessageReceived(ChannelMessageReceived channelMessageReceived) {
355 String message = MessageCleaner.getDefaultInstance().clean(channelMessageReceived.message());
356 if (!message.startsWith("#")) {
357 /* most probably not a pack announcement. */
361 Optional<Network> network = getNetwork(channelMessageReceived.connection());
362 if (!network.isPresent()) {
363 /* message for unknown connection? */
367 /* parse pack information. */
368 Optional<Pack> pack = parsePack(message);
369 if (!pack.isPresent()) {
374 synchronized (networkBots) {
375 if (!networkBots.contains(network.get(), channelMessageReceived.source().nick().get())) {
376 bot = new Bot(network.get()).name(channelMessageReceived.source().nick().get());
377 networkBots.put(network.get(), channelMessageReceived.source().nick().get(), bot);
378 eventBus.post(new BotAdded(bot));
380 bot = networkBots.get(network.get(), channelMessageReceived.source().nick().get());
385 bot.addPack(pack.get());
386 logger.fine(String.format("Bot %s now has %d packs.", bot, bot.packs().size()));
390 * Forward all private messages to every console.
392 * @param privateMessageReceived
393 * The private message recevied event
396 public void privateMessageReceived(PrivateMessageReceived privateMessageReceived) {
397 eventBus.post(new MessageReceived(privateMessageReceived.source(), privateMessageReceived.message()));
401 * Starts a DCC download.
403 * @param dccSendReceived
407 public void dccSendReceived(DccSendReceived dccSendReceived) {
408 Optional<Network> network = getNetwork(dccSendReceived.connection());
409 if (!network.isPresent()) {
413 Download download = downloads.get(dccSendReceived.filename());
414 if (download == null) {
415 /* unknown download, ignore. */
419 logger.info(String.format("Starting download of %s.", dccSendReceived.filename()));
421 File outputFile = new File(temporaryDirectory, dccSendReceived.filename());
422 OutputStream fileOutputStream = new FileOutputStream(outputFile);
423 DccReceiver dccReceiver = new DccReceiver(eventBus, dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream);
424 download.filename(outputFile.getPath()).outputStream(fileOutputStream).dccReceiver(dccReceiver);
425 dccReceivers.add(dccReceiver);
427 eventBus.post(new DownloadStarted(download));
428 } catch (FileNotFoundException fnfe1) {
429 logger.log(Level.WARNING, "Could not open file for download!", fnfe1);
434 * Closes the output stream of the download and moves the file to the final
437 * @param dccDownloadFinished
438 * The DCC download finished event
441 public void dccDownloadFinished(DccDownloadFinished dccDownloadFinished) {
442 Download download = downloads.get(dccDownloadFinished.dccReceiver().filename());
443 if (download == null) {
444 /* probably shouldn’t happen. */
449 download.outputStream().close();
450 File file = new File(download.filename());
451 file.renameTo(new File(finalDirectory, download.pack().name()));
452 eventBus.post(new DownloadFinished(download));
453 dccReceivers.remove(dccDownloadFinished.dccReceiver());
454 downloads.remove(download);
455 } catch (IOException ioe1) {
456 /* TODO - handle all the errors. */
457 logger.log(Level.WARNING, String.format("Could not move file %s to directory %s.", download.filename(), finalDirectory), ioe1);
462 * Closes the output stream and notifies all listeners of the failure.
464 * @param dccDownloadFailed
465 * The DCC download failed event
468 public void dccDownloadFailed(DccDownloadFailed dccDownloadFailed) {
469 Download download = downloads.get(dccDownloadFailed.dccReceiver().filename());
470 if (download == null) {
471 /* probably shouldn’t happen. */
476 Closeables.close(download.outputStream(), true);
477 eventBus.post(new DownloadFailed(download));
478 dccReceivers.remove(dccDownloadFailed.dccReceiver());
479 downloads.remove(download);
480 } catch (IOException ioe1) {
481 /* swallow silently. */
490 * Searches all current connections for the given connection, returning the
491 * associated network.
494 * The connection to get the network for
495 * @return The network belonging to the connection, or {@link
498 private Optional<Network> getNetwork(Connection connection) {
499 for (Entry<Network, Connection> networkConnectionEntry : networkConnections.entrySet()) {
500 if (networkConnectionEntry.getValue().equals(connection)) {
501 return Optional.of(networkConnectionEntry.getKey());
504 return Optional.absent();
508 * Returns the configured channel for the given network and name.
511 * The network the channel is located on
513 * The name of the channel
514 * @return The configured channel, or {@link Optional#absent()} if no
515 * configured channel matching the given network and name was found
517 public Optional<Channel> getChannel(Network network, String channelName) {
518 for (Channel channel : channels) {
519 if (channel.network().equals(network) && (channel.name().equalsIgnoreCase(channelName))) {
520 return Optional.of(channel);
523 return Optional.absent();
527 * Returns the extra channel for the given network and name.
530 * The network the channel is located on
532 * The name of the channel
533 * @return The extra channel, or {@link Optional#absent()} if no extra channel
534 * matching the given network and name was found
536 public Optional<Channel> getExtraChannel(Network network, String channelName) {
537 for (Channel channel : extraChannels) {
538 if (channel.network().equals(network) && (channel.name().equalsIgnoreCase(channelName))) {
539 return Optional.of(channel);
542 return Optional.absent();
546 * Parses {@link Pack} information from the given message.
549 * The message to parse pack information from
550 * @return The parsed pack, or {@link Optional#absent()} if the message could
551 * not be parsed into a pack
553 private Optional<Pack> parsePack(String message) {
554 int squareOpen = message.indexOf('[');
555 int squareClose = message.indexOf(']', squareOpen);
556 if ((squareOpen == -1) && (squareClose == -1)) {
557 return Optional.absent();
559 String packSize = message.substring(squareOpen + 1, squareClose);
560 String packName = message.substring(message.lastIndexOf(' ') + 1);
561 String packIndex = message.substring(0, message.indexOf(' ')).substring(1);
562 return Optional.of(new Pack(packIndex, packSize, packName));