diff --git a/notify-mail.hy b/notify-mail.hy index 8d6428f..32d3758 100755 --- a/notify-mail.hy +++ b/notify-mail.hy @@ -4,14 +4,17 @@ (import threading [Thread]) (import enum [StrEnum]) (import os [path]) +(import re) (import json) (import shutil) +(setv global-did-delete False) + (defclass OutputType [StrEnum] (setv LINES "text" JSON "json")) -(defn notmuch [#* args [output OutputType.LINES]] +(defn notmuch [#* args [output None]] (let [result (run ["notmuch" #* args] :stdout (if (is-not output None) @@ -27,39 +30,39 @@ mail-root None sender None subject None - file None attachment? False notified? False read? False) (defn __init__ [self uid mail-root sender subject - file attachment? notified? read?] + attachment? notified? read?] (setv self.uid uid self.mail-root mail-root self.sender sender self.subject subject - self.file file self.attachment? attachment? self.notified? notified? self.notified? read?)) (defn tag [self #* tags] - (when (is self.uid None) - (raise (ValueError "uid is None"))) (notmuch "tag" #* tags (+ "id:" self.uid))) (defn move [self dest] - (let [name (path.basename self.file) - new_path (+ mail-root "/" dest "/cur/" name)] - (shutil.move msg.file new_path))) + (let [old-path (self.get-filename) + name (re.sub "U=[0-9]+:[0-9]+,([DFPRS]*$)" + r"\1" (path.basename old-path)) + new-path (+ self.mail-root "/" dest "/cur/" name)] + (shutil.move old-path new-path))) + (defn get-filename [self] + (get (notmuch "search" "--output=files" (+ "id:" (str self.uid)) + :output OutputType.LINES) 0)) (defn from-json [root mail-root] (let [uid (get root "id") headers (get root "headers") sender (parse-from-address (get headers "From")) subject (get headers "Subject") - file (. root ["filename"] [0]) tags (get root "tags") attachment? (in "attachment" tags) notified? (not-in "notnotified" tags) read? (not-in "unread" tags)] - (Message uid mail-root sender subject file attachment? notified? read?))) + (Message uid mail-root sender subject attachment? notified? read?))) (defn __str__ [self] (+ "Message From \"" self.sender "\": " self.subject " (" @@ -96,10 +99,12 @@ (match result 0 (msg.tag "-unread") 1 (do + (setv global-did-delete True) (msg.tag "-unread") (msg.move "Trash"))))) -(let [mail-root (get (notmuch "config" "get" "database.mail_root") 0) +(let [mail-root (get (notmuch "config" "get" "database.mail_root" + :output OutputType.LINES) 0) json-root (notmuch "show" "--format=json" "--body=false" "tag:notnotified" "and" "tag:unread" "and" @@ -111,3 +116,6 @@ :args #(msg)))))) (notmuch "tag" "-notnotified" "*") +(when global-did-delete + (notmuch "tag" "+deleted" "folder:Trash") + (notmuch "new" "--no-hooks")) diff --git a/notmuch-mbsync-refresh.sh b/notmuch-mbsync-refresh.sh new file mode 100755 index 0000000..043385b --- /dev/null +++ b/notmuch-mbsync-refresh.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env zsh + +~/scripts/notmuch-pre.sh +mbsync -a +notmuch new +~/scripts/notmuch-post.sh