]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/Manager.java
Skip invalid contact records
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / Manager.java
index 0f75f9093806e4e2f3470252c699234736c0b305..08789d9581589c5a7ada04be362e8fc17f395361 100644 (file)
@@ -121,6 +121,7 @@ import org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage
 import org.whispersystems.signalservice.api.profiles.ProfileAndCredential;
 import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
 import org.whispersystems.signalservice.api.push.SignalServiceAddress;
+import org.whispersystems.signalservice.api.push.exceptions.ConflictException;
 import org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException;
 import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
 import org.whispersystems.signalservice.api.util.DeviceNameUtil;
@@ -900,19 +901,37 @@ public class Manager implements Closeable {
         var group = getGroupForUpdating(groupId);
 
         if (group instanceof GroupInfoV2) {
-            return updateGroupV2((GroupInfoV2) group,
-                    name,
-                    description,
-                    members,
-                    removeMembers,
-                    admins,
-                    removeAdmins,
-                    resetGroupLink,
-                    groupLinkState,
-                    addMemberPermission,
-                    editDetailsPermission,
-                    avatarFile,
-                    expirationTimer);
+            try {
+                return updateGroupV2((GroupInfoV2) group,
+                        name,
+                        description,
+                        members,
+                        removeMembers,
+                        admins,
+                        removeAdmins,
+                        resetGroupLink,
+                        groupLinkState,
+                        addMemberPermission,
+                        editDetailsPermission,
+                        avatarFile,
+                        expirationTimer);
+            } catch (ConflictException e) {
+                // Detected conflicting update, refreshing group and trying again
+                group = getGroup(groupId, true);
+                return updateGroupV2((GroupInfoV2) group,
+                        name,
+                        description,
+                        members,
+                        removeMembers,
+                        admins,
+                        removeAdmins,
+                        resetGroupLink,
+                        groupLinkState,
+                        addMemberPermission,
+                        editDetailsPermission,
+                        avatarFile,
+                        expirationTimer);
+            }
         }
 
         final var gv1 = (GroupInfoV1) group;
@@ -2221,7 +2240,16 @@ public class Manager implements Closeable {
                         try (var attachmentAsStream = retrieveAttachmentAsStream(groupsMessage.asPointer(), tmpFile)) {
                             var s = new DeviceGroupsInputStream(attachmentAsStream);
                             DeviceGroup g;
-                            while ((g = s.read()) != null) {
+                            while (true) {
+                                try {
+                                    g = s.read();
+                                } catch (IOException e) {
+                                    logger.warn("Sync groups contained invalid group, ignoring: {}", e.getMessage());
+                                    continue;
+                                }
+                                if (g == null) {
+                                    break;
+                                }
                                 var syncGroup = account.getGroupStore().getOrCreateGroupV1(GroupId.v1(g.getId()));
                                 if (syncGroup != null) {
                                     if (g.getName().isPresent()) {
@@ -2292,7 +2320,17 @@ public class Manager implements Closeable {
                                 .asPointer(), tmpFile)) {
                             var s = new DeviceContactsInputStream(attachmentAsStream);
                             DeviceContact c;
-                            while ((c = s.read()) != null) {
+                            while (true) {
+                                try {
+                                    c = s.read();
+                                } catch (IOException e) {
+                                    logger.warn("Sync contacts contained invalid contact, ignoring: {}",
+                                            e.getMessage());
+                                    continue;
+                                }
+                                if (c == null) {
+                                    break;
+                                }
                                 if (c.getAddress().matches(account.getSelfAddress()) && c.getProfileKey().isPresent()) {
                                     account.setProfileKey(c.getProfileKey().get());
                                 }
@@ -2703,8 +2741,12 @@ public class Manager implements Closeable {
     }
 
     public GroupInfo getGroup(GroupId groupId) {
+        return getGroup(groupId, false);
+    }
+
+    public GroupInfo getGroup(GroupId groupId, boolean forceUpdate) {
         final var group = account.getGroupStore().getGroup(groupId);
-        if (group instanceof GroupInfoV2 && ((GroupInfoV2) group).getGroup() == null) {
+        if (group instanceof GroupInfoV2 && (forceUpdate || ((GroupInfoV2) group).getGroup() == null)) {
             final var groupSecretParams = GroupSecretParams.deriveFromMasterKey(((GroupInfoV2) group).getMasterKey());
             ((GroupInfoV2) group).setGroup(groupV2Helper.getDecryptedGroup(groupSecretParams), this::resolveRecipient);
             account.getGroupStore().updateGroup(group);