Relicense "GPLv2 or later" files to "GPLv3 or later".
[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     (when (ignore-errors
544             (make-directory new-dir t)
545             t)
546       ;; We move the articles file by file instead of renaming
547       ;; the directory -- there may be subgroups in this group.
548       ;; One might be more clever, I guess.
549       (dolist (file (nnheader-article-to-file-alist old-dir))
550         (rename-file
551          (concat old-dir (cdr file))
552          (concat new-dir (cdr file))))
553       ;; Move .overview file.
554       (let ((overview (concat old-dir nnml-nov-file-name)))
555         (when (file-exists-p overview)
556           (rename-file overview (concat new-dir nnml-nov-file-name))))
557       ;; Move .marks file.
558       (let ((marks (concat old-dir nnml-marks-file-name)))
559         (when (file-exists-p marks)
560           (rename-file marks (concat new-dir nnml-marks-file-name))))
561       (when (<= (length (directory-files old-dir)) 2)
562         (ignore-errors (delete-directory old-dir)))
563       ;; That went ok, so we change the internal structures.
564       (let ((entry (assoc group nnml-group-alist)))
565         (when entry
566           (setcar entry new-name))
567         (setq nnml-current-directory nil
568               nnml-current-group nil)
569         ;; Save the new group alist.
570         (nnmail-save-active nnml-group-alist nnml-active-file)
571         t))))
572
573 (deffoo nnml-set-status (article name value &optional group server)
574   (nnml-possibly-change-directory group server)
575   (let ((file (nnml-article-to-file article)))
576     (cond
577      ((not (file-exists-p file))
578       (nnheader-report 'nnml "File %s does not exist" file))
579      (t
580       (with-temp-file file
581         (nnheader-insert-file-contents file)
582         (nnmail-replace-status name value))
583       t))))
584
585 \f
586 ;;; Internal functions.
587
588 (defun nnml-article-to-file (article)
589   (nnml-update-file-alist)
590   (let (file)
591     (if (setq file
592               (if nnml-use-compressed-files
593                   (cdr (assq article nnml-article-file-alist))
594                 (number-to-string article)))
595         (expand-file-name file nnml-current-directory)
596       (when (not nnheader-directory-files-is-safe)
597         ;; Just to make sure nothing went wrong when reading over NFS --
598         ;; check once more.
599         (when (file-exists-p
600                (setq file (expand-file-name (number-to-string article)
601                                             nnml-current-directory)))
602           (nnml-update-file-alist t)
603           file)))))
604
605 (defun nnml-deletable-article-p (group article)
606   "Say whether ARTICLE in GROUP can be deleted."
607   (let ((file-name-coding-system nnmail-pathname-coding-system)
608         path)
609     (when (setq path (nnml-article-to-file article))
610       (when (file-writable-p path)
611         (or (not nnmail-keep-last-article)
612             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
613                      article)))))))
614
615 ;; Find an article number in the current group given the Message-ID.
616 (defun nnml-find-group-number (id server)
617   (save-excursion
618     (set-buffer (get-buffer-create " *nnml id*"))
619     (let ((alist nnml-group-alist)
620           number)
621       ;; We want to look through all .overview files, but we want to
622       ;; start with the one in the current directory.  It seems most
623       ;; likely that the article we are looking for is in that group.
624       (if (setq number (nnml-find-id nnml-current-group id server))
625           (cons nnml-current-group number)
626       ;; It wasn't there, so we look through the other groups as well.
627         (while (and (not number)
628                     alist)
629           (or (string= (caar alist) nnml-current-group)
630               (setq number (nnml-find-id (caar alist) id server)))
631           (or number
632               (setq alist (cdr alist))))
633         (and number
634              (cons (caar alist) number))))))
635
636 (defun nnml-find-id (group id server)
637   (erase-buffer)
638   (let ((nov (nnml-group-pathname group nnml-nov-file-name server))
639         number found)
640     (when (file-exists-p nov)
641       (nnheader-insert-file-contents nov)
642       (while (and (not found)
643                   (search-forward id nil t)) ; We find the ID.
644         ;; And the id is in the fourth field.
645         (if (not (and (search-backward "\t" nil t 4)
646                       (not (search-backward "\t" (point-at-bol) t))))
647             (forward-line 1)
648           (beginning-of-line)
649           (setq found t)
650           ;; We return the article number.
651           (setq number
652                 (ignore-errors (read (current-buffer))))))
653       number)))
654
655 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
656   (if (or gnus-nov-is-evil nnml-nov-is-evil)
657       nil
658     (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
659       (when (file-exists-p nov)
660         (save-excursion
661           (set-buffer nntp-server-buffer)
662           (erase-buffer)
663           (nnheader-insert-file-contents nov)
664           (if (and fetch-old
665                    (not (numberp fetch-old)))
666               t                         ; Don't remove anything.
667             (nnheader-nov-delete-outside-range
668              (if fetch-old (max 1 (- (car articles) fetch-old))
669                (car articles))
670              (car (last articles)))
671             t))))))
672
673 (defun nnml-possibly-change-directory (group &optional server)
674   (when (and server
675              (not (nnml-server-opened server)))
676     (nnml-open-server server))
677   (if (not group)
678       t
679     (let ((pathname (nnml-group-pathname group nil server))
680           (file-name-coding-system nnmail-pathname-coding-system))
681       (when (not (equal pathname nnml-current-directory))
682         (setq nnml-current-directory pathname
683               nnml-current-group group
684               nnml-article-file-alist nil))
685       (file-exists-p nnml-current-directory))))
686
687 (defun nnml-possibly-create-directory (group &optional server)
688   (let ((dir (nnml-group-pathname group nil server))
689         (file-name-coding-system nnmail-pathname-coding-system))
690     (unless (file-exists-p dir)
691       (make-directory (directory-file-name dir) t)
692       (nnheader-message 5 "Creating mail directory %s" dir))))
693
694 (defun nnml-save-mail (group-art &optional server)
695   "Save a mail into the groups GROUP-ART in the nnml server SERVER.
696 GROUP-ART is a list that each element is a cons of a group name and an
697 article number.  This function is called narrowed to an article."
698   (let* ((chars (nnmail-insert-lines))
699          (extension (and nnml-use-compressed-files
700                          (> chars nnml-compressed-files-size-threshold)
701                          (if (stringp nnml-use-compressed-files)
702                              nnml-use-compressed-files
703                            ".gz")))
704          decoded dec file first headers)
705     (when nnmail-group-names-not-encoded-p
706       (dolist (ga (prog1 group-art (setq group-art nil)))
707         (setq group-art (nconc group-art
708                                (list (cons (nnml-encoded-group-name (car ga)
709                                                                     server)
710                                            (cdr ga))))
711               decoded (nconc decoded (list (car ga)))))
712       (setq dec decoded))
713     (nnmail-insert-xref group-art)
714     (run-hooks 'nnmail-prepare-save-mail-hook)
715     (run-hooks 'nnml-prepare-save-mail-hook)
716     (goto-char (point-min))
717     (while (looking-at "From ")
718       (replace-match "X-From-Line: ")
719       (forward-line 1))
720     ;; We save the article in all the groups it belongs in.
721     (dolist (ga group-art)
722       (if nnmail-group-names-not-encoded-p
723           (progn
724             (nnml-possibly-create-directory (car decoded) server)
725             (setq file (nnmail-group-pathname
726                         (pop decoded) nnml-directory
727                         (concat (number-to-string (cdr ga)) extension))))
728         (nnml-possibly-create-directory (car ga) server)
729         (setq file (nnml-group-pathname
730                     (car ga) (concat (number-to-string (cdr ga)) extension)
731                     server)))
732       (if first
733           ;; It was already saved, so we just make a hard link.
734           (let ((file-name-coding-system nnmail-pathname-coding-system))
735             (funcall nnmail-crosspost-link-function first file t))
736         ;; Save the article.
737         (nnmail-write-region (point-min) (point-max) file nil
738                              (if (nnheader-be-verbose 5) nil 'nomesg))
739         (setq first file)))
740     ;; Generate a nov line for this article.  We generate the nov
741     ;; line after saving, because nov generation destroys the
742     ;; header.
743     (setq headers (nnml-parse-head chars))
744     ;; Output the nov line to all nov databases that should have it.
745     (if nnmail-group-names-not-encoded-p
746         (dolist (ga group-art)
747           (nnml-add-nov (pop dec) (cdr ga) headers))
748       (dolist (ga group-art)
749         (nnml-add-nov (car ga) (cdr ga) headers))))
750   group-art)
751
752 (defun nnml-active-number (group &optional server)
753   "Compute the next article number in GROUP on SERVER."
754   (let ((active (cadr (assoc (if nnmail-group-names-not-encoded-p
755                                  (nnml-encoded-group-name group server)
756                                group)
757                              nnml-group-alist))))
758     ;; The group wasn't known to nnml, so we just create an active
759     ;; entry for it.
760     (unless active
761       ;; Perhaps the active file was corrupt?  See whether
762       ;; there are any articles in this group.
763       (nnml-possibly-create-directory group server)
764       (nnml-possibly-change-directory group server)
765       (unless nnml-article-file-alist
766         (setq nnml-article-file-alist
767               (sort
768                (nnml-current-group-article-to-file-alist)
769                'car-less-than-car)))
770       (setq active
771             (if nnml-article-file-alist
772                 (cons (caar nnml-article-file-alist)
773                       (caar (last nnml-article-file-alist)))
774               (cons 1 0)))
775       (push (list group active) nnml-group-alist))
776     (setcdr active (1+ (cdr active)))
777     (while (file-exists-p
778             (nnml-group-pathname group (int-to-string (cdr active)) server))
779       (setcdr active (1+ (cdr active))))
780     (cdr active)))
781
782 (defun nnml-add-nov (group article headers)
783   "Add a nov line for the GROUP base."
784   (save-excursion
785     (set-buffer (nnml-open-nov group))
786     (goto-char (point-max))
787     (mail-header-set-number headers article)
788     (nnheader-insert-nov headers)))
789
790 (defsubst nnml-header-value ()
791   (buffer-substring (match-end 0) (point-at-eol)))
792
793 (defun nnml-parse-head (chars &optional number)
794   "Parse the head of the current buffer."
795   (save-excursion
796     (save-restriction
797       (unless (zerop (buffer-size))
798         (narrow-to-region
799          (goto-char (point-min))
800          (if (re-search-forward "\n\r?\n" nil t)
801              (1- (point))
802            (point-max))))
803       (let ((headers (nnheader-parse-naked-head)))
804         (mail-header-set-chars headers chars)
805         (mail-header-set-number headers number)
806         headers))))
807
808 (defun nnml-get-nov-buffer (group)
809   (let* ((decoded (nnml-decoded-group-name group))
810          (buffer (get-buffer-create (format " *nnml overview %s*" decoded)))
811          (file-name-coding-system nnmail-pathname-coding-system))
812     (save-excursion
813       (set-buffer buffer)
814       (set (make-local-variable 'nnml-nov-buffer-file-name)
815            (nnmail-group-pathname decoded nnml-directory nnml-nov-file-name))
816       (erase-buffer)
817       (when (file-exists-p nnml-nov-buffer-file-name)
818         (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
819     buffer))
820
821 (defun nnml-open-nov (group)
822   (or (cdr (assoc group nnml-nov-buffer-alist))
823       (let ((buffer (nnml-get-nov-buffer group)))
824         (push (cons group buffer) nnml-nov-buffer-alist)
825         buffer)))
826
827 (defun nnml-save-nov ()
828   (save-excursion
829     (while nnml-nov-buffer-alist
830       (when (buffer-name (cdar nnml-nov-buffer-alist))
831         (set-buffer (cdar nnml-nov-buffer-alist))
832         (when (buffer-modified-p)
833           (nnmail-write-region (point-min) (point-max)
834                                nnml-nov-buffer-file-name nil 'nomesg))
835         (set-buffer-modified-p nil)
836         (kill-buffer (current-buffer)))
837       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
838
839 ;;;###autoload
840 (defun nnml-generate-nov-databases (&optional server)
841   "Generate NOV databases in all nnml directories."
842   (interactive (list (or (nnoo-current-server 'nnml) "")))
843   ;; Read the active file to make sure we don't re-use articles
844   ;; numbers in empty groups.
845   (nnmail-activate 'nnml)
846   (unless (nnml-server-opened server)
847     (nnml-open-server server))
848   (setq nnml-directory (expand-file-name nnml-directory))
849   ;; Recurse down the directories.
850   (nnml-generate-nov-databases-directory nnml-directory nil t)
851   ;; Save the active file.
852   (nnmail-save-active nnml-group-alist nnml-active-file))
853
854 (defun nnml-generate-nov-databases-directory (dir &optional seen no-active)
855   "Regenerate the NOV database in DIR.
856
857 Unless no-active is non-nil, update the active file too."
858   (interactive (list (let ((file-name-coding-system
859                             nnmail-pathname-coding-system))
860                        (read-directory-name "Regenerate NOV in: "
861                                             nnml-directory nil t))))
862   (setq dir (file-name-as-directory dir))
863   (let ((file-name-coding-system nnmail-pathname-coding-system))
864     ;; Only scan this sub-tree if we haven't been here yet.
865     (unless (member (file-truename dir) seen)
866       (push (file-truename dir) seen)
867       ;; We descend recursively
868       (dolist (dir (directory-files dir t nil t))
869         (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
870                    (file-directory-p dir))
871           (nnml-generate-nov-databases-directory dir seen)))
872       ;; Do this directory.
873       (let ((files (sort (nnheader-article-to-file-alist dir)
874                          'car-less-than-car)))
875         (if (not files)
876             (let* ((group (nnheader-file-to-group
877                            (directory-file-name dir) nnml-directory))
878                    (info (cadr (assoc group nnml-group-alist))))
879               (when info
880                 (setcar info (1+ (cdr info)))))
881           (funcall nnml-generate-active-function dir)
882           ;; Generate the nov file.
883           (nnml-generate-nov-file dir files)
884           (unless no-active
885             (nnmail-save-active nnml-group-alist nnml-active-file)))))))
886
887 (eval-when-compile (defvar files))
888 (defun nnml-generate-active-info (dir)
889   ;; Update the active info for this group.
890   (let ((group (directory-file-name dir))
891         entry last)
892     (setq group (nnheader-file-to-group (nnml-encoded-group-name group)
893                                         nnml-directory)
894           entry (assoc group nnml-group-alist)
895           last (or (caadr entry) 0)
896           nnml-group-alist (delq entry nnml-group-alist))
897     (push (list group
898                 (cons (or (caar files) (1+ last))
899                       (max last
900                            (or (caar (last files))
901                                0))))
902           nnml-group-alist)))
903
904 (defun nnml-generate-nov-file (dir files)
905   (let* ((dir (file-name-as-directory dir))
906          (nov (concat dir nnml-nov-file-name))
907          (nov-buffer (get-buffer-create " *nov*"))
908          chars file headers)
909     (save-excursion
910       ;; Init the nov buffer.
911       (set-buffer nov-buffer)
912       (buffer-disable-undo)
913       (erase-buffer)
914       (set-buffer nntp-server-buffer)
915       ;; Delete the old NOV file.
916       (when (file-exists-p nov)
917         (funcall nnmail-delete-file-function nov))
918       (while files
919         (unless (file-directory-p (setq file (concat dir (cdar files))))
920           (erase-buffer)
921           (nnheader-insert-file-contents file)
922           (narrow-to-region
923            (goto-char (point-min))
924            (progn
925              (re-search-forward "\n\r?\n" nil t)
926              (setq chars (- (point-max) (point)))
927              (max (point-min) (1- (point)))))
928           (unless (zerop (buffer-size))
929             (goto-char (point-min))
930             (setq headers (nnml-parse-head chars (caar files)))
931             (save-excursion
932               (set-buffer nov-buffer)
933               (goto-char (point-max))
934               (nnheader-insert-nov headers)))
935           (widen))
936         (setq files (cdr files)))
937       (save-excursion
938         (set-buffer nov-buffer)
939         (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
940         (kill-buffer (current-buffer))))))
941
942 (defun nnml-nov-delete-article (group article)
943   (save-excursion
944     (set-buffer (nnml-open-nov group))
945     (when (nnheader-find-nov-line article)
946       (delete-region (point) (progn (forward-line 1) (point)))
947       (when (bobp)
948         (let ((active (cadr (assoc group nnml-group-alist)))
949               num)
950           (when active
951             (if (eobp)
952                 (setf (car active) (1+ (cdr active)))
953               (when (and (setq num (ignore-errors (read (current-buffer))))
954                          (numberp num))
955                 (setf (car active) num)))))))
956     t))
957
958 (defun nnml-update-file-alist (&optional force)
959   (when nnml-use-compressed-files
960     (when (or (not nnml-article-file-alist)
961               force)
962       (setq nnml-article-file-alist
963             (nnml-current-group-article-to-file-alist)))))
964
965 (defun nnml-directory-articles (dir)
966   "Return a list of all article files in a directory.
967 Use the nov database for that directory if available."
968   (if (or gnus-nov-is-evil nnml-nov-is-evil
969           (not (file-exists-p
970                 (expand-file-name nnml-nov-file-name dir))))
971       (nnheader-directory-articles dir)
972     ;; build list from .overview if available
973     ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
974     ;; defvoo'd, and we might get called when it hasn't been swapped in.
975     (save-excursion
976       (let ((list nil)
977             art
978             (buffer (nnml-get-nov-buffer nnml-current-group)))
979         (set-buffer buffer)
980         (goto-char (point-min))
981         (while (not (eobp))
982           (setq art (read (current-buffer)))
983           (push art list)
984           (forward-line 1))
985         list))))
986
987 (defun nnml-current-group-article-to-file-alist ()
988   "Return an alist of article/file pairs in the current group.
989 Use the nov database for the current group if available."
990   (if (or nnml-use-compressed-files
991           gnus-nov-is-evil
992           nnml-nov-is-evil
993           (not (file-exists-p
994                 (expand-file-name nnml-nov-file-name
995                                   nnml-current-directory))))
996       (nnheader-article-to-file-alist nnml-current-directory)
997     ;; build list from .overview if available
998     (save-excursion
999       (let ((alist nil)
1000             (buffer (nnml-get-nov-buffer nnml-current-group))
1001             art)
1002         (set-buffer buffer)
1003         (goto-char (point-min))
1004         (while (not (eobp))
1005           (setq art (read (current-buffer)))
1006           ;; assume file name is unadorned (ie. not compressed etc)
1007           (push (cons art (int-to-string art)) alist)
1008           (forward-line 1))
1009         alist))))
1010
1011 (deffoo nnml-request-set-mark (group actions &optional server)
1012   (nnml-possibly-change-directory group server)
1013   (unless nnml-marks-is-evil
1014     (nnml-open-marks group server)
1015     (dolist (action actions)
1016       (let ((range (nth 0 action))
1017             (what  (nth 1 action))
1018             (marks (nth 2 action)))
1019         (assert (or (eq what 'add) (eq what 'del)) nil
1020                 "Unknown request-set-mark action: %s" what)
1021         (dolist (mark marks)
1022           (setq nnml-marks (gnus-update-alist-soft
1023                             mark
1024                             (funcall (if (eq what 'add) 'gnus-range-add
1025                                        'gnus-remove-from-range)
1026                                      (cdr (assoc mark nnml-marks)) range)
1027                             nnml-marks)))))
1028     (nnml-save-marks group server))
1029   nil)
1030
1031 (deffoo nnml-request-update-info (group info &optional server)
1032   (nnml-possibly-change-directory group server)
1033   (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group server))
1034     (nnheader-message 8 "Updating marks for %s..." group)
1035     (nnml-open-marks group server)
1036     ;; Update info using `nnml-marks'.
1037     (mapc (lambda (pred)
1038             (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1039               (gnus-info-set-marks
1040                info
1041                (gnus-update-alist-soft
1042                 (cdr pred)
1043                 (cdr (assq (cdr pred) nnml-marks))
1044                 (gnus-info-marks info))
1045                t)))
1046           gnus-article-mark-lists)
1047     (let ((seen (cdr (assq 'read nnml-marks))))
1048       (gnus-info-set-read info
1049                           (if (and (integerp (car seen))
1050                                    (null (cdr seen)))
1051                               (list (cons (car seen) (car seen)))
1052                             seen)))
1053     (nnheader-message 8 "Updating marks for %s...done" group))
1054   info)
1055
1056 (defun nnml-marks-changed-p (group server)
1057   (let ((file (nnml-group-pathname group nnml-marks-file-name server)))
1058     (if (null (gnus-gethash file nnml-marks-modtime))
1059         t ;; never looked at marks file, assume it has changed
1060       (not (equal (gnus-gethash file nnml-marks-modtime)
1061                   (nth 5 (file-attributes file)))))))
1062
1063 (defun nnml-save-marks (group server)
1064   (let ((file-name-coding-system nnmail-pathname-coding-system)
1065         (file (nnml-group-pathname group nnml-marks-file-name server)))
1066     (condition-case err
1067         (progn
1068           (nnml-possibly-create-directory group server)
1069           (with-temp-file file
1070             (erase-buffer)
1071             (gnus-prin1 nnml-marks)
1072             (insert "\n"))
1073           (gnus-sethash file
1074                         (nth 5 (file-attributes file))
1075                         nnml-marks-modtime))
1076       (error (or (gnus-yes-or-no-p
1077                   (format "Could not write to %s (%s).  Continue? " file err))
1078                  (error "Cannot write to %s (%s)" file err))))))
1079
1080 (defun nnml-open-marks (group server)
1081   (let* ((decoded (nnml-decoded-group-name group server))
1082          (file (nnmail-group-pathname decoded nnml-directory
1083                                       nnml-marks-file-name))
1084          (file-name-coding-system nnmail-pathname-coding-system))
1085     (if (file-exists-p file)
1086         (condition-case err
1087             (with-temp-buffer
1088               (gnus-sethash file (nth 5 (file-attributes file))
1089                             nnml-marks-modtime)
1090               (nnheader-insert-file-contents file)
1091               (setq nnml-marks (read (current-buffer)))
1092               (dolist (el gnus-article-unpropagated-mark-lists)
1093                 (setq nnml-marks (gnus-remassoc el nnml-marks))))
1094           (error (or (gnus-yes-or-no-p
1095                       (format "Error reading nnml marks file %s (%s).  Continuing will use marks from .newsrc.eld.  Continue? " file err))
1096                      (error "Cannot read nnml marks file %s (%s)" file err))))
1097       ;; User didn't have a .marks file.  Probably first time
1098       ;; user of the .marks stuff.  Bootstrap it from .newsrc.eld.
1099       (let ((info (gnus-get-info
1100                    (gnus-group-prefixed-name
1101                     group
1102                     (gnus-server-to-method
1103                      (format "nnml:%s" (or server "")))))))
1104         (setq decoded (if (member server '(nil ""))
1105                           (concat "nnml:" decoded)
1106                         (format "nnml+%s:%s" server decoded)))
1107         (nnheader-message 7 "Bootstrapping marks for %s..." decoded)
1108         (setq nnml-marks (gnus-info-marks info))
1109         (push (cons 'read (gnus-info-read info)) nnml-marks)
1110         (dolist (el gnus-article-unpropagated-mark-lists)
1111           (setq nnml-marks (gnus-remassoc el nnml-marks)))
1112         (nnml-save-marks group server)
1113         (nnheader-message 7 "Bootstrapping marks for %s...done" decoded)))))
1114
1115
1116 ;;;
1117 ;;; Group and server compaction. -- dvl
1118 ;;;
1119
1120 ;; #### FIXME: this function handles self Xref: entry correctly, but I don't
1121 ;; #### know how to handle external cross-references. I actually don't know if
1122 ;; #### this is handled correctly elsewhere. For instance, what happens if you
1123 ;; #### move all articles to a new group (that's what people do for manual
1124 ;; #### compaction) ?
1125
1126 ;; #### NOTE: the function below handles the article backlog. This is
1127 ;; #### conceptually the wrong place to do it because the backend is at a
1128 ;; #### lower level. However, this is the only place where we have the needed
1129 ;; #### information to do the job. Ideally, this function should not handle
1130 ;; #### the backlog by itself, but return a list of moved groups / articles to
1131 ;; #### the caller. This will become important to avoid code duplication when
1132 ;; #### other backends get a compaction feature. Also, note that invalidating
1133 ;; #### the "original article buffer" is already done at an upper level.
1134
1135 ;; Shouldn't `nnml-request-compact-group' be interactive? --rsteib
1136
1137 (defun nnml-request-compact-group (group &optional server save)
1138   (nnml-possibly-change-directory group server)
1139   (unless nnml-article-file-alist
1140     (setq nnml-article-file-alist
1141           (sort (nnml-current-group-article-to-file-alist)
1142                 'car-less-than-car)))
1143   (if (not nnml-article-file-alist)
1144       ;; The group is empty: do nothing but return t
1145       t
1146     ;; The group is not empty:
1147     (let* ((group-full-name
1148             (gnus-group-prefixed-name
1149              group
1150              (gnus-server-to-method (format "nnml:%s" server))))
1151            (info (gnus-get-info group-full-name))
1152            (new-number 1)
1153            compacted)
1154       (let ((articles nnml-article-file-alist)
1155             article)
1156         (while (setq article (pop articles))
1157           (let ((old-number (car article)))
1158             (when (> old-number new-number)
1159               ;; There is a gap here:
1160               (let ((old-number-string (int-to-string old-number))
1161                     (new-number-string (int-to-string new-number)))
1162                 (setq compacted t)
1163                 ;; #### NOTE: `nnml-article-to-file' calls
1164                 ;; #### `nnml-update-file-alist'  (which in turn calls
1165                 ;; #### `nnml-current-group-article-to-file-alist', which
1166                 ;; #### might use the NOV database). This might turn out to be
1167                 ;; #### inefficient. In that case, we will do the work
1168                 ;; #### manually.
1169                 ;; 1/ Move the article to a new file:
1170                 (let* ((oldfile (nnml-article-to-file old-number))
1171                        (newfile
1172                         (gnus-replace-in-string
1173                          oldfile
1174                          ;; nnml-use-compressed-files might be any string, but
1175                          ;; probably it's sufficient to take into account only
1176                          ;; "\\.[a-z0-9]+".  Note that we can't only use the
1177                          ;; value of nnml-use-compressed-files because old
1178                          ;; articles might have been saved with a different
1179                          ;; value.
1180                          (concat
1181                           "\\(" old-number-string "\\)\\(\\(\\.[a-z0-9]+\\)?\\)$")
1182                          (concat new-number-string "\\2"))))
1183                   (with-current-buffer nntp-server-buffer
1184                     (nnmail-find-file oldfile)
1185                     ;; Update the Xref header in the article itself:
1186                     (when (and (re-search-forward "^Xref: [^ ]+ " nil t)
1187                                (re-search-forward
1188                                 (concat "\\<"
1189                                         (regexp-quote
1190                                          (concat group ":" old-number-string))
1191                                         "\\>")
1192                                 (point-at-eol) t))
1193                       (replace-match
1194                        (concat group ":" new-number-string)))
1195                     ;; Save to the new file:
1196                     (nnmail-write-region (point-min) (point-max) newfile))
1197                   (funcall nnmail-delete-file-function oldfile))
1198                 ;; 2/ Update all marks for this article:
1199                 ;; #### NOTE: it is possible that the new article number
1200                 ;; #### already belongs to a range, whereas the corresponding
1201                 ;; #### article doesn't exist (for example, if you delete an
1202                 ;; #### article). For that reason, it is important to update
1203                 ;; #### the ranges (meaning remove inexistant articles) before
1204                 ;; #### doing anything on them.
1205                 ;; 2 a/ read articles:
1206                 (let ((read (gnus-info-read info)))
1207                   (setq read (gnus-remove-from-range read (list new-number)))
1208                   (when (gnus-member-of-range old-number read)
1209                     (setq read (gnus-remove-from-range read (list old-number)))
1210                     (setq read (gnus-add-to-range read (list new-number))))
1211                   (gnus-info-set-read info read))
1212                 ;; 2 b/ marked articles:
1213                 (let ((oldmarks (gnus-info-marks info))
1214                       mark newmarks)
1215                   (while (setq mark (pop oldmarks))
1216                     (setcdr mark (gnus-remove-from-range (cdr mark)
1217                                                          (list new-number)))
1218                     (when (gnus-member-of-range old-number (cdr mark))
1219                       (setcdr mark (gnus-remove-from-range (cdr mark)
1220                                                            (list old-number)))
1221                       (setcdr mark (gnus-add-to-range (cdr mark)
1222                                                       (list new-number))))
1223                     (push mark newmarks))
1224                   (gnus-info-set-marks info newmarks))
1225                 ;; 3/ Update the NOV entry for this article:
1226                 (unless nnml-nov-is-evil
1227                   (save-excursion
1228                     (set-buffer (nnml-open-nov group))
1229                     (when (nnheader-find-nov-line old-number)
1230                       ;; Replace the article number:
1231                       (looking-at old-number-string)
1232                       (replace-match new-number-string nil t)
1233                       ;; Update the Xref header:
1234                       (when (re-search-forward
1235                              (concat "\\(Xref:[^\t\n]* \\)\\<"
1236                                      (regexp-quote
1237                                       (concat group ":" old-number-string))
1238                                      "\\>")
1239                              (point-at-eol) t)
1240                         (replace-match
1241                          (concat "\\1" group ":" new-number-string))))))
1242                 ;; 4/ Possibly remove the article from the backlog:
1243                 (when gnus-keep-backlog
1244                   ;; #### NOTE: instead of removing the article, we could
1245                   ;; #### modify the backlog to reflect the numbering change,
1246                   ;; #### but I don't think it's worth it.
1247                   (gnus-backlog-remove-article group-full-name old-number)
1248                   (gnus-backlog-remove-article group-full-name new-number))))
1249             (setq new-number (1+ new-number)))))
1250       (if (not compacted)
1251           ;; No compaction had to be done:
1252           t
1253         ;; Some articles have actually been renamed:
1254         ;; 1/ Rebuild active information:
1255         (let ((entry (assoc group nnml-group-alist))
1256               (active (cons 1 (1- new-number))))
1257           (setq nnml-group-alist (delq entry nnml-group-alist))
1258           (push (list group active) nnml-group-alist)
1259           ;; Update the active hashtable to let the *Group* buffer display
1260           ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1261           ;; gnus-newwrc-alist are out of date, since all we did is to modify
1262           ;; the info of the group internally.
1263           (gnus-set-active group-full-name active))
1264         ;; 1 bis/
1265         ;; #### NOTE: normally, we should save the overview (NOV) file
1266         ;; #### here, just like we save the marks file. However, there is no
1267         ;; #### such function as nnml-save-nov for a single group. Only for
1268         ;; #### all groups. Gnus inconsistency is getting worse every day...
1269         ;; 2/ Rebuild marks file:
1270         (unless nnml-marks-is-evil
1271           ;; #### NOTE: this constant use of global variables everywhere is
1272           ;; #### truly disgusting. Gnus really needs a *major* cleanup.
1273           (setq nnml-marks (gnus-info-marks info))
1274           (push (cons 'read (gnus-info-read info)) nnml-marks)
1275           (dolist (el gnus-article-unpropagated-mark-lists)
1276             (setq nnml-marks (gnus-remassoc el nnml-marks)))
1277           (nnml-save-marks group server))
1278         ;; 3/ Save everything if this was not part of a bigger operation:
1279         (if (not save)
1280             ;; Nothing to save (yet):
1281             t
1282           ;; Something to save:
1283           ;; a/ Save the NOV databases:
1284           ;; #### NOTE: this should be done directory per directory in 1bis
1285           ;; #### above. See comment there.
1286           (nnml-save-nov)
1287           ;; b/ Save the active file:
1288           (nnmail-save-active nnml-group-alist nnml-active-file)
1289           t)))))
1290
1291 (defun nnml-request-compact (&optional server)
1292   "Request compaction of all SERVER nnml groups."
1293   (interactive (list (or (nnoo-current-server 'nnml) "")))
1294   (nnmail-activate 'nnml)
1295   (unless (nnml-server-opened server)
1296     (nnml-open-server server))
1297   (setq nnml-directory (expand-file-name nnml-directory))
1298   (let* ((groups (gnus-groups-from-server
1299                   (gnus-server-to-method (format "nnml:%s" server))))
1300          (first (pop groups))
1301          group)
1302     (when first
1303       (while (setq group (pop groups))
1304         (nnml-request-compact-group (gnus-group-real-name group) server))
1305       (nnml-request-compact-group (gnus-group-real-name first) server t))))
1306
1307
1308 (provide 'nnml)
1309
1310 ;;; arch-tag: 52c97dc3-9735-45de-b439-9e4d23b52004
1311 ;;; nnml.el ends here