]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/cli/Main.java
Exit on timeout when receiving
[signal-cli] / src / main / java / cli / Main.java
index adeb5262588295bfdf3ece96b7c3aa95c8479ff4..4cdbb806fb3394eb00250320eff3f053288a7487 100644 (file)
@@ -1,16 +1,16 @@
 /**
  * Copyright (C) 2015 AsamK
- * <p>
+ *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
- * <p>
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * <p>
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
@@ -24,6 +24,10 @@ import org.whispersystems.textsecure.api.TextSecureMessageSender;
 import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
 import org.whispersystems.textsecure.api.messages.*;
 import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage;
+import org.whispersystems.textsecure.api.push.TextSecureAddress;
+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 java.io.File;
@@ -150,11 +154,29 @@ public class Main {
                     messageBuilder.withAttachments(textSecureAttachments);
                 }
                 TextSecureDataMessage message = messageBuilder.build();
+
+                List<TextSecureAddress> recipients = new ArrayList<>(ns.<String>getList("recipient").size());
                 for (String recipient : ns.<String>getList("recipient")) {
                     try {
-                        messageSender.sendMessage(m.getPushAddress(recipient), message);
-                    } catch (UntrustedIdentityException | IOException | InvalidNumberException e) {
-                        System.out.println("Failed to send message  to \"" + recipient + "\": " + e.getMessage());
+                        recipients.add(m.getPushAddress(recipient));
+                    } catch (InvalidNumberException e) {
+                        System.out.println("Failed to send message to \"" + recipient + "\": " + e.getMessage());
+                    }
+                }
+                try {
+                    messageSender.sendMessage(recipients, message);
+                } catch (IOException e) {
+                    System.out.println("Failed to send message: " + e.getMessage());
+                } catch (EncapsulatedExceptions e) {
+                    System.out.println("Failed to send (some) messages:");
+                    for (NetworkFailureException n : e.getNetworkExceptions()) {
+                        System.out.println("Network failure for \"" + n.getE164number() + "\": " + n.getMessage());
+                    }
+                    for (UnregisteredUserException n : e.getUnregisteredUserExceptions()) {
+                        System.out.println("Unregistered user \"" + n.getE164Number() + "\": " + n.getMessage());
+                    }
+                    for (UntrustedIdentityException n : e.getUntrustedIdentityExceptions()) {
+                        System.out.println("Untrusted Identity for \"" + n.getE164Number() + "\": " + n.getMessage());
                     }
                 }
                 break;
@@ -164,7 +186,7 @@ public class Main {
                     System.exit(1);
                 }
                 try {
-                    m.receiveMessages(new Manager.ReceiveMessageHandler() {
+                    m.receiveMessages(5, true, new Manager.ReceiveMessageHandler() {
                         @Override
                         public void handleMessage(TextSecureEnvelope envelope) {
                             System.out.println("Envelope from: " + envelope.getSource());
@@ -180,9 +202,11 @@ public class Main {
                                 } else {
                                     if (content.getDataMessage().isPresent()) {
                                         TextSecureDataMessage message = content.getDataMessage().get();
-
                                         System.out.println("Body: " + message.getBody().get());
-                                        if (message.getAttachments().isPresent()) {
+
+                                        if (message.isEndSession()) {
+                                            m.handleEndSession(envelope.getSource());
+                                        } else if (message.getAttachments().isPresent()) {
                                             System.out.println("Attachments: ");
                                             for (TextSecureAttachment attachment : message.getAttachments().get()) {
                                                 System.out.println("- " + attachment.getContentType() + " (" + (attachment.isPointer() ? "Pointer" : "") + (attachment.isStream() ? "Stream" : "") + ")");
@@ -209,5 +233,6 @@ public class Main {
                 break;
         }
         m.save();
+        System.exit(0);
     }
 }