]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/cli/Main.java
Implement sending EndSession messages
[signal-cli] / src / main / java / cli / Main.java
index cac746d21607a9fe0afa488cbb2f3c2be5252731..c4a6a4c0eec0b40a120e6cae58e31b1587e720ba 100644 (file)
@@ -95,37 +95,9 @@ public class Main {
                     System.err.println("User is not registered.");
                     System.exit(1);
                 }
                     System.err.println("User is not registered.");
                     System.exit(1);
                 }
-                String messageText = ns.getString("message");
-                if (messageText == null) {
-                    try {
-                        messageText = IOUtils.toString(System.in);
-                    } catch (IOException e) {
-                        System.err.println("Failed to read message from stdin: " + e.getMessage());
-                        System.err.println("Aborting sending.");
-                        System.exit(1);
-                    }
-                }
 
 
-                final List<String> attachments = ns.getList("attachment");
-                List<TextSecureAttachment> textSecureAttachments = null;
-                if (attachments != null) {
-                    textSecureAttachments = new ArrayList<>(attachments.size());
-                    for (String attachment : attachments) {
-                        try {
-                            File attachmentFile = new File(attachment);
-                            InputStream attachmentStream = new FileInputStream(attachmentFile);
-                            final long attachmentSize = attachmentFile.length();
-                            String mime = Files.probeContentType(Paths.get(attachment));
-                            textSecureAttachments.add(new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null));
-                        } catch (IOException e) {
-                            System.err.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage());
-                            System.err.println("Aborting sending.");
-                            System.exit(1);
-                        }
-                    }
-                }
                 TextSecureGroup group = null;
                 TextSecureGroup group = null;
-                List<String> recipientStrings = null;
+                List<String> recipients = null;
                 if (ns.getString("group") != null) {
                     try {
                         GroupInfo g = m.getGroupInfo(Base64.decode(ns.getString("group")));
                 if (ns.getString("group") != null) {
                     try {
                         GroupInfo g = m.getGroupInfo(Base64.decode(ns.getString("group")));
@@ -135,28 +107,52 @@ public class Main {
                             System.exit(1);
                         }
                         group = new TextSecureGroup(g.groupId);
                             System.exit(1);
                         }
                         group = new TextSecureGroup(g.groupId);
-                        recipientStrings = g.members;
+                        recipients = g.members;
                     } catch (IOException e) {
                         System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": " + e.getMessage());
                         System.err.println("Aborting sending.");
                         System.exit(1);
                     }
                 } else {
                     } catch (IOException e) {
                         System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": " + e.getMessage());
                         System.err.println("Aborting sending.");
                         System.exit(1);
                     }
                 } else {
-                    recipientStrings = ns.<String>getList("recipient");
+                    recipients = ns.<String>getList("recipient");
                 }
 
                 }
 
-                List<TextSecureAddress> recipients = new ArrayList<>(ns.<String>getList("recipient").size());
-                for (String recipient : recipientStrings) {
-                    try {
-                        recipients.add(m.getPushAddress(recipient));
-                    } catch (InvalidNumberException e) {
-                        System.err.println("Failed to add recipient \"" + recipient + "\": " + e.getMessage());
-                        System.err.println("Aborting sending.");
-                        System.exit(1);
+                if (ns.getBoolean("endsession")) {
+                    sendEndSessionMessage(m, recipients);
+                } else {
+                    final List<String> attachments = ns.getList("attachment");
+                    List<TextSecureAttachment> textSecureAttachments = null;
+                    if (attachments != null) {
+                        textSecureAttachments = new ArrayList<>(attachments.size());
+                        for (String attachment : attachments) {
+                            try {
+                                File attachmentFile = new File(attachment);
+                                InputStream attachmentStream = new FileInputStream(attachmentFile);
+                                final long attachmentSize = attachmentFile.length();
+                                String mime = Files.probeContentType(Paths.get(attachment));
+                                textSecureAttachments.add(new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null));
+                            } catch (IOException e) {
+                                System.err.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage());
+                                System.err.println("Aborting sending.");
+                                System.exit(1);
+                            }
+                        }
                     }
                     }
+
+                    String messageText = ns.getString("message");
+                    if (messageText == null) {
+                        try {
+                            messageText = IOUtils.toString(System.in);
+                        } catch (IOException e) {
+                            System.err.println("Failed to read message from stdin: " + e.getMessage());
+                            System.err.println("Aborting sending.");
+                            System.exit(1);
+                        }
+                    }
+
+                    sendMessage(m, messageText, textSecureAttachments, recipients, group);
                 }
 
                 }
 
