Merge the t-gnus-6_17-quimby branch.
[elisp/gnus.git-] / lisp / nnml.el
1 ;;; nnml.el --- mail spool access for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;;   2004, 2005, 2006 Free Software Foundation, Inc.
5
6 ;; Authors: Didier Verna <didier@xemacs.org> (adding compaction)
7 ;;      Simon Josefsson <simon@josefsson.org> (adding MARKS)
8 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; Keywords: news, mail
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
28
29 ;;; Commentary:
30
31 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
32 ;; For an overview of what the interface functions do, please see the
33 ;; Gnus sources.
34
35 ;;; Code:
36
37 (eval-when-compile (require 'cl))
38
39 (require 'gnus)
40 (require 'nnheader)
41 (require 'nnmail)
42 (require 'nnoo)
43
44 (eval-and-compile
45   (autoload 'gnus-article-unpropagatable-p "gnus-sum")
46   (autoload 'gnus-backlog-remove-article "gnus-bcklg"))
47
48 (nnoo-declare nnml)
49
50 (defvoo nnml-directory message-directory
51   "Spool directory for the nnml mail backend.")
52
53 (defvoo nnml-active-file
54     (expand-file-name "active" nnml-directory)
55   "Mail active file.")
56
57 (defvoo nnml-newsgroups-file
58     (expand-file-name "newsgroups" nnml-directory)
59   "Mail newsgroups description file.")
60
61 (defvoo nnml-get-new-mail t
62   "If non-nil, nnml will check the incoming mail file and split the mail.")
63
64 (defvoo nnml-nov-is-evil nil
65   "If non-nil, Gnus will never generate and use nov databases for mail spools.
66 Using nov databases will speed up header fetching considerably.
67 This variable shouldn't be flipped much.  If you have, for some reason,
68 set this to t, and want to set it to nil again, you should always run
69 the `nnml-generate-nov-databases' command.  The function will go
70 through all nnml directories and generate nov databases for them
71 all.  This may very well take some time.")
72
73 (defvoo nnml-marks-is-evil nil
74   "If non-nil, Gnus will never generate and use marks file for mail spools.
75 Using marks files makes it possible to backup and restore mail groups
76 separately from `.newsrc.eld'.  If you have, for some reason, set this
77 to t, and want to set it to nil again, you should always remove the
78 corresponding marks file (usually named `.marks' in the nnml group
79 directory, but see `nnml-marks-file-name') for the group.  Then the
80 marks file will be regenerated properly by Gnus.")
81
82 (defvoo nnml-prepare-save-mail-hook nil
83   "Hook run narrowed to an article before saving.")
84
85 (defvoo nnml-inhibit-expiry nil
86   "If non-nil, inhibit expiry.")
87
88 (defvoo nnml-use-compressed-files nil
89   "If non-nil, allow using compressed message files.
90
91 If it is a string, use it as the file extension which specifies
92 the comression program.  You can set it to \".bz2\" if your Emacs
93 supports auto-compression using the bzip2 program.  A value of t
94 is equivalent to \".gz\".")
95
96 (defvoo nnml-compressed-files-size-threshold 1000
97   "Default size threshold for compressed message files.
98 Message files with bodies larger than that many characters will
99 be automatically compressed if `nnml-use-compressed-files' is
100 non-nil.")
101
102 \f
103
104 (defconst nnml-version "nnml 1.0"
105   "nnml version.")
106
107 (defvoo nnml-nov-file-name ".overview")
108 (defvoo nnml-marks-file-name ".marks")
109
110 (defvoo nnml-current-directory nil)
111 (defvoo nnml-current-group nil)
112 (defvoo nnml-status-string "")
113 (defvoo nnml-nov-buffer-alist nil)
114 (defvoo nnml-group-alist nil)
115 (defvoo nnml-active-timestamp nil)
116 (defvoo nnml-article-file-alist nil)
117
118 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
119
120 (defvar nnml-nov-buffer-file-name nil)
121
122 (defvoo nnml-file-coding-system nnmail-file-coding-system)
123
124 (defvoo nnml-marks nil)
125
126 (defvar nnml-marks-modtime (gnus-make-hashtable))
127
128 \f
129 ;;; Interface functions.
130
131 (nnoo-define-basics nnml)
132
133 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
134   (when (nnml-possibly-change-directory group server)
135     (save-excursion
136       (set-buffer nntp-server-buffer)
137       (erase-buffer)
138       (let* ((file nil)
139              (number (length sequence))
140              (count 0)
141              (file-name-coding-system nnmail-pathname-coding-system)
142              beg article)
143         (if (stringp (car sequence))
144             'headers
145           (if (nnml-retrieve-headers-with-nov sequence fetch-old)
146               'nov
147             (while sequence
148               (setq article (car sequence))
149               (setq file (nnml-article-to-file article))
150               (when (and file
151                          (file-exists-p file)
152                          (not (file-directory-p file)))
153                 (insert (format "221 %d Article retrieved.\n" article))
154                 (setq beg (point))
155                 (nnheader-insert-head file)
156                 (goto-char beg)
157                 (if (re-search-forward "\n\r?\n" nil t)
158                     (forward-char -1)
159                   (goto-char (point-max))
160                   (insert "\n\n"))
161                 (insert ".\n")
162                 (delete-region (point) (point-max)))
163               (setq sequence (cdr sequence))
164               (setq count (1+ count))
165               (and (numberp nnmail-large-newsgroup)
166                    (> number nnmail-large-newsgroup)
167                    (zerop (% count 20))
168                    (nnheader-message 6 "nnml: Receiving headers... %d%%"
169                                      (/ (* count 100) number))))
170
171             (and (numberp nnmail-large-newsgroup)
172                  (> number nnmail-large-newsgroup)
173                  (nnheader-message 6 "nnml: Receiving headers...done"))
174
175             (nnheader-fold-continuation-lines)
176             'headers))))))
177
178 (deffoo nnml-open-server (server &optional defs)
179   (nnoo-change-server 'nnml server defs)
180   (when (not (file-exists-p nnml-directory))
181     (ignore-errors (make-directory nnml-directory t)))
182   (cond
183    ((not (file-exists-p nnml-directory))
184     (nnml-close-server)
185     (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
186    ((not (file-directory-p (file-truename nnml-directory)))
187     (nnml-close-server)
188     (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
189    (t
190     (nnheader-report 'nnml "Opened server %s using directory %s"
191                      server nnml-directory)
192     t)))
193
194 (deffoo nnml-request-regenerate (server)
195   (nnml-possibly-change-directory nil server)
196   (nnml-generate-nov-databases server)
197   t)
198
199 (deffoo nnml-request-article (id &optional group server buffer)
200   (nnml-possibly-change-directory group server)
201   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
202          (file-name-coding-system nnmail-pathname-coding-system)
203          path gpath group-num)
204     (if (stringp id)
205         (when (and (setq group-num (nnml-find-group-number id))
206                    (cdr
207                     (assq (cdr group-num)
208                           (nnheader-article-to-file-alist
209                            (setq gpath
210                                  (nnmail-group-pathname
211                                   (car group-num)
212                                   nnml-directory))))))
213           (setq path (concat gpath (int-to-string (cdr group-num)))))
214       (setq path (nnml-article-to-file id)))
215     (cond
216      ((not path)
217       (nnheader-report 'nnml "No such article: %s" id))
218      ((not (file-exists-p path))
219       (nnheader-report 'nnml "No such file: %s" path))
220      ((file-directory-p path)
221       (nnheader-report 'nnml "File is a directory: %s" path))
222      ((not (save-excursion (let ((nnmail-file-coding-system
223                                   nnml-file-coding-system))
224                              (nnmail-find-file path))))
225       (nnheader-report 'nnml "Couldn't read file: %s" path))
226      (t
227       (nnheader-report 'nnml "Article %s retrieved" id)
228       ;; We return the article number.
229       (cons (if group-num (car group-num) group)
230             (string-to-number (file-name-nondirectory path)))))))
231
232 (deffoo nnml-request-group (group &optional server dont-check)
233   (let ((file-name-coding-system nnmail-pathname-coding-system))
234     (cond
235      ((not (nnml-possibly-change-directory group server))
236       (nnheader-report 'nnml "Invalid group (no such directory)"))
237      ((not (file-exists-p nnml-current-directory))
238       (nnheader-report 'nnml "Directory %s does not exist"
239                        nnml-current-directory))
240      ((not (file-directory-p nnml-current-directory))
241       (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
242      (dont-check
243       (nnheader-report 'nnml "Group %s selected" group)
244       t)
245      (t
246       (nnheader-re-read-dir nnml-current-directory)
247       (nnmail-activate 'nnml)
248       (let ((active (nth 1 (assoc group nnml-group-alist))))
249         (if (not active)
250             (nnheader-report 'nnml "No such group: %s" group)
251           (nnheader-report 'nnml "Selected group %s" group)
252           (nnheader-insert "211 %d %d %d %s\n"
253                            (max (1+ (- (cdr active) (car active))) 0)
254                            (car active) (cdr active) group)))))))
255
256 (deffoo nnml-request-scan (&optional group server)
257   (setq nnml-article-file-alist nil)
258   (nnml-possibly-change-directory group server)
259   (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
260
261 (deffoo nnml-close-group (group &optional server)
262   (setq nnml-article-file-alist nil)
263   t)
264
265 (deffoo nnml-request-create-group (group &optional server args)
266   (nnml-possibly-change-directory nil server)
267   (nnmail-activate 'nnml)
268   (cond
269    ((assoc group nnml-group-alist)
270     t)
271    ((and (file-exists-p (nnmail-group-pathname group nnml-directory))
272          (not (file-directory-p (nnmail-group-pathname group nnml-directory))))
273     (nnheader-report 'nnml "%s is a file"
274                      (nnmail-group-pathname group nnml-directory)))
275    (t
276     (let (active)
277       (push (list group (setq active (cons 1 0)))
278             nnml-group-alist)
279       (nnml-possibly-create-directory group)
280       (nnml-possibly-change-directory group server)
281       (let ((articles (nnml-directory-articles nnml-current-directory)))
282         (when articles
283           (setcar active (apply 'min articles))
284           (setcdr active (apply 'max articles))))
285       (nnmail-save-active nnml-group-alist nnml-active-file)
286       t))))
287
288 (deffoo nnml-request-list (&optional server)
289   (save-excursion
290     (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
291           (file-name-coding-system nnmail-pathname-coding-system))
292       (nnmail-find-file nnml-active-file))
293     (setq nnml-group-alist (nnmail-get-active))
294     t))
295
296 (deffoo nnml-request-newgroups (date &optional server)
297   (nnml-request-list server))
298
299 (deffoo nnml-request-list-newsgroups (&optional server)
300   (save-excursion
301     (nnmail-find-file nnml-newsgroups-file)))
302
303 (deffoo nnml-request-expire-articles (articles group &optional server force)
304   (nnml-possibly-change-directory group server)
305   (let ((active-articles
306          (nnml-directory-articles nnml-current-directory))
307         (is-old t)
308         article rest mod-time number)
309     (nnmail-activate 'nnml)
310
311     (setq active-articles (sort active-articles '<))
312     ;; Articles not listed in active-articles are already gone,
313     ;; so don't try to expire them.
314     (setq articles (gnus-sorted-intersection articles active-articles))
315
316     (while (and articles is-old)
317       (if (and (setq article (nnml-article-to-file
318                               (setq number (pop articles))))
319                (setq mod-time (nth 5 (file-attributes article)))
320                (nnml-deletable-article-p group number)
321                (setq is-old (nnmail-expired-article-p group mod-time force
322                                                       nnml-inhibit-expiry)))
323           (progn
324             ;; Allow a special target group.
325             (unless (eq nnmail-expiry-target 'delete)
326               (with-temp-buffer
327                 (nnml-request-article number group server (current-buffer))
328                 (let (nnml-current-directory
329                       nnml-current-group
330                       nnml-article-file-alist)
331                   (nnmail-expiry-target-group nnmail-expiry-target group)))
332               ;; Maybe directory is changed during nnmail-expiry-target-group.
333               (nnml-possibly-change-directory group server))
334             (nnheader-message 5 "Deleting article %s in %s"
335                               number group)
336             (condition-case ()
337                 (funcall nnmail-delete-file-function article)
338               (file-error
339                (push number rest)))
340             (setq active-articles (delq number active-articles))
341             (nnml-nov-delete-article group number))
342         (push number rest)))
343     (let ((active (nth 1 (assoc group nnml-group-alist))))
344       (when active
345         (setcar active (or (and active-articles
346                                 (apply 'min active-articles))
347                            (1+ (cdr active)))))
348       (nnmail-save-active nnml-group-alist nnml-active-file))
349     (nnml-save-nov)
350     (nconc rest articles)))
351
352 (deffoo nnml-request-move-article
353     (article group server accept-form &optional last move-is-internal)
354   (let ((buf (get-buffer-create " *nnml move*"))
355         result)
356     (nnml-possibly-change-directory group server)
357     (nnml-update-file-alist)
358     (and
359      (nnml-deletable-article-p group article)
360      (nnml-request-article article group server)
361      (let (nnml-current-directory
362            nnml-current-group
363            nnml-article-file-alist)
364        (save-excursion
365          (set-buffer buf)
366          (insert-buffer-substring nntp-server-buffer)
367          (setq result (eval accept-form))
368          (kill-buffer (current-buffer))
369          result))
370      (progn
371        (nnml-possibly-change-directory group server)
372        (condition-case ()
373            (funcall nnmail-delete-file-function
374                     (nnml-article-to-file  article))
375          (file-error nil))
376        (nnml-nov-delete-article group article)
377        (when last
378          (nnml-save-nov)
379          (nnmail-save-active nnml-group-alist nnml-active-file))))
380     result))
381
382 (deffoo nnml-request-accept-article (group &optional server last)
383   (nnml-possibly-change-directory group server)
384   (nnmail-check-syntax)
385   (let (result)
386     (when nnmail-cache-accepted-message-ids
387       (nnmail-cache-insert (nnmail-fetch-field "message-id")
388                            group
389                            (nnmail-fetch-field "subject")
390                            (nnmail-fetch-field "from")))
391     (if (stringp group)
392         (and
393          (nnmail-activate 'nnml)
394          (setq result (car (nnml-save-mail
395                             (list (cons group (nnml-active-number group))))))
396          (progn
397            (nnmail-save-active nnml-group-alist nnml-active-file)
398            (and last (nnml-save-nov))))
399       (and
400        (nnmail-activate 'nnml)
401        (if (and (not (setq result (nnmail-article-group 'nnml-active-number)))
402                 (yes-or-no-p "Moved to `junk' group; delete article? "))
403            (setq result 'junk)
404          (setq result (car (nnml-save-mail result))))
405        (when last
406          (nnmail-save-active nnml-group-alist nnml-active-file)
407          (when nnmail-cache-accepted-message-ids
408            (nnmail-cache-close))
409          (nnml-save-nov))))
410     result))
411
412 (deffoo nnml-request-post (&optional server)
413   (nnmail-do-request-post 'nnml-request-accept-article server))
414
415 (deffoo nnml-request-replace-article (article group buffer)
416   (nnml-possibly-change-directory group)
417   (save-excursion
418     (set-buffer buffer)
419     (nnml-possibly-create-directory group)
420     (let ((chars (nnmail-insert-lines))
421           (art (concat (int-to-string article) "\t"))
422           headers)
423       (when (ignore-errors
424               (nnmail-write-region
425                (point-min) (point-max)
426                (or (nnml-article-to-file article)
427                    (expand-file-name (int-to-string article)
428                                      nnml-current-directory))
429                nil (if (nnheader-be-verbose 5) nil 'nomesg))
430               t)
431         (setq headers (nnml-parse-head chars article))
432         ;; Replace the NOV line in the NOV file.
433         (save-excursion
434           (set-buffer (nnml-open-nov group))
435           (goto-char (point-min))
436           (if (or (looking-at art)
437                   (search-forward (concat "\n" art) nil t))
438               ;; Delete the old NOV line.
439               (gnus-delete-line)
440             ;; The line isn't here, so we have to find out where
441             ;; we should insert it.  (This situation should never
442             ;; occur, but one likes to make sure...)
443             (while (and (looking-at "[0-9]+\t")
444                         (< (string-to-number
445                             (buffer-substring
446                              (match-beginning 0) (match-end 0)))
447                            article)
448                         (zerop (forward-line 1)))))
449           (beginning-of-line)
450           (nnheader-insert-nov headers)
451           (nnml-save-nov)
452           t)))))
453
454 (deffoo nnml-request-delete-group (group &optional force server)
455   (nnml-possibly-change-directory group server)
456   (when force
457     ;; Delete all articles in GROUP.
458     (let ((articles
459            (directory-files
460             nnml-current-directory t
461             (concat nnheader-numerical-short-files
462                     "\\|" (regexp-quote nnml-nov-file-name) "$"
463                     "\\|" (regexp-quote nnml-marks-file-name) "$"))))
464       (dolist (article articles)
465         (when (file-writable-p article)
466           (nnheader-message 5 "Deleting article %s in %s..." article group)
467           (funcall nnmail-delete-file-function article))))
468     ;; Try to delete the directory itself.
469     (ignore-errors (delete-directory nnml-current-directory)))
470   ;; Remove the group from all structures.
471   (setq nnml-group-alist
472         (delq (assoc group nnml-group-alist) nnml-group-alist)
473         nnml-current-group nil
474         nnml-current-directory nil)
475   ;; Save the active file.
476   (nnmail-save-active nnml-group-alist nnml-active-file)
477   t)
478
479 (deffoo nnml-request-rename-group (group new-name &optional server)
480   (nnml-possibly-change-directory group server)
481   (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
482         (old-dir (nnmail-group-pathname group nnml-directory)))
483     (when (ignore-errors
484             (make-directory new-dir t)
485             t)
486       ;; We move the articles file by file instead of renaming
487       ;; the directory -- there may be subgroups in this group.
488       ;; One might be more clever, I guess.
489       (dolist (file (nnheader-article-to-file-alist old-dir))
490         (rename-file
491          (concat old-dir (cdr file))
492          (concat new-dir (cdr file))))
493       ;; Move .overview file.
494       (let ((overview (concat old-dir nnml-nov-file-name)))
495         (when (file-exists-p overview)
496           (rename-file overview (concat new-dir nnml-nov-file-name))))
497       ;; Move .marks file.
498       (let ((marks (concat old-dir nnml-marks-file-name)))
499         (when (file-exists-p marks)
500           (rename-file marks (concat new-dir nnml-marks-file-name))))
501       (when (<= (length (directory-files old-dir)) 2)
502         (ignore-errors (delete-directory old-dir)))
503       ;; That went ok, so we change the internal structures.
504       (let ((entry (assoc group nnml-group-alist)))
505         (when entry
506           (setcar entry new-name))
507         (setq nnml-current-directory nil
508               nnml-current-group nil)
509         ;; Save the new group alist.
510         (nnmail-save-active nnml-group-alist nnml-active-file)
511         t))))
512
513 (deffoo nnml-set-status (article name value &optional group server)
514   (nnml-possibly-change-directory group server)
515   (let ((file (nnml-article-to-file article)))
516     (cond
517      ((not (file-exists-p file))
518       (nnheader-report 'nnml "File %s does not exist" file))
519      (t
520       (with-temp-file file
521         (nnheader-insert-file-contents file)
522         (nnmail-replace-status name value))
523       t))))
524
525 \f
526 ;;; Internal functions.
527
528 (defun nnml-article-to-file (article)
529   (nnml-update-file-alist)
530   (let (file)
531     (if (setq file
532               (if nnml-use-compressed-files
533                   (cdr (assq article nnml-article-file-alist))
534                 (number-to-string article)))
535         (expand-file-name file nnml-current-directory)
536       (when (not nnheader-directory-files-is-safe)
537         ;; Just to make sure nothing went wrong when reading over NFS --
538         ;; check once more.
539         (when (file-exists-p
540                (setq file (expand-file-name (number-to-string article)
541                                             nnml-current-directory)))
542           (nnml-update-file-alist t)
543           file)))))
544
545 (defun nnml-deletable-article-p (group article)
546   "Say whether ARTICLE in GROUP can be deleted."
547   (let (path)
548     (when (setq path (nnml-article-to-file article))
549       (when (file-writable-p path)
550         (or (not nnmail-keep-last-article)
551             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
552                      article)))))))
553
554 ;; Find an article number in the current group given the Message-ID.
555 (defun nnml-find-group-number (id)
556   (save-excursion
557     (set-buffer (get-buffer-create " *nnml id*"))
558     (let ((alist nnml-group-alist)
559           number)
560       ;; We want to look through all .overview files, but we want to
561       ;; start with the one in the current directory.  It seems most
562       ;; likely that the article we are looking for is in that group.
563       (if (setq number (nnml-find-id nnml-current-group id))
564           (cons nnml-current-group number)
565         ;; It wasn't there, so we look through the other groups as well.
566         (while (and (not number)
567                     alist)
568           (or (string= (caar alist) nnml-current-group)
569               (setq number (nnml-find-id (caar alist) id)))
570           (or number
571               (setq alist (cdr alist))))
572         (and number
573              (cons (caar alist) number))))))
574
575 (defun nnml-find-id (group id)
576   (erase-buffer)
577   (let ((nov (expand-file-name nnml-nov-file-name
578                                (nnmail-group-pathname group nnml-directory)))
579         number found)
580     (when (file-exists-p nov)
581       (nnheader-insert-file-contents nov)
582       (while (and (not found)
583                   (search-forward id nil t)) ; We find the ID.
584         ;; And the id is in the fourth field.
585         (if (not (and (search-backward "\t" nil t 4)
586                       (not (search-backward "\t" (point-at-bol) t))))
587             (forward-line 1)
588           (beginning-of-line)
589           (setq found t)
590           ;; We return the article number.
591           (setq number
592                 (ignore-errors (read (current-buffer))))))
593       number)))
594
595 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
596   (if (or gnus-nov-is-evil nnml-nov-is-evil)
597       nil
598     (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
599       (when (file-exists-p nov)
600         (save-excursion
601           (set-buffer nntp-server-buffer)
602           (erase-buffer)
603           (nnheader-insert-file-contents nov)
604           (if (and fetch-old
605                    (not (numberp fetch-old)))
606               t                         ; Don't remove anything.
607             (nnheader-nov-delete-outside-range
608              (if fetch-old (max 1 (- (car articles) fetch-old))
609                (car articles))
610              (car (last articles)))
611             t))))))
612
613 (defun nnml-possibly-change-directory (group &optional server)
614   (when (and server
615              (not (nnml-server-opened server)))
616     (nnml-open-server server))
617   (if (not group)
618       t
619     (let ((pathname (nnmail-group-pathname group nnml-directory))
620           (file-name-coding-system nnmail-pathname-coding-system))
621       (when (not (equal pathname nnml-current-directory))
622         (setq nnml-current-directory pathname
623               nnml-current-group group
624               nnml-article-file-alist nil))
625       (file-exists-p nnml-current-directory))))
626
627 (defun nnml-possibly-create-directory (group)
628   (let ((dir (nnmail-group-pathname group nnml-directory)))
629     (unless (file-exists-p dir)
630       (make-directory (directory-file-name dir) t)
631       (nnheader-message 5 "Creating mail directory %s" dir))))
632
633 (defun nnml-save-mail (group-art)
634   "Called narrowed to an article."
635   (let (chars headers extension)
636     (setq chars (nnmail-insert-lines))
637     (setq extension
638          (and nnml-use-compressed-files
639               (> chars nnml-compressed-files-size-threshold)
640               (if (stringp nnml-use-compressed-files)
641                   nnml-use-compressed-files
642                 ".gz")))
643     (nnmail-insert-xref group-art)
644     (run-hooks 'nnmail-prepare-save-mail-hook)
645     (run-hooks 'nnml-prepare-save-mail-hook)
646     (goto-char (point-min))
647     (while (looking-at "From ")
648       (replace-match "X-From-Line: ")
649       (forward-line 1))
650     ;; We save the article in all the groups it belongs in.
651     (let ((ga group-art)
652           first)
653       (while ga
654         (nnml-possibly-create-directory (caar ga))
655         (let ((file (concat (nnmail-group-pathname
656                              (caar ga) nnml-directory)
657                             (int-to-string (cdar ga))
658                             extension)))
659           (if first
660               ;; It was already saved, so we just make a hard link.
661               (funcall nnmail-crosspost-link-function first file t)
662             ;; Save the article.
663             (nnmail-write-region (point-min) (point-max) file nil
664                                  (if (nnheader-be-verbose 5) nil 'nomesg))
665             (setq first file)))
666         (setq ga (cdr ga))))
667     ;; Generate a nov line for this article.  We generate the nov
668     ;; line after saving, because nov generation destroys the
669     ;; header.
670     (setq headers (nnml-parse-head chars))
671     ;; Output the nov line to all nov databases that should have it.
672     (let ((ga group-art))
673       (while ga
674         (nnml-add-nov (caar ga) (cdar ga) headers)
675         (setq ga (cdr ga))))
676     group-art))
677
678 (defun nnml-active-number (group)
679   "Compute the next article number in GROUP."
680   (let ((active (cadr (assoc group nnml-group-alist))))
681     ;; The group wasn't known to nnml, so we just create an active
682     ;; entry for it.
683     (unless active
684       ;; Perhaps the active file was corrupt?  See whether
685       ;; there are any articles in this group.
686       (nnml-possibly-create-directory group)
687       (nnml-possibly-change-directory group)
688       (unless nnml-article-file-alist
689         (setq nnml-article-file-alist
690               (sort
691                (nnml-current-group-article-to-file-alist)
692                'car-less-than-car)))
693       (setq active
694             (if nnml-article-file-alist
695                 (cons (caar nnml-article-file-alist)
696                       (caar (last nnml-article-file-alist)))
697               (cons 1 0)))
698       (push (list group active) nnml-group-alist))
699     (setcdr active (1+ (cdr active)))
700     (while (file-exists-p
701             (expand-file-name (int-to-string (cdr active))
702                               (nnmail-group-pathname group nnml-directory)))
703       (setcdr active (1+ (cdr active))))
704     (cdr active)))
705
706 (defun nnml-add-nov (group article headers)
707   "Add a nov line for the GROUP base."
708   (save-excursion
709     (set-buffer (nnml-open-nov group))
710     (goto-char (point-max))
711     (mail-header-set-number headers article)
712     (nnheader-insert-nov headers)))
713
714 (defsubst nnml-header-value ()
715   (buffer-substring (match-end 0) (point-at-eol)))
716
717 (defun nnml-parse-head (chars &optional number)
718   "Parse the head of the current buffer."
719   (save-excursion
720     (save-restriction
721       (unless (zerop (buffer-size))
722         (narrow-to-region
723          (goto-char (point-min))
724          (if (re-search-forward "\n\r?\n" nil t)
725              (1- (point))
726            (point-max))))
727       (let ((headers (nnheader-parse-naked-head)))
728         (mail-header-set-chars headers chars)
729         (mail-header-set-number headers number)
730         headers))))
731
732 (defun nnml-get-nov-buffer (group)
733   (let ((buffer (get-buffer-create (format " *nnml overview %s*" group))))
734     (save-excursion
735       (set-buffer buffer)
736       (set (make-local-variable 'nnml-nov-buffer-file-name)
737            (expand-file-name
738             nnml-nov-file-name
739             (nnmail-group-pathname group nnml-directory)))
740       (erase-buffer)
741       (when (file-exists-p nnml-nov-buffer-file-name)
742         (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
743     buffer))
744
745 (defun nnml-open-nov (group)
746   (or (cdr (assoc group nnml-nov-buffer-alist))
747       (let ((buffer (nnml-get-nov-buffer group)))
748         (push (cons group buffer) nnml-nov-buffer-alist)
749         buffer)))
750
751 (defun nnml-save-nov ()
752   (save-excursion
753     (while nnml-nov-buffer-alist
754       (when (buffer-name (cdar nnml-nov-buffer-alist))
755         (set-buffer (cdar nnml-nov-buffer-alist))
756         (when (buffer-modified-p)
757           (nnmail-write-region (point-min) (point-max)
758                                nnml-nov-buffer-file-name nil 'nomesg))
759         (set-buffer-modified-p nil)
760         (kill-buffer (current-buffer)))
761       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
762
763 ;;;###autoload
764 (defun nnml-generate-nov-databases (&optional server)
765   "Generate NOV databases in all nnml directories."
766   (interactive (list (or (nnoo-current-server 'nnml) "")))
767   ;; Read the active file to make sure we don't re-use articles
768   ;; numbers in empty groups.
769   (nnmail-activate 'nnml)
770   (unless (nnml-server-opened server)
771     (nnml-open-server server))
772   (setq nnml-directory (expand-file-name nnml-directory))
773   ;; Recurse down the directories.
774   (nnml-generate-nov-databases-directory nnml-directory nil t)
775   ;; Save the active file.
776   (nnmail-save-active nnml-group-alist nnml-active-file))
777
778 (defun nnml-generate-nov-databases-directory (dir &optional seen no-active)
779   "Regenerate the NOV database in DIR.
780
781 Unless no-active is non-nil, update the active file too."
782   (interactive "DRegenerate NOV in: ")
783   (setq dir (file-name-as-directory dir))
784   ;; Only scan this sub-tree if we haven't been here yet.
785   (unless (member (file-truename dir) seen)
786     (push (file-truename dir) seen)
787     ;; We descend recursively
788     (dolist (dir (directory-files dir t nil t))
789       (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
790                  (file-directory-p dir))
791         (nnml-generate-nov-databases-directory dir seen)))
792     ;; Do this directory.
793     (let ((files (sort (nnheader-article-to-file-alist dir)
794                        'car-less-than-car)))
795       (if (not files)
796           (let* ((group (nnheader-file-to-group
797                          (directory-file-name dir) nnml-directory))
798                  (info (cadr (assoc group nnml-group-alist))))
799             (when info
800               (setcar info (1+ (cdr info)))))
801         (funcall nnml-generate-active-function dir)
802         ;; Generate the nov file.
803         (nnml-generate-nov-file dir files)
804         (unless no-active
805           (nnmail-save-active nnml-group-alist nnml-active-file))))))
806
807 (eval-when-compile (defvar files))
808 (defun nnml-generate-active-info (dir)
809   ;; Update the active info for this group.
810   (let* ((group (nnheader-file-to-group
811                  (directory-file-name dir) nnml-directory))
812          (entry (assoc group nnml-group-alist))
813          (last (or (caadr entry) 0)))
814     (setq nnml-group-alist (delq entry nnml-group-alist))
815     (push (list group
816                 (cons (or (caar files) (1+ last))
817                       (max last
818                            (or (caar (last files))
819                                0))))
820           nnml-group-alist)))
821
822 (defun nnml-generate-nov-file (dir files)
823   (let* ((dir (file-name-as-directory dir))
824          (nov (concat dir nnml-nov-file-name))
825          (nov-buffer (get-buffer-create " *nov*"))
826          chars file headers)
827     (save-excursion
828       ;; Init the nov buffer.
829       (set-buffer nov-buffer)
830       (buffer-disable-undo)
831       (erase-buffer)
832       (set-buffer nntp-server-buffer)
833       ;; Delete the old NOV file.
834       (when (file-exists-p nov)
835         (funcall nnmail-delete-file-function nov))
836       (while files
837         (unless (file-directory-p (setq file (concat dir (cdar files))))
838           (erase-buffer)
839           (nnheader-insert-file-contents file)
840           (narrow-to-region
841            (goto-char (point-min))
842            (progn
843              (re-search-forward "\n\r?\n" nil t)
844              (setq chars (- (point-max) (point)))
845              (max (point-min) (1- (point)))))
846           (unless (zerop (buffer-size))
847             (goto-char (point-min))
848             (setq headers (nnml-parse-head chars (caar files)))
849             (save-excursion
850               (set-buffer nov-buffer)
851               (goto-char (point-max))
852               (nnheader-insert-nov headers)))
853           (widen))
854         (setq files (cdr files)))
855       (save-excursion
856         (set-buffer nov-buffer)
857         (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
858         (kill-buffer (current-buffer))))))
859
860 (defun nnml-nov-delete-article (group article)
861   (save-excursion
862     (set-buffer (nnml-open-nov group))
863     (when (nnheader-find-nov-line article)
864       (delete-region (point) (progn (forward-line 1) (point)))
865       (when (bobp)
866         (let ((active (cadr (assoc group nnml-group-alist)))
867               num)
868           (when active
869             (if (eobp)
870                 (setf (car active) (1+ (cdr active)))
871               (when (and (setq num (ignore-errors (read (current-buffer))))
872                          (numberp num))
873                 (setf (car active) num)))))))
874     t))
875
876 (defun nnml-update-file-alist (&optional force)
877   (when nnml-use-compressed-files
878     (when (or (not nnml-article-file-alist)
879               force)
880       (setq nnml-article-file-alist
881             (nnml-current-group-article-to-file-alist)))))
882
883 (defun nnml-directory-articles (dir)
884   "Return a list of all article files in a directory.
885 Use the nov database for that directory if available."
886   (if (or gnus-nov-is-evil nnml-nov-is-evil
887           (not (file-exists-p
888                 (expand-file-name nnml-nov-file-name dir))))
889       (nnheader-directory-articles dir)
890     ;; build list from .overview if available
891     ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
892     ;; defvoo'd, and we might get called when it hasn't been swapped in.
893     (save-excursion
894       (let ((list nil)
895             art
896             (buffer (nnml-get-nov-buffer nnml-current-group)))
897         (set-buffer buffer)
898         (goto-char (point-min))
899         (while (not (eobp))
900           (setq art (read (current-buffer)))
901           (push art list)
902           (forward-line 1))
903         list))))
904
905 (defun nnml-current-group-article-to-file-alist ()
906   "Return an alist of article/file pairs in the current group.
907 Use the nov database for the current group if available."
908   (if (or nnml-use-compressed-files
909           gnus-nov-is-evil
910           nnml-nov-is-evil
911           (not (file-exists-p
912                 (expand-file-name nnml-nov-file-name
913                                   nnml-current-directory))))
914       (nnheader-article-to-file-alist nnml-current-directory)
915     ;; build list from .overview if available
916     (save-excursion
917       (let ((alist nil)
918             (buffer (nnml-get-nov-buffer nnml-current-group))
919             art)
920         (set-buffer buffer)
921         (goto-char (point-min))
922         (while (not (eobp))
923           (setq art (read (current-buffer)))
924           ;; assume file name is unadorned (ie. not compressed etc)
925           (push (cons art (int-to-string art)) alist)
926           (forward-line 1))
927         alist))))
928
929 (deffoo nnml-request-set-mark (group actions &optional server)
930   (nnml-possibly-change-directory group server)
931   (unless nnml-marks-is-evil
932     (nnml-open-marks group server)
933     (dolist (action actions)
934       (let ((range (nth 0 action))
935             (what  (nth 1 action))
936             (marks (nth 2 action)))
937         (assert (or (eq what 'add) (eq what 'del)) nil
938                 "Unknown request-set-mark action: %s" what)
939         (dolist (mark marks)
940           (setq nnml-marks (gnus-update-alist-soft
941                             mark
942                             (funcall (if (eq what 'add) 'gnus-range-add
943                                        'gnus-remove-from-range)
944                                      (cdr (assoc mark nnml-marks)) range)
945                             nnml-marks)))))
946     (nnml-save-marks group server))
947   nil)
948
949 (deffoo nnml-request-update-info (group info &optional server)
950   (nnml-possibly-change-directory group server)
951   (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group))
952     (nnheader-message 8 "Updating marks for %s..." group)
953     (nnml-open-marks group server)
954     ;; Update info using `nnml-marks'.
955     (mapc (lambda (pred)
956             (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
957               (gnus-info-set-marks
958                info
959                (gnus-update-alist-soft
960                 (cdr pred)
961                 (cdr (assq (cdr pred) nnml-marks))
962                 (gnus-info-marks info))
963                t)))
964           gnus-article-mark-lists)
965     (let ((seen (cdr (assq 'read nnml-marks))))
966       (gnus-info-set-read info
967                           (if (and (integerp (car seen))
968                                    (null (cdr seen)))
969                               (list (cons (car seen) (car seen)))
970                             seen)))
971     (nnheader-message 8 "Updating marks for %s...done" group))
972   info)
973
974 (defun nnml-marks-changed-p (group)
975   (let ((file (expand-file-name nnml-marks-file-name
976                                 (nnmail-group-pathname group nnml-directory))))
977     (if (null (gnus-gethash file nnml-marks-modtime))
978         t ;; never looked at marks file, assume it has changed
979       (not (equal (gnus-gethash file nnml-marks-modtime)
980                   (nth 5 (file-attributes file)))))))
981
982 (defun nnml-save-marks (group server)
983   (let ((file-name-coding-system nnmail-pathname-coding-system)
984         (file (expand-file-name nnml-marks-file-name
985                                 (nnmail-group-pathname group nnml-directory))))
986     (condition-case err
987         (progn
988           (nnml-possibly-create-directory group)
989           (with-temp-file file
990             (erase-buffer)
991             (gnus-prin1 nnml-marks)
992             (insert "\n"))
993           (gnus-sethash file
994                         (nth 5 (file-attributes file))
995                         nnml-marks-modtime))
996       (error (or (gnus-yes-or-no-p
997                   (format "Could not write to %s (%s).  Continue? " file err))
998                  (error "Cannot write to %s (%s)" file err))))))
999
1000 (defun nnml-open-marks (group server)
1001   (let ((file (expand-file-name
1002                nnml-marks-file-name
1003                (nnmail-group-pathname group nnml-directory))))
1004     (if (file-exists-p file)
1005         (condition-case err
1006             (with-temp-buffer
1007               (gnus-sethash file (nth 5 (file-attributes file))
1008                             nnml-marks-modtime)
1009               (nnheader-insert-file-contents file)
1010               (setq nnml-marks (read (current-buffer)))
1011               (dolist (el gnus-article-unpropagated-mark-lists)
1012                 (setq nnml-marks (gnus-remassoc el nnml-marks))))
1013           (error (or (gnus-yes-or-no-p
1014                       (format "Error reading nnml marks file %s (%s).  Continuing will use marks from .newsrc.eld.  Continue? " file err))
1015                      (error "Cannot read nnml marks file %s (%s)" file err))))
1016       ;; User didn't have a .marks file.  Probably first time
1017       ;; user of the .marks stuff.  Bootstrap it from .newsrc.eld.
1018       (let ((info (gnus-get-info
1019                    (gnus-group-prefixed-name
1020                     group
1021                     (gnus-server-to-method (format "nnml:%s" server))))))
1022         (nnheader-message 7 "Bootstrapping marks for %s..." group)
1023         (setq nnml-marks (gnus-info-marks info))
1024         (push (cons 'read (gnus-info-read info)) nnml-marks)
1025         (dolist (el gnus-article-unpropagated-mark-lists)
1026           (setq nnml-marks (gnus-remassoc el nnml-marks)))
1027         (nnml-save-marks group server)
1028         (nnheader-message 7 "Bootstrapping marks for %s...done" group)))))
1029
1030
1031 ;;;
1032 ;;; Group and server compaction. -- dvl
1033 ;;;
1034
1035 ;; #### FIXME: this function handles self Xref: entry correctly, but I don't
1036 ;; #### know how to handle external cross-references. I actually don't know if
1037 ;; #### this is handled correctly elsewhere. For instance, what happens if you
1038 ;; #### move all articles to a new group (that's what people do for manual
1039 ;; #### compaction) ?
1040
1041 ;; #### NOTE: the function below handles the article backlog. This is
1042 ;; #### conceptually the wrong place to do it because the backend is at a
1043 ;; #### lower level. However, this is the only place where we have the needed
1044 ;; #### information to do the job. Ideally, this function should not handle
1045 ;; #### the backlog by itself, but return a list of moved groups / articles to
1046 ;; #### the caller. This will become important to avoid code duplication when
1047 ;; #### other backends get a compaction feature. Also, note that invalidating
1048 ;; #### the "original article buffer" is already done at an upper level.
1049
1050 ;; Shouldn't `nnml-request-compact-group' be interactive? --rsteib
1051
1052 (defun nnml-request-compact-group (group &optional server save)
1053   (nnml-possibly-change-directory group server)
1054   (unless nnml-article-file-alist
1055     (setq nnml-article-file-alist
1056           (sort (nnml-current-group-article-to-file-alist)
1057                 'car-less-than-car)))
1058   (if (not nnml-article-file-alist)
1059       ;; The group is empty: do nothing but return t
1060       t
1061     ;; The group is not empty:
1062     (let* ((group-full-name
1063             (gnus-group-prefixed-name
1064              group
1065              (gnus-server-to-method (format "nnml:%s" server))))
1066            (info (gnus-get-info group-full-name))
1067            (new-number 1)
1068            compacted)
1069       (let ((articles nnml-article-file-alist)
1070             article)
1071         (while (setq article (pop articles))
1072           (let ((old-number (car article)))
1073             (when (> old-number new-number)
1074               ;; There is a gap here:
1075               (let ((old-number-string (int-to-string old-number))
1076                     (new-number-string (int-to-string new-number)))
1077                 (setq compacted t)
1078                 ;; #### NOTE: `nnml-article-to-file' calls
1079                 ;; #### `nnml-update-file-alist'  (which in turn calls
1080                 ;; #### `nnml-current-group-article-to-file-alist', which
1081                 ;; #### might use the NOV database). This might turn out to be
1082                 ;; #### inefficient. In that case, we will do the work
1083                 ;; #### manually.
1084                 ;; 1/ Move the article to a new file:
1085                 (let* ((oldfile (nnml-article-to-file old-number))
1086                        (newfile
1087                         (gnus-replace-in-string
1088                          oldfile (concat "\\("
1089                                          old-number-string
1090                                          "\\)\\(\\(\\.gz\\)?\\)$")
1091                          (concat new-number-string "\\2"))))
1092                   (with-current-buffer nntp-server-buffer
1093                     (nnmail-find-file oldfile)
1094                     ;; Update the Xref header in the article itself:
1095                     (when (and (re-search-forward "^Xref: [^ ]+ " nil t)
1096                                (re-search-forward
1097                                 (concat "\\<"
1098                                         (regexp-quote
1099                                          (concat group ":" old-number-string))
1100                                         "\\>")
1101                                 (point-at-eol) t))
1102                       (replace-match
1103                        (concat group ":" new-number-string)))
1104                     ;; Save to the new file:
1105                     (nnmail-write-region (point-min) (point-max) newfile))
1106                   (funcall nnmail-delete-file-function oldfile))
1107                 ;; 2/ Update all marks for this article:
1108                 ;; #### NOTE: it is possible that the new article number
1109                 ;; #### already belongs to a range, whereas the corresponding
1110                 ;; #### article doesn't exist (for example, if you delete an
1111                 ;; #### article). For that reason, it is important to update
1112                 ;; #### the ranges (meaning remove inexistant articles) before
1113                 ;; #### doing anything on them.
1114                 ;; 2 a/ read articles:
1115                 (let ((read (gnus-info-read info)))
1116                   (setq read (gnus-remove-from-range read (list new-number)))
1117                   (when (gnus-member-of-range old-number read)
1118                     (setq read (gnus-remove-from-range read (list old-number)))
1119                     (setq read (gnus-add-to-range read (list new-number))))
1120                   (gnus-info-set-read info read))
1121                 ;; 2 b/ marked articles:
1122                 (let ((oldmarks (gnus-info-marks info))
1123                       mark newmarks)
1124                   (while (setq mark (pop oldmarks))
1125                     (setcdr mark (gnus-remove-from-range (cdr mark)
1126                                                          (list new-number)))
1127                     (when (gnus-member-of-range old-number (cdr mark))
1128                       (setcdr mark (gnus-remove-from-range (cdr mark)
1129                                                            (list old-number)))
1130                       (setcdr mark (gnus-add-to-range (cdr mark)
1131                                                       (list new-number))))
1132                     (push mark newmarks))
1133                   (gnus-info-set-marks info newmarks))
1134                 ;; 3/ Update the NOV entry for this article:
1135                 (unless nnml-nov-is-evil
1136                   (save-excursion
1137                     (set-buffer (nnml-open-nov group))
1138                     (when (nnheader-find-nov-line old-number)
1139                       ;; Replace the article number:
1140                       (looking-at old-number-string)
1141                       (replace-match new-number-string nil t)
1142                       ;; Update the Xref header:
1143                       (when (re-search-forward
1144                              (concat "\\(Xref:[^\t\n]* \\)\\<"
1145                                      (regexp-quote
1146                                       (concat group ":" old-number-string))
1147                                      "\\>")
1148                              (point-at-eol) t)
1149                         (replace-match
1150                          (concat "\\1" group ":" new-number-string))))))
1151                 ;; 4/ Possibly remove the article from the backlog:
1152                 (when gnus-keep-backlog
1153                   ;; #### NOTE: instead of removing the article, we could
1154                   ;; #### modify the backlog to reflect the numbering change,
1155                   ;; #### but I don't think it's worth it.
1156                   (gnus-backlog-remove-article group-full-name old-number)
1157                   (gnus-backlog-remove-article group-full-name new-number))))
1158             (setq new-number (1+ new-number)))))
1159       (if (not compacted)
1160           ;; No compaction had to be done:
1161           t
1162         ;; Some articles have actually been renamed:
1163         ;; 1/ Rebuild active information:
1164         (let ((entry (assoc group nnml-group-alist))
1165               (active (cons 1 (1- new-number))))
1166           (setq nnml-group-alist (delq entry nnml-group-alist))
1167           (push (list group active) nnml-group-alist)
1168           ;; Update the active hashtable to let the *Group* buffer display
1169           ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1170           ;; gnus-newwrc-alist are out of date, since all we did is to modify
1171           ;; the info of the group internally.
1172           (gnus-set-active group-full-name active))
1173         ;; 1 bis/
1174         ;; #### NOTE: normally, we should save the overview (NOV) file
1175         ;; #### here, just like we save the marks file. However, there is no
1176         ;; #### such function as nnml-save-nov for a single group. Only for
1177         ;; #### all groups. Gnus inconsistency is getting worse every day...
1178         ;; 2/ Rebuild marks file:
1179         (unless nnml-marks-is-evil
1180           ;; #### NOTE: this constant use of global variables everywhere is
1181           ;; #### truly disgusting. Gnus really needs a *major* cleanup.
1182           (setq nnml-marks (gnus-info-marks info))
1183           (push (cons 'read (gnus-info-read info)) nnml-marks)
1184           (dolist (el gnus-article-unpropagated-mark-lists)
1185             (setq nnml-marks (gnus-remassoc el nnml-marks)))
1186           (nnml-save-marks group server))
1187         ;; 3/ Save everything if this was not part of a bigger operation:
1188         (if (not save)
1189             ;; Nothing to save (yet):
1190             t
1191           ;; Something to save:
1192           ;; a/ Save the NOV databases:
1193           ;; #### NOTE: this should be done directory per directory in 1bis
1194           ;; #### above. See comment there.
1195           (nnml-save-nov)
1196           ;; b/ Save the active file:
1197           (nnmail-save-active nnml-group-alist nnml-active-file)
1198           t)))))
1199
1200 (defun nnml-request-compact (&optional server)
1201   "Request compaction of all SERVER nnml groups."
1202   (interactive (list (or (nnoo-current-server 'nnml) "")))
1203   (nnmail-activate 'nnml)
1204   (unless (nnml-server-opened server)
1205     (nnml-open-server server))
1206   (setq nnml-directory (expand-file-name nnml-directory))
1207   (let* ((groups (gnus-groups-from-server
1208                   (gnus-server-to-method (format "nnml:%s" server))))
1209          (first (pop groups))
1210          group)
1211     (when first
1212       (while (setq group (pop groups))
1213         (nnml-request-compact-group (gnus-group-real-name group) server))
1214       (nnml-request-compact-group (gnus-group-real-name first) server t))))
1215
1216
1217 (provide 'nnml)
1218
1219 ;;; nnml.el ends here