From 930b01bd09f42be77520b753c6068b7710c98c4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93scar=20N=C3=A1jera?= Date: Mon, 4 Jan 2021 15:12:52 +0100 Subject: Macros for queries --- bin/tagmail | 62 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 30 deletions(-) (limited to 'bin') diff --git a/bin/tagmail b/bin/tagmail index 2d65365..d8cda62 100755 --- a/bin/tagmail +++ b/bin/tagmail @@ -24,6 +24,7 @@ (ice-9 regex) (ice-9 receive) (mail-tools) + (utils) (srfi srfi-1) (system ffi-help-rt) (system foreign) @@ -95,32 +96,32 @@ (define (notmuch-tag options ffi-db rules new) (map (lambda (rule) (match-let* (((tags query-str) (with-new rule new)) - (info (if (null? (cddr rule)) query-str (caddr rule))) - (query (nm-query-db ffi-db query-str))) - (simple-format #t " ~a messages: ~a~a~%" - (nm-count-messages query) - (if (string=? info query-str) "" - (string-append info " | ")) - tags) - (nm-iter messages (nm-result-messages query) (apply-tags-to-message options tags)) - (notmuch_query_destroy query))) + (info (if (null? (cddr rule)) query-str (caddr rule)))) + (with-nm-query + (ffi-db query query-str) + (simple-format #t " ~a messages: ~a~a~%" + (nm-count-messages query) + (if (string=? info query-str) "" + (string-append info " | ")) + tags) + (nm-iter messages (nm-result-messages query) (apply-tags-to-message options tags))))) rules)) (define (list-tag options ffi-db new) (display (colorize-string "[LISTS filters]\n" 'BOLD 'YELLOW)) (match-let* (((tags-str query-str) (with-new '("+lists" "not tag:lists") new)) - (reg (make-regexp "<([a-z0-9=_-]+)\\." regexp/icase)) - (query (nm-query-db ffi-db query-str)) - (result (nm-iter - messages (nm-result-messages query) - (lambda (msg*) - (and-let* ((list-id (regexp-exec reg (nm-header msg* "list-id"))) - (tags (format #f "~a +lists/~a" tags-str - (string-downcase (match:substring list-id 1))))) - ((apply-tags-to-message options tags) msg*)))))) - (simple-format #t " ~a messages belonging to lists~%" - (length (filter identity result))) - (notmuch_query_destroy query))) + (reg (make-regexp "<([a-z0-9=_-]+)\\." regexp/icase))) + (with-nm-query + (ffi-db query query-str) + (->> (lambda (msg*) + (and-let* ((list-id (regexp-exec reg (nm-header msg* "list-id"))) + (tags (format #f "~a +lists/~a" tags-str + (string-downcase (match:substring list-id 1))))) + ((apply-tags-to-message options tags) msg*))) + (nm-iter messages (nm-result-messages query)) + (filter identity) + length + (simple-format #t " ~a messages belonging to lists~%"))))) (define (spam-channel push result) (setvbuf push 'line) @@ -145,15 +146,16 @@ "bogofilter -bTv" (lambda (push result) (display (colorize-string "[SPAM Flag]\n" 'BOLD 'YELLOW)) - (let* ((spam? (spam-channel push result)) - (query (nm-query-db ffi-db query-str)) - (result (nm-iter messages (nm-result-messages query) - (lambda (message) - (when (spam? (pointer->string (notmuch_message_get_filename message))) - ((apply-tags-to-message options "+spam -inbox") message)))))) - (format #t " ~a messages added to spam~%" - (length (filter boolean? result))) - (notmuch_query_destroy query))))) + (let ((spam? (spam-channel push result))) + (with-nm-query + (ffi-db query query-str) + (->> (lambda (message) + (when (spam? (pointer->string (notmuch_message_get_filename message))) + ((apply-tags-to-message options "+spam -inbox") message))) + (nm-iter messages (nm-result-messages query)) + (filter boolean?) + length + (format #t " ~a messages added to spam~%"))))))) (define tag-rules '(("+ci" "from:travis-ci.com or from:travis-ci.org or from:appveyor.com or from:circleci.com or from:mg.gitlab.com and subject:Pipeline") -- cgit v1.2.3