]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/cli/Main.java
Extract getTextSecureAttachments method
[signal-cli] / src / main / java / cli / Main.java
index 8aedd95840cbb175db703f105135a412e3d49901..08c7b935393023f965aec38cd9e3aaf0374f9288 100644 (file)
@@ -27,6 +27,7 @@ import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions;
 import org.whispersystems.textsecure.api.push.exceptions.NetworkFailureException;
 import org.whispersystems.textsecure.api.push.exceptions.UnregisteredUserException;
 import org.whispersystems.textsecure.api.util.InvalidNumberException;
 import org.whispersystems.textsecure.api.push.exceptions.NetworkFailureException;
 import org.whispersystems.textsecure.api.push.exceptions.UnregisteredUserException;
 import org.whispersystems.textsecure.api.util.InvalidNumberException;
+import org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
 
 import java.io.File;
 import java.io.FileInputStream;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -118,19 +119,13 @@ public class Main {
                 if (ns.getBoolean("endsession")) {
                     sendEndSessionMessage(m, recipients);
                 } else {
                 if (ns.getBoolean("endsession")) {
                     sendEndSessionMessage(m, recipients);
                 } else {
-                    final List<String> attachments = ns.getList("attachment");
                     List<TextSecureAttachment> textSecureAttachments = null;
                     List<TextSecureAttachment> textSecureAttachments = null;
-                    if (attachments != null) {
-                        textSecureAttachments = new ArrayList<>(attachments.size());
-                        for (String attachment : attachments) {
-                            try {
-                                textSecureAttachments.add(createAttachment(attachment));
-                            } catch (IOException e) {
-                                System.err.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage());
-                                System.err.println("Aborting sending.");
-                                System.exit(1);
-                            }
-                        }
+                    try {
+                        textSecureAttachments = getTextSecureAttachments(ns.<String>getList("attachment"));
+                    } catch (IOException e) {
+                        System.err.println("Failed to add attachment: " + e.getMessage());
+                        System.err.println("Aborting sending.");
+                        System.exit(1);
                     }
 
                     String messageText = ns.getString("message");
                     }
 
                     String messageText = ns.getString("message");
@@ -269,6 +264,18 @@ public class Main {
         System.exit(0);
     }
 
         System.exit(0);
     }
 
+    private static List<TextSecureAttachment> getTextSecureAttachments(List<String> attachments) {
+    private static List<TextSecureAttachment> getTextSecureAttachments(List<String> attachments) throws IOException {
+        List<TextSecureAttachment> textSecureAttachments = null;
+        if (attachments != null) {
+            textSecureAttachments = new ArrayList<>(attachments.size());
+            for (String attachment : attachments) {
+                textSecureAttachments.add(createAttachment(attachment));
+             }
+        }
+        return textSecureAttachments;
+    }
+
     private static TextSecureAttachmentStream createAttachment(String attachment) throws IOException {
         File attachmentFile = new File(attachment);
         InputStream attachmentStream = new FileInputStream(attachmentFile);
     private static TextSecureAttachmentStream createAttachment(String attachment) throws IOException {
         File attachmentFile = new File(attachment);
         InputStream attachmentStream = new FileInputStream(attachmentFile);
@@ -347,6 +354,10 @@ public class Main {
                 System.err.println("You need to specify a username (phone number)");
                 System.exit(2);
             }
                 System.err.println("You need to specify a username (phone number)");
                 System.exit(2);
             }
+            if (!PhoneNumberFormatter.isValidNumber(ns.getString("username"))) {
+                System.err.println("Invalid username (phone number), make sure you include the country code.");
+                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);
             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);