Search in sources :

Example 71 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 72 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)

Example 73 with SignalServiceAddress

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

the class PushMediaSendJob method deliver.

private boolean deliver(OutgoingMediaMessage message) throws IOException, InsecureFallbackApprovalException, UntrustedIdentityException, UndeliverableMessageException {
    if (message.getRecipient() == null) {
        throw new UndeliverableMessageException("No destination address.");
    }
    try {
        rotateSenderCertificateIfNecessary();
        Recipient messageRecipient = message.getRecipient().fresh();
        if (messageRecipient.isUnregistered()) {
            throw new UndeliverableMessageException(messageRecipient.getId() + " not registered!");
        }
        SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
        SignalServiceAddress address = RecipientUtil.toSignalServiceAddress(context, messageRecipient);
        List<Attachment> attachments = Stream.of(message.getAttachments()).filterNot(Attachment::isSticker).toList();
        List<SignalServiceAttachment> serviceAttachments = getAttachmentPointersFor(attachments);
        Optional<byte[]> profileKey = getProfileKey(messageRecipient);
        Optional<SignalServiceDataMessage.Quote> quote = getQuoteFor(message);
        Optional<SignalServiceDataMessage.Sticker> sticker = getStickerFor(message);
        List<SharedContact> sharedContacts = getSharedContactsFor(message);
        List<Preview> previews = getPreviewsFor(message);
        SignalServiceDataMessage mediaMessage = SignalServiceDataMessage.newBuilder().withBody(message.getBody()).withAttachments(serviceAttachments).withTimestamp(message.getSentTimeMillis()).withExpiration((int) (message.getExpiresIn() / 1000)).withViewOnce(message.isViewOnce()).withProfileKey(profileKey.orNull()).withQuote(quote.orNull()).withSticker(sticker.orNull()).withSharedContacts(sharedContacts).withPreviews(previews).asExpirationUpdate(message.isExpirationUpdate()).build();
        if (Util.equals(SignalStore.account().getAci(), address.getServiceId())) {
            Optional<UnidentifiedAccessPair> syncAccess = UnidentifiedAccessUtil.getAccessForSync(context);
            SendMessageResult result = messageSender.sendSyncMessage(mediaMessage);
            SignalDatabase.messageLog().insertIfPossible(messageRecipient.getId(), message.getSentTimeMillis(), result, ContentHint.RESENDABLE, new MessageId(messageId, true));
            return syncAccess.isPresent();
        } else {
            SendMessageResult result = messageSender.sendDataMessage(address, UnidentifiedAccessUtil.getAccessFor(context, messageRecipient), ContentHint.RESENDABLE, mediaMessage, IndividualSendEvents.EMPTY);
            SignalDatabase.messageLog().insertIfPossible(messageRecipient.getId(), message.getSentTimeMillis(), result, ContentHint.RESENDABLE, new MessageId(messageId, true));
            return result.getSuccess().isUnidentified();
        }
    } catch (UnregisteredUserException e) {
        warn(TAG, String.valueOf(message.getSentTimeMillis()), e);
        throw new InsecureFallbackApprovalException(e);
    } catch (FileNotFoundException e) {
        warn(TAG, String.valueOf(message.getSentTimeMillis()), e);
        throw new UndeliverableMessageException(e);
    } catch (ServerRejectedException e) {
        throw new UndeliverableMessageException(e);
    }
}
Also used : UnregisteredUserException(org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException) FileNotFoundException(java.io.FileNotFoundException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) InsecureFallbackApprovalException(org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException) UndeliverableMessageException(org.thoughtcrime.securesms.transport.UndeliverableMessageException) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) Preview(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage.Preview) Recipient(org.thoughtcrime.securesms.recipients.Recipient) UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair) ServerRejectedException(org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException) SendMessageResult(org.whispersystems.signalservice.api.messages.SendMessageResult) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) SharedContact(org.whispersystems.signalservice.api.messages.shared.SharedContact) MessageId(org.thoughtcrime.securesms.database.model.MessageId) SyncMessageId(org.thoughtcrime.securesms.database.MessageDatabase.SyncMessageId)

Example 74 with SignalServiceAddress

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

the class PushSendJob method getQuoteFor.

