import net.pterodactylus.irc.event.ClientQuit;
import net.pterodactylus.irc.event.ConnectionClosed;
import net.pterodactylus.irc.event.ConnectionEstablished;
+import net.pterodactylus.irc.event.ConnectionFailed;
import net.pterodactylus.irc.event.DccAcceptReceived;
import net.pterodactylus.irc.event.DccDownloadFailed;
import net.pterodactylus.irc.event.DccDownloadFinished;
}
}
+ /**
+ * Removes the given connection and all its channels and bots.
+ *
+ * @param connection
+ * The connection to remove
+ */
+ private void removeConnection(Connection connection) {
+ Optional<Network> network = getNetwork(connection);
+ if (!network.isPresent()) {
+ return;
+ }
+
+ /* find all channels that need to be removed. */
+ for (Collection<Channel> channels : ImmutableList.of(joinedChannels, extraChannels)) {
+ for (Iterator<Channel> channelIterator = channels.iterator(); channelIterator.hasNext(); ) {
+ Channel joinedChannel = channelIterator.next();
+ if (!joinedChannel.network().equals(network.get())) {
+ continue;
+ }
+
+ channelIterator.remove();
+ }
+ }
+
+ /* now remove all bots for that network. */
+ Map<String, Bot> bots = networkBots.row(network.get());
+ int botCount = bots.size();
+ int packCount = 0;
+ for (Bot bot : bots.values()) {
+ packCount += bot.packs().size();
+ }
+ bots.clear();
+ eventBus.post(new GenericMessage(String.format("Network %s disconnected, %d bots removed, %d packs removed.", network.get().name(), botCount, packCount)));
+
+ /* now remove the network. */
+ networkConnections.remove(network.get());
+ }
+
//
// EVENT HANDLERS
//
*/
@Subscribe
public void connectionClosed(ConnectionClosed connectionClosed) {
- Optional<Network> network = getNetwork(connectionClosed.connection());
- if (!network.isPresent()) {
- return;
- }
-
- /* find all channels that need to be removed. */
- for (Collection channels : ImmutableList.of(joinedChannels, extraChannels)) {
- for (Iterator<Channel> channelIterator = channels.iterator(); channelIterator.hasNext(); ) {
- Channel joinedChannel = channelIterator.next();
- if (!joinedChannel.network().equals(network.get())) {
- continue;
- }
-
- channelIterator.remove();
- }
- }
-
- /* now remove all bots for that network. */
- Map<String, Bot> bots = networkBots.row(network.get());
- int botCount = bots.size();
- int packCount = 0;
- for (Bot bot : bots.values()) {
- packCount += bot.packs().size();
- }
- bots.clear();
- eventBus.post(new GenericMessage(String.format("Network %s disconnected, %d bots removed, %d packs removed.", network.get().name(), botCount, packCount)));
+ removeConnection(connectionClosed.connection());
+ }
- /* now remove the network. */
- networkConnections.remove(network.get());
+ /**
+ * Remove all data stored for a network if the connection fails.
+ *
+ * @param connectionFailed
+ * The connection failed event
+ */
+ public void connectionFailed(ConnectionFailed connectionFailed) {
+ removeConnection(connectionFailed.connection());
}
/**