summaryrefslogtreecommitdiffhomepage
path: root/lib/net/nnnotmuch.el
blob: 7aa62eb72c88ead1a17dea7f3b58c1ffcc287a00 (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
266
267
268
269
270
271
272
273
274
275
276
;;; nnnotmuch.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)
(require 'notmuch)

(nnoo-declare nnnotmuch)

(defvoo nnnotmuch-directory message-directory
  "Spool directory for the nnnotmuch mail backend.")

(defvoo nnnotmuch-inhibit-expiry nil
  "If non-nil, inhibit expiry.")

(defvoo nnnotmuch-saved-searches nil)



;;; Interface functions.

(nnoo-define-basics nnnotmuch)

(deffoo nnnotmuch-retrieve-headers (sequence &optional group server fetch-old)
  (when (nnnotmuch-set-group-context group server)
    (with-current-buffer nntp-server-buffer
      (erase-buffer)
      (let* ((file nil)
	     (number (length sequence))
	     (count 0)
	     (file-name-coding-system nnmail-pathname-coding-system)
	     beg article)
	(if (stringp (car sequence))
	    'headers
	  (while sequence
	    (setq article (car sequence))
	    (setq file (nnnotmuch-article-to-file article))
	    (when (and file
		       (file-exists-p file)
		       (not (file-directory-p file)))
	      (insert (format "221 %d Article retrieved.\n" article))
	      (setq beg (point))
	      (nnheader-insert-head file)
	      (goto-char beg)
	      (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)))
	    (setq sequence (cdr sequence))
	    (setq count (1+ count))
	    (and (numberp nnmail-large-newsgroup)
		 (> number nnmail-large-newsgroup)
		 (zerop (% count 20))
		 (nnheader-message 6 "nnnotmuch: Receiving headers... %d%%"
				   (floor (* count 100.0) number))))
          (and (numberp nnmail-large-newsgroup)
		 (> number nnmail-large-newsgroup)
		 (nnheader-message 6 "nnml: Receiving headers...done"))
	  (nnheader-fold-continuation-lines)
	  'headers)))))

(deffoo nnnotmuch-open-server (server &optional defs)
  (nnoo-change-server 'nnnotmuch server defs)
  (cond
   ((not (file-exists-p nnnotmuch-directory))
    (nnnotmuch-close-server)
    (nnheader-report 'nnnotmuch "Directory doesn't exist: %s" nnnotmuch-directory))
   ((not (file-directory-p (file-truename nnnotmuch-directory)))
    (nnnotmuch-close-server)
    (nnheader-report 'nnnotmuch "Not a directory: %s" nnnotmuch-directory))
   (t
    (nnheader-report 'nnnotmuch "Opened server %s using directory %s"
		     server nnnotmuch-directory)
    t)))

(deffoo nnnotmuch-request-regenerate (server)
  (nnnotmuch-set-group-context nil server)
  t)

(deffoo nnnotmuch-request-article (id &optional group server buffer)
  (nnnotmuch-set-group-context group server)
  (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
	 (file-name-coding-system nnmail-pathname-coding-system)
	 (path (if (stringp id)
                   (nnnotmuch--find-message-file id)
                 (nnnotmuch-article-to-file id))))
    (cond
     ((not path)
      (nnheader-report 'nnnotmuch "No such article: %s" id))
     ((not (file-exists-p path))
      (nnheader-report 'nnnotmuch "No such file: %s" path))
     ((file-directory-p path)
      (nnheader-report 'nnnotmuch "File is a directory: %s" path))
     ((not (save-excursion (nnmail-find-file path)))
      (nnheader-report 'nnnotmuch "Couldn't read file: %s" path))
     (t
      (nnheader-report 'nnnotmuch "Article %s retrieved" id)
      (cons group id)))))

(deffoo nnnotmuch-request-group (group &optional server _dont-check info)
  (cond
   ((not (nnnotmuch-set-group-context group server))
    (nnheader-report 'nnnotmuch "Invalid group"))
   (t
    (let* ((info (or info (gnus-get-info group)))
           (num-headers (length nnnotmuch-article-files))
           (status (format "211 %d %d %d %s" num-headers 1 num-headers group)))
      (with-current-buffer nntp-server-buffer
        (erase-buffer)
        (nnheader-insert "%s\n" status)
        ;; (when info
        ;;   (gnus-info-set-marks
        ;;    info
        ;;    (append (assq-delete-all 'seen (gnus-info-marks info))
        ;;            (list `(seen (1 . ,num-headers))))
        ;;    t))
        ))
    t)))

(deffoo nnnotmuch-request-group-scan (group &optional server info)
  (setq nnnotmuch-article-files nil)
  (nnnotmuch-set-group-context group server)
  (gnus-get-unread-articles-in-group
   (or info (gnus-get-info gnus-newsgroup-name))
   (gnus-active (gnus-info-group info))))

(deffoo nnnotmuch-request-scan (&optional group server)
  (nnnotmuch-set-group-context group server))

(deffoo nnnotmuch-close-group (_group &optional _server)
  (setq nnnotmuch-article-files nil)
  t)

(deffoo nnnotmuch-request-create-group (group &optional server _args)
  (nnnotmuch-set-group-context nil server)
  (nnmail-activate 'nnnotmuch)
  (error "Not implemented yet"))

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

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

(deffoo nnnotmuch-request-list-newsgroups (&optional _server)
  nil)

(deffoo nnnotmuch-request-expire-articles (articles group &optional server force)
  (nnnotmuch-set-group-context group server)
  (when (eq 'delete nnmail-expiry-target)
    (error "Not implemented yet")))

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

(deffoo nnnotmuch-request-accept-article (group &optional server last)
  (error "Not implemented yet"))

(deffoo nnnotmuch-request-post (&optional server)
  (nnmail-do-request-post 'nnnotmuch-request-accept-article server))

(deffoo nnnotmuch-request-update-info (group info &optional server)
  (when-let (n (nnnotmuch--count-files (nnnotmuch--find-query group)))
    (gnus-set-active (gnus-group-prefixed-name group `(nnnotmuch ,server))
                     (cons 1 n))))

(deffoo nnnotmuch-request-replace-article (article group buffer)
  (error "Not implemented yet"))

(deffoo nnnotmuch-request-delete-group (group &optional force server)
  (error "Not implemented yet"))

(deffoo nnnotmuch-request-rename-group (group new-name &optional server)
  (error "Not implemented yet"))

(deffoo nnnotmuch-set-status (article name value &optional group server)
  (nnnotmuch-set-group-context group server)
  (let ((file (nnnotmuch-article-to-file article)))
    (cond
     ((not (file-exists-p file))
      (nnheader-report 'nnnotmuch "File %s does not exist" file))
     (t
      (message "Setting status %s to %s" name value)
      ;; (with-temp-file file
      ;;   (nnheader-insert-file-contents file)
      ;;   (nnmail-replace-status name value))
      t))))



(defvar nnnotmuch-article-files nil)

(defun nnnotmuch-article-to-file (article)
  (when (numberp article)
    (elt nnnotmuch-article-files (1- article))))

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

(defun nnnotmuch--find-group-message-ids (group &optional search)
  (when-let (q (or search (nnnotmuch--find-query group)))
    (notmuch-query-get-message-ids q)))

(defun nnnotmuch--find-message-file (id)
  (let ((q (notmuch-id-to-query id)))
    (string-trim
     (shell-command-to-string
      (format "notmuch search --output=files %s" q)))))

(defun nnnotmuch--count-files (query &optional unread)
  (let ((cmd (format "notmuch count --output=files -- %s%s"
                     (shell-quote-argument (format "(%s)" query))
                     (if unread " AND tag:unread" ""))))
    (string-to-number (shell-command-to-string cmd))))

(defun nnnotmuch--query-files (query)
  (let ((cmd (format "notmuch search --sort=oldest-first --output=files -- %s"
                     (shell-quote-argument query))))
    (split-string (shell-command-to-string cmd))))

(defun nnnotmuch--find-group-files (group)
  (when-let (q (nnnotmuch--find-query group))
    (nnnotmuch--query-files q)))

(defun nnnotmuch-set-group-context (group &optional server)
  (when (and server
	     (not (nnnotmuch-server-opened server)))
    (nnnotmuch-open-server server))
  (setq nnnotmuch-current-group group
	nnnotmuch-article-files (nnnotmuch--find-group-files group))
  t)




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