aboutsummaryrefslogtreecommitdiffstats
path: root/notmuch-collect-tasks
blob: c11ce3baac53ab7e0b1961297e29f366b60def70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env sh
# -*- mode: scheme; -*-
IFS=" "
exec scsh -e main -s "$0" "$@"
!#
;; Copyright (C) 2022  Tom Willemse

;; This program is free software: you can redistribute it and/or modify it under
;; the terms of the GNU General Public License as published by the Free Software
;; Foundation, either version 3 of the License, or (at your option) any later
;; version.

;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
;; details.

;; You should have received a copy of the GNU General Public License along with
;; this program. If not, see <https://www.gnu.org/licenses/>.

(define message-query
  '(from:fidobill@fidomobile.ca
    and "subject:Your Fido bill is ready"
    or "subject:[alwaysdata] Negative balance to settle"
    or "subject:[alwaysdata] Automatic renewal of domains impossible"))

(define processed-tag-name "processed")

(define (message->task message)
  (let ((query (caadr (member ':query message)))
        (subject (cadr (member ':subject message)))
        (date (format-date "~Y-~m-~d ~a ~H:~M" (date))))
    (format #f "* TODO ~a
:PROPERTIES:
:CREATED:  [~a]
:END:

[[notmuch:~a][~a]]" subject date query subject)))

(define (print-task task)
  (format #t "~a~%~%" task))

(define (main args)
  (let ((output-file (cadr args))
        (messages (run/sexp
                   (notmuch search --format=sexp
                            "(" ,@message-query ")"
                            and not ,(format #f "tag:~a" processed-tag-name)))))
    (with-current-output-port
     (open-output-file output-file (file-options create append))
     (for-each (lambda (message)
                 (print-task (message->task message)))
               messages))
    (run (notmuch tag ,(format #f "+~a" processed-tag-name) ,@message-query)
         (> /dev/null))
    (format #t "Created ~a task~a~%"
            (length messages)
            (if (= 1 (length messages)) "" "s"))))