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