aboutsummaryrefslogtreecommitdiffstats
path: root/assets/hooks/mailrules.py
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-11-16 12:24:22 +0100
committerDanilo M. <danix@danix.xyz>2026-11-16 12:24:22 +0100
commit227258a199283089ec16e4798988277cf5062e67 (patch)
tree66500eb7a9f84b2ab85c3bbf1ab1c771c4eb48dd /assets/hooks/mailrules.py
parent21da4ee6c225d2b0b04fc51e17f39f446740a35a (diff)
downloadqtmaildir-227258a199283089ec16e4798988277cf5062e67.tar.gz
qtmaildir-227258a199283089ec16e4798988277cf5062e67.zip
feat(hooks): let a rule move new mail to spam or trash
A rule may carry "move": "spam" | "trash", an account role rather than a folder name. The post-new hook moves each matching tag:new message into its own account's configured folder under a fresh Maildir name in cur/, records the renames with one notmuch new --no-hooks, then tags +spam or +deleted, -inbox and the account-relative moved-from: origin. unread is never touched. A message with several files, in no account, or whose account has no folder for the role is logged and left alone, and a configured folder that does not exist is never created. Also fixes _quote, which escaped quotes with a backslash notmuch does not honour, so an id holding a quote matched nothing on the carve-out path. Item 214. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'assets/hooks/mailrules.py')
-rwxr-xr-xassets/hooks/mailrules.py24
1 files changed, 21 insertions, 3 deletions
diff --git a/assets/hooks/mailrules.py b/assets/hooks/mailrules.py
index dbb80a5..e96e0ad 100755
--- a/assets/hooks/mailrules.py
+++ b/assets/hooks/mailrules.py
@@ -41,7 +41,12 @@ DEFAULT_STAGE = 50
# Fields this version understands. Anything else in a rule object is kept in
# `unknown` and written back untouched, which is what makes the file neutral
# rather than this tool's file that another program may read.
-KNOWN_KEYS = {"id", "stage", "enabled", "add", "remove", "query", "note"}
+KNOWN_KEYS = {"id", "stage", "enabled", "add", "remove", "query", "note",
+ "move"}
+
+# What `move` may say. Mirrors qtmaildirconf.MOVE_ROLES and the C++ reader;
+# see "Changing the rule format" in AGENTS.md.
+MOVE_ROLES = ("spam", "trash")
# An id is a handle, not a display name: a UI selects on it and a diff tracks
# it. Tags may contain '/' and may be renamed; ids may not.
@@ -54,6 +59,7 @@ class Rule:
query: str
add: list = field(default_factory=list)
remove: list = field(default_factory=list)
+ move: str = ""
stage: int = DEFAULT_STAGE
enabled: bool = True
note: str = ""
@@ -194,6 +200,9 @@ def _rule_to_dict(rule):
"query": rule.query,
"note": rule.note,
}
+ # Only when set, so a file with no moves is byte-identical after a save.
+ if rule.move:
+ out["move"] = rule.move
out.update(rule.unknown)
return out
@@ -236,9 +245,17 @@ def _parse_rule(obj, index, seen, warnings):
add = [t for t in obj.get("add", []) if isinstance(t, str) and t.strip()]
remove = [t for t in obj.get("remove", []) if isinstance(t, str) and t.strip()]
- if not add and not remove:
+
+ move = obj.get("move", "")
+ if move and move not in MOVE_ROLES:
+ warnings.append(
+ f"rule '{rule_id}': move '{move}' is not spam or trash; "
+ f"ignoring the move")
+ move = ""
+
+ if not add and not remove and not move:
warnings.append(
- f"rule '{rule_id}': adds and removes nothing; dropped")
+ f"rule '{rule_id}': adds, removes and moves nothing; dropped")
return None
try:
@@ -254,6 +271,7 @@ def _parse_rule(obj, index, seen, warnings):
query=query,
add=add,
remove=remove,
+ move=move,
stage=stage,
enabled=bool(obj.get("enabled", True)),
note=obj.get("note", "") if isinstance(obj.get("note", ""), str) else "",