Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ class EmailUtil {
outgoingMsgInfo: OutgoingMessageInfo
): Message {
val session = Session.getInstance(Properties())
val senderEmail = outgoingMsgInfo.from
val senderEmail = outgoingMsgInfo.from.address
var pubKeys: List<String>? = null
var prvKeys: List<String>? = null
var ringProtector: SecretKeyRingProtector? = null
Expand Down Expand Up @@ -978,7 +978,7 @@ class EmailUtil {
): MimeMessage {
val msg = FlowCryptMimeMessage(session)
msg.subject = info.subject
msg.setFrom(InternetAddress(info.from))
msg.setFrom(info.from)
msg.setRecipients(Message.RecipientType.TO, info.toRecipients.toTypedArray())
msg.setRecipients(Message.RecipientType.CC, info.ccRecipients?.toTypedArray())
msg.setRecipients(Message.RecipientType.BCC, info.bccRecipients?.toTypedArray())
Expand All @@ -996,7 +996,7 @@ class EmailUtil {
protector: SecretKeyRingProtector? = null
): Message {
val reply = replyToMsg.reply(false)//we use replyToAll == false to use the own logic
reply.setFrom(InternetAddress(info.from))
reply.setFrom(info.from)
reply.setContent(MimeMultipart().apply {
addBodyPart(prepareBodyPart(info, pubKeys, prvKeys, protector))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ data class OutgoingMessageInfo constructor(
val toRecipients: List<InternetAddress>,
val ccRecipients: List<InternetAddress>? = null,
val bccRecipients: List<InternetAddress>? = null,
val from: String,
val from: InternetAddress,
val atts: List<AttachmentInfo>? = null,
val forwardedAtts: List<AttachmentInfo>? = null,
val encryptionType: MessageEncryptionType,
Expand Down Expand Up @@ -60,7 +60,7 @@ data class OutgoingMessageInfo constructor(
parcel.readValue(InternetAddress::class.java.classLoader) as List<InternetAddress>,
parcel.readValue(InternetAddress::class.java.classLoader) as List<InternetAddress>?,
parcel.readValue(InternetAddress::class.java.classLoader) as List<InternetAddress>?,
parcel.readString()!!,
parcel.readSerializable() as InternetAddress,
parcel.readValue(AttachmentInfo::class.java.classLoader) as List<AttachmentInfo>?,
parcel.readValue(AttachmentInfo::class.java.classLoader) as List<AttachmentInfo>?,
parcel.readParcelable<MessageEncryptionType>(MessageEncryptionType::class.java.classLoader)!!,
Expand All @@ -82,7 +82,7 @@ data class OutgoingMessageInfo constructor(
writeValue(toRecipients)
writeValue(ccRecipients)
writeValue(bccRecipients)
writeString(from)
writeSerializable(from)
writeValue(atts)
writeValue(forwardedAtts)
writeParcelable(encryptionType, flags)
Expand Down Expand Up @@ -138,15 +138,8 @@ data class OutgoingMessageInfo constructor(
return result
}

companion object {
@JvmField
@Suppress("unused")
val CREATOR: Parcelable.Creator<OutgoingMessageInfo> =
object : Parcelable.Creator<OutgoingMessageInfo> {
override fun createFromParcel(source: Parcel): OutgoingMessageInfo =
OutgoingMessageInfo(source)

override fun newArray(size: Int): Array<OutgoingMessageInfo?> = arrayOfNulls(size)
}
companion object CREATOR : Parcelable.Creator<OutgoingMessageInfo> {
override fun createFromParcel(parcel: Parcel) = OutgoingMessageInfo(parcel)
override fun newArray(size: Int): Array<OutgoingMessageInfo?> = arrayOfNulls(size)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ interface RecipientDao : BaseDao<RecipientEntity> {
@Query("SELECT * FROM recipients WHERE email = :email")
fun getRecipientByEmail(email: String): RecipientEntity?

@Query("SELECT * FROM recipients WHERE email IN (:emails)")
fun getRecipientsByEmails(emails: Collection<String>): List<RecipientEntity>

@Transaction
@Query("SELECT * FROM recipients WHERE email = :email")
fun getRecipientsWithPubKeysByEmailsLD(email: String): LiveData<RecipientWithPubKeys?>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
import javax.mail.internet.InternetAddress

/**
* @author Denis Bondarenko
Expand Down Expand Up @@ -52,6 +53,10 @@ data class RecipientEntity(
return 0
}

fun toInternetAddress(): InternetAddress {
return InternetAddress(email, name)
}

companion object CREATOR : Parcelable.Creator<RecipientEntity> {
override fun createFromParcel(parcel: Parcel): RecipientEntity = RecipientEntity(parcel)
override fun newArray(size: Int): Array<RecipientEntity?> = arrayOfNulls(size)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* © 2016-present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com
* Contributors: denbond7
*/

package com.flowcrypt.email.extensions

import android.content.Context
import com.flowcrypt.email.api.email.model.OutgoingMessageInfo
import com.flowcrypt.email.database.FlowCryptRoomDatabase

fun OutgoingMessageInfo.replaceWithCachedRecipients(context: Context): OutgoingMessageInfo {
val recipientDao = FlowCryptRoomDatabase.getDatabase(context).recipientDao()
val updatedFrom = recipientDao.getRecipientByEmail(from.address.lowercase())
?.toInternetAddress() ?: from
return copy(
from = updatedFrom,
toRecipients = recipientDao.getRecipientsByEmails(toRecipients
.map { it.address.lowercase() })
.map { it.toInternetAddress() },
ccRecipients = ccRecipients?.let { list ->
recipientDao.getRecipientsByEmails(list.map { it.address.lowercase() })
.map { it.toInternetAddress() }
},
bccRecipients = bccRecipients?.let { list ->
recipientDao.getRecipientsByEmails(list.map { it.address.lowercase() })
.map { it.toInternetAddress() }
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ fun MimeMessage.getAddresses(type: Message.RecipientType): List<String> {
?.mapNotNull { (it as? InternetAddress)?.address?.lowercase() } ?: emptyList()
}

fun MimeMessage.getFromAddress(): String {
return (from.first() as? InternetAddress)?.address?.lowercase()
fun MimeMessage.getFromAddress(): InternetAddress {
return (from.first() as? InternetAddress)
?: throw IllegalStateException("'from' address is undefined")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ class HandlePasswordProtectedMsgWorker(context: Context, params: WorkerParameter

//start of creating and uploading a password-protected msg to FES
val fromAddress = plainMimeMsgWithAttachments.getFromAddress()
val domain = EmailUtil.getDomain(fromAddress)
val domain = EmailUtil.getDomain(fromAddress.address)
val idToken = getGoogleIdToken()
val replyToken = fetchReplyToken(apiRepository, domain, idToken)
val replyInfoData = ReplyInfoData(
sender = fromAddress,
sender = fromAddress.address,
recipient = (toCandidates + ccCandidates + bccCandidates)
.mapNotNull { (it as? InternetAddress)?.address }
.filterNot {
it.equals(fromAddress, true)
it.equals(fromAddress.address, true)
},
subject = plainMimeMsgWithAttachments.subject,
token = replyToken
Expand Down Expand Up @@ -211,16 +211,16 @@ class HandlePasswordProtectedMsgWorker(context: Context, params: WorkerParameter

private fun genMessageUploadRequest(
replyToken: String,
fromAddress: String,
fromAddress: Address,
toCandidates: Array<Address>,
ccCandidates: Array<Address>,
bccCandidates: Array<Address>
) = MessageUploadRequest(
associateReplyToken = replyToken,
from = fromAddress,
to = toCandidates.map { (it as InternetAddress).address },
cc = ccCandidates.map { (it as InternetAddress).address },
bcc = bccCandidates.map { (it as InternetAddress).address }
from = (fromAddress as InternetAddress).toString(),
to = toCandidates.map { (it as InternetAddress).toString() },
cc = ccCandidates.map { (it as InternetAddress).toString() },
bcc = bccCandidates.map { (it as InternetAddress).toString() }
)

private suspend fun handleExceptionsForMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.database.entity.AttachmentEntity
import com.flowcrypt.email.database.entity.MessageEntity
import com.flowcrypt.email.database.entity.RecipientEntity
import com.flowcrypt.email.extensions.replaceWithCachedRecipients
import com.flowcrypt.email.jetpack.workmanager.ForwardedAttachmentsDownloaderWorker
import com.flowcrypt.email.jetpack.workmanager.HandlePasswordProtectedMsgWorker
import com.flowcrypt.email.jetpack.workmanager.MessagesSenderWorker
Expand All @@ -44,7 +45,6 @@ import java.io.ByteArrayOutputStream
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.util.ArrayList
import java.util.UUID
import javax.mail.Message

Expand Down Expand Up @@ -76,12 +76,12 @@ class PrepareOutgoingMessagesJobIntentService : JobIntentService() {
LogsUtil.d(TAG, "onHandleWork")

val roomDatabase = FlowCryptRoomDatabase.getDatabase(applicationContext)
val outgoingMsgInfo =
val originalOutgoingMsgInfo =
intent.getParcelableExtra<OutgoingMessageInfo>(EXTRA_KEY_OUTGOING_MESSAGE_INFO)
?: return
val accountEntity =
roomDatabase.accountDao().getAccount(outgoingMsgInfo.account.lowercase())
?: return
val outgoingMsgInfo = originalOutgoingMsgInfo.replaceWithCachedRecipients(applicationContext)
val accountEntity = roomDatabase.accountDao().getAccount(outgoingMsgInfo.account.lowercase())
?: return

val uid = outgoingMsgInfo.uid
val email = accountEntity.email
Expand Down Expand Up @@ -271,7 +271,7 @@ class PrepareOutgoingMessagesJobIntentService : JobIntentService() {
var pubKeys: List<String>? = null

if (outgoingMsgInfo.encryptionType === MessageEncryptionType.ENCRYPTED) {
val senderEmail = outgoingMsgInfo.from
val senderEmail = outgoingMsgInfo.from.address
val recipients = outgoingMsgInfo.getAllRecipients().toMutableList()
pubKeys = mutableListOf()
pubKeys.addAll(SecurityUtils.getRecipientsUsablePubKeys(applicationContext, recipients))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1858,7 +1858,7 @@ class CreateMessageFragment : BaseSyncFragment(), View.OnFocusChangeListener,
?: emptyList(),
ccRecipients = binding?.editTextRecipientCc?.chipValues?.map { InternetAddress(it) },
bccRecipients = binding?.editTextRecipientBcc?.chipValues?.map { InternetAddress(it) },
from = binding?.editTextFrom?.text.toString(),
from = InternetAddress(binding?.editTextFrom?.text.toString()),
atts = attachments,
forwardedAtts = getForwardedAttachments(),
encryptionType = composeMsgViewModel.msgEncryptionType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ParcelableTest(val name: String, private val currentClass: Class<Parcelabl
InternetAddress("bcc@test.com"),
InternetAddress("bcc1@test.com")
),
from = "from@test.com",
from = InternetAddress("from@test.com"),
atts = null,
forwardedAtts = listOf(),
encryptionType = MessageEncryptionType.STANDARD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class EmailUtilTest {
toRecipients = toRecipients,
ccRecipients = ccRecipients,
bccRecipients = bccRecipients,
from = accountEntity.email,
from = InternetAddress(accountEntity.email),
encryptionType = MessageEncryptionType.STANDARD,
messageType = MessageType.NEW,
uid = 1000
Expand Down Expand Up @@ -126,7 +126,7 @@ class EmailUtilTest {
subject = "subject that will be overridden",
msg = replyText,
toRecipients = replyToMIME.from.toList().map { it as InternetAddress },
from = accountEntity.email,
from = InternetAddress(accountEntity.email),
encryptionType = MessageEncryptionType.STANDARD,
messageType = MessageType.REPLY,
uid = 1000
Expand Down