Warn about suspicious mail senders and links

Bump version to 0.1.21

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-10-07 01:53:01 +03:00
co-authored by Copilot
parent cae6825f2c
commit 47ec7bdb80
15 changed files with 284 additions and 13 deletions
@@ -528,7 +528,8 @@ internal object AndroidMailService {
val subject = decode(message.subject).ifBlank { "(no subject)" }
val body = ArrayList<String>()
val attachments = JSONArray()
collectParts(message, body, attachments)
val links = linkedSetOf<String>()
collectParts(message, body, attachments, links)
val bodyText = body.joinToString("\n\n").trim().take(8000)
val snippet = (bodyText.ifBlank { subject }).replace(Regex("\\s+"), " ").take(180)
val date = message.receivedDate ?: message.sentDate ?: Date()
@@ -567,6 +568,7 @@ internal object AndroidMailService {
val paragraphs = bodyText.split(Regex("\\n\\s*\\n")).map(String::trim).filter(String::isNotEmpty).take(40)
if (paragraphs.isEmpty()) put("This message has no plain-text body.") else paragraphs.forEach(::put)
})
.put("links", JSONArray().apply { links.take(30).forEach(::put) })
.put("attachments", attachments)
.put("attachment", files.firstOrNull()?.optString("filename") ?: JSONObject.NULL)
.put("size", files.firstOrNull()?.optString("size") ?: "")
@@ -575,7 +577,7 @@ internal object AndroidMailService {
private fun attachmentList(attachments: JSONArray): List<JSONObject> =
(0 until attachments.length()).map { attachments.getJSONObject(it) }
private fun collectParts(part: javax.mail.Part, body: MutableList<String>, attachments: JSONArray) {
private fun collectParts(part: javax.mail.Part, body: MutableList<String>, attachments: JSONArray, links: MutableSet<String>) {
val filename = decode(part.fileName)
if (filename.isNotBlank() || part.disposition.equals(javax.mail.Part.ATTACHMENT, true)) {
val name = filename.ifBlank { "attachment" }
@@ -587,13 +589,18 @@ internal object AndroidMailService {
when (content) {
is String -> if (part.isMimeType("text/plain")) {
body.add(content.replace("\r", "").trim())
} else if (part.isMimeType("text/html") && body.isEmpty()) {
body.add(Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY).toString().trim())
} else if (part.isMimeType("text/html")) {
Regex("""<a\b[^>]*\bhref\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))""", RegexOption.IGNORE_CASE)
.findAll(content)
.mapNotNull { match -> (match.groups[1]?.value ?: match.groups[2]?.value ?: match.groups[3]?.value)?.replace("&amp;", "&") }
.take(30)
.forEach(links::add)
if (body.isEmpty()) body.add(Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY).toString().trim())
}
is Multipart -> for (index in 0 until content.count) {
collectParts(content.getBodyPart(index), body, attachments)
collectParts(content.getBodyPart(index), body, attachments, links)
}
is javax.mail.BodyPart -> collectParts(content, body, attachments)
is javax.mail.BodyPart -> collectParts(content, body, attachments, links)
}
}
@@ -4,7 +4,9 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.os.Build
import android.app.AlertDialog
import com.getcapacitor.JSObject
import com.getcapacitor.Plugin
import com.getcapacitor.PluginCall
@@ -224,6 +226,34 @@ class EpMailPlugin : Plugin() {
}
}
@PluginMethod
fun openExternal(call: PluginCall) {
val raw = call.getString("url").orEmpty()
val uri = runCatching { Uri.parse(raw) }.getOrNull()
if (uri == null || uri.scheme?.lowercase(Locale.ROOT) !in setOf("http", "https") ||
uri.host.isNullOrBlank() || uri.userInfo != null) {
call.reject("Only standard web links can be opened from mail.")
return
}
AlertDialog.Builder(activity)
.setTitle("Open external link?")
.setMessage("Email links can lead to fake sign-in pages or harmful content.\n\nCheck the full address before continuing:\n${uri}")
.setNegativeButton("Cancel") { _, _ -> call.resolve(JSObject().put("opened", false)) }
.setPositiveButton("Open link") { _, _ ->
try {
val intent = Intent(Intent.ACTION_VIEW, uri)
activity.startActivity(intent)
call.resolve(JSObject().put("opened", true))
} catch (_: android.content.ActivityNotFoundException) {
call.reject("No browser is available to open this link.")
} catch (error: Exception) {
call.reject(error.message ?: "Could not open the link.")
}
}
.setOnCancelListener { call.resolve(JSObject().put("opened", false)) }
.show()
}
@PluginMethod
fun send(call: PluginCall) {
val input = JSONObject(call.data.toString()).apply {