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