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