-                sendMessage(m, messageText, textSecureAttachments, recipients, group);
                 break;
             case "receive":
                 if (!m.isRegistered()) {
                 break;
             case "receive":
                 if (!m.isRegistered()) {
@@ -227,6 +223,9 @@ public class Main {
         parserSend.addArgument("-a", "--attachment")
                 .nargs("*")
                 .help("Add file as attachment");
         parserSend.addArgument("-a", "--attachment")
                 .nargs("*")
                 .help("Add file as attachment");
+        parserSend.addArgument("-e", "--endsession")
+                .help("Clear session state and send end session message.")
+                .action(Arguments.storeTrue());
 
         Subparser parserReceive = subparsers.addParser("receive");
         parserReceive.addArgument("-t", "--timeout")
 
         Subparser parserReceive = subparsers.addParser("receive");
         parserReceive.addArgument("-t", "--timeout")
@@ -252,7 +251,7 @@ public class Main {
     }
 
     private static void sendMessage(Manager m, String messageText, List<TextSecureAttachment> textSecureAttachments,
     }
 
     private static void sendMessage(Manager m, String messageText, List<TextSecureAttachment> textSecureAttachments,
-                                    List<TextSecureAddress> recipients, TextSecureGroup group) {
+                                    List<String> recipients, TextSecureGroup group) {
         final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
         if (textSecureAttachments != null) {
             messageBuilder.withAttachments(textSecureAttachments);
         final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
         if (textSecureAttachments != null) {
             messageBuilder.withAttachments(textSecureAttachments);
@@ -262,6 +261,18 @@ public class Main {
         }
         TextSecureDataMessage message = messageBuilder.build();
 
         }
         TextSecureDataMessage message = messageBuilder.build();
 
+        sendMessage(m, message, recipients);
+    }
+
+    private static void sendEndSessionMessage(Manager m, List<String> recipients) {
+        final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().asEndSessionMessage();
+
+        TextSecureDataMessage message = messageBuilder.build();
+
+        sendMessage(m, message, recipients);
+    }
+
+    private static void sendMessage(Manager m, TextSecureDataMessage message, List<String> recipients) {
         try {
             m.sendMessage(recipients, message);
         } catch (IOException e) {
         try {
             m.sendMessage(recipients, message);
         } catch (IOException e) {
@@ -335,7 +346,6 @@ public class Main {
                         }
                         if (message.isEndSession()) {
                             System.out.println("Is end session");
                         }
                         if (message.isEndSession()) {
                             System.out.println("Is end session");
-                            m.handleEndSession(envelope.getSource());
                         }
 
                         if (message.getAttachments().isPresent()) {
                         }
 
                         if (message.getAttachments().isPresent()) {
@@ -362,11 +372,9 @@ public class Main {
                 final TextSecureAttachmentPointer pointer = attachment.asPointer();
                 System.out.println("  Id: " + pointer.getId() + " Key length: " + pointer.getKey().length + (pointer.getRelay().isPresent() ? " Relay: " + pointer.getRelay().get() : ""));
                 System.out.println("  Size: " + (pointer.getSize().isPresent() ? pointer.getSize().get() + " bytes" : "<unavailable>") + (pointer.getPreview().isPresent() ? " (Preview is available: " + pointer.getPreview().get().length + " bytes)" : ""));
                 final TextSecureAttachmentPointer pointer = attachment.asPointer();
                 System.out.println("  Id: " + pointer.getId() + " Key length: " + pointer.getKey().length + (pointer.getRelay().isPresent() ? " Relay: " + pointer.getRelay().get() : ""));
                 System.out.println("  Size: " + (pointer.getSize().isPresent() ? pointer.getSize().get() + " bytes" : "<unavailable>") + (pointer.getPreview().isPresent() ? " (Preview is available: " + pointer.getPreview().get().length + " bytes)" : ""));
-                try {
-                    File file = m.retrieveAttachment(pointer);
+                File file = m.getAttachmentFile(pointer.getId());
+                if (file.exists()) {
                     System.out.println("  Stored plaintext in: " + file);
                     System.out.println("  Stored plaintext in: " + file);
-                } catch (IOException | InvalidMessageException e) {
-                    System.out.println("Failed to retrieve attachment: " + e.getMessage());
                 }
             }
         }
                 }
             }
         }