1c9513d21916c279c0ea23e342dd1b32f3edf4c7
[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, 2008, 2009, 2010 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 of the License, or
17 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Commentary:
28
29 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
30 ;; For an overview of what the interface functions do, please see the
31 ;; Gnus sources.
32
33 ;;; Code:
34
35 (require 'gnus)
36 (require 'nnheader)
37 (require 'nnmail)
38 (require 'nnoo)
39 (eval-when-compile (require 'cl))
40
41 ;; FIXME first is unused in this file.
42 (autoload 'gnus-article-unpropagatable-p "gnus-sum")
43 (autoload 'gnus-backlog-remove-article "gnus-bcklg")
44
45 (nnoo-declare nnml)
46
47 (defvoo nnml-directory message-directory
48   "Spool directory for the nnml mail backend.")
49
50 (defvoo nnml-active-file
51     (expand-file-name "active" nnml-directory)
52   "Mail active file.")
53
54 (defvoo nnml-newsgroups-file
55     (expand-file-name "newsgroups" nnml-directory)
56   "Mail newsgroups description file.")
57
58 (defvoo nnml-get-new-mail t
59   "If non-nil, nnml will check the incoming mail file and split the mail.")
60
61 (defvoo nnml-nov-is-evil nil
62   "If non-nil, Gnus will never generate and use nov databases for mail spools.
63 Using nov databases will speed up header fetching considerably.
64 This variable shouldn't be flipped much.  If you have, for some reason,
65 set this to t, and want to set it to nil again, you should always run
66 the `nnml-generate-nov-databases' command.  The function will go
67 through all nnml directories and generate nov databases for them
68 all.  This may very well take some time.")
69
70 (defvoo nnml-marks-is-evil nil
71   "If non-nil, Gnus will never generate and use marks file for mail spools.
72 Using marks files makes it possible to backup and restore mail groups
73 separately from `.newsrc.eld'.  If you have, for some reason, set this
74 to t, and want to set it to nil again, you should always remove the
75 corresponding marks file (usually named `.marks' in the nnml group
76 directory, but see `nnml-marks-file-name') for the group.  Then the
77 marks file will be regenerated properly by Gnus.")
78
79 (defvoo nnml-prepare-save-mail-hook nil
80   "Hook run narrowed to an article before saving.")
81
82 (defvoo nnml-inhibit-expiry nil
83   "If non-nil, inhibit expiry.")
84
85 (defvoo nnml-use-compressed-files nil
86   "If non-nil, allow using compressed message files.
87
88 If it is a string, use it as the file extension which specifies
89 the compression program.  You can set it to \".bz2\" if your Emacs
90 supports auto-compression using the bzip2 program.  A value of t
91 is equivalent to \".gz\".")
92
93 (defvoo nnml-compressed-files-size-threshold 1000
94   "Default size threshold for compressed message files.
95 Message files with bodies larger than that many characters will
96 be automatically compressed if `nnml-use-compressed-files' is
97 non-nil.")
98
99 \f
100
101 (defconst nnml-version "nnml 1.0"
102   "nnml version.")
103
104 (defvoo nnml-nov-file-name ".overview")
105 (defvoo nnml-marks-file-name ".marks")
106
107 (defvoo nnml-current-directory nil)
108 (defvoo nnml-current-group nil)
109 (defvoo nnml-status-string "")
110 (defvoo nnml-nov-buffer-alist nil)
111 (defvoo nnml-group-alist nil)
112 (defvoo nnml-active-timestamp nil)
113 (defvoo nnml-article-file-alist nil)
114
115 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
116
117 (defvar nnml-nov-buffer-file-name nil)
118
119 (defvoo nnml-file-coding-system nnmail-file-coding-system)
120
121 (defvoo nnml-marks nil)
122
123 (defvar nnml-marks-modtime (gnus-make-hashtable))
124
125 \f
126 ;;; Interface functions.
127
128 (nnoo-define-basics nnml)
129
130 (eval-when-compile
131   (defsubst nnml-group-name-charset (group server-or-method)
132     (gnus-group-name-charset
133      (if (stringp server-or-method)
134          (gnus-server-to-method
135           (if (string-match "\\+" server-or-method)
136               (concat (substring server-or-method 0 (match-beginning 0))
137                       ":" (substring server-or-method (match-end 0)))
138             (concat "nnml:" server-or-method)))
139        (or server-or-method gnus-command-method '(nnml "")))
140      group)))
141
142 (defun nnml-decoded-group-name (group &optional server-or-method)
143   "Return a decoded group name of GROUP on SERVER-OR-METHOD."
144   (if nnmail-group-names-not-encoded-p
145       group
146     (mm-decode-coding-string
147      group
148      (nnml-group-name-charset group server-or-method))))
149
150 (defun nnml-encoded-group-name (group &optional server-or-method)
151   "Return an encoded group name of GROUP on SERVER-OR-METHOD."
152   (mm-encode-coding-string
153    group
154    (nnml-group-name-charset group server-or-method)))
155
156 (defun nnml-group-pathname (group &optional file server)
157   "Return an absolute file name of FILE for GROUP on SERVER."
158   (nnmail-group-pathname (inline (nnml-decoded-group-name group server))
159                          nnml-directory file))
160
161 (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
162   (when (nnml-possibly-change-directory group server)
163     (save-excursion
164       (set-buffer nntp-server-buffer)
165       (erase-buffer)
166       (let* ((file nil)
167              (number (length sequence))
168              (count 0)
169              (file-name-coding-system nnmail-pathname-coding-system)
170              beg article)
171         (if (stringp (car sequence))
172             'headers
173           (if (nnml-retrieve-headers-with-nov sequence fetch-old)
174               'nov
175             (while sequence
176               (setq article (car sequence))
177               (setq file (nnml-article-to-file article))
178               (when (and file
179                          (file-exists-p file)
180                          (not (file-directory-p file)))
181                 (insert (format "221 %d Article retrieved.\n" article))
182                 (setq beg (point))
183                 (nnheader-insert-head file)
184                 (goto-char beg)
185                 (if (re-search-forward "\n\r?\n" nil t)
186                     (forward-char -1)
187                   (goto-char (point-max))
188                   (insert "\n\n"))
189                 (insert ".\n")
190                 (delete-region (point) (point-max)))
191               (setq sequence (cdr sequence))
192               (setq count (1+ count))
193               (and (numberp nnmail-large-newsgroup)
194                    (> number nnmail-large-newsgroup)
195                    (zerop (% count 20))
196                    (nnheader-message 6 "nnml: Receiving headers... %d%%"
197                                      (/ (* count 100) number))))
198
199             (and (numberp nnmail-large-newsgroup)
200                  (> number nnmail-large-newsgroup)
201                  (nnheader-message 6 "nnml: Receiving headers...done"))
202
203             (nnheader-fold-continuation-lines)
204             'headers))))))
205
206 (deffoo nnml-open-server (server &optional defs)
207   (nnoo-change-server 'nnml server defs)
208   (when (not (file-exists-p nnml-directory))
209     (ignore-errors (make-directory nnml-directory t)))
210   (cond
211    ((not (file-exists-p nnml-directory))
212     (nnml-close-server)
213     (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
214    ((not (file-directory-p (file-truename nnml-directory)))
215     (nnml-close-server)
216     (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
217    (t
218     (nnheader-report 'nnml "Opened server %s using directory %s"
219                      server nnml-directory)
220     t)))
221
222 (deffoo nnml-request-regenerate (server)
223   (nnml-possibly-change-directory nil server)
224   (nnml-generate-nov-databases server)
225   t)
226
227 (deffoo nnml-request-article (id &optional group server buffer)
228   (nnml-possibly-change-directory group server)
229   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
230          (file-name-coding-system nnmail-pathname-coding-system)
231          path gpath group-num)
232     (if (stringp id)
233         (when (and (setq group-num (nnml-find-group-number id server))
234                    (cdr
235                     (assq (cdr group-num)
236                           (nnheader-article-to-file-alist
237                            (setq gpath (nnml-group-pathname (car group-num)
238                                                             nil server))))))
239           (setq path (concat gpath (int-to-string (cdr group-num)))))
240       (setq path (nnml-article-to-file id)))
241     (cond
242      ((not path)
243       (nnheader-report 'nnml "No such article: %s" id))
244      ((not (file-exists-p path))
245       (nnheader-report 'nnml "No such file: %s" path))
246      ((file-directory-p path)
247       (nnheader-report 'nnml "File is a directory: %s" path))
248      ((not (save-excursion (let ((nnmail-file-coding-system
249                                   nnml-file-coding-system))
250                              (nnmail-find-file path))))
251       (nnheader-report 'nnml "Couldn't read file: %s" path))
252      (t
253       (nnheader-report 'nnml "Article %s retrieved" id)
254       ;; We return the article number.
255       (cons (if group-num (car group-num) group)
256             (string-to-number (file-name-nondirectory path)))))))
257
258 (deffoo nnml-request-group (group &optional server dont-check)
259   (let ((file-name-coding-system nnmail-pathname-coding-system)
260         (decoded (nnml-decoded-group-name group server)))
261     (cond
262      ((not (nnml-possibly-change-directory group server))
263       (nnheader-report 'nnml "Invalid group (no such directory)"))
264      ((not (file-exists-p nnml-current-directory))
265       (nnheader-report 'nnml "Directory %s does not exist"
266                        nnml-current-directory))
267      ((not (file-directory-p nnml-current-directory))
268       (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
269      (dont-check
270       (nnheader-report 'nnml "Group %s selected" decoded)
271       t)
272      (t
273       (nnheader-re-read-dir nnml-current-directory)
274       (nnmail-activate 'nnml)
275       (let ((active (nth 1 (assoc group nnml-group-alist))))
276         (if (not active)
277             (nnheader-report 'nnml "No such group: %s" decoded)
278           (nnheader-report 'nnml "Selected group %s" decoded)
279           (nnheader-insert "211 %d %d %d %s\n"
280                            (max (1+ (- (cdr active) (car active))) 0)
281                            (car active) (cdr active) group)))))))
282
283 (deffoo nnml-request-scan (&optional group server)
284   (setq nnml-article-file-alist nil)
285   (nnml-possibly-change-directory group server)
286   (nnmail-get-new-mail 'nnml 'nnml-save-incremental-nov nnml-directory group))
287
288 (deffoo nnml-close-group (group &optional server)
289   (setq nnml-article-file-alist nil)
290   t)
291
292 (deffoo nnml-request-create-group (group &optional server args)
293   (nnml-possibly-change-directory nil server)
294   (nnmail-activate 'nnml)
295   (cond
296    ((let ((file (directory-file-name (nnml-group-pathname group nil server)))
297           (file-name-coding-system nnmail-pathname-coding-system))
298       (and (file-exists-p file)
299            (not (file-directory-p file))))
300     (nnheader-report 'nnml "%s is a file"
301                      (directory-file-name (nnml-group-pathname group
302                                                                nil server))))
303    ((assoc group nnml-group-alist)
304     t)
305    (t
306     (let (active)
307       (push (list group (setq active (cons 1 0)))
308             nnml-group-alist)
309       (nnml-possibly-create-directory group server)
310       (nnml-possibly-change-directory group server)
311       (let* ((file-name-coding-system nnmail-pathname-coding-system)
312              (articles (nnml-directory-articles nnml-current-directory)))
313         (when articles
314           (setcar active (apply 'min articles))
315           (setcdr active (apply 'max articles))))
316       (nnmail-save-active nnml-group-alist nnml-active-file)
317       t))))
318
319 (deffoo nnml-request-list (&optional server)
320   (save-excursion
321     (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
322           (file-name-coding-system nnmail-pathname-coding-system))
323       (nnmail-find-file nnml-active-file))
324     (setq nnml-group-alist (nnmail-get-active))
325     t))
326
327 (deffoo nnml-request-newgroups (date &optional server)
328   (nnml-request-list server))
329
330 (deffoo nnml-request-list-newsgroups (&optional server)
331   (save-excursion
332     (nnmail-find-file nnml-newsgroups-file)))
333
334 (deffoo nnml-request-expire-articles (articles group &optional server force)
335   (nnml-possibly-change-directory group server)
336   (let* ((file-name-coding-system nnmail-pathname-coding-system)
337          (active-articles
338           (nnml-directory-articles nnml-current-directory))
339          (is-old t)
340          (decoded (nnml-decoded-group-name group server))
341          article rest mod-time number target)
342     (nnmail-activate 'nnml)
343
344     (setq active-articles (sort active-articles '<))
345     ;; Articles not listed in active-articles are already gone,
346     ;; so don't try to expire them.
347     (setq articles (gnus-sorted-intersection articles active-articles))
348
349     (while (and articles is-old)
350       (if (and (setq article (nnml-article-to-file
351                               (setq number (pop articles))))
352                (setq mod-time (nth 5 (file-attributes article)))
353                (nnml-deletable-article-p group number)
354                (setq is-old (nnmail-expired-article-p group mod-time force
355                                                       nnml-inhibit-expiry)))
356           (progn
357             ;; Allow a special target group.
358             (setq target nnmail-expiry-target)
359             (unless (eq target 'delete)
360               (with-temp-buffer
361                 (nnml-request-article number group server (current-buffer))
362                 (let (nnml-current-directory
363                       nnml-current-group
364                       nnml-article-file-alist)
365                   (when (functionp target)
366                     (setq target (funcall target group)))
367                   (when (and target (not (eq target 'delete)))
368                     (if (or (gnus-request-group target)
369                             (gnus-request-create-group target))
370                         (nnmail-expiry-target-group target group)
371                       (setq target nil)))))
372               ;; Maybe directory is changed during nnmail-expiry-target-group.
373               (nnml-possibly-change-directory group server))
374             (if target
375                 (progn
376                   (nnheader-message 5 "Deleting article %s in %s"
377                                     number decoded)
378                   (condition-case ()
379                       (funcall nnmail-delete-file-function article)
380                     (file-error
381                      (push number rest)))
382                   (setq active-articles (delq number active-articles))
383                   (nnml-nov-delete-article group number))
384               (push number rest)))
385         (push number rest)))
386     (let ((active (nth 1 (assoc group nnml-group-alist))))
387       (when active
388         (setcar active (or (and active-articles
389                                 (apply 'min active-articles))
390                            (1+ (cdr active)))))
391       (nnmail-save-active nnml-group-alist nnml-active-file))
392     (nnml-save-nov)
393     (nconc rest articles)))
394
395 (deffoo nnml-request-move-article
396     (article group server accept-form &optional last move-is-internal)
397   (let ((buf (get-buffer-create " *nnml move*"))
398         (file-name-coding-system nnmail-pathname-coding-system)
399         result)
400     (nnml-possibly-change-directory group server)
401     (nnml-update-file-alist)
402     (and
403      (nnml-deletable-article-p group article)
404      (nnml-request-article article group server)
405      (let (nnml-current-directory
406            nnml-current-group
407            nnml-article-file-alist)
408        (save-excursion
409          (set-buffer buf)
410          (insert-buffer-substring nntp-server-buffer)
411          (setq result (eval accept-form))
412          (kill-buffer (current-buffer))
413          result))
414      (progn
415        (nnml-possibly-change-directory group server)
416        (condition-case ()
417            (funcall nnmail-delete-file-function
418                     (nnml-article-to-file  article))
419          (file-error nil))
420        (nnml-nov-delete-article group article)
421        (when last
422          (nnml-save-nov)
423          (nnmail-save-active nnml-group-alist nnml-active-file))))
424     result))
425
426 (deffoo nnml-request-accept-article (group &optional server last)
427   (nnml-possibly-change-directory group server)
428   (nnmail-check-syntax)
429   (let (result)
430     (when nnmail-cache-accepted-message-ids
431       (nnmail-cache-insert (nnmail-fetch-field "message-id")
432                            group
433                            (nnmail-fetch-field "subject")
434                            (nnmail-fetch-field "from")))
435     (if (stringp group)
436         (and
437          (nnmail-activate 'nnml)
438          (setq result (car (nnml-save-mail
439                             (list (cons group (nnml-active-number group
440                                                                   server)))
441                             server t)))
442          (progn
443            (nnmail-save-active nnml-group-alist nnml-active-file)
444            (and last (nnml-save-nov))))
445       (and
446        (nnmail-activate 'nnml)
447        (if (and (not (setq result (nnmail-article-group
448                                    `(lambda (group)
449                                       (nnml-active-number group ,server)))))
450                 (yes-or-no-p "Moved to `junk' group; delete article? "))
451            (setq result 'junk)
452          (setq result (car (nnml-save-mail result server t))))
453        (when last
454          (nnmail-save-active nnml-group-alist nnml-active-file)
455          (when nnmail-cache-accepted-message-ids
456            (nnmail-cache-close))
457          (nnml-save-nov))))
458     result))
459
460 (deffoo nnml-request-post (&optional server)
461   (nnmail-do-request-post 'nnml-request-accept-article server))
462
463 (deffoo nnml-request-replace-article (article group buffer)
464   (nnml-possibly-change-directory group)
465   (save-excursion
466     (set-buffer buffer)
467     (nnml-possibly-create-directory group)
468     (let ((chars (nnmail-insert-lines))
469           (art (concat (int-to-string article) "\t"))
470           headers)
471       (when (ignore-errors
472               (nnmail-write-region
473                (point-min) (point-max)
474                (or (nnml-article-to-file article)
475                    (expand-file-name (int-to-string article)
476                                      nnml-current-directory))
477                nil (if (nnheader-be-verbose 5) nil 'nomesg))
478               t)
479         (setq headers (nnml-parse-head chars article))
480         ;; Replace the NOV line in the NOV file.
481         (save-excursion
482           (set-buffer (nnml-open-nov group))
483           (goto-char (point-min))
484           (if (or (looking-at art)
485                   (search-forward (concat "\n" art) nil t))
486               ;; Delete the old NOV line.
487               (gnus-delete-line)
488             ;; The line isn't here, so we have to find out where
489             ;; we should insert it.  (This situation should never
490             ;; occur, but one likes to make sure...)
491             (while (and (looking-at "[0-9]+\t")
492                         (< (string-to-number
493                             (buffer-substring
494                              (match-beginning 0) (match-end 0)))
495                            article)
496                         (zerop (forward-line 1)))))
497           (beginning-of-line)
498           (nnheader-insert-nov headers)
499           (nnml-save-nov)
500           t)))))
501
502 (deffoo nnml-request-delete-group (group &optional force server)
503   (nnml-possibly-change-directory group server)
504   (let ((file (directory-file-name nnml-current-directory))
505         (file-name-coding-system nnmail-pathname-coding-system))
506     (if (file-exists-p file)
507         (if (file-directory-p file)
508             (progn
509               (when force
510                 ;; Delete all articles in GROUP.
511                 (let ((articles
512                        (directory-files
513                         nnml-current-directory t
514                         (concat
515                          nnheader-numerical-short-files
516                          "\\|" (regexp-quote nnml-nov-file-name) "$"
517                          "\\|" (regexp-quote nnml-marks-file-name) "$")))
518                       (decoded (nnml-decoded-group-name group server)))
519                   (dolist (article articles)
520                     (when (file-writable-p article)
521                       (nnheader-message 5 "Deleting article %s in %s..."
522                                         (file-name-nondirectory article)
523                                         decoded)
524                       (funcall nnmail-delete-file-function article))))
525                 ;; Try to delete the directory itself.
526                 (ignore-errors (delete-directory nnml-current-directory))))
527           (nnheader-report 'nnml "%s is not a directory" file))
528       (nnheader-report 'nnml "No such directory: %s/" file))
529     ;; Remove the group from all structures.
530     (setq nnml-group-alist
531           (delq (assoc group nnml-group-alist) nnml-group-alist)
532           nnml-current-group nil
533           nnml-current-directory nil)
534     ;; Save the active file.
535     (nnmail-save-active nnml-group-alist nnml-active-file))
536   t)
537
538 (deffoo nnml-request-rename-group (group new-name &optional server)
539   (nnml-possibly-change-directory group server)
540   (let ((new-dir (nnml-group-pathname new-name nil server))
541         (old-dir (nnml-group-pathname group nil server))
542         (file-name-coding-system nnmail-pathname-coding-system))
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 full-nov)
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     (let ((func (if full-nov
746                     'nnml-add-nov
747                   'nnml-add-incremental-nov)))
748       (if nnmail-group-names-not-encoded-p
749           (dolist (ga group-art)
750             (funcall func (pop dec) (cdr ga) headers))
751         (dolist (ga group-art)
752           (funcall func (car ga) (cdr ga) headers)))))
753   group-art)
754
755 (defun nnml-active-number (group &optional server)
756   "Compute the next article number in GROUP on SERVER."
757   (let* ((encoded (if nnmail-group-names-not-encoded-p
758                       (nnml-encoded-group-name group server)))
759          (active (cadr (assoc (or encoded group) nnml-group-alist))))
760     ;; The group wasn't known to nnml, so we just create an active
761     ;; entry for it.
762     (unless active
763       ;; Perhaps the active file was corrupt?  See whether
764       ;; there are any articles in this group.
765       (nnml-possibly-create-directory group server)
766       (nnml-possibly-change-directory group server)
767       (unless nnml-article-file-alist
768         (setq nnml-article-file-alist
769               (sort
770                (nnml-current-group-article-to-file-alist)
771                'car-less-than-car)))
772       (setq active
773             (if nnml-article-file-alist
774                 (cons (caar nnml-article-file-alist)
775                       (caar (last nnml-article-file-alist)))
776               (cons 1 0)))
777       (push (list (or encoded group) active) nnml-group-alist))
778     (setcdr active (1+ (cdr active)))
779     (while (file-exists-p
780             (nnml-group-pathname group (int-to-string (cdr active)) server))
781       (setcdr active (1+ (cdr active))))
782     (cdr active)))
783
784 (defvar nnml-incremental-nov-buffer-alist nil)
785
786 (defun nnml-save-incremental-nov ()
787   (message "nnml saving incremental nov...")
788   (save-excursion
789     (while nnml-incremental-nov-buffer-alist
790       (when (buffer-name (cdar nnml-incremental-nov-buffer-alist))
791         (set-buffer (cdar nnml-incremental-nov-buffer-alist))
792         (when (buffer-modified-p)
793           (nnmail-write-region (point-min) (point-max)
794                                nnml-nov-buffer-file-name t 'nomesg))
795         (set-buffer-modified-p nil)
796         (kill-buffer (current-buffer)))
797       (setq nnml-incremental-nov-buffer-alist
798             (cdr nnml-incremental-nov-buffer-alist))))
799   (message "nnml saving incremental nov...done"))
800
801 (defun nnml-open-incremental-nov (group)
802   (or (cdr (assoc group nnml-incremental-nov-buffer-alist))
803       (let ((buffer (nnml-get-nov-buffer group t)))
804         (push (cons group buffer) nnml-incremental-nov-buffer-alist)
805         buffer)))
806
807 (defun nnml-add-incremental-nov (group article headers)
808   "Add a nov line for the GROUP nov headers, incrementally."
809   (save-excursion
810     (set-buffer (nnml-open-incremental-nov group))
811     (goto-char (point-max))
812     (mail-header-set-number headers article)
813     (nnheader-insert-nov headers)))
814
815 (defun nnml-add-nov (group article headers)
816   "Add a nov line for the GROUP base."
817   (save-excursion
818     (set-buffer (nnml-open-nov group))
819     (goto-char (point-max))
820     (mail-header-set-number headers article)
821     (nnheader-insert-nov headers)))
822
823 (defsubst nnml-header-value ()
824   (buffer-substring (match-end 0) (point-at-eol)))
825
826 (defun nnml-parse-head (chars &optional number)
827   "Parse the head of the current buffer."
828   (save-excursion
829     (save-restriction
830       (unless (zerop (buffer-size))
831         (narrow-to-region
832          (goto-char (point-min))
833          (if (re-search-forward "\n\r?\n" nil t)
834              (1- (point))
835            (point-max))))
836       (let ((headers (nnheader-parse-naked-head)))
837         (mail-header-set-chars headers chars)
838         (mail-header-set-number headers number)
839         headers))))
840
841 (defun nnml-get-nov-buffer (group &optional incrementalp)
842   (let* ((decoded (nnml-decoded-group-name group))
843          (buffer (get-buffer-create (format " *nnml %soverview %s*"
844                                             (if incrementalp
845                                                 "incremental "
846                                               "")
847                                             decoded)))
848          (file-name-coding-system nnmail-pathname-coding-system))
849     (save-excursion
850       (set-buffer buffer)
851       (set (make-local-variable 'nnml-nov-buffer-file-name)
852            (nnmail-group-pathname decoded nnml-directory nnml-nov-file-name))
853       (erase-buffer)
854       (when (and (not incrementalp)
855                  (file-exists-p nnml-nov-buffer-file-name))
856         (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
857     buffer))
858
859 (defun nnml-open-nov (group)
860   (or (cdr (assoc group nnml-nov-buffer-alist))
861       (let ((buffer (nnml-get-nov-buffer group)))
862         (push (cons group buffer) nnml-nov-buffer-alist)
863         buffer)))
864
865 (defun nnml-save-nov ()
866   (message "nnml saving nov...")
867   (save-excursion
868     (while nnml-nov-buffer-alist
869       (when (buffer-name (cdar nnml-nov-buffer-alist))
870         (set-buffer (cdar nnml-nov-buffer-alist))
871         (when (buffer-modified-p)
872           (nnmail-write-region (point-min) (point-max)
873                                nnml-nov-buffer-file-name nil 'nomesg))
874         (set-buffer-modified-p nil)
875         (kill-buffer (current-buffer)))
876       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist))))
877   (message "nnml saving nov...done"))
878
879 ;;;###autoload
880 (defun nnml-generate-nov-databases (&optional server)
881   "Generate NOV databases in all nnml directories."
882   (interactive (list (or (nnoo-current-server 'nnml) "")))
883   ;; Read the active file to make sure we don't re-use articles
884   ;; numbers in empty groups.
885   (nnmail-activate 'nnml)
886   (unless (nnml-server-opened server)
887     (nnml-open-server server))
888   (setq nnml-directory (expand-file-name nnml-directory))
889   ;; Recurse down the directories.
890   (nnml-generate-nov-databases-directory nnml-directory nil t)
891   ;; Save the active file.
892   (nnmail-save-active nnml-group-alist nnml-active-file))
893
894 (defun nnml-generate-nov-databases-directory (dir &optional seen no-active)
895   "Regenerate the NOV database in DIR.
896
897 Unless no-active is non-nil, update the active file too."
898   (interactive (list (let ((file-name-coding-system
899                             nnmail-pathname-coding-system))
900                        (read-directory-name "Regenerate NOV in: "
901                                             nnml-directory nil t))))
902   (setq dir (file-name-as-directory dir))
903   (let ((file-name-coding-system nnmail-pathname-coding-system))
904     ;; Only scan this sub-tree if we haven't been here yet.
905     (unless (member (file-truename dir) seen)
906       (push (file-truename dir) seen)
907       ;; We descend recursively
908       (dolist (dir (directory-files dir t nil t))
909         (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
910                    (file-directory-p dir))
911           (nnml-generate-nov-databases-directory dir seen)))
912       ;; Do this directory.
913       (let ((files (sort (nnheader-article-to-file-alist dir)
914                          'car-less-than-car)))
915         (if (not files)
916             (let* ((group (nnheader-file-to-group
917                            (directory-file-name dir) nnml-directory))
918                    (info (cadr (assoc group nnml-group-alist))))
919               (when info
920                 (setcar info (1+ (cdr info)))))
921           (funcall nnml-generate-active-function dir)
922           ;; Generate the nov file.
923           (nnml-generate-nov-file dir files)
924           (unless no-active
925             (nnmail-save-active nnml-group-alist nnml-active-file)))))))
926
927 (defvar files)
928 (defun nnml-generate-active-info (dir)
929   ;; Update the active info for this group.
930   (let ((group (directory-file-name dir))
931         entry last)
932     (setq group (nnheader-file-to-group (nnml-encoded-group-name group)
933                                         nnml-directory)
934           entry (assoc group nnml-group-alist)
935           last (or (caadr entry) 0)
936           nnml-group-alist (delq entry nnml-group-alist))
937     (push (list group
938                 (cons (or (caar files) (1+ last))
939                       (max last
940                            (or (caar (last files))
941                                0))))
942           nnml-group-alist)))
943
944 (defun nnml-generate-nov-file (dir files)
945   (let* ((dir (file-name-as-directory dir))
946          (nov (concat dir nnml-nov-file-name))
947          (nov-buffer (get-buffer-create " *nov*"))
948          chars file headers)
949     (save-excursion
950       ;; Init the nov buffer.
951       (set-buffer nov-buffer)
952       (buffer-disable-undo)
953       (erase-buffer)
954       (set-buffer nntp-server-buffer)
955       ;; Delete the old NOV file.
956       (when (file-exists-p nov)
957         (funcall nnmail-delete-file-function nov))
958       (while files
959         (unless (file-directory-p (setq file (concat dir (cdar files))))
960           (erase-buffer)
961           (nnheader-insert-file-contents file)
962           (narrow-to-region
963            (goto-char (point-min))
964            (progn
965              (re-search-forward "\n\r?\n" nil t)
966              (setq chars (- (point-max) (point)))
967              (max (point-min) (1- (point)))))
968           (unless (zerop (buffer-size))
969             (goto-char (point-min))
970             (setq headers (nnml-parse-head chars (caar files)))
971             (save-excursion
972               (set-buffer nov-buffer)
973               (goto-char (point-max))
974               (nnheader-insert-nov headers)))
975           (widen))
976         (setq files (cdr files)))
977       (save-excursion
978         (set-buffer nov-buffer)
979         (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
980         (kill-buffer (current-buffer))))))
981
982 (defun nnml-nov-delete-article (group article)
983   (save-excursion
984     (set-buffer (nnml-open-nov group))
985     (when (nnheader-find-nov-line article)
986       (delete-region (point) (progn (forward-line 1) (point)))
987       (when (bobp)
988         (let ((active (cadr (assoc group nnml-group-alist)))
989               num)
990           (when active
991             (if (eobp)
992                 (setf (car active) (1+ (cdr active)))
993               (when (and (setq num (ignore-errors (read (current-buffer))))
994                          (numberp num))
995                 (setf (car active) num)))))))
996     t))
997
998 (defun nnml-update-file-alist (&optional force)
999   (when nnml-use-compressed-files
1000     (when (or (not nnml-article-file-alist)
1001               force)
1002       (setq nnml-article-file-alist
1003             (nnml-current-group-article-to-file-alist)))))
1004
1005 (defun nnml-directory-articles (dir)
1006   "Return a list of all article files in a directory.
1007 Use the nov database for that directory if available."
1008   (if (or gnus-nov-is-evil nnml-nov-is-evil
1009           (not (file-exists-p
1010                 (expand-file-name nnml-nov-file-name dir))))
1011       (nnheader-directory-articles dir)
1012     ;; build list from .overview if available
1013     ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
1014     ;; defvoo'd, and we might get called when it hasn't been swapped in.
1015     (save-excursion
1016       (let ((list nil)
1017             art
1018             (buffer (nnml-get-nov-buffer nnml-current-group)))
1019         (set-buffer buffer)
1020         (goto-char (point-min))
1021         (while (not (eobp))
1022           (setq art (read (current-buffer)))
1023           (push art list)
1024           (forward-line 1))
1025         list))))
1026
1027 (defun nnml-current-group-article-to-file-alist ()
1028   "Return an alist of article/file pairs in the current group.
1029 Use the nov database for the current group if available."
1030   (if (or nnml-use-compressed-files
1031           gnus-nov-is-evil
1032           nnml-nov-is-evil
1033           (not (file-exists-p
1034                 (expand-file-name nnml-nov-file-name
1035                                   nnml-current-directory))))
1036       (nnheader-article-to-file-alist nnml-current-directory)
1037     ;; build list from .overview if available
1038     (save-excursion
1039       (let ((alist nil)
1040             (buffer (nnml-get-nov-buffer nnml-current-group))
1041             art)
1042         (set-buffer buffer)
1043         (goto-char (point-min))
1044         (while (not (eobp))
1045           (setq art (read (current-buffer)))
1046           ;; assume file name is unadorned (ie. not compressed etc)
1047           (push (cons art (int-to-string art)) alist)
1048           (forward-line 1))
1049         alist))))
1050
1051 (deffoo nnml-request-set-mark (group actions &optional server)
1052   (nnml-possibly-change-directory group server)
1053   (unless nnml-marks-is-evil
1054     (nnml-open-marks group server)
1055     (dolist (action actions)
1056       (let ((range (nth 0 action))
1057             (what  (nth 1 action))
1058             (marks (nth 2 action)))
1059         (assert (or (eq what 'add) (eq what 'del)) nil
1060                 "Unknown request-set-mark action: %s" what)
1061         (dolist (mark marks)
1062           (setq nnml-marks (gnus-update-alist-soft
1063                             mark
1064                             (funcall (if (eq what 'add) 'gnus-range-add
1065                                        'gnus-remove-from-range)
1066                                      (cdr (assoc mark nnml-marks)) range)
1067                             nnml-marks)))))
1068     (nnml-save-marks group server))
1069   nil)
1070
1071 (deffoo nnml-request-update-info (group info &optional server)
1072   (nnml-possibly-change-directory group server)
1073   (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group server))
1074     (nnheader-message 8 "Updating marks for %s..." group)
1075     (nnml-open-marks group server)
1076     ;; Update info using `nnml-marks'.
1077     (mapc (lambda (pred)
1078             (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
1079               (gnus-info-set-marks
1080                info
1081                (gnus-update-alist-soft
1082                 (cdr pred)
1083                 (cdr (assq (cdr pred) nnml-marks))
1084                 (gnus-info-marks info))
1085                t)))
1086           gnus-article-mark-lists)
1087     (let ((seen (cdr (assq 'read nnml-marks))))
1088       (gnus-info-set-read info
1089                           (if (and (integerp (car seen))
1090                                    (null (cdr seen)))
1091                               (list (cons (car seen) (car seen)))
1092                             seen)))
1093     (nnheader-message 8 "Updating marks for %s...done" group))
1094   info)
1095
1096 (defun nnml-marks-changed-p (group server)
1097   (let ((file (nnml-group-pathname group nnml-marks-file-name server)))
1098     (if (null (gnus-gethash file nnml-marks-modtime))
1099         t ;; never looked at marks file, assume it has changed
1100       (not (equal (gnus-gethash file nnml-marks-modtime)
1101                   (nth 5 (file-attributes file)))))))
1102
1103 (defun nnml-save-marks (group server)
1104   (let ((file-name-coding-system nnmail-pathname-coding-system)
1105         (file (nnml-group-pathname group nnml-marks-file-name server)))
1106     (condition-case err
1107         (progn
1108           (nnml-possibly-create-directory group server)
1109           (with-temp-file file
1110             (erase-buffer)
1111             (gnus-prin1 nnml-marks)
1112             (insert "\n"))
1113           (gnus-sethash file
1114                         (nth 5 (file-attributes file))
1115                         nnml-marks-modtime))
1116       (error (or (gnus-yes-or-no-p
1117                   (format "Could not write to %s (%s).  Continue? " file err))
1118                  (error "Cannot write to %s (%s)" file err))))))
1119
1120 (defun nnml-open-marks (group server)
1121   (let* ((decoded (nnml-decoded-group-name group server))
1122          (file (nnmail-group-pathname decoded nnml-directory
1123                                       nnml-marks-file-name))
1124          (file-name-coding-system nnmail-pathname-coding-system))
1125     (if (file-exists-p file)
1126         (condition-case err
1127             (with-temp-buffer
1128               (gnus-sethash file (nth 5 (file-attributes file))
1129                             nnml-marks-modtime)
1130               (nnheader-insert-file-contents file)
1131               (setq nnml-marks (read (current-buffer)))
1132               (dolist (el gnus-article-unpropagated-mark-lists)
1133                 (setq nnml-marks (gnus-remassoc el nnml-marks))))
1134           (error (or (gnus-yes-or-no-p
1135                       (format "Error reading nnml marks file %s (%s).  Continuing will use marks from .newsrc.eld.  Continue? " file err))
1136                      (error "Cannot read nnml marks file %s (%s)" file err))))
1137       ;; User didn't have a .marks file.  Probably first time
1138       ;; user of the .marks stuff.  Bootstrap it from .newsrc.eld.
1139       (let ((info (gnus-get-info
1140                    (gnus-group-prefixed-name
1141                     group
1142                     (gnus-server-to-method
1143                      (format "nnml:%s" (or server "")))))))
1144         (setq decoded (if (member server '(nil ""))
1145                           (concat "nnml:" decoded)
1146                         (format "nnml+%s:%s" server decoded)))
1147         (nnheader-message 7 "Bootstrapping marks for %s..." decoded)
1148         (setq nnml-marks (gnus-info-marks info))
1149         (push (cons 'read (gnus-info-read info)) nnml-marks)
1150         (dolist (el gnus-article-unpropagated-mark-lists)
1151           (setq nnml-marks (gnus-remassoc el nnml-marks)))
1152         (nnml-save-marks group server)
1153         (nnheader-message 7 "Bootstrapping marks for %s...done" decoded)))))
1154
1155
1156 ;;;
1157 ;;; Group and server compaction. -- dvl
1158 ;;;
1159
1160 ;; #### FIXME: this function handles self Xref: entry correctly, but I don't
1161 ;; #### know how to handle external cross-references. I actually don't know if
1162 ;; #### this is handled correctly elsewhere. For instance, what happens if you
1163 ;; #### move all articles to a new group (that's what people do for manual
1164 ;; #### compaction) ?
1165
1166 ;; #### NOTE: the function below handles the article backlog. This is
1167 ;; #### conceptually the wrong place to do it because the backend is at a
1168 ;; #### lower level. However, this is the only place where we have the needed
1169 ;; #### information to do the job. Ideally, this function should not handle
1170 ;; #### the backlog by itself, but return a list of moved groups / articles to
1171 ;; #### the caller. This will become important to avoid code duplication when
1172 ;; #### other backends get a compaction feature. Also, note that invalidating
1173 ;; #### the "original article buffer" is already done at an upper level.
1174
1175 ;; Shouldn't `nnml-request-compact-group' be interactive? --rsteib
1176
1177 (defun nnml-request-compact-group (group &optional server save)
1178   (nnml-possibly-change-directory group server)
1179   (unless nnml-article-file-alist
1180     (setq nnml-article-file-alist
1181           (sort (nnml-current-group-article-to-file-alist)
1182                 'car-less-than-car)))
1183   (if (not nnml-article-file-alist)
1184       ;; The group is empty: do nothing but return t
1185       t
1186     ;; The group is not empty:
1187     (let* ((group-full-name
1188             (gnus-group-prefixed-name
1189              group
1190              (gnus-server-to-method (format "nnml:%s" server))))
1191            (info (gnus-get-info group-full-name))
1192            (new-number 1)
1193            compacted)
1194       (let ((articles nnml-article-file-alist)
1195             article)
1196         (while (setq article (pop articles))
1197           (let ((old-number (car article)))
1198             (when (> old-number new-number)
1199               ;; There is a gap here:
1200               (let ((old-number-string (int-to-string old-number))
1201                     (new-number-string (int-to-string new-number)))
1202                 (setq compacted t)
1203                 ;; #### NOTE: `nnml-article-to-file' calls
1204                 ;; #### `nnml-update-file-alist'  (which in turn calls
1205                 ;; #### `nnml-current-group-article-to-file-alist', which
1206                 ;; #### might use the NOV database). This might turn out to be
1207                 ;; #### inefficient. In that case, we will do the work
1208                 ;; #### manually.
1209                 ;; 1/ Move the article to a new file:
1210                 (let* ((oldfile (nnml-article-to-file old-number))
1211                        (newfile
1212                         (gnus-replace-in-string
1213                          oldfile
1214                          ;; nnml-use-compressed-files might be any string, but
1215                          ;; probably it's sufficient to take into account only
1216                          ;; "\\.[a-z0-9]+".  Note that we can't only use the
1217                          ;; value of nnml-use-compressed-files because old
1218                          ;; articles might have been saved with a different
1219                          ;; value.
1220                          (concat
1221                           "\\(" old-number-string "\\)\\(\\(\\.[a-z0-9]+\\)?\\)$")
1222                          (concat new-number-string "\\2"))))
1223                   (with-current-buffer nntp-server-buffer
1224                     (nnmail-find-file oldfile)
1225                     ;; Update the Xref header in the article itself:
1226                     (when (and (re-search-forward "^Xref: [^ ]+ " nil t)
1227                                (re-search-forward
1228                                 (concat "\\<"
1229                                         (regexp-quote
1230                                          (concat group ":" old-number-string))
1231                                         "\\>")
1232                                 (point-at-eol) t))
1233                       (replace-match
1234                        (concat group ":" new-number-string)))
1235                     ;; Save to the new file:
1236                     (nnmail-write-region (point-min) (point-max) newfile))
1237                   (funcall nnmail-delete-file-function oldfile))
1238                 ;; 2/ Update all marks for this article:
1239                 ;; #### NOTE: it is possible that the new article number
1240                 ;; #### already belongs to a range, whereas the corresponding
1241                 ;; #### article doesn't exist (for example, if you delete an
1242                 ;; #### article). For that reason, it is important to update
1243                 ;; #### the ranges (meaning remove inexistent articles) before
1244                 ;; #### doing anything on them.
1245                 ;; 2 a/ read articles:
1246                 (let ((read (gnus-info-read info)))
1247                   (setq read (gnus-remove-from-range read (list new-number)))
1248                   (when (gnus-member-of-range old-number read)
1249                     (setq read (gnus-remove-from-range read (list old-number)))
1250                     (setq read (gnus-add-to-range read (list new-number))))
1251                   (gnus-info-set-read info read))
1252                 ;; 2 b/ marked articles:
1253                 (let ((oldmarks (gnus-info-marks info))
1254                       mark newmarks)
1255                   (while (setq mark (pop oldmarks))
1256                     (setcdr mark (gnus-remove-from-range (cdr mark)
1257                                                          (list new-number)))
1258                     (when (gnus-member-of-range old-number (cdr mark))
1259                       (setcdr mark (gnus-remove-from-range (cdr mark)
1260                                                            (list old-number)))
1261                       (setcdr mark (gnus-add-to-range (cdr mark)
1262                                                       (list new-number))))
1263                     (push mark newmarks))
1264                   (gnus-info-set-marks info newmarks))
1265                 ;; 3/ Update the NOV entry for this article:
1266                 (unless nnml-nov-is-evil
1267                   (save-excursion
1268                     (set-buffer (nnml-open-nov group))
1269                     (when (nnheader-find-nov-line old-number)
1270                       ;; Replace the article number:
1271                       (looking-at old-number-string)
1272                       (replace-match new-number-string nil t)
1273                       ;; Update the Xref header:
1274                       (when (re-search-forward
1275                              (concat "\\(Xref:[^\t\n]* \\)\\<"
1276                                      (regexp-quote
1277                                       (concat group ":" old-number-string))
1278                                      "\\>")
1279                              (point-at-eol) t)
1280                         (replace-match
1281                          (concat "\\1" group ":" new-number-string))))))
1282                 ;; 4/ Possibly remove the article from the backlog:
1283                 (when gnus-keep-backlog
1284                   ;; #### NOTE: instead of removing the article, we could
1285                   ;; #### modify the backlog to reflect the numbering change,
1286                   ;; #### but I don't think it's worth it.
1287                   (gnus-backlog-remove-article group-full-name old-number)
1288                   (gnus-backlog-remove-article group-full-name new-number))))
1289             (setq new-number (1+ new-number)))))
1290       (if (not compacted)
1291           ;; No compaction had to be done:
1292           t
1293         ;; Some articles have actually been renamed:
1294         ;; 1/ Rebuild active information:
1295         (let ((entry (assoc group nnml-group-alist))
1296               (active (cons 1 (1- new-number))))
1297           (setq nnml-group-alist (delq entry nnml-group-alist))
1298           (push (list group active) nnml-group-alist)
1299           ;; Update the active hashtable to let the *Group* buffer display
1300           ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
1301           ;; gnus-newwrc-alist are out of date, since all we did is to modify
1302           ;; the info of the group internally.
1303           (gnus-set-active group-full-name active))
1304         ;; 1 bis/
1305         ;; #### NOTE: normally, we should save the overview (NOV) file
1306         ;; #### here, just like we save the marks file. However, there is no
1307         ;; #### such function as nnml-save-nov for a single group. Only for
1308         ;; #### all groups. Gnus inconsistency is getting worse every day...
1309         ;; 2/ Rebuild marks file:
1310         (unless nnml-marks-is-evil
1311           ;; #### NOTE: this constant use of global variables everywhere is
1312           ;; #### truly disgusting. Gnus really needs a *major* cleanup.
1313           (setq nnml-marks (gnus-info-marks info))
1314           (push (cons 'read (gnus-info-read info)) nnml-marks)
1315           (dolist (el gnus-article-unpropagated-mark-lists)
1316             (setq nnml-marks (gnus-remassoc el nnml-marks)))
1317           (nnml-save-marks group server))
1318         ;; 3/ Save everything if this was not part of a bigger operation:
1319         (if (not save)
1320             ;; Nothing to save (yet):
1321             t
1322           ;; Something to save:
1323           ;; a/ Save the NOV databases:
1324           ;; #### NOTE: this should be done directory per directory in 1bis
1325           ;; #### above. See comment there.
1326           (nnml-save-nov)
1327           ;; b/ Save the active file:
1328           (nnmail-save-active nnml-group-alist nnml-active-file)
1329           t)))))
1330
1331 (defun nnml-request-compact (&optional server)
1332   "Request compaction of all SERVER nnml groups."
1333   (interactive (list (or (nnoo-current-server 'nnml) "")))
1334   (nnmail-activate 'nnml)
1335   (unless (nnml-server-opened server)
1336     (nnml-open-server server))
1337   (setq nnml-directory (expand-file-name nnml-directory))
1338   (let* ((groups (gnus-groups-from-server
1339                   (gnus-server-to-method (format "nnml:%s" server))))
1340          (first (pop groups))
1341          group)
1342     (when first
1343       (while (setq group (pop groups))
1344         (nnml-request-compact-group (gnus-group-real-name group) server))
1345       (nnml-request-compact-group (gnus-group-real-name first) server t))))
1346
1347
1348 (provide 'nnml)
1349
1350 ;;; nnml.el ends here