X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fxdcc%2Fcore%2FCore.java;h=96555e39576c62adbfba5fac4d537ae4a10cb7cb;hb=2fb2343aea0a4c6d81fa1acb76e8ad85844f82b2;hp=c59c9d5592d4ecaadaba6647ce5867f0df055591;hpb=103a6342cdf0c852da2a9aef1d2129e51782d9ad;p=xudocci.git diff --git a/src/main/java/net/pterodactylus/xdcc/core/Core.java b/src/main/java/net/pterodactylus/xdcc/core/Core.java index c59c9d5..96555e3 100644 --- a/src/main/java/net/pterodactylus/xdcc/core/Core.java +++ b/src/main/java/net/pterodactylus/xdcc/core/Core.java @@ -17,7 +17,11 @@ package net.pterodactylus.xdcc.core; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -28,8 +32,11 @@ import java.util.logging.Logger; import net.pterodactylus.irc.Connection; import net.pterodactylus.irc.ConnectionBuilder; +import net.pterodactylus.irc.DccReceiver; +import net.pterodactylus.irc.event.ChannelJoined; import net.pterodactylus.irc.event.ChannelMessageReceived; import net.pterodactylus.irc.event.ConnectionEstablished; +import net.pterodactylus.irc.event.DccSendReceived; import net.pterodactylus.irc.util.MessageCleaner; import net.pterodactylus.irc.util.RandomNickname; import net.pterodactylus.xdcc.core.event.BotAdded; @@ -40,11 +47,12 @@ import net.pterodactylus.xdcc.data.Network; import net.pterodactylus.xdcc.data.Pack; import net.pterodactylus.xdcc.data.Server; -import com.beust.jcommander.internal.Maps; -import com.beust.jcommander.internal.Sets; import com.google.common.base.Optional; import com.google.common.collect.HashBasedTable; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import com.google.common.collect.Table; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; @@ -67,12 +75,18 @@ public class Core extends AbstractIdleService { /** The channels that should be monitored. */ private final Collection channels = Sets.newHashSet(); + /** The channels that are currentlymonitored. */ + private final Collection joinedChannels = Sets.newHashSet(); + /** The current network connections. */ private final Map networkConnections = Collections.synchronizedMap(Maps.newHashMap()); /** The currently known bots. */ private final Table networkBots = HashBasedTable.create(); + /** The current DCC receivers. */ + private final Collection dccReceivers = Sets.newHashSet(); + /** * Creates a new core. * @@ -85,6 +99,47 @@ public class Core extends AbstractIdleService { } // + // ACCESSORS + // + + /** + * Returns all configured channels. Due to various circumstances, configured + * channels might not actually be joined. + * + * @return All configured channels + */ + public Collection channels() { + return ImmutableSet.copyOf(channels); + } + + /** + * Returns all currently joined channels. + * + * @return All currently joined channels + */ + public Collection joinedChannels() { + return ImmutableSet.copyOf(joinedChannels); + } + + /** + * Returns all currently known bots. + * + * @return All currently known bots + */ + public Collection bots() { + return networkBots.values(); + } + + /** + * Returns the currently active DCC receivers. + * + * @return The currently active DCC receivers + */ + public Collection dccReceivers() { + return dccReceivers; + } + + // // ACTIONS // @@ -98,6 +153,27 @@ public class Core extends AbstractIdleService { channels.add(channel); } + /** + * Fetches the given pack from the given bot. + * + * @param bot + * The bot to fetch the pack from + * @param pack + * The pack to fetch + */ + public void fetch(Bot bot, Pack pack) { + Connection connection = networkConnections.get(bot.network()); + if (connection == null) { + return; + } + + try { + connection.sendMessage(bot.name(), "XDCC SEND " + pack.id()); + } catch (IOException ioe1) { + logger.log(Level.WARNING, "Could not send message to bot!", ioe1); + } + } + // // ABSTRACTIDLESERVICE METHODS // @@ -160,6 +236,30 @@ public class Core extends AbstractIdleService { } /** + * Shows a message when a channel was joined by us. + * + * @param channelJoined + * The channel joined event + */ + @Subscribe + public void channelJoined(ChannelJoined channelJoined) { + if (channelJoined.connection().isSource(channelJoined.client())) { + Optional network = getNetwork(channelJoined.connection()); + if (!network.isPresent()) { + return; + } + + Optional channel = getChannel(network.get(), channelJoined.channel()); + if (!channel.isPresent()) { + return; + } + + joinedChannels.add(channel.get()); + logger.info(String.format("Joined Channel %s on %s.", channelJoined.channel(), network.get().name())); + } + } + + /** * If a message on a channel is received, it is parsed for pack information * with is then added to a bot. * @@ -202,6 +302,25 @@ public class Core extends AbstractIdleService { logger.fine(String.format("Bot %s now has %d packs.", bot, bot.packs().size())); } + /** + * Starts a DCC download. + * + * @param dccSendReceived + * The DCC SEND event + */ + @Subscribe + public void dccSendReceived(DccSendReceived dccSendReceived) { + logger.info(String.format("Starting download of %s.", dccSendReceived.filename())); + try { + OutputStream fileOutputStream = new FileOutputStream(new File("/home/bombe/Temp", dccSendReceived.filename())); + DccReceiver dccReceiver = new DccReceiver(dccSendReceived.inetAddress(), dccSendReceived.port(), dccSendReceived.filename(), dccSendReceived.filesize(), fileOutputStream); + dccReceivers.add(dccReceiver); + dccReceiver.start(); + } catch (FileNotFoundException fnfe1) { + logger.log(Level.WARNING, "Could not open file for download!", fnfe1); + } + } + // // PRIVATE METHODS // @@ -225,6 +344,25 @@ public class Core extends AbstractIdleService { } /** + * Returns the configured channel for the given network and name. + * + * @param network + * The network the channel is located on + * @param channelName + * The name of the channel + * @return The configured channel, or {@link Optional#absent()} if no + * configured channel matching the given network and name was found + */ + public Optional getChannel(Network network, String channelName) { + for (Channel channel : channels) { + if (channel.network().equals(network) && (channel.name().equals(channelName))) { + return Optional.of(channel); + } + } + return Optional.absent(); + } + + /** * Parses {@link Pack} information from the given message. * * @param message