Identify unsafe combinations of Bcc and encryption
[gnus] / lisp / nnmaildir.el
1 ;;; nnmaildir.el --- maildir backend for Gnus
2
3 ;; This file is in the public domain.
4
5 ;; Author: Paul Jarc <prj@po.cwru.edu>
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;; Maildir format is documented at <URL:http://cr.yp.to/proto/maildir.html>
25 ;; and in the maildir(5) man page from qmail (available at
26 ;; <URL:http://www.qmail.org/man/man5/maildir.html>).  nnmaildir also stores
27 ;; extra information in the .nnmaildir/ directory within a maildir.
28 ;;
29 ;; Some goals of nnmaildir:
30 ;; * Everything Just Works, and correctly.  E.g., NOV data is automatically
31 ;;   regenerated when stale; no need for manually running
32 ;;   *-generate-nov-databases.
33 ;; * Perfect reliability: [C-g] will never corrupt its data in memory, and
34 ;;   SIGKILL will never corrupt its data in the filesystem.
35 ;; * Allow concurrent operation as much as possible.  If files change out
36 ;;   from under us, adapt to the changes or degrade gracefully.
37 ;; * We use the filesystem as a database, so that, e.g., it's easy to
38 ;;   manipulate marks from outside Gnus.
39 ;; * All information about a group is stored in the maildir, for easy backup,
40 ;;   copying, restoring, etc.
41 ;;
42 ;; Todo:
43 ;; * When moving an article for expiry, copy all the marks except 'expire
44 ;;   from the original article.
45 ;; * Add a hook for when moving messages from new/ to cur/, to support
46 ;;   nnmail's duplicate detection.
47 ;; * Improve generated Xrefs, so crossposts are detectable.
48 ;; * Improve code readability.
49
50 ;;; Code:
51
52 ;; eval this before editing
53 [(progn
54    (put 'nnmaildir--with-nntp-buffer 'lisp-indent-function 0)
55    (put 'nnmaildir--with-work-buffer 'lisp-indent-function 0)
56    (put 'nnmaildir--with-nov-buffer  'lisp-indent-function 0)
57    (put 'nnmaildir--with-move-buffer 'lisp-indent-function 0)
58    (put 'nnmaildir--condcase         'lisp-indent-function 2)
59    )
60 ]
61
62 (require 'nnheader)
63 (require 'gnus)
64 (require 'gnus-util)
65 (require 'gnus-range)
66 (require 'gnus-start)
67 (require 'gnus-int)
68 (require 'message)
69 (require 'nnmail)
70
71 (eval-when-compile
72   (require 'cl))
73
74 (defconst nnmaildir-version "Gnus")
75
76 (defconst nnmaildir-flag-mark-mapping
77   '((?F . tick)
78     (?P . forward)
79     (?R . reply)
80     (?S . read))
81   "Alist mapping Maildir filename flags to Gnus marks.
82 Maildir filenames are of the form \"unique-id:2,FLAGS\",
83 where FLAGS are a string of characters in ASCII order.
84 Some of the FLAGS correspond to Gnus marks.")
85
86 (defsubst nnmaildir--mark-to-flag (mark)
87   "Find the Maildir flag that corresponds to MARK (an atom).
88 Return a character, or nil if not found.
89 See `nnmaildir-flag-mark-mapping'."
90   (car (rassq mark nnmaildir-flag-mark-mapping)))
91
92 (defsubst nnmaildir--flag-to-mark (flag)
93   "Find the Gnus mark that corresponds to FLAG (a character).
94 Return an atom, or nil if not found.
95 See `nnmaildir-flag-mark-mapping'."
96   (cdr (assq flag nnmaildir-flag-mark-mapping)))
97
98 (defun nnmaildir--ensure-suffix (filename)
99   "Ensure that FILENAME contains the suffix \":2,\"."
100   (if (gnus-string-match-p ":2," filename)
101       filename
102     (concat filename ":2,")))
103
104 (defun nnmaildir--add-flag (flag suffix)
105   "Return a copy of SUFFIX where FLAG is set.
106 SUFFIX should start with \":2,\"."
107   (unless (gnus-string-match-p "^:2," suffix)
108     (error "Invalid suffix `%s'" suffix))
109   (let* ((flags (substring suffix 3))
110          (flags-as-list (append flags nil))
111          (new-flags
112           (concat (gnus-delete-duplicates
113                    ;; maildir flags must be sorted
114                    (sort (cons flag flags-as-list) '<)))))
115     (concat ":2," new-flags)))
116
117 (defun nnmaildir--remove-flag (flag suffix)
118   "Return a copy of SUFFIX where FLAG is cleared.
119 SUFFIX should start with \":2,\"."
120   (unless (gnus-string-match-p "^:2," suffix)
121     (error "Invalid suffix `%s'" suffix))
122   (let* ((flags (substring suffix 3))
123          (flags-as-list (append flags nil))
124          (new-flags (concat (delq flag flags-as-list))))
125     (concat ":2," new-flags)))
126
127 (defvar nnmaildir-article-file-name nil
128   "*The filename of the most recently requested article.  This variable is set
129 by nnmaildir-request-article.")
130
131 ;; The filename of the article being moved/copied:
132 (defvar nnmaildir--file nil)
133
134 ;; Variables to generate filenames of messages being delivered:
135 (defvar   nnmaildir--delivery-time "")
136 (defconst nnmaildir--delivery-pid (concat "P" (number-to-string (emacs-pid))))
137 (defvar   nnmaildir--delivery-count nil)
138
139 ;; An obarry containing symbols whose names are server names and whose values
140 ;; are servers:
141 (defvar nnmaildir--servers (make-vector 3 0))
142 ;; The current server:
143 (defvar nnmaildir--cur-server nil)
144
145 ;; A copy of nnmail-extra-headers
146 (defvar nnmaildir--extra nil)
147
148 ;; A NOV structure looks like this (must be prin1-able, so no defstruct):
149 ["subject\tfrom\tdate"
150  "references\tchars\tlines"
151  "To: you\tIn-Reply-To: <your.mess@ge>"
152  (12345 67890)     ;; modtime of the corresponding article file
153  (to in-reply-to)] ;; contemporary value of nnmail-extra-headers
154 (defconst nnmaildir--novlen 5)
155 (defmacro nnmaildir--nov-new (beg mid end mtime extra)
156   `(vector ,beg ,mid ,end ,mtime ,extra))
157 (defmacro nnmaildir--nov-get-beg   (nov) `(aref ,nov 0))
158 (defmacro nnmaildir--nov-get-mid   (nov) `(aref ,nov 1))
159 (defmacro nnmaildir--nov-get-end   (nov) `(aref ,nov 2))
160 (defmacro nnmaildir--nov-get-mtime (nov) `(aref ,nov 3))
161 (defmacro nnmaildir--nov-get-extra (nov) `(aref ,nov 4))
162 (defmacro nnmaildir--nov-set-beg   (nov value) `(aset ,nov 0 ,value))
163 (defmacro nnmaildir--nov-set-mid   (nov value) `(aset ,nov 1 ,value))
164 (defmacro nnmaildir--nov-set-end   (nov value) `(aset ,nov 2 ,value))
165 (defmacro nnmaildir--nov-set-mtime (nov value) `(aset ,nov 3 ,value))
166 (defmacro nnmaildir--nov-set-extra (nov value) `(aset ,nov 4 ,value))
167
168 (defstruct nnmaildir--art
169   (prefix nil :type string)  ;; "time.pid.host"
170   (suffix nil :type string)  ;; ":2,flags"
171   (num    nil :type natnum)  ;; article number
172   (msgid  nil :type string)  ;; "<mess.age@id>"
173   (nov    nil :type vector)) ;; cached nov structure, or nil
174
175 (defstruct nnmaildir--grp
176   (name  nil :type string)  ;; "group.name"
177   (new   nil :type list)    ;; new/ modtime
178   (cur   nil :type list)    ;; cur/ modtime
179   (min   1   :type natnum)  ;; minimum article number
180   (count 0   :type natnum)  ;; count of articles
181   (nlist nil :type list)    ;; list of articles, ordered descending by number
182   (flist nil :type vector)  ;; obarray mapping filename prefix->article
183   (mlist nil :type vector)  ;; obarray mapping message-id->article
184   (cache nil :type vector)  ;; nov cache
185   (index nil :type natnum)  ;; index of next cache entry to replace
186   (mmth  nil :type vector)) ;; obarray mapping mark name->dir modtime
187                                         ; ("Mark Mod Time Hash")
188
189 (defstruct nnmaildir--srv
190   (address       nil :type string)         ;; server address string
191   (method        nil :type list)           ;; (nnmaildir "address" ...)
192   (prefix        nil :type string)         ;; "nnmaildir+address:"
193   (dir           nil :type string)         ;; "/expanded/path/to/server/dir/"
194   (ls            nil :type function)       ;; directory-files function
195   (groups        nil :type vector)         ;; obarray mapping group name->group
196   (curgrp        nil :type nnmaildir--grp) ;; current group, or nil
197   (error         nil :type string)         ;; last error message, or nil
198   (mtime         nil :type list)           ;; modtime of dir
199   (gnm           nil)                      ;; flag: split from mail-sources?
200   (target-prefix nil :type string))        ;; symlink target prefix
201
202 (defun nnmaildir--article-set-flags (article new-suffix curdir)
203   (let* ((prefix (nnmaildir--art-prefix article))
204          (suffix (nnmaildir--art-suffix article))
205          (article-file (concat curdir prefix suffix))
206          (new-name (concat curdir prefix new-suffix)))
207     (unless (file-exists-p article-file)
208       (error "Couldn't find article file %s" article-file))
209     (rename-file article-file new-name 'replace)
210     (setf (nnmaildir--art-suffix article) new-suffix)))
211
212 (defun nnmaildir--expired-article (group article)
213   (setf (nnmaildir--art-nov article) nil)
214   (let ((flist  (nnmaildir--grp-flist group))
215         (mlist  (nnmaildir--grp-mlist group))
216         (min    (nnmaildir--grp-min   group))
217         (count  (1- (nnmaildir--grp-count group)))
218         (prefix (nnmaildir--art-prefix article))
219         (msgid  (nnmaildir--art-msgid  article))
220         (new-nlist nil)
221         (nlist-pre '(nil . nil))
222         nlist-post num)
223     (unless (zerop count)
224       (setq nlist-post (nnmaildir--grp-nlist group)
225             num (nnmaildir--art-num article))
226       (if (eq num (caar nlist-post))
227           (setq new-nlist (cdr nlist-post))
228         (setq new-nlist nlist-post
229               nlist-pre nlist-post
230               nlist-post (cdr nlist-post))
231         (while (/= num (caar nlist-post))
232           (setq nlist-pre nlist-post
233                 nlist-post (cdr nlist-post)))
234         (setq nlist-post (cdr nlist-post))
235         (if (eq num min)
236             (setq min (caar nlist-pre)))))
237     (let ((inhibit-quit t))
238       (setf (nnmaildir--grp-min   group) min)
239       (setf (nnmaildir--grp-count group) count)
240       (setf (nnmaildir--grp-nlist group) new-nlist)
241       (setcdr nlist-pre nlist-post)
242       (unintern prefix flist)
243       (unintern msgid mlist))))
244
245 (defun nnmaildir--nlist-art (group num)
246   (let ((entry (assq num (nnmaildir--grp-nlist group))))
247     (if entry
248         (cdr entry))))
249 (defmacro nnmaildir--flist-art (list file)
250   `(symbol-value (intern-soft ,file ,list)))
251 (defmacro nnmaildir--mlist-art (list msgid)
252   `(symbol-value (intern-soft ,msgid ,list)))
253
254 (defun nnmaildir--pgname (server gname)
255   (let ((prefix (nnmaildir--srv-prefix server)))
256     (if prefix (concat prefix gname)
257       (setq gname (gnus-group-prefixed-name gname
258                                             (nnmaildir--srv-method server)))
259       (setf (nnmaildir--srv-prefix server) (gnus-group-real-prefix gname))
260       gname)))
261
262 (defun nnmaildir--param (pgname param)
263   (setq param (gnus-group-find-parameter pgname param 'allow-list))
264   (if (vectorp param) (setq param (aref param 0)))
265   (eval param))
266
267 (defmacro nnmaildir--with-nntp-buffer (&rest body)
268   (declare (debug (body)))
269   `(with-current-buffer nntp-server-buffer
270      ,@body))
271 (defmacro nnmaildir--with-work-buffer (&rest body)
272   (declare (debug (body)))
273   `(with-current-buffer (get-buffer-create " *nnmaildir work*")
274      ,@body))
275 (defmacro nnmaildir--with-nov-buffer (&rest body)
276   (declare (debug (body)))
277   `(with-current-buffer (get-buffer-create " *nnmaildir nov*")
278      ,@body))
279 (defmacro nnmaildir--with-move-buffer (&rest body)
280   (declare (debug (body)))
281   `(with-current-buffer (get-buffer-create " *nnmaildir move*")
282      ,@body))
283
284 (defsubst nnmaildir--subdir (dir subdir)
285   (file-name-as-directory (concat dir subdir)))
286 (defsubst nnmaildir--srvgrp-dir (srv-dir gname)
287   (nnmaildir--subdir srv-dir gname))
288 (defsubst nnmaildir--tmp       (dir) (nnmaildir--subdir dir "tmp"))
289 (defsubst nnmaildir--new       (dir) (nnmaildir--subdir dir "new"))
290 (defsubst nnmaildir--cur       (dir) (nnmaildir--subdir dir "cur"))
291 (defsubst nnmaildir--nndir     (dir) (nnmaildir--subdir dir ".nnmaildir"))
292 (defsubst nnmaildir--nov-dir   (dir) (nnmaildir--subdir dir "nov"))
293 (defsubst nnmaildir--marks-dir (dir) (nnmaildir--subdir dir "marks"))
294 (defsubst nnmaildir--num-dir   (dir) (nnmaildir--subdir dir "num"))
295
296 (defmacro nnmaildir--unlink (file-arg)
297   `(let ((file ,file-arg))
298      (if (file-attributes file) (delete-file file))))
299 (defun nnmaildir--mkdir (dir)
300   (or (file-exists-p (file-name-as-directory dir))
301       (make-directory-internal (directory-file-name dir))))
302 (defun nnmaildir--mkfile (file)
303   (write-region "" nil file nil 'no-message))
304 (defun nnmaildir--delete-dir-files (dir ls)
305   (when (file-attributes dir)
306     (mapc 'delete-file (funcall ls dir 'full "\\`[^.]" 'nosort))
307     (delete-directory dir)))
308
309 (defun nnmaildir--group-maxnum (server group)
310   (catch 'return
311     (if (zerop (nnmaildir--grp-count group)) (throw 'return 0))
312     (let ((dir (nnmaildir--srvgrp-dir (nnmaildir--srv-dir server)
313                                     (nnmaildir--grp-name group)))
314           (number-opened 1)
315           attr ino-opened nlink number-linked)
316       (setq dir (nnmaildir--nndir dir)
317             dir (nnmaildir--num-dir dir))
318       (while t
319         (setq attr (file-attributes
320                     (concat dir (number-to-string number-opened))))
321         (or attr (throw 'return (1- number-opened)))
322         (setq ino-opened (nth 10 attr)
323               nlink (nth 1 attr)
324               number-linked (+ number-opened nlink))
325         (if (or (< nlink 1) (< number-linked nlink))
326             (signal 'error '("Arithmetic overflow")))
327         (setq attr (file-attributes
328                     (concat dir (number-to-string number-linked))))
329         (or attr (throw 'return (1- number-linked)))
330         (unless (equal ino-opened (nth 10 attr))
331           (setq number-opened number-linked))))))
332
333 ;; Make the given server, if non-nil, be the current server.  Then make the
334 ;; given group, if non-nil, be the current group of the current server.  Then
335 ;; return the group object for the current group.
336 (defun nnmaildir--prepare (server group)
337   (catch 'return
338     (if (null server)
339         (unless (setq server nnmaildir--cur-server)
340           (throw 'return nil))
341       (unless (setq server (intern-soft server nnmaildir--servers))
342         (throw 'return nil))
343       (setq server (symbol-value server)
344             nnmaildir--cur-server server))
345     (let ((groups (nnmaildir--srv-groups server)))
346       (when groups
347         (unless (nnmaildir--srv-method server)
348           (setf (nnmaildir--srv-method server)
349                 (or (gnus-server-to-method
350                      (concat "nnmaildir:" (nnmaildir--srv-address server)))
351                     (throw 'return nil))))
352         (if (null group)
353             (nnmaildir--srv-curgrp server)
354           (symbol-value (intern-soft group groups)))))))
355
356 (defun nnmaildir--tab-to-space (string)
357   (let ((pos 0))
358     (while (string-match "\t" string pos)
359       (aset string (match-beginning 0) ? )
360       (setq pos (match-end 0))))
361   string)
362
363 (defmacro nnmaildir--condcase (errsym body &rest handler)
364   (declare (debug (sexp form body)))
365   `(condition-case ,errsym
366        (let ((system-messages-locale "C")) ,body)
367      (error . ,handler)))
368
369 (defun nnmaildir--emlink-p (err)
370   (and (eq (car err) 'file-error)
371        (string= (downcase (caddr err)) "too many links")))
372
373 (defun nnmaildir--enoent-p (err)
374   (and (eq (car err) 'file-error)
375        (string= (downcase (caddr err)) "no such file or directory")))
376
377 (defun nnmaildir--eexist-p (err)
378   (eq (car err) 'file-already-exists))
379
380 (defun nnmaildir--new-number (nndir)
381   "Allocate a new article number by atomically creating a file under NNDIR."
382   (let ((numdir (nnmaildir--num-dir nndir))
383         (make-new-file t)
384         (number-open 1)
385         number-link previous-number-link path-open path-link ino-open)
386     (nnmaildir--mkdir numdir)
387     (catch 'return
388       (while t
389         (setq path-open (concat numdir (number-to-string number-open)))
390         (if (not make-new-file)
391             (setq previous-number-link number-link)
392           (nnmaildir--mkfile path-open)
393           ;; If Emacs had O_CREAT|O_EXCL, we could return number-open here.
394           (setq make-new-file nil
395                 previous-number-link 0))
396         (let* ((attr (file-attributes path-open))
397                (nlink (nth 1 attr)))
398           (setq ino-open (nth 10 attr)
399                 number-link (+ number-open nlink))
400           (if (or (< nlink 1) (< number-link nlink))
401               (signal 'error '("Arithmetic overflow"))))
402         (if (= number-link previous-number-link)
403             ;; We've already tried this number, in the previous loop iteration,
404             ;; and failed.
405             (signal 'error `("Corrupt internal nnmaildir data" ,path-open)))
406         (setq path-link (concat numdir (number-to-string number-link)))
407         (nnmaildir--condcase err
408             (progn
409               (add-name-to-file path-open path-link)
410               (throw 'return number-link))
411           (cond
412            ((nnmaildir--emlink-p err)
413             (setq make-new-file t
414                   number-open number-link))
415            ((nnmaildir--eexist-p err)
416             (let ((attr (file-attributes path-link)))
417               (unless (equal (nth 10 attr) ino-open)
418                 (setq number-open number-link
419                       number-link 0))))
420            (t (signal (car err) (cdr err)))))))))
421
422 (defun nnmaildir--update-nov (server group article)
423   (let ((nnheader-file-coding-system 'binary)
424         (srv-dir (nnmaildir--srv-dir server))
425         (storage-version 1) ;; [version article-number msgid [...nov...]]
426         dir gname pgname msgdir prefix suffix file attr mtime novdir novfile
427         nov msgid nov-beg nov-mid nov-end field val old-extra num
428         deactivate-mark)
429     (catch 'return
430       (setq gname (nnmaildir--grp-name group)
431             pgname (nnmaildir--pgname server gname)
432             dir (nnmaildir--srvgrp-dir srv-dir gname)
433             msgdir (if (nnmaildir--param pgname 'read-only)
434                        (nnmaildir--new dir) (nnmaildir--cur dir))
435             prefix (nnmaildir--art-prefix article)
436             suffix (nnmaildir--art-suffix article)
437             file (concat msgdir prefix suffix)
438             attr (file-attributes file))
439       (unless attr
440         (nnmaildir--expired-article group article)
441         (throw 'return nil))
442       (setq mtime (nth 5 attr)
443             attr (nth 7 attr)
444             nov (nnmaildir--art-nov article)
445             dir (nnmaildir--nndir dir)
446             novdir (nnmaildir--nov-dir dir)
447             novfile (concat novdir prefix))
448       (unless (equal nnmaildir--extra nnmail-extra-headers)
449         (setq nnmaildir--extra (copy-sequence nnmail-extra-headers)))
450       (nnmaildir--with-nov-buffer
451         ;; First we'll check for already-parsed NOV data.
452         (cond ((not (file-exists-p novfile))
453                ;; The NOV file doesn't exist; we have to parse the message.
454                (setq nov nil))
455               ((not nov)
456                ;; The file exists, but the data isn't in memory; read the file.
457                (erase-buffer)
458                (nnheader-insert-file-contents novfile)
459                (setq nov (read (current-buffer)))
460                (if (not (and (vectorp nov)
461                              (/= 0 (length nov))
462                              (equal storage-version (aref nov 0))))
463                    ;; This NOV data seems to be in the wrong format.
464                    (setq nov nil)
465                  (unless (nnmaildir--art-num   article)
466                    (setf (nnmaildir--art-num   article) (aref nov 1)))
467                  (unless (nnmaildir--art-msgid article)
468                    (setf (nnmaildir--art-msgid article) (aref nov 2)))
469                  (setq nov (aref nov 3)))))
470         ;; Now check whether the already-parsed data (if we have any) is
471         ;; usable: if the message has been edited or if nnmail-extra-headers
472         ;; has been augmented since this data was parsed from the message,
473         ;; then we have to reparse.  Otherwise it's up-to-date.
474         (when (and nov (equal mtime (nnmaildir--nov-get-mtime nov)))
475           ;; The timestamp matches.  Now check nnmail-extra-headers.
476           (setq old-extra (nnmaildir--nov-get-extra nov))
477           (when (equal nnmaildir--extra old-extra) ;; common case
478             ;; Save memory; use a single copy of the list value.
479             (nnmaildir--nov-set-extra nov nnmaildir--extra)
480             (throw 'return nov))
481           ;; They're not equal, but maybe the new is a subset of the old.
482           (if (null nnmaildir--extra)
483               ;; The empty set is a subset of every set.
484               (throw 'return nov))
485           (if (not (memq nil (mapcar (lambda (e) (memq e old-extra))
486                                      nnmaildir--extra)))
487               (throw 'return nov)))
488         ;; Parse the NOV data out of the message.
489         (erase-buffer)
490         (nnheader-insert-file-contents file)
491         (insert "\n")
492         (goto-char (point-min))
493         (save-restriction
494           (if (search-forward "\n\n" nil 'noerror)
495               (progn
496                 (setq nov-mid (count-lines (point) (point-max)))
497                 (narrow-to-region (point-min) (1- (point))))
498             (setq nov-mid 0))
499           (goto-char (point-min))
500           (delete-char 1)
501           (setq nov (nnheader-parse-naked-head)
502                 field (or (mail-header-lines nov) 0)))
503         (unless (or (zerop field) (nnmaildir--param pgname 'distrust-Lines:))
504           (setq nov-mid field))
505         (setq nov-mid (number-to-string nov-mid)
506               nov-mid (concat (number-to-string attr) "\t" nov-mid))
507         (save-match-data
508           (setq field (or (mail-header-references nov) ""))
509           (nnmaildir--tab-to-space field)
510           (setq nov-mid (concat field "\t" nov-mid)
511                 nov-beg (mapconcat
512                           (lambda (f) (nnmaildir--tab-to-space (or f "")))
513                           (list (mail-header-subject nov)
514                                 (mail-header-from nov)
515                                 (mail-header-date nov)) "\t")
516                 nov-end (mapconcat
517                           (lambda (extra)
518                             (setq field (symbol-name (car extra))
519                                   val (cdr extra))
520                             (nnmaildir--tab-to-space field)
521                             (nnmaildir--tab-to-space val)
522                             (concat field ": " val))
523                           (mail-header-extra nov) "\t")))
524         (setq msgid (mail-header-id nov))
525         (if (or (null msgid) (nnheader-fake-message-id-p msgid))
526             (setq msgid (concat "<" prefix "@nnmaildir>")))
527         (nnmaildir--tab-to-space msgid)
528         ;; The data is parsed; create an nnmaildir NOV structure.
529         (setq nov (nnmaildir--nov-new nov-beg nov-mid nov-end mtime
530                                       nnmaildir--extra)
531               num (nnmaildir--art-num article))
532         (unless num
533           (setq num (nnmaildir--new-number dir))
534           (setf (nnmaildir--art-num article) num))
535         ;; Store this new NOV data in a file
536         (erase-buffer)
537         (prin1 (vector storage-version num msgid nov) (current-buffer))
538         (setq file (concat novfile ":"))
539         (nnmaildir--unlink file)
540         (gmm-write-region (point-min) (point-max) file nil 'no-message nil
541                           'excl))
542       (rename-file file novfile 'replace)
543       (setf (nnmaildir--art-msgid article) msgid)
544       nov)))
545
546 (defun nnmaildir--cache-nov (group article nov)
547   (let ((cache (nnmaildir--grp-cache group))
548         (index (nnmaildir--grp-index group))
549         goner)
550     (unless (nnmaildir--art-nov article)
551       (setq goner (aref cache index))
552       (if goner (setf (nnmaildir--art-nov goner) nil))
553       (aset cache index article)
554       (setf (nnmaildir--grp-index group) (% (1+ index) (length cache))))
555     (setf (nnmaildir--art-nov article) nov)))
556
557 (defun nnmaildir--grp-add-art (server group article)
558   (let ((nov (nnmaildir--update-nov server group article))
559         count num min nlist nlist-cdr insert-nlist)
560     (when nov
561       (setq count (1+ (nnmaildir--grp-count group))
562             num (nnmaildir--art-num article)
563             min (if (= count 1) num
564                   (min num (nnmaildir--grp-min group)))
565             nlist (nnmaildir--grp-nlist group))
566       (if (or (null nlist) (> num (caar nlist)))
567           (setq nlist (cons (cons num article) nlist))
568         (setq insert-nlist t
569               nlist-cdr (cdr nlist))
570         (while (and nlist-cdr (< num (caar nlist-cdr)))
571           (setq nlist nlist-cdr
572                 nlist-cdr (cdr nlist))))
573       (let ((inhibit-quit t))
574         (setf (nnmaildir--grp-count group) count)
575         (setf (nnmaildir--grp-min group) min)
576         (if insert-nlist
577             (setcdr nlist (cons (cons num article) nlist-cdr))
578           (setf (nnmaildir--grp-nlist group) nlist))
579         (set (intern (nnmaildir--art-prefix article)
580                      (nnmaildir--grp-flist group))
581              article)
582         (set (intern (nnmaildir--art-msgid article)
583                      (nnmaildir--grp-mlist group))
584              article)
585         (set (intern (nnmaildir--grp-name group)
586                      (nnmaildir--srv-groups server))
587              group))
588       (nnmaildir--cache-nov group article nov)
589       t)))
590
591 (defun nnmaildir--group-ls (server pgname)
592   (or (nnmaildir--param pgname 'directory-files)
593       (nnmaildir--srv-ls server)))
594
595 (defun nnmaildir-article-number-to-file-name
596   (number group-name server-address-string)
597   (let ((group (nnmaildir--prepare server-address-string group-name))
598         article dir pgname)
599     (catch 'return
600       (unless group
601         ;; The given group or server does not exist.
602         (throw 'return nil))
603       (setq article (nnmaildir--nlist-art group number))
604       (unless article
605         ;; The given article number does not exist in this group.
606         (throw 'return nil))
607       (setq pgname (nnmaildir--pgname nnmaildir--cur-server group-name)
608             dir (nnmaildir--srv-dir nnmaildir--cur-server)
609             dir (nnmaildir--srvgrp-dir dir group-name)
610             dir (if (nnmaildir--param pgname 'read-only)
611                     (nnmaildir--new dir) (nnmaildir--cur dir)))
612       (concat dir (nnmaildir--art-prefix article)
613               (nnmaildir--art-suffix article)))))
614
615 (defun nnmaildir-article-number-to-base-name
616   (number group-name server-address-string)
617   (let ((x (nnmaildir--prepare server-address-string group-name)))
618     (when x
619       (setq x (nnmaildir--nlist-art x number))
620       (and x (cons (nnmaildir--art-prefix x)
621                    (nnmaildir--art-suffix x))))))
622
623 (defun nnmaildir-base-name-to-article-number
624   (base-name group-name server-address-string)
625   (let ((x (nnmaildir--prepare server-address-string group-name)))
626     (when x
627       (setq x (nnmaildir--grp-flist x)
628             x (nnmaildir--flist-art x base-name))
629       (and x (nnmaildir--art-num x)))))
630
631 (defun nnmaildir--nlist-iterate (nlist ranges func)
632   (let (entry high low nlist2)
633     (if (eq ranges 'all)
634         (setq ranges `((1 . ,(caar nlist)))))
635     (while ranges
636       (setq entry (car ranges) ranges (cdr ranges))
637       (while (and ranges (eq entry (car ranges)))
638         (setq ranges (cdr ranges))) ;; skip duplicates
639       (if (numberp entry)
640           (setq low entry
641                 high entry)
642         (setq low (car entry)
643               high (cdr entry)))
644       (setq nlist2 nlist) ;; Don't assume any sorting of ranges
645       (catch 'iterate-loop
646         (while nlist2
647           (if (<= (caar nlist2) high) (throw 'iterate-loop nil))
648           (setq nlist2 (cdr nlist2))))
649       (catch 'iterate-loop
650         (while nlist2
651           (setq entry (car nlist2) nlist2 (cdr nlist2))
652           (if (< (car entry) low) (throw 'iterate-loop nil))
653           (funcall func (cdr entry)))))))
654
655 (defun nnmaildir--up2-1 (n)
656   (if (zerop n) 1 (1- (lsh 1 (1+ (logb n))))))
657
658 (defun nnmaildir--system-name ()
659   (gnus-replace-in-string
660    (gnus-replace-in-string
661     (gnus-replace-in-string
662      (system-name)
663      "\\\\" "\\134" 'literal)
664     "/" "\\057" 'literal)
665    ":" "\\072" 'literal))
666
667 (defun nnmaildir-request-type (_group &optional _article)
668   'mail)
669
670 (defun nnmaildir-status-message (&optional server)
671   (nnmaildir--prepare server nil)
672   (nnmaildir--srv-error nnmaildir--cur-server))
673
674 (defun nnmaildir-server-opened (&optional server)
675   (and nnmaildir--cur-server
676        (if server
677            (string-equal server (nnmaildir--srv-address nnmaildir--cur-server))
678          t)
679        (nnmaildir--srv-groups nnmaildir--cur-server)
680        t))
681
682 (defun nnmaildir-open-server (server &optional defs)
683   (let ((x server)
684         dir size)
685     (catch 'return
686       (setq server (intern-soft x nnmaildir--servers))
687       (if server
688           (and (setq server (symbol-value server))
689                (nnmaildir--srv-groups server)
690                (setq nnmaildir--cur-server server)
691                (throw 'return t))
692         (setq server (make-nnmaildir--srv :address x))
693         (let ((inhibit-quit t))
694           (set (intern x nnmaildir--servers) server)))
695       (setq dir (assq 'directory defs))
696       (unless dir
697         (setf (nnmaildir--srv-error server)
698               "You must set \"directory\" in the select method")
699         (throw 'return nil))
700       (setq dir (cadr dir)
701             dir (eval dir)
702             dir (expand-file-name dir)
703             dir (file-name-as-directory dir))
704       (unless (file-exists-p dir)
705         (setf (nnmaildir--srv-error server) (concat "No such directory: " dir))
706         (throw 'return nil))
707       (setf (nnmaildir--srv-dir server) dir)
708       (setq x (assq 'directory-files defs))
709       (if (null x)
710           (setq x (if nnheader-directory-files-is-safe 'directory-files
711                     'nnheader-directory-files-safe))
712         (setq x (cadr x))
713         (unless (functionp x)
714           (setf (nnmaildir--srv-error server)
715                 (concat "Not a function: " (prin1-to-string x)))
716           (throw 'return nil)))
717       (setf (nnmaildir--srv-ls server) x)
718       (setq size (length (funcall x dir nil "\\`[^.]" 'nosort))
719             size (nnmaildir--up2-1 size))
720       (and (setq x (assq 'get-new-mail defs))
721            (setq x (cdr x))
722            (car x)
723            (setf (nnmaildir--srv-gnm server) t)
724            (require 'nnmail))
725       (setq x (assq 'target-prefix defs))
726       (if x
727           (progn
728             (setq x (cadr x)
729                   x (eval x))
730             (setf (nnmaildir--srv-target-prefix server) x))
731         (setq x (assq 'create-directory defs))
732         (if x
733             (progn
734               (setq x (cadr x)
735                     x (eval x)
736                     x (file-name-as-directory x))
737               (setf (nnmaildir--srv-target-prefix server) x))
738           (setf (nnmaildir--srv-target-prefix server) "")))
739       (setf (nnmaildir--srv-groups server) (make-vector size 0))
740       (setq nnmaildir--cur-server server)
741       t)))
742
743 (defun nnmaildir--parse-filename (file)
744   (let ((prefix (car file))
745         timestamp len)
746     (if (string-match "\\`\\([0-9]+\\)\\(\\..*\\)\\'" prefix)
747         (progn
748           (setq timestamp (concat "0000" (match-string 1 prefix))
749                 len (- (length timestamp) 4))
750           (vector (string-to-number (substring timestamp 0 len))
751                   (string-to-number (substring timestamp len))
752                   (match-string 2 prefix)
753                   file))
754       file)))
755
756 (defun nnmaildir--sort-files (a b)
757   (catch 'return
758     (if (consp a)
759         (throw 'return (and (consp b) (string-lessp (car a) (car b)))))
760     (if (consp b) (throw 'return t))
761     (if (< (aref a 0) (aref b 0)) (throw 'return t))
762     (if (> (aref a 0) (aref b 0)) (throw 'return nil))
763     (if (< (aref a 1) (aref b 1)) (throw 'return t))
764     (if (> (aref a 1) (aref b 1)) (throw 'return nil))
765     (string-lessp (aref a 2) (aref b 2))))
766
767 (defun nnmaildir--scan (gname scan-msgs groups _method srv-dir srv-ls)
768   (catch 'return
769     (let ((36h-ago (- (car (current-time)) 2))
770           absdir nndir tdir ndir cdir nattr cattr isnew pgname read-only ls
771           files num dir flist group x)
772       (setq absdir (nnmaildir--srvgrp-dir srv-dir gname)
773             nndir (nnmaildir--nndir absdir))
774       (unless (file-exists-p absdir)
775         (setf (nnmaildir--srv-error nnmaildir--cur-server)
776               (concat "No such directory: " absdir))
777         (throw 'return nil))
778       (setq tdir (nnmaildir--tmp absdir)
779             ndir (nnmaildir--new absdir)
780             cdir (nnmaildir--cur absdir)
781             nattr (file-attributes ndir)
782             cattr (file-attributes cdir))
783       (unless (and (file-exists-p tdir) nattr cattr)
784         (setf (nnmaildir--srv-error nnmaildir--cur-server)
785               (concat "Not a maildir: " absdir))
786         (throw 'return nil))
787       (setq group (nnmaildir--prepare nil gname)
788             pgname (nnmaildir--pgname nnmaildir--cur-server gname))
789       (if group
790           (setq isnew nil)
791         (setq isnew t
792               group (make-nnmaildir--grp :name gname :index 0))
793         (nnmaildir--mkdir nndir)
794         (nnmaildir--mkdir (nnmaildir--nov-dir   nndir))
795         (nnmaildir--mkdir (nnmaildir--marks-dir nndir)))
796       (setq read-only (nnmaildir--param pgname 'read-only)
797             ls (or (nnmaildir--param pgname 'directory-files) srv-ls))
798       (unless read-only
799         (setq x (nth 11 (file-attributes tdir)))
800         (unless (and (equal x (nth 11 nattr)) (equal x (nth 11 cattr)))
801           (setf (nnmaildir--srv-error nnmaildir--cur-server)
802                 (concat "Maildir spans filesystems: " absdir))
803           (throw 'return nil))
804         (dolist (file (funcall ls tdir 'full "\\`[^.]" 'nosort))
805           (setq x (file-attributes file))
806           (if (or (> (cadr x) 1) (< (car (nth 4 x)) 36h-ago))
807               (delete-file file))))
808       (or scan-msgs
809           isnew
810           (throw 'return t))
811       (setq nattr (nth 5 nattr))
812       (if (equal nattr (nnmaildir--grp-new group))
813           (setq nattr nil))
814       (if read-only (setq dir (and (or isnew nattr) ndir))
815         (when (or isnew nattr)
816           (dolist (file  (funcall ls ndir nil "\\`[^.]" 'nosort))
817             (setq x (concat ndir file))
818             (and (time-less-p (nth 5 (file-attributes x)) (current-time))
819                  (rename-file x (concat cdir (nnmaildir--ensure-suffix file)))))
820           (setf (nnmaildir--grp-new group) nattr))
821         (setq cattr (nth 5 (file-attributes cdir)))
822         (if (equal cattr (nnmaildir--grp-cur group))
823             (setq cattr nil))
824         (setq dir (and (or isnew cattr) cdir)))
825       (unless dir (throw 'return t))
826       (setq files (funcall ls dir nil "\\`[^.]" 'nosort)
827             files (save-match-data
828                     (mapcar
829                      (lambda (f)
830                        (string-match "\\`\\([^:]*\\)\\(\\(:.*\\)?\\)\\'" f)
831                        (cons (match-string 1 f) (match-string 2 f)))
832                      files)))
833       (when isnew
834         (setq num (nnmaildir--up2-1 (length files)))
835         (setf (nnmaildir--grp-flist group) (make-vector num 0))
836         (setf (nnmaildir--grp-mlist group) (make-vector num 0))
837         (setf (nnmaildir--grp-mmth group) (make-vector 1 0))
838         (setq num (nnmaildir--param pgname 'nov-cache-size))
839         (if (numberp num) (if (< num 1) (setq num 1))
840           (setq num 16
841                 cdir (nnmaildir--marks-dir nndir)
842                 ndir (nnmaildir--subdir cdir "tick")
843                 cdir (nnmaildir--subdir cdir "read"))
844           (dolist (prefix-suffix files)
845             (let ((prefix (car prefix-suffix))
846                   (suffix (cdr prefix-suffix)))
847               ;; increase num for each unread or ticked article
848               (when (or
849                      ;; first look for marks in suffix, if it's valid...
850                      (when (and (stringp suffix)
851                                 (gnus-string-prefix-p ":2," suffix))
852                        (or
853                         (not (gnus-string-match-p
854                               (string (nnmaildir--mark-to-flag 'read)) suffix))
855                         (gnus-string-match-p
856                          (string (nnmaildir--mark-to-flag 'tick)) suffix)))
857                      ;; then look in marks directories
858                      (not (file-exists-p (concat cdir prefix)))
859                      (file-exists-p (concat ndir prefix)))
860                 (incf num)))))
861         (setf (nnmaildir--grp-cache group) (make-vector num nil))
862         (let ((inhibit-quit t))
863           (set (intern gname groups) group))
864         (or scan-msgs (throw 'return t)))
865       (setq flist (nnmaildir--grp-flist group)
866             files (mapcar
867                    (lambda (file)
868                      (and (null (nnmaildir--flist-art flist (car file)))
869                           file))
870                    files)
871             files (delq nil files)
872             files (mapcar 'nnmaildir--parse-filename files)
873             files (sort files 'nnmaildir--sort-files))
874       (dolist (file files)
875         (setq file (if (consp file) file (aref file 3))
876               x (make-nnmaildir--art :prefix (car file) :suffix (cdr file)))
877         (nnmaildir--grp-add-art nnmaildir--cur-server group x))
878       (if read-only (setf (nnmaildir--grp-new group) nattr)
879         (setf (nnmaildir--grp-cur group) cattr)))
880     t))
881
882 (defvar nnmaildir-get-new-mail)
883 (defvar nnmaildir-group-alist)
884 (defvar nnmaildir-active-file)
885
886 (defun nnmaildir-request-scan (&optional scan-group server)
887   (let ((coding-system-for-write nnheader-file-coding-system)
888         (buffer-file-coding-system nil)
889         (file-coding-system-alist nil)
890         (nnmaildir-get-new-mail t)
891         (nnmaildir-group-alist nil)
892         (nnmaildir-active-file nil)
893         x srv-ls srv-dir method groups target-prefix dirs seen
894         deactivate-mark)
895     (nnmaildir--prepare server nil)
896     (setq srv-ls (nnmaildir--srv-ls nnmaildir--cur-server)
897           srv-dir (nnmaildir--srv-dir nnmaildir--cur-server)
898           method (nnmaildir--srv-method nnmaildir--cur-server)
899           groups (nnmaildir--srv-groups nnmaildir--cur-server)
900           target-prefix (nnmaildir--srv-target-prefix nnmaildir--cur-server))
901     (nnmaildir--with-work-buffer
902       (save-match-data
903         (if (stringp scan-group)
904             (if (nnmaildir--scan scan-group t groups method srv-dir srv-ls)
905                 (if (nnmaildir--srv-gnm nnmaildir--cur-server)
906                     (nnmail-get-new-mail 'nnmaildir nil nil scan-group))
907               (unintern scan-group groups))
908           (setq x (nth 5 (file-attributes srv-dir))
909                 scan-group (null scan-group))
910           (if (equal x (nnmaildir--srv-mtime nnmaildir--cur-server))
911               (if scan-group
912                   (mapatoms (lambda (sym)
913                               (nnmaildir--scan (symbol-name sym) t groups
914                                                method srv-dir srv-ls))
915                             groups))
916             (setq dirs (funcall srv-ls srv-dir nil "\\`[^.]" 'nosort)
917                   dirs (if (zerop (length target-prefix))
918                            dirs
919                          (gnus-remove-if
920                           (lambda (dir)
921                             (and (>= (length dir) (length target-prefix))
922                                  (string= (substring dir 0
923                                                      (length target-prefix))
924                                           target-prefix)))
925                           dirs))
926                   seen (nnmaildir--up2-1 (length dirs))
927                   seen (make-vector seen 0))
928             (dolist (grp-dir dirs)
929               (if (nnmaildir--scan grp-dir scan-group groups method srv-dir
930                                    srv-ls)
931                   (intern grp-dir seen)))
932             (setq x nil)
933             (mapatoms (lambda (group)
934                         (setq group (symbol-name group))
935                         (unless (intern-soft group seen)
936                           (setq x (cons group x))))
937                       groups)
938             (dolist (grp x)
939               (unintern grp groups))
940             (setf (nnmaildir--srv-mtime nnmaildir--cur-server)
941                   (nth 5 (file-attributes srv-dir))))
942           (and scan-group
943                (nnmaildir--srv-gnm nnmaildir--cur-server)
944                (nnmail-get-new-mail 'nnmaildir nil nil))))))
945   t)
946
947 (defun nnmaildir-request-list (&optional server)
948   (nnmaildir-request-scan 'find-new-groups server)
949   (let (pgname ro deactivate-mark)
950     (nnmaildir--prepare server nil)
951     (nnmaildir--with-nntp-buffer
952       (erase-buffer)
953       (mapatoms (lambda (group)
954                   (setq pgname (symbol-name group)
955                         pgname (nnmaildir--pgname nnmaildir--cur-server pgname)
956                         group (symbol-value group)
957                         ro (nnmaildir--param pgname 'read-only))
958                   (insert (gnus-replace-in-string
959                            (nnmaildir--grp-name group) " " "\\ " t)
960                           " ")
961                   (princ (nnmaildir--group-maxnum nnmaildir--cur-server group)
962                          nntp-server-buffer)
963                   (insert " ")
964                   (princ (nnmaildir--grp-min group) nntp-server-buffer)
965                   (insert " " (if ro "n" "y") "\n"))
966                 (nnmaildir--srv-groups nnmaildir--cur-server))))
967   t)
968
969 (defun nnmaildir-request-newgroups (_date &optional server)
970   (nnmaildir-request-list server))
971
972 (defun nnmaildir-retrieve-groups (groups &optional server)
973   (let (group deactivate-mark)
974     (nnmaildir--prepare server nil)
975     (nnmaildir--with-nntp-buffer
976       (erase-buffer)
977       (dolist (gname groups)
978         (setq group (nnmaildir--prepare nil gname))
979         (if (null group) (insert "411 no such news group\n")
980           (insert "211 ")
981           (princ (nnmaildir--grp-count group) nntp-server-buffer)
982           (insert " ")
983           (princ (nnmaildir--grp-min   group) nntp-server-buffer)
984           (insert " ")
985           (princ (nnmaildir--group-maxnum nnmaildir--cur-server group)
986                  nntp-server-buffer)
987           (insert " "
988                   (gnus-replace-in-string gname " " "\\ " t)
989                   "\n")))))
990   'group)
991
992 (defun nnmaildir-request-update-info (gname info &optional server)
993   (let* ((group (nnmaildir--prepare server gname))
994          (curdir (nnmaildir--cur
995                   (nnmaildir--srvgrp-dir
996                    (nnmaildir--srv-dir nnmaildir--cur-server) gname)))
997          (curdir-mtime (nth 5 (file-attributes curdir)))
998          pgname flist always-marks never-marks old-marks dir
999          all-marks marks ranges markdir read ls
1000          old-mmth new-mmth mtime existing missing deactivate-mark)
1001     (catch 'return
1002       (unless group
1003         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1004               (concat "No such group: " gname))
1005         (throw 'return nil))
1006       (setq gname (nnmaildir--grp-name group)
1007             pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1008             flist (nnmaildir--grp-flist group))
1009       (when (zerop (nnmaildir--grp-count group))
1010         (gnus-info-set-read info nil)
1011         (gnus-info-set-marks info nil 'extend)
1012         (throw 'return info))
1013       (setq old-marks (cons 'read (gnus-info-read info))
1014             old-marks (cons old-marks (gnus-info-marks info))
1015             always-marks (nnmaildir--param pgname 'always-marks)
1016             never-marks (nnmaildir--param pgname 'never-marks)
1017             existing (nnmaildir--grp-nlist group)
1018             existing (mapcar 'car existing)
1019             existing (nreverse existing)
1020             existing (gnus-compress-sequence existing 'always-list)
1021             missing (list (cons 1 (nnmaildir--group-maxnum
1022                                    nnmaildir--cur-server group)))
1023             missing (gnus-range-difference missing existing)
1024             dir (nnmaildir--srv-dir nnmaildir--cur-server)
1025             dir (nnmaildir--srvgrp-dir dir gname)
1026             dir (nnmaildir--nndir dir)
1027             dir (nnmaildir--marks-dir dir)
1028             ls (nnmaildir--group-ls nnmaildir--cur-server pgname)
1029             all-marks (gnus-delete-duplicates
1030                        ;; get mark names from mark dirs and from flag
1031                        ;; mappings
1032                        (append
1033                         (mapcar 'cdr nnmaildir-flag-mark-mapping)
1034                         (mapcar 'intern (funcall ls dir nil "\\`[^.]" 'nosort))))
1035             new-mmth (nnmaildir--up2-1 (length all-marks))
1036             new-mmth (make-vector new-mmth 0)
1037             old-mmth (nnmaildir--grp-mmth group))
1038       (dolist (mark all-marks)
1039         (setq markdir (nnmaildir--subdir dir (symbol-name mark))
1040               ranges nil)
1041         (catch 'got-ranges
1042           (if (memq mark never-marks) (throw 'got-ranges nil))
1043           (when (memq mark always-marks)
1044             (setq ranges existing)
1045             (throw 'got-ranges nil))
1046           ;; Find the mtime for this mark.  If this mark can be expressed as
1047           ;; a filename flag, get the later of the mtimes for markdir and
1048           ;; curdir, otherwise only the markdir counts.
1049           (setq mtime
1050                 (let ((markdir-mtime (nth 5 (file-attributes markdir))))
1051                   (cond
1052                    ((null (nnmaildir--mark-to-flag mark))
1053                     markdir-mtime)
1054                    ((null markdir-mtime)
1055                     curdir-mtime)
1056                    ((null curdir-mtime)
1057                     ;; this should never happen...
1058                     markdir-mtime)
1059                    ((time-less-p markdir-mtime curdir-mtime)
1060                     curdir-mtime)
1061                    (t
1062                     markdir-mtime))))
1063           (set (intern (symbol-name mark) new-mmth) mtime)
1064           (when (equal mtime (symbol-value (intern-soft (symbol-name mark) old-mmth)))
1065             (setq ranges (assq mark old-marks))
1066             (if ranges (setq ranges (cdr ranges)))
1067             (throw 'got-ranges nil))
1068           (let ((article-list nil))
1069             ;; Consider the article marked if it either has the flag in the
1070             ;; filename, or is in the markdir.  As you'd rarely remove a
1071             ;; flag/mark, this should avoid losing information in the most
1072             ;; common usage pattern.
1073             (or
1074              (let ((flag (nnmaildir--mark-to-flag mark)))
1075                ;; If this mark has a corresponding maildir flag...
1076                (when flag
1077                  (let ((regexp
1078                         (concat "\\`[^.].*:2,[A-Z]*" (string flag))))
1079                    ;; ...then find all files with that flag.
1080                    (dolist (filename (funcall ls curdir nil regexp 'nosort))
1081                      (let* ((prefix (car (split-string filename ":2,")))
1082                             (article (nnmaildir--flist-art flist prefix)))
1083                        (when article
1084                          (push (nnmaildir--art-num article) article-list)))))))
1085              ;; Also check Gnus-specific mark directory, if it exists.
1086              (when (file-directory-p markdir)
1087                (dolist (prefix (funcall ls markdir nil "\\`[^.]" 'nosort))
1088                  (let ((article (nnmaildir--flist-art flist prefix)))
1089                    (when article
1090                      (push (nnmaildir--art-num article) article-list))))))
1091             (setq ranges (gnus-add-to-range ranges (sort article-list '<)))))
1092         (if (eq mark 'read) (setq read ranges)
1093           (if ranges (setq marks (cons (cons mark ranges) marks)))))
1094       (gnus-info-set-read info (gnus-range-add read missing))
1095       (gnus-info-set-marks info marks 'extend)
1096       (setf (nnmaildir--grp-mmth group) new-mmth)
1097       info)))
1098
1099 (defun nnmaildir-request-group (gname &optional server fast _info)
1100   (let ((group (nnmaildir--prepare server gname))
1101         deactivate-mark)
1102     (catch 'return
1103       (unless group
1104         ;; (insert "411 no such news group\n")
1105         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1106               (concat "No such group: " gname))
1107         (throw 'return nil))
1108       (setf (nnmaildir--srv-curgrp nnmaildir--cur-server) group)
1109       (if fast (throw 'return t))
1110       (nnmaildir--with-nntp-buffer
1111         (erase-buffer)
1112         (insert "211 ")
1113         (princ (nnmaildir--grp-count group) nntp-server-buffer)
1114         (insert " ")
1115         (princ (nnmaildir--grp-min   group) nntp-server-buffer)
1116         (insert " ")
1117         (princ (nnmaildir--group-maxnum nnmaildir--cur-server group)
1118                nntp-server-buffer)
1119         (insert " " (gnus-replace-in-string gname " " "\\ " t) "\n")
1120         t))))
1121
1122 (defun nnmaildir-request-create-group (gname &optional server _args)
1123   (nnmaildir--prepare server nil)
1124   (catch 'return
1125     (let ((target-prefix (nnmaildir--srv-target-prefix nnmaildir--cur-server))
1126           srv-dir dir groups)
1127       (when (zerop (length gname))
1128         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1129               "Invalid (empty) group name")
1130         (throw 'return nil))
1131       (when (eq (aref "." 0) (aref gname 0))
1132         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1133               "Group names may not start with \".\"")
1134         (throw 'return nil))
1135       (when (save-match-data (string-match "[\0/\t]" gname))
1136         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1137               (concat "Invalid characters (null, tab, or /) in group name: "
1138                       gname))
1139         (throw 'return nil))
1140       (setq groups (nnmaildir--srv-groups nnmaildir--cur-server))
1141       (when (intern-soft gname groups)
1142         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1143               (concat "Group already exists: " gname))
1144         (throw 'return nil))
1145       (setq srv-dir (nnmaildir--srv-dir nnmaildir--cur-server))
1146       (if (file-name-absolute-p target-prefix)
1147           (setq dir (expand-file-name target-prefix))
1148         (setq dir srv-dir
1149               dir (file-truename dir)
1150               dir (concat dir target-prefix)))
1151       (setq dir (nnmaildir--subdir dir gname))
1152       (nnmaildir--mkdir dir)
1153       (nnmaildir--mkdir (nnmaildir--tmp dir))
1154       (nnmaildir--mkdir (nnmaildir--new dir))
1155       (nnmaildir--mkdir (nnmaildir--cur dir))
1156       (unless (string= target-prefix "")
1157         (make-symbolic-link (concat target-prefix gname)
1158                             (concat srv-dir gname)))
1159       (nnmaildir-request-scan 'find-new-groups))))
1160
1161 (defun nnmaildir-request-rename-group (gname new-name &optional server)
1162   (let ((group (nnmaildir--prepare server gname))
1163         (coding-system-for-write nnheader-file-coding-system)
1164         (buffer-file-coding-system nil)
1165         (file-coding-system-alist nil)
1166         srv-dir x groups)
1167     (catch 'return
1168       (unless group
1169         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1170               (concat "No such group: " gname))
1171         (throw 'return nil))
1172       (when (zerop (length new-name))
1173         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1174               "Invalid (empty) group name")
1175         (throw 'return nil))
1176       (when (eq (aref "." 0) (aref new-name 0))
1177         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1178               "Group names may not start with \".\"")
1179         (throw 'return nil))
1180       (when (save-match-data (string-match "[\0/\t]" new-name))
1181         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1182               (concat "Invalid characters (null, tab, or /) in group name: "
1183                       new-name))
1184         (throw 'return nil))
1185       (if (string-equal gname new-name) (throw 'return t))
1186       (when (intern-soft new-name
1187                          (nnmaildir--srv-groups nnmaildir--cur-server))
1188         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1189               (concat "Group already exists: " new-name))
1190         (throw 'return nil))
1191       (setq srv-dir (nnmaildir--srv-dir nnmaildir--cur-server))
1192       (condition-case err
1193           (rename-file (concat srv-dir gname)
1194                        (concat srv-dir new-name))
1195         (error
1196          (setf (nnmaildir--srv-error nnmaildir--cur-server)
1197                (concat "Error renaming link: " (prin1-to-string err)))
1198          (throw 'return nil)))
1199       (setq x (nnmaildir--srv-groups nnmaildir--cur-server)
1200             groups (make-vector (length x) 0))
1201       (mapatoms (lambda (sym)
1202                   (unless (eq (symbol-value sym) group)
1203                     (set (intern (symbol-name sym) groups)
1204                          (symbol-value sym))))
1205                 x)
1206       (setq group (copy-sequence group))
1207       (setf (nnmaildir--grp-name group) new-name)
1208       (set (intern new-name groups) group)
1209       (setf (nnmaildir--srv-groups nnmaildir--cur-server) groups)
1210       t)))
1211
1212 (defun nnmaildir-request-delete-group (gname force &optional server)
1213   (let ((group (nnmaildir--prepare server gname))
1214         pgname grp-dir target dir ls deactivate-mark)
1215     (catch 'return
1216       (unless group
1217         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1218               (concat "No such group: " gname))
1219         (throw 'return nil))
1220       (setq gname (nnmaildir--grp-name group)
1221             pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1222             grp-dir (nnmaildir--srv-dir nnmaildir--cur-server)
1223             target (car (file-attributes (concat grp-dir gname)))
1224             grp-dir (nnmaildir--srvgrp-dir grp-dir gname))
1225       (unless (or force (stringp target))
1226         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1227               (concat "Not a symlink: " gname))
1228         (throw 'return nil))
1229       (if (eq group (nnmaildir--srv-curgrp nnmaildir--cur-server))
1230           (setf (nnmaildir--srv-curgrp nnmaildir--cur-server) nil))
1231       (unintern gname (nnmaildir--srv-groups nnmaildir--cur-server))
1232       (if (not force)
1233           (progn
1234             (setq grp-dir (directory-file-name grp-dir))
1235             (nnmaildir--unlink grp-dir))
1236         (setq ls (nnmaildir--group-ls nnmaildir--cur-server pgname))
1237         (if (nnmaildir--param pgname 'read-only)
1238             (progn (delete-directory  (nnmaildir--tmp grp-dir))
1239                    (nnmaildir--unlink (nnmaildir--new grp-dir))
1240                    (delete-directory  (nnmaildir--cur grp-dir)))
1241           (nnmaildir--delete-dir-files (nnmaildir--tmp grp-dir) ls)
1242           (nnmaildir--delete-dir-files (nnmaildir--new grp-dir) ls)
1243           (nnmaildir--delete-dir-files (nnmaildir--cur grp-dir) ls))
1244         (setq dir (nnmaildir--nndir grp-dir))
1245         (dolist (subdir `(,(nnmaildir--nov-dir dir) ,(nnmaildir--num-dir dir)
1246                           ,@(funcall ls (nnmaildir--marks-dir dir)
1247                                      'full "\\`[^.]" 'nosort)))
1248           (nnmaildir--delete-dir-files subdir ls))
1249         (setq dir (nnmaildir--nndir grp-dir))
1250         (nnmaildir--unlink (concat dir "markfile"))
1251         (nnmaildir--unlink (concat dir "markfile{new}"))
1252         (delete-directory (nnmaildir--marks-dir dir))
1253         (delete-directory dir)
1254         (if (not (stringp target))
1255             (delete-directory grp-dir)
1256           (setq grp-dir (directory-file-name grp-dir)
1257                 dir target)
1258           (unless (eq (aref "/" 0) (aref dir 0))
1259             (setq dir (concat (file-truename
1260                                (nnmaildir--srv-dir nnmaildir--cur-server))
1261                               dir)))
1262           (delete-directory dir)
1263           (nnmaildir--unlink grp-dir)))
1264       t)))
1265
1266 (defun nnmaildir-retrieve-headers (articles &optional gname server fetch-old)
1267   (let ((group (nnmaildir--prepare server gname))
1268         nlist mlist article num start stop nov insert-nov
1269         deactivate-mark)
1270     (setq insert-nov
1271           (lambda (article)
1272             (setq nov (nnmaildir--update-nov nnmaildir--cur-server group
1273                                              article))
1274             (when nov
1275               (nnmaildir--cache-nov group article nov)
1276               (setq num (nnmaildir--art-num article))
1277               (princ num nntp-server-buffer)
1278               (insert "\t" (nnmaildir--nov-get-beg nov) "\t"
1279                       (nnmaildir--art-msgid article) "\t"
1280                       (nnmaildir--nov-get-mid nov) "\tXref: nnmaildir "
1281                       (gnus-replace-in-string gname " " "\\ " t) ":")
1282               (princ num nntp-server-buffer)
1283               (insert "\t" (nnmaildir--nov-get-end nov) "\n"))))
1284     (catch 'return
1285       (unless group
1286         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1287               (if gname (concat "No such group: " gname) "No current group"))
1288         (throw 'return nil))
1289       (nnmaildir--with-nntp-buffer
1290         (erase-buffer)
1291         (setq mlist (nnmaildir--grp-mlist group)
1292               nlist (nnmaildir--grp-nlist group)
1293               gname (nnmaildir--grp-name group))
1294         (cond
1295          ((null nlist))
1296          ((and fetch-old (not (numberp fetch-old)))
1297           (nnmaildir--nlist-iterate nlist 'all insert-nov))
1298          ((null articles))
1299          ((stringp (car articles))
1300           (dolist (msgid articles)
1301             (setq article (nnmaildir--mlist-art mlist msgid))
1302             (if article (funcall insert-nov article))))
1303          (t
1304           (if fetch-old
1305               ;; Assume the article range list is sorted ascending
1306               (setq stop (car articles)
1307                     start (car (last articles))
1308                     stop  (if (numberp stop)  stop  (car stop))
1309                     start (if (numberp start) start (cdr start))
1310                     stop (- stop fetch-old)
1311                     stop (if (< stop 1) 1 stop)
1312                     articles (list (cons stop start))))
1313           (nnmaildir--nlist-iterate nlist articles insert-nov)))
1314         (sort-numeric-fields 1 (point-min) (point-max))
1315         'nov))))
1316
1317 (defun nnmaildir-request-article (num-msgid &optional gname server to-buffer)
1318   (let ((group (nnmaildir--prepare server gname))
1319         (case-fold-search t)
1320         list article dir pgname deactivate-mark)
1321     (catch 'return
1322       (unless group
1323         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1324               (if gname (concat "No such group: " gname) "No current group"))
1325         (throw 'return nil))
1326       (if (numberp num-msgid)
1327           (setq article (nnmaildir--nlist-art group num-msgid))
1328         (setq list (nnmaildir--grp-mlist group)
1329               article (nnmaildir--mlist-art list num-msgid))
1330         (if article (setq num-msgid (nnmaildir--art-num article))
1331           (catch 'found
1332             (mapatoms
1333               (lambda (group-sym)
1334                 (setq group (symbol-value group-sym)
1335                       list (nnmaildir--grp-mlist group)
1336                       article (nnmaildir--mlist-art list num-msgid))
1337                 (when article
1338                   (setq num-msgid (nnmaildir--art-num article))
1339                   (throw 'found nil)))
1340               (nnmaildir--srv-groups nnmaildir--cur-server))))
1341         (unless article
1342           (setf (nnmaildir--srv-error nnmaildir--cur-server) "No such article")
1343           (throw 'return nil)))
1344       (setq gname (nnmaildir--grp-name group)
1345             pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1346             dir (nnmaildir--srv-dir nnmaildir--cur-server)
1347             dir (nnmaildir--srvgrp-dir dir gname)
1348             dir (if (nnmaildir--param pgname 'read-only)
1349                     (nnmaildir--new dir) (nnmaildir--cur dir))
1350             nnmaildir-article-file-name
1351             (concat dir
1352                     (nnmaildir--art-prefix article)
1353                     (nnmaildir--art-suffix article)))
1354       (unless (file-exists-p nnmaildir-article-file-name)
1355         (nnmaildir--expired-article group article)
1356         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1357               "Article has expired")
1358         (throw 'return nil))
1359       (with-current-buffer (or to-buffer nntp-server-buffer)
1360         (erase-buffer)
1361         (nnheader-insert-file-contents nnmaildir-article-file-name))
1362       (cons gname num-msgid))))
1363
1364 (defun nnmaildir-request-post (&optional _server)
1365   (let (message-required-mail-headers)
1366     (funcall message-send-mail-function)))
1367
1368 (defun nnmaildir-request-replace-article (number gname buffer)
1369   (let ((group (nnmaildir--prepare nil gname))
1370         (coding-system-for-write nnheader-file-coding-system)
1371         (buffer-file-coding-system nil)
1372         (file-coding-system-alist nil)
1373         dir file article suffix tmpfile deactivate-mark)
1374     (catch 'return
1375       (unless group
1376         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1377               (concat "No such group: " gname))
1378         (throw 'return nil))
1379       (when (nnmaildir--param (nnmaildir--pgname nnmaildir--cur-server gname)
1380                               'read-only)
1381         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1382               (concat "Read-only group: " group))
1383         (throw 'return nil))
1384       (setq dir (nnmaildir--srv-dir nnmaildir--cur-server)
1385             dir (nnmaildir--srvgrp-dir dir gname)
1386             article (nnmaildir--nlist-art group number))
1387       (unless article
1388         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1389               (concat "No such article: " (number-to-string number)))
1390         (throw 'return nil))
1391       (setq suffix (nnmaildir--art-suffix article)
1392             file (nnmaildir--art-prefix article)
1393             tmpfile (concat (nnmaildir--tmp dir) file))
1394       (when (file-exists-p tmpfile)
1395         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1396               (concat "File exists: " tmpfile))
1397         (throw 'return nil))
1398       (with-current-buffer buffer
1399         (gmm-write-region (point-min) (point-max) tmpfile nil 'no-message nil
1400                           'excl))
1401       (unix-sync) ;; no fsync :(
1402       (rename-file tmpfile (concat (nnmaildir--cur dir) file suffix) 'replace)
1403       t)))
1404
1405 (defun nnmaildir-request-move-article (article gname server accept-form
1406                                        &optional _last _move-is-internal)
1407   (let ((group (nnmaildir--prepare server gname))
1408         pgname suffix result nnmaildir--file deactivate-mark)
1409     (catch 'return
1410       (unless group
1411         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1412               (concat "No such group: " gname))
1413         (throw 'return nil))
1414       (setq gname (nnmaildir--grp-name group)
1415             pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1416             article (nnmaildir--nlist-art group article))
1417       (unless article
1418         (setf (nnmaildir--srv-error nnmaildir--cur-server) "No such article")
1419         (throw 'return nil))
1420       (setq suffix (nnmaildir--art-suffix article)
1421             nnmaildir--file (nnmaildir--srv-dir nnmaildir--cur-server)
1422             nnmaildir--file (nnmaildir--srvgrp-dir nnmaildir--file gname)
1423             nnmaildir--file (if (nnmaildir--param pgname 'read-only)
1424                                 (nnmaildir--new nnmaildir--file)
1425                               (nnmaildir--cur nnmaildir--file))
1426             nnmaildir--file (concat nnmaildir--file
1427                                     (nnmaildir--art-prefix article)
1428                                     suffix))
1429       (unless (file-exists-p nnmaildir--file)
1430         (nnmaildir--expired-article group article)
1431         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1432               "Article has expired")
1433         (throw 'return nil))
1434       (nnmaildir--with-move-buffer
1435         (erase-buffer)
1436         (nnheader-insert-file-contents nnmaildir--file)
1437         (setq result (eval accept-form)))
1438       (unless (or (null result) (nnmaildir--param pgname 'read-only))
1439         (nnmaildir--unlink nnmaildir--file)
1440         (nnmaildir--expired-article group article))
1441       result)))
1442
1443 (defun nnmaildir-request-accept-article (gname &optional server _last)
1444   (let ((group (nnmaildir--prepare server gname))
1445         (coding-system-for-write nnheader-file-coding-system)
1446         (buffer-file-coding-system nil)
1447         (file-coding-system-alist nil)
1448         srv-dir dir file time tmpfile curfile 24h article)
1449     (catch 'return
1450       (unless group
1451         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1452               (concat "No such group: " gname))
1453         (throw 'return nil))
1454       (setq gname (nnmaildir--grp-name group))
1455       (when (nnmaildir--param (nnmaildir--pgname nnmaildir--cur-server gname)
1456                               'read-only)
1457         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1458               (concat "Read-only group: " gname))
1459         (throw 'return nil))
1460       (setq srv-dir (nnmaildir--srv-dir nnmaildir--cur-server)
1461             dir (nnmaildir--srvgrp-dir srv-dir gname)
1462             time (current-time)
1463             file (format-time-string "%s." time))
1464       (unless (string-equal nnmaildir--delivery-time file)
1465         (setq nnmaildir--delivery-time file
1466               nnmaildir--delivery-count 0))
1467       (when (and (consp (cdr time))
1468                  (consp (cddr time)))
1469         (setq file (concat file "M" (number-to-string (caddr time)))))
1470       (setq file (concat file nnmaildir--delivery-pid)
1471             file (concat file "Q" (number-to-string nnmaildir--delivery-count))
1472             file (concat file "." (nnmaildir--system-name))
1473             tmpfile (concat (nnmaildir--tmp dir) file)
1474             curfile (concat (nnmaildir--cur dir) file ":2,"))
1475       (when (file-exists-p tmpfile)
1476         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1477               (concat "File exists: " tmpfile))
1478         (throw 'return nil))
1479       (when (file-exists-p curfile)
1480         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1481               (concat "File exists: " curfile))
1482         (throw 'return nil))
1483       (setq nnmaildir--delivery-count (1+ nnmaildir--delivery-count)
1484             24h (run-with-timer 86400 nil
1485                                 (lambda ()
1486                                   (nnmaildir--unlink tmpfile)
1487                                   (setf (nnmaildir--srv-error
1488                                           nnmaildir--cur-server)
1489                                         "24-hour timer expired")
1490                                   (throw 'return nil))))
1491       (condition-case nil (add-name-to-file nnmaildir--file tmpfile)
1492         (error
1493          (gmm-write-region (point-min) (point-max) tmpfile nil 'no-message nil
1494                            'excl)
1495          (when (fboundp 'unix-sync)
1496            (unix-sync)))) ;; no fsync :(
1497       (nnheader-cancel-timer 24h)
1498       (condition-case err
1499           (add-name-to-file tmpfile curfile)
1500         (error
1501          (setf (nnmaildir--srv-error nnmaildir--cur-server)
1502                (concat "Error linking: " (prin1-to-string err)))
1503          (nnmaildir--unlink tmpfile)
1504          (throw 'return nil)))
1505       (nnmaildir--unlink tmpfile)
1506       (setq article (make-nnmaildir--art :prefix file :suffix ":2,"))
1507       (if (nnmaildir--grp-add-art nnmaildir--cur-server group article)
1508           (cons gname (nnmaildir--art-num article))))))
1509
1510 (defun nnmaildir-save-mail (group-art)
1511   (catch 'return
1512     (unless group-art
1513       (throw 'return nil))
1514     (let (ga gname x groups nnmaildir--file deactivate-mark)
1515       (save-excursion
1516         (goto-char (point-min))
1517         (save-match-data
1518           (while (looking-at "From ")
1519             (replace-match "X-From-Line: ")
1520             (forward-line 1))))
1521       (setq groups (nnmaildir--srv-groups nnmaildir--cur-server)
1522             ga (car group-art) group-art (cdr group-art)
1523             gname (car ga))
1524       (or (intern-soft gname groups)
1525           (nnmaildir-request-create-group gname)
1526           (throw 'return nil)) ;; not that nnmail bothers to check :(
1527       (unless (nnmaildir-request-accept-article gname)
1528         (throw 'return nil))
1529       (setq nnmaildir--file (nnmaildir--srv-dir nnmaildir--cur-server)
1530             nnmaildir--file (nnmaildir--srvgrp-dir nnmaildir--file gname)
1531             x (nnmaildir--prepare nil gname)
1532             x (nnmaildir--grp-nlist x)
1533             x (cdar x)
1534             nnmaildir--file (concat nnmaildir--file
1535                                     (nnmaildir--art-prefix x)
1536                                     (nnmaildir--art-suffix x)))
1537       (delq nil
1538             (mapcar
1539              (lambda (ga)
1540                (setq gname (car ga))
1541                (and (or (intern-soft gname groups)
1542                         (nnmaildir-request-create-group gname))
1543                     (nnmaildir-request-accept-article gname)
1544                     ga))
1545              group-art)))))
1546
1547 (defun nnmaildir-active-number (_gname)
1548   0)
1549
1550 (declare-function gnus-group-mark-article-read "gnus-group" (group article))
1551
1552 (defun nnmaildir-request-expire-articles (ranges &optional gname server force)
1553   (let ((no-force (not force))
1554         (group (nnmaildir--prepare server gname))
1555         pgname time boundary bound-iter high low target dir nlist
1556         didnt nnmaildir--file nnmaildir-article-file-name
1557         deactivate-mark)
1558     (catch 'return
1559       (unless group
1560         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1561               (if gname (concat "No such group: " gname) "No current group"))
1562         (throw 'return (gnus-uncompress-range ranges)))
1563       (setq gname (nnmaildir--grp-name group)
1564             pgname (nnmaildir--pgname nnmaildir--cur-server gname))
1565       (if (nnmaildir--param pgname 'read-only)
1566           (throw 'return (gnus-uncompress-range ranges)))
1567       (setq time (nnmaildir--param pgname 'expire-age))
1568       (unless time
1569         (setq time (or (and nnmail-expiry-wait-function
1570                             (funcall nnmail-expiry-wait-function gname))
1571                        nnmail-expiry-wait))
1572         (if (eq time 'immediate)
1573             (setq time 0)
1574           (if (numberp time)
1575               (setq time (round (* time 86400))))))
1576       (when no-force
1577         (unless (integerp time) ;; handle 'never
1578           (throw 'return (gnus-uncompress-range ranges)))
1579         (setq boundary (current-time)
1580               high (- (car boundary) (/ time 65536))
1581               low (- (cadr boundary) (% time 65536)))
1582         (if (< low 0)
1583             (setq low (+ low 65536)
1584                   high (1- high)))
1585         (setcar (cdr boundary) low)
1586         (setcar boundary high))
1587       (setq dir (nnmaildir--srv-dir nnmaildir--cur-server)
1588             dir (nnmaildir--srvgrp-dir dir gname)
1589             dir (nnmaildir--cur dir)
1590             nlist (nnmaildir--grp-nlist group)
1591             ranges (reverse ranges))
1592       (nnmaildir--with-move-buffer
1593         (nnmaildir--nlist-iterate
1594          nlist ranges
1595          (lambda (article)
1596            (setq nnmaildir--file (nnmaildir--art-prefix article)
1597                  nnmaildir--file (concat dir nnmaildir--file
1598                                          (nnmaildir--art-suffix article))
1599                  time (file-attributes nnmaildir--file))
1600            (cond
1601             ((null time)
1602              (nnmaildir--expired-article group article))
1603             ((and no-force
1604                   (progn
1605                     (setq time (nth 5 time)
1606                           bound-iter boundary)
1607                     (while (and bound-iter time
1608                                 (= (car bound-iter) (car time)))
1609                       (setq bound-iter (cdr bound-iter)
1610                             time (cdr time)))
1611                     (and bound-iter time
1612                          (car-less-than-car bound-iter time))))
1613              (setq didnt (cons (nnmaildir--art-num article) didnt)))
1614             (t
1615              (setq nnmaildir-article-file-name nnmaildir--file
1616                    target (if force nil
1617                             (save-excursion
1618                               (save-restriction
1619                                 (nnmaildir--param pgname 'expire-group)))))
1620              (when (and (stringp target)
1621                         (not (string-equal target pgname))) ;; Move it.
1622                (erase-buffer)
1623                (nnheader-insert-file-contents nnmaildir--file)
1624                (let ((group-art (gnus-request-accept-article
1625                                  target nil nil 'no-encode)))
1626                  (when (consp group-art)
1627                    ;; Maybe also copy: dormant forward reply save tick
1628                    ;; (gnus-add-mark? gnus-request-set-mark?)
1629                    (gnus-group-mark-article-read target (cdr group-art)))))
1630              (if (equal target pgname)
1631                  ;; Leave it here.
1632                  (setq didnt (cons (nnmaildir--art-num article) didnt))
1633                (nnmaildir--unlink nnmaildir--file)
1634                (nnmaildir--expired-article group article))))))
1635         (erase-buffer))
1636       didnt)))
1637
1638 (defvar nnmaildir--article)
1639
1640 (defun nnmaildir-request-set-mark (gname actions &optional server)
1641   (let* ((group (nnmaildir--prepare server gname))
1642          (curdir (nnmaildir--cur
1643                   (nnmaildir--srvgrp-dir
1644                    (nnmaildir--srv-dir nnmaildir--cur-server)
1645                    gname)))
1646          (coding-system-for-write nnheader-file-coding-system)
1647          (buffer-file-coding-system nil)
1648          (file-coding-system-alist nil)
1649          marksdir nlist
1650          ranges all-marks todo-marks mdir mfile
1651          pgname ls permarkfile deactivate-mark
1652          (del-mark
1653           (lambda (mark)
1654             (let ((prefix (nnmaildir--art-prefix nnmaildir--article))
1655                   (suffix (nnmaildir--art-suffix nnmaildir--article))
1656                   (flag (nnmaildir--mark-to-flag mark)))
1657               (when flag
1658                 ;; If this mark corresponds to a flag, remove the flag from
1659                 ;; the file name.
1660                 (nnmaildir--article-set-flags
1661                  nnmaildir--article (nnmaildir--remove-flag flag suffix)
1662                  curdir))
1663               ;; We still want to delete the hardlink in the marks dir if
1664               ;; present, regardless of whether this mark has a maildir flag or
1665               ;; not, to avoid getting out of sync.
1666               (setq mfile (nnmaildir--subdir marksdir (symbol-name mark))
1667                     mfile (concat mfile prefix))
1668               (nnmaildir--unlink mfile))))
1669          (del-action (lambda (article)
1670                        (let ((nnmaildir--article article))
1671                          (mapcar del-mark todo-marks))))
1672          (add-action
1673           (lambda (article)
1674             (mapcar
1675              (lambda (mark)
1676                (let ((prefix (nnmaildir--art-prefix article))
1677                      (suffix (nnmaildir--art-suffix article))
1678                      (flag (nnmaildir--mark-to-flag mark)))
1679                  (if flag
1680                      ;; If there is a corresponding maildir flag, just rename
1681                      ;; the file.
1682                      (nnmaildir--article-set-flags
1683                       article (nnmaildir--add-flag flag suffix) curdir)
1684                    ;; Otherwise, use nnmaildir-specific marks dir.
1685                    (setq mdir (nnmaildir--subdir marksdir (symbol-name mark))
1686                          permarkfile (concat mdir ":")
1687                          mfile (concat mdir prefix))
1688                    (nnmaildir--condcase err (add-name-to-file permarkfile mfile)
1689                      (cond
1690                       ((nnmaildir--eexist-p err))
1691                       ((nnmaildir--enoent-p err)
1692                        (nnmaildir--mkdir mdir)
1693                        (nnmaildir--mkfile permarkfile)
1694                        (add-name-to-file permarkfile mfile))
1695                       ((nnmaildir--emlink-p err)
1696                        (let ((permarkfilenew (concat permarkfile "{new}")))
1697                          (nnmaildir--mkfile permarkfilenew)
1698                          (rename-file permarkfilenew permarkfile 'replace)
1699                          (add-name-to-file permarkfile mfile)))
1700                       (t (signal (car err) (cdr err))))))))
1701              todo-marks)))
1702          (set-action (lambda (article)
1703                        (funcall add-action article)
1704                        (let ((nnmaildir--article article))
1705                          (mapcar (lambda (mark)
1706                                    (unless (memq mark todo-marks)
1707                                      (funcall del-mark mark)))
1708                                  all-marks)))))
1709     (catch 'return
1710       (unless group
1711         (setf (nnmaildir--srv-error nnmaildir--cur-server)
1712               (concat "No such group: " gname))
1713         (dolist (action actions)
1714           (setq ranges (gnus-range-add ranges (car action))))
1715         (throw 'return ranges))
1716       (setq nlist (nnmaildir--grp-nlist group)
1717             marksdir (nnmaildir--srv-dir nnmaildir--cur-server)
1718             marksdir (nnmaildir--srvgrp-dir marksdir gname)
1719             marksdir (nnmaildir--nndir marksdir)
1720             marksdir (nnmaildir--marks-dir marksdir)
1721             gname (nnmaildir--grp-name group)
1722             pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1723             ls (nnmaildir--group-ls nnmaildir--cur-server pgname)
1724             all-marks (funcall ls marksdir nil "\\`[^.]" 'nosort)
1725             all-marks (gnus-delete-duplicates
1726                        ;; get mark names from mark dirs and from flag
1727                        ;; mappings
1728                        (append
1729                         (mapcar 'cdr nnmaildir-flag-mark-mapping)
1730                         (mapcar 'intern all-marks))))
1731       (dolist (action actions)
1732         (setq ranges (car action)
1733               todo-marks (caddr action))
1734         (dolist (mark todo-marks)
1735           (pushnew mark all-marks :test #'equal))
1736         (if (numberp (cdr ranges)) (setq ranges (list ranges)))
1737         (nnmaildir--nlist-iterate nlist ranges
1738                                   (cond ((eq 'del (cadr action)) del-action)
1739                                         ((eq 'add (cadr action)) add-action)
1740                                         ((eq 'set (cadr action)) set-action))))
1741       nil)))
1742
1743 (defun nnmaildir-close-group (gname &optional server)
1744   (let ((group (nnmaildir--prepare server gname))
1745         pgname ls dir msgdir files flist dirs)
1746     (if (null group)
1747         (progn
1748           (setf (nnmaildir--srv-error nnmaildir--cur-server)
1749                 (concat "No such group: " gname))
1750           nil)
1751       (setq pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1752             ls (nnmaildir--group-ls nnmaildir--cur-server pgname)
1753             dir (nnmaildir--srv-dir nnmaildir--cur-server)
1754             dir (nnmaildir--srvgrp-dir dir gname)
1755             msgdir (if (nnmaildir--param pgname 'read-only)
1756                        (nnmaildir--new dir) (nnmaildir--cur dir))
1757             dir (nnmaildir--nndir dir)
1758             dirs (cons (nnmaildir--nov-dir dir)
1759                        (funcall ls (nnmaildir--marks-dir dir) 'full "\\`[^.]"
1760                                 'nosort))
1761             dirs (mapcar
1762                   (lambda (dir)
1763                     (cons dir (funcall ls dir nil "\\`[^.]" 'nosort)))
1764                   dirs)
1765             files (funcall ls msgdir nil "\\`[^.]" 'nosort)
1766             flist (nnmaildir--up2-1 (length files))
1767             flist (make-vector flist 0))
1768       (save-match-data
1769         (dolist (file files)
1770           (string-match "\\`\\([^:]*\\)\\(:.*\\)?\\'" file)
1771           (intern (match-string 1 file) flist)))
1772       (dolist (dir dirs)
1773         (setq files (cdr dir)
1774               dir (file-name-as-directory (car dir)))
1775         (dolist (file files)
1776           (unless (or (intern-soft file flist) (string= file ":"))
1777             (setq file (concat dir file))
1778             (delete-file file))))
1779       t)))
1780
1781 (defun nnmaildir-close-server (&optional server)
1782   (defvar flist) (defvar ls) (defvar dirs) (defvar dir)
1783   (defvar files) (defvar file) (defvar x)
1784   (let (flist ls dirs dir files file x)
1785     (nnmaildir--prepare server nil)
1786     (when nnmaildir--cur-server
1787       (setq server nnmaildir--cur-server
1788             nnmaildir--cur-server nil)
1789       (unintern (nnmaildir--srv-address server) nnmaildir--servers)))
1790   t)
1791
1792 (defun nnmaildir-request-close ()
1793   (let (servers buffer)
1794     (mapatoms (lambda (server)
1795                 (setq servers (cons (symbol-name server) servers)))
1796               nnmaildir--servers)
1797     (mapc 'nnmaildir-close-server servers)
1798     (setq buffer (get-buffer " *nnmaildir work*"))
1799     (if buffer (kill-buffer buffer))
1800     (setq buffer (get-buffer " *nnmaildir nov*"))
1801     (if buffer (kill-buffer buffer))
1802     (setq buffer (get-buffer " *nnmaildir move*"))
1803     (if buffer (kill-buffer buffer)))
1804   t)
1805
1806 (provide 'nnmaildir)
1807
1808 ;; Local Variables:
1809 ;; indent-tabs-mode: t
1810 ;; fill-column: 77
1811 ;; End:
1812
1813 ;;; nnmaildir.el ends here