Search in sources :

Example 46 with SignalServiceAddress

use of org.whispersystems.signalservice.api.push.SignalServiceAddress in project Signal-Android by WhisperSystems.

the class ContactRecordProcessor method merge.

@NonNull
SignalContactRecord merge(@NonNull SignalContactRecord remote, @NonNull SignalContactRecord local, @NonNull StorageKeyGenerator keyGenerator) {
    String givenName;
    String familyName;
    if (remote.getGivenName().isPresent() || remote.getFamilyName().isPresent()) {
        givenName = remote.getGivenName().or("");
        familyName = remote.getFamilyName().or("");
    } else {
        givenName = local.getGivenName().or("");
        familyName = local.getFamilyName().or("");
    }
    byte[] unknownFields = remote.serializeUnknownFields();
    ServiceId serviceId = local.getAddress().getServiceId() == ServiceId.UNKNOWN ? remote.getAddress().getServiceId() : local.getAddress().getServiceId();
    String e164 = remote.getAddress().getNumber().or(local.getAddress().getNumber()).orNull();
    SignalServiceAddress address = new SignalServiceAddress(serviceId, e164);
    byte[] profileKey = remote.getProfileKey().or(local.getProfileKey()).orNull();
    String username = remote.getUsername().or(local.getUsername()).or("");
    IdentityState identityState = remote.getIdentityState();
    byte[] identityKey = remote.getIdentityKey().or(local.getIdentityKey()).orNull();
    boolean blocked = remote.isBlocked();
    boolean profileSharing = remote.isProfileSharingEnabled();
    boolean archived = remote.isArchived();
    boolean forcedUnread = remote.isForcedUnread();
    long muteUntil = remote.getMuteUntil();
    boolean matchesRemote = doParamsMatch(remote, unknownFields, address, givenName, familyName, profileKey, username, identityState, identityKey, blocked, profileSharing, archived, forcedUnread, muteUntil);
    boolean matchesLocal = doParamsMatch(local, unknownFields, address, givenName, familyName, profileKey, username, identityState, identityKey, blocked, profileSharing, archived, forcedUnread, muteUntil);
    if (matchesRemote) {
        return remote;
    } else if (matchesLocal) {
        return local;
    } else {
        return new SignalContactRecord.Builder(keyGenerator.generate(), address, unknownFields).setGivenName(givenName).setFamilyName(familyName).setProfileKey(profileKey).setUsername(username).setIdentityState(identityState).setIdentityKey(identityKey).setBlocked(blocked).setProfileSharingEnabled(profileSharing).setArchived(archived).setForcedUnread(forcedUnread).setMuteUntil(muteUntil).build();
    }
}
Also used : SignalContactRecord(org.whispersystems.signalservice.api.storage.SignalContactRecord) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) IdentityState(org.whispersystems.signalservice.internal.storage.protos.ContactRecord.IdentityState) ServiceId(org.whispersystems.signalservice.api.push.ServiceId) NonNull(androidx.annotation.NonNull)

Example 47 with SignalServiceAddress

use of org.whispersystems.signalservice.api.push.SignalServiceAddress in project Signal-Android by WhisperSystems.

the class ContactRecordProcessor method getMatching.

@Override
@NonNull
Optional<SignalContactRecord> getMatching(@NonNull SignalContactRecord remote, @NonNull StorageKeyGenerator keyGenerator) {
    SignalServiceAddress address = remote.getAddress();
    Optional<RecipientId> byUuid = recipientDatabase.getByServiceId(address.getServiceId());
    Optional<RecipientId> byE164 = address.getNumber().isPresent() ? recipientDatabase.getByE164(address.getNumber().get()) : Optional.absent();
    return byUuid.or(byE164).transform(recipientDatabase::getRecordForSync).transform(settings -> {
        if (settings.getStorageId() != null) {
            return StorageSyncModels.localToRemoteRecord(settings);
        } else {
            Log.w(TAG, "Newly discovering a registered user via storage service. Saving a storageId for them.");
            recipientDatabase.updateStorageId(settings.getId(), keyGenerator.generate());
            RecipientRecord updatedSettings = Objects.requireNonNull(recipientDatabase.getRecordForSync(settings.getId()));
            return StorageSyncModels.localToRemoteRecord(updatedSettings);
        }
    }).transform(r -> r.getContact().get());
}
Also used : Context(android.content.Context) SignalDatabase(org.thoughtcrime.securesms.database.SignalDatabase) Arrays(java.util.Arrays) ACI(org.whispersystems.signalservice.api.push.ACI) NonNull(androidx.annotation.NonNull) RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) IdentityState(org.whispersystems.signalservice.internal.storage.protos.ContactRecord.IdentityState) Optional(org.whispersystems.libsignal.util.guava.Optional) Objects(java.util.Objects) Log(org.signal.core.util.logging.Log) Nullable(androidx.annotation.Nullable) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalContactRecord(org.whispersystems.signalservice.api.storage.SignalContactRecord) ServiceId(org.whispersystems.signalservice.api.push.ServiceId) RecipientRecord(org.thoughtcrime.securesms.database.model.RecipientRecord) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) RecipientRecord(org.thoughtcrime.securesms.database.model.RecipientRecord) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) NonNull(androidx.annotation.NonNull)

