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