X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/85025d2e25a9993162d5c859858e7ecb6149fea6..31434ac5ec6cee7d45ab81759de7ae647f675b11:/src/main/java/org/asamk/signal/manager/Manager.java diff --git a/src/main/java/org/asamk/signal/manager/Manager.java b/src/main/java/org/asamk/signal/manager/Manager.java index 3ca49958..8d0d46c8 100644 --- a/src/main/java/org/asamk/signal/manager/Manager.java +++ b/src/main/java/org/asamk/signal/manager/Manager.java @@ -1,5 +1,5 @@ /* - Copyright (C) 2015-2018 AsamK + Copyright (C) 2015-2020 AsamK and contributors This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,7 +17,11 @@ package org.asamk.signal.manager; import org.asamk.Signal; -import org.asamk.signal.*; +import org.asamk.signal.AttachmentInvalidException; +import org.asamk.signal.GroupNotFoundException; +import org.asamk.signal.NotAGroupMemberException; +import org.asamk.signal.TrustLevel; +import org.asamk.signal.UserAlreadyExists; import org.asamk.signal.storage.SignalAccount; import org.asamk.signal.storage.contacts.ContactInfo; import org.asamk.signal.storage.groups.GroupInfo; @@ -49,6 +53,7 @@ import org.whispersystems.libsignal.state.PreKeyRecord; import org.whispersystems.libsignal.state.SignedPreKeyRecord; import org.whispersystems.libsignal.util.KeyHelper; import org.whispersystems.libsignal.util.Medium; +import org.whispersystems.libsignal.util.Pair; import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.signalservice.api.SignalServiceAccountManager; import org.whispersystems.signalservice.api.SignalServiceMessagePipe; @@ -66,6 +71,7 @@ import org.whispersystems.signalservice.api.messages.SignalServiceContent; import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.api.messages.SignalServiceGroup; +import org.whispersystems.signalservice.api.messages.multidevice.BlockedListMessage; import org.whispersystems.signalservice.api.messages.multidevice.ContactsMessage; import org.whispersystems.signalservice.api.messages.multidevice.DeviceContact; import org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsInputStream; @@ -145,6 +151,14 @@ public class Manager implements Signal { return username; } + private SignalServiceAddress getSelfAddress() { + return new SignalServiceAddress(null, username); + } + + private SignalServiceAccountManager getSignalServiceAccountManager() { + return new SignalServiceAccountManager(BaseConfig.serviceConfiguration, null, account.getUsername(), account.getPassword(), account.getDeviceId(), BaseConfig.USER_AGENT, timer); + } + private IdentityKey getIdentity() { return account.getSignalProtocolStore().getIdentityKeyPair().getPublicKey(); } @@ -179,7 +193,7 @@ public class Manager implements Signal { migrateLegacyConfigs(); - accountManager = new SignalServiceAccountManager(BaseConfig.serviceConfiguration, null, username, account.getPassword(), account.getDeviceId(), BaseConfig.USER_AGENT, timer); + accountManager = getSignalServiceAccountManager(); try { if (account.isRegistered() && accountManager.getPreKeysCount() < BaseConfig.PREKEY_MINIMUM_COUNT) { refreshPreKeys(); @@ -238,7 +252,7 @@ public class Manager implements Signal { createNewIdentity(); } account.setPassword(KeyUtils.createPassword()); - accountManager = new SignalServiceAccountManager(BaseConfig.serviceConfiguration, null, account.getUsername(), account.getPassword(), BaseConfig.USER_AGENT, timer); + accountManager = getSignalServiceAccountManager(); if (voiceVerification) { accountManager.requestVoiceVerificationCode(Locale.getDefault(), Optional.absent(), Optional.absent()); @@ -251,7 +265,7 @@ public class Manager implements Signal { } public void updateAccountAttributes() throws IOException { - accountManager.setAccountAttributes(account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, account.getRegistrationLockPin(), getSelfUnidentifiedAccessKey(), false); + accountManager.setAccountAttributes(account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, account.getRegistrationLockPin(), account.getRegistrationLock(), getSelfUnidentifiedAccessKey(), false); } public void setProfileName(String name) throws IOException { @@ -283,7 +297,7 @@ public class Manager implements Signal { createNewIdentity(); } account.setPassword(KeyUtils.createPassword()); - accountManager = new SignalServiceAccountManager(BaseConfig.serviceConfiguration, null, username, account.getPassword(), BaseConfig.USER_AGENT, timer); + accountManager = getSignalServiceAccountManager(); String uuid = accountManager.getNewDeviceUuid(); return Utils.createDeviceLinkUri(new Utils.DeviceLinkInfo(uuid, getIdentity().getPublicKey())); @@ -382,7 +396,7 @@ public class Manager implements Signal { verificationCode = verificationCode.replace("-", ""); account.setSignalingKey(KeyUtils.createSignalingKey()); // TODO make unrestricted unidentified access configurable - accountManager.verifyAccountWithCode(verificationCode, account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, pin, getSelfUnidentifiedAccessKey(), false); + accountManager.verifyAccountWithCode(verificationCode, account.getSignalingKey(), account.getSignalProtocolStore().getLocalRegistrationId(), true, pin, null, getSelfUnidentifiedAccessKey(), false); //accountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID))); account.setRegistered(true); @@ -393,11 +407,12 @@ public class Manager implements Signal { } public void setRegistrationLockPin(Optional pin) throws IOException { - accountManager.setPin(pin); if (pin.isPresent()) { account.setRegistrationLockPin(pin.get()); + throw new RuntimeException("Not implemented anymore, will be replaced with KBS"); } else { account.setRegistrationLockPin(null); + accountManager.removeV1Pin(); } account.save(); } @@ -414,6 +429,11 @@ public class Manager implements Signal { return new SignalServiceMessageReceiver(BaseConfig.serviceConfiguration, null, username, account.getPassword(), account.getDeviceId(), account.getSignalingKey(), BaseConfig.USER_AGENT, null, timer); } + private SignalServiceMessageSender getMessageSender() { + return new SignalServiceMessageSender(BaseConfig.serviceConfiguration, null, username, account.getPassword(), + account.getDeviceId(), account.getSignalProtocolStore(), BaseConfig.USER_AGENT, account.isMultiDevice(), Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.absent()); + } + private Optional createGroupAvatarAttachment(byte[] groupId) throws IOException { File file = getGroupAvatarFile(groupId); if (!file.exists()) { @@ -476,6 +496,26 @@ public class Manager implements Signal { sendMessageLegacy(messageBuilder, membersSend); } + public void sendGroupMessageReaction(String emoji, boolean remove, SignalServiceAddress targetAuthor, + long targetSentTimestamp, byte[] groupId) + 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()); + if (groupId != null) { + SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.DELIVER) + .withId(groupId) + .build(); + messageBuilder.asGroupMessage(group); + } + final GroupInfo g = getGroupForSending(groupId); + // Don't send group message to ourself + final List membersSend = new ArrayList<>(g.members); + membersSend.remove(this.username); + sendMessageLegacy(messageBuilder, membersSend); + } + public void sendQuitGroupMessage(byte[] groupId) throws GroupNotFoundException, IOException, EncapsulatedExceptions { SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.QUIT) .withId(groupId) @@ -569,15 +609,10 @@ public class Manager implements Signal { } private SignalServiceDataMessage.Builder getGroupUpdateMessageBuilder(GroupInfo g) { - ArrayList members = new ArrayList<>(g.members.size()); - for (String member : g.members) { - members.add(new SignalServiceAddress(null, member)); - } - SignalServiceGroup.Builder group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.UPDATE) .withId(g.groupId) .withName(g.name) - .withMembers(members); + .withMembers(new ArrayList<>(g.getMembers())); File aFile = getGroupAvatarFile(g.groupId); if (aFile.exists()) { @@ -635,12 +670,35 @@ public class Manager implements Signal { throws IOException, EncapsulatedExceptions, AttachmentInvalidException { final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText); if (attachments != null) { - messageBuilder.withAttachments(Utils.getSignalServiceAttachments(attachments)); + List attachmentStreams = Utils.getSignalServiceAttachments(attachments); + + // Upload attachments here, so we only upload once even for multiple recipients + SignalServiceMessageSender messageSender = getMessageSender(); + List attachmentPointers = new ArrayList<>(attachmentStreams.size()); + for (SignalServiceAttachment attachment : attachmentStreams) { + if (attachment.isStream()) { + attachmentPointers.add(messageSender.uploadAttachment(attachment.asStream())); + } else if (attachment.isPointer()) { + attachmentPointers.add(attachment.asPointer()); + } + } + + messageBuilder.withAttachments(attachmentPointers); } messageBuilder.withProfileKey(account.getProfileKey()); sendMessageLegacy(messageBuilder, recipients); } + public void sendMessageReaction(String emoji, boolean remove, SignalServiceAddress targetAuthor, + long targetSentTimestamp, List recipients) + 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()); + sendMessageLegacy(messageBuilder, recipients); + } + @Override public void sendEndSessionMessage(List recipients) throws IOException, EncapsulatedExceptions { SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder() @@ -650,8 +708,9 @@ public class Manager implements Signal { } @Override - public String getContactName(String number) { - ContactInfo contact = account.getContactStore().getContact(number); + public String getContactName(String number) throws InvalidNumberException { + String canonicalizedNumber = Utils.canonicalizeNumber(number, username); + ContactInfo contact = account.getContactStore().getContact(canonicalizedNumber); if (contact == null) { return ""; } else { @@ -660,20 +719,50 @@ public class Manager implements Signal { } @Override - public void setContactName(String number, String name) { + public void setContactName(String number, String name) throws InvalidNumberException { + String canonicalizedNumber = Utils.canonicalizeNumber(number, username); + ContactInfo contact = account.getContactStore().getContact(canonicalizedNumber); + if (contact == null) { + contact = new ContactInfo(); + contact.number = canonicalizedNumber; + System.err.println("Add contact " + canonicalizedNumber + " named " + name); + } else { + System.err.println("Updating contact " + canonicalizedNumber + " name " + contact.name + " -> " + name); + } + contact.name = name; + account.getContactStore().updateContact(contact); + account.save(); + } + + @Override + public void setContactBlocked(String number, boolean blocked) throws InvalidNumberException { + number = Utils.canonicalizeNumber(number, username); ContactInfo contact = account.getContactStore().getContact(number); if (contact == null) { contact = new ContactInfo(); contact.number = number; - System.err.println("Add contact " + number + " named " + name); + System.err.println("Adding and " + (blocked ? "blocking" : "unblocking") + " contact " + number); } else { - System.err.println("Updating contact " + number + " name " + contact.name + " -> " + name); + System.err.println((blocked ? "Blocking" : "Unblocking") + " contact " + number); } - contact.name = name; + contact.blocked = blocked; account.getContactStore().updateContact(contact); account.save(); } + @Override + public void setGroupBlocked(final byte[] groupId, final boolean blocked) throws GroupNotFoundException { + GroupInfo group = getGroup(groupId); + if (group == null) { + throw new GroupNotFoundException(groupId); + } else { + System.err.println((blocked ? "Blocking" : "Unblocking") + " group " + Base64.encodeBytes(groupId)); + group.blocked = blocked; + account.getGroupStore().updateGroup(group); + account.save(); + } + } + @Override public List getGroupIds() { List groups = getGroups(); @@ -802,8 +891,7 @@ public class Manager implements Signal { private void sendSyncMessage(SignalServiceSyncMessage message) throws IOException, UntrustedIdentityException { - SignalServiceMessageSender messageSender = new SignalServiceMessageSender(BaseConfig.serviceConfiguration, null, username, account.getPassword(), - account.getDeviceId(), account.getSignalProtocolStore(), BaseConfig.USER_AGENT, account.isMultiDevice(), Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.absent()); + SignalServiceMessageSender messageSender = getMessageSender(); try { messageSender.sendMessage(message, getAccessForSync()); } catch (UntrustedIdentityException e) { @@ -847,8 +935,7 @@ public class Manager implements Signal { SignalServiceDataMessage message = null; try { - SignalServiceMessageSender messageSender = new SignalServiceMessageSender(BaseConfig.serviceConfiguration, null, username, account.getPassword(), - account.getDeviceId(), account.getSignalProtocolStore(), BaseConfig.USER_AGENT, account.isMultiDevice(), Optional.fromNullable(messagePipe), Optional.fromNullable(unidentifiedMessagePipe), Optional.absent()); + SignalServiceMessageSender messageSender = getMessageSender(); message = messageBuilder.build(); if (message.getGroupInfo().isPresent()) { @@ -865,14 +952,14 @@ public class Manager implements Signal { account.getSignalProtocolStore().saveIdentity(e.getIdentifier(), e.getIdentityKey(), TrustLevel.UNTRUSTED); return Collections.emptyList(); } - } else if (recipientsTS.size() == 1 && recipientsTS.contains(new SignalServiceAddress(null, username))) { - SignalServiceAddress recipient = new SignalServiceAddress(null, username); + } else if (recipientsTS.size() == 1 && recipientsTS.contains(getSelfAddress())) { + SignalServiceAddress recipient = getSelfAddress(); final Optional unidentifiedAccess = getAccessFor(recipient); - SentTranscriptMessage transcript = new SentTranscriptMessage(Optional.of(new SignalServiceAddress(null, recipient.getNumber().get())), + SentTranscriptMessage transcript = new SentTranscriptMessage(Optional.of(recipient), message.getTimestamp(), message, message.getExpiresInSeconds(), - Collections.singletonMap(new SignalServiceAddress(null, recipient.getNumber()), unidentifiedAccess.isPresent()), + Collections.singletonMap(recipient, unidentifiedAccess.isPresent()), false); SignalServiceSyncMessage syncMessage = SignalServiceSyncMessage.forSentTranscript(transcript); @@ -916,7 +1003,7 @@ public class Manager implements Signal { } private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws InvalidMetadataMessageException, ProtocolInvalidMessageException, ProtocolDuplicateMessageException, ProtocolLegacyMessageException, ProtocolInvalidKeyIdException, InvalidMetadataVersionException, ProtocolInvalidVersionException, ProtocolNoSessionException, ProtocolInvalidKeyException, ProtocolUntrustedIdentityException, SelfSendException, UnsupportedDataMessageException { - SignalServiceCipher cipher = new SignalServiceCipher(new SignalServiceAddress(null, username), account.getSignalProtocolStore(), Utils.getCertificateValidator()); + SignalServiceCipher cipher = new SignalServiceCipher(getSelfAddress(), account.getSignalProtocolStore(), Utils.getCertificateValidator()); try { return cipher.decrypt(envelope); } catch (ProtocolUntrustedIdentityException e) { @@ -930,7 +1017,7 @@ public class Manager implements Signal { account.getSignalProtocolStore().deleteAllSessions(source); } - private void handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, String source, String destination, boolean ignoreAttachments) { + private void handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, String source, SignalServiceAddress destination, boolean ignoreAttachments) { String threadId; if (message.getGroupInfo().isPresent()) { SignalServiceGroup groupInfo = message.getGroupInfo().get(); @@ -958,11 +1045,7 @@ public class Manager implements Signal { } if (groupInfo.getMembers().isPresent()) { - List members = new ArrayList<>(groupInfo.getMembers().get().size()); - for (SignalServiceAddress address : groupInfo.getMembers().get()) { - members.add(address.getNumber().get()); - } - group.members.addAll(members); + group.addMembers(groupInfo.getMembers().get()); } account.getGroupStore().updateGroup(group); @@ -1002,13 +1085,13 @@ public class Manager implements Signal { } } else { if (isSync) { - threadId = destination; + threadId = destination.getNumber().get(); } else { threadId = source; } } if (message.isEndSession()) { - handleEndSession(isSync ? destination : source); + handleEndSession(isSync ? destination.getNumber().get() : source); } if (message.isExpirationUpdate() || message.getBody().isPresent()) { ThreadInfo thread = account.getThreadStore().getThread(threadId); @@ -1043,6 +1126,19 @@ public class Manager implements Signal { } contact.profileKey = Base64.encodeBytes(message.getProfileKey().get()); } + if (message.getPreviews().isPresent()) { + final List previews = message.getPreviews().get(); + for (SignalServiceDataMessage.Preview preview : previews) { + if (preview.getImage().isPresent() && preview.getImage().get().isPointer()) { + SignalServiceAttachmentPointer attachment = preview.getImage().get().asPointer(); + try { + retrieveAttachment(attachment); + } catch (IOException | InvalidMessageException e) { + System.err.println("Failed to retrieve attachment (" + attachment.getId() + "): " + e.getMessage()); + } + } + } + } } private void retryFailedReceivedMessages(ReceiveMessageHandler handler, boolean ignoreAttachments) { @@ -1135,7 +1231,9 @@ public class Manager implements Signal { handleMessage(envelope, content, ignoreAttachments); } account.save(); - handler.handleMessage(envelope, content, exception); + if (!isMessageBlocked(envelope, content)) { + handler.handleMessage(envelope, content, exception); + } if (!(exception instanceof ProtocolUntrustedIdentityException)) { File cacheFile = null; try { @@ -1156,18 +1254,51 @@ public class Manager implements Signal { } } + private boolean isMessageBlocked(SignalServiceEnvelope envelope, SignalServiceContent content) { + SignalServiceAddress source; + if (!envelope.isUnidentifiedSender() && envelope.hasSource()) { + source = envelope.getSourceAddress(); + } else if (content != null) { + source = content.getSender(); + } else { + return false; + } + ContactInfo sourceContact = getContact(source.getNumber().get()); + if (sourceContact != null && sourceContact.blocked) { + return true; + } + + if (content != null && content.getDataMessage().isPresent()) { + SignalServiceDataMessage message = content.getDataMessage().get(); + if (message.getGroupInfo().isPresent()) { + SignalServiceGroup groupInfo = message.getGroupInfo().get(); + GroupInfo group = getGroup(groupInfo.getGroupId()); + if (groupInfo.getType() == SignalServiceGroup.Type.DELIVER && group != null && group.blocked) { + return true; + } + } + } + return false; + } + private void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, boolean ignoreAttachments) { if (content != null) { + SignalServiceAddress sender; + if (!envelope.isUnidentifiedSender() && envelope.hasSource()) { + sender = envelope.getSourceAddress(); + } else { + sender = content.getSender(); + } if (content.getDataMessage().isPresent()) { SignalServiceDataMessage message = content.getDataMessage().get(); - handleSignalServiceDataMessage(message, false, envelope.getSourceE164().get(), username, ignoreAttachments); + handleSignalServiceDataMessage(message, false, sender.getNumber().get(), getSelfAddress(), ignoreAttachments); } if (content.getSyncMessage().isPresent()) { account.setMultiDevice(true); SignalServiceSyncMessage syncMessage = content.getSyncMessage().get(); if (syncMessage.getSent().isPresent()) { - SignalServiceDataMessage message = syncMessage.getSent().get().getMessage(); - handleSignalServiceDataMessage(message, true, envelope.getSourceE164().get(), syncMessage.getSent().get().getDestination().get().getNumber().get(), ignoreAttachments); + SentTranscriptMessage message = syncMessage.getSent().get(); + handleSignalServiceDataMessage(message.getMessage(), true, sender.getNumber().get(), message.getDestination().orNull(), ignoreAttachments); } if (syncMessage.getRequest().isPresent()) { RequestMessage rm = syncMessage.getRequest().get(); @@ -1185,7 +1316,14 @@ public class Manager implements Signal { e.printStackTrace(); } } - // TODO Handle rm.isBlockedListRequest(); rm.isConfigurationRequest(); + if (rm.isBlockedListRequest()) { + try { + sendBlockedList(); + } catch (UntrustedIdentityException | IOException e) { + e.printStackTrace(); + } + } + // TODO Handle rm.isConfigurationRequest(); } if (syncMessage.getGroups().isPresent()) { File tmpFile = null; @@ -1202,12 +1340,9 @@ public class Manager implements Signal { if (g.getName().isPresent()) { syncGroup.name = g.getName().get(); } - List members = new ArrayList<>(g.getMembers().size()); - for (SignalServiceAddress member : g.getMembers()) { - members.add(member.getNumber().get()); - } - syncGroup.members.addAll(members); + syncGroup.addMembers(g.getMembers()); syncGroup.active = g.isActive(); + syncGroup.blocked = g.isBlocked(); if (g.getColor().isPresent()) { syncGroup.color = g.getColor().get(); } @@ -1215,6 +1350,8 @@ public class Manager implements Signal { if (g.getAvatar().isPresent()) { retrieveGroupAvatarAttachment(g.getAvatar().get(), syncGroup.groupId); } + syncGroup.inboxPosition = g.getInboxPosition().orNull(); + syncGroup.archived = g.isArchived(); account.getGroupStore().updateGroup(syncGroup); } } @@ -1231,7 +1368,23 @@ public class Manager implements Signal { } } if (syncMessage.getBlockedList().isPresent()) { - // TODO store list of blocked numbers + final BlockedListMessage blockedListMessage = syncMessage.getBlockedList().get(); + for (SignalServiceAddress address : blockedListMessage.getAddresses()) { + if (address.getNumber().isPresent()) { + try { + setContactBlocked(address.getNumber().get(), true); + } catch (InvalidNumberException e) { + e.printStackTrace(); + } + } + } + for (byte[] groupId : blockedListMessage.getGroupIds()) { + try { + setGroupBlocked(groupId, true); + } catch (GroupNotFoundException e) { + System.err.println("BlockedListMessage contained groupID that was not found in GroupStore: " + Base64.encodeBytes(groupId)); + } + } } if (syncMessage.getContacts().isPresent()) { File tmpFile = null; @@ -1245,7 +1398,7 @@ public class Manager implements Signal { } DeviceContact c; while ((c = s.read()) != null) { - if (c.getAddress().getNumber().get().equals(account.getUsername()) && c.getProfileKey().isPresent()) { + if (c.getAddress().matches(account.getSelfAddress()) && c.getProfileKey().isPresent()) { account.setProfileKey(c.getProfileKey().get()); } ContactInfo contact = account.getContactStore().getContact(c.getAddress().getNumber().get()); @@ -1275,9 +1428,9 @@ public class Manager implements Signal { thread.messageExpirationTime = c.getExpirationTimer().get(); account.getThreadStore().updateThread(thread); } - if (c.isBlocked()) { - // TODO store list of blocked numbers - } + contact.blocked = c.isBlocked(); + contact.inboxPosition = c.getInboxPosition().orNull(); + contact.archived = c.isArchived(); account.getContactStore().updateContact(contact); if (c.getAvatar().isPresent()) { @@ -1402,14 +1555,10 @@ public class Manager implements Signal { DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(fos); for (GroupInfo record : account.getGroupStore().getGroups()) { ThreadInfo info = account.getThreadStore().getThread(Base64.encodeBytes(record.groupId)); - List members = new ArrayList<>(record.members.size()); - for (String member : record.members) { - members.add(new SignalServiceAddress(null, member)); - } out.write(new DeviceGroup(record.groupId, Optional.fromNullable(record.name), - members, createGroupAvatarAttachment(record.groupId), + new ArrayList<>(record.getMembers()), createGroupAvatarAttachment(record.groupId), record.active, Optional.fromNullable(info != null ? info.messageExpirationTime : null), - Optional.fromNullable(record.color), false)); + Optional.fromNullable(record.color), record.blocked, Optional.fromNullable(record.inboxPosition), record.archived)); } } @@ -1450,25 +1599,25 @@ public class Manager implements Signal { } } if (currentIdentity != null) { - verifiedMessage = new VerifiedMessage(new SignalServiceAddress(null, record.number), currentIdentity.getIdentityKey(), currentIdentity.getTrustLevel().toVerifiedState(), currentIdentity.getDateAdded().getTime()); + verifiedMessage = new VerifiedMessage(record.getAddress(), currentIdentity.getIdentityKey(), currentIdentity.getTrustLevel().toVerifiedState(), currentIdentity.getDateAdded().getTime()); } } byte[] profileKey = record.profileKey == null ? null : Base64.decode(record.profileKey); - // TODO store list of blocked numbers - boolean blocked = false; - out.write(new DeviceContact(new SignalServiceAddress(null, record.number), Optional.fromNullable(record.name), + out.write(new DeviceContact(record.getAddress(), Optional.fromNullable(record.name), createContactAvatarAttachment(record.number), Optional.fromNullable(record.color), - Optional.fromNullable(verifiedMessage), Optional.fromNullable(profileKey), blocked, Optional.fromNullable(info != null ? info.messageExpirationTime : null))); + Optional.fromNullable(verifiedMessage), Optional.fromNullable(profileKey), record.blocked, + Optional.fromNullable(info != null ? info.messageExpirationTime : null), + Optional.fromNullable(record.inboxPosition), record.archived)); } if (account.getProfileKey() != null) { // Send our own profile key as well - out.write(new DeviceContact(new SignalServiceAddress(null, account.getUsername()), + out.write(new DeviceContact(account.getSelfAddress(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.of(account.getProfileKey()), - false, Optional.absent())); + false, Optional.absent(), Optional.absent(), false)); } } @@ -1492,8 +1641,24 @@ public class Manager implements Signal { } } - private void sendVerifiedMessage(String destination, IdentityKey identityKey, TrustLevel trustLevel) throws IOException, UntrustedIdentityException { - VerifiedMessage verifiedMessage = new VerifiedMessage(new SignalServiceAddress(null, destination), identityKey, trustLevel.toVerifiedState(), System.currentTimeMillis()); + private void sendBlockedList() throws IOException, UntrustedIdentityException { + List addresses = new ArrayList<>(); + for (ContactInfo record : account.getContactStore().getContacts()) { + if (record.blocked) { + addresses.add(record.getAddress()); + } + } + List groupIds = new ArrayList<>(); + for (GroupInfo record : account.getGroupStore().getGroups()) { + if (record.blocked) { + groupIds.add(record.groupId); + } + } + sendSyncMessage(SignalServiceSyncMessage.forBlocked(new BlockedListMessage(addresses, groupIds))); + } + + private void sendVerifiedMessage(SignalServiceAddress destination, IdentityKey identityKey, TrustLevel trustLevel) throws IOException, UntrustedIdentityException { + VerifiedMessage verifiedMessage = new VerifiedMessage(destination, identityKey, trustLevel.toVerifiedState(), System.currentTimeMillis()); sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessage)); } @@ -1513,8 +1678,9 @@ public class Manager implements Signal { return account.getSignalProtocolStore().getIdentities(); } - public List getIdentities(String number) { - return account.getSignalProtocolStore().getIdentities(number); + public Pair> getIdentities(String number) throws InvalidNumberException { + String canonicalizedNumber = Utils.canonicalizeNumber(number, username); + return new Pair<>(canonicalizedNumber, account.getSignalProtocolStore().getIdentities(canonicalizedNumber)); } /** @@ -1535,7 +1701,7 @@ public class Manager implements Signal { account.getSignalProtocolStore().saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED); try { - sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED); + sendVerifiedMessage(new SignalServiceAddress(null, name), id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED); } catch (IOException | UntrustedIdentityException e) { e.printStackTrace(); } @@ -1563,7 +1729,7 @@ public class Manager implements Signal { account.getSignalProtocolStore().saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED); try { - sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED); + sendVerifiedMessage(new SignalServiceAddress(null, name), id.getIdentityKey(), TrustLevel.TRUSTED_VERIFIED); } catch (IOException | UntrustedIdentityException e) { e.printStackTrace(); } @@ -1587,7 +1753,7 @@ public class Manager implements Signal { if (id.getTrustLevel() == TrustLevel.UNTRUSTED) { account.getSignalProtocolStore().saveIdentity(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED); try { - sendVerifiedMessage(name, id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED); + sendVerifiedMessage(new SignalServiceAddress(null, name), id.getIdentityKey(), TrustLevel.TRUSTED_UNVERIFIED); } catch (IOException | UntrustedIdentityException e) { e.printStackTrace(); }