]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/cli/Manager.java
Add possibility to send messages via dbus daemon
[signal-cli] / src / main / java / cli / Manager.java
index b15457af5b5fde47b1fa0a837ab743338e0a6513..caa3fb5d85c3f523ac5f42f8728eb239e335122a 100644 (file)
@@ -136,7 +136,7 @@ class Manager implements TextSecure {
         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 implements TextSecure {
         axolotlStore = new JsonAxolotlStore(identityKey, registrationId);
         groupStore = new JsonGroupStore();
         registered = false;
+        save();
     }
 
     public boolean isRegistered() {
@@ -177,6 +178,7 @@ class Manager implements TextSecure {
             accountManager.requestSmsVerificationCode();
 
         registered = false;
+        save();
     }
 
     private static final int BATCH_SIZE = 100;
@@ -194,6 +196,8 @@ class Manager implements TextSecure {
         }
 
         preKeyIdOffset = (preKeyIdOffset + BATCH_SIZE + 1) % Medium.MAX_VALUE;
+        save();
+
         return records;
     }
 
@@ -210,6 +214,7 @@ class Manager implements TextSecure {
         PreKeyRecord record = new PreKeyRecord(Medium.MAX_VALUE, keyPair);
 
         axolotlStore.storePreKey(Medium.MAX_VALUE, record);
+        save();
 
         return record;
     }
@@ -222,6 +227,7 @@ class Manager implements TextSecure {
 
             axolotlStore.storeSignedPreKey(nextSignedPreKeyId, record);
             nextSignedPreKeyId = (nextSignedPreKeyId + 1) % Medium.MAX_VALUE;
+            save();
 
             return record;
         } catch (InvalidKeyException e) {
@@ -244,6 +250,7 @@ class Manager implements TextSecure {
         SignedPreKeyRecord signedPreKeyRecord = generateSignedPreKey(axolotlStore.getIdentityKeyPair());
 
         accountManager.setPreKeys(axolotlStore.getIdentityKeyPair().getPublicKey(), lastResortKey, signedPreKeyRecord, oneTimePreKeys);
+        save();
     }
 
 
@@ -286,7 +293,7 @@ class Manager implements TextSecure {
         }
         TextSecureDataMessage message = messageBuilder.build();
 
-        sendMessage(message, getGroupInfo(groupId).members);
+        sendMessage(message, groupStore.getGroup(groupId).members);
     }
 
     public void sendQuitGroupMessage(byte[] groupId) throws GroupNotFoundException, IOException, EncapsulatedExceptions {
@@ -298,7 +305,7 @@ class Manager implements TextSecure {
                 .asGroupMessage(group)
                 .build();
 
-        sendMessage(message, getGroupInfo(groupId).members);
+        sendMessage(message, groupStore.getGroup(groupId).members);
     }
 
     public byte[] sendUpdateGroupMessage(byte[] groupId, String name, Collection<String> members, String avatarFile) throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
@@ -308,7 +315,7 @@ class Manager implements TextSecure {
             g = new GroupInfo(Util.getSecretBytes(16));
             g.members.add(username);
         } else {
-            g = getGroupInfo(groupId);
+            g = groupStore.getGroup(groupId);
         }
 
         if (name != null) {
@@ -342,7 +349,7 @@ class Manager implements TextSecure {
             }
         }
 
-        setGroupInfo(g);
+        groupStore.updateGroup(g);
 
         TextSecureDataMessage message = TextSecureDataMessage.newBuilder()
                 .asGroupMessage(group.build())
@@ -360,8 +367,9 @@ class Manager implements TextSecure {
         sendMessage(message, attachments, recipients);
     }
 
+    @Override
     public void sendMessage(String messageText, List<String> attachments,
-                            Collection<String> recipients)
+                            List<String> recipients)
             throws IOException, EncapsulatedExceptions, AttachmentInvalidException {
         final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
         if (attachments != null) {
@@ -372,6 +380,7 @@ class Manager implements TextSecure {
         sendMessage(message, recipients);
     }
 
+    @Override
     public void sendEndSessionMessage(List<String> recipients) throws IOException, EncapsulatedExceptions {
         TextSecureDataMessage message = TextSecureDataMessage.newBuilder()
                 .asEndSessionMessage()
@@ -392,6 +401,7 @@ class Manager implements TextSecure {
             } catch (InvalidNumberException e) {
                 System.err.println("Failed to add recipient \"" + recipient + "\": " + e.getMessage());
                 System.err.println("Aborting sending.");
+                save();
                 return;
             }
         }
@@ -584,14 +594,6 @@ class Manager implements TextSecure {
         return new TextSecureAddress(e164number);
     }
 
-    private GroupInfo getGroupInfo(byte[] groupId) throws GroupNotFoundException {
-        return groupStore.getGroup(groupId);
-    }
-
-    private void setGroupInfo(GroupInfo group) {
-        groupStore.updateGroup(group);
-    }
-
     @Override
     public boolean isRemote() {
         return false;