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