]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/cli/Manager.java
Always call save() after modifying something
[signal-cli] / src / main / java / cli / Manager.java
index 2a1ddb220de886683dcae85b7267a33169b08777..86850ed46ffa53c7a8fc55043dd749b7177b0c9d 100644 (file)
@@ -50,7 +50,7 @@ import java.util.*;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
-class Manager {
+class Manager implements TextSecure {
     private final static String URL = "https://textsecure-service.whispersystems.org";
     private final static TrustStore TRUST_STORE = new WhisperTrustStore();
 
@@ -136,7 +136,7 @@ class Manager {
         accountManager = new TextSecureAccountManager(URL, TRUST_STORE, username, password, USER_AGENT);
     }
 
-    public void save() {
+    private void save() {
         ObjectNode rootNode = jsonProcessot.createObjectNode();
         rootNode.put("username", username)
                 .put("password", password)
@@ -160,6 +160,7 @@ class Manager {
         axolotlStore = new JsonAxolotlStore(identityKey, registrationId);
         groupStore = new JsonGroupStore();
         registered = false;
+        save();
     }
 
     public boolean isRegistered() {
@@ -177,6 +178,7 @@ class Manager {
             accountManager.requestSmsVerificationCode();
 
         registered = false;
+        save();
     }
 
     private static final int BATCH_SIZE = 100;
@@ -194,6 +196,8 @@ class Manager {
         }
 
         preKeyIdOffset = (preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE;
+        save();
+
         return records;
     }
 
@@ -210,6 +214,7 @@ class Manager {
         PreKeyRecord record = new PreKeyRecord(Medium.MAX_VALUE, keyPair);
 
         axolotlStore.storePreKey(Medium.MAX_VALUE, record);
+        save();
 
         return record;
     }
@@ -222,6 +227,7 @@ class Manager {
 
             axolotlStore.storeSignedPreKey(nextSignedPreKeyId, record);
             nextSignedPreKeyId = (nextSignedPreKeyId + 1) % Medium.MAX_VALUE;
+            save();
 
             return record;
         } catch (InvalidKeyException e) {
@@ -244,6 +250,7 @@ class Manager {
         SignedPreKeyRecord signedPreKeyRecord = generateSignedPreKey(axolotlStore.getIdentityKeyPair());
 
         accountManager.setPreKeys(axolotlStore.getIdentityKeyPair().getPublicKey(), lastResortKey, signedPreKeyRecord, oneTimePreKeys);
+        save();
     }
 
 
@@ -270,6 +277,7 @@ class Manager {
         return new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null);
     }
 
+    @Override
     public void sendGroupMessage(String messageText, List<String> attachments,
                                  byte[] groupId)
             throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
@@ -351,9 +359,17 @@ class Manager {
         return g.groupId;
     }
 
+    @Override
+    public void sendMessage(String message, List<String> attachments, String recipient)
+            throws EncapsulatedExceptions, AttachmentInvalidException, IOException {
+        List<String> recipients = new ArrayList<>(1);
+        recipients.add(recipient);
+        sendMessage(message, attachments, recipients);
+    }
+
     public void sendMessage(String messageText, List<String> attachments,
                             Collection<String> recipients)
-            throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
+            throws IOException, EncapsulatedExceptions, AttachmentInvalidException {
         final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
         if (attachments != null) {
             messageBuilder.withAttachments(getTextSecureAttachments(attachments));
@@ -383,6 +399,7 @@ class Manager {
             } catch (InvalidNumberException e) {
                 System.err.println("Failed to add recipient \"" + recipient + "\": " + e.getMessage());
                 System.err.println("Aborting sending.");
+                save();
                 return;
             }
         }
@@ -394,6 +411,7 @@ class Manager {
                 handleEndSession(recipient.getNumber());
             }
         }
+        save();
     }
 
     private TextSecureContent decryptMessage(TextSecureEnvelope envelope) {
@@ -498,6 +516,7 @@ class Manager {
                             }
                         }
                     }
+                    save();
                     handler.handleMessage(envelope, content, group);
                 } catch (TimeoutException e) {
                     if (returnOnTimeout)
@@ -505,7 +524,6 @@ class Manager {
                 } catch (InvalidVersionException e) {
                     System.err.println("Ignoring error: " + e.getMessage());
                 }
-                save();
             }
         } finally {
             if (messagePipe != null)
@@ -581,4 +599,9 @@ class Manager {
     private void setGroupInfo(GroupInfo group) {
         groupStore.updateGroup(group);
     }
+
+    @Override
+    public boolean isRemote() {
+        return false;
+    }
 }