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