protected Optional<SignalServiceDataMessage.Quote> getQuoteFor(OutgoingMediaMessage message) throws IOException {
    if (message.getOutgoingQuote() == null)
        return Optional.absent();
    long quoteId = message.getOutgoingQuote().getId();
    String quoteBody = message.getOutgoingQuote().getText();
    RecipientId quoteAuthor = message.getOutgoingQuote().getAuthor();
    List<SignalServiceDataMessage.Mention> quoteMentions = getMentionsFor(message.getOutgoingQuote().getMentions());
    List<SignalServiceDataMessage.Quote.QuotedAttachment> quoteAttachments = new LinkedList<>();
    List<Attachment> filteredAttachments = Stream.of(message.getOutgoingQuote().getAttachments()).filterNot(a -> MediaUtil.isViewOnceType(a.getContentType())).toList();
    for (Attachment attachment : filteredAttachments) {
        BitmapUtil.ScaleResult thumbnailData = null;
        SignalServiceAttachment thumbnail = null;
        String thumbnailType = MediaUtil.IMAGE_JPEG;
        try {
            if (MediaUtil.isImageType(attachment.getContentType()) && attachment.getUri() != null) {
                Bitmap.CompressFormat format = BitmapUtil.getCompressFormatForContentType(attachment.getContentType());
                thumbnailData = BitmapUtil.createScaledBytes(context, new DecryptableStreamUriLoader.DecryptableUri(attachment.getUri()), 100, 100, 500 * 1024, format);
                thumbnailType = attachment.getContentType();
            } else if (Build.VERSION.SDK_INT >= 23 && MediaUtil.isVideoType(attachment.getContentType()) && attachment.getUri() != null) {
                Bitmap bitmap = MediaUtil.getVideoThumbnail(context, attachment.getUri(), 1000);
                if (bitmap != null) {
                    thumbnailData = BitmapUtil.createScaledBytes(context, bitmap, 100, 100, 500 * 1024);
                }
            }
            if (thumbnailData != null) {
                SignalServiceAttachment.Builder builder = SignalServiceAttachment.newStreamBuilder().withContentType(thumbnailType).withWidth(thumbnailData.getWidth()).withHeight(thumbnailData.getHeight()).withLength(thumbnailData.getBitmap().length).withStream(new ByteArrayInputStream(thumbnailData.getBitmap())).withResumableUploadSpec(ApplicationDependencies.getSignalServiceMessageSender().getResumableUploadSpec());
                thumbnail = builder.build();
            }
            quoteAttachments.add(new SignalServiceDataMessage.Quote.QuotedAttachment(attachment.isVideoGif() ? MediaUtil.IMAGE_GIF : attachment.getContentType(), attachment.getFileName(), thumbnail));
        } catch (BitmapDecodingException e) {
            Log.w(TAG, e);
        }
    }
    Recipient quoteAuthorRecipient = Recipient.resolved(quoteAuthor);
    if (quoteAuthorRecipient.isMaybeRegistered()) {
        SignalServiceAddress quoteAddress = RecipientUtil.toSignalServiceAddress(context, quoteAuthorRecipient);
        return Optional.of(new SignalServiceDataMessage.Quote(quoteId, quoteAddress, quoteBody, quoteAttachments, quoteMentions));
    } else {
        return Optional.absent();
    }
}
Also used : SignalStore(org.thoughtcrime.securesms.keyvalue.SignalStore) ServerRejectedException(org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException) PartProgressEvent(org.thoughtcrime.securesms.events.PartProgressEvent) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) LinkPreview(org.thoughtcrime.securesms.linkpreview.LinkPreview) NonNull(androidx.annotation.NonNull) JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) RecipientUtil(org.thoughtcrime.securesms.recipients.RecipientUtil) Mention(org.thoughtcrime.securesms.database.model.Mention) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) ByteArrayInputStream(java.io.ByteArrayInputStream) SenderCertificate(org.signal.libsignal.metadata.certificate.SenderCertificate) Locale(java.util.Locale) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) Recipient(org.thoughtcrime.securesms.recipients.Recipient) PartAuthority(org.thoughtcrime.securesms.mms.PartAuthority) TextSecureExpiredException(org.thoughtcrime.securesms.TextSecureExpiredException) DecryptableStreamUriLoader(org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader) Base64(org.thoughtcrime.securesms.util.Base64) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) Collection(java.util.Collection) ProfileKeyUtil(org.thoughtcrime.securesms.crypto.ProfileKeyUtil) Set(java.util.Set) ThreadMode(org.greenrobot.eventbus.ThreadMode) BitmapDecodingException(org.thoughtcrime.securesms.util.BitmapDecodingException) Log(org.signal.core.util.logging.Log) CountDownLatch(java.util.concurrent.CountDownLatch) FeatureFlags(org.thoughtcrime.securesms.util.FeatureFlags) List(java.util.List) Nullable(androidx.annotation.Nullable) Job(org.thoughtcrime.securesms.jobmanager.Job) SharedContact(org.whispersystems.signalservice.api.messages.shared.SharedContact) BitmapUtil(org.thoughtcrime.securesms.util.BitmapUtil) Attachment(org.thoughtcrime.securesms.attachments.Attachment) OutgoingMediaMessage(org.thoughtcrime.securesms.mms.OutgoingMediaMessage) CertificateType(org.thoughtcrime.securesms.keyvalue.CertificateType) MediaUtil(org.thoughtcrime.securesms.util.MediaUtil) Preview(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage.Preview) Context(android.content.Context) SignalDatabase(org.thoughtcrime.securesms.database.SignalDatabase) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException) Stream(com.annimon.stream.Stream) Util(org.thoughtcrime.securesms.util.Util) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) InvalidCertificateException(org.signal.libsignal.metadata.certificate.InvalidCertificateException) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) HashSet(java.util.HashSet) ContactModelMapper(org.thoughtcrime.securesms.contactshare.ContactModelMapper) EventBus(org.greenrobot.eventbus.EventBus) SignalServiceAttachmentRemoteId(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId) Build(android.os.Build) LinkedList(java.util.LinkedList) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) Contact(org.thoughtcrime.securesms.contactshare.Contact) StickerRecord(org.thoughtcrime.securesms.database.model.StickerRecord) BackoffUtil(org.thoughtcrime.securesms.jobmanager.impl.BackoffUtil) TextUtils(android.text.TextUtils) NetworkConstraint(org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint) Hex(org.thoughtcrime.securesms.util.Hex) IOException(java.io.IOException) Optional(org.whispersystems.libsignal.util.guava.Optional) BlurHash(org.thoughtcrime.securesms.blurhash.BlurHash) TimeUnit(java.util.concurrent.TimeUnit) ProofRequiredException(org.whispersystems.signalservice.api.push.exceptions.ProofRequiredException) Subscribe(org.greenrobot.eventbus.Subscribe) Bitmap(android.graphics.Bitmap) InputStream(java.io.InputStream) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) Bitmap(android.graphics.Bitmap) Mention(org.thoughtcrime.securesms.database.model.Mention) BitmapUtil(org.thoughtcrime.securesms.util.BitmapUtil) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) BitmapDecodingException(org.thoughtcrime.securesms.util.BitmapDecodingException) Recipient(org.thoughtcrime.securesms.recipients.Recipient) LinkedList(java.util.LinkedList) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) SignalServiceAttachment(org.whispersystems.signalservice.api.messages.SignalServiceAttachment) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 75 with SignalServiceAddress

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

