]> nmode's Git Repositories - signal-cli/commitdiff
Move endsession and attachment handling to Manager
authorAsamK <asamk@gmx.de>
Thu, 26 Nov 2015 13:17:32 +0000 (14:17 +0100)
committerAsamK <asamk@gmx.de>
Thu, 26 Nov 2015 13:17:32 +0000 (14:17 +0100)
src/main/java/cli/Main.java
src/main/java/cli/Manager.java

index cac746d21607a9fe0afa488cbb2f3c2be5252731..199ea1eb04ff06347cf27cdd8dc644b525c271f5 100644 (file)
@@ -125,7 +125,7 @@ public class Main {
                     }
                 }
                 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")));
@@ -135,25 +135,14 @@ public class Main {
                             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 {
-                    recipientStrings = 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);
-                    }
+                    recipients = ns.<String>getList("recipient");
                 }
 
                 sendMessage(m, messageText, textSecureAttachments, recipients, group);
@@ -252,7 +241,7 @@ public class Main {
     }
 
     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);
@@ -335,7 +324,6 @@ public class Main {
                         }
                         if (message.isEndSession()) {
                             System.out.println("Is end session");
-                            m.handleEndSession(envelope.getSource());
                         }
 
                         if (message.getAttachments().isPresent()) {
@@ -362,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());
                 }
             }
         }
index 758dabc3f6e8149b4204af9dac2eb76815ac7970..a1d58f036e10b7d33cd523eddfd41e0c426aea90 100644 (file)
@@ -44,6 +44,7 @@ import org.whispersystems.textsecure.api.util.InvalidNumberException;
 import org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
 
 import java.io.*;
+import java.util.ArrayList;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
@@ -242,14 +243,32 @@ class Manager {
         accountManager.setPreKeys(axolotlStore.getIdentityKeyPair().getPublicKey(), lastResortKey, signedPreKeyRecord, oneTimePreKeys);
     }
 
-    public void sendMessage(List<TextSecureAddress> recipients, TextSecureDataMessage message)
+    public void sendMessage(List<String> recipients, TextSecureDataMessage message)
             throws IOException, EncapsulatedExceptions {
         TextSecureMessageSender messageSender = new TextSecureMessageSender(URL, TRUST_STORE, username, password,
                 axolotlStore, USER_AGENT, Optional.<TextSecureMessageSender.EventListener>absent());
-        messageSender.sendMessage(recipients, message);
+
+        List<TextSecureAddress> recipientsTS = new ArrayList<>(recipients.size());
+        for (String recipient : recipients) {
+            try {
+                recipientsTS.add(getPushAddress(recipient));
+            } catch (InvalidNumberException e) {
+                System.err.println("Failed to add recipient \"" + recipient + "\": " + e.getMessage());
+                System.err.println("Aborting sending.");
+                return;
+            }
+        }
+
+        messageSender.sendMessage(recipientsTS, message);
+
+        if (message.isEndSession()) {
+            for (TextSecureAddress recipient : recipientsTS) {
+                handleEndSession(recipient.getNumber());
+            }
+        }
     }
 
-    public TextSecureContent decryptMessage(TextSecureEnvelope envelope) {
+    private TextSecureContent decryptMessage(TextSecureEnvelope envelope) {
         TextSecureCipher cipher = new TextSecureCipher(new TextSecureAddress(username), axolotlStore);
         try {
             return cipher.decrypt(envelope);
@@ -260,7 +279,7 @@ class Manager {
         }
     }
 
-    public void handleEndSession(String source) {
+    private void handleEndSession(String source) {
         axolotlStore.deleteAllSessions(source);
     }
 
@@ -290,7 +309,20 @@ class Manager {
                                     TextSecureGroup groupInfo = message.getGroupInfo().get();
                                     switch (groupInfo.getType()) {
                                         case UPDATE:
-                                            group = new GroupInfo(groupInfo.getGroupId(), groupInfo.getName().get(), groupInfo.getMembers().get(), groupInfo.getAvatar().get().asPointer().getId());
+                                            long avatarId = 0;
+                                            if (groupInfo.getAvatar().isPresent()) {
+                                                TextSecureAttachment avatar = groupInfo.getAvatar().get();
+                                                if (avatar.isPointer()) {
+                                                    avatarId = avatar.asPointer().getId();
+                                                    try {
+                                                        retrieveAttachment(avatar.asPointer());
+                                                    } catch (IOException | InvalidMessageException e) {
+                                                        System.err.println("Failed to retrieve group avatar (" + avatarId + "): " + e.getMessage());
+                                                    }
+                                                }
+                                            }
+
+                                            group = new GroupInfo(groupInfo.getGroupId(), groupInfo.getName().get(), groupInfo.getMembers().get(), avatarId);
                                             groupStore.updateGroup(group);
                                             break;
                                         case DELIVER:
@@ -304,6 +336,20 @@ class Manager {
                                             break;
                                     }
                                 }
+                                if (message.isEndSession()) {
+                                    handleEndSession(envelope.getSource());
+                                }
+                                if (message.getAttachments().isPresent()) {
+                                    for (TextSecureAttachment attachment : message.getAttachments().get()) {
+                                        if (attachment.isPointer()) {
+                                            try {
+                                                retrieveAttachment(attachment.asPointer());
+                                            } catch (IOException | InvalidMessageException e) {
+                                                System.err.println("Failed to retrieve attachment (" + attachment.asPointer().getId() + "): " + e.getMessage());
+                                            }
+                                        }
+                                    }
+                                }
                             }
                         }
                     }
@@ -322,14 +368,18 @@ class Manager {
         }
     }
 
-    public File retrieveAttachment(TextSecureAttachmentPointer pointer) throws IOException, InvalidMessageException {
+    public File getAttachmentFile(long attachmentId) {
+        return new File(attachmentsPath + "/" + attachmentId);
+    }
+
+    private File retrieveAttachment(TextSecureAttachmentPointer pointer) throws IOException, InvalidMessageException {
         final TextSecureMessageReceiver messageReceiver = new TextSecureMessageReceiver(URL, TRUST_STORE, username, password, signalingKey, USER_AGENT);
 
         File tmpFile = File.createTempFile("ts_attach_" + pointer.getId(), ".tmp");
         InputStream input = messageReceiver.retrieveAttachment(pointer, tmpFile);
 
         new File(attachmentsPath).mkdirs();
-        File outputFile = new File(attachmentsPath + "/" + pointer.getId());
+        File outputFile = getAttachmentFile(pointer.getId());
         OutputStream output = null;
         try {
             output = new FileOutputStream(outputFile);
@@ -374,12 +424,12 @@ class Manager {
         return PhoneNumberFormatter.formatNumber(number, localNumber);
     }
 
-    TextSecureAddress getPushAddress(String number) throws InvalidNumberException {
+    private TextSecureAddress getPushAddress(String number) throws InvalidNumberException {
         String e164number = canonicalizeNumber(number);
         return new TextSecureAddress(e164number);
     }
 
-    GroupInfo getGroupInfo(byte[] groupId) {
+    public GroupInfo getGroupInfo(byte[] groupId) {
         return groupStore.getGroup(groupId);
     }
 }