summaryrefslogtreecommitdiffhomepage
path: root/lib/net/nnnm.el
blob: 3df1386963da1092d81cc2425791a627c2219039 (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
;;; 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)

(defvoo 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--init-group-data (full-group query)
  (nnnm--tag (concat query " AND NOT tag:new AND not tag:nnnm") "+new")
  (let* ((ids (nnnm--search query "tag:nnnm"))
         (nids (length ids))
         (info (gnus-get-info full-group))
         (read (when (> nids 0) (list (cons 1 nids))))
         (marks (list (cons 'seen read))))
    (message "*** group info before %s - %s" full-group (gnus-get-info full-group))
    (gnus-info-set-marks info marks)
    (setf (gnus-info-read info) read)
    (gnus-set-info full-group info)
    (message "*** group info after %s" (gnus-get-info full-group))
    (mapcar #'list ids)))

(defun nnnm--update-group-data (group &optional server)
  ;; (when (and server (not (nnnm-server-opened server)))
  ;;   (nnnm-open-server server))
  (when-let (query (nnnm--find-query group))
    (let* ((full-group (gnus-group-prefixed-name group `(nnnm ,server)))
           (data (or (nnnm--group-data group)
                     (nnnm--init-group-data full-group query)))
           (ids (nnnm--search query "tag:new AND NOT tag:nnnm"))
           (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))
      (gnus-set-active (gnus-group-prefixed-name group `(nnnm ,server))
                       (cons 1 (+ nids (length data))))
      (length ids))))



;;; Interface functions.

(nnoo-define-basics nnnm)

(deffoo nnnm-open-server (server &optional defs)
  (nnoo-change-server 'nnnm server defs)
  (setq nnnm--group-data nil)
  ;; (when (and nnnm-maildir (file-exists-p nnnm-maildir))
  ;;     )
  (message "defs: %s" defs)
  (nnheader-report 'nnnm "Opened server %s using maildir %s" server nnnm-maildir)
  t)

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

(deffoo nnnm-request-regenerate (_server)
  (setq nnnm--group-data nil)
  (nnnm--tag "*" "-nnnm")
  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 "+nnnm")
      (nnheader-report 'nnnm "Article %s retrieved and tagged" id)
      (cons group id)))))

(deffoo nnnm-request-expire-articles (articles group &optional _server _force)
  (let ((excluded) (ids))
    (dolist (a articles)
      (if-let (id (car (nnnm--article-data a group)))
          (push id ids)
        (push a excluded)))
    (when ids
      (nnnm--tag (nnnm--ids-query ids) "+deleted +nnnm"))
    excluded))

;; (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