package net.pterodactylus.xdcc.core;
import static java.lang.String.format;
+import static java.util.stream.Collectors.toSet;
import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.banned;
import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.inviteOnly;
import static net.pterodactylus.irc.event.ChannelNotJoined.Reason.registeredNicknamesOnly;
import static net.pterodactylus.irc.util.MessageCleaner.getDefaultInstance;
-import static net.pterodactylus.xdcc.data.Channel.TO_NETWORK;
import static net.pterodactylus.xdcc.data.Download.FILTER_RUNNING;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
* @return All defined networks
*/
public Collection<Network> networks() {
- return FluentIterable.from(channels).transform(TO_NETWORK).toSet();
+ return FluentIterable.from(channels).transform(Channel::network).toSet();
}
/**
downloads.put(pack.name(), download);
try {
+ connection.sendMessage(bot.name(), "XDCC OPTION +ACTIVE");
connection.sendMessage(bot.name(), "XDCC SEND " + pack.id());
} catch (IOException ioe1) {
logger.warn("Could not send message to bot!", ioe1);
.map(Channel::network)
.distinct()
.filter((network) -> !networkConnections.containsKey(network))
- .collect(Collectors.toSet());
+ .collect(toSet());
if (missingNetworks.isEmpty()) {
if (!missingChannels.isEmpty()) {
* The connection to remove
*/
private void removeConnection(Connection connection) {
+ logger.debug(String.format("Removing Connection %s...", connection));
Optional<Network> network = getNetwork(connection);
if (!network.isPresent()) {
+ logger.debug(String.format("Connection %s did not belong to any network.", connection));
return;
}
+ logger.debug(String.format("Connection %s belongs to network %s.", connection, network.get()));
networkConnections.remove(network.get());
/* find all channels that need to be removed. */
if (!joinedChannel.network().equals(network.get())) {
continue;
}
-
+ logger.debug(String.format("Channel %s will be removed.", joinedChannel));
channelIterator.remove();
}
}
.values().stream()
.filter(bot -> bot.channel()
.equalsIgnoreCase(channel.get().name()))
- .collect(Collectors.toSet());
+ .collect(toSet());
botsToRemove.stream()
.forEach(bot -> networkBots.row(network.get())
.remove(bot.name()));
@Subscribe
public void dccSendReceived(final DccSendReceived dccSendReceived) {
final Optional<Network> network = getNetwork(dccSendReceived.connection());
+ eventBus.post(new GenericMessage(format("Got DCC Send from %s for %s.", dccSendReceived.source(), dccSendReceived.filename())));
if (!network.isPresent()) {
return;
}
- Collection<Download> packDownloads = downloads.get(dccSendReceived.filename());
- if (packDownloads.isEmpty()) {
- /* unknown download, ignore. */
- return;
- }
+ Set<Download> openDownloads = downloads.values().stream()
+ .filter(download -> download.bot().name().equalsIgnoreCase(dccSendReceived.source().nick().orNull()))
+ .filter(download -> download.dccReceiver() == null)
+ .collect(toSet());
- /* check if it’s already downloading. */
- Collection<Download> runningDownloads = FluentIterable.from(packDownloads).filter(FILTER_RUNNING).toSet();
- if (!runningDownloads.isEmpty()) {
- eventBus.post(new GenericMessage(String.format("Ignoring offer for %s, it’s already being downloaded.", dccSendReceived.filename())));
+ if (openDownloads.isEmpty()) {
+ /* I don't think we requested this. */
+ eventBus.post(new GenericMessage(format("Ignoring offer for %s, no open download from %s.", dccSendReceived.filename(), dccSendReceived.source())));
return;
}
- /* locate the correct download. */
- Collection<Download> requestedDownload = FluentIterable.from(packDownloads).filter(new Predicate<Download>() {
-
- @Override
- public boolean apply(Download download) {
- return download.bot().network().equals(network.get()) && download.bot().name().equalsIgnoreCase(dccSendReceived.source().nick().get());
- }
- }).toSet();
-
- /* we did not request this download. */
- if (requestedDownload.isEmpty()) {
+ /* check if it’s already downloading. */
+ if (downloads.values().stream().anyMatch(download -> Objects.equals(download.filename(), dccSendReceived.filename()) && download.dccReceiver() != null)) {
+ eventBus.post(new GenericMessage(format("Ignoring offer for %s, it’s already being downloaded.", dccSendReceived.filename())));
return;
}
- Download download = requestedDownload.iterator().next();
+ Download download = openDownloads.stream()
+ .filter(it -> Objects.equals(it.filename(), dccSendReceived.filename()))
+ .findFirst()
+ .orElse(openDownloads.iterator().next());
+ eventBus.post(new GenericMessage(format("Downloading %s from %s as %s.", dccSendReceived.filename(), dccSendReceived.source(), download.pack().name())));
/* check if the file already exists. */
File outputFile = new File(temporaryDirectory, dccSendReceived.filename());