Example 48 with SignalServiceAddress

use of org.whispersystems.signalservice.api.push.SignalServiceAddress in project Signal-Android by WhisperSystems.

the class GroupV1MessageProcessor method handleGroupCreate.

@Nullable
private static Long handleGroupCreate(@NonNull Context context, @NonNull SignalServiceContent content, @NonNull SignalServiceGroup group, boolean outgoing) {
    GroupDatabase database = SignalDatabase.groups();
    GroupId.V1 id = GroupId.v1orThrow(group.getGroupId());
    GroupContext.Builder builder = createGroupContext(group);
    builder.setType(GroupContext.Type.UPDATE);
    SignalServiceAttachment avatar = group.getAvatar().orNull();
    List<RecipientId> members = new LinkedList<>();
    if (group.getMembers().isPresent()) {
        for (SignalServiceAddress member : group.getMembers().get()) {
            members.add(Recipient.externalGV1Member(member).getId());
        }
    }
    database.create(id, group.getName().orNull(), members, avatar != null && avatar.isPointer() ? avatar.asPointer() : null, null);
    Recipient sender = Recipient.externalHighTrustPush(context, content.getSender());
    if (sender.isSystemContact() || sender.isProfileSharing()) {
        Log.i(TAG, "Auto-enabling profile sharing because 'adder' is trusted. contact: " + sender.isSystemContact() + ", profileSharing: " + sender.isProfileSharing());
        SignalDatabase.recipients().setProfileSharing(Recipient.externalGroupExact(context, id).getId(), true);
    }
    return storeMessage(context, content, group, builder.build(), outgoing);
}
Also used : SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) Recipient(org.thoughtcrime.securesms.recipients.Recipient) GroupContext(org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext) SignalServiceGroupContext(org.whispersystems.signalservice.api.messages.SignalServiceGroupContext) LinkedList(java.util.LinkedList) Nullable(androidx.annotation.Nullable)

Example 49 with SignalServiceAddress

use of org.whispersystems.signalservice.api.push.SignalServiceAddress in project Signal-Android by WhisperSystems.

the class PushGroupUpdateJob method onRun.

@Override
public void onRun() throws IOException, UntrustedIdentityException {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    Recipient sourceRecipient = Recipient.resolved(source);
    if (sourceRecipient.isUnregistered()) {
        Log.w(TAG, sourceRecipient.getId() + " not registered!");
        return;
    }
    GroupDatabase groupDatabase = SignalDatabase.groups();
    Optional<GroupRecord> record = groupDatabase.getGroup(groupId);
    SignalServiceAttachment avatar = null;
    if (record == null || !record.isPresent()) {
        Log.w(TAG, "No information for group record info request: " + groupId.toString());
        return;
    }
    if (AvatarHelper.hasAvatar(context, record.get().getRecipientId())) {
        avatar = SignalServiceAttachmentStream.newStreamBuilder().withContentType("image/jpeg").withStream(AvatarHelper.getAvatar(context, record.get().getRecipientId())).withLength(AvatarHelper.getAvatarLength(context, record.get().getRecipientId())).build();
    }
    List<SignalServiceAddress> members = new LinkedList<>();
    for (RecipientId member : record.get().getMembers()) {
        Recipient recipient = Recipient.resolved(member);
        if (recipient.isMaybeRegistered()) {
            members.add(RecipientUtil.toSignalServiceAddress(context, recipient));
        }
    }
    SignalServiceGroup groupContext = SignalServiceGroup.newBuilder(Type.UPDATE).withAvatar(avatar).withId(groupId.getDecodedId()).withMembers(members).withName(record.get().getTitle()).build();
    RecipientId groupRecipientId = SignalDatabase.recipients().getOrInsertFromGroupId(groupId);
    Recipient groupRecipient = Recipient.resolved(groupRecipientId);
    SignalServiceDataMessage message = SignalServiceDataMessage.newBuilder().asGroupMessage(groupContext).withTimestamp(System.currentTimeMillis()).withExpiration(groupRecipient.getExpiresInSeconds()).build();
    SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
    messageSender.sendDataMessage(RecipientUtil.toSignalServiceAddress(context, sourceRecipient), UnidentifiedAccessUtil.getAccessFor(context, sourceRecipient), ContentHint.DEFAULT, message, IndividualSendEvents.EMPTY);
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) Recipient(org.thoughtcrime.securesms.recipients.Recipient) GroupRecord(org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord) LinkedList(java.util.LinkedList) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) SignalServiceGroup(org.whispersystems.signalservice.api.messages.SignalServiceGroup)

