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;
import net.pterodactylus.irc.event.DccSendReceived;
import net.pterodactylus.irc.event.NicknameChanged;
-import net.pterodactylus.irc.event.PrivateNoticeReceived;
import net.pterodactylus.irc.event.PrivateMessageReceived;
+import net.pterodactylus.irc.event.PrivateNoticeReceived;
+import net.pterodactylus.irc.event.ReplyReceived;
import net.pterodactylus.irc.util.MessageCleaner;
import net.pterodactylus.irc.util.RandomNickname;
import net.pterodactylus.xdcc.core.event.BotAdded;
//
/**
+ * Returns all currently known connections.
+ *
+ * @return All currently known connections
+ */
+ public Collection<Connection> connections() {
+ return networkConnections.values();
+ }
+
+ /**
* Returns all configured channels. Due to various circumstances, configured
* channels might not actually be joined.
*
}
}
+ /**
+ * Closes the given connection.
+ *
+ * @param connection
+ * The connection to close
+ */
+ public void closeConnection(Connection connection) {
+ try {
+ connection.close();
+ } catch (IOException ioe1) {
+ /* TODO */
+ }
+ }
+
//
// ABSTRACTIDLESERVICE METHODS
//
}
}
+ /**
+ * 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());
}
/**
}
}
+ @Subscribe
+ public void replyReceived(ReplyReceived replyReceived) {
+ logger.log(Level.FINEST, String.format("%s: %s", replyReceived.connection().hostname(), replyReceived.reply()));
+ }
+
//
// PRIVATE METHODS
//