]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/manager/Manager.java
Use legacy sender certificate, until uuid support is complete
[signal-cli] / src / main / java / org / asamk / signal / manager / Manager.java
index 0aada70364fa2802448136be4e12cfdd40886dae..b418d310e1a3e8acb1e6c2d6a2f828a22eeb8b2c 100644 (file)
@@ -99,7 +99,6 @@ import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage
 import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
 import org.whispersystems.signalservice.api.push.ContactTokenDetails;
 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
-import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
 import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
 import org.whispersystems.signalservice.api.push.exceptions.NetworkFailureException;
 import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
@@ -215,20 +214,15 @@ public class Manager implements Signal {
         migrateLegacyConfigs();
 
         accountManager = getSignalServiceAccountManager();
-        try {
-            if (account.isRegistered()) {
-                if (accountManager.getPreKeysCount() < BaseConfig.PREKEY_MINIMUM_COUNT) {
-                    refreshPreKeys();
-                    account.save();
-                }
-                if (account.getUuid() == null) {
-                    account.setUuid(accountManager.getOwnUuid());
-                    account.save();
-                }
+        if (account.isRegistered()) {
+            if (accountManager.getPreKeysCount() < BaseConfig.PREKEY_MINIMUM_COUNT) {
+                refreshPreKeys();
+                account.save();
+            }
+            if (account.getUuid() == null) {
+                account.setUuid(accountManager.getOwnUuid());
+                account.save();
             }
-        } catch (AuthorizationFailedException e) {
-            System.err.println("Authorization failed, was the number registered elsewhere?");
-            throw e;
         }
     }
 
@@ -264,6 +258,8 @@ public class Manager implements Signal {
         if (username == null) {
             account = SignalAccount.createTemporaryAccount(identityKey, registrationId);
         } else {
+            account.getSignalProtocolStore().saveIdentity(username, identityKey.getPublicKey(), TrustLevel.TRUSTED_VERIFIED);
+
             ProfileKey profileKey = KeyUtils.createProfileKey();
             account = SignalAccount.create(dataPath, username, identityKey, registrationId, profileKey);
             account.save();
@@ -279,6 +275,7 @@ public class Manager implements Signal {
             createNewIdentity();
         }
         account.setPassword(KeyUtils.createPassword());
+        account.setUuid(null);
         accountManager = getSignalServiceAccountManager();
 
         if (voiceVerification) {
@@ -548,8 +545,7 @@ public class Manager implements Signal {
             throws IOException, EncapsulatedExceptions, AttachmentInvalidException {
         SignalServiceDataMessage.Reaction reaction = new SignalServiceDataMessage.Reaction(emoji, remove, targetAuthor, targetSentTimestamp);
         final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
-                .withReaction(reaction)
-                .withProfileKey(account.getProfileKey().serialize());
+                .withReaction(reaction);
         if (groupId != null) {
             SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.DELIVER)
                     .withId(groupId)
@@ -714,7 +710,6 @@ public class Manager implements Signal {
 
             messageBuilder.withAttachments(attachmentPointers);
         }
-        messageBuilder.withProfileKey(account.getProfileKey().serialize());
         sendMessageLegacy(messageBuilder, getSignalServiceAddresses(recipients));
     }
 
@@ -723,8 +718,7 @@ public class Manager implements Signal {
             throws IOException, EncapsulatedExceptions, AttachmentInvalidException, InvalidNumberException {
         SignalServiceDataMessage.Reaction reaction = new SignalServiceDataMessage.Reaction(emoji, remove, targetAuthor, targetSentTimestamp);
         final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder()
-                .withReaction(reaction)
-                .withProfileKey(account.getProfileKey().serialize());
+                .withReaction(reaction);
         sendMessageLegacy(messageBuilder, getSignalServiceAddresses(recipients));
     }
 
@@ -1003,7 +997,9 @@ public class Manager implements Signal {
     }
 
     private byte[] getSenderCertificate() throws IOException {
-        byte[] certificate = accountManager.getSenderCertificate();
+        // TODO support UUID capable sender certificates
+        // byte[] certificate = accountManager.getSenderCertificate();
+        byte[] certificate = accountManager.getSenderCertificateLegacy();
         // TODO cache for a day
         return certificate;
     }
@@ -1141,8 +1137,11 @@ public class Manager implements Signal {
             if (canonicalizedNumber.equals(username)) {
                 signalServiceAddresses.add(account.getSelfAddress());
             } else {
-                // TODO get corresponding uuid
-                signalServiceAddresses.add(new SignalServiceAddress(null, canonicalizedNumber));
+                SignalServiceAddress address = new SignalServiceAddress(null, canonicalizedNumber);
+                ContactInfo contact = account.getContactStore().getContact(address);
+                signalServiceAddresses.add(contact == null
+                        ? address
+                        : contact.getAddress());
             }
         }
         return signalServiceAddresses;
@@ -1201,8 +1200,10 @@ public class Manager implements Signal {
                     ContactInfo contact = account.getContactStore().getContact(address);
                     if (contact != null) {
                         messageBuilder.withExpiration(contact.messageExpirationTime);
+                        messageBuilder.withProfileKey(account.getProfileKey().serialize());
                     } else {
                         messageBuilder.withExpiration(0);
+                        messageBuilder.withProfileKey(null);
                     }
                     message = messageBuilder.build();
                     try {
@@ -1342,18 +1343,24 @@ public class Manager implements Signal {
             }
         }
         if (message.getProfileKey().isPresent() && message.getProfileKey().get().length == 32) {
-            if (source.equals(account.getSelfAddress())) {
+            if (source.matches(account.getSelfAddress())) {
                 try {
                     this.account.setProfileKey(new ProfileKey(message.getProfileKey().get()));
                 } catch (InvalidInputException ignored) {
                 }
+                ContactInfo contact = account.getContactStore().getContact(source);
+                if (contact != null) {
+                    contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
+                    account.getContactStore().updateContact(contact);
+                }
+            } else {
+                ContactInfo contact = account.getContactStore().getContact(source);
+                if (contact == null) {
+                    contact = new ContactInfo(source);
+                }
+                contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
+                account.getContactStore().updateContact(contact);
             }
-            ContactInfo contact = account.getContactStore().getContact(source);
-            if (contact == null) {
-                contact = new ContactInfo(source);
-            }
-            contact.profileKey = Base64.encodeBytes(message.getProfileKey().get());
-            account.getContactStore().updateContact(contact);
         }
         if (message.getPreviews().isPresent()) {
             final List<SignalServiceDataMessage.Preview> previews = message.getPreviews().get();