private Pair<Long, Long> insertSyncMediaMessage( MasterSecret masterSecret, TextSecureEnvelope envelope, TextSecureMessage message) throws MmsException { MmsDatabase database = DatabaseFactory.getMmsDatabase(context); TextSecureSyncContext syncContext = message.getSyncContext().get(); Recipients recipients = getSyncMessageDestination(message); OutgoingMediaMessage mediaMessage = new OutgoingMediaMessage( context, masterSecret, recipients, message.getAttachments().get(), message.getBody().orNull()); mediaMessage = new OutgoingSecureMediaMessage(mediaMessage); long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients); long messageId = database.insertMessageOutbox( masterSecret, mediaMessage, threadId, false, syncContext.getTimestamp()); database.markAsSent(messageId, "push".getBytes(), 0); database.markAsPush(messageId); return new Pair<>(messageId, threadId); }
private Pair<Long, Long> insertSyncTextMessage( MasterSecret masterSecret, TextSecureEnvelope envelope, TextSecureMessage message, Optional<Long> smsMessageId) { EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context); Recipients recipients = getSyncMessageDestination(message); String body = message.getBody().or(""); OutgoingTextMessage outgoingTextMessage = new OutgoingTextMessage(recipients, body); long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients); long messageId = database.insertMessageOutbox( masterSecret, threadId, outgoingTextMessage, false, message.getSyncContext().get().getTimestamp()); database.markAsSent(messageId); database.markAsPush(messageId); database.markAsSecure(messageId); if (smsMessageId.isPresent()) { database.deleteMessage(smsMessageId.get()); } return new Pair<>(messageId, threadId); }
private Recipients getSyncMessageDestination(TextSecureMessage message) { if (message.getGroupInfo().isPresent()) { return RecipientFactory.getRecipientsFromString( context, GroupUtil.getEncodedId(message.getGroupInfo().get().getGroupId()), false); } else { return RecipientFactory.getRecipientsFromString( context, message.getSyncContext().get().getDestination(), false); } }
private void deliver(MasterSecret masterSecret, SendReq message) throws RetryLaterException, InsecureFallbackApprovalException, UntrustedIdentityException, UndeliverableMessageException { TextSecureMessageSender messageSender = messageSenderFactory.create(masterSecret); String destination = message.getTo()[0].getString(); try { message = getResolvedMessage(masterSecret, message, MediaConstraints.PUSH_CONSTRAINTS, false); TextSecureAddress address = getPushAddress(destination); List<TextSecureAttachment> attachments = getAttachments(masterSecret, message); String body = PartParser.getMessageText(message.getBody()); TextSecureMessage mediaMessage = TextSecureMessage.newBuilder() .withBody(body) .withAttachments(attachments) .withTimestamp(message.getSentTimestamp()) .build(); messageSender.sendMessage(address, mediaMessage); } catch (InvalidNumberException | UnregisteredUserException e) { Log.w(TAG, e); throw new InsecureFallbackApprovalException(e); } catch (IOException e) { Log.w(TAG, e); throw new RetryLaterException(e); } }
private void handleEndSessionMessage( MasterSecret masterSecret, TextSecureEnvelope envelope, TextSecureMessage message, Optional<Long> smsMessageId) { EncryptingSmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context); IncomingTextMessage incomingTextMessage = new IncomingTextMessage( envelope.getSource(), envelope.getSourceDevice(), message.getTimestamp(), "", Optional.<TextSecureGroup>absent()); long threadId; if (!smsMessageId.isPresent()) { IncomingEndSessionMessage incomingEndSessionMessage = new IncomingEndSessionMessage(incomingTextMessage); Pair<Long, Long> messageAndThreadId = smsDatabase.insertMessageInbox(masterSecret, incomingEndSessionMessage); threadId = messageAndThreadId.second; } else { smsDatabase.markAsEndSession(smsMessageId.get()); threadId = smsDatabase.getThreadIdForMessage(smsMessageId.get()); } SessionStore sessionStore = new TextSecureSessionStore(context, masterSecret); sessionStore.deleteAllSessions(envelope.getSource()); SecurityEvent.broadcastSecurityUpdateEvent(context, threadId); MessageNotifier.updateNotification(context, masterSecret, threadId); }
private Pair<Long, Long> insertStandardMediaMessage( MasterSecret masterSecret, TextSecureEnvelope envelope, TextSecureMessage message) throws MmsException { MmsDatabase database = DatabaseFactory.getMmsDatabase(context); String localNumber = TextSecurePreferences.getLocalNumber(context); IncomingMediaMessage mediaMessage = new IncomingMediaMessage( masterSecret, envelope.getSource(), localNumber, message.getTimestamp(), Optional.fromNullable(envelope.getRelay()), message.getBody(), message.getGroupInfo(), message.getAttachments()); return database.insertSecureDecryptedMessageInbox(masterSecret, mediaMessage, -1); }
private Pair<Long, Long> insertStandardTextMessage( MasterSecret masterSecret, TextSecureEnvelope envelope, TextSecureMessage message, Optional<Long> smsMessageId) { EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context); String body = message.getBody().isPresent() ? message.getBody().get() : ""; if (smsMessageId.isPresent()) { return database.updateBundleMessageBody(masterSecret, smsMessageId.get(), body); } else { IncomingTextMessage textMessage = new IncomingTextMessage( envelope.getSource(), envelope.getSourceDevice(), message.getTimestamp(), body, message.getGroupInfo()); textMessage = new IncomingEncryptedMessage(textMessage, body); return database.insertMessageInbox(masterSecret, textMessage); } }
private void handleTextMessage( MasterSecret masterSecret, TextSecureEnvelope envelope, TextSecureMessage message, Optional<Long> smsMessageId) { Pair<Long, Long> messageAndThreadId; if (message.getSyncContext().isPresent()) { messageAndThreadId = insertSyncTextMessage(masterSecret, envelope, message, smsMessageId); } else { messageAndThreadId = insertStandardTextMessage(masterSecret, envelope, message, smsMessageId); } MessageNotifier.updateNotification(context, masterSecret, messageAndThreadId.second); }
private void handleMediaMessage( MasterSecret masterSecret, TextSecureEnvelope envelope, TextSecureMessage message, Optional<Long> smsMessageId) throws MmsException { Pair<Long, Long> messageAndThreadId; if (message.getSyncContext().isPresent()) { messageAndThreadId = insertSyncMediaMessage(masterSecret, envelope, message); } else { messageAndThreadId = insertStandardMediaMessage(masterSecret, envelope, message); } ApplicationContext.getInstance(context) .getJobManager() .add(new AttachmentDownloadJob(context, messageAndThreadId.first)); if (smsMessageId.isPresent()) { DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get()); } MessageNotifier.updateNotification(context, masterSecret, messageAndThreadId.second); }