summaryrefslogtreecommitdiffhomepage
path: root/attic/elisp/nnnm.el
blob: 552e95ca7b0e1218f87ae489f0d4506587eefab2 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
;;; nnnm.el --- Gnus backend for notmuch        -*- lexical-binding: t; -*-

;; Copyright (C) 2021  jao

;; Author: jao <mail@jao.io>
;; Keywords: mail

;; 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/>.

;;; Commentary:

;; A Gnus mail backend using notmuch.

;;; Code:

(require 'gnus)
(require 'nnheader)
(require 'nnmail)
(require 'nnoo)


(nnoo-declare nnnm)

(defvar nnnm-marks-to-tags '((tick . "flagged")))

(defvar nnnm-saved-searches nil)

(defvar nnnm-maildir nil)

(defvar nnnm--group-data nil)

(defun nnnm--group-data (group) (cdr (assoc group nnnm--group-data)))

(defun nnnm--set-group-data (group data)
  (setf (alist-get group nnnm--group-data nil t #'string=) data))


(defun nnnm--find-query (name)
  (when-let (s (seq-find (lambda (s) (string= (plist-get s :name) name))
                         nnnm-saved-searches))
    (plist-get s :query)))

(defun nnnm--find-message-file (id)
  (car (split-string
        (shell-command-to-string
         (format "notmuch search --output=files %s"
                 (if (string-prefix-p "id:" id) id (concat "id:" id)))))))

(defun nnnm--article-data (article group)
  (cond ((stringp article) (list article))
        ((numberp article)
         (when-let (data (nnnm--group-data group))
           (elt data (1- article))))))

(defun nnnm-article-to-file (article group)
  (when-let (d (nnnm--article-data article group))
    (or (cadr d) (nnnm--find-message-file (car d)))))

(defun nnnm--count (query &optional context)
  (let ((cmd (format "notmuch count -- '(%s)%s'"
                     query
                     (if context (concat " AND " context) ""))))
    (string-to-number (shell-command-to-string cmd))))

(defun nnnm--search (query &optional context)
  (let ((cmd (format "notmuch search --sort=oldest-first --output=messages -- %s%s"
                     (shell-quote-argument (format "(%s)" query))
                     (if context (concat " AND " context) ""))))
    (split-string (shell-command-to-string cmd))))

(defun nnnm--tag (query tags)
  (let ((cmd (format "notmuch tag %s -- '(%s)'" tags query)))
    (shell-command-to-string cmd)))

(defun nnnm--ids-query (ids) (mapconcat #'identity ids " OR "))

(defun nnnm--prefixed (group server)
  (gnus-group-prefixed-name group `(nnnm ,server)))

(defun nnnm--get-group-marks (group server)
  (gnus-info-marks (gnus-get-info (nnnm--prefixed group server))))

(defun nnnm--set-group-marks (marks group server)
  (let* ((full-group (nnnm--prefixed group server))
         (info (gnus-get-info full-group)))
    (gnus-info-set-marks info marks)
    (gnus-set-info full-group info)))

(defun nnnm--subtract-from-ranges (ranges lst)
  (let ((ranges (gnus-uncompress-sequence ranges)))
    (dolist (n lst)
      (let ((rs (seq-group-by (lambda (r) (> n r)) ranges)))
        (setq ranges
              (append (alist-get t rs) (mapcar #'1- (alist-get nil rs))))))
    (gnus-compress-sequence ranges)))

(defun nnnm--remove-articles-from-marks (ranges group server)
  (let ((marks (nnnm--get-group-marks group server))
        (lst (gnus-uncompress-sequence ranges))
        (new-marks))
    (dolist (m marks)
      (push (cons (car m) (nnnm--subtract-from-ranges (cdr m))) lst))
    (nnnm--set-group-marks marks group server)))

(defun nnnm--set-active (n group server)
  (gnus-set-active (nnnm--prefixed group server) (cons 1 n)))

(defun nnnm--update-group-data (group &optional server)
  (when-let (query (nnnm--find-query group))
    (let* ((data (or (nnnm--group-data group)
                     (mapcar #'list (nnnm--search query "NOT tag:new"))))
           (ids (nnnm--search query "tag:new"))
           (nids (length ids))
           (new-data (mapcar (lambda (id)
                               (list id (nnnm--find-message-file id)))
                             ids)))
      (when (> nids 0)
        (nnnm--tag (nnnm--ids-query ids) "-new")
        (nnheader-report 'nnnm "%s: %d new messages retrieved" group nids))
      (nnnm--set-group-data group (append data new-data))
      (nnnm--set-active (+ nids (length data)) group server)
      (length ids))))



;;; Interface functions.

(nnoo-define-basics nnnm)

(defun nnnm-request-type (_group &optional _article)
  'mail)

(deffoo nnnm-open-server (server &optional defs)
  (nnoo-change-server 'nnnm server defs)
  (setq nnnm--group-data nil)
  (nnheader-report 'nnnm "Opened server %s" server)
  t)

(deffoo nnnm-close-server (_server)
  (setq nnnm--group-data nil))

(deffoo nnnm-request-regenerate (_server)
  (setq nnnm--group-data nil)
  t)

(deffoo nnnm-request-list (&optional _server)
  (with-current-buffer nntp-server-buffer
    (erase-buffer)
    (dolist (s nnnm-saved-searches)
      (when-let (query (plist-get s :query))
        (let ((name (plist-get s :name))
              (total (nnnm--count query)))
          (insert (format "%s %d 1 y\n" name total))))))
  t)

(deffoo nnnm-retrieve-headers (sequence &optional group server _fetch-old)
  (when (nnnm--update-group-data group server)
    (with-current-buffer nntp-server-buffer
      (delete-region (point-min) (point-max))
      (let ((file-name-coding-system nnmail-pathname-coding-system)
	    (count -1))
	(if (stringp (car sequence))
	    'headers
          (dolist (article sequence)
            (when-let (file (nnnm-article-to-file article group))
	      (insert (format "221 %d Article retrieved.\n" article))
              (save-excursion (nnheader-insert-head file))
	      (if (re-search-forward "\n\r?\n" nil t)
		  (forward-char -1)
		(goto-char (point-max))
		(insert "\n\n"))
	      (insert ".\n")
	      (delete-region (point) (point-max)))
            (when (zerop (% (cl-incf count) 20))
	      (nnheader-message 6 "nnnm: Receiving headers... %d%%"
				(floor (* count 100.0) (length sequence)))))
          (nnheader-fold-continuation-lines)
	  'headers)))))

(deffoo nnnm-request-article (id &optional group _server buffer)
  (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
	 (file-name-coding-system nnmail-pathname-coding-system)
	 (d (nnnm--article-data id group))
         (id (car d))
         (file (when id (or (cadr d) (nnnm--find-message-file id)))))
    (cond
     ((not file)
      (nnheader-report 'nnnm "No such article: %s" id))
     ((not (file-exists-p file))
      (nnheader-report 'nnnm "No such file: %s" file))
     ((not (save-excursion (nnmail-find-file file)))
      (nnheader-report 'nnnm "Couldn't read file: %s" file))
     (t
      (nnnm--tag id "-unread")
      (nnheader-report 'nnnm "Article %s retrieved and tagged" id)
      (cons group id)))))

(deffoo nnnm-request-expire-articles (articles group &optional _server _force)
  (let* ((articles (gnus-uncompress-range articles))
         (ids (mapcar (lambda (a) (car (nnnm--article-data a group))) articles)))
    (when ids
      (nnnm--tag (nnnm--ids-query ids) "+deleted")
      (let ((data (nnnm--group-data group)))
        (dolist (id ids)
          (setq data
                (cl-delete-if (lambda (d) (string= (car d) id)) data :count 1)))
        (nnnm--set-group-data group data)
        (nnnm--remove-articles-from-marks articles group server)
        (nnnm--set-active (length data) group server)))
    articles))

(deffoo nnnm-request-set-mark (group actions &optional _server)
  (message "set marks: %s: %S" group actions)
  actions)

;; (deffoo nnnm-request-move-article
;;     (article group server accept-form &optional last _move-is-internal)
;;   (error "Not implemented yet"))

(deffoo nnnm-request-group (group &optional server _dont-check info)
  (nnheader-message 7 "nnnm: Opening %s -- %s" info group)
  (if (nnnm--update-group-data group server)
      (let ((n (length (nnnm--group-data group))))
        (with-current-buffer nntp-server-buffer
          (erase-buffer)
          (nnheader-insert "211 %d %d %d %s\n" n 1 n group)
          n))
    (nnheader-report 'nnnm "Invalid group")))

(deffoo nnnm-request-newgroups (_date &optional server)
  (nnnm-request-list server))

(deffoo nnnm-request-group-scan (group &optional server _info)
  (nnnm--set-group-data group nil)
  (nnnm--update-group-data group server))

(deffoo nnnm-request-scan (&optional group server)
  (if group
      (nnnm--update-group-data group server)
    (setq nnnm--group-data nil)))

(deffoo nnnm-request-create-group (group &optional _server _args)
  (let ((query (read-string "Query: ")))
    (add-to-list 'nnnm-saved-searches `(:name ,group :query ,query))))

;; (deffoo nnnm-request-rename-group (group new-name &optional _server)
;;   (error "Not implemented yet"))

(deffoo nnnm-close-group (_group &optional _server) t)



(provide 'nnnm)
;;; nnnm.el ends here