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 ;; * Replace create-directory with target-prefix, so the maildirs can be in
45 ;; the same directory as the symlinks, starting with, e.g., ".".
46 ;; * Add a hook for when moving messages from new/ to cur/, to support
47 ;; nnmail's duplicate detection.
48 ;; * Allow each mark directory in a group to have its own inode for mark
49 ;; files, to accommodate AFS.
50 ;; * Improve generated Xrefs, so crossposts are detectable.
51 ;; * Improve code readability.
55 ;; eval this before editing
57 (put 'nnmaildir--with-nntp-buffer 'lisp-indent-function 0)
58 (put 'nnmaildir--with-work-buffer 'lisp-indent-function 0)
59 (put 'nnmaildir--with-nov-buffer 'lisp-indent-function 0)
60 (put 'nnmaildir--with-move-buffer 'lisp-indent-function 0)
76 (defconst nnmaildir-version "Gnus")
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.")
82 ;; The filename of the article being moved/copied:
83 (defvar nnmaildir--file nil)
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)
90 ;; An obarry containing symbols whose names are server names and whose values
92 (defvar nnmaildir--servers (make-vector 3 0))
93 ;; The current server:
94 (defvar nnmaildir--cur-server nil)
96 ;; A copy of nnmail-extra-headers
97 (defvar nnmaildir--extra nil)
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))
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
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")
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 names->groups
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 (create-dir nil :type string)) ;; group creation directory
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))
162 (nlist-pre '(nil . nil))
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
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))
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))))
186 (defun nnmaildir--nlist-art (group num)
187 (let ((entry (assq num (nnmaildir--grp-nlist group))))
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)))
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))
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)))
208 (defmacro nnmaildir--with-nntp-buffer (&rest body)
210 (set-buffer nntp-server-buffer)
212 (defmacro nnmaildir--with-work-buffer (&rest body)
214 (set-buffer (get-buffer-create " *nnmaildir work*"))
216 (defmacro nnmaildir--with-nov-buffer (&rest body)
218 (set-buffer (get-buffer-create " *nnmaildir nov*"))
220 (defmacro nnmaildir--with-move-buffer (&rest body)
222 (set-buffer (get-buffer-create " *nnmaildir move*"))
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 (defmacro nnmaildir--num-file (dir) `(concat ,dir ":"))
238 (defmacro nnmaildir--unlink (file-arg)
239 `(let ((file ,file-arg))
240 (if (file-attributes file) (delete-file file))))
241 (defun nnmaildir--mkdir (dir)
242 (or (file-exists-p (file-name-as-directory dir))
243 (make-directory-internal (directory-file-name dir))))
244 (defun nnmaildir--delete-dir-files (dir ls)
245 (mapcar 'delete-file (funcall ls dir 'full "\\`[^.]" 'nosort))
246 (delete-directory dir))
248 (defun nnmaildir--group-maxnum (server group)
249 (if (zerop (nnmaildir--grp-count group)) 0
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 (setq nov (nnheader-parse-naked-head)
373 field (or (mail-header-lines nov) 0)))
374 (unless (or (zerop field) (nnmaildir--param pgname 'distrust-Lines:))
375 (setq nov-mid field))
376 (setq nov-mid (number-to-string nov-mid)
377 nov-mid (concat (number-to-string attr) "\t" nov-mid))
379 (setq field (or (mail-header-references nov) ""))
380 (nnmaildir--tab-to-space field)
381 (setq nov-mid (concat field "\t" nov-mid)
383 (lambda (f) (nnmaildir--tab-to-space (or f "")))
384 (list (mail-header-subject nov)
385 (mail-header-from nov)
386 (mail-header-date nov)) "\t")
389 (setq field (symbol-name (car extra))
391 (nnmaildir--tab-to-space field)
392 (nnmaildir--tab-to-space val)
393 (concat field ": " val))
394 (mail-header-extra nov) "\t")))
395 (setq msgid (mail-header-id nov))
396 (if (or (null msgid) (nnheader-fake-message-id-p msgid))
397 (setq msgid (concat "<" prefix "@nnmaildir>")))
398 (nnmaildir--tab-to-space msgid)
399 ;; The data is parsed; create an nnmaildir NOV structure.
400 (setq nov (nnmaildir--nov-new nov-beg nov-mid nov-end mtime
402 num (nnmaildir--art-num article))
404 ;; Allocate a new article number.
406 (setq numdir (nnmaildir--num-dir dir)
407 file (nnmaildir--num-file numdir)
409 (nnmaildir--mkdir numdir)
410 (write-region "" nil file nil 'no-message)
412 ;; Get the number of links to file.
413 (setq attr (nth 1 (file-attributes file)))
415 ;; We've already tried this number, in the previous loop
416 ;; iteration, and failed.
417 (signal 'error `("Corrupt internal nnmaildir data" ,numdir)))
418 ;; If attr is 123, try to link file to "123". This atomically
419 ;; increases the link count and creates the "123" link, failing
420 ;; if that link was already created by another Gnus, just after
424 (add-name-to-file file (concat numdir (format "%x" attr)))
425 (setq file nil)) ;; Stop looping.
426 (file-already-exists nil))
428 (setf (nnmaildir--art-num article) num))
429 ;; Store this new NOV data in a file
431 (prin1 (vector storage-version num msgid nov) (current-buffer))
432 (setq file (concat novfile ":"))
433 (nnmaildir--unlink file)
434 (write-region (point-min) (point-max) file nil 'no-message nil 'excl))
435 (rename-file file novfile 'replace)
436 (setf (nnmaildir--art-msgid article) msgid)
439 (defun nnmaildir--cache-nov (group article nov)
440 (let ((cache (nnmaildir--grp-cache group))
441 (index (nnmaildir--grp-index group))
443 (unless (nnmaildir--art-nov article)
444 (setq goner (aref cache index))
445 (if goner (setf (nnmaildir--art-nov goner) nil))
446 (aset cache index article)
447 (setf (nnmaildir--grp-index group) (% (1+ index) (length cache))))
448 (setf (nnmaildir--art-nov article) nov)))
450 (defun nnmaildir--grp-add-art (server group article)
451 (let ((nov (nnmaildir--update-nov server group article))
452 count num min nlist nlist-cdr insert-nlist)
454 (setq count (1+ (nnmaildir--grp-count group))
455 num (nnmaildir--art-num article)
456 min (if (= count 1) num
457 (min num (nnmaildir--grp-min group)))
458 nlist (nnmaildir--grp-nlist group))
459 (if (or (null nlist) (> num (caar nlist)))
460 (setq nlist (cons (cons num article) nlist))
462 nlist-cdr (cdr nlist))
463 (while (and nlist-cdr (< num (caar nlist-cdr)))
464 (setq nlist nlist-cdr
465 nlist-cdr (cdr nlist))))
466 (let ((inhibit-quit t))
467 (setf (nnmaildir--grp-count group) count)
468 (setf (nnmaildir--grp-min group) min)
470 (setcdr nlist (cons (cons num article) nlist-cdr))
471 (setf (nnmaildir--grp-nlist group) nlist))
472 (set (intern (nnmaildir--art-prefix article)
473 (nnmaildir--grp-flist group))
475 (set (intern (nnmaildir--art-msgid article)
476 (nnmaildir--grp-mlist group))
478 (set (intern (nnmaildir--grp-name group)
479 (nnmaildir--srv-groups server))
481 (nnmaildir--cache-nov group article nov)
484 (defun nnmaildir--group-ls (server pgname)
485 (or (nnmaildir--param pgname 'directory-files)
486 (nnmaildir--srv-ls server)))
488 (defun nnmaildir-article-number-to-file-name
489 (number group-name server-address-string)
490 (let ((group (nnmaildir--prepare server-address-string group-name))
494 ;; The given group or server does not exist.
496 (setq article (nnmaildir--nlist-art group number))
498 ;; The given article number does not exist in this group.
500 (setq pgname (nnmaildir--pgname nnmaildir--cur-server group-name)
501 dir (nnmaildir--srv-dir nnmaildir--cur-server)
502 dir (nnmaildir--srvgrp-dir dir group-name)
503 dir (if (nnmaildir--param pgname 'read-only)
504 (nnmaildir--new dir) (nnmaildir--cur dir)))
505 (concat dir (nnmaildir--art-prefix article)
506 (nnmaildir--art-suffix article)))))
508 (defun nnmaildir-article-number-to-base-name
509 (number group-name server-address-string)
510 (let ((x (nnmaildir--prepare server-address-string group-name)))
512 (setq x (nnmaildir--nlist-art x number))
513 (and x (cons (nnmaildir--art-prefix x)
514 (nnmaildir--art-suffix x))))))
516 (defun nnmaildir-base-name-to-article-number
517 (base-name group-name server-address-string)
518 (let ((x (nnmaildir--prepare server-address-string group-name)))
520 (setq x (nnmaildir--grp-flist x)
521 x (nnmaildir--flist-art x base-name))
522 (and x (nnmaildir--art-num x)))))
524 (defun nnmaildir--nlist-iterate (nlist ranges func)
525 (let (entry high low nlist2)
527 (setq ranges `((1 . ,(caar nlist)))))
529 (setq entry (car ranges) ranges (cdr ranges))
530 (while (and ranges (eq entry (car ranges)))
531 (setq ranges (cdr ranges))) ;; skip duplicates
535 (setq low (car entry)
537 (setq nlist2 nlist) ;; Don't assume any sorting of ranges
540 (if (<= (caar nlist2) high) (throw 'iterate-loop nil))
541 (setq nlist2 (cdr nlist2))))
544 (setq entry (car nlist2) nlist2 (cdr nlist2))
545 (if (< (car entry) low) (throw 'iterate-loop nil))
546 (funcall func (cdr entry)))))))
548 (defun nnmaildir--up2-1 (n)
549 (if (zerop n) 1 (1- (lsh 1 (1+ (logb n))))))
551 (defun nnmaildir-request-type (group &optional article)
554 (defun nnmaildir-status-message (&optional server)
555 (nnmaildir--prepare server nil)
556 (nnmaildir--srv-error nnmaildir--cur-server))
558 (defun nnmaildir-server-opened (&optional server)
559 (and nnmaildir--cur-server
561 (string-equal server (nnmaildir--srv-address nnmaildir--cur-server))
563 (nnmaildir--srv-groups nnmaildir--cur-server)
566 (defun nnmaildir-open-server (server &optional defs)
570 (setq server (intern-soft x nnmaildir--servers))
572 (and (setq server (symbol-value server))
573 (nnmaildir--srv-groups server)
574 (setq nnmaildir--cur-server server)
576 (setq server (make-nnmaildir--srv :address x))
577 (let ((inhibit-quit t))
578 (set (intern x nnmaildir--servers) server)))
579 (setq dir (assq 'directory defs))
581 (setf (nnmaildir--srv-error server)
582 "You must set \"directory\" in the select method")
586 dir (expand-file-name dir)
587 dir (file-name-as-directory dir))
588 (unless (file-exists-p dir)
589 (setf (nnmaildir--srv-error server) (concat "No such directory: " dir))
591 (setf (nnmaildir--srv-dir server) dir)
592 (setq x (assq 'directory-files defs))
594 (setq x (if nnheader-directory-files-is-safe 'directory-files
595 'nnheader-directory-files-safe))
597 (unless (functionp x)
598 (setf (nnmaildir--srv-error server)
599 (concat "Not a function: " (prin1-to-string x)))
600 (throw 'return nil)))
601 (setf (nnmaildir--srv-ls server) x)
602 (setq size (length (funcall x dir nil "\\`[^.]" 'nosort))
603 size (nnmaildir--up2-1 size))
604 (and (setq x (assq 'get-new-mail defs))
607 (setf (nnmaildir--srv-gnm server) t)
609 (setq x (assq 'create-directory defs))
613 (setf (nnmaildir--srv-create-dir server) x))
614 (setf (nnmaildir--srv-groups server) (make-vector size 0))
615 (setq nnmaildir--cur-server server)
618 (defun nnmaildir--parse-filename (file)
619 (let ((prefix (car file))
621 (if (string-match "\\`\\([0-9]+\\)\\(\\..*\\)\\'" prefix)
623 (setq timestamp (concat "0000" (match-string 1 prefix))
624 len (- (length timestamp) 4))
625 (vector (string-to-number (substring timestamp 0 len))
626 (string-to-number (substring timestamp len))
627 (match-string 2 prefix)
631 (defun nnmaildir--sort-files (a b)
634 (throw 'return (and (consp b) (string-lessp (car a) (car b)))))
635 (if (consp b) (throw 'return t))
636 (if (< (aref a 0) (aref b 0)) (throw 'return t))
637 (if (> (aref a 0) (aref b 0)) (throw 'return nil))
638 (if (< (aref a 1) (aref b 1)) (throw 'return t))
639 (if (> (aref a 1) (aref b 1)) (throw 'return nil))
640 (string-lessp (aref a 2) (aref b 2))))
642 (defun nnmaildir--scan (gname scan-msgs groups method srv-dir srv-ls)
644 (let ((36h-ago (- (car (current-time)) 2))
645 absdir nndir tdir ndir cdir nattr cattr isnew pgname read-only ls
646 files num dir flist group x)
647 (setq absdir (nnmaildir--srvgrp-dir srv-dir gname)
648 nndir (nnmaildir--nndir absdir))
649 (unless (file-exists-p absdir)
650 (setf (nnmaildir--srv-error nnmaildir--cur-server)
651 (concat "No such directory: " absdir))
653 (setq tdir (nnmaildir--tmp absdir)
654 ndir (nnmaildir--new absdir)
655 cdir (nnmaildir--cur absdir)
656 nattr (file-attributes ndir)
657 cattr (file-attributes cdir))
658 (unless (and (file-exists-p tdir) nattr cattr)
659 (setf (nnmaildir--srv-error nnmaildir--cur-server)
660 (concat "Not a maildir: " absdir))
662 (setq group (nnmaildir--prepare nil gname)
663 pgname (nnmaildir--pgname nnmaildir--cur-server gname))
667 group (make-nnmaildir--grp :name gname :index 0))
668 (nnmaildir--mkdir nndir)
669 (nnmaildir--mkdir (nnmaildir--nov-dir nndir))
670 (nnmaildir--mkdir (nnmaildir--marks-dir nndir))
671 (write-region "" nil (concat nndir "markfile") nil 'no-message))
672 (setq read-only (nnmaildir--param pgname 'read-only)
673 ls (or (nnmaildir--param pgname 'directory-files) srv-ls))
675 (setq x (nth 11 (file-attributes tdir)))
676 (unless (and (= x (nth 11 nattr)) (= x (nth 11 cattr)))
677 (setf (nnmaildir--srv-error nnmaildir--cur-server)
678 (concat "Maildir spans filesystems: " absdir))
682 (setq x (file-attributes file))
683 (if (or (> (cadr x) 1) (< (car (nth 4 x)) 36h-ago))
685 (funcall ls tdir 'full "\\`[^.]" 'nosort)))
689 (setq nattr (nth 5 nattr))
690 (if (equal nattr (nnmaildir--grp-new group))
692 (if read-only (setq dir (and (or isnew nattr) ndir))
693 (when (or isnew nattr)
696 (let ((path (concat ndir file)))
697 (and (time-less-p (nth 5 (file-attributes path)) (current-time))
698 (rename-file path (concat cdir file ":2,")))))
699 (funcall ls ndir nil "\\`[^.]" 'nosort))
700 (setf (nnmaildir--grp-new group) nattr))
701 (setq cattr (nth 5 (file-attributes cdir)))
702 (if (equal cattr (nnmaildir--grp-cur group))
704 (setq dir (and (or isnew cattr) cdir)))
705 (unless dir (throw 'return t))
706 (setq files (funcall ls dir nil "\\`[^.]" 'nosort)
707 files (save-match-data
710 (string-match "\\`\\([^:]*\\)\\(\\(:.*\\)?\\)\\'" f)
711 (cons (match-string 1 f) (match-string 2 f)))
714 (setq num (nnmaildir--up2-1 (length files)))
715 (setf (nnmaildir--grp-flist group) (make-vector num 0))
716 (setf (nnmaildir--grp-mlist group) (make-vector num 0))
717 (setf (nnmaildir--grp-mmth group) (make-vector 1 0))
718 (setq num (nnmaildir--param pgname 'nov-cache-size))
719 (if (numberp num) (if (< num 1) (setq num 1))
721 cdir (nnmaildir--marks-dir nndir)
722 ndir (nnmaildir--subdir cdir "tick")
723 cdir (nnmaildir--subdir cdir "read"))
726 (setq file (car file))
727 (if (or (not (file-exists-p (concat cdir file)))
728 (file-exists-p (concat ndir file)))
729 (setq num (1+ num))))
731 (setf (nnmaildir--grp-cache group) (make-vector num nil))
732 (let ((inhibit-quit t))
733 (set (intern gname groups) group))
734 (or scan-msgs (throw 'return t)))
735 (setq flist (nnmaildir--grp-flist group)
738 (and (null (nnmaildir--flist-art flist (car file)))
741 files (delq nil files)
742 files (mapcar 'nnmaildir--parse-filename files)
743 files (sort files 'nnmaildir--sort-files))
746 (setq file (if (consp file) file (aref file 3))
747 x (make-nnmaildir--art :prefix (car file) :suffix (cdr file)))
748 (nnmaildir--grp-add-art nnmaildir--cur-server group x))
750 (if read-only (setf (nnmaildir--grp-new group) nattr)
751 (setf (nnmaildir--grp-cur group) cattr)))
754 (defun nnmaildir-request-scan (&optional scan-group server)
755 (let ((coding-system-for-write nnheader-file-coding-system)
756 (buffer-file-coding-system nil)
757 (file-coding-system-alist nil)
758 (nnmaildir-get-new-mail t)
759 (nnmaildir-group-alist nil)
760 (nnmaildir-active-file nil)
761 x srv-ls srv-dir method groups group dirs grp-dir seen deactivate-mark)
762 (nnmaildir--prepare server nil)
763 (setq srv-ls (nnmaildir--srv-ls nnmaildir--cur-server)
764 srv-dir (nnmaildir--srv-dir nnmaildir--cur-server)
765 method (nnmaildir--srv-method nnmaildir--cur-server)
766 groups (nnmaildir--srv-groups nnmaildir--cur-server))
767 (nnmaildir--with-work-buffer
769 (if (stringp scan-group)
770 (if (nnmaildir--scan scan-group t groups method srv-dir srv-ls)
771 (if (nnmaildir--srv-gnm nnmaildir--cur-server)
772 (nnmail-get-new-mail 'nnmaildir nil nil scan-group))
773 (unintern scan-group groups))
774 (setq x (nth 5 (file-attributes srv-dir))
775 scan-group (null scan-group))
776 (if (equal x (nnmaildir--srv-mtime nnmaildir--cur-server))
778 (mapatoms (lambda (sym)
779 (nnmaildir--scan (symbol-name sym) t groups
780 method srv-dir srv-ls))
782 (setq dirs (funcall srv-ls srv-dir nil "\\`[^.]" 'nosort)
783 seen (nnmaildir--up2-1 (length dirs))
784 seen (make-vector seen 0))
787 (if (nnmaildir--scan grp-dir scan-group groups method srv-dir
789 (intern grp-dir seen)))
792 (mapatoms (lambda (group)
793 (setq group (symbol-name group))
794 (unless (intern-soft group seen)
795 (setq x (cons group x))))
797 (mapcar (lambda (grp) (unintern grp groups)) x)
798 (setf (nnmaildir--srv-mtime nnmaildir--cur-server)
799 (nth 5 (file-attributes srv-dir))))
801 (nnmaildir--srv-gnm nnmaildir--cur-server)
802 (nnmail-get-new-mail 'nnmaildir nil nil))))))
805 (defun nnmaildir-request-list (&optional server)
806 (nnmaildir-request-scan 'find-new-groups server)
807 (let (pgname ro deactivate-mark)
808 (nnmaildir--prepare server nil)
809 (nnmaildir--with-nntp-buffer
811 (mapatoms (lambda (group)
812 (setq pgname (symbol-name group)
813 pgname (nnmaildir--pgname nnmaildir--cur-server pgname)
814 group (symbol-value group)
815 ro (nnmaildir--param pgname 'read-only))
816 (insert (nnmaildir--grp-name group) " ")
817 (princ (nnmaildir--group-maxnum nnmaildir--cur-server group)
820 (princ (nnmaildir--grp-min group) nntp-server-buffer)
821 (insert " " (if ro "n" "y") "\n"))
822 (nnmaildir--srv-groups nnmaildir--cur-server))))
825 (defun nnmaildir-request-newgroups (date &optional server)
826 (nnmaildir-request-list server))
828 (defun nnmaildir-retrieve-groups (groups &optional server)
829 (let (group deactivate-mark)
830 (nnmaildir--prepare server nil)
831 (nnmaildir--with-nntp-buffer
835 (setq group (nnmaildir--prepare nil gname))
836 (if (null group) (insert "411 no such news group\n")
838 (princ (nnmaildir--grp-count group) nntp-server-buffer)
840 (princ (nnmaildir--grp-min group) nntp-server-buffer)
842 (princ (nnmaildir--group-maxnum nnmaildir--cur-server group)
844 (insert " " gname "\n")))
848 (defun nnmaildir-request-update-info (gname info &optional server)
849 (let ((group (nnmaildir--prepare server gname))
850 pgname flist always-marks never-marks old-marks dotfile num dir
851 markdirs marks mark ranges markdir article read end new-marks ls
852 old-mmth new-mmth mtime mark-sym existing missing deactivate-mark)
855 (setf (nnmaildir--srv-error nnmaildir--cur-server)
856 (concat "No such group: " gname))
858 (setq gname (nnmaildir--grp-name group)
859 pgname (nnmaildir--pgname nnmaildir--cur-server gname)
860 flist (nnmaildir--grp-flist group))
861 (when (zerop (nnmaildir--grp-count group))
862 (gnus-info-set-read info nil)
863 (gnus-info-set-marks info nil 'extend)
864 (throw 'return info))
865 (setq old-marks (cons 'read (gnus-info-read info))
866 old-marks (cons old-marks (gnus-info-marks info))
867 always-marks (nnmaildir--param pgname 'always-marks)
868 never-marks (nnmaildir--param pgname 'never-marks)
869 existing (nnmaildir--grp-nlist group)
870 existing (mapcar 'car existing)
871 existing (nreverse existing)
872 existing (gnus-compress-sequence existing 'always-list)
873 missing (list (cons 1 (nnmaildir--group-maxnum
874 nnmaildir--cur-server group)))
875 missing (gnus-range-difference missing existing)
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)
893 (setq ranges existing)
894 (throw 'got-ranges nil))
895 (setq mtime (nth 5 (file-attributes markdir)))
896 (set (intern mark new-mmth) mtime)
897 (when (equal mtime (symbol-value (intern-soft mark old-mmth)))
898 (setq ranges (assq mark-sym old-marks))
899 (if ranges (setq ranges (cdr ranges)))
900 (throw 'got-ranges nil))
903 (setq article (nnmaildir--flist-art flist prefix))
906 (gnus-add-to-range ranges
907 `(,(nnmaildir--art-num article))))))
908 (funcall ls markdir nil "\\`[^.]" 'nosort)))
909 (if (eq mark-sym 'read) (setq read ranges)
910 (if ranges (setq marks (cons (cons mark-sym ranges) marks)))))
912 (gnus-info-set-read info (gnus-range-add read missing))
913 (gnus-info-set-marks info marks 'extend)
914 (setf (nnmaildir--grp-mmth group) new-mmth)
917 (defun nnmaildir-request-group (gname &optional server fast)
918 (let ((group (nnmaildir--prepare server gname))
922 ;; (insert "411 no such news group\n")
923 (setf (nnmaildir--srv-error nnmaildir--cur-server)
924 (concat "No such group: " gname))
926 (setf (nnmaildir--srv-curgrp nnmaildir--cur-server) group)
927 (if fast (throw 'return t))
928 (nnmaildir--with-nntp-buffer
931 (princ (nnmaildir--grp-count group) nntp-server-buffer)
933 (princ (nnmaildir--grp-min group) nntp-server-buffer)
935 (princ (nnmaildir--group-maxnum nnmaildir--cur-server group)
937 (insert " " gname "\n")
940 (defun nnmaildir-request-create-group (gname &optional server args)
941 (nnmaildir--prepare server nil)
943 (let ((create-dir (nnmaildir--srv-create-dir nnmaildir--cur-server))
945 (when (zerop (length gname))
946 (setf (nnmaildir--srv-error nnmaildir--cur-server)
947 "Invalid (empty) group name")
949 (when (eq (aref "." 0) (aref gname 0))
950 (setf (nnmaildir--srv-error nnmaildir--cur-server)
951 "Group names may not start with \".\"")
953 (when (save-match-data (string-match "[\0/\t]" gname))
954 (setf (nnmaildir--srv-error nnmaildir--cur-server)
955 (concat "Illegal characters (null, tab, or /) in group name: "
958 (setq groups (nnmaildir--srv-groups nnmaildir--cur-server))
959 (when (intern-soft gname groups)
960 (setf (nnmaildir--srv-error nnmaildir--cur-server)
961 (concat "Group already exists: " gname))
963 (setq srv-dir (nnmaildir--srv-dir nnmaildir--cur-server))
964 (if (file-name-absolute-p create-dir)
965 (setq dir (expand-file-name create-dir))
967 dir (file-truename dir)
968 dir (concat dir create-dir)))
969 (setq dir (nnmaildir--subdir (file-name-as-directory dir) gname))
970 (nnmaildir--mkdir dir)
971 (nnmaildir--mkdir (nnmaildir--tmp dir))
972 (nnmaildir--mkdir (nnmaildir--new dir))
973 (nnmaildir--mkdir (nnmaildir--cur dir))
974 (setq create-dir (file-name-as-directory create-dir))
975 (make-symbolic-link (concat create-dir gname) (concat srv-dir gname))
976 (nnmaildir-request-scan 'find-new-groups))))
978 (defun nnmaildir-request-rename-group (gname new-name &optional server)
979 (let ((group (nnmaildir--prepare server gname))
980 (coding-system-for-write nnheader-file-coding-system)
981 (buffer-file-coding-system nil)
982 (file-coding-system-alist nil)
986 (setf (nnmaildir--srv-error nnmaildir--cur-server)
987 (concat "No such group: " gname))
989 (when (zerop (length new-name))
990 (setf (nnmaildir--srv-error nnmaildir--cur-server)
991 "Invalid (empty) group name")
993 (when (eq (aref "." 0) (aref new-name 0))
994 (setf (nnmaildir--srv-error nnmaildir--cur-server)
995 "Group names may not start with \".\"")
997 (when (save-match-data (string-match "[\0/\t]" new-name))
998 (setf (nnmaildir--srv-error nnmaildir--cur-server)
999 (concat "Illegal characters (null, tab, or /) in group name: "
1001 (throw 'return nil))
1002 (if (string-equal gname new-name) (throw 'return t))
1003 (when (intern-soft new-name
1004 (nnmaildir--srv-groups nnmaildir--cur-server))
1005 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1006 (concat "Group already exists: " new-name))
1007 (throw 'return nil))
1008 (setq srv-dir (nnmaildir--srv-dir nnmaildir--cur-server))
1010 (rename-file (concat srv-dir gname)
1011 (concat srv-dir new-name))
1013 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1014 (concat "Error renaming link: " (prin1-to-string err)))
1015 (throw 'return nil)))
1016 (setq x (nnmaildir--srv-groups nnmaildir--cur-server)
1017 groups (make-vector (length x) 0))
1018 (mapatoms (lambda (sym)
1019 (unless (eq (symbol-value sym) group)
1020 (set (intern (symbol-name sym) groups)
1021 (symbol-value sym))))
1023 (setq group (copy-sequence group))
1024 (setf (nnmaildir--grp-name group) new-name)
1025 (set (intern new-name groups) group)
1026 (setf (nnmaildir--srv-groups nnmaildir--cur-server) groups)
1029 (defun nnmaildir-request-delete-group (gname force &optional server)
1030 (let ((group (nnmaildir--prepare server gname))
1031 pgname grp-dir dir ls deactivate-mark)
1034 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1035 (concat "No such group: " gname))
1036 (throw 'return nil))
1037 (if (eq group (nnmaildir--srv-curgrp nnmaildir--cur-server))
1038 (setf (nnmaildir--srv-curgrp nnmaildir--cur-server) nil))
1039 (setq gname (nnmaildir--grp-name group)
1040 pgname (nnmaildir--pgname nnmaildir--cur-server gname))
1041 (unintern gname (nnmaildir--srv-groups nnmaildir--cur-server))
1042 (setq grp-dir (nnmaildir--srv-dir nnmaildir--cur-server)
1043 grp-dir (nnmaildir--srvgrp-dir grp-dir gname))
1044 (if (not force) (setq grp-dir (directory-file-name grp-dir))
1045 (setq ls (nnmaildir--group-ls nnmaildir--cur-server pgname))
1046 (if (nnmaildir--param pgname 'read-only)
1047 (progn (delete-directory (nnmaildir--tmp grp-dir))
1048 (nnmaildir--unlink (nnmaildir--new grp-dir))
1049 (delete-directory (nnmaildir--cur grp-dir)))
1050 (nnmaildir--delete-dir-files (nnmaildir--tmp grp-dir) ls)
1051 (nnmaildir--delete-dir-files (nnmaildir--new grp-dir) ls)
1052 (nnmaildir--delete-dir-files (nnmaildir--cur grp-dir) ls))
1053 (setq dir (nnmaildir--nndir grp-dir))
1054 (mapcar (lambda (subdir) (nnmaildir--delete-dir-files subdir ls))
1055 `(,(nnmaildir--nov-dir dir) ,(nnmaildir--num-dir dir)
1056 ,@(funcall ls (nnmaildir--marks-dir dir) 'full "\\`[^.]"
1058 (setq dir (nnmaildir--nndir grp-dir))
1059 (nnmaildir--unlink (concat dir "markfile"))
1060 (nnmaildir--unlink (concat dir "markfile{new}"))
1061 (delete-directory (nnmaildir--marks-dir dir))
1062 (delete-directory dir)
1063 (setq grp-dir (directory-file-name grp-dir)
1064 dir (car (file-attributes grp-dir)))
1065 (unless (eq (aref "/" 0) (aref dir 0))
1066 (setq dir (concat (file-truename
1067 (nnmaildir--srv-dir nnmaildir--cur-server))
1069 (delete-directory dir))
1070 (nnmaildir--unlink grp-dir)
1073 (defun nnmaildir-retrieve-headers (articles &optional gname server fetch-old)
1074 (let ((group (nnmaildir--prepare server gname))
1075 srv-dir dir nlist mlist article num start stop nov nlist2 insert-nov
1079 (setq nov (nnmaildir--update-nov nnmaildir--cur-server group
1082 (nnmaildir--cache-nov group article nov)
1083 (setq num (nnmaildir--art-num article))
1084 (princ num nntp-server-buffer)
1085 (insert "\t" (nnmaildir--nov-get-beg nov) "\t"
1086 (nnmaildir--art-msgid article) "\t"
1087 (nnmaildir--nov-get-mid nov) "\tXref: nnmaildir "
1089 (princ num nntp-server-buffer)
1090 (insert "\t" (nnmaildir--nov-get-end nov) "\n"))))
1093 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1094 (if gname (concat "No such group: " gname) "No current group"))
1095 (throw 'return nil))
1096 (nnmaildir--with-nntp-buffer
1098 (setq mlist (nnmaildir--grp-mlist group)
1099 nlist (nnmaildir--grp-nlist group)
1100 gname (nnmaildir--grp-name group)
1101 srv-dir (nnmaildir--srv-dir nnmaildir--cur-server)
1102 dir (nnmaildir--srvgrp-dir srv-dir gname))
1105 ((and fetch-old (not (numberp fetch-old)))
1106 (nnmaildir--nlist-iterate nlist 'all insert-nov))
1108 ((stringp (car articles))
1111 (setq article (nnmaildir--mlist-art mlist msgid))
1112 (if article (funcall insert-nov article)))
1116 ;; Assume the article range list is sorted ascending
1117 (setq stop (car articles)
1118 start (car (last articles))
1119 stop (if (numberp stop) stop (car stop))
1120 start (if (numberp start) start (cdr start))
1121 stop (- stop fetch-old)
1122 stop (if (< stop 1) 1 stop)
1123 articles (list (cons stop start))))
1124 (nnmaildir--nlist-iterate nlist articles insert-nov)))
1125 (sort-numeric-fields 1 (point-min) (point-max))
1128 (defun nnmaildir-request-article (num-msgid &optional gname server to-buffer)
1129 (let ((group (nnmaildir--prepare server gname))
1130 (case-fold-search t)
1131 list article dir pgname deactivate-mark)
1134 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1135 (if gname (concat "No such group: " gname) "No current group"))
1136 (throw 'return nil))
1137 (if (numberp num-msgid)
1138 (setq article (nnmaildir--nlist-art group num-msgid))
1139 (setq list (nnmaildir--grp-mlist group)
1140 article (nnmaildir--mlist-art list num-msgid))
1141 (if article (setq num-msgid (nnmaildir--art-num article))
1145 (setq group (symbol-value group-sym)
1146 list (nnmaildir--grp-mlist group)
1147 article (nnmaildir--mlist-art list num-msgid))
1149 (setq num-msgid (nnmaildir--art-num article))
1150 (throw 'found nil)))
1151 (nnmaildir--srv-groups nnmaildir--cur-server))))
1153 (setf (nnmaildir--srv-error nnmaildir--cur-server) "No such article")
1154 (throw 'return nil)))
1155 (setq gname (nnmaildir--grp-name group)
1156 pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1157 dir (nnmaildir--srv-dir nnmaildir--cur-server)
1158 dir (nnmaildir--srvgrp-dir dir gname)
1159 dir (if (nnmaildir--param pgname 'read-only)
1160 (nnmaildir--new dir) (nnmaildir--cur dir))
1161 nnmaildir-article-file-name
1163 (nnmaildir--art-prefix article)
1164 (nnmaildir--art-suffix article)))
1165 (unless (file-exists-p nnmaildir-article-file-name)
1166 (nnmaildir--expired-article group article)
1167 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1168 "Article has expired")
1169 (throw 'return nil))
1171 (set-buffer (or to-buffer nntp-server-buffer))
1173 (nnheader-insert-file-contents nnmaildir-article-file-name))
1174 (cons gname num-msgid))))
1176 (defun nnmaildir-request-post (&optional server)
1177 (let (message-required-mail-headers)
1178 (funcall message-send-mail-function)))
1180 (defun nnmaildir-request-replace-article (number gname buffer)
1181 (let ((group (nnmaildir--prepare nil gname))
1182 (coding-system-for-write nnheader-file-coding-system)
1183 (buffer-file-coding-system nil)
1184 (file-coding-system-alist nil)
1185 dir file article suffix tmpfile deactivate-mark)
1188 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1189 (concat "No such group: " gname))
1190 (throw 'return nil))
1191 (when (nnmaildir--param (nnmaildir--pgname nnmaildir--cur-server gname)
1193 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1194 (concat "Read-only group: " group))
1195 (throw 'return nil))
1196 (setq dir (nnmaildir--srv-dir nnmaildir--cur-server)
1197 dir (nnmaildir--srvgrp-dir dir gname)
1198 article (nnmaildir--nlist-art group number))
1200 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1201 (concat "No such article: " (number-to-string number)))
1202 (throw 'return nil))
1203 (setq suffix (nnmaildir--art-suffix article)
1204 file (nnmaildir--art-prefix article)
1205 tmpfile (concat (nnmaildir--tmp dir) file))
1206 (when (file-exists-p tmpfile)
1207 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1208 (concat "File exists: " tmpfile))
1209 (throw 'return nil))
1212 (write-region (point-min) (point-max) tmpfile nil 'no-message nil
1214 (unix-sync) ;; no fsync :(
1215 (rename-file tmpfile (concat (nnmaildir--cur dir) file suffix) 'replace)
1218 (defun nnmaildir-request-move-article (article gname server accept-form
1220 (let ((group (nnmaildir--prepare server gname))
1221 pgname suffix result nnmaildir--file deactivate-mark)
1224 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1225 (concat "No such group: " gname))
1226 (throw 'return nil))
1227 (setq gname (nnmaildir--grp-name group)
1228 pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1229 article (nnmaildir--nlist-art group article))
1231 (setf (nnmaildir--srv-error nnmaildir--cur-server) "No such article")
1232 (throw 'return nil))
1233 (setq suffix (nnmaildir--art-suffix article)
1234 nnmaildir--file (nnmaildir--srv-dir nnmaildir--cur-server)
1235 nnmaildir--file (nnmaildir--srvgrp-dir nnmaildir--file gname)
1236 nnmaildir--file (if (nnmaildir--param pgname 'read-only)
1237 (nnmaildir--new nnmaildir--file)
1238 (nnmaildir--cur nnmaildir--file))
1239 nnmaildir--file (concat nnmaildir--file
1240 (nnmaildir--art-prefix article)
1242 (unless (file-exists-p nnmaildir--file)
1243 (nnmaildir--expired-article group article)
1244 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1245 "Article has expired")
1246 (throw 'return nil))
1247 (nnmaildir--with-move-buffer
1249 (nnheader-insert-file-contents nnmaildir--file)
1250 (setq result (eval accept-form)))
1251 (unless (or (null result) (nnmaildir--param pgname 'read-only))
1252 (nnmaildir--unlink nnmaildir--file)
1253 (nnmaildir--expired-article group article))
1256 (defun nnmaildir-request-accept-article (gname &optional server last)
1257 (let ((group (nnmaildir--prepare server gname))
1258 (coding-system-for-write nnheader-file-coding-system)
1259 (buffer-file-coding-system nil)
1260 (file-coding-system-alist nil)
1261 srv-dir dir file time tmpfile curfile 24h article)
1264 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1265 (concat "No such group: " gname))
1266 (throw 'return nil))
1267 (setq gname (nnmaildir--grp-name group))
1268 (when (nnmaildir--param (nnmaildir--pgname nnmaildir--cur-server gname)
1270 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1271 (concat "Read-only group: " gname))
1272 (throw 'return nil))
1273 (setq srv-dir (nnmaildir--srv-dir nnmaildir--cur-server)
1274 dir (nnmaildir--srvgrp-dir srv-dir gname)
1276 file (format-time-string "%s." time))
1277 (unless (string-equal nnmaildir--delivery-time file)
1278 (setq nnmaildir--delivery-time file
1279 nnmaildir--delivery-count 0))
1280 (when (and (consp (cdr time))
1281 (consp (cddr time)))
1282 (setq file (concat file "M" (number-to-string (caddr time)))))
1283 (setq file (concat file nnmaildir--delivery-pid)
1284 file (concat file "Q" (number-to-string nnmaildir--delivery-count))
1285 file (concat file "." (system-name)) ;;;; FIXME: encode / and :
1286 tmpfile (concat (nnmaildir--tmp dir) file)
1287 curfile (concat (nnmaildir--cur dir) file ":2,"))
1288 (when (file-exists-p tmpfile)
1289 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1290 (concat "File exists: " tmpfile))
1291 (throw 'return nil))
1292 (when (file-exists-p curfile)
1293 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1294 (concat "File exists: " curfile))
1295 (throw 'return nil))
1296 (setq nnmaildir--delivery-count (1+ nnmaildir--delivery-count)
1297 24h (run-with-timer 86400 nil
1299 (nnmaildir--unlink tmpfile)
1300 (setf (nnmaildir--srv-error
1301 nnmaildir--cur-server)
1302 "24-hour timer expired")
1303 (throw 'return nil))))
1305 (add-name-to-file nnmaildir--file tmpfile)
1307 (write-region (point-min) (point-max) tmpfile nil 'no-message nil
1309 (unix-sync))) ;; no fsync :(
1312 (add-name-to-file tmpfile curfile)
1314 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1315 (concat "Error linking: " (prin1-to-string err)))
1316 (nnmaildir--unlink tmpfile)
1317 (throw 'return nil)))
1318 (nnmaildir--unlink tmpfile)
1319 (setq article (make-nnmaildir--art :prefix file :suffix ":2,"))
1320 (if (nnmaildir--grp-add-art nnmaildir--cur-server group article)
1321 (cons gname (nnmaildir--art-num article))))))
1323 (defun nnmaildir-save-mail (group-art)
1326 (throw 'return nil))
1327 (let (ga gname x groups nnmaildir--file deactivate-mark)
1329 (goto-char (point-min))
1331 (while (looking-at "From ")
1332 (replace-match "X-From-Line: ")
1334 (setq groups (nnmaildir--srv-groups nnmaildir--cur-server)
1335 ga (car group-art) group-art (cdr group-art)
1337 (or (intern-soft gname groups)
1338 (nnmaildir-request-create-group gname)
1339 (throw 'return nil)) ;; not that nnmail bothers to check :(
1340 (unless (nnmaildir-request-accept-article gname)
1341 (throw 'return nil))
1342 (setq nnmaildir--file (nnmaildir--srv-dir nnmaildir--cur-server)
1343 nnmaildir--file (nnmaildir--srvgrp-dir nnmaildir--file gname)
1344 x (nnmaildir--prepare nil gname)
1345 x (nnmaildir--grp-nlist x)
1347 nnmaildir--file (concat nnmaildir--file
1348 (nnmaildir--art-prefix x)
1349 (nnmaildir--art-suffix x)))
1353 (setq gname (car ga))
1354 (and (or (intern-soft gname groups)
1355 (nnmaildir-request-create-group gname))
1356 (nnmaildir-request-accept-article gname)
1360 (defun nnmaildir-active-number (gname)
1363 (defun nnmaildir-request-expire-articles (ranges &optional gname server force)
1364 (let ((no-force (not force))
1365 (group (nnmaildir--prepare server gname))
1366 pgname time boundary bound-iter high low target dir nlist nlist2
1367 stop article didnt nnmaildir--file nnmaildir-article-file-name
1371 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1372 (if gname (concat "No such group: " gname) "No current group"))
1373 (throw 'return (gnus-uncompress-range ranges)))
1374 (setq gname (nnmaildir--grp-name group)
1375 pgname (nnmaildir--pgname nnmaildir--cur-server gname))
1376 (if (nnmaildir--param pgname 'read-only)
1377 (throw 'return (gnus-uncompress-range ranges)))
1378 (setq time (nnmaildir--param pgname 'expire-age))
1380 (setq time (or (and nnmail-expiry-wait-function
1381 (funcall nnmail-expiry-wait-function gname))
1382 nnmail-expiry-wait))
1383 (if (eq time 'immediate)
1386 (setq time (* time 86400)))))
1388 (unless (integerp time) ;; handle 'never
1389 (throw 'return (gnus-uncompress-range ranges)))
1390 (setq boundary (current-time)
1391 high (- (car boundary) (/ time 65536))
1392 low (- (cadr boundary) (% time 65536)))
1394 (setq low (+ low 65536)
1396 (setcar (cdr boundary) low)
1397 (setcar boundary high))
1398 (setq dir (nnmaildir--srv-dir nnmaildir--cur-server)
1399 dir (nnmaildir--srvgrp-dir dir gname)
1400 dir (nnmaildir--cur dir)
1401 nlist (nnmaildir--grp-nlist group)
1402 ranges (reverse ranges))
1403 (nnmaildir--with-move-buffer
1404 (nnmaildir--nlist-iterate
1407 (setq nnmaildir--file (nnmaildir--art-prefix article)
1408 nnmaildir--file (concat dir nnmaildir--file
1409 (nnmaildir--art-suffix article))
1410 time (file-attributes nnmaildir--file))
1413 (nnmaildir--expired-article group article))
1416 (setq time (nth 5 time)
1417 bound-iter boundary)
1418 (while (and bound-iter time
1419 (= (car bound-iter) (car time)))
1420 (setq bound-iter (cdr bound-iter)
1422 (and bound-iter time
1423 (car-less-than-car bound-iter time))))
1424 (setq didnt (cons (nnmaildir--art-num article) didnt)))
1426 (setq nnmaildir-article-file-name nnmaildir--file
1427 target (if force nil
1430 (nnmaildir--param pgname 'expire-group)))))
1431 (when (and (stringp target)
1432 (not (string-equal target pgname))) ;; Move it.
1434 (nnheader-insert-file-contents nnmaildir--file)
1435 (gnus-request-accept-article target nil nil 'no-encode))
1436 (if (equal target pgname)
1438 (setq didnt (cons (nnmaildir--art-num article) didnt))
1439 (nnmaildir--unlink nnmaildir--file)
1440 (nnmaildir--expired-article group article))))))
1444 (defun nnmaildir-request-set-mark (gname actions &optional server)
1445 (let ((group (nnmaildir--prepare server gname))
1446 (coding-system-for-write nnheader-file-coding-system)
1447 (buffer-file-coding-system nil)
1448 (file-coding-system-alist nil)
1449 del-mark del-action add-action set-action marksdir markfile nlist
1450 ranges begin end article all-marks todo-marks did-marks mdir mfile
1451 pgname ls markfilenew deactivate-mark)
1454 (setq mfile (nnmaildir--subdir marksdir (symbol-name mark))
1455 mfile (concat mfile (nnmaildir--art-prefix article)))
1456 (nnmaildir--unlink mfile))
1457 del-action (lambda (article) (mapcar del-mark todo-marks))
1462 (setq mdir (nnmaildir--subdir marksdir (symbol-name mark))
1463 mfile (concat mdir (nnmaildir--art-prefix article)))
1464 (unless (memq mark did-marks)
1465 (nnmaildir--mkdir mdir)
1466 (setq did-marks (cons mark did-marks)))
1467 (unless (file-exists-p mfile)
1469 (add-name-to-file markfile mfile)
1471 (unless (file-exists-p mfile)
1472 ;; too many links, maybe
1473 (write-region "" nil markfilenew nil 'no-message)
1474 (add-name-to-file markfilenew mfile
1475 'ok-if-already-exists)
1476 (rename-file markfilenew markfile 'replace))))))
1478 set-action (lambda (article)
1479 (funcall add-action)
1480 (mapcar (lambda (mark)
1481 (unless (memq mark todo-marks)
1482 (funcall del-mark mark)))
1486 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1487 (concat "No such group: " gname))
1488 (mapcar (lambda (action)
1489 (setq ranges (gnus-range-add ranges (car action))))
1491 (throw 'return ranges))
1492 (setq nlist (nnmaildir--grp-nlist group)
1493 marksdir (nnmaildir--srv-dir nnmaildir--cur-server)
1494 marksdir (nnmaildir--srvgrp-dir marksdir gname)
1495 marksdir (nnmaildir--nndir marksdir)
1496 markfile (concat marksdir "markfile")
1497 markfilenew (concat markfile "{new}")
1498 marksdir (nnmaildir--marks-dir marksdir)
1499 gname (nnmaildir--grp-name group)
1500 pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1501 ls (nnmaildir--group-ls nnmaildir--cur-server pgname)
1502 all-marks (funcall ls marksdir nil "\\`[^.]" 'nosort)
1503 all-marks (mapcar 'intern all-marks))
1506 (setq ranges (car action)
1507 todo-marks (caddr action))
1508 (mapcar (lambda (mark) (add-to-list 'all-marks mark)) todo-marks)
1509 (if (numberp (cdr ranges)) (setq ranges (list ranges)))
1510 (nnmaildir--nlist-iterate nlist ranges
1511 (cond ((eq 'del (cadr action)) del-action)
1512 ((eq 'add (cadr action)) add-action)
1517 (defun nnmaildir-close-group (gname &optional server)
1518 (let ((group (nnmaildir--prepare server gname))
1519 pgname ls dir msgdir files flist dirs)
1522 (setf (nnmaildir--srv-error nnmaildir--cur-server)
1523 (concat "No such group: " gname))
1525 (setq pgname (nnmaildir--pgname nnmaildir--cur-server gname)
1526 ls (nnmaildir--group-ls nnmaildir--cur-server pgname)
1527 dir (nnmaildir--srv-dir nnmaildir--cur-server)
1528 dir (nnmaildir--srvgrp-dir dir gname)
1529 msgdir (if (nnmaildir--param pgname 'read-only)
1530 (nnmaildir--new dir) (nnmaildir--cur dir))
1531 dir (nnmaildir--nndir dir)
1532 dirs (cons (nnmaildir--nov-dir dir)
1533 (funcall ls (nnmaildir--marks-dir dir) 'full "\\`[^.]"
1537 (cons dir (funcall ls dir nil "\\`[^.]" 'nosort)))
1539 files (funcall ls msgdir nil "\\`[^.]" 'nosort)
1540 flist (nnmaildir--up2-1 (length files))
1541 flist (make-vector flist 0))
1545 (string-match "\\`\\([^:]*\\)\\(:.*\\)?\\'" file)
1546 (intern (match-string 1 file) flist))
1550 (setq files (cdr dir)
1551 dir (file-name-as-directory (car dir)))
1554 (unless (intern-soft file flist)
1555 (setq file (concat dir file))
1556 (delete-file file)))
1561 (defun nnmaildir-close-server (&optional server)
1562 (let (flist ls dirs dir files file x)
1563 (nnmaildir--prepare server nil)
1564 (when nnmaildir--cur-server
1565 (setq server nnmaildir--cur-server
1566 nnmaildir--cur-server nil)
1567 (unintern (nnmaildir--srv-address server) nnmaildir--servers)))
1570 (defun nnmaildir-request-close ()
1571 (let (servers buffer)
1572 (mapatoms (lambda (server)
1573 (setq servers (cons (symbol-name server) servers)))
1575 (mapcar 'nnmaildir-close-server servers)
1576 (setq buffer (get-buffer " *nnmaildir work*"))
1577 (if buffer (kill-buffer buffer))
1578 (setq buffer (get-buffer " *nnmaildir nov*"))
1579 (if buffer (kill-buffer buffer))
1580 (setq buffer (get-buffer " *nnmaildir move*"))
1581 (if buffer (kill-buffer buffer)))
1584 (provide 'nnmaildir)
1587 ;; indent-tabs-mode: t
1591 ;;; nnmaildir.el ends here