šŸš§ Parse token from DCC SEND
[xudocci.git] / src / main / java / net / pterodactylus / irc / connection / CtcpHandler.java
index ff9757e..fb59ea7 100644 (file)
@@ -49,7 +49,7 @@ public class CtcpHandler implements Handler {
                return reply.command().equalsIgnoreCase(command);
        }
 
-       private boolean messageIsCtcp(String message) {
+       public static boolean messageIsCtcp(String message) {
                return message.startsWith("\u0001") && message.endsWith("\u0001");
        }
 
@@ -66,26 +66,63 @@ public class CtcpHandler implements Handler {
                String ctcpCommand = messageWords[0];
                if (ctcpCommand.equalsIgnoreCase("DCC")) {
                        if (messageWords[1].equalsIgnoreCase("SEND")) {
-                               Optional<InetAddress> inetAddress = parseInetAddress(messageWords[3]);
-                               Optional<Integer> port = fromNullable(Ints.tryParse(messageWords[4]));
-                               long fileSize = fromNullable(Longs.tryParse(messageWords[5])).or(-1L);
-                               if (inetAddress.isPresent() && port.isPresent()) {
-                                       eventBus.post(new DccSendReceived(connection, client, messageWords[2], inetAddress.get(), port.get(), fileSize));
-                               } else {
-                                       logger.warn(format("Received malformed DCC SEND: ā€œ%sā€", message));
-                               }
+                               processDccSend(client, message, messageWords);
                        } else if (messageWords[1].equalsIgnoreCase("ACCEPT")) {
-                               Optional<Integer> port = fromNullable(Ints.tryParse(messageWords[3]));
-                               long position = (messageWords.length > 4) ? fromNullable(Longs.tryParse(messageWords[4])).or(-1L) : -1;
-                               if (port.isPresent()) {
-                                       eventBus.post(new DccAcceptReceived(connection, client, messageWords[2], port.get(), position));
-                               } else {
-                                       logger.warn(format("Received malformed DCC ACCEPT: ā€œ%sā€", message));
-                               }
+                               processDccAccept(client, message, messageWords);
                        }
                }
        }
 
+       private void processDccSend(Source client, String message,
+                       String[] messageWords) {
+               Optional<DccSendInformation> dccSendInformation = parseDccSendInformation(messageWords);
+               if (dccSendInformation.isPresent()) {
+                       eventBus.post(new DccSendReceived(connection, client,
+                                       dccSendInformation.get().filename,
+                                       dccSendInformation.get().internetAddress,
+                                       dccSendInformation.get().port,
+                                       dccSendInformation.get().size,
+                                       dccSendInformation.get().token));
+               } else {
+                       logger.warn(format("Received malformed DCC SEND: ā€œ%sā€", message));
+               }
+       }
+
+       private Optional<DccSendInformation> parseDccSendInformation(String[] messageWords) {
+               if (messageWords.length <5) {
+                       return absent();
+               }
+               Optional<InetAddress> internetAddress = parseInetAddress(messageWords[3]);
+               Optional<Integer> port = fromNullable(Ints.tryParse(messageWords[4]));
+               long fileSize = (messageWords.length > 5) ? fromNullable(Longs.tryParse(messageWords[5])).or(-1L) : -1;
+               if (!internetAddress.isPresent() || !port.isPresent()) {
+                       return absent();
+               }
+               String token = null;
+               if (messageWords.length > 6) {
+                       token = messageWords[6];
+               }
+               return of(new DccSendInformation(messageWords[2], internetAddress.get(), port.get(), fileSize, token));
+       }
+
+       private static class DccSendInformation {
+
+               private final String filename;
+               private final InetAddress internetAddress;
+               private final int port;
+               private final long size;
+               private final String token;
+
+               private DccSendInformation(String filename, InetAddress internetAddress, int port, long size, String token) {
+                       this.filename = filename;
+                       this.internetAddress = internetAddress;
+                       this.port = port;
+                       this.size = size;
+                       this.token = token;
+               }
+
+       }
+
        private Optional<InetAddress> parseInetAddress(String ip) {
                Long ipNumber = Longs.tryParse(ip);
                if (ipNumber == null) {
@@ -104,4 +141,45 @@ public class CtcpHandler implements Handler {
                }
        }
 
+       private void processDccAccept(Source client, String message,
+                       String[] messageWords) {
+               Optional<DccAcceptInformation> dccAcceptInformation = parseDccAcceptInformation(messageWords);
+               if (dccAcceptInformation.isPresent()) {
+                       eventBus.post(new DccAcceptReceived(connection, client,
+                                       dccAcceptInformation.get().filename,
+                                       dccAcceptInformation.get().port,
+                                       dccAcceptInformation.get().position));
+               } else {
+                       logger.warn(format("Received malformed DCC ACCEPT: ā€œ%sā€", message));
+               }
+       }
+
+       private Optional<DccAcceptInformation> parseDccAcceptInformation(
+                       String[] messageWords) {
+               if (messageWords.length < 4) {
+                       return absent();
+               }
+               Optional<Integer> port = fromNullable(Ints.tryParse(messageWords[3]));
+               long position = (messageWords.length > 4) ? fromNullable(
+                               Longs.tryParse(messageWords[4])).or(-1L) : -1;
+               if (!port.isPresent()) {
+                       return absent();
+               }
+               return of(new DccAcceptInformation(messageWords[2], port.get(), position));
+       }
+
+       private static class DccAcceptInformation {
+
+               private final String filename;
+               private final int port;
+               private final long position;
+
+               private DccAcceptInformation(String filename, int port, long position) {
+                       this.filename = filename;
+                       this.port = port;
+                       this.position = position;
+               }
+
+       }
+
 }