Add per-account mail rules and settings
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -325,10 +325,49 @@ class EpMailPlugin : Plugin() {
|
||||
.put("displayName", displayName)
|
||||
.put("imapHost", imapHost)
|
||||
.put("smtpHost", smtpHost)
|
||||
.put("signature", input.optString("signature").take(5000))
|
||||
.put("notificationsEnabled", input.optBoolean("notificationsEnabled", true))
|
||||
.put("syncIntervalMinutes", input.optInt("syncIntervalMinutes", 2).also {
|
||||
require(it in setOf(2, 5, 15, 30, 60)) { "Choose a supported mailbox sync interval." }
|
||||
})
|
||||
.put("rules", normalizeRules(input.optJSONArray("rules")))
|
||||
.put("imapPort", input.optInt("imapPort", 143))
|
||||
.put("smtpPort", input.optInt("smtpPort", 587))
|
||||
}
|
||||
|
||||
private fun normalizeRules(input: JSONArray?): JSONArray {
|
||||
val result = JSONArray()
|
||||
if (input == null) return result
|
||||
require(input.length() <= 50) { "You can set up to 50 mail rules per account." }
|
||||
for (index in 0 until input.length()) {
|
||||
val rule = input.getJSONObject(index)
|
||||
val sender = rule.optString("senderContains").trim().take(200)
|
||||
val subject = rule.optString("subjectContains").trim().take(200)
|
||||
val action = rule.optString("action")
|
||||
val value = rule.optString("value").trim().take(200)
|
||||
require((sender.isNotEmpty() || subject.isNotEmpty()) &&
|
||||
action in setOf("archive", "move", "label", "flag")) {
|
||||
"Mail rule ${index + 1} needs a sender or subject condition and a valid action."
|
||||
}
|
||||
require(action !in setOf("move", "label") || value.isNotEmpty()) {
|
||||
"Mail rule ${index + 1} needs a destination folder or label."
|
||||
}
|
||||
require(action != "move" || !value.any { it == '\r' || it == '\n' || it == '\u0000' }) {
|
||||
"Mail rule ${index + 1} has an invalid destination folder."
|
||||
}
|
||||
require(action != "move" || value in setOf("Inbox", "Archive", "Trash")) {
|
||||
"Mail rule ${index + 1} must move messages to Inbox, Archive, or Trash."
|
||||
}
|
||||
result.put(JSONObject()
|
||||
.put("id", rule.optString("id").ifBlank { UUID.randomUUID().toString() })
|
||||
.put("senderContains", sender)
|
||||
.put("subjectContains", subject)
|
||||
.put("action", action)
|
||||
.put("value", value))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private fun JSONObject.toJSObject(): JSObject = JSObject(toString())
|
||||
|
||||
companion object {
|
||||
|
||||
Reference in New Issue
Block a user