Skip to content

Commit

Permalink
wip (#2820)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 authored Jul 31, 2024
1 parent 1251e62 commit 705ab4c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 53 deletions.
28 changes: 28 additions & 0 deletions FlowCrypt/src/androidTest/java/com/flowcrypt/email/GeneralTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* © 2016-present FlowCrypt a.s. Limitations apply. Contact [email protected]
* Contributors: denbond7
*/

package com.flowcrypt.email

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.flowcrypt.email.extensions.kotlin.asInternetAddress
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

/**
* @author Denys Bondarenko
*/
@SmallTest
@RunWith(AndroidJUnit4::class)
class GeneralTest {
@Test
fun testParsingInternetAddressesWithIllegalCharacters() {
val emailWithInvalidCharacters =
"Test “Test Test” test, sometest, hello, test <[email protected]>"

assertEquals("[email protected]", emailWithInvalidCharacters.asInternetAddress()?.address)
}
}
30 changes: 11 additions & 19 deletions FlowCrypt/src/main/java/com/flowcrypt/email/api/email/EmailUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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.extensions.jakarta.mail.isAttachment
import com.flowcrypt.email.extensions.kotlin.asInternetAddresses
import com.flowcrypt.email.model.MessageEncryptionType
import com.flowcrypt.email.model.MessageType
import com.flowcrypt.email.security.KeysStorageImpl
Expand All @@ -47,15 +48,6 @@ import com.google.android.gms.common.GooglePlayServicesNotAvailableException
import com.google.android.gms.common.GooglePlayServicesRepairableException
import com.google.android.gms.security.ProviderInstaller
import com.google.api.services.gmail.GmailScopes
import org.eclipse.angus.mail.gimap.GmailRawSearchTerm
import org.eclipse.angus.mail.iap.Argument
import org.eclipse.angus.mail.imap.IMAPBodyPart
import org.eclipse.angus.mail.imap.IMAPFolder
import org.eclipse.angus.mail.imap.protocol.BODY
import org.eclipse.angus.mail.imap.protocol.FetchResponse
import org.eclipse.angus.mail.imap.protocol.UID
import org.eclipse.angus.mail.imap.protocol.UIDSet
import org.eclipse.angus.mail.util.ASCIIUtility
import jakarta.activation.DataHandler
import jakarta.mail.BodyPart
import jakarta.mail.FetchProfile
Expand All @@ -66,7 +58,6 @@ import jakarta.mail.Multipart
import jakarta.mail.Part
import jakarta.mail.Session
import jakarta.mail.UIDFolder
import jakarta.mail.internet.AddressException
import jakarta.mail.internet.InternetAddress
import jakarta.mail.internet.MimeBodyPart
import jakarta.mail.internet.MimeMessage
Expand All @@ -86,6 +77,15 @@ import kotlinx.coroutines.withContext
import org.apache.commons.io.FilenameUtils
import org.apache.commons.io.IOUtils
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection
import org.eclipse.angus.mail.gimap.GmailRawSearchTerm
import org.eclipse.angus.mail.iap.Argument
import org.eclipse.angus.mail.imap.IMAPBodyPart
import org.eclipse.angus.mail.imap.IMAPFolder
import org.eclipse.angus.mail.imap.protocol.BODY
import org.eclipse.angus.mail.imap.protocol.FetchResponse
import org.eclipse.angus.mail.imap.protocol.UID
import org.eclipse.angus.mail.imap.protocol.UIDSet
import org.eclipse.angus.mail.util.ASCIIUtility
import org.pgpainless.PGPainless
import org.pgpainless.key.protection.PasswordBasedSecretKeyRingProtector
import org.pgpainless.key.protection.SecretKeyRingProtector
Expand Down Expand Up @@ -984,14 +984,6 @@ class EmailUtil {
return parameters.joinToString(separator = " ")
}

fun parseAddresses(fromAddress: String?): List<InternetAddress> {
return try {
InternetAddress.parse(fromAddress ?: "").toList()
} catch (e: AddressException) {
emptyList()
}
}

suspend fun prepareNewMsg(
session: Session,
info: OutgoingMessageInfo,
Expand Down Expand Up @@ -1265,7 +1257,7 @@ class EmailUtil {
val msg = FlowCryptMimeMessage(session)

msg.setFrom(InternetAddress(account.email))
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(account.email))
msg.setRecipients(Message.RecipientType.TO, account.email.asInternetAddresses())
msg.subject =
context.getString(R.string.your_key_backup, context.getString(R.string.app_name))
return msg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.flowcrypt.email.api.email.model.OutgoingMessageInfo
import com.flowcrypt.email.database.MessageState
import com.flowcrypt.email.extensions.com.google.api.services.gmail.model.hasPgp
import com.flowcrypt.email.extensions.jakarta.mail.hasPgp
import com.flowcrypt.email.extensions.kotlin.asInternetAddresses
import com.flowcrypt.email.extensions.kotlin.capitalize
import com.flowcrypt.email.extensions.kotlin.toHex
import com.flowcrypt.email.extensions.uid
Expand Down Expand Up @@ -88,19 +89,19 @@ data class MessageEntity(

@IgnoredOnParcel
@Ignore
val from: List<InternetAddress> = EmailUtil.parseAddresses(fromAddress)
val from: List<InternetAddress> = fromAddress.asInternetAddresses().asList()

@IgnoredOnParcel
@Ignore
val replyToAddress: List<InternetAddress> = EmailUtil.parseAddresses(replyTo)
val replyToAddress: List<InternetAddress> = replyTo.asInternetAddresses().asList()

@IgnoredOnParcel
@Ignore
val to: List<InternetAddress> = EmailUtil.parseAddresses(toAddress)
val to: List<InternetAddress> = toAddress.asInternetAddresses().asList()

@IgnoredOnParcel
@Ignore
val cc: List<InternetAddress> = EmailUtil.parseAddresses(ccAddress)
val cc: List<InternetAddress> = ccAddress.asInternetAddresses().asList()

@IgnoredOnParcel
@Ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ package com.flowcrypt.email.extensions.kotlin

import android.content.Context
import android.graphics.Color
import android.util.Patterns
import android.util.TypedValue
import androidx.core.content.ContextCompat
import com.flowcrypt.email.R
import com.flowcrypt.email.util.BetterInternetAddress
import com.flowcrypt.email.util.UIUtil
import jakarta.mail.internet.AddressException
import jakarta.mail.internet.ContentType
import jakarta.mail.internet.InternetAddress
import org.json.JSONObject
Expand Down Expand Up @@ -156,11 +158,23 @@ fun String.capitalize(): String {
}

fun String?.asInternetAddress(): InternetAddress? {
return asInternetAddresses().firstOrNull()
}

fun String?.asInternetAddresses(): Array<InternetAddress> {
return try {
InternetAddress.parse(this)
} catch (e: Exception) {
emptyArray<InternetAddress>()
}.firstOrNull()
if (e is AddressException) {
val pattern = Patterns.EMAIL_ADDRESS
val matcher = this?.let { pattern.matcher(it) }
if (matcher?.find() == true) {
arrayOf(InternetAddress(matcher.group().lowercase()))
} else emptyArray<InternetAddress>()
} else {
emptyArray<InternetAddress>()
}
}
}

fun String?.asContentTypeOrNull(): ContentType? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.flowcrypt.email.api.email.gmail.api.GmaiAPIMimeMessage
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.database.entity.RecipientEntity
import com.flowcrypt.email.extensions.kotlin.asInternetAddresses
import org.eclipse.angus.mail.imap.IMAPFolder
import jakarta.mail.FetchProfile
import jakarta.mail.Folder
Expand Down Expand Up @@ -210,7 +211,7 @@ class LoadRecipientsWorker(context: Context, params: WorkerParameters) :
val header = msg.getHeader(recipientType.toString()) ?: return@withContext emptyList()
if (header.isNotEmpty()) {
if (!TextUtils.isEmpty(header[0])) {
val addresses = InternetAddress.parse(header[0])
val addresses = header[0].asInternetAddresses()
val emailAndNamePairs = mutableListOf<Pair<String, String>>()
for (address in addresses) {
emailAndNamePairs.add(Pair(address.address.lowercase(), address.personal))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import androidx.lifecycle.switchMap
import com.flowcrypt.email.database.FlowCryptRoomDatabase
import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.extensions.kotlin.asInternetAddresses
import com.flowcrypt.email.extensions.org.bouncycastle.openpgp.toPgpKeyRingDetails
import com.flowcrypt.email.extensions.org.pgpainless.key.info.usableForEncryption
import com.flowcrypt.email.model.KeysStorage
import com.flowcrypt.email.security.model.PgpKeyRingDetails
import com.flowcrypt.email.security.pgp.PgpDecryptAndOrVerify
import com.flowcrypt.email.security.pgp.PgpKey
import com.flowcrypt.email.util.exception.DecryptionException
import jakarta.mail.internet.InternetAddress
import kotlinx.coroutines.flow.Flow
import org.bouncycastle.openpgp.PGPException
import org.bouncycastle.openpgp.PGPSecretKeyRing
Expand Down Expand Up @@ -135,7 +135,7 @@ class KeysStorageImpl private constructor(context: Context) : KeysStorage {
for (secretKey in getPGPSecretKeyRings()) {
for (userId in secretKey.publicKey.userIDs) {
try {
val internetAddresses = InternetAddress.parse(userId)
val internetAddresses = userId.asInternetAddresses()
for (internetAddress in internetAddresses) {
if (user.equals(internetAddress.address, true)) {
list.add(secretKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ package com.flowcrypt.email.security.model
import android.content.Context
import android.content.res.ColorStateList
import android.os.Parcelable
import android.util.Patterns
import androidx.core.content.ContextCompat
import com.flowcrypt.email.R
import com.flowcrypt.email.database.entity.AccountEntity
import com.flowcrypt.email.database.entity.KeyEntity
import com.flowcrypt.email.database.entity.PublicKeyEntity
import com.flowcrypt.email.database.entity.RecipientEntity
import com.flowcrypt.email.extensions.kotlin.asInternetAddress
import com.flowcrypt.email.extensions.kotlin.asInternetAddresses
import com.flowcrypt.email.model.KeyImportDetails
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName
import jakarta.mail.internet.AddressException
import jakarta.mail.internet.InternetAddress
import kotlinx.parcelize.Parcelize
import org.pgpainless.algorithm.KeyFlag
Expand Down Expand Up @@ -63,13 +63,7 @@ data class PgpKeyRingDetails(
get() = parseMimeAddresses()

val primaryMimeAddress: InternetAddress?
get() = primaryUserId?.let {
try {
InternetAddress.parse(it).firstOrNull()
} catch (e: Exception) {
null
}
}
get() = primaryUserId?.asInternetAddress()

val isPartiallyEncrypted: Boolean
get() {
Expand All @@ -91,22 +85,7 @@ data class PgpKeyRingDetails(
}

private fun parseMimeAddresses(): List<InternetAddress> {
val results = mutableListOf<InternetAddress>()

for (user in users) {
try {
results.addAll(listOf(*InternetAddress.parse(user)))
} catch (e: AddressException) {
e.printStackTrace()
val pattern = Patterns.EMAIL_ADDRESS
val matcher = pattern.matcher(user)
if (matcher.find()) {
results.add(InternetAddress(matcher.group()))
}
}
}

return results
return users.flatMap { it.asInternetAddresses().asList() }
}

fun toKeyEntity(accountEntity: AccountEntity): KeyEntity {
Expand Down

0 comments on commit 705ab4c

Please sign in to comment.