the class MultiDeviceContactUpdateJob method getVerifiedMessage.

private Optional<VerifiedMessage> getVerifiedMessage(Recipient recipient, Optional<IdentityRecord> identity) throws InvalidNumberException, IOException {
    if (!identity.isPresent())
        return Optional.absent();
    SignalServiceAddress destination = RecipientUtil.toSignalServiceAddress(context, recipient);
    IdentityKey identityKey = identity.get().getIdentityKey();
    VerifiedMessage.VerifiedState state;
    switch(identity.get().getVerifiedStatus()) {
        case VERIFIED:
            state = VerifiedMessage.VerifiedState.VERIFIED;
            break;
        case UNVERIFIED:
            state = VerifiedMessage.VerifiedState.UNVERIFIED;
            break;
        case DEFAULT:
            state = VerifiedMessage.VerifiedState.DEFAULT;
            break;
        default:
            throw new AssertionError("Unknown state: " + identity.get().getVerifiedStatus());
    }
    return Optional.of(new VerifiedMessage(destination, identityKey, state, System.currentTimeMillis()));
}
Also used : IdentityKey(org.whispersystems.libsignal.IdentityKey) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)

Aggregations

SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)113 Recipient (org.thoughtcrime.securesms.recipients.Recipient)44 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)32 SendMessageResult (org.whispersystems.signalservice.api.messages.SendMessageResult)28 LinkedList (java.util.LinkedList)27 Optional (org.whispersystems.libsignal.util.guava.Optional)23 SignalServiceDataMessage (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage)21 NotPushRegisteredException (org.thoughtcrime.securesms.net.NotPushRegisteredException)20 UnidentifiedAccessPair (org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair)20 NonNull (androidx.annotation.NonNull)18 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)18 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)17 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)16 SignalServiceAttachment (org.whispersystems.signalservice.api.messages.SignalServiceAttachment)15 List (java.util.List)14 ByteString (com.google.protobuf.ByteString)13 Nullable (androidx.annotation.Nullable)12 UntrustedIdentityException (org.whispersystems.signalservice.api.crypto.UntrustedIdentityException)12 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)10