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