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