1 ;;; nnmaildir.el --- maildir backend for Gnus
4 ;; Author: Paul Jarc <prj@po.cwru.edu>
6 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
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.
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.
56 ;; eval this before editing
58 (put 'nnmaildir--with-nntp-buffer 'lisp-indent-function 0)
59 (put 'nnmaildir--with-work-buffer 'lisp-indent-function 0)
60 (put 'nnmaildir--with-nov-buffer 'lisp-indent-function 0)
61 (put 'nnmaildir--with-move-buffer 'lisp-indent-function 0)
77 (defconst nnmaildir-version "Gnus")
79 (defvar nnmaildir-article-file-name nil
80 "*The filename of the most recently requested article. This variable is set
81 by nnmaildir-request-article.")
83 ;; The filename of the article being moved/copied:
84 (defvar nnmaildir--file nil)
86 ;; Variables to generate filenames of messages being delivered:
87 (defvar nnmaildir--delivery-time "")
88 (defconst nnmaildir--delivery-pid (number-to-string (emacs-pid)))
89 (defvar nnmaildir--delivery-ct nil)
91 ;; An obarry containing symbols whose names are server names and whose values
93 (defvar nnmaildir--servers (make-vector 3 0))
94 ;; The current server:
95 (defvar nnmaildir--cur-server nil)
97 ;; A copy of nnmail-extra-headers
98 (defvar nnmaildir--extra nil)
100 ;; A NOV structure looks like this (must be prin1-able, so no defstruct):
101 ["subject\tfrom\tdate"
102 "references\tchars\lines"
103 "To: you\tIn-Reply-To: <your.mess@ge>"
104 (12345 67890) ;; modtime of the corresponding article file
105 (to in-reply-to)] ;; contemporary value of nnmail-extra-headers
106 (defconst nnmaildir--novlen 5)
107 (defmacro nnmaildir--nov-new (beg mid end mtime extra)
108 `(vector ,beg ,mid ,end ,mtime ,extra))
109 (defmacro nnmaildir--nov-get-beg (nov) `(aref ,nov 0))
110 (defmacro nnmaildir--nov-get-mid (nov) `(aref ,nov 1))
111 (defmacro nnmaildir--nov-get-end (nov) `(aref ,nov 2))
112 (defmacro nnmaildir--nov-get-mtime (nov) `(aref ,nov 3))
113 (defmacro nnmaildir--nov-get-extra (nov) `(aref ,nov 4))
114 (defmacro nnmaildir--nov-set-beg (nov value) `(aset ,nov 0 ,value))
115 (defmacro nnmaildir--nov-set-mid (nov value) `(aset ,nov 1 ,value))
116 (defmacro nnmaildir--nov-set-end (nov value) `(aset ,nov 2 ,value))
117 (defmacro nnmaildir--nov-set-mtime (nov value) `(aset ,nov 3 ,value))
118 (defmacro nnmaildir--nov-set-extra (nov value) `(aset ,nov 4 ,value))
120 (defstruct nnmaildir--art
121 (prefix nil :type string) ;; "time.pid.host"
122 (suffix nil :type string) ;; ":2,flags"
123 (num nil :type natnum) ;; article number
124 (msgid nil :type string) ;; "<mess.age@id>"
125 (nov nil :type vector)) ;; cached nov structure, or nil
127 (defstruct nnmaildir--grp
128 (name nil :type string) ;; "group.name"
129 (new nil :type list) ;; new/ modtime
130 (cur nil :type list) ;; cur/ modtime
131 (min 1 :type natnum) ;; minimum article number
132 (count 0 :type natnum) ;; count of articles
133 (nlist nil :type list) ;; list of articles, ordered descending by number
134 (flist nil :type vector) ;; obarray mapping filename prefix->article
135 (mlist nil :type vector) ;; obarray mapping message-id->article
136 (cache nil :type vector) ;; nov cache
137 (index nil :type natnum) ;; index of next cache entry to replace
138 (mmth nil :type vector)) ;; obarray mapping mark name->dir modtime
139 ; ("Mark Mod Time Hash")
141 (defstruct nnmaildir--srv
142 (address nil :type string) ;; server address string
143 (method nil :type list) ;; (nnmaildir "address" ...)
144 (prefix nil :type string) ;; "nnmaildir+address:"
145 (dir nil :type string) ;; "/expanded/path/to/server/dir/"
146 (ls nil :type function) ;; directory-files function
147 (groups nil :type vector) ;; obarray mapping group names->groups
148 (curgrp nil :type nnmaildir--grp) ;; current group, or nil
149 (error nil :type string) ;; last error message, or nil
150 (mtime nil :type list) ;; modtime of dir
151 (gnm nil) ;; flag: split from mail-sources?
152 (create-dir nil :type string)) ;; group creation directory
154 (defun nnmaildir--expired-article (group article)
155 (setf (nnmaildir--art-nov article) nil)
156 (let ((flist (nnmaildir--grp-flist group))
157 (mlist (nnmaildir--grp-mlist group))
158 (min (nnmaildir--grp-min group))
159 (count (1- (nnmaildir--grp-count group)))
160 (prefix (nnmaildir--art-prefix article))
161 (msgid (nnmaildir--art-msgid article))
163 (nlist-pre '(nil . nil))
165 (unless (zerop count)
166 (setq nlist-post (nnmaildir--grp-nlist group)
167 num (nnmaildir--art-num article))
168 (if (eq num (caar nlist-post))
169 (setq new-nlist (cdr nlist-post))
170 (setq new-nlist nlist-post
172 nlist-post (cdr nlist-post))
173 (while (/= num (caar nlist-post))
174 (setq nlist-pre nlist-post
175 nlist-post (cdr nlist-post)))
176 (setq nlist-post (cdr nlist-post))
178 (setq min (caar nlist-pre)))))
179 (let ((inhibit-quit t))
180 (setf (nnmaildir--grp-min group) min)
181 (setf (nnmaildir--grp-count group) count)
182 (setf (nnmaildir--grp-nlist group) new-nlist)
183 (setcdr nlist-pre nlist-post)
184 (unintern prefix flist)
185 (unintern msgid mlist))))
187 (defun nnmaildir--nlist-art (group num)
188 (let ((entry (assq num (nnmaildir--grp-nlist group))))
191 (defmacro nnmaildir--flist-art (list file)
192 `(symbol-value (intern-soft ,file ,list)))
193 (defmacro nnmaildir--mlist-art (list msgid)
194 `(symbol-value (intern-soft ,msgid ,list)))
196 (defun nnmaildir--pgname (server gname)
197 (let ((prefix (nnmaildir--srv-prefix server)))
198 (if prefix (concat prefix gname)
199 (setq gname (gnus-group-prefixed-name gname
200 (nnmaildir--srv-method server)))
201 (setf (nnmaildir--srv-prefix server) (gnus-group-real-prefix gname))
204 (defun nnmaildir--param (pgname param)
205 (setq param (gnus-group-find-parameter pgname param 'allow-list))
206 (if (vectorp param) (setq param (aref param 0)))
209 (defmacro nnmaildir--with-nntp-buffer (&rest body)
211 (set-buffer nntp-server-buffer)
213 (defmacro nnmaildir--with-work-buffer (&rest body)
215 (set-buffer (get-buffer-create " *nnmaildir work*"))
217 (defmacro nnmaildir--with-nov-buffer (&rest body)
219 (set-buffer (get-buffer-create " *nnmaildir nov*"))
221 (defmacro nnmaildir--with-move-buffer (&rest body)
223 (set-buffer (get-buffer-create " *nnmaildir move*"))
226 (defmacro nnmaildir--subdir (dir subdir)
227 `(file-name-as-directory (concat ,dir ,subdir)))
228 (defmacro nnmaildir--srvgrp-dir (srv-dir gname)
229 `(nnmaildir--subdir ,srv-dir ,gname))
230 (defmacro nnmaildir--tmp (dir) `(nnmaildir--subdir ,dir "tmp"))
231 (defmacro nnmaildir--new (dir) `(nnmaildir--subdir ,dir "new"))
232 (defmacro nnmaildir--cur (dir) `(nnmaildir--subdir ,dir "cur"))
233 (defmacro nnmaildir--nndir (dir) `(nnmaildir--subdir ,dir ".nnmaildir"))
234 (defmacro nnmaildir--nov-dir (dir) `(nnmaildir--subdir ,dir "nov"))
235 (defmacro nnmaildir--marks-dir (dir) `(nnmaildir--subdir ,dir "marks"))
236 (defmacro nnmaildir--num-dir (dir) `(nnmaildir--subdir ,dir "num"))
237 (defmacro nnmaildir--num-file (dir) `(concat ,dir ":"))
239 (defmacro nnmaildir--unlink (file-arg)
240 `(let ((file ,file-arg))
241 (if (file-attributes file) (delete-file file))))
242 (defun nnmaildir--mkdir (dir)
243 (or (file-exists-p (file-name-as-directory dir))
244 (make-directory-internal (directory-file-name dir))))
245 (defun nnmaildir--delete-dir-files (dir ls)
246 (mapcar 'delete-file (funcall ls dir 'full "\\`[^.]" 'nosort))
247 (delete-directory dir))
249 (defun nnmaildir--group-maxnum (server group)
250 (let ((x (nnmaildir--srvgrp-dir (nnmaildir--srv-dir server)
251 (nnmaildir--grp-name group))))
252 (setq x (nnmaildir--nndir x)
253 x (nnmaildir--num-dir x)
254 x (nnmaildir--num-file x)
255 x (file-attributes x))
256 (if x (1- (nth 1 x)) 0)))
258 ;; Make the given server, if non-nil, be the current server. Then make the
259 ;; given group, if non-nil, be the current group of the current server. Then
260 ;; return the group object for the current group.
261 (defun nnmaildir--prepare (server group)
265 (unless (setq server nnmaildir--cur-server)
267 (unless (setq server (intern-soft server nnmaildir--servers))
269 (setq server (symbol-value server)
270 nnmaildir--cur-server server))
271 (unless (setq groups (nnmaildir--srv-groups server))
273 (unless (nnmaildir--srv-method server)
274 (setq x (concat "nnmaildir:" (nnmaildir--srv-address server))
275 x (gnus-server-to-method x))
276 (unless x (throw 'return nil))
277 (setf (nnmaildir--srv-method server) x))
279 (unless (setq group (nnmaildir--srv-curgrp server))
281 (unless (setq group (intern-soft group groups))
283 (setq group (symbol-value group)))
286 (defun nnmaildir--tab-to-space (string)
288 (while (string-match "\t" string pos)
289 (aset string (match-beginning 0) ? )
290 (setq pos (match-end 0))))
293 (defun nnmaildir--update-nov (server group article)
294 (let ((nnheader-file-coding-system 'binary)
295 (srv-dir (nnmaildir--srv-dir server))
296 (storage-version 1) ;; [version article-number msgid [...nov...]]
297 dir gname pgname msgdir prefix suffix file attr mtime novdir novfile
298 nov msgid nov-beg nov-mid nov-end field val old-extra num numdir
301 (setq gname (nnmaildir--grp-name group)
302 pgname (nnmaildir--pgname server gname)
303 dir (nnmaildir--srvgrp-dir srv-dir gname)
304 msgdir (if (nnmaildir--param pgname 'read-only)
305 (nnmaildir--new dir) (nnmaildir--cur dir))
306 prefix (nnmaildir--art-prefix article)
307 suffix (nnmaildir--art-suffix article)
308 file (concat msgdir prefix suffix)
309 attr (file-attributes file))
311 (nnmaildir--expired-article group article)
313 (setq mtime (nth 5 attr)
315 nov (nnmaildir--art-nov article)
316 dir (nnmaildir--nndir dir)
317 novdir (nnmaildir--nov-dir dir)
318 novfile (concat novdir prefix))
319 (unless (equal nnmaildir--extra nnmail-extra-headers)
320 (setq nnmaildir--extra (copy-sequence nnmail-extra-headers)))
321 (nnmaildir--with-nov-buffer
322 ;; First we'll check for already-parsed NOV data.
323 (cond ((not (file-exists-p novfile))
324 ;; The NOV file doesn't exist; we have to parse the message.
327 ;; The file exists, but the data isn't in memory; read the file.
329 (nnheader-insert-file-contents novfile)
330 (setq nov (read (current-buffer)))
331 (if (not (and (vectorp nov)
333 (equal storage-version (aref nov 0))))
334 ;; This NOV data seems to be in the wrong format.
336 (unless (nnmaildir--art-num article)
337 (setf (nnmaildir--art-num article) (aref nov 1)))
338 (unless (nnmaildir--art-msgid article)
339 (setf (nnmaildir--art-msgid article) (aref nov 2)))
340 (setq nov (aref nov 3)))))
341 ;; Now check whether the already-parsed data (if we have any) is
342 ;; usable: if the message has been edited or if nnmail-extra-headers
343 ;; has been augmented since this data was parsed from the message,
344 ;; then we have to reparse. Otherwise it's up-to-date.
345 (when (and nov (equal mtime (nnmaildir--nov-get-mtime nov)))
346 ;; The timestamp matches. Now check nnmail-extra-headers.
347 (setq old-extra (nnmaildir--nov-get-extra nov))
348 (when (equal nnmaildir--extra old-extra) ;; common case
349 ;; Save memory; use a single copy of the list value.
350 (nnmaildir--nov-set-extra nov nnmaildir--extra)
352 ;; They're not equal, but maybe the new is a subset of the old.
353 (if (null nnmaildir--extra)
354 ;; The empty set is a subset of every set.
356 (if (not (memq nil (mapcar (lambda (e) (memq e old-extra))
358 (throw 'return nov)))
359 ;; Parse the NOV data out of the message.
361 (nnheader-insert-file-contents file)
363 (goto-char (point-min))
365 (if (search-forward "\n\n" nil 'noerror)
367 (setq nov-mid (count-lines (point) (point-max)))
368 (narrow-to-region (point-min) (1- (point))))
370 (goto-char (point-min))
372 (nnheader-fold-continuation-lines)
373 (setq nov (nnheader-parse-head 'naked)
374 field (or (mail-header-lines nov) 0)))
375 (unless (or (zerop field) (nnmaildir--param pgname 'distrust-Lines:))
376 (setq nov-mid field))
377 (setq nov-mid (number-to-string nov-mid)
378 nov-mid (concat (number-to-string attr) "\t" nov-mid))
380 (setq field (or (mail-header-references nov) ""))
381 (nnmaildir--tab-to-space field)
382 (setq nov-mid (concat field "\t" nov-mid)
384 (lambda (f) (nnmaildir--tab-to-space (or f "")))
385 (list (mail-header-subject nov)
386 (mail-header-from nov)
387 (mail-header-date nov)) "\t")
390 (setq field (symbol-name (car extra))
392 (nnmaildir--tab-to-space field)
393 (nnmaildir--tab-to-space val)
394 (concat field ": " val))
395 (mail-header-extra nov) "\t")))
396 (setq msgid (mail-header-id nov))
397 (if (or (null msgid) (nnheader-fake-message-id-p msgid))
398 (setq msgid (concat "<" prefix "@nnmaildir>")))
399 (nnmaildir--tab-to-space msgid)
400 ;; The data is parsed; create an nnmaildir NOV structure.
401 (setq nov (nnmaildir--nov-new nov-beg nov-mid nov-end mtime
403 num (nnmaildir--art-num article))
405 ;; Allocate a new article number.
407 (setq numdir (nnmaildir--num-dir dir)
408 file (nnmaildir--num-file numdir)
410 (nnmaildir--mkdir numdir)
411 (write-region "" nil file nil 'no-message)
413 ;; Get the number of links to file.
414 (setq attr (nth 1 (file-attributes file)))
416 ;; We've already tried this number, in the previous loop
417 ;; iteration, and failed.
418 (signal 'error `("Corrupt internal nnmaildir data" ,numdir)))
419 ;; If attr is 123, try to link file to "123". This atomically
420 ;; increases the link count and creates the "123" link, failing
421 ;; if that link was already created by another Gnus, just after
425 (add-name-to-file file (concat numdir (format "%x" attr)))
426 (setq file nil)) ;; Stop looping.
427 (file-already-exists nil))
429 (setf (nnmaildir--art-num article) num))
430 ;; Store this new NOV data in a file
432 (prin1 (vector storage-version num msgid nov) (current-buffer))
433 (setq file (concat novfile ":"))
434 (nnmaildir--unlink file)
435 (write-region (point-min) (point-max) file nil 'no-message nil 'excl))
436 (rename-file file novfile 'replace)
437 (setf (nnmaildir--art-msgid article) msgid)
440 (defun nnmaildir--cache-nov (group article nov)
441 (let ((cache (nnmaildir--grp-cache group))
442 (index (nnmaildir--grp-index group))
444 (unless (nnmaildir--art-nov article)
445 (setq goner (aref cache index))
446 (if goner (setf (nnmaildir--art-nov goner) nil))
447 (aset cache index article)
448 (setf (nnmaildir--grp-index group) (% (1+ index) (length cache))))
449 (setf (nnmaildir--art-nov article) nov)))
451 (defun nnmaildir--grp-add-art (server group article)
452 (let ((nov (nnmaildir--update-nov server group article))
453 count num min nlist nlist-cdr insert-nlist)
455 (setq count (1+ (nnmaildir--grp-count group))
456 min (nnmaildir--grp-min group)
457 num (nnmaildir--art-num article))
458 (setq min (min min num))
459 (setq nlist (nnmaildir--grp-nlist group))
460 (if (or (null nlist) (> num (caar nlist)))
461 (setq nlist (cons (cons num article) nlist))
463 nlist-cdr (cdr nlist))
464 (while (< num (caar nlist-cdr))
465 (setq nlist nlist-cdr
466 nlist-cdr (cdr nlist))))
467 (let ((inhibit-quit t))
468 (setf (nnmaildir--grp-count group) count)
469 (setf (nnmaildir--grp-min group) min)
471 (setcdr nlist (cons (cons num article) nlist-cdr))
472 (setf (nnmaildir--grp-nlist group) nlist))
473 (set (intern (nnmaildir--art-prefix article)
474 (nnmaildir--grp-flist group))
476 (set (intern (nnmaildir--art-msgid article)
477 (nnmaildir--grp-mlist group))
479 (set (intern (nnmaildir--grp-name group)
480 (nnmaildir--srv-groups server))
482 (nnmaildir--cache-nov group article nov)
485 (defun nnmaildir--group-ls (server pgname)
486 (or (nnmaildir--param pgname 'directory-files)
487 (nnmaildir--srv-ls server)))
489 (defun nnmaildir-article-number-to-file-name
490 (number group-name server-address-string)
491 (let ((group (nnmaildir--prepare server-address-string group-name))
495 ;; The given group or server does not exist.
497 (setq article (nnmaildir--nlist-art group number))
499 ;; The given article number does not exist in this group.
501 (setq pgname (nnmaildir--pgname nnmaildir--cur-server group-name)
502 dir (nnmaildir--srv-dir nnmaildir--cur-server)
503 dir (nnmaildir--srvgrp-dir dir group-name)
504 dir (if (nnmaildir--param pgname 'read-only)
505 (nnmaildir--new dir) (nnmaildir--cur dir)))
506 (concat dir (nnmaildir--art-prefix article)
507 (nnmaildir--art-suffix article)))))
509 (defun nnmaildir-article-number-to-base-name
510 (number group-name server-address-string)
511 (let ((x (nnmaildir--prepare server-address-string group-name)))
513 (setq x (nnmaildir--nlist-art x number))
514 (and x (cons (nnmaildir--art-prefix x)
515 (nnmaildir--art-suffix x))))))
517 (defun nnmaildir-base-name-to-article-number
518 (base-name group-name server-address-string)
519 (let ((x (nnmaildir--prepare server-address-string group-name)))
521 (setq x (nnmaildir--grp-flist x)
522 x (nnmaildir--flist-art x base-name))
523 (and x (nnmaildir--art-num x)))))
525 (defun nnmaildir--nlist-iterate (nlist ranges func)
526 (let (entry high low nlist2)
528 (setq ranges `((1 . ,(caar nlist)))))
530 (setq entry (car ranges) ranges (cdr ranges))
531 (while (and ranges (eq entry (car ranges)))
532 (setq ranges (cdr ranges))) ;; skip duplicates
536 (setq low (car entry)
538 (setq nlist2 nlist) ;; Don't assume any sorting of ranges
541 (if (<= (caar nlist2) high) (throw 'iterate-loop nil))
542 (setq nlist2 (cdr nlist2))))
545 (setq entry (car nlist2) nlist2 (cdr nlist2))
546 (if (< (car entry) low) (throw 'iterate-loop nil))
547 (funcall func (cdr entry)))))))
549 (defun nnmaildir--up2-1 (n)
550 (if (zerop n) 1 (1- (lsh 1 (1+ (logb n))))))
552 (defun nnmaildir-request-type (group &optional article)
555 (defun nnmaildir-status-message (&optional server)
556 (nnmaildir--prepare server nil)
557 (nnmaildir--srv-error nnmaildir--cur-server))
559 (defun nnmaildir-server-opened (&optional server)
560 (and nnmaildir--cur-server
562 (string-equal server (nnmaildir--srv-address nnmaildir--cur-server))
564 (nnmaildir--srv-groups nnmaildir--cur-server)
567 (defun nnmaildir-open-server (server &optional defs)
571 (setq server (intern-soft x nnmaildir--servers))
573 (and (setq server (symbol-value server))
574 (nnmaildir--srv-groups server)
575 (setq nnmaildir--cur-server server)
577 (setq server (make-nnmaildir--srv :address x))
578 (let ((inhibit-quit t))
579 (set (intern x nnmaildir--servers) server)))
580 (setq dir (assq 'directory defs))
582 (setf (nnmaildir--srv-error server)
583 "You must set \"directory\" in the select method")
587 dir (expand-file-name dir)
588 dir (file-name-as-directory dir))
589 (unless (file-exists-p dir)
590 (setf (nnmaildir--srv-error server) (concat "No such directory: " dir))
592 (setf (nnmaildir--srv-dir server) dir)
593 (setq x (assq 'directory-files defs))
595 (setq x (if nnheader-directory-files-is-safe 'directory-files
596 'nnheader-directory-files-safe))
598 (unless (functionp x)
599 (setf (nnmaildir--srv-error server)
600 (concat "Not a function: " (prin1-to-string x)))
601 (throw 'return nil)))
602 (setf (nnmaildir--srv-ls server) x)
603 (setq size (length (funcall x dir nil "\\`[^.]" 'nosort))
604 size (nnmaildir--up2-1 size))
605 (and (setq x (assq 'get-new-mail defs))
608 (setf (nnmaildir--srv-gnm server) t)
610 (setq x (assq 'create-directory defs))
614 (setf (nnmaildir--srv-create-dir server) x))
615 (setf (nnmaildir--srv-groups server) (make-vector size 0))
616 (setq nnmaildir--cur-server server)
619 (defun nnmaildir--parse-filename (file)
620 (let ((prefix (car file))
623 "\\`\\([0-9]+\\)\\.\\([0-9]+\\)\\(_\\([0-9]+\\)\\)?\\(\\..*\\)\\'"
626 (setq timestamp (concat "0000" (match-string 1 prefix))
627 len (- (length timestamp) 4))
628 (vector (string-to-number (substring timestamp 0 len))
629 (string-to-number (substring timestamp len))
630 (string-to-number (match-string 2 prefix))
631 (string-to-number (or (match-string 4 prefix) "-1"))
632 (match-string 5 prefix)
636 (defun nnmaildir--sort-files (a b)
639 (throw 'return (and (consp b) (string-lessp (car a) (car b)))))
640 (if (consp b) (throw 'return t))
641 (if (< (aref a 0) (aref b 0)) (throw 'return t))
642 (if (> (aref a 0) (aref b 0)) (throw 'return nil))
643 (if (< (aref a 1) (aref b 1)) (throw 'return t))
644 (if (> (aref a 1) (aref b 1)) (throw 'return nil))
645 (if (< (aref a 2) (aref b 2)) (throw 'return t))
646 (if (> (aref a 2) (aref b 2)) (throw 'return nil))
647 (if (< (aref a 3) (aref b 3)) (throw 'return t))
648 (if (> (aref a 3) (aref b 3)) (throw 'return nil))
649 (string-lessp (aref a 4) (aref b 4))))
651 (defun nnmaildir--scan (gname scan-msgs groups method srv-dir srv-ls)
653 (let ((36h-ago (- (car (current-time)) 2))
654 absdir nndir tdir ndir cdir nattr cattr isnew pgname read-only ls
655 files num dir flist group x)
656 (setq absdir (nnmaildir--srvgrp-dir srv-dir gname)
657 nndir (nnmaildir--nndir absdir))
658 (unless (file-exists-p absdir)
659 (setf (nnmaildir--srv-error nnmaildir--cur-server)
660 (concat "No such directory: " absdir))
662 (setq tdir (nnmaildir--tmp absdir)
663 ndir (nnmaildir--new absdir)
664 cdir (nnmaildir--cur absdir)
665 nattr (file-attributes ndir)
666 cattr (file-attributes cdir))
667 (unless (and (file-exists-p tdir) nattr cattr)
668 (setf (nnmaildir--srv-error nnmaildir--cur-server)
669 (concat "Not a maildir: " absdir))
671 (setq group (nnmaildir--prepare nil gname)
672 pgname (nnmaildir--pgname nnmaildir--cur-server gname))
676 group (make-nnmaildir--grp :name gname :index 0))
677 (nnmaildir--mkdir nndir)
678 (nnmaildir--mkdir (nnmaildir--nov-dir nndir))
679 (nnmaildir--mkdir (nnmaildir--marks-dir nndir))
680 (write-region "" nil (concat nndir "markfile") nil 'no-message))
681 (setq read-only (nnmaildir--param pgname 'read-only)
682 ls (or (nnmaildir--param pgname 'directory-files) srv-ls))
684 (setq x (nth 11 (file-attributes tdir)))
685 (unless (and (= x (nth 11 nattr)) (= x (nth 11 cattr)))
686 (setf (nnmaildir--srv-error nnmaildir--cur-server)
687 (concat "Maildir spans filesystems: " absdir))
691 (setq x (file-attributes file))
692 (if (or (> (cadr x) 1) (< (car (nth 4 x)) 36h-ago))
694 (funcall ls tdir 'full "\\`[^.]" 'nosort)))
698 (setq nattr (nth 5 nattr))
699 (if (equal nattr (nnmaildir--grp-new group))
701 (if read-only (setq dir (and (or isnew nattr) ndir))
702 (when (or isnew nattr)
705 (rename-file (concat ndir file) (concat cdir file ":2,")))
706 (funcall ls ndir nil "\\`[^.]" 'nosort))
707 (setf (nnmaildir--grp-new group) nattr))
708 (setq cattr (nth 5 (file-attributes cdir)))
709 (if (equal cattr (nnmaildir--grp-cur group))
711 (setq dir (and (or isnew cattr) cdir)))
712 (unless dir (throw 'return t))
713 (setq files (funcall ls dir nil "\\`[^.]" 'nosort)
714 files (save-match-data
717 (string-match "\\`\\([^:]*\\)\\(\\(:.*\\)?\\)\\'" f)
718 (cons (match-string 1 f) (match-string 2 f)))
721 (setq num (nnmaildir--up2-1 (length files)))
722 (setf (nnmaildir--grp-flist group) (make-vector num 0))
723 (setf (nnmaildir--grp-mlist group) (make-vector num 0))
724 (setf (nnmaildir--grp-mmth group) (make-vector 1 0))
725 (setq num (nnmaildir--param pgname 'nov-cache-size))
726 (if (numberp num) (if (< num 1) (setq num 1))
728 cdir (nnmaildir--marks-dir nndir)
729 ndir (nnmaildir--subdir cdir "tick")
730 cdir (nnmaildir--subdir cdir "read"))
733 (setq file (car file))
734 (if (or (not (file-exists-p (concat cdir file)))
735 (file-exists-p (concat ndir file)))
736 (setq num (1+ num))))
738 (setf (nnmaildir--grp-cache group) (make-vector num nil))
739 (let ((inhibit-quit t))
740 (set (intern gname groups) group))
741 (or scan-msgs (throw 'return t)))
742 (setq flist (nnmaildir--grp-flist group)
745 (and (null (nnmaildir--flist-art flist (car file)))
748 files (delq nil files)
749 files (mapcar 'nnmaildir--parse-filename files)
750 files (sort files 'nnmaildir--sort-files))
753 (setq file (if (consp file) file (aref file 5))
754 x (make-nnmaildir--art :prefix (car file) :suffix (cdr file)))
755 (nnmaildir--grp-add-art nnmaildir--cur-server group x))
757 (if read-only (setf (nnmaildir--grp-new group) nattr)
758 (setf (nnmaildir--grp-cur group) cattr)))
761 (defun nnmaildir-request-scan (&optional scan-group server)
762 (let ((coding-system-for-write nnheader-file-coding-system)
763 (buffer-file-coding-system nil)
764 (file-coding-system-alist nil)
765 (nnmaildir-get-new-mail t)
766 (nnmaildir-group-alist nil)
767 (nnmaildir-active-file nil)
768 x srv-ls srv-dir method groups group dirs grp-dir seen deactivate-mark)
769 (nnmaildir--prepare server nil)
770 (setq srv-ls (nnmaildir--srv-ls nnmaildir--cur-server)
771 srv-dir (nnmaildir--srv-dir nnmaildir--cur-server)
772 method (nnmaildir--srv-method nnmaildir--cur-server)
773 groups (nnmaildir--srv-groups nnmaildir--cur-server))
774 (nnmaildir--with-work-buffer
776 (if (stringp scan-group)
777 (if (nnmaildir--scan scan-group t groups method srv-dir srv-ls)
778 (if (nnmaildir--srv-gnm nnmaildir--cur-server)
779 (nnmail-get-new-mail 'nnmaildir nil nil scan-group))
780 (unintern scan-group groups))
781 (setq x (nth 5 (file-attributes srv-dir))
782 scan-group (null scan-group))
783 (if (equal x (nnmaildir--srv-mtime nnmaildir--cur-server))
785 (mapatoms (lambda (sym)
786 (nnmaildir--scan (symbol-name sym) t groups
787 method srv-dir srv-ls))
789 (setq dirs (funcall srv-ls srv-dir nil "\\`[^.]" 'nosort)
790 seen (nnmaildir--up2-1 (length dirs))
791 seen (make-vector seen 0))
794 (if (nnmaildir--scan grp-dir scan-group groups method srv-dir
796 (intern grp-dir seen)))
799 (mapatoms (lambda (group)
800 (setq group (symbol-name group))
801 (unless (intern-soft group seen)
802 (setq x (cons group x))))
804 (mapcar (lambda (grp) (unintern grp groups)) x)
805 (setf (nnmaildir--srv-mtime nnmaildir--cur-server)
806 (nth 5 (file-attributes srv-dir))))
808 (nnmaildir--srv-gnm nnmaildir--cur-server)
809 (nnmail-get-new-mail 'nnmaildir nil nil))))))
812 (defun nnmaildir-request-list (&optional server)
813 (nnmaildir-request-scan 'find-new-groups server)
814 (let (pgname ro deactivate-mark)
815 (nnmaildir--prepare server nil)
816 (nnmaildir--with-nntp-buffer
818 (mapatoms (lambda (group)
819 (setq pgname (symbol-name group)
820 pgname (nnmaildir--pgname nnmaildir--cur-server pgname)
821 group (symbol-value group)
822 ro (nnmaildir--param pgname 'read-only))
823 (insert (nnmaildir--grp-name group) " ")
824 (princ (nnmaildir--group-maxnum nnmaildir--cur-server group)
827 (princ (nnmaildir--grp-min group) nntp-server-buffer)
828 (insert " " (if ro "n" "y") "\n"))
829 (nnmaildir--srv-groups nnmaildir--cur-server))))
832 (defun nnmaildir-request-newgroups (date &optional server)
833 (nnmaildir-request-list server))
835 (defun nnmaildir-retrieve-groups (groups &optional server)
836 (let (group deactivate-mark)
837 (nnmaildir--prepare server nil)
838 (nnmaildir--with-nntp-buffer
842 (setq group (nnmaildir--prepare nil gname))
843 (if (null group) (insert "411 no such news group\n")
845 (princ (nnmaildir--grp-count group) nntp-server-buffer)
847 (princ (nnmaildir--grp-min group) nntp-server-buffer)
849 (princ (nnmaildir--group-maxnum nnmaildir--cur-server group)
851 (insert " " gname "\n")))
855 (defun nnmaildir-request-update-info (gname info &optional server)
856 (let ((group (nnmaildir--prepare server gname))
857 pgname flist all always-marks never-marks old-marks dotfile num dir
858 markdirs marks mark ranges markdir article read end new-marks ls
859 old-mmth new-mmth mtime mark-sym deactivate-mark)
862 (setf (nnmaildir--srv-error nnmaildir--cur-server)
863 (concat "No such group: " gname))
865 (setq gname (nnmaildir--grp-name group)
866 pgname (nnmaildir--pgname nnmaildir--cur-server gname)
867 flist (nnmaildir--grp-flist group))
868 (when (zerop (nnmaildir--grp-count group))
869 (gnus-info-set-read info nil)
870 (gnus-info-set-marks info nil 'extend)
871 (throw 'return info))
872 (setq old-marks (cons 'read (gnus-info-read info))
873 old-marks (cons old-marks (gnus-info-marks info))
874 always-marks (nnmaildir--param pgname 'always-marks)
875 never-marks (nnmaildir--param pgname 'never-marks)
876 dir (nnmaildir--srv-dir nnmaildir--cur-server)
877 dir (nnmaildir--srvgrp-dir dir gname)
878 dir (nnmaildir--nndir dir)
879 dir (nnmaildir--marks-dir dir)
880 ls (nnmaildir--group-ls nnmaildir--cur-server pgname)
881 markdirs (funcall ls dir nil "\\`[^.]" 'nosort)
882 new-mmth (nnmaildir--up2-1 (length markdirs))
883 new-mmth (make-vector new-mmth 0)
884 old-mmth (nnmaildir--grp-mmth group))
887 (setq markdir (nnmaildir--subdir dir mark)
888 mark-sym (intern mark)
891 (if (memq mark-sym never-marks) (throw 'got-ranges nil))
892 (when (memq mark-sym always-marks)
894 (setq all (nnmaildir--grp-nlist group)
895 all (mapcar 'car all)
897 all (gnus-compress-sequence all 'always-list)
898 all (cons 'dummy-mark-symbol all)))
899 (setq ranges (cdr all))
900 (throw 'got-ranges nil))
901 (setq mtime (nth 5 (file-attributes markdir)))
902 (set (intern mark new-mmth) mtime)
903 (when (equal mtime (symbol-value (intern-soft mark old-mmth)))
904 (setq ranges (assq mark-sym old-marks))
905 (if ranges (setq ranges (cdr ranges)))
906 (throw 'got-ranges nil))
909 (setq article (nnmaildir--flist-art flist prefix))
912 (gnus-add-to-range ranges
913 `(,(nnmaildir--art-num article))))))
914 (funcall ls markdir nil "\\`[^.]" 'nosort)))
915 (if (eq mark-sym 'read) (setq read ranges)
916 (if ranges (setq marks (cons (cons mark-sym ranges) marks)))))
918 (gnus-info-set-read info read)
919 (gnus-info-set-marks info marks 'extend)
920 (setf (nnmaildir--grp-mmth group) new-mmth)
923 (defun nnmaildir-request-group (gname &optional server fast)
924 (let ((group (nnmaildir--prepare server gname))
926 (nnmaildir--with-nntp-buffer
930 (insert "411 no such news group\n")
931 (setf (nnmaildir--srv-error nnmaildir--cur-server)
932 (concat "No such group: " gname))
934 (setf (nnmaildir--srv-curgrp nnmaildir--cur-server) group)
935 (if fast (throw 'return t))
937 (princ (nnmaildir--grp-count group) nntp-server-buffer)
939 (princ (nnmaildir--grp-min group) nntp-server-buffer)
941 (princ (nnmaildir--group-maxnum nnmaildir--cur-server group)
943 (insert " " gname "\n")
946 (defun nnmaildir-request-create-group (gname &optional server args)
947 (nnmaildir--prepare server nil)
949 (let ((create-dir (nnmaildir--srv-create-dir nnmaildir--cur-server))
951 (when (zerop (length gname))
952 (setf (nnmaildir--srv-error nnmaildir--cur-server)
953 "Invalid (empty) group name")
955 (when (eq (aref "." 0) (aref gname 0))
956 (setf (nnmaildir--srv-error nnmaildir--cur-server)
957 "Group names may not start with \".\"")
959 (when (save-match-data (string-match "[\0/\t]" gname))
960 (setf (nnmaildir--srv-error nnmaildir--cur-server)
961 (concat "Illegal characters (null, tab, or /) in group name: "
964 (setq groups (nnmaildir--srv-groups nnmaildir--cur-server))
965 (when (intern-soft gname groups)
966 (setf (nnmaildir--srv-error nnmaildir--cur-server)
967 (concat "Group already exists: " gname))
969 (setq srv-dir (nnmaildir--srv-dir nnmaildir--cur-server))
970 (if (file-name-absolute-p create-dir)
971 (setq dir (expand-file-name create-dir))
973 dir (file-truename dir)
974 dir (concat dir create-dir)))
975 (setq dir (nnmaildir--subdir (file-name-as-directory dir) gname))
976 (nnmaildir--mkdir dir)
977 (nnmaildir--mkdir (nnmaildir--tmp dir))
978 (nnmaildir--mkdir (nnmaildir--new dir))
979 (nnmaildir--mkdir (nnmaildir--cur dir))
980 (setq create-dir (file-name-as-directory create-dir))
981 (make-symbolic-link (concat create-dir gname) (concat srv-dir gname))
982 (nnmaildir-request-scan 'find-new-groups))))
984 (defun nnmaildir-request-rename-group (gname new-name &optional server)
985 (let ((group (nnmaildir--prepare server gname))
986 (coding-system-for-write nnheader-file-coding-system)
987 (buffer-file-coding-system nil)
988 (file-coding-system-alist nil)
992 (setf (nnmaildir--srv-error nnmaildir--cur-server)
993 (concat "No such group: " gname))
995 (when (zerop (length new-name))
996 (setf (nnmaildir--srv-error nnmaildir--cur-server)
997 "Invalid (empty) group name")
999 (when (eq (aref "." 0) (aref new-name 0))
1000 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1001 "Group names may not start with \".\"")
1002 (throw 'return nil))
1003 (when (save-match-data (string-match "[\0/\t]" new-name))
1004 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1005 (concat "Illegal characters (null, tab, or /) in group name: "
1007 (throw 'return nil))
1008 (if (string-equal gname new-name) (throw 'return t))
1009 (when (intern-soft new-name
1010 (nnmaildir--srv-groups nnmaildir--cur-server))
1011 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1012 (concat "Group already exists: " new-name))
1013 (throw 'return nil))
1014 (setq srv-dir (nnmaildir--srv-dir nnmaildir--cur-server))
1016 (rename-file (concat srv-dir gname)
1017 (concat srv-dir new-name))
1019 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1020 (concat "Error renaming link: " (prin1-to-string err)))
1021 (throw 'return nil)))
1022 (setq x (nnmaildir--srv-groups nnmaildir--cur-server)
1023 groups (make-vector (length x) 0))
1024 (mapatoms (lambda (sym)
1025 (unless (eq (symbol-value sym) group)
1026 (set (intern (symbol-name sym) groups)
1027 (symbol-value sym))))
1029 (setq group (copy-sequence group))
1030 (setf (nnmaildir--grp-name group) new-name)
1031 (set (intern new-name groups) group)
1032 (setf (nnmaildir--srv-groups nnmaildir--cur-server) groups)
1035 (defun nnmaildir-request-delete-group (gname force &optional server)
1036 (let ((group (nnmaildir--prepare server gname))
1037 pgname grp-dir dir ls deactivate-mark)
1040 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1041 (concat "No such group: " gname))
1042 (throw 'return nil))
1043 (if (eq group (nnmaildir--srv-curgrp nnmaildir--cur-server))
1044 (setf (nnmaildir--srv-curgrp nnmaildir--cur-server) nil))
1045 (setq gname (nnmaildir--grp-name group)
1046 pgname (nnmaildir--pgname nnmaildir--cur-server gname))
1047 (unintern gname (nnmaildir--srv-groups nnmaildir--cur-server))
1048 (setq grp-dir (nnmaildir--srv-dir nnmaildir--cur-server)
1049 grp-dir (nnmaildir--srvgrp-dir grp-dir gname))
1050 (if (not force) (setq grp-dir (directory-file-name grp-dir))
1051 (setq ls (nnmaildir--group-ls nnmaildir--cur-server pgname))
1052 (if (nnmaildir--param pgname 'read-only)
1053 (progn (delete-directory (nnmaildir--tmp grp-dir))
1054 (nnmaildir--unlink (nnmaildir--new grp-dir))
1055 (delete-directory (nnmaildir--cur grp-dir)))
1056 (nnmaildir--delete-dir-files (nnmaildir--tmp grp-dir) ls)
1057 (nnmaildir--delete-dir-files (nnmaildir--new grp-dir) ls)
1058 (nnmaildir--delete-dir-files (nnmaildir--cur grp-dir) ls))
1059 (setq dir (nnmaildir--nndir grp-dir))
1060 (mapcar (lambda (subdir) (nnmaildir--delete-dir-files subdir ls))
1061 `(,(nnmaildir--nov-dir dir) ,(nnmaildir--num-dir dir)
1062 ,@(funcall ls (nnmaildir--marks-dir dir) 'full "\\`[^.]"
1064 (setq dir (nnmaildir--nndir grp-dir))
1065 (nnmaildir--unlink (concat dir "markfile"))
1066 (nnmaildir--unlink (concat dir "markfile{new}"))
1067 (delete-directory (nnmaildir--marks-dir dir))
1068 (delete-directory dir)
1069 (setq grp-dir (directory-file-name grp-dir)
1070 dir (car (file-attributes grp-dir)))
1071 (unless (eq (aref "/" 0) (aref dir 0))
1072 (setq dir (concat (file-truename
1073 (nnmaildir--srv-dir nnmaildir--cur-server))
1075 (delete-directory dir))
1076 (nnmaildir--unlink grp-dir)
1079 (defun nnmaildir-retrieve-headers (articles &optional gname server fetch-old)
1080 (let ((group (nnmaildir--prepare server gname))
1081 srv-dir dir nlist mlist article num start stop nov nlist2 insert-nov
1085 (setq nov (nnmaildir--update-nov nnmaildir--cur-server group
1088 (nnmaildir--cache-nov group article nov)
1089 (setq num (nnmaildir--art-num article))
1090 (princ num nntp-server-buffer)
1091 (insert "\t" (nnmaildir--nov-get-beg nov) "\t"
1092 (nnmaildir--art-msgid article) "\t"
1093 (nnmaildir--nov-get-mid nov) "\tXref: nnmaildir "
1095 (princ num nntp-server-buffer)
1096 (insert "\t" (nnmaildir--nov-get-end nov) "\n"))))
1099 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1100 (if gname (concat "No such group: " gname) "No current group"))
1101 (throw 'return nil))
1102 (nnmaildir--with-nntp-buffer
1104 (setq mlist (nnmaildir--grp-mlist group)
1105 nlist (nnmaildir--grp-nlist group)
1106 gname (nnmaildir--grp-name group)
1107 srv-dir (nnmaildir--srv-dir nnmaildir--cur-server)
1108 dir (nnmaildir--srvgrp-dir srv-dir gname))
1111 ((and fetch-old (not (numberp fetch-old)))
1112 (nnmaildir--nlist-iterate nlist 'all insert-nov))
1114 ((stringp (car articles))
1117 (setq article (nnmaildir--mlist-art mlist msgid))
1118 (if article (funcall insert-nov article)))
1122 ;; Assume the article range list is sorted ascending
1123 (setq stop (car articles)
1124 start (car (last articles))
1125 stop (if (numberp stop) stop (car stop))
1126 start (if (numberp start) start (cdr start))
1127 stop (- stop fetch-old)
1128 stop (if (< stop 1) 1 stop)
1129 articles (list (cons stop start))))
1130 (nnmaildir--nlist-iterate nlist articles insert-nov)))
1131 (sort-numeric-fields 1 (point-min) (point-max))
1134 (defun nnmaildir-request-article (num-msgid &optional gname server to-buffer)
1135 (let ((group (nnmaildir--prepare server gname))
1136 (case-fold-search t)
1137 list article dir pgname deactivate-mark)
1140 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1141 (if gname (concat "No such group: " gname) "No current group"))
1142 (throw 'return nil))
1143 (if (numberp num-msgid)
1144 (setq article (nnmaildir--nlist-art group num-msgid))
1145 (setq list (nnmaildir--grp-mlist group)
1146 article (nnmaildir--mlist-art list num-msgid))
1147 (if article (setq num-msgid (nnmaildir--art-num article))
1151 (setq group (symbol-value group-sym)
1152 list (nnmaildir--grp-mlist group)
1153 article (nnmaildir--mlist-art list num-msgid))
1155 (setq num-msgid (nnmaildir--art-num article))
1156 (throw 'found nil)))
1157 (nnmaildir--srv-groups nnmaildir--cur-server))))
1159 (setf (nnmaildir--srv-error nnmaildir--cur-server) "No such article")
1160 (throw 'return nil)))
1161 (setq gname (nnmaildir--grp-name group)
1162 pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1163 dir (nnmaildir--srv-dir nnmaildir--cur-server)
1164 dir (nnmaildir--srvgrp-dir dir gname)
1165 dir (if (nnmaildir--param pgname 'read-only)
1166 (nnmaildir--new dir) (nnmaildir--cur dir))
1167 nnmaildir-article-file-name
1169 (nnmaildir--art-prefix article)
1170 (nnmaildir--art-suffix article)))
1171 (unless (file-exists-p nnmaildir-article-file-name)
1172 (nnmaildir--expired-article group article)
1173 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1174 "Article has expired")
1175 (throw 'return nil))
1177 (set-buffer (or to-buffer nntp-server-buffer))
1179 (nnheader-insert-file-contents nnmaildir-article-file-name))
1180 (cons gname num-msgid))))
1182 (defun nnmaildir-request-post (&optional server)
1183 (let (message-required-mail-headers)
1184 (funcall message-send-mail-function)))
1186 (defun nnmaildir-request-replace-article (number gname buffer)
1187 (let ((group (nnmaildir--prepare nil gname))
1188 (coding-system-for-write nnheader-file-coding-system)
1189 (buffer-file-coding-system nil)
1190 (file-coding-system-alist nil)
1191 dir file article suffix tmpfile deactivate-mark)
1194 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1195 (concat "No such group: " gname))
1196 (throw 'return nil))
1197 (when (nnmaildir--param (nnmaildir--pgname nnmaildir--cur-server gname)
1199 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1200 (concat "Read-only group: " group))
1201 (throw 'return nil))
1202 (setq dir (nnmaildir--srv-dir nnmaildir--cur-server)
1203 dir (nnmaildir--srvgrp-dir dir gname)
1204 article (nnmaildir--nlist-art group number))
1206 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1207 (concat "No such article: " (number-to-string number)))
1208 (throw 'return nil))
1209 (setq suffix (nnmaildir--art-suffix article)
1210 file (nnmaildir--art-prefix article)
1211 tmpfile (concat (nnmaildir--tmp dir) file))
1212 (when (file-exists-p tmpfile)
1213 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1214 (concat "File exists: " tmpfile))
1215 (throw 'return nil))
1218 (write-region (point-min) (point-max) tmpfile nil 'no-message nil
1220 (unix-sync) ;; no fsync :(
1221 (rename-file tmpfile (concat (nnmaildir--cur dir) file suffix) 'replace)
1224 (defun nnmaildir-request-move-article (article gname server accept-form
1226 (let ((group (nnmaildir--prepare server gname))
1227 pgname suffix result nnmaildir--file deactivate-mark)
1230 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1231 (concat "No such group: " gname))
1232 (throw 'return nil))
1233 (setq gname (nnmaildir--grp-name group)
1234 pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1235 article (nnmaildir--nlist-art group article))
1237 (setf (nnmaildir--srv-error nnmaildir--cur-server) "No such article")
1238 (throw 'return nil))
1239 (setq suffix (nnmaildir--art-suffix article)
1240 nnmaildir--file (nnmaildir--srv-dir nnmaildir--cur-server)
1241 nnmaildir--file (nnmaildir--srvgrp-dir nnmaildir--file gname)
1242 nnmaildir--file (if (nnmaildir--param pgname 'read-only)
1243 (nnmaildir--new nnmaildir--file)
1244 (nnmaildir--cur nnmaildir--file))
1245 nnmaildir--file (concat nnmaildir--file
1246 (nnmaildir--art-prefix article)
1248 (unless (file-exists-p nnmaildir--file)
1249 (nnmaildir--expired-article group article)
1250 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1251 "Article has expired")
1252 (throw 'return nil))
1253 (nnmaildir--with-move-buffer
1255 (nnheader-insert-file-contents nnmaildir--file)
1256 (setq result (eval accept-form)))
1257 (unless (or (null result) (nnmaildir--param pgname 'read-only))
1258 (nnmaildir--unlink nnmaildir--file)
1259 (nnmaildir--expired-article group article))
1262 (defun nnmaildir-request-accept-article (gname &optional server last)
1263 (let ((group (nnmaildir--prepare server gname))
1264 (coding-system-for-write nnheader-file-coding-system)
1265 (buffer-file-coding-system nil)
1266 (file-coding-system-alist nil)
1267 srv-dir dir file tmpfile curfile 24h article)
1270 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1271 (concat "No such group: " gname))
1272 (throw 'return nil))
1273 (setq gname (nnmaildir--grp-name group))
1274 (when (nnmaildir--param (nnmaildir--pgname nnmaildir--cur-server gname)
1276 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1277 (concat "Read-only group: " gname))
1278 (throw 'return nil))
1279 (setq srv-dir (nnmaildir--srv-dir nnmaildir--cur-server)
1280 dir (nnmaildir--srvgrp-dir srv-dir gname)
1281 file (format-time-string "%s" nil))
1282 (unless (string-equal nnmaildir--delivery-time file)
1283 (setq nnmaildir--delivery-time file
1284 nnmaildir--delivery-ct 0))
1285 (setq file (concat file "." nnmaildir--delivery-pid))
1286 (unless (zerop nnmaildir--delivery-ct)
1287 (setq file (concat file "_"
1288 (number-to-string nnmaildir--delivery-ct))))
1289 (setq file (concat file "." (system-name))
1290 tmpfile (concat (nnmaildir--tmp dir) file)
1291 curfile (concat (nnmaildir--cur dir) file ":2,"))
1292 (when (file-exists-p tmpfile)
1293 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1294 (concat "File exists: " tmpfile))
1295 (throw 'return nil))
1296 (when (file-exists-p curfile)
1297 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1298 (concat "File exists: " curfile))
1299 (throw 'return nil))
1300 (setq nnmaildir--delivery-ct (1+ nnmaildir--delivery-ct)
1301 24h (run-with-timer 86400 nil
1303 (nnmaildir--unlink tmpfile)
1304 (setf (nnmaildir--srv-error
1305 nnmaildir--cur-server)
1306 "24-hour timer expired")
1307 (throw 'return nil))))
1309 (add-name-to-file nnmaildir--file tmpfile)
1311 (write-region (point-min) (point-max) tmpfile nil 'no-message nil
1313 (unix-sync))) ;; no fsync :(
1316 (add-name-to-file tmpfile curfile)
1318 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1319 (concat "Error linking: " (prin1-to-string err)))
1320 (nnmaildir--unlink tmpfile)
1321 (throw 'return nil)))
1322 (nnmaildir--unlink tmpfile)
1323 (setq article (make-nnmaildir--art :prefix file :suffix ":2,"))
1324 (if (nnmaildir--grp-add-art nnmaildir--cur-server group article)
1325 (cons gname (nnmaildir--art-num article))))))
1327 (defun nnmaildir-save-mail (group-art)
1330 (throw 'return nil))
1331 (let (ga gname x groups nnmaildir--file deactivate-mark)
1333 (goto-char (point-min))
1335 (while (looking-at "From ")
1336 (replace-match "X-From-Line: ")
1338 (setq groups (nnmaildir--srv-groups nnmaildir--cur-server)
1339 ga (car group-art) group-art (cdr group-art)
1341 (or (intern-soft gname groups)
1342 (nnmaildir-request-create-group gname)
1343 (throw 'return nil)) ;; not that nnmail bothers to check :(
1344 (unless (nnmaildir-request-accept-article gname)
1345 (throw 'return nil))
1346 (setq nnmaildir--file (nnmaildir--srv-dir nnmaildir--cur-server)
1347 nnmaildir--file (nnmaildir--srvgrp-dir nnmaildir--file gname)
1348 x (nnmaildir--prepare nil gname)
1349 x (nnmaildir--grp-nlist x)
1351 nnmaildir--file (concat nnmaildir--file
1352 (nnmaildir--art-prefix x)
1353 (nnmaildir--art-suffix x)))
1357 (setq gname (car ga))
1358 (and (or (intern-soft gname groups)
1359 (nnmaildir-request-create-group gname))
1360 (nnmaildir-request-accept-article gname)
1364 (defun nnmaildir-active-number (gname)
1367 (defun nnmaildir-request-expire-articles (ranges &optional gname server force)
1368 (let ((no-force (not force))
1369 (group (nnmaildir--prepare server gname))
1370 pgname time boundary bound-iter high low target dir nlist nlist2
1371 stop article didnt nnmaildir--file nnmaildir-article-file-name
1375 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1376 (if gname (concat "No such group: " gname) "No current group"))
1377 (throw 'return (gnus-uncompress-range ranges)))
1378 (setq gname (nnmaildir--grp-name group)
1379 pgname (nnmaildir--pgname nnmaildir--cur-server gname))
1380 (if (nnmaildir--param pgname 'read-only)
1381 (throw 'return (gnus-uncompress-range ranges)))
1382 (setq time (nnmaildir--param pgname 'expire-age))
1384 (setq time (or (and nnmail-expiry-wait-function
1385 (funcall nnmail-expiry-wait-function gname))
1386 nnmail-expiry-wait))
1387 (if (eq time 'immediate)
1390 (setq time (* time 86400)))))
1392 (unless (integerp time) ;; handle 'never
1393 (throw 'return (gnus-uncompress-range ranges)))
1394 (setq boundary (current-time)
1395 high (- (car boundary) (/ time 65536))
1396 low (- (cadr boundary) (% time 65536)))
1398 (setq low (+ low 65536)
1400 (setcar (cdr boundary) low)
1401 (setcar boundary high))
1402 (setq dir (nnmaildir--srv-dir nnmaildir--cur-server)
1403 dir (nnmaildir--srvgrp-dir dir gname)
1404 dir (nnmaildir--cur dir)
1405 nlist (nnmaildir--grp-nlist group)
1406 ranges (reverse ranges))
1407 (nnmaildir--with-move-buffer
1408 (nnmaildir--nlist-iterate
1411 (setq nnmaildir--file (nnmaildir--art-prefix article)
1412 nnmaildir--file (concat dir nnmaildir--file
1413 (nnmaildir--art-suffix article))
1414 time (file-attributes nnmaildir--file))
1417 (nnmaildir--expired-article group article))
1420 (setq time (nth 5 time)
1421 bound-iter boundary)
1422 (while (and bound-iter time
1423 (= (car bound-iter) (car time)))
1424 (setq bound-iter (cdr bound-iter)
1426 (and bound-iter time
1427 (car-less-than-car bound-iter time))))
1428 (setq didnt (cons (nnmaildir--art-num article) didnt)))
1430 (setq nnmaildir-article-file-name nnmaildir--file
1431 target (if force nil
1434 (nnmaildir--param pgname 'expire-group)))))
1435 (when (and (stringp target)
1436 (not (string-equal target pgname))) ;; Move it.
1438 (nnheader-insert-file-contents nnmaildir--file)
1439 (gnus-request-accept-article target nil nil 'no-encode))
1440 (if (equal target pgname)
1442 (setq didnt (cons (nnmaildir--art-num article) didnt))
1443 (nnmaildir--unlink nnmaildir--file)
1444 (nnmaildir--expired-article group article))))))
1448 (defun nnmaildir-request-set-mark (gname actions &optional server)
1449 (let ((group (nnmaildir--prepare server gname))
1450 (coding-system-for-write nnheader-file-coding-system)
1451 (buffer-file-coding-system nil)
1452 (file-coding-system-alist nil)
1453 del-mark del-action add-action set-action marksdir markfile nlist
1454 ranges begin end article all-marks todo-marks did-marks mdir mfile
1455 pgname ls markfilenew deactivate-mark)
1458 (setq mfile (nnmaildir--subdir marksdir (symbol-name mark))
1459 mfile (concat mfile (nnmaildir--art-prefix article)))
1460 (nnmaildir--unlink mfile))
1461 del-action (lambda (article) (mapcar del-mark todo-marks))
1466 (setq mdir (nnmaildir--subdir marksdir (symbol-name mark))
1467 mfile (concat mdir (nnmaildir--art-prefix article)))
1468 (unless (memq mark did-marks)
1469 (nnmaildir--mkdir mdir)
1470 (setq did-marks (cons mark did-marks)))
1471 (unless (file-exists-p mfile)
1473 (add-name-to-file markfile mfile)
1475 (unless (file-exists-p mfile)
1476 ;; too many links, maybe
1477 (write-region "" nil markfilenew nil 'no-message)
1478 (add-name-to-file markfilenew mfile
1479 'ok-if-already-exists)
1480 (rename-file markfilenew markfile 'replace))))))
1482 set-action (lambda (article)
1483 (funcall add-action)
1484 (mapcar (lambda (mark)
1485 (unless (memq mark todo-marks)
1486 (funcall del-mark mark)))
1490 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1491 (concat "No such group: " gname))
1492 (mapcar (lambda (action)
1493 (setq ranges (gnus-range-add ranges (car action))))
1495 (throw 'return ranges))
1496 (setq nlist (nnmaildir--grp-nlist group)
1497 marksdir (nnmaildir--srv-dir nnmaildir--cur-server)
1498 marksdir (nnmaildir--srvgrp-dir marksdir gname)
1499 marksdir (nnmaildir--nndir marksdir)
1500 markfile (concat marksdir "markfile")
1501 markfilenew (concat markfile "{new}")
1502 marksdir (nnmaildir--marks-dir marksdir)
1503 gname (nnmaildir--grp-name group)
1504 pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1505 ls (nnmaildir--group-ls nnmaildir--cur-server pgname)
1506 all-marks (funcall ls marksdir nil "\\`[^.]" 'nosort)
1507 all-marks (mapcar 'intern all-marks))
1510 (setq ranges (car action)
1511 todo-marks (caddr action))
1512 (mapcar (lambda (mark) (add-to-list 'all-marks mark)) todo-marks)
1513 (if (numberp (cdr ranges)) (setq ranges (list ranges)))
1514 (nnmaildir--nlist-iterate nlist ranges
1515 (cond ((eq 'del (cadr action)) del-action)
1516 ((eq 'add (cadr action)) add-action)
1521 (defun nnmaildir-close-group (gname &optional server)
1522 (let ((group (nnmaildir--prepare server gname))
1523 pgname ls dir msgdir files flist dirs)
1526 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1527 (concat "No such group: " gname))
1529 (setq pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1530 ls (nnmaildir--group-ls nnmaildir--cur-server pgname)
1531 dir (nnmaildir--srv-dir nnmaildir--cur-server)
1532 dir (nnmaildir--srvgrp-dir dir gname)
1533 msgdir (if (nnmaildir--param pgname 'read-only)
1534 (nnmaildir--new dir) (nnmaildir--cur dir))
1535 dir (nnmaildir--nndir dir)
1536 dirs (cons (nnmaildir--nov-dir dir)
1537 (funcall ls (nnmaildir--marks-dir dir) 'full "\\`[^.]"
1541 (cons dir (funcall ls dir nil "\\`[^.]" 'nosort)))
1543 files (funcall ls msgdir nil "\\`[^.]" 'nosort)
1544 flist (nnmaildir--up2-1 (length files))
1545 flist (make-vector flist 0))
1549 (string-match "\\`\\([^:]*\\)\\(:.*\\)?\\'" file)
1550 (intern (match-string 1 file) flist))
1554 (setq files (cdr dir)
1555 dir (file-name-as-directory (car dir)))
1558 (unless (intern-soft file flist)
1559 (setq file (concat dir file))
1560 (delete-file file)))
1565 (defun nnmaildir-close-server (&optional server)
1566 (let (flist ls dirs dir files file x)
1567 (nnmaildir--prepare server nil)
1568 (when nnmaildir--cur-server
1569 (setq server nnmaildir--cur-server
1570 nnmaildir--cur-server nil)
1571 (unintern (nnmaildir--srv-address server) nnmaildir--servers)))
1574 (defun nnmaildir-request-close ()
1575 (let (servers buffer)
1576 (mapatoms (lambda (server)
1577 (setq servers (cons (symbol-name server) servers)))
1579 (mapcar 'nnmaildir-close-server servers)
1580 (setq buffer (get-buffer " *nnmaildir work*"))
1581 (if buffer (kill-buffer buffer))
1582 (setq buffer (get-buffer " *nnmaildir nov*"))
1583 (if buffer (kill-buffer buffer))
1584 (setq buffer (get-buffer " *nnmaildir move*"))
1585 (if buffer (kill-buffer buffer)))
1588 (provide 'nnmaildir)
1591 ;; indent-tabs-mode: t
1595 ;;; nnmaildir.el ends here