Search in sources :

Example 51 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 52 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 53 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)

Example 54 with SignalServiceAddress

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

the class MultiDeviceVerifiedUpdateJob method onRun.

@Override
public void onRun() throws IOException, UntrustedIdentityException {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    try {
        if (!TextSecurePreferences.isMultiDevice(context)) {
            Log.i(TAG, "Not multi device...");
            return;
        }
        if (destination == null) {
            Log.w(TAG, "No destination...");
            return;
        }
        SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
        Recipient recipient = Recipient.resolved(destination);
        if (recipient.isUnregistered()) {
            Log.w(TAG, recipient.getId() + " not registered!");
            return;
        }
        VerifiedMessage.VerifiedState verifiedState = getVerifiedState(verifiedStatus);
        SignalServiceAddress verifiedAddress = RecipientUtil.toSignalServiceAddress(context, recipient);
        VerifiedMessage verifiedMessage = new VerifiedMessage(verifiedAddress, new IdentityKey(identityKey, 0), verifiedState, timestamp);
        messageSender.sendSyncMessage(SignalServiceSyncMessage.forVerified(verifiedMessage), UnidentifiedAccessUtil.getAccessFor(context, recipient));
    } catch (InvalidKeyException e) {
        throw new IOException(e);
    }
}
Also used : IdentityKey(org.whispersystems.libsignal.IdentityKey) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) Recipient(org.thoughtcrime.securesms.recipients.Recipient) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) IOException(java.io.IOException) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Example 55 with SignalServiceAddress

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

the class MultiDeviceGroupUpdateJob method onRun.

@Override
public void onRun() throws Exception {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    if (!TextSecurePreferences.isMultiDevice(context)) {
        Log.i(TAG, "Not multi device, aborting...");
        return;
    }
    if (SignalStore.account().isLinkedDevice()) {
        Log.i(TAG, "Not primary device, aborting...");
        return;
    }
    ParcelFileDescriptor[] pipe = ParcelFileDescriptor.createPipe();
    InputStream inputStream = new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]);
    Uri uri = BlobProvider.getInstance().forData(inputStream, 0).withFileName("multidevice-group-update").createForSingleSessionOnDiskAsync(context, () -> Log.i(TAG, "Write successful."), e -> Log.w(TAG, "Error during write.", e));
    try (GroupDatabase.Reader reader = SignalDatabase.groups().getGroups()) {
        DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]));
        boolean hasData = false;
        GroupDatabase.GroupRecord record;
        while ((record = reader.getNext()) != null) {
            if (record.isV1Group()) {
                List<SignalServiceAddress> members = new LinkedList<>();
                List<Recipient> registeredMembers = RecipientUtil.getEligibleForSending(Recipient.resolvedList(record.getMembers()));
                for (Recipient member : registeredMembers) {
                    members.add(RecipientUtil.toSignalServiceAddress(context, member));
                }
                RecipientId recipientId = SignalDatabase.recipients().getOrInsertFromPossiblyMigratedGroupId(record.getId());
                Recipient recipient = Recipient.resolved(recipientId);
                Optional<Integer> expirationTimer = recipient.getExpiresInSeconds() > 0 ? Optional.of(recipient.getExpiresInSeconds()) : Optional.absent();
                Map<RecipientId, Integer> inboxPositions = SignalDatabase.threads().getInboxPositions();
                Set<RecipientId> archived = SignalDatabase.threads().getArchivedRecipients();
                out.write(new DeviceGroup(record.getId().getDecodedId(), Optional.fromNullable(record.getTitle()), members, getAvatar(record.getRecipientId()), record.isActive(), expirationTimer, Optional.of(ChatColorsMapper.getMaterialColor(recipient.getChatColors()).serialize()), recipient.isBlocked(), Optional.fromNullable(inboxPositions.get(recipientId)), archived.contains(recipientId)));
                hasData = true;
            }
        }
        out.close();
        if (hasData) {
            long length = BlobProvider.getInstance().calculateFileSize(context, uri);
            sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), BlobProvider.getInstance().getStream(context, uri), length);
        } else {
            Log.w(TAG, "No groups present for sync message. Sending an empty update.");
            sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), null, 0);
        }
    } finally {
        BlobProvider.getInstance().delete(context, uri);
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) InputStream(java.io.InputStream) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) DeviceGroup(org.whispersystems.signalservice.api.messages.multidevice.DeviceGroup) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Uri(android.net.Uri) LinkedList(java.util.LinkedList) ParcelFileDescriptor(android.os.ParcelFileDescriptor) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) DeviceGroupsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream)

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