2020-12-07 05:22:02 +01:00
|
|
|
package org.session.libsession.messaging.sending_receiving
|
|
|
|
|
2021-01-11 23:58:38 +01:00
|
|
|
import android.text.TextUtils
|
2020-12-10 05:33:57 +01:00
|
|
|
import org.session.libsession.messaging.MessagingConfiguration
|
2020-12-18 06:48:45 +01:00
|
|
|
import org.session.libsession.messaging.jobs.AttachmentDownloadJob
|
2021-01-08 01:13:05 +01:00
|
|
|
import org.session.libsession.messaging.jobs.JobQueue
|
2020-12-07 05:22:02 +01:00
|
|
|
import org.session.libsession.messaging.messages.Message
|
2021-02-09 01:45:38 +01:00
|
|
|
import org.session.libsession.messaging.messages.control.*
|
2020-12-18 06:48:45 +01:00
|
|
|
import org.session.libsession.messaging.messages.visible.Attachment
|
2020-12-07 05:22:02 +01:00
|
|
|
import org.session.libsession.messaging.messages.visible.VisibleMessage
|
2021-01-11 23:58:38 +01:00
|
|
|
import org.session.libsession.messaging.sending_receiving.attachments.PointerAttachment
|
|
|
|
import org.session.libsession.messaging.sending_receiving.linkpreview.LinkPreview
|
2020-12-07 05:22:02 +01:00
|
|
|
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
2021-01-11 23:58:38 +01:00
|
|
|
import org.session.libsession.messaging.sending_receiving.quotes.QuoteModel
|
2020-12-10 05:33:57 +01:00
|
|
|
import org.session.libsession.messaging.threads.Address
|
2021-02-09 01:45:38 +01:00
|
|
|
import org.session.libsession.messaging.threads.GroupRecord
|
2021-01-08 01:13:05 +01:00
|
|
|
import org.session.libsession.messaging.threads.recipients.Recipient
|
2020-12-10 05:33:57 +01:00
|
|
|
import org.session.libsession.utilities.GroupUtil
|
2021-01-08 01:13:05 +01:00
|
|
|
import org.session.libsession.utilities.SSKEnvironment
|
|
|
|
import org.session.libsession.utilities.TextSecurePreferences
|
2021-02-09 01:45:38 +01:00
|
|
|
import org.session.libsignal.libsignal.ecc.DjbECPrivateKey
|
|
|
|
import org.session.libsignal.libsignal.ecc.DjbECPublicKey
|
|
|
|
import org.session.libsignal.libsignal.ecc.ECKeyPair
|
2021-01-11 23:58:38 +01:00
|
|
|
import org.session.libsignal.libsignal.util.guava.Optional
|
2021-01-13 07:11:30 +01:00
|
|
|
import org.session.libsignal.service.api.messages.SignalServiceGroup
|
2020-12-07 05:22:02 +01:00
|
|
|
import org.session.libsignal.service.internal.push.SignalServiceProtos
|
2021-02-09 01:45:38 +01:00
|
|
|
import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded
|
2020-12-07 05:22:02 +01:00
|
|
|
import org.session.libsignal.service.loki.utilities.toHexString
|
2021-03-09 07:26:29 +01:00
|
|
|
import org.session.libsignal.utilities.logging.Log
|
2021-01-08 01:13:05 +01:00
|
|
|
import java.security.MessageDigest
|
2020-12-07 05:22:02 +01:00
|
|
|
import java.util.*
|
|
|
|
|
|
|
|
internal fun MessageReceiver.isBlock(publicKey: String): Boolean {
|
2021-01-13 07:11:30 +01:00
|
|
|
val context = MessagingConfiguration.shared.context
|
|
|
|
val recipient = Recipient.from(context, Address.fromSerialized(publicKey), false)
|
|
|
|
return recipient.isBlocked
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun MessageReceiver.handle(message: Message, proto: SignalServiceProtos.Content, openGroupID: String?) {
|
|
|
|
when (message) {
|
|
|
|
is ReadReceipt -> handleReadReceipt(message)
|
|
|
|
is TypingIndicator -> handleTypingIndicator(message)
|
2021-02-09 01:45:38 +01:00
|
|
|
is ClosedGroupControlMessage -> handleClosedGroupControlMessage(message)
|
2021-01-13 07:11:30 +01:00
|
|
|
is ExpirationTimerUpdate -> handleExpirationTimerUpdate(message, proto)
|
2021-02-09 01:45:38 +01:00
|
|
|
is ConfigurationMessage -> handleConfigurationMessage(message)
|
2020-12-07 05:22:02 +01:00
|
|
|
is VisibleMessage -> handleVisibleMessage(message, proto, openGroupID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun MessageReceiver.handleReadReceipt(message: ReadReceipt) {
|
2021-01-13 07:11:30 +01:00
|
|
|
val context = MessagingConfiguration.shared.context
|
2021-03-05 00:17:34 +01:00
|
|
|
SSKEnvironment.shared.readReceiptManager.processReadReceipts(context, message.sender!!, message.timestamps!!, message.receivedTimestamp!!)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun MessageReceiver.handleTypingIndicator(message: TypingIndicator) {
|
|
|
|
when (message.kind!!) {
|
|
|
|
TypingIndicator.Kind.STARTED -> showTypingIndicatorIfNeeded(message.sender!!)
|
|
|
|
TypingIndicator.Kind.STOPPED -> hideTypingIndicatorIfNeeded(message.sender!!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun MessageReceiver.showTypingIndicatorIfNeeded(senderPublicKey: String) {
|
2021-01-08 01:13:05 +01:00
|
|
|
val context = MessagingConfiguration.shared.context
|
|
|
|
val address = Address.fromSerialized(senderPublicKey)
|
|
|
|
val threadID = MessagingConfiguration.shared.storage.getThreadIdFor(address) ?: return
|
2021-01-20 06:29:52 +01:00
|
|
|
SSKEnvironment.shared.typingIndicators.didReceiveTypingStartedMessage(context, threadID, address, 1)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun MessageReceiver.hideTypingIndicatorIfNeeded(senderPublicKey: String) {
|
2021-01-08 01:13:05 +01:00
|
|
|
val context = MessagingConfiguration.shared.context
|
|
|
|
val address = Address.fromSerialized(senderPublicKey)
|
|
|
|
val threadID = MessagingConfiguration.shared.storage.getThreadIdFor(address) ?: return
|
2021-01-20 06:29:52 +01:00
|
|
|
SSKEnvironment.shared.typingIndicators.didReceiveTypingStoppedMessage(context, threadID, address, 1, false)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fun MessageReceiver.cancelTypingIndicatorsIfNeeded(senderPublicKey: String) {
|
2021-01-08 01:13:05 +01:00
|
|
|
val context = MessagingConfiguration.shared.context
|
|
|
|
val address = Address.fromSerialized(senderPublicKey)
|
|
|
|
val threadID = MessagingConfiguration.shared.storage.getThreadIdFor(address) ?: return
|
2021-01-20 06:29:52 +01:00
|
|
|
SSKEnvironment.shared.typingIndicators.didReceiveIncomingMessage(context, threadID, address, 1)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 07:11:30 +01:00
|
|
|
private fun MessageReceiver.handleExpirationTimerUpdate(message: ExpirationTimerUpdate, proto: SignalServiceProtos.Content) {
|
2020-12-07 05:22:02 +01:00
|
|
|
if (message.duration!! > 0) {
|
2021-01-13 07:11:30 +01:00
|
|
|
setExpirationTimer(message, proto)
|
2020-12-07 05:22:02 +01:00
|
|
|
} else {
|
2021-01-13 07:11:30 +01:00
|
|
|
disableExpirationTimer(message, proto)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 07:11:30 +01:00
|
|
|
fun MessageReceiver.setExpirationTimer(message: ExpirationTimerUpdate, proto: SignalServiceProtos.Content) {
|
2021-01-20 06:29:52 +01:00
|
|
|
val id = message.id
|
2021-01-13 07:11:30 +01:00
|
|
|
val duration = message.duration!!
|
|
|
|
val senderPublicKey = message.sender!!
|
|
|
|
SSKEnvironment.shared.messageExpirationManager.setExpirationTimer(id, duration, senderPublicKey, proto)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
2021-01-13 07:11:30 +01:00
|
|
|
fun MessageReceiver.disableExpirationTimer(message: ExpirationTimerUpdate, proto: SignalServiceProtos.Content) {
|
2021-01-20 06:29:52 +01:00
|
|
|
val id = message.id
|
2021-01-13 07:11:30 +01:00
|
|
|
val senderPublicKey = message.sender!!
|
|
|
|
SSKEnvironment.shared.messageExpirationManager.disableExpirationTimer(id, senderPublicKey, proto)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 01:45:38 +01:00
|
|
|
private fun MessageReceiver.handleConfigurationMessage(message: ConfigurationMessage) {
|
2021-02-10 06:48:03 +01:00
|
|
|
val context = MessagingConfiguration.shared.context
|
2021-02-09 01:45:38 +01:00
|
|
|
val storage = MessagingConfiguration.shared.storage
|
2021-02-10 06:48:03 +01:00
|
|
|
if (TextSecurePreferences.getConfigurationMessageSynced(context)) return
|
2021-02-09 01:45:38 +01:00
|
|
|
if (message.sender != storage.getUserPublicKey()) return
|
|
|
|
val allClosedGroupPublicKeys = storage.getAllClosedGroupPublicKeys()
|
|
|
|
for (closeGroup in message.closedGroups) {
|
|
|
|
if (allClosedGroupPublicKeys.contains(closeGroup.publicKey)) continue
|
2021-03-15 05:43:05 +01:00
|
|
|
handleNewClosedGroup(message.sender!!, message.sentTimestamp!!, closeGroup.publicKey, closeGroup.name, closeGroup.encryptionKeyPair!!, closeGroup.members, closeGroup.admins, message.sentTimestamp!!)
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
|
|
|
val allOpenGroups = storage.getAllOpenGroups().map { it.value.server }
|
|
|
|
for (openGroup in message.openGroups) {
|
|
|
|
if (allOpenGroups.contains(openGroup)) continue
|
2021-02-09 03:16:33 +01:00
|
|
|
storage.addOpenGroup(openGroup, 1)
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
2021-02-23 07:13:57 +01:00
|
|
|
// TODO: in future handle the latest in config messages
|
2021-02-10 06:48:03 +01:00
|
|
|
TextSecurePreferences.setConfigurationMessageSynced(context, true)
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
|
|
|
|
2020-12-07 05:22:02 +01:00
|
|
|
fun MessageReceiver.handleVisibleMessage(message: VisibleMessage, proto: SignalServiceProtos.Content, openGroupID: String?) {
|
2020-12-18 06:48:45 +01:00
|
|
|
val storage = MessagingConfiguration.shared.storage
|
2021-01-08 01:13:05 +01:00
|
|
|
val context = MessagingConfiguration.shared.context
|
2020-12-18 06:48:45 +01:00
|
|
|
// Update profile if needed
|
|
|
|
val newProfile = message.profile
|
|
|
|
if (newProfile != null) {
|
2021-01-08 01:13:05 +01:00
|
|
|
val profileManager = SSKEnvironment.shared.profileManager
|
|
|
|
val recipient = Recipient.from(context, Address.fromSerialized(message.sender!!), false)
|
|
|
|
val displayName = newProfile.displayName!!
|
|
|
|
val userPublicKey = storage.getUserPublicKey()
|
|
|
|
if (userPublicKey == message.sender) {
|
|
|
|
// Update the user's local name if the message came from their master device
|
|
|
|
TextSecurePreferences.setProfileName(context, displayName)
|
|
|
|
}
|
2021-01-13 07:11:30 +01:00
|
|
|
profileManager.setDisplayName(context, recipient, displayName)
|
2021-01-08 01:13:05 +01:00
|
|
|
if (recipient.profileKey == null || !MessageDigest.isEqual(recipient.profileKey, newProfile.profileKey)) {
|
2021-01-13 07:11:30 +01:00
|
|
|
profileManager.setProfileKey(context, recipient, newProfile.profileKey!!)
|
|
|
|
profileManager.setUnidentifiedAccessMode(context, recipient, Recipient.UnidentifiedAccessMode.UNKNOWN)
|
2021-01-08 01:13:05 +01:00
|
|
|
val url = newProfile.profilePictureURL.orEmpty()
|
2021-01-13 07:11:30 +01:00
|
|
|
profileManager.setProfilePictureURL(context, recipient, url)
|
2021-01-08 01:13:05 +01:00
|
|
|
if (userPublicKey == message.sender) {
|
2021-01-13 07:11:30 +01:00
|
|
|
profileManager.updateOpenGroupProfilePicturesIfNeeded(context)
|
2021-01-08 01:13:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-18 06:48:45 +01:00
|
|
|
}
|
|
|
|
// Get or create thread
|
2021-02-10 06:48:03 +01:00
|
|
|
val threadID = storage.getOrCreateThreadIdFor(message.syncTarget ?: message.sender!!, message.groupPublicKey, openGroupID)
|
2020-12-18 06:48:45 +01:00
|
|
|
// Parse quote if needed
|
2021-01-11 23:58:38 +01:00
|
|
|
var quoteModel: QuoteModel? = null
|
2020-12-18 06:48:45 +01:00
|
|
|
if (message.quote != null && proto.dataMessage.hasQuote()) {
|
2021-01-11 23:58:38 +01:00
|
|
|
val quote = proto.dataMessage.quote
|
|
|
|
val author = Address.fromSerialized(quote.author)
|
|
|
|
val messageID = MessagingConfiguration.shared.messageDataProvider.getMessageForQuote(quote.id, author)
|
|
|
|
if (messageID != null) {
|
2021-03-24 07:17:01 +01:00
|
|
|
val attachments = MessagingConfiguration.shared.messageDataProvider.getAttachmentsAndLinkPreviewFor(messageID)
|
|
|
|
quoteModel = QuoteModel(quote.id, author, MessagingConfiguration.shared.messageDataProvider.getMessageBodyFor(quote.id, quote.author), false, attachments)
|
2021-01-11 23:58:38 +01:00
|
|
|
} else {
|
|
|
|
quoteModel = QuoteModel(quote.id, author, quote.text, true, PointerAttachment.forPointers(proto.dataMessage.quote.attachmentsList))
|
|
|
|
}
|
2020-12-18 06:48:45 +01:00
|
|
|
}
|
|
|
|
// Parse link preview if needed
|
2021-01-11 23:58:38 +01:00
|
|
|
val linkPreviews: MutableList<LinkPreview?> = mutableListOf()
|
2020-12-18 06:48:45 +01:00
|
|
|
if (message.linkPreview != null && proto.dataMessage.previewCount > 0) {
|
2021-01-11 23:58:38 +01:00
|
|
|
for (preview in proto.dataMessage.previewList) {
|
|
|
|
val thumbnail = PointerAttachment.forPointer(preview.image)
|
|
|
|
val url = Optional.fromNullable(preview.url)
|
|
|
|
val title = Optional.fromNullable(preview.title)
|
|
|
|
val hasContent = !TextUtils.isEmpty(title.or("")) || thumbnail.isPresent
|
|
|
|
if (hasContent) {
|
|
|
|
val linkPreview = LinkPreview(url.get(), title.or(""), thumbnail)
|
|
|
|
linkPreviews.add(linkPreview)
|
|
|
|
} else {
|
|
|
|
Log.w("Loki", "Discarding an invalid link preview. hasContent: $hasContent")
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 06:48:45 +01:00
|
|
|
}
|
2021-03-15 03:35:05 +01:00
|
|
|
val attachments = proto.dataMessage.attachmentsList.mapNotNull { proto ->
|
|
|
|
val attachment = Attachment.fromProto(proto)
|
|
|
|
if (!attachment.isValid()) {
|
|
|
|
return@mapNotNull null
|
|
|
|
} else {
|
|
|
|
return@mapNotNull attachment
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 23:58:38 +01:00
|
|
|
// Parse stickers if needed
|
2020-12-18 06:48:45 +01:00
|
|
|
// Persist the message
|
|
|
|
message.threadID = threadID
|
2021-03-19 07:08:31 +01:00
|
|
|
val messageID = storage.persist(message, quoteModel, linkPreviews, message.groupPublicKey, openGroupID, attachments) ?: throw MessageReceiver.Error.NoThread
|
2021-03-15 03:35:05 +01:00
|
|
|
// Parse & persist attachments
|
2020-12-18 06:48:45 +01:00
|
|
|
// Start attachment downloads if needed
|
2021-03-15 03:35:05 +01:00
|
|
|
storage.getAttachmentsForMessage(messageID).forEach { attachment ->
|
|
|
|
attachment.attachmentId?.let { id ->
|
|
|
|
val downloadJob = AttachmentDownloadJob(id.rowId, messageID)
|
|
|
|
JobQueue.shared.add(downloadJob)
|
|
|
|
}
|
2020-12-18 06:48:45 +01:00
|
|
|
}
|
2021-01-11 23:58:38 +01:00
|
|
|
// Cancel any typing indicators if needed
|
|
|
|
cancelTypingIndicatorsIfNeeded(message.sender!!)
|
|
|
|
//Notify the user if needed
|
|
|
|
SSKEnvironment.shared.notificationManager.updateNotification(context, threadID)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 01:45:38 +01:00
|
|
|
private fun MessageReceiver.handleClosedGroupControlMessage(message: ClosedGroupControlMessage) {
|
2020-12-07 05:22:02 +01:00
|
|
|
when (message.kind!!) {
|
2021-02-09 01:45:38 +01:00
|
|
|
is ClosedGroupControlMessage.Kind.New -> handleNewClosedGroup(message)
|
|
|
|
is ClosedGroupControlMessage.Kind.Update -> handleClosedGroupUpdated(message)
|
|
|
|
is ClosedGroupControlMessage.Kind.EncryptionKeyPair -> handleClosedGroupEncryptionKeyPair(message)
|
|
|
|
is ClosedGroupControlMessage.Kind.NameChange -> handleClosedGroupNameChanged(message)
|
|
|
|
is ClosedGroupControlMessage.Kind.MembersAdded -> handleClosedGroupMembersAdded(message)
|
|
|
|
is ClosedGroupControlMessage.Kind.MembersRemoved -> handleClosedGroupMembersRemoved(message)
|
2021-03-04 05:16:47 +01:00
|
|
|
is ClosedGroupControlMessage.Kind.MemberLeft -> handleClosedGroupMemberLeft(message)
|
|
|
|
is ClosedGroupControlMessage.Kind.EncryptionKeyPairRequest -> handleClosedGroupEncryptionKeyPairRequest(message)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 01:45:38 +01:00
|
|
|
private fun MessageReceiver.handleNewClosedGroup(message: ClosedGroupControlMessage) {
|
|
|
|
val kind = message.kind!! as? ClosedGroupControlMessage.Kind.New ?: return
|
|
|
|
val groupPublicKey = kind.publicKey.toByteArray().toHexString()
|
|
|
|
val members = kind.members.map { it.toByteArray().toHexString() }
|
|
|
|
val admins = kind.admins.map { it.toByteArray().toHexString() }
|
2021-03-15 05:43:05 +01:00
|
|
|
handleNewClosedGroup(message.sender!!, message.sentTimestamp!!, groupPublicKey, kind.name, kind.encryptionKeyPair!!, members, admins, message.sentTimestamp!!)
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parameter @sender:String is just for inserting incoming info message
|
2021-03-11 05:31:14 +01:00
|
|
|
private fun handleNewClosedGroup(sender: String, sentTimestamp: Long, groupPublicKey: String, name: String, encryptionKeyPair: ECKeyPair, members: List<String>, admins: List<String>, formationTimestamp: Long) {
|
2021-01-13 07:11:30 +01:00
|
|
|
val context = MessagingConfiguration.shared.context
|
2020-12-10 05:33:57 +01:00
|
|
|
val storage = MessagingConfiguration.shared.storage
|
2020-12-07 05:22:02 +01:00
|
|
|
// Create the group
|
2021-02-09 01:45:38 +01:00
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
2020-12-10 05:33:57 +01:00
|
|
|
if (storage.getGroup(groupID) != null) {
|
2020-12-07 05:22:02 +01:00
|
|
|
// Update the group
|
2020-12-10 05:33:57 +01:00
|
|
|
storage.updateTitle(groupID, name)
|
|
|
|
storage.updateMembers(groupID, members.map { Address.fromSerialized(it) })
|
2020-12-07 05:22:02 +01:00
|
|
|
} else {
|
2020-12-10 05:33:57 +01:00
|
|
|
storage.createGroup(groupID, name, LinkedList(members.map { Address.fromSerialized(it) }),
|
2021-02-16 01:14:27 +01:00
|
|
|
null, null, LinkedList(admins.map { Address.fromSerialized(it) }), formationTimestamp)
|
2021-02-10 06:48:03 +01:00
|
|
|
// Notify the user
|
2021-03-11 05:31:14 +01:00
|
|
|
storage.insertIncomingInfoMessage(context, sender, groupID, SignalServiceProtos.GroupContext.Type.UPDATE, SignalServiceGroup.Type.UPDATE, name, members, admins, sentTimestamp)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
2020-12-10 05:33:57 +01:00
|
|
|
storage.setProfileSharing(Address.fromSerialized(groupID), true)
|
2020-12-07 05:22:02 +01:00
|
|
|
// Add the group to the user's set of public keys to poll for
|
2021-02-09 01:45:38 +01:00
|
|
|
storage.addClosedGroupPublicKey(groupPublicKey)
|
|
|
|
// Store the encryption key pair
|
|
|
|
storage.addClosedGroupEncryptionKeyPair(encryptionKeyPair, groupPublicKey)
|
|
|
|
// Notify the PN server
|
|
|
|
PushNotificationAPI.performOperation(PushNotificationAPI.ClosedGroupOperation.Subscribe, groupPublicKey, storage.getUserPublicKey()!!)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 01:45:38 +01:00
|
|
|
private fun MessageReceiver.handleClosedGroupUpdated(message: ClosedGroupControlMessage) {
|
|
|
|
// Prepare
|
2021-01-13 07:11:30 +01:00
|
|
|
val context = MessagingConfiguration.shared.context
|
2020-12-18 06:48:45 +01:00
|
|
|
val storage = MessagingConfiguration.shared.storage
|
2021-02-09 01:45:38 +01:00
|
|
|
val senderPublicKey = message.sender ?: return
|
|
|
|
val kind = message.kind!! as? ClosedGroupControlMessage.Kind.Update ?: return
|
|
|
|
val groupPublicKey = message.groupPublicKey ?: return
|
2020-12-18 06:48:45 +01:00
|
|
|
val userPublicKey = storage.getUserPublicKey()!!
|
2021-02-09 01:45:38 +01:00
|
|
|
// Unwrap the message
|
|
|
|
val name = kind.name
|
|
|
|
val members = kind.members.map { it.toByteArray().toHexString() }
|
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
|
|
|
val group = storage.getGroup(groupID) ?: run {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for nonexistent group.")
|
|
|
|
return
|
|
|
|
}
|
2021-03-23 00:00:51 +01:00
|
|
|
if (!group.isActive) {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for inactive group")
|
|
|
|
return
|
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
val oldMembers = group.members.map { it.serialize() }
|
|
|
|
// Check common group update logic
|
|
|
|
if (!isValidGroupUpdate(group, message.sentTimestamp!!, senderPublicKey)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Check that the admin wasn't removed unless the group was destroyed entirely
|
|
|
|
if (!members.contains(group.admins.first().toString()) && members.isNotEmpty()) {
|
|
|
|
android.util.Log.d("Loki", "Ignoring invalid closed group update message.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Remove the group from the user's set of public keys to poll for if the current user was removed
|
|
|
|
val wasCurrentUserRemoved = !members.contains(userPublicKey)
|
|
|
|
if (wasCurrentUserRemoved) {
|
|
|
|
disableLocalGroupAndUnsubscribe(groupPublicKey, groupID, userPublicKey)
|
|
|
|
}
|
|
|
|
// Generate and distribute a new encryption key pair if needed
|
|
|
|
val wasAnyUserRemoved = (members.toSet().intersect(oldMembers) != oldMembers.toSet())
|
|
|
|
val isCurrentUserAdmin = group.admins.map { it.toString() }.contains(userPublicKey)
|
|
|
|
if (wasAnyUserRemoved && isCurrentUserAdmin) {
|
|
|
|
MessageSender.generateAndSendNewEncryptionKeyPair(groupPublicKey, members)
|
2020-12-18 06:48:45 +01:00
|
|
|
}
|
|
|
|
// Update the group
|
|
|
|
storage.updateTitle(groupID, name)
|
2021-02-09 01:45:38 +01:00
|
|
|
if (!wasCurrentUserRemoved) {
|
|
|
|
// The call below sets isActive to true, so if the user is leaving we have to use groupDB.remove(...) instead
|
|
|
|
storage.updateMembers(groupID, members.map { Address.fromSerialized(it) })
|
|
|
|
}
|
|
|
|
// Notify the user
|
|
|
|
val wasSenderRemoved = !members.contains(senderPublicKey)
|
2021-01-13 07:11:30 +01:00
|
|
|
val type0 = if (wasSenderRemoved) SignalServiceProtos.GroupContext.Type.QUIT else SignalServiceProtos.GroupContext.Type.UPDATE
|
|
|
|
val type1 = if (wasSenderRemoved) SignalServiceGroup.Type.QUIT else SignalServiceGroup.Type.UPDATE
|
2021-03-11 05:31:14 +01:00
|
|
|
storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, type0, type1, name, members, group.admins.map { it.toString() }, message.sentTimestamp!!)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 01:45:38 +01:00
|
|
|
private fun MessageReceiver.handleClosedGroupEncryptionKeyPair(message: ClosedGroupControlMessage) {
|
|
|
|
// Prepare
|
2021-01-08 01:13:05 +01:00
|
|
|
val storage = MessagingConfiguration.shared.storage
|
2021-02-09 01:45:38 +01:00
|
|
|
val senderPublicKey = message.sender ?: return
|
|
|
|
val kind = message.kind!! as? ClosedGroupControlMessage.Kind.EncryptionKeyPair ?: return
|
2021-02-10 06:48:03 +01:00
|
|
|
val groupPublicKey = kind.publicKey?.toByteArray()?.toHexString() ?: message.groupPublicKey ?: return
|
2021-01-08 01:13:05 +01:00
|
|
|
val userPublicKey = storage.getUserPublicKey()!!
|
2021-02-09 01:45:38 +01:00
|
|
|
val userKeyPair = storage.getUserX25519KeyPair()
|
|
|
|
// Unwrap the message
|
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
|
|
|
val group = storage.getGroup(groupID) ?: run {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for nonexistent group.")
|
|
|
|
return
|
|
|
|
}
|
2021-03-23 00:00:51 +01:00
|
|
|
if (!group.isActive) {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for inactive group")
|
|
|
|
return
|
|
|
|
}
|
2021-02-10 06:48:03 +01:00
|
|
|
if (!group.members.map { it.toString() }.contains(senderPublicKey)) {
|
2021-03-12 00:21:09 +01:00
|
|
|
Log.d("Loki", "Ignoring closed group encryption key pair from non-member.")
|
2021-02-09 01:45:38 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Find our wrapper and decrypt it if possible
|
|
|
|
val wrapper = kind.wrappers.firstOrNull { it.publicKey!!.toByteArray().toHexString() == userPublicKey } ?: return
|
|
|
|
val encryptedKeyPair = wrapper.encryptedKeyPair!!.toByteArray()
|
|
|
|
val plaintext = MessageReceiverDecryption.decryptWithSessionProtocol(encryptedKeyPair, userKeyPair).first
|
|
|
|
// Parse it
|
|
|
|
val proto = SignalServiceProtos.KeyPair.parseFrom(plaintext)
|
|
|
|
val keyPair = ECKeyPair(DjbECPublicKey(proto.publicKey.toByteArray().removing05PrefixIfNeeded()), DjbECPrivateKey(proto.privateKey.toByteArray()))
|
2021-02-10 06:48:03 +01:00
|
|
|
// Store it if needed
|
|
|
|
val closedGroupEncryptionKeyPairs = storage.getClosedGroupEncryptionKeyPairs(groupPublicKey)
|
|
|
|
if (closedGroupEncryptionKeyPairs.contains(keyPair)) {
|
|
|
|
Log.d("Loki", "Ignoring duplicate closed group encryption key pair.")
|
|
|
|
return
|
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
storage.addClosedGroupEncryptionKeyPair(keyPair, groupPublicKey)
|
|
|
|
Log.d("Loki", "Received a new closed group encryption key pair")
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun MessageReceiver.handleClosedGroupNameChanged(message: ClosedGroupControlMessage) {
|
|
|
|
val context = MessagingConfiguration.shared.context
|
|
|
|
val storage = MessagingConfiguration.shared.storage
|
|
|
|
val senderPublicKey = message.sender ?: return
|
|
|
|
val kind = message.kind!! as? ClosedGroupControlMessage.Kind.NameChange ?: return
|
|
|
|
val groupPublicKey = message.groupPublicKey ?: return
|
|
|
|
// Check that the sender is a member of the group (before the update)
|
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
|
|
|
val group = storage.getGroup(groupID) ?: run {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for nonexistent group.")
|
|
|
|
return
|
|
|
|
}
|
2021-03-23 00:00:51 +01:00
|
|
|
if (!group.isActive) {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for inactive group")
|
|
|
|
return
|
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
// Check common group update logic
|
|
|
|
if (!isValidGroupUpdate(group, message.sentTimestamp!!, senderPublicKey)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
val members = group.members.map { it.serialize() }
|
|
|
|
val admins = group.admins.map { it.serialize() }
|
|
|
|
val name = kind.name
|
|
|
|
storage.updateTitle(groupID, name)
|
|
|
|
|
2021-03-11 05:31:14 +01:00
|
|
|
storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, SignalServiceProtos.GroupContext.Type.UPDATE, SignalServiceGroup.Type.UPDATE, name, members, admins, message.sentTimestamp!!)
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun MessageReceiver.handleClosedGroupMembersAdded(message: ClosedGroupControlMessage) {
|
|
|
|
val context = MessagingConfiguration.shared.context
|
|
|
|
val storage = MessagingConfiguration.shared.storage
|
2021-03-12 00:21:09 +01:00
|
|
|
val userPublicKey = storage.getUserPublicKey()!!
|
2021-02-09 01:45:38 +01:00
|
|
|
val senderPublicKey = message.sender ?: return
|
|
|
|
val kind = message.kind!! as? ClosedGroupControlMessage.Kind.MembersAdded ?: return
|
|
|
|
val groupPublicKey = message.groupPublicKey ?: return
|
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
|
|
|
val group = storage.getGroup(groupID) ?: run {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for nonexistent group.")
|
2021-01-08 01:13:05 +01:00
|
|
|
return
|
|
|
|
}
|
2021-03-23 00:00:51 +01:00
|
|
|
if (!group.isActive) {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for inactive group")
|
|
|
|
return
|
|
|
|
}
|
2021-03-12 00:21:09 +01:00
|
|
|
if (!isValidGroupUpdate(group, message.sentTimestamp!!, senderPublicKey)) { return }
|
2021-02-09 01:45:38 +01:00
|
|
|
val name = group.title
|
|
|
|
// Check common group update logic
|
|
|
|
val members = group.members.map { it.serialize() }
|
|
|
|
val admins = group.admins.map { it.serialize() }
|
|
|
|
|
|
|
|
val updateMembers = kind.members.map { it.toByteArray().toHexString() }
|
|
|
|
val newMembers = members + updateMembers
|
|
|
|
storage.updateMembers(groupID, newMembers.map { Address.fromSerialized(it) })
|
2021-03-12 00:21:09 +01:00
|
|
|
if (userPublicKey == senderPublicKey) {
|
|
|
|
val threadID = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID))
|
|
|
|
storage.insertOutgoingInfoMessage(context, groupID, SignalServiceProtos.GroupContext.Type.UPDATE, name, members, admins, threadID, message.sentTimestamp!!)
|
|
|
|
} else {
|
|
|
|
storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, SignalServiceProtos.GroupContext.Type.UPDATE, SignalServiceGroup.Type.UPDATE, name, members, admins, message.sentTimestamp!!)
|
|
|
|
}
|
|
|
|
if (userPublicKey in admins) {
|
|
|
|
// send current encryption key to the latest added members
|
|
|
|
val encryptionKeyPair = pendingKeyPair[groupPublicKey]?.orNull()
|
|
|
|
?: storage.getLatestClosedGroupEncryptionKeyPair(groupPublicKey)
|
|
|
|
if (encryptionKeyPair == null) {
|
|
|
|
android.util.Log.d("Loki", "Couldn't get encryption key pair for closed group.")
|
|
|
|
} else {
|
|
|
|
for (user in updateMembers) {
|
|
|
|
MessageSender.sendEncryptionKeyPair(groupPublicKey, encryptionKeyPair, setOf(user), targetUser = user, force = false)
|
|
|
|
}
|
2021-02-11 06:57:43 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 05:31:14 +01:00
|
|
|
storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, SignalServiceProtos.GroupContext.Type.UPDATE, SignalServiceGroup.Type.UPDATE, name, members, admins, message.sentTimestamp!!)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 01:45:38 +01:00
|
|
|
private fun MessageReceiver.handleClosedGroupMembersRemoved(message: ClosedGroupControlMessage) {
|
|
|
|
val context = MessagingConfiguration.shared.context
|
|
|
|
val storage = MessagingConfiguration.shared.storage
|
|
|
|
val userPublicKey = storage.getUserPublicKey()!!
|
|
|
|
val senderPublicKey = message.sender ?: return
|
|
|
|
val kind = message.kind!! as? ClosedGroupControlMessage.Kind.MembersRemoved ?: return
|
|
|
|
val groupPublicKey = message.groupPublicKey ?: return
|
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
|
|
|
val group = storage.getGroup(groupID) ?: run {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for nonexistent group.")
|
2021-01-08 01:13:05 +01:00
|
|
|
return
|
|
|
|
}
|
2021-03-23 00:00:51 +01:00
|
|
|
if (!group.isActive) {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for inactive group")
|
|
|
|
return
|
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
val name = group.title
|
|
|
|
// Check common group update logic
|
|
|
|
val members = group.members.map { it.serialize() }
|
|
|
|
val admins = group.admins.map { it.toString() }
|
|
|
|
|
|
|
|
// Users that are part of this remove update
|
|
|
|
val updateMembers = kind.members.map { it.toByteArray().toHexString() }
|
|
|
|
|
|
|
|
if (!isValidGroupUpdate(group, message.sentTimestamp!!, senderPublicKey)) { return }
|
|
|
|
// If admin leaves the group is disbanded
|
|
|
|
val didAdminLeave = admins.any { it in updateMembers }
|
|
|
|
// newMembers to save is old members minus removed members
|
|
|
|
val newMembers = members - updateMembers
|
|
|
|
// user should be posting MEMBERS_LEFT so this should not be encountered
|
|
|
|
val senderLeft = senderPublicKey in updateMembers
|
|
|
|
if (senderLeft) {
|
|
|
|
android.util.Log.d("Loki", "Received a MEMBERS_REMOVED instead of a MEMBERS_LEFT from sender $senderPublicKey")
|
|
|
|
}
|
|
|
|
val wasCurrentUserRemoved = userPublicKey in updateMembers
|
|
|
|
|
|
|
|
// admin should send a MEMBERS_LEFT message but handled here in case
|
|
|
|
if (didAdminLeave || wasCurrentUserRemoved) {
|
|
|
|
disableLocalGroupAndUnsubscribe(groupPublicKey, groupID, userPublicKey)
|
|
|
|
} else {
|
|
|
|
val isCurrentUserAdmin = admins.contains(userPublicKey)
|
|
|
|
storage.updateMembers(groupID, newMembers.map { Address.fromSerialized(it) })
|
|
|
|
if (isCurrentUserAdmin) {
|
|
|
|
MessageSender.generateAndSendNewEncryptionKeyPair(groupPublicKey, newMembers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
val (contextType, signalType) =
|
|
|
|
if (senderLeft) SignalServiceProtos.GroupContext.Type.QUIT to SignalServiceGroup.Type.QUIT
|
|
|
|
else SignalServiceProtos.GroupContext.Type.UPDATE to SignalServiceGroup.Type.UPDATE
|
|
|
|
|
2021-03-11 05:31:14 +01:00
|
|
|
storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, contextType, signalType, name, members, admins, message.sentTimestamp!!)
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun MessageReceiver.handleClosedGroupMemberLeft(message: ClosedGroupControlMessage) {
|
|
|
|
val context = MessagingConfiguration.shared.context
|
|
|
|
val storage = MessagingConfiguration.shared.storage
|
|
|
|
val senderPublicKey = message.sender ?: return
|
|
|
|
val userPublicKey = storage.getUserPublicKey()!!
|
2021-02-10 06:48:03 +01:00
|
|
|
if (message.kind!! !is ClosedGroupControlMessage.Kind.MemberLeft) return
|
2021-02-09 01:45:38 +01:00
|
|
|
val groupPublicKey = message.groupPublicKey ?: return
|
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
|
|
|
val group = storage.getGroup(groupID) ?: run {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for nonexistent group.")
|
|
|
|
return
|
|
|
|
}
|
2021-03-23 00:00:51 +01:00
|
|
|
if (!group.isActive) {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for inactive group")
|
|
|
|
return
|
|
|
|
}
|
2021-02-09 01:45:38 +01:00
|
|
|
val name = group.title
|
|
|
|
// Check common group update logic
|
|
|
|
val members = group.members.map { it.serialize() }
|
|
|
|
val admins = group.admins.map { it.toString() }
|
|
|
|
if (!isValidGroupUpdate(group, message.sentTimestamp!!, senderPublicKey)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// If admin leaves the group is disbanded
|
|
|
|
val didAdminLeave = admins.contains(senderPublicKey)
|
|
|
|
val updatedMemberList = members - senderPublicKey
|
|
|
|
|
|
|
|
if (didAdminLeave) {
|
|
|
|
disableLocalGroupAndUnsubscribe(groupPublicKey, groupID, userPublicKey)
|
|
|
|
} else {
|
|
|
|
val isCurrentUserAdmin = admins.contains(userPublicKey)
|
|
|
|
storage.updateMembers(groupID, updatedMemberList.map { Address.fromSerialized(it) })
|
|
|
|
if (isCurrentUserAdmin) {
|
|
|
|
MessageSender.generateAndSendNewEncryptionKeyPair(groupPublicKey, updatedMemberList)
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 05:31:14 +01:00
|
|
|
storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, SignalServiceProtos.GroupContext.Type.QUIT, SignalServiceGroup.Type.QUIT, name, members, admins, message.sentTimestamp!!)
|
2021-02-09 01:45:38 +01:00
|
|
|
}
|
|
|
|
|
2021-02-10 06:48:03 +01:00
|
|
|
private fun MessageReceiver.handleClosedGroupEncryptionKeyPairRequest(message: ClosedGroupControlMessage) {
|
|
|
|
val storage = MessagingConfiguration.shared.storage
|
|
|
|
val senderPublicKey = message.sender ?: return
|
|
|
|
val userPublicKey = storage.getUserPublicKey()!!
|
|
|
|
if (message.kind!! !is ClosedGroupControlMessage.Kind.EncryptionKeyPairRequest) return
|
|
|
|
if (senderPublicKey == userPublicKey) {
|
|
|
|
Log.d("Loki", "Ignoring invalid closed group update.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
val groupPublicKey = message.groupPublicKey ?: return
|
|
|
|
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
|
|
|
|
val group = storage.getGroup(groupID) ?: run {
|
|
|
|
Log.d("Loki", "Ignoring closed group info message for nonexistent group.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!isValidGroupUpdate(group, message.sentTimestamp!!, senderPublicKey)) { return }
|
2021-03-12 00:21:09 +01:00
|
|
|
val encryptionKeyPair = pendingKeyPair[groupPublicKey]?.orNull()
|
|
|
|
?: storage.getLatestClosedGroupEncryptionKeyPair(groupPublicKey)
|
|
|
|
if (encryptionKeyPair == null) {
|
|
|
|
Log.d("Loki", "Couldn't get encryption key pair for closed group.")
|
|
|
|
} else {
|
|
|
|
MessageSender.sendEncryptionKeyPair(groupPublicKey, encryptionKeyPair, setOf(senderPublicKey), targetUser = senderPublicKey, force = false)
|
|
|
|
}
|
2021-02-10 06:48:03 +01:00
|
|
|
}
|
|
|
|
|
2021-02-09 01:45:38 +01:00
|
|
|
private fun isValidGroupUpdate(group: GroupRecord,
|
|
|
|
sentTimestamp: Long,
|
|
|
|
senderPublicKey: String): Boolean {
|
|
|
|
val oldMembers = group.members.map { it.serialize() }
|
|
|
|
// Check that the message isn't from before the group was created
|
2021-02-16 01:14:27 +01:00
|
|
|
if (group.formationTimestamp > sentTimestamp) {
|
2021-02-09 01:45:38 +01:00
|
|
|
android.util.Log.d("Loki", "Ignoring closed group update from before thread was created.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Check that the sender is a member of the group (before the update)
|
|
|
|
if (senderPublicKey !in oldMembers) {
|
|
|
|
android.util.Log.d("Loki", "Ignoring closed group info message from non-member.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-09 04:45:22 +01:00
|
|
|
fun MessageReceiver.disableLocalGroupAndUnsubscribe(groupPublicKey: String, groupID: String, userPublicKey: String) {
|
2021-02-09 01:45:38 +01:00
|
|
|
val storage = MessagingConfiguration.shared.storage
|
|
|
|
storage.removeClosedGroupPublicKey(groupPublicKey)
|
|
|
|
// Remove the key pairs
|
|
|
|
storage.removeAllClosedGroupEncryptionKeyPairs(groupPublicKey)
|
|
|
|
// Mark the group as inactive
|
|
|
|
storage.setActive(groupID, false)
|
|
|
|
storage.removeMember(groupID, Address.fromSerialized(userPublicKey))
|
|
|
|
// Notify the PN server
|
|
|
|
PushNotificationAPI.performOperation(PushNotificationAPI.ClosedGroupOperation.Unsubscribe, groupPublicKey, userPublicKey)
|
2020-12-07 05:22:02 +01:00
|
|
|
}
|