Update notify-mail.hy

This commit is contained in:
Alexander Rosenberg 2023-05-27 05:08:30 -07:00
parent f82931a538
commit fbf7e67019
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730

View File

@ -1,7 +1,7 @@
#!/usr/bin/env hy #!/usr/bin/env hy
(import email.parser :as parser) (import email.parser :as parser)
(import subprocess [run PIPE]) (import subprocess [run CompletedProcess PIPE])
(import threading [Thread]) (import threading [Thread])
(import shutil) (import shutil)
(import os) (import os)
@ -14,10 +14,28 @@
(defn get-new-messages [self folder] (defn get-new-messages [self folder]
(let [target-dir (+ self.maildir-path "/" folder "/new")] (let [target-dir (+ self.maildir-path "/" folder "/new")]
(lfor file (os.listdir target-dir) (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 [] (defclass MailMessage []
(setv inbox None (setv inbox None
uid None
file None file None
folder None folder None
sender None sender None
@ -26,9 +44,10 @@
read? False read? False
attachment? False attachment? False
new? 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)] (let [dir-path (os.path.dirname path)]
(setv self.inbox inbox (setv self.inbox inbox
self.uid uid
self.file (os.path.basename path) self.file (os.path.basename path)
self.folder (os.path.relpath (os.path.dirname dir-path) self.folder (os.path.relpath (os.path.dirname dir-path)
inbox.maildir-path) inbox.maildir-path)
@ -42,10 +61,12 @@
(+ self.inbox.maildir-path "/" (+ self.inbox.maildir-path "/"
self.folder "/" self.folder "/"
(if self.new? "new" "cur"))) (if self.new? "new" "cur")))
(defn get-full-path [self]
(+ (self.get-dir-path) "/" self.file))
(defn move [self new-folder] (defn move [self new-folder]
(let [clean-new-folder (MailMessage.-clean-folder new-folder)] (let [clean-new-folder (MailMessage.-clean-folder new-folder)]
(when (!= self.folder clean-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 "/" (+ self.inbox.maildir-path "/"
clean-new-folder "/" clean-new-folder "/"
(if self.new? "new" "cur") "/" (if self.new? "new" "cur") "/"
@ -53,7 +74,7 @@
(setv self.folder clean-new-folder)))) (setv self.folder clean-new-folder))))
(defn process [self] (defn process [self]
(when self.new? (when self.new?
(shutil.move (+ (self.get-dir-path) "/" self.file) (shutil.move (self.get-full-path)
(+ self.inbox.maildir-path "/" (+ self.inbox.maildir-path "/"
self.folder self.folder
"/cur/" "/cur/"
@ -93,6 +114,7 @@
(let [parse (parser.Parser) (let [parse (parser.Parser)
mail-obj (parse.parse file-obj :headersonly True)] mail-obj (parse.parse file-obj :headersonly True)]
(MailMessage inbox (MailMessage inbox
(mail-obj.get "Message-Id")
path path
(MailMessage.-parse-from-address (mail-obj.get "From")) (MailMessage.-parse-from-address (mail-obj.get "From"))
(mail-obj.get "Subject") (mail-obj.get "Subject")
@ -117,9 +139,9 @@
msg.subject msg.subject
:time 10000 :time 10000
:actions ["Mark Read" "Delete"]) :actions ["Mark Read" "Delete"])
0 (msg.mark-read) 0 (msg.inbox.mark-all-read msg.uid)
1 (do 1 (do
(msg.mark-read) (msg.inbox.mark-all-read msg.uid)
(msg.move "Trash"))))) (msg.move "Trash")))))
(when (< (len sys.argv) 2) (when (< (len sys.argv) 2)