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;
if (channelBanManager.isBanned(channel)) {
continue;
}
- if (networkConnections.containsKey(channel.network())) {
- if (networkConnections.get(channel.network()).established()) {
- missingChannels.add(channel);
- }
+ if (!networkConnections.containsKey(channel.network()) || networkConnections.get(channel.network()).established()) {
+ missingChannels.add(channel);
}
}
Set<Network> missingNetworks = missingChannels.stream()
.filter((network) -> !networkConnections.containsKey(network))
.collect(Collectors.toSet());
- if (!missingChannels.isEmpty()) {
- for (Channel missingChannel : missingChannels) {
- Network network = missingChannel.network();
- eventBus.post(new GenericMessage(String.format("Trying to join %s on %s...", missingChannel.name(), network)));
- try {
- channelsBeingJoined.add(missingChannel);
- networkConnections.get(network).joinChannel(missingChannel.name());
- } catch (IOException ioe1) {
- logger.warn(String.format("Could not join %s on %s!", missingChannel.name(), network.name()), ioe1);
+ if (missingNetworks.isEmpty()) {
+ if (!missingChannels.isEmpty()) {
+ for (Channel missingChannel : missingChannels) {
+ Network network = missingChannel.network();
+ eventBus.post(new GenericMessage(String.format("Trying to join %s on %s...", missingChannel.name(), network)));
+ try {
+ channelsBeingJoined.add(missingChannel);
+ networkConnections.get(network).joinChannel(missingChannel.name());
+ } catch (IOException ioe1) {
+ logger.warn(String.format("Could not join %s on %s!", missingChannel.name(), network.name()), ioe1);
+ }
}
- }
- } else if (missingNetworks.isEmpty()) {
- synchronized (syncObject) {
- try {
- syncObject.wait(TimeUnit.MINUTES.toMillis(1));
- } catch (InterruptedException ie1) {
+ } else {
+ synchronized (syncObject) {
+ try {
+ syncObject.wait(TimeUnit.MINUTES.toMillis(1));
+ } catch (InterruptedException ie1) {
/* ignore. */
+ }
}
}
continue;
}
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)));
- Entry<Long, Network> firstNetwork = timesForNextConnects.entrySet().stream().findFirst().get();
- if (firstNetwork.getKey() > 0) {
- eventBus.post(new GenericMessage(String.format("Waiting %d seconds to connect to %s...", TimeUnit.MILLISECONDS.toMinutes(firstNetwork.getKey()), firstNetwork.getValue().name())));
+ Optional<Entry<Long, Network>> firstNetwork = Optional.fromNullable(timesForNextConnects.entrySet().stream().findFirst().orElse(null));
+ if (!firstNetwork.isPresent()) {
+ continue;
+ }
+ 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.getKey());
+ syncObject.wait(firstNetwork.get().getKey() - System.currentTimeMillis());
} catch (InterruptedException ie1) {
/* ignore. */
}
if (!isRunning()) {
break;
}
+ if (firstNetwork.get().getKey() > System.currentTimeMillis()) {
+ continue;
+ }
}
- connectNetwork(firstNetwork.getValue());
+ connectNetwork(firstNetwork.get().getValue());
}
}
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());
}
Optional<Channel> channel = getChannel(network.get(), channelNotJoined.channel());
+ synchronized (syncObject) {
+ syncObject.notifyAll();
+ }
if (!channel.isPresent()) {
eventBus.post(new GenericMessage(format("Could not join %s but didn’t try to join, either.", channelNotJoined.channel())));
return;
}
-
channelsBeingJoined.remove(channel.get());
- synchronized (syncObject) {
- syncObject.notifyAll();
- }
+
/* remove all bots for this channel, we might have been kicked. */
Collection<Bot> botsToRemove = networkBots.row(network.get())
.forEach(bot -> networkBots.row(network.get())
.remove(bot.name()));
+ channelBanManager.ban(channel.get());
if (channelNotJoined.reason() == registeredNicknamesOnly) {
- channels.remove(channel.get());
eventBus.post(new GenericMessage(
format("Not trying to join %s anymore.", channel.get())));
- return;
- }
- if (channelNotJoined.reason() == banned) {
- channelBanManager.ban(channel.get());
+ } else if (channelNotJoined.reason() == inviteOnly) {
+ eventBus.post(new GenericMessage(
+ format("%s is invite-only, suspending join for a day.",
+ channel.get())));
+ } else if (channelNotJoined.reason() == banned) {
eventBus.post(new GenericMessage(
format("Banned from %s, suspending join for a day.",
channel.get())));
- return;
+ } else {
+ eventBus.post(new GenericMessage(
+ format("Could not join %s: %s", channelNotJoined.channel(),
+ channelNotJoined.reason())));
}
-
- eventBus.post(new GenericMessage(
- format("Could not join %s: %s", channelNotJoined.channel(),
- channelNotJoined.reason())));
}
/**