]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/cli/Main.java
Move endsession and attachment handling to Manager
[signal-cli] / src / main / java / cli / Main.java
index 07353a199a4f3d1167b93f9f8dff3815e7fd853f..199ea1eb04ff06347cf27cdd8dc644b525c271f5 100644 (file)
@@ -101,6 +101,7 @@ public class Main {
                         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);
                     }
                 }
@@ -123,29 +124,42 @@ public class Main {
                         }
                     }
                 }
-
-                List<TextSecureAddress> recipients = new ArrayList<>(ns.<String>getList("recipient").size());
-                for (String recipient : ns.<String>getList("recipient")) {
+                TextSecureGroup group = null;
+                List<String> recipients = null;
+                if (ns.getString("group") != null) {
                     try {
-                        recipients.add(m.getPushAddress(recipient));
-                    } catch (InvalidNumberException e) {
-                        System.err.println("Failed to add recipient \"" + recipient + "\": " + e.getMessage());
+                        GroupInfo g = m.getGroupInfo(Base64.decode(ns.getString("group")));
+                        if (g == null) {
+                            System.err.println("Failed to send to grup \"" + ns.getString("group") + "\": Unknown group");
+                            System.err.println("Aborting sending.");
+                            System.exit(1);
+                        }
+                        group = new TextSecureGroup(g.groupId);
+                        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 {
+                    recipients = ns.<String>getList("recipient");
                 }
-                sendMessage(m, messageText, textSecureAttachments, recipients);
+
+                sendMessage(m, messageText, textSecureAttachments, recipients, group);
                 break;
             case "receive":
                 if (!m.isRegistered()) {
                     System.err.println("User is not registered.");
                     System.exit(1);
                 }
-                int timeout = ns.getInt("timeout");
+                int timeout = 5;
+                if (ns.getInt("timeout") != null) {
+                    timeout = ns.getInt("timeout");
+                }
                 boolean returnOnTimeout = true;
                 if (timeout < 0) {
                     returnOnTimeout = false;
-                    timeout = 5;
+                    timeout = 3600;
                 }
                 try {
                     m.receiveMessages(timeout, returnOnTimeout, new ReceiveMessageHandler(m));
@@ -192,6 +206,8 @@ public class Main {
                 .help("The verification code you received via sms or voice call.");
 
         Subparser parserSend = subparsers.addParser("send");
+        parserSend.addArgument("-g", "--group")
+                .help("Specify the recipient group ID.");
         parserSend.addArgument("recipient")
                 .help("Specify the recipients' phone number.")
                 .nargs("*");
@@ -213,6 +229,10 @@ public class Main {
                 System.err.println("You need to specify a username (phone number)");
                 System.exit(2);
             }
+            if (ns.getList("recipient") != null && !ns.getList("recipient").isEmpty() && ns.getString("group") != null) {
+                System.err.println("You cannot specify recipients by phone number and groups a the same time");
+                System.exit(2);
+            }
             return ns;
         } catch (ArgumentParserException e) {
             parser.handleError(e);
@@ -221,11 +241,14 @@ public class Main {
     }
 
     private static void sendMessage(Manager m, String messageText, List<TextSecureAttachment> textSecureAttachments,
-                                    List<TextSecureAddress> recipients) {
+                                    List<String> recipients, TextSecureGroup group) {
         final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
         if (textSecureAttachments != null) {
             messageBuilder.withAttachments(textSecureAttachments);
         }
+        if (group != null) {
+            messageBuilder.asGroupMessage(group);
+        }
         TextSecureDataMessage message = messageBuilder.build();
 
         try {
@@ -259,15 +282,13 @@ public class Main {
         }
 
         @Override
-        public void handleMessage(TextSecureEnvelope envelope) {
+        public void handleMessage(TextSecureEnvelope envelope, TextSecureContent content, GroupInfo group) {
             System.out.println("Envelope from: " + envelope.getSource());
             System.out.println("Timestamp: " + envelope.getTimestamp());
 
             if (envelope.isReceipt()) {
                 System.out.println("Got receipt.");
             } else if (envelope.isWhisperMessage() | envelope.isPreKeyWhisperMessage()) {
-                TextSecureContent content = m.decryptMessage(envelope);
-
                 if (content == null) {
                     System.out.println("Failed to decrypt message.");
                 } else {
@@ -285,6 +306,10 @@ public class Main {
                             System.out.println("  Id: " + Base64.encodeBytes(groupInfo.getGroupId()));
                             if (groupInfo.getName().isPresent()) {
                                 System.out.println("  Name: " + groupInfo.getName().get());
+                            } else if (group != null) {
+                                System.out.println("  Name: " + group.name);
+                            } else {
+                                System.out.println("  Name: <Unknown group>");
                             }
                             System.out.println("  Type: " + groupInfo.getType());
                             if (groupInfo.getMembers().isPresent()) {
@@ -299,7 +324,6 @@ public class Main {
                         }
                         if (message.isEndSession()) {
                             System.out.println("Is end session");
-                            m.handleEndSession(envelope.getSource());
                         }
 
                         if (message.getAttachments().isPresent()) {
@@ -326,11 +350,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)" : ""));
-                try {
-                    File file = m.retrieveAttachment(pointer);
+                File file = m.getAttachmentFile(pointer.getId());
+                if (file.exists()) {
                     System.out.println("  Stored plaintext in: " + file);
-                } catch (IOException | InvalidMessageException e) {
-                    System.out.println("Failed to retrieve attachment: " + e.getMessage());
                 }
             }
         }