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 ;; * Add a hook for when moving messages from new/ to cur/, to support
45 ;; nnmail's duplicate detection.
46 ;; * Improve generated Xrefs, so crossposts are detectable.
47 ;; * Improve code readability.
51 ;; eval this before editing
53 (put 'nnmaildir--with-nntp-buffer 'lisp-indent-function 0)
54 (put 'nnmaildir--with-work-buffer 'lisp-indent-function 0)
55 (put 'nnmaildir--with-nov-buffer 'lisp-indent-function 0)
56 (put 'nnmaildir--with-move-buffer 'lisp-indent-function 0)
72 (defconst nnmaildir-version "Gnus")
74 (defvar nnmaildir-article-file-name nil
75 "*The filename of the most recently requested article. This variable is set
76 by nnmaildir-request-article.")
78 ;; The filename of the article being moved/copied:
79 (defvar nnmaildir--file nil)
81 ;; Variables to generate filenames of messages being delivered:
82 (defvar nnmaildir--delivery-time "")
83 (defconst nnmaildir--delivery-pid (concat "P" (number-to-string (emacs-pid))))
84 (defvar nnmaildir--delivery-count nil)
86 ;; An obarry containing symbols whose names are server names and whose values
88 (defvar nnmaildir--servers (make-vector 3 0))
89 ;; The current server:
90 (defvar nnmaildir--cur-server nil)
92 ;; A copy of nnmail-extra-headers
93 (defvar nnmaildir--extra nil)
95 ;; A NOV structure looks like this (must be prin1-able, so no defstruct):
96 ["subject\tfrom\tdate"
97 "references\tchars\lines"
98 "To: you\tIn-Reply-To: <your.mess@ge>"
99 (12345 67890) ;; modtime of the corresponding article file
100 (to in-reply-to)] ;; contemporary value of nnmail-extra-headers
101 (defconst nnmaildir--novlen 5)
102 (defmacro nnmaildir--nov-new (beg mid end mtime extra)
103 `(vector ,beg ,mid ,end ,mtime ,extra))
104 (defmacro nnmaildir--nov-get-beg (nov) `(aref ,nov 0))
105 (defmacro nnmaildir--nov-get-mid (nov) `(aref ,nov 1))
106 (defmacro nnmaildir--nov-get-end (nov) `(aref ,nov 2))
107 (defmacro nnmaildir--nov-get-mtime (nov) `(aref ,nov 3))
108 (defmacro nnmaildir--nov-get-extra (nov) `(aref ,nov 4))
109 (defmacro nnmaildir--nov-set-beg (nov value) `(aset ,nov 0 ,value))
110 (defmacro nnmaildir--nov-set-mid (nov value) `(aset ,nov 1 ,value))
111 (defmacro nnmaildir--nov-set-end (nov value) `(aset ,nov 2 ,value))
112 (defmacro nnmaildir--nov-set-mtime (nov value) `(aset ,nov 3 ,value))
113 (defmacro nnmaildir--nov-set-extra (nov value) `(aset ,nov 4 ,value))
115 (defstruct nnmaildir--art
116 (prefix nil :type string) ;; "time.pid.host"
117 (suffix nil :type string) ;; ":2,flags"
118 (num nil :type natnum) ;; article number
119 (msgid nil :type string) ;; "<mess.age@id>"
120 (nov nil :type vector)) ;; cached nov structure, or nil
122 (defstruct nnmaildir--grp
123 (name nil :type string) ;; "group.name"
124 (new nil :type list) ;; new/ modtime
125 (cur nil :type list) ;; cur/ modtime
126 (min 1 :type natnum) ;; minimum article number
127 (count 0 :type natnum) ;; count of articles
128 (nlist nil :type list) ;; list of articles, ordered descending by number
129 (flist nil :type vector) ;; obarray mapping filename prefix->article
130 (mlist nil :type vector) ;; obarray mapping message-id->article
131 (cache nil :type vector) ;; nov cache
132 (index nil :type natnum) ;; index of next cache entry to replace
133 (mmth nil :type vector)) ;; obarray mapping mark name->dir modtime
134 ; ("Mark Mod Time Hash")
136 (defstruct nnmaildir--srv
137 (address nil :type string) ;; server address string
138 (method nil :type list) ;; (nnmaildir "address" ...)
139 (prefix nil :type string) ;; "nnmaildir+address:"
140 (dir nil :type string) ;; "/expanded/path/to/server/dir/"
141 (ls nil :type function) ;; directory-files function
142 (groups nil :type vector) ;; obarray mapping group name->group
143 (curgrp nil :type nnmaildir--grp) ;; current group, or nil
144 (error nil :type string) ;; last error message, or nil
145 (mtime nil :type list) ;; modtime of dir
146 (gnm nil) ;; flag: split from mail-sources?
147 (target-prefix nil :type string)) ;; symlink target prefix
149 (defun nnmaildir--expired-article (group article)
150 (setf (nnmaildir--art-nov article) nil)
151 (let ((flist (nnmaildir--grp-flist group))
152 (mlist (nnmaildir--grp-mlist group))
153 (min (nnmaildir--grp-min group))
154 (count (1- (nnmaildir--grp-count group)))
155 (prefix (nnmaildir--art-prefix article))
156 (msgid (nnmaildir--art-msgid article))
158 (nlist-pre '(nil . nil))
160 (unless (zerop count)
161 (setq nlist-post (nnmaildir--grp-nlist group)
162 num (nnmaildir--art-num article))
163 (if (eq num (caar nlist-post))
164 (setq new-nlist (cdr nlist-post))
165 (setq new-nlist nlist-post
167 nlist-post (cdr nlist-post))
168 (while (/= num (caar nlist-post))
169 (setq nlist-pre nlist-post
170 nlist-post (cdr nlist-post)))
171 (setq nlist-post (cdr nlist-post))
173 (setq min (caar nlist-pre)))))
174 (let ((inhibit-quit t))
175 (setf (nnmaildir--grp-min group) min)
176 (setf (nnmaildir--grp-count group) count)
177 (setf (nnmaildir--grp-nlist group) new-nlist)
178 (setcdr nlist-pre nlist-post)
179 (unintern prefix flist)
180 (unintern msgid mlist))))
182 (defun nnmaildir--nlist-art (group num)
183 (let ((entry (assq num (nnmaildir--grp-nlist group))))
186 (defmacro nnmaildir--flist-art (list file)
187 `(symbol-value (intern-soft ,file ,list)))
188 (defmacro nnmaildir--mlist-art (list msgid)
189 `(symbol-value (intern-soft ,msgid ,list)))
191 (defun nnmaildir--pgname (server gname)
192 (let ((prefix (nnmaildir--srv-prefix server)))
193 (if prefix (concat prefix gname)
194 (setq gname (gnus-group-prefixed-name gname
195 (nnmaildir--srv-method server)))
196 (setf (nnmaildir--srv-prefix server) (gnus-group-real-prefix gname))
199 (defun nnmaildir--param (pgname param)
200 (setq param (gnus-group-find-parameter pgname param 'allow-list))
201 (if (vectorp param) (setq param (aref param 0)))
204 (defmacro nnmaildir--with-nntp-buffer (&rest body)
206 (set-buffer nntp-server-buffer)
208 (defmacro nnmaildir--with-work-buffer (&rest body)
210 (set-buffer (get-buffer-create " *nnmaildir work*"))
212 (defmacro nnmaildir--with-nov-buffer (&rest body)
214 (set-buffer (get-buffer-create " *nnmaildir nov*"))
216 (defmacro nnmaildir--with-move-buffer (&rest body)
218 (set-buffer (get-buffer-create " *nnmaildir move*"))
221 (defmacro nnmaildir--subdir (dir subdir)
222 `(file-name-as-directory (concat ,dir ,subdir)))
223 (defmacro nnmaildir--srvgrp-dir (srv-dir gname)
224 `(nnmaildir--subdir ,srv-dir ,gname))
225 (defmacro nnmaildir--tmp (dir) `(nnmaildir--subdir ,dir "tmp"))
226 (defmacro nnmaildir--new (dir) `(nnmaildir--subdir ,dir "new"))
227 (defmacro nnmaildir--cur (dir) `(nnmaildir--subdir ,dir "cur"))
228 (defmacro nnmaildir--nndir (dir) `(nnmaildir--subdir ,dir ".nnmaildir"))
229 (defmacro nnmaildir--nov-dir (dir) `(nnmaildir--subdir ,dir "nov"))
230 (defmacro nnmaildir--marks-dir (dir) `(nnmaildir--subdir ,dir "marks"))
231 (defmacro nnmaildir--num-dir (dir) `(nnmaildir--subdir ,dir "num"))
232 (defmacro nnmaildir--num-file (dir) `(concat ,dir ":"))
234 (defmacro nnmaildir--unlink (file-arg)
235 `(let ((file ,file-arg))
236 (if (file-attributes file) (delete-file file))))
237 (defun nnmaildir--mkdir (dir)
238 (or (file-exists-p (file-name-as-directory dir))
239 (make-directory-internal (directory-file-name dir))))
240 (defun nnmaildir--delete-dir-files (dir ls)
241 (when (file-attributes dir)
242 (mapcar 'delete-file (funcall ls dir 'full "\\`[^.]" 'nosort))
243 (delete-directory dir)))
245 (defun nnmaildir--group-maxnum (server group)
246 (if (zerop (nnmaildir--grp-count group)) 0
247 (let ((x (nnmaildir--srvgrp-dir (nnmaildir--srv-dir server)
248 (nnmaildir--grp-name group))))
249 (setq x (nnmaildir--nndir x)
250 x (nnmaildir--num-dir x)
251 x (nnmaildir--num-file x)
252 x (file-attributes x))
253 (if x (1- (nth 1 x)) 0))))
255 ;; Make the given server, if non-nil, be the current server. Then make the
256 ;; given group, if non-nil, be the current group of the current server. Then
257 ;; return the group object for the current group.
258 (defun nnmaildir--prepare (server group)
262 (unless (setq server nnmaildir--cur-server)
264 (unless (setq server (intern-soft server nnmaildir--servers))
266 (setq server (symbol-value server)
267 nnmaildir--cur-server server))
268 (unless (setq groups (nnmaildir--srv-groups server))
270 (unless (nnmaildir--srv-method server)
271 (setq x (concat "nnmaildir:" (nnmaildir--srv-address server))
272 x (gnus-server-to-method x))
273 (unless x (throw 'return nil))
274 (setf (nnmaildir--srv-method server) x))
276 (unless (setq group (nnmaildir--srv-curgrp server))
278 (unless (setq group (intern-soft group groups))
280 (setq group (symbol-value group)))
283 (defun nnmaildir--tab-to-space (string)
285 (while (string-match "\t" string pos)
286 (aset string (match-beginning 0) ? )
287 (setq pos (match-end 0))))
290 (defun nnmaildir--update-nov (server group article)
291 (let ((nnheader-file-coding-system 'binary)
292 (srv-dir (nnmaildir--srv-dir server))
293 (storage-version 1) ;; [version article-number msgid [...nov...]]
294 dir gname pgname msgdir prefix suffix file attr mtime novdir novfile
295 nov msgid nov-beg nov-mid nov-end field val old-extra num numdir
298 (setq gname (nnmaildir--grp-name group)
299 pgname (nnmaildir--pgname server gname)
300 dir (nnmaildir--srvgrp-dir srv-dir gname)
301 msgdir (if (nnmaildir--param pgname 'read-only)
302 (nnmaildir--new dir) (nnmaildir--cur dir))
303 prefix (nnmaildir--art-prefix article)
304 suffix (nnmaildir--art-suffix article)
305 file (concat msgdir prefix suffix)
306 attr (file-attributes file))
308 (nnmaildir--expired-article group article)
310 (setq mtime (nth 5 attr)
312 nov (nnmaildir--art-nov article)
313 dir (nnmaildir--nndir dir)
314 novdir (nnmaildir--nov-dir dir)
315 novfile (concat novdir prefix))
316 (unless (equal nnmaildir--extra nnmail-extra-headers)
317 (setq nnmaildir--extra (copy-sequence nnmail-extra-headers)))
318 (nnmaildir--with-nov-buffer
319 ;; First we'll check for already-parsed NOV data.
320 (cond ((not (file-exists-p novfile))
321 ;; The NOV file doesn't exist; we have to parse the message.
324 ;; The file exists, but the data isn't in memory; read the file.
326 (nnheader-insert-file-contents novfile)
327 (setq nov (read (current-buffer)))
328 (if (not (and (vectorp nov)
330 (equal storage-version (aref nov 0))))
331 ;; This NOV data seems to be in the wrong format.
333 (unless (nnmaildir--art-num article)
334 (setf (nnmaildir--art-num article) (aref nov 1)))
335 (unless (nnmaildir--art-msgid article)
336 (setf (nnmaildir--art-msgid article) (aref nov 2)))
337 (setq nov (aref nov 3)))))
338 ;; Now check whether the already-parsed data (if we have any) is
339 ;; usable: if the message has been edited or if nnmail-extra-headers
340 ;; has been augmented since this data was parsed from the message,
341 ;; then we have to reparse. Otherwise it's up-to-date.
342 (when (and nov (equal mtime (nnmaildir--nov-get-mtime nov)))
343 ;; The timestamp matches. Now check nnmail-extra-headers.
344 (setq old-extra (nnmaildir--nov-get-extra nov))
345 (when (equal nnmaildir--extra old-extra) ;; common case
346 ;; Save memory; use a single copy of the list value.
347 (nnmaildir--nov-set-extra nov nnmaildir--extra)
349 ;; They're not equal, but maybe the new is a subset of the old.
350 (if (null nnmaildir--extra)
351 ;; The empty set is a subset of every set.
353 (if (not (memq nil (mapcar (lambda (e) (memq e old-extra))
355 (throw 'return nov)))
356 ;; Parse the NOV data out of the message.
358 (nnheader-insert-file-contents file)
360 (goto-char (point-min))
362 (if (search-forward "\n\n" nil 'noerror)
364 (setq nov-mid (count-lines (point) (point-max)))
365 (narrow-to-region (point-min) (1- (point))))
367 (goto-char (point-min))
369 (setq nov (nnheader-parse-naked-head)
370 field (or (mail-header-lines nov) 0)))
371 (unless (or (zerop field) (nnmaildir--param pgname 'distrust-Lines:))
372 (setq nov-mid field))
373 (setq nov-mid (number-to-string nov-mid)
374 nov-mid (concat (number-to-string attr) "\t" nov-mid))
376 (setq field (or (mail-header-references nov) ""))
377 (nnmaildir--tab-to-space field)
378 (setq nov-mid (concat field "\t" nov-mid)
380 (lambda (f) (nnmaildir--tab-to-space (or f "")))
381 (list (mail-header-subject nov)
382 (mail-header-from nov)
383 (mail-header-date nov)) "\t")
386 (setq field (symbol-name (car extra))
388 (nnmaildir--tab-to-space field)
389 (nnmaildir--tab-to-space val)
390 (concat field ": " val))
391 (mail-header-extra nov) "\t")))
392 (setq msgid (mail-header-id nov))
393 (if (or (null msgid) (nnheader-fake-message-id-p msgid))
394 (setq msgid (concat "<" prefix "@nnmaildir>")))
395 (nnmaildir--tab-to-space msgid)
396 ;; The data is parsed; create an nnmaildir NOV structure.
397 (setq nov (nnmaildir--nov-new nov-beg nov-mid nov-end mtime
399 num (nnmaildir--art-num article))
401 ;; Allocate a new article number.
403 (setq numdir (nnmaildir--num-dir dir)
404 file (nnmaildir--num-file numdir)
406 (nnmaildir--mkdir numdir)
407 (write-region "" nil file nil 'no-message)
409 ;; Get the number of links to file.
410 (setq attr (nth 1 (file-attributes file)))
412 ;; We've already tried this number, in the previous loop
413 ;; iteration, and failed.
414 (signal 'error `("Corrupt internal nnmaildir data" ,numdir)))
415 ;; If attr is 123, try to link file to "123". This atomically
416 ;; increases the link count and creates the "123" link, failing
417 ;; if that link was already created by another Gnus, just after
421 (add-name-to-file file (concat numdir (format "%x" attr)))
422 (setq file nil)) ;; Stop looping.
423 (file-already-exists nil))
425 (setf (nnmaildir--art-num article) num))
426 ;; Store this new NOV data in a file
428 (prin1 (vector storage-version num msgid nov) (current-buffer))
429 (setq file (concat novfile ":"))
430 (nnmaildir--unlink file)
431 (write-region (point-min) (point-max) file nil 'no-message nil 'excl))
432 (rename-file file novfile 'replace)
433 (setf (nnmaildir--art-msgid article) msgid)
436 (defun nnmaildir--cache-nov (group article nov)
437 (let ((cache (nnmaildir--grp-cache group))
438 (index (nnmaildir--grp-index group))
440 (unless (nnmaildir--art-nov article)
441 (setq goner (aref cache index))
442 (if goner (setf (nnmaildir--art-nov goner) nil))
443 (aset cache index article)
444 (setf (nnmaildir--grp-index group) (% (1+ index) (length cache))))
445 (setf (nnmaildir--art-nov article) nov)))
447 (defun nnmaildir--grp-add-art (server group article)
448 (let ((nov (nnmaildir--update-nov server group article))
449 count num min nlist nlist-cdr insert-nlist)
451 (setq count (1+ (nnmaildir--grp-count group))
452 num (nnmaildir--art-num article)
453 min (if (= count 1) num
454 (min num (nnmaildir--grp-min group)))
455 nlist (nnmaildir--grp-nlist group))
456 (if (or (null nlist) (> num (caar nlist)))
457 (setq nlist (cons (cons num article) nlist))
459 nlist-cdr (cdr nlist))
460 (while (and nlist-cdr (< num (caar nlist-cdr)))
461 (setq nlist nlist-cdr
462 nlist-cdr (cdr nlist))))
463 (let ((inhibit-quit t))
464 (setf (nnmaildir--grp-count group) count)
465 (setf (nnmaildir--grp-min group) min)
467 (setcdr nlist (cons (cons num article) nlist-cdr))
468 (setf (nnmaildir--grp-nlist group) nlist))
469 (set (intern (nnmaildir--art-prefix article)