Fix waiting for connections.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index 0db311c..00d1fbd 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.xdcc.core;
 
 import static java.lang.String.format;
 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;
@@ -427,17 +428,17 @@ public class Core extends AbstractExecutionThreadService {
                        }
 
                        Map<Long, Network> timesForNextConnects = new TreeMap<>(missingNetworks.stream()
-                                       .collect(Collectors.toMap(connectionBackoff::getBackoff, Function.identity(), (network, ignore) -> network)));
+                                       .collect(Collectors.toMap(connectionBackoff::getConnectionTime, Function.identity(), (network, ignore) -> network)));
 
                        Optional<Entry<Long, Network>> firstNetwork = Optional.fromNullable(timesForNextConnects.entrySet().stream().findFirst().orElse(null));
                        if (!firstNetwork.isPresent()) {
                                continue;
                        }
-                       if (firstNetwork.get().getKey() > 0) {
-                               eventBus.post(new GenericMessage(String.format("Waiting %d minutes to connect to %s...", TimeUnit.MILLISECONDS.toMinutes(firstNetwork.get().getKey()), firstNetwork.get().getValue().name())));
+                       if (firstNetwork.get().getKey() > System.currentTimeMillis()) {
+                               eventBus.post(new GenericMessage(String.format("Waiting %d minutes to connect to %s...", TimeUnit.MILLISECONDS.toMinutes(firstNetwork.get().getKey() - System.currentTimeMillis()), firstNetwork.get().getValue().name())));
                                synchronized (syncObject) {
                                        try {
-                                               syncObject.wait(firstNetwork.get().getKey());
+                                               syncObject.wait(firstNetwork.get().getKey() - System.currentTimeMillis());
                                        } catch (InterruptedException ie1) {
                                                /* ignore. */
                                        }
@@ -445,6 +446,9 @@ public class Core extends AbstractExecutionThreadService {
                                if (!isRunning()) {
                                        break;
                                }
+                               if (firstNetwork.get().getKey() > System.currentTimeMillis()) {
+                                       continue;
+                               }
                        }
 
                        connectNetwork(firstNetwork.get().getValue());
@@ -478,6 +482,7 @@ public class Core extends AbstractExecutionThreadService {
                                return;
                        }
                        Server server = servers.get((int) (Math.random() * servers.size()));
+                       eventBus.post(new GenericMessage(String.format("Connecting to %s on %s...", network.name(), server.hostname())));
                        Connection connection = connectionFactory.createConnection(server.hostname(),
                                        server.unencryptedPorts().iterator().next());
                        connection.username(RandomNickname.get()).realName(RandomNickname.get());
@@ -659,6 +664,13 @@ public class Core extends AbstractExecutionThreadService {
                                        format("Not trying to join %s anymore.", channel.get())));
                        return;
                }
+               if (channelNotJoined.reason() == inviteOnly) {
+                       channelBanManager.ban(channel.get());
+                       eventBus.post(new GenericMessage(
+                                       format("%s is invite-only, suspending join for a day.",
+                                                       channel.get())));
+                       return;
+               }
                if (channelNotJoined.reason() == banned) {
                        channelBanManager.ban(channel.get());
                        eventBus.post(new GenericMessage(