Debug-log every single message.
[xudocci.git] / src / main / java / net / pterodactylus / xdcc / core / Core.java
index 607246c..d02e679 100644 (file)
@@ -43,13 +43,15 @@ import net.pterodactylus.irc.event.ChannelMessageReceived;
 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;
@@ -147,6 +149,15 @@ public class Core extends AbstractExecutionThreadService {
        //
 
        /**
+        * 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.
         *
@@ -220,6 +231,25 @@ public class Core extends AbstractExecutionThreadService {
                        return;
                }
 
+               /* check if we are already downloading the file? */
+               if (downloads.containsKey(pack.name())) {
+                       Collection<Download> packDownloads = downloads.get(pack.name());
+                       Collection<Download> runningDownloads = FluentIterable.from(packDownloads).filter(FILTER_RUNNING).toSet();
+                       if (!runningDownloads.isEmpty()) {
+                               Download download = runningDownloads.iterator().next();
+                               eventBus.post(new GenericMessage(String.format("File %s is already downloading from %s (%s).", pack.name(), download.bot().name(), download.bot().network().name())));
+                               return;
+                       }
+                       StringBuilder bots = new StringBuilder();
+                       for (Download download : packDownloads) {
+                               if (bots.length() > 0) {
+                                       bots.append(", ");
+                               }
+                               bots.append(download.bot().name()).append(" (").append(download.bot().network().name()).append(')');
+                       }
+                       eventBus.post(new GenericMessage(String.format("File %s is already requested from %d bots (%s).", pack.name(), packDownloads.size(), bots.toString())));
+               }
+
                Download download = new Download(bot, pack);
                downloads.put(pack.name(), download);
 
@@ -230,6 +260,20 @@ public class Core extends AbstractExecutionThreadService {
                }
        }
 
+       /**
+        * Closes the given connection.
+        *
+        * @param connection
+        *              The connection to close
+        */
+       public void closeConnection(Connection connection) {
+               try {
+                       connection.close();
+               } catch (IOException ioe1) {
+                       /* TODO */
+               }
+       }
+
        //
        // ABSTRACTIDLESERVICE METHODS
        //
@@ -301,6 +345,44 @@ public class Core extends AbstractExecutionThreadService {
                }
        }
 
+       /**
+        * 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
        //
@@ -343,35 +425,17 @@ public class Core extends AbstractExecutionThreadService {
         */
        @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());
        }
 
        /**
@@ -515,7 +579,7 @@ public class Core extends AbstractExecutionThreadService {
                Bot bot;
                synchronized (networkBots) {
                        if (!networkBots.contains(network.get(), channelMessageReceived.source().nick().get())) {
-                               bot = new Bot(network.get()).name(channelMessageReceived.source().nick().get());
+                               bot = new Bot(network.get()channelMessageReceived.source().nick().get());
                                networkBots.put(network.get(), channelMessageReceived.source().nick().get(), bot);
                                eventBus.post(new BotAdded(bot));
                        } else {
@@ -552,7 +616,7 @@ public class Core extends AbstractExecutionThreadService {
                        return;
                }
 
-               eventBus.post(new GenericMessage(String.format("Notice from %s (%s): %s", privateNoticeReceived.reply().source(), network.get(), privateNoticeReceived.text())));
+               eventBus.post(new GenericMessage(String.format("Notice from %s (%s): %s", privateNoticeReceived.reply().source().get(), network.get(), privateNoticeReceived.text())));
        }
 
        /**
@@ -755,6 +819,11 @@ public class Core extends AbstractExecutionThreadService {
                }
        }
 
+       @Subscribe
+       public void replyReceived(ReplyReceived replyReceived) {
+               logger.log(Level.FINEST, String.format("%s: %s", replyReceived.connection().hostname(), replyReceived.reply()));
+       }
+
        //
        // PRIVATE METHODS
        //