Example 50 with SignalServiceAddress

use of org.whispersystems.signalservice.api.push.SignalServiceAddress in project Signal-Android by WhisperSystems.

the class PushTextSendJob method deliver.

private boolean deliver(SmsMessageRecord message) throws UntrustedIdentityException, InsecureFallbackApprovalException, UndeliverableMessageException, IOException {
    try {
        rotateSenderCertificateIfNecessary();
        Recipient messageRecipient = message.getIndividualRecipient().resolve();
        if (messageRecipient.isUnregistered()) {
            throw new UndeliverableMessageException(messageRecipient.getId() + " not registered!");
        }
        SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
        SignalServiceAddress address = RecipientUtil.toSignalServiceAddress(context, messageRecipient);
        Optional<byte[]> profileKey = getProfileKey(messageRecipient);
        Optional<UnidentifiedAccessPair> unidentifiedAccess = UnidentifiedAccessUtil.getAccessFor(context, messageRecipient);
        log(TAG, String.valueOf(message.getDateSent()), "Have access key to use: " + unidentifiedAccess.isPresent());
        SignalServiceDataMessage textSecureMessage = SignalServiceDataMessage.newBuilder().withTimestamp(message.getDateSent()).withBody(message.getBody()).withExpiration((int) (message.getExpiresIn() / 1000)).withProfileKey(profileKey.orNull()).asEndSessionMessage(message.isEndSession()).build();
        if (Util.equals(SignalStore.account().getAci(), address.getServiceId())) {
            Optional<UnidentifiedAccessPair> syncAccess = UnidentifiedAccessUtil.getAccessForSync(context);
            SignalLocalMetrics.IndividualMessageSend.onDeliveryStarted(messageId);
            SendMessageResult result = messageSender.sendSyncMessage(textSecureMessage);
            SignalDatabase.messageLog().insertIfPossible(messageRecipient.getId(), message.getDateSent(), result, ContentHint.RESENDABLE, new MessageId(messageId, false));
            return syncAccess.isPresent();
        } else {
            SignalLocalMetrics.IndividualMessageSend.onDeliveryStarted(messageId);
            SendMessageResult result = messageSender.sendDataMessage(address, unidentifiedAccess, ContentHint.RESENDABLE, textSecureMessage, new MetricEventListener(messageId));
            SignalDatabase.messageLog().insertIfPossible(messageRecipient.getId(), message.getDateSent(), result, ContentHint.RESENDABLE, new MessageId(messageId, false));
            return result.getSuccess().isUnidentified();
        }
    } catch (UnregisteredUserException e) {
        warn(TAG, "Failure", e);
        throw new InsecureFallbackApprovalException(e);
    } catch (ServerRejectedException e) {
        throw new UndeliverableMessageException(e);
    }
}
Also used : UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) Recipient(org.thoughtcrime.securesms.recipients.Recipient) UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair) ServerRejectedException(org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException) InsecureFallbackApprovalException(org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException) SendMessageResult(org.whispersystems.signalservice.api.messages.SendMessageResult) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) UndeliverableMessageException(org.thoughtcrime.securesms.transport.UndeliverableMessageException) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) MessageId(org.thoughtcrime.securesms.database.model.MessageId) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)

Aggregations

SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)62 Recipient (org.thoughtcrime.securesms.recipients.Recipient)22 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)18 SignalServiceDataMessage (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage)17 LinkedList (java.util.LinkedList)13 SendMessageResult (org.whispersystems.signalservice.api.messages.SendMessageResult)13 Optional (org.whispersystems.libsignal.util.guava.Optional)11 SignalServiceAttachment (org.whispersystems.signalservice.api.messages.SignalServiceAttachment)11 IOException (java.io.IOException)10 NotPushRegisteredException (org.thoughtcrime.securesms.net.NotPushRegisteredException)10 UnidentifiedAccessPair (org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair)10 NonNull (androidx.annotation.NonNull)9 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)8 UnregisteredUserException (org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException)8 List (java.util.List)7 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)7 Nullable (androidx.annotation.Nullable)6 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)6 UntrustedIdentityException (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException)6 SignalServiceGroup (org.whispersystems.signalservice.api.messages.SignalServiceGroup)6