From fbf7e670199bb2c0390597c6f29e3a57f136bb90 Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Sat, 27 May 2023 05:08:30 -0700 Subject: [PATCH] Update notify-mail.hy --- mail/notify-mail.hy | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/mail/notify-mail.hy b/mail/notify-mail.hy index 438222e..3d07359 100755 --- a/mail/notify-mail.hy +++ b/mail/notify-mail.hy @@ -1,7 +1,7 @@ #!/usr/bin/env hy (import email.parser :as parser) -(import subprocess [run PIPE]) +(import subprocess [run CompletedProcess PIPE]) (import threading [Thread]) (import shutil) (import os) @@ -14,10 +14,28 @@ (defn get-new-messages [self folder] (let [target-dir (+ self.maildir-path "/" folder "/new")] (lfor file (os.listdir target-dir) - (MailMessage.from-file self (+ target-dir "/" file)))))) + (MailMessage.from-file self (+ target-dir "/" file))))) + (defn find-by-uid [self uid [exclude-msgs None]] + (let [files (str.splitlines + (. (run ["rg" "-Fl" uid self.maildir-path] + :stdout PIPE :text True) stdout)) + output #{}] + (when (is-not exclude-msgs None) + (for [excluded exclude-msgs] + (files.remove (os.path.abspath (excluded.get-full-path))))) + (for [file files] + (let [msg (MailMessage.from-file self file)] + (when (= msg.uid uid) + (output.add msg)))) + output)) + (defn mark-all-read [self uid [exclude-msgs None]] + (let [files (self.find-by-uid uid exclude-msgs)] + (for [file files] + (file.mark-read))))) (defclass MailMessage [] (setv inbox None + uid None file None folder None sender None @@ -26,9 +44,10 @@ read? False attachment? False new? False) - (defn __init__ [self inbox path sender subject attachment?] + (defn __init__ [self inbox uid path sender subject attachment?] (let [dir-path (os.path.dirname path)] (setv self.inbox inbox + self.uid uid self.file (os.path.basename path) self.folder (os.path.relpath (os.path.dirname dir-path) inbox.maildir-path) @@ -42,10 +61,12 @@ (+ self.inbox.maildir-path "/" self.folder "/" (if self.new? "new" "cur"))) + (defn get-full-path [self] + (+ (self.get-dir-path) "/" self.file)) (defn move [self new-folder] (let [clean-new-folder (MailMessage.-clean-folder new-folder)] (when (!= self.folder clean-new-folder) - (shutil.move (+ (self.get-dir-path) "/" self.file) + (shutil.move (self.get-full-path) (+ self.inbox.maildir-path "/" clean-new-folder "/" (if self.new? "new" "cur") "/" @@ -53,7 +74,7 @@ (setv self.folder clean-new-folder)))) (defn process [self] (when self.new? - (shutil.move (+ (self.get-dir-path) "/" self.file) + (shutil.move (self.get-full-path) (+ self.inbox.maildir-path "/" self.folder "/cur/" @@ -93,6 +114,7 @@ (let [parse (parser.Parser) mail-obj (parse.parse file-obj :headersonly True)] (MailMessage inbox + (mail-obj.get "Message-Id") path (MailMessage.-parse-from-address (mail-obj.get "From")) (mail-obj.get "Subject") @@ -117,9 +139,9 @@ msg.subject :time 10000 :actions ["Mark Read" "Delete"]) - 0 (msg.mark-read) + 0 (msg.inbox.mark-all-read msg.uid) 1 (do - (msg.mark-read) + (msg.inbox.mark-all-read msg.uid) (msg.move "Trash"))))) (when (< (len sys.argv) 2)