Feedback from `t-gnus-6_15-quimby' branch.
[elisp/gnus.git-] / lisp / nnfolder.el
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Simon Josefsson <simon@josefsson.org> (adding MARKS)
6 ;;      ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
7 ;;      Scott Byer <byer@mv.us.adobe.com>
8 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; Keywords: 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., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;;; Code:
32
33 (eval-when-compile (require 'cl))
34 (eval-when-compile (require 'gnus-clfns))
35
36 (require 'nnheader)
37 (require 'message)
38 (require 'nnmail)
39 (require 'nnoo)
40 (require 'gnus)
41 (require 'gnus-util)
42 (require 'gnus-range)
43
44 (eval-and-compile
45   (autoload 'gnus-article-unpropagatable-p "gnus-sum")
46   (autoload 'gnus-intersection "gnus-range"))
47
48 (nnoo-declare nnfolder)
49
50 (defvoo nnfolder-directory (expand-file-name message-directory)
51   "The name of the nnfolder directory.")
52
53 (defvoo nnfolder-nov-directory nil
54   "The name of the nnfolder NOV directory.
55 If nil, `nnfolder-directory' is used.")
56
57 (defvoo nnfolder-marks-directory nil
58   "The name of the nnfolder MARKS directory.
59 If nil, `nnfolder-directory' is used.")
60
61 (defvoo nnfolder-active-file
62     (nnheader-concat nnfolder-directory "active")
63   "The name of the active file.")
64
65 ;; I renamed this variable to something more in keeping with the general GNU
66 ;; style. -SLB
67
68 (defvoo nnfolder-ignore-active-file nil
69   "If non-nil, the active file is ignored.
70 This causes nnfolder to do some extra work in order to determine the
71 true active ranges of an mbox file.  Note that the active file is
72 still saved, but its values are not used.  This costs some extra time
73 when scanning an mbox when opening it.")
74
75 (defvoo nnfolder-distrust-mbox nil
76   "If non-nil, the folder will be distrusted.
77 This means that nnfolder will not trust the user with respect to
78 inserting unaccounted for mail in the middle of an mbox file.  This
79 can greatly slow down scans, which now must scan the entire file for
80 unmarked messages.  When nil, scans occur forward from the last marked
81 message, a huge time saver for large mailboxes.")
82
83 (defvoo nnfolder-newsgroups-file
84     (concat (file-name-as-directory nnfolder-directory) "newsgroups")
85   "Mail newsgroups description file.")
86
87 (defvoo nnfolder-get-new-mail t
88   "If non-nil, nnfolder will check the incoming mail file and split the mail.")
89
90 (defvoo nnfolder-prepare-save-mail-hook nil
91   "Hook run narrowed to an article before saving.")
92
93 (defvoo nnfolder-save-buffer-hook nil
94   "Hook run before saving the nnfolder mbox buffer.")
95
96
97 (defvoo nnfolder-inhibit-expiry nil
98   "If non-nil, inhibit expiry.")
99
100 \f
101
102 (defconst nnfolder-version "nnfolder 2.0"
103   "nnfolder version.")
104
105 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
106   "String used to demarcate what the article number for a message is.")
107
108 (defvoo nnfolder-current-group nil)
109 (defvoo nnfolder-current-buffer nil)
110 (defvoo nnfolder-status-string "")
111 (defvoo nnfolder-group-alist nil)
112 (defvoo nnfolder-buffer-alist nil)
113 (defvoo nnfolder-scantime-alist nil)
114 (defvoo nnfolder-active-timestamp nil)
115 (defvoo nnfolder-active-file-coding-system nnheader-text-coding-system)
116 (defvoo nnfolder-active-file-coding-system-for-write
117     nnmail-active-file-coding-system)
118 (defvoo nnfolder-file-coding-system nnheader-text-coding-system)
119 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
120   "Coding system for save nnfolder file.
121 if nil, `nnfolder-file-coding-system' is used.") ; FIXME: fill-in the doc-string of this variable
122
123 (defvoo nnfolder-nov-is-evil nil
124   "If non-nil, Gnus will never generate and use nov databases for mail groups.
125 Using nov databases will speed up header fetching considerably.
126 This variable shouldn't be flipped much.  If you have, for some reason,
127 set this to t, and want to set it to nil again, you should always run
128 the `nnfolder-generate-active-file' command.  The function will go
129 through all nnfolder directories and generate nov databases for them
130 all.  This may very well take some time.")
131
132 (defvoo nnfolder-nov-file-suffix ".nov")
133
134 (defvoo nnfolder-nov-buffer-alist nil)
135
136 (defvar nnfolder-nov-buffer-file-name nil)
137
138 (defvoo nnfolder-marks-is-evil nil
139   "If non-nil, Gnus will never generate and use marks file for mail groups.
140 Using marks files makes it possible to backup and restore mail groups
141 separately from `.newsrc.eld'.  If you have, for some reason, set
142 this to t, and want to set it to nil again, you should always remove
143 the corresponding marks file (usually base nnfolder file name
144 concatenated with `.mrk', but see `nnfolder-marks-file-suffix') for
145 the group.  Then the marks file will be regenerated properly by Gnus.")
146
147 (defvoo nnfolder-marks nil)
148
149 (defvoo nnfolder-marks-file-suffix ".mrk")
150
151 (defvar nnfolder-marks-modtime (gnus-make-hashtable))
152
153 \f
154
155 ;;; Interface functions
156
157 (nnoo-define-basics nnfolder)
158
159 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
160   (save-excursion
161     (set-buffer nntp-server-buffer)
162     (erase-buffer)
163     (let (article start stop)
164       (nnfolder-possibly-change-group group server)
165       (when nnfolder-current-buffer
166         (set-buffer nnfolder-current-buffer)
167         (goto-char (point-min))
168         (if (stringp (car articles))
169             'headers
170           (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
171               'nov
172             (setq articles (gnus-sorted-intersection
173                             ;; Is ARTICLES sorted?
174                             (sort articles '<)
175                             (nnfolder-existing-articles)))
176             (while (setq article (pop articles))
177               (set-buffer nnfolder-current-buffer)
178               (when (nnfolder-goto-article article)
179                 (setq start (point))
180                 (setq stop (if (search-forward "\n\n" nil t)
181                                (1- (point))
182                              (point-max)))
183                 (set-buffer nntp-server-buffer)
184                 (insert (format "221 %d Article retrieved.\n" article))
185                 (insert-buffer-substring nnfolder-current-buffer start stop)
186                 (goto-char (point-max))
187                 (insert ".\n")))
188             (set-buffer nntp-server-buffer)
189             (nnheader-fold-continuation-lines)
190             'headers))))))
191
192 (deffoo nnfolder-open-server (server &optional defs)
193   (nnoo-change-server 'nnfolder server defs)
194   (nnmail-activate 'nnfolder t)
195   (gnus-make-directory nnfolder-directory)
196   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
197     (and nnfolder-nov-directory
198          (gnus-make-directory nnfolder-nov-directory)))
199   (unless nnfolder-marks-is-evil
200     (and nnfolder-marks-directory
201          (gnus-make-directory nnfolder-marks-directory)))
202   (cond
203    ((not (file-exists-p nnfolder-directory))
204     (nnfolder-close-server)
205     (nnheader-report 'nnfolder "Couldn't create directory: %s"
206                      nnfolder-directory))
207    ((not (file-directory-p (file-truename nnfolder-directory)))
208     (nnfolder-close-server)
209     (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
210    (t
211     (nnmail-activate 'nnfolder)
212     (nnheader-report 'nnfolder "Opened server %s using directory %s"
213                      server nnfolder-directory)
214     t)))
215
216 (deffoo nnfolder-request-close ()
217   (let ((alist nnfolder-buffer-alist))
218     (while alist
219       (nnfolder-close-group (caar alist) nil t)
220       (setq alist (cdr alist))))
221   (nnoo-close-server 'nnfolder)
222   (setq nnfolder-buffer-alist nil
223         nnfolder-group-alist nil))
224
225 (deffoo nnfolder-request-article (article &optional group server buffer)
226   (nnfolder-possibly-change-group group server)
227   (save-excursion
228     (set-buffer nnfolder-current-buffer)
229     (goto-char (point-min))
230     (when (nnfolder-goto-article article)
231       (let (start stop)
232         (setq start (point))
233         (forward-line 1)
234         (unless (and (nnmail-search-unix-mail-delim)
235                      (forward-line -1))
236           (goto-char (point-max)))
237         (setq stop (point))
238         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
239           (set-buffer nntp-server-buffer)
240           (erase-buffer)
241           (insert-buffer-substring nnfolder-current-buffer start stop)
242           (goto-char (point-min))
243           (while (looking-at "From ")
244             (delete-char 5)
245             (insert "X-From-Line: ")
246             (forward-line 1))
247           (if (numberp article)
248               (cons nnfolder-current-group article)
249             (goto-char (point-min))
250             (cons nnfolder-current-group
251                   (if (search-forward (concat "\n" nnfolder-article-marker)
252                                       nil t)
253                       (string-to-int
254                        (buffer-substring
255                         (point) (progn (end-of-line) (point))))
256                     -1))))))))
257
258 (deffoo nnfolder-request-group (group &optional server dont-check)
259   (nnfolder-possibly-change-group group server t)
260   (save-excursion
261     (if (not (assoc group nnfolder-group-alist))
262         (nnheader-report 'nnfolder "No such group: %s" group)
263       (if dont-check
264           (progn
265             (nnheader-report 'nnfolder "Selected group %s" group)
266             t)
267         (let* ((active (assoc group nnfolder-group-alist))
268                (group (car active))
269                (range (cadr active)))
270           (cond
271            ((null active)
272             (nnheader-report 'nnfolder "No such group: %s" group))
273            ((null nnfolder-current-group)
274             (nnheader-report 'nnfolder "Empty group: %s" group))
275            (t
276             (nnheader-report 'nnfolder "Selected group %s" group)
277             (nnheader-insert "211 %d %d %d %s\n"
278                              (1+ (- (cdr range) (car range)))
279                              (car range) (cdr range) group))))))))
280
281 (deffoo nnfolder-request-scan (&optional group server)
282   (nnfolder-possibly-change-group nil server)
283   (when nnfolder-get-new-mail
284     (nnfolder-possibly-change-group group server)
285     (nnmail-get-new-mail
286      'nnfolder
287      (lambda ()
288        (let ((bufs nnfolder-buffer-alist))
289          (save-excursion
290            (while bufs
291              (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
292                  (setq nnfolder-buffer-alist
293                        (delq (car bufs) nnfolder-buffer-alist))
294                (set-buffer (nth 1 (car bufs)))
295                (nnfolder-save-buffer)
296                (kill-buffer (current-buffer)))
297              (setq bufs (cdr bufs))))))
298      nnfolder-directory
299      group)))
300
301 ;; Don't close the buffer if we're not shutting down the server.  This way,
302 ;; we can keep the buffer in the group buffer cache, and not have to grovel
303 ;; over the buffer again unless we add new mail to it or modify it in some
304 ;; way.
305
306 (deffoo nnfolder-close-group (group &optional server force)
307   ;; Make sure we _had_ the group open.
308   (when (or (assoc group nnfolder-buffer-alist)
309             (equal group nnfolder-current-group))
310     (let ((inf (assoc group nnfolder-buffer-alist)))
311       (when inf
312         (when (and nnfolder-current-group
313                    nnfolder-current-buffer)
314           (push (list nnfolder-current-group nnfolder-current-buffer)
315                 nnfolder-buffer-alist))
316         (setq nnfolder-buffer-alist
317               (delq inf nnfolder-buffer-alist))
318         (setq nnfolder-current-buffer (cadr inf)
319               nnfolder-current-group (car inf))))
320     (when (and nnfolder-current-buffer
321                (buffer-name nnfolder-current-buffer))
322       (save-excursion
323         (set-buffer nnfolder-current-buffer)
324         ;; If the buffer was modified, write the file out now.
325         (nnfolder-save-buffer)
326         ;; If we're shutting the server down, we need to kill the
327         ;; buffer and remove it from the open buffer list.  Or, of
328         ;; course, if we're trying to minimize our space impact.
329         (kill-buffer (current-buffer))
330         (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
331                                           nnfolder-buffer-alist)))))
332   (setq nnfolder-current-group nil
333         nnfolder-current-buffer nil)
334   t)
335
336 (deffoo nnfolder-request-create-group (group &optional server args)
337   (nnfolder-possibly-change-group nil server)
338   (nnmail-activate 'nnfolder)
339   (when group
340     (unless (assoc group nnfolder-group-alist)
341       (push (list group (cons 1 0)) nnfolder-group-alist)
342       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
343       (nnfolder-read-folder group)))
344   t)
345
346 (deffoo nnfolder-request-list (&optional server)
347   (nnfolder-possibly-change-group nil server)
348   (save-excursion
349     (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
350       (nnmail-find-file nnfolder-active-file)
351       (setq nnfolder-group-alist (nnmail-get-active)))
352     t))
353
354 (deffoo nnfolder-request-newgroups (date &optional server)
355   (nnfolder-possibly-change-group nil server)
356   (nnfolder-request-list server))
357
358 (deffoo nnfolder-request-list-newsgroups (&optional server)
359   (nnfolder-possibly-change-group nil server)
360   (save-excursion
361     (let ((nnmail-file-coding-system nnfolder-file-coding-system))
362       (nnmail-find-file nnfolder-newsgroups-file))))
363
364 ;; Return a list consisting of all article numbers existing in the
365 ;; current folder.
366
367 (defun nnfolder-existing-articles ()
368   (save-excursion
369     (when nnfolder-current-buffer
370       (set-buffer nnfolder-current-buffer)
371       (goto-char (point-min))
372       (let ((marker (concat "\n" nnfolder-article-marker))
373             (number "[0-9]+")
374             numbers)
375         (while (and (search-forward marker nil t)
376                     (re-search-forward number nil t))
377           (let ((newnum (string-to-number (match-string 0))))
378             (if (nnmail-within-headers-p)
379                 (push newnum numbers))))
380         ;; The article numbers are increasing, so this result is sorted.
381         (nreverse numbers)))))
382
383 (deffoo nnfolder-request-expire-articles
384     (articles newsgroup &optional server force)
385   (nnfolder-possibly-change-group newsgroup server)
386   (let* ((is-old t)
387          ;; The articles we have deleted so far.
388          (deleted-articles nil)
389          ;; The articles that really exist and will
390          ;; be expired if they are old enough.
391          (maybe-expirable
392           (gnus-sorted-intersection articles (nnfolder-existing-articles))))
393     (nnmail-activate 'nnfolder)
394
395     (save-excursion
396       (set-buffer nnfolder-current-buffer)
397       ;; Since messages are sorted in arrival order and expired in the
398       ;; same order, we can stop as soon as we find a message that is
399       ;; too old.
400       (while (and maybe-expirable is-old)
401         (goto-char (point-min))
402         (when (and (nnfolder-goto-article (car maybe-expirable))
403                    (search-forward (concat "\n" nnfolder-article-marker)
404                                    nil t))
405           (forward-sexp)
406           (when (setq is-old
407                       (nnmail-expired-article-p
408                        newsgroup
409                        (buffer-substring
410                         (point) (progn (end-of-line) (point)))
411                        force nnfolder-inhibit-expiry))
412             (unless (eq nnmail-expiry-target 'delete)
413               (with-temp-buffer
414                 (nnfolder-request-article (car maybe-expirable)
415                                           newsgroup server (current-buffer))
416                 (let ((nnfolder-current-directory nil))
417                   (nnmail-expiry-target-group
418                    nnmail-expiry-target newsgroup)))
419               (nnfolder-possibly-change-group newsgroup server))
420             (nnheader-message 5 "Deleting article %d in %s..."
421                               (car maybe-expirable) newsgroup)
422             (nnfolder-delete-mail)
423             (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
424               (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
425             ;; Must remember which articles were actually deleted
426             (push (car maybe-expirable) deleted-articles)))
427         (setq maybe-expirable (cdr maybe-expirable)))
428       (unless nnfolder-inhibit-expiry
429         (nnheader-message 5 "Deleting articles...done"))
430       (nnfolder-save-buffer)
431       (nnfolder-adjust-min-active newsgroup)
432       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
433       (gnus-sorted-complement articles (nreverse deleted-articles)))))
434
435 (deffoo nnfolder-request-move-article (article group server
436                                                accept-form &optional last)
437   (save-excursion
438     (let ((buf (get-buffer-create " *nnfolder move*"))
439           result)
440       (and
441        (nnfolder-request-article article group server)
442        (save-excursion
443          (set-buffer buf)
444          (erase-buffer)
445          (insert-buffer-substring nntp-server-buffer)
446          (goto-char (point-min))
447          (while (re-search-forward
448                  (concat "^" nnfolder-article-marker)
449                  (save-excursion (and (search-forward "\n\n" nil t) (point)))
450                  t)
451            (delete-region (progn (beginning-of-line) (point))
452                           (progn (forward-line 1) (point))))
453          (setq result (eval accept-form))
454          (kill-buffer buf)
455          result)
456        (save-excursion
457          (nnfolder-possibly-change-group group server)
458          (set-buffer nnfolder-current-buffer)
459          (goto-char (point-min))
460          (when (nnfolder-goto-article article)
461            (nnfolder-delete-mail))
462          (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
463            (nnfolder-nov-delete-article group article))
464          (when last
465            (nnfolder-save-buffer)
466            (nnfolder-adjust-min-active group)
467            (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
468       result)))
469
470 (deffoo nnfolder-request-accept-article (group &optional server last)
471   (save-excursion
472     (nnfolder-possibly-change-group group server)
473     (nnmail-check-syntax)
474     (let ((buf (current-buffer))
475           result art-group)
476       (goto-char (point-min))
477       (when (looking-at "X-From-Line: ")
478         (replace-match "From "))
479       (with-temp-buffer
480         (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
481               (nntp-server-buffer (current-buffer)))
482           (nnmail-find-file nnfolder-active-file)
483           (setq nnfolder-group-alist (nnmail-parse-active))))
484       (save-excursion
485         (goto-char (point-min))
486         (if (search-forward "\n\n" nil t)
487             (forward-line -1)
488           (goto-char (point-max)))
489         (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
490           (delete-region (point) (progn (forward-line 1) (point))))
491         (when nnmail-cache-accepted-message-ids
492           (nnmail-cache-insert (nnmail-fetch-field "message-id")))
493         (setq result (if (stringp group)
494                          (list (cons group (nnfolder-active-number group)))
495                        (setq art-group
496                              (nnmail-article-group 'nnfolder-active-number))))
497         (if (and (null result)
498                  (yes-or-no-p "Moved to `junk' group; delete article? "))
499             (setq result 'junk)
500           (setq result
501                 (car (nnfolder-save-mail result)))))
502       (when last
503         (save-excursion
504           (nnfolder-possibly-change-folder (or (caar art-group) group))
505           (nnfolder-save-buffer)
506           (when nnmail-cache-accepted-message-ids
507             (nnmail-cache-close))))
508       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
509       (unless result
510         (nnheader-report 'nnfolder "Couldn't store article"))
511       result)))
512
513 (deffoo nnfolder-request-replace-article (article group buffer)
514   (nnfolder-possibly-change-group group)
515   (save-excursion
516     (set-buffer buffer)
517     (goto-char (point-min))
518     (let (xfrom)
519       (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
520         (setq xfrom (match-string 1))
521         (gnus-delete-line))
522       (goto-char (point-min))
523       (if xfrom
524           (insert "From " xfrom "\n")
525         (unless (looking-at "From ")
526           (insert "From nobody " (current-time-string) "\n"))))
527     (nnfolder-normalize-buffer)
528     (set-buffer nnfolder-current-buffer)
529     (goto-char (point-min))
530     (if (not (nnfolder-goto-article article))
531         nil
532       (nnfolder-delete-mail)
533       (insert-buffer-substring buffer)
534       (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
535         (save-excursion
536           (set-buffer buffer)
537           (let ((headers (nnfolder-parse-head article
538                                               (point-min) (point-max))))
539             (with-current-buffer (nnfolder-open-nov group)
540               (if (nnheader-find-nov-line article)
541                   (delete-region (point) (progn (forward-line 1) (point))))
542               (nnheader-insert-nov headers)))))
543       (nnfolder-save-buffer)
544       t)))
545
546 (deffoo nnfolder-request-delete-group (group &optional force server)
547   (nnfolder-close-group group server t)
548   ;; Delete all articles in GROUP.
549   (if (not force)
550       ()                                ; Don't delete the articles.
551     ;; Delete the file that holds the group.
552     (ignore-errors
553       (delete-file (nnfolder-group-pathname group))
554       (when (file-exists-p (nnfolder-group-nov-pathname group))
555         (delete-file (nnfolder-group-nov-pathname group)))
556       (when (file-exists-p (nnfolder-group-marks-pathname group))
557         (delete-file (nnfolder-group-marks-pathname group)))))
558   ;; Remove the group from all structures.
559   (setq nnfolder-group-alist
560         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
561         nnfolder-current-group nil
562         nnfolder-current-buffer nil)
563   ;; Save the active file.
564   (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
565   t)
566
567 (deffoo nnfolder-request-rename-group (group new-name &optional server)
568   (nnfolder-possibly-change-group group server)
569   (save-excursion
570     (set-buffer nnfolder-current-buffer)
571     (and (file-writable-p buffer-file-name)
572          (ignore-errors
573            (let ((new-file (nnfolder-group-pathname new-name)))
574              (gnus-make-directory (file-name-directory new-file))
575              (rename-file buffer-file-name new-file)
576              (when (file-exists-p (nnfolder-group-nov-pathname group))
577                (setq new-file (nnfolder-group-nov-pathname new-name))
578                (gnus-make-directory (file-name-directory new-file))
579                (rename-file (nnfolder-group-nov-pathname group) new-file))
580              (when (file-exists-p (nnfolder-group-marks-pathname group))
581                (setq new-file (nnfolder-group-marks-pathname new-name))
582                (gnus-make-directory (file-name-directory new-file))
583                (rename-file (nnfolder-group-marks-pathname group) new-file)))
584            t)
585          ;; That went ok, so we change the internal structures.
586          (let ((entry (assoc group nnfolder-group-alist)))
587            (and entry (setcar entry new-name))
588            (setq nnfolder-current-buffer nil
589                  nnfolder-current-group nil)
590            ;; Save the new group alist.
591            (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
592            ;; We kill the buffer instead of renaming it and stuff.
593            (kill-buffer (current-buffer))
594            t))))
595
596 (deffoo nnfolder-request-regenerate (server)
597   (nnfolder-possibly-change-group nil server)
598   (nnfolder-generate-active-file)
599   t)
600
601 \f
602 ;;; Internal functions.
603
604 (defun nnfolder-adjust-min-active (group)
605   ;; Find the lowest active article in this group.
606   (let* ((active (cadr (assoc group nnfolder-group-alist)))
607          (marker (concat "\n" nnfolder-article-marker))
608          (number "[0-9]+")
609          (activemin (cdr active)))
610     (save-excursion
611       (set-buffer nnfolder-current-buffer)
612       (goto-char (point-min))
613       (while (and (search-forward marker nil t)
614                   (re-search-forward number nil t))
615         (let ((newnum (string-to-number (match-string 0))))
616           (if (nnmail-within-headers-p)
617               (setq activemin (min activemin newnum)))))
618       (setcar active activemin))))
619
620 (defun nnfolder-article-string (article)
621   (if (numberp article)
622       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
623     (concat "\nMessage-ID: " article)))
624
625 (defun nnfolder-goto-article (article)
626   "Place point at the start of the headers of ARTICLE.
627 ARTICLE can be an article number or a Message-ID.
628 Returns t if successful, nil otherwise."
629   (let ((art-string (nnfolder-article-string article))
630         start found)
631     ;; It is likely that we are at or before the delimiter line.
632     ;; We therefore go to the end of the previous line, and start
633     ;; searching from there.
634     (beginning-of-line)
635     (unless (bobp)
636       (forward-char -1))
637     (setq start (point))
638     ;; First search forward.
639     (while (and (setq found (search-forward art-string nil t))
640                 (not (nnmail-within-headers-p))))
641     ;; If unsuccessful, search backward from where we started,
642     (unless found
643       (goto-char start)
644       (while (and (setq found (search-backward art-string nil t))
645                   (not (nnmail-within-headers-p)))))
646     (when found
647       (nnmail-search-unix-mail-delim-backward))))
648
649 (defun nnfolder-delete-mail (&optional leave-delim)
650   "Delete the message that point is in.
651 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
652 deleted.  Point is left where the deleted region was."
653   (save-restriction
654     (narrow-to-region
655      (save-excursion
656        ;; In case point is at the beginning of the message already.
657        (forward-line 1)
658        (nnmail-search-unix-mail-delim-backward)
659        (if leave-delim (progn (forward-line 1) (point))
660          (point)))
661      (progn
662        (forward-line 1)
663        (if (nnmail-search-unix-mail-delim)
664            (point)
665          (point-max))))
666     (run-hooks 'nnfolder-delete-mail-hook)
667     (delete-region (point-min) (point-max))))
668
669 (defun nnfolder-possibly-change-group (group &optional server dont-check)
670   ;; Change servers.
671   (when (and server
672              (not (nnfolder-server-opened server)))
673     (nnfolder-open-server server))
674   (unless (gnus-buffer-live-p nnfolder-current-buffer)
675     (setq nnfolder-current-buffer nil
676           nnfolder-current-group nil))
677   ;; Change group.
678   (let ((file-name-coding-system nnmail-pathname-coding-system)
679         (pathname-coding-system nnmail-pathname-coding-system))
680     (when (and group
681                (not (equal group nnfolder-current-group))
682                (progn
683                  (nnmail-activate 'nnfolder)
684                  (and (assoc group nnfolder-group-alist)
685                       (file-exists-p (nnfolder-group-pathname group)))))
686       (if dont-check
687           (setq nnfolder-current-group group
688                 nnfolder-current-buffer nil)
689         (let (inf file)
690           ;; If we have to change groups, see if we don't already have the
691           ;; folder in memory.  If we do, verify the modtime and destroy
692           ;; the folder if needed so we can rescan it.
693           (setq nnfolder-current-buffer
694                 (nth 1 (assoc group nnfolder-buffer-alist)))
695
696           ;; If the buffer is not live, make sure it isn't in the alist.  If it
697           ;; is live, verify that nobody else has touched the file since last
698           ;; time.
699           (when (and nnfolder-current-buffer
700                      (not (gnus-buffer-live-p nnfolder-current-buffer)))
701             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
702                   nnfolder-current-buffer nil))
703
704           (setq nnfolder-current-group group)
705
706           (when (or (not nnfolder-current-buffer)
707                     (not (verify-visited-file-modtime
708                           nnfolder-current-buffer)))
709             (save-excursion
710               (setq file (nnfolder-group-pathname group))
711               ;; See whether we need to create the new file.
712               (unless (file-exists-p file)
713                 (gnus-make-directory (file-name-directory file))
714                 (let ((nnmail-file-coding-system
715                        (or nnfolder-file-coding-system-for-write
716                            nnfolder-file-coding-system-for-write)))
717                   (nnmail-write-region 1 1 file t 'nomesg)))
718               (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
719                 (set-buffer nnfolder-current-buffer)
720                 (push (list group nnfolder-current-buffer)
721                       nnfolder-buffer-alist)))))))))
722
723 (defun nnfolder-save-mail (group-art-list)
724   "Called narrowed to an article."
725   (let* (save-list group-art)
726     (goto-char (point-min))
727     ;; The From line may have been quoted by movemail.
728     (when (looking-at ">From")
729       (delete-char 1))
730     ;; This might come from somewhere else.
731     (unless (looking-at "From ")
732       (insert "From nobody " (current-time-string) "\n")
733       (goto-char (point-min)))
734     ;; Quote all "From " lines in the article.
735     (forward-line 1)
736     (let (case-fold-search)
737       (while (re-search-forward "^From " nil t)
738         (beginning-of-line)
739         (insert "> ")))
740     (setq save-list group-art-list)
741     (nnmail-insert-lines)
742     (nnmail-insert-xref group-art-list)
743     (run-hooks 'nnmail-prepare-save-mail-hook)
744     (run-hooks 'nnfolder-prepare-save-mail-hook)
745
746     ;; Insert the mail into each of the destination groups.
747     (while (setq group-art (pop group-art-list))
748       ;; Kill any previous newsgroup markers.
749       (goto-char (point-min))
750       (if (search-forward "\n\n" nil t)
751           (forward-line -1)
752         (goto-char (point-max)))
753       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
754         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
755
756       ;; Insert the new newsgroup marker.
757       (nnfolder-insert-newsgroup-line group-art)
758
759       (save-excursion
760         (let ((beg (point-min))
761               (end (point-max))
762               (obuf (current-buffer)))
763           (nnfolder-possibly-change-folder (car group-art))
764           (let ((buffer-read-only nil))
765             (nnfolder-normalize-buffer)
766             (insert-buffer-substring obuf beg end))
767           (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
768             (set-buffer obuf)
769             (nnfolder-add-nov (car group-art) (cdr group-art)
770                               (nnfolder-parse-head nil beg end))))))
771
772     ;; Did we save it anywhere?
773     save-list))
774
775 (defun nnfolder-normalize-buffer ()
776   "Make sure there are two newlines at the end of the buffer."
777   (goto-char (point-max))
778   (skip-chars-backward "\n")
779   (delete-region (point) (point-max))
780   (unless (bobp)
781     (insert "\n\n")))
782
783 (defun nnfolder-insert-newsgroup-line (group-art)
784   (save-excursion
785     (goto-char (point-min))
786     (unless (search-forward "\n\n" nil t)
787       (goto-char (point-max))
788       (insert "\n"))
789     (forward-char -1)
790     (insert (format (concat nnfolder-article-marker "%d   %s\n")
791                     (cdr group-art) (current-time-string)))))
792
793 (defun nnfolder-active-number (group)
794   ;; Find the next article number in GROUP.
795   (let ((active (cadr (assoc group nnfolder-group-alist))))
796     (if active
797         (setcdr active (1+ (cdr active)))
798       ;; This group is new, so we create a new entry for it.
799       ;; This might be a bit naughty... creating groups on the drop of
800       ;; a hat, but I don't know...
801       (push (list group (setq active (cons 1 1)))
802             nnfolder-group-alist))
803     (cdr active)))
804
805 (defun nnfolder-possibly-change-folder (group)
806   (let ((inf (assoc group nnfolder-buffer-alist)))
807     (if (and inf
808              (gnus-buffer-live-p (cadr inf)))
809         (set-buffer (cadr inf))
810       (when inf
811         (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
812       (when nnfolder-group-alist
813         (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
814       (push (list group (nnfolder-read-folder group))
815             nnfolder-buffer-alist))))
816
817 ;; This method has a problem if you've accidentally let the active
818 ;; list get out of sync with the files.  This could happen, say, if
819 ;; you've accidentally gotten new mail with something other than Gnus
820 ;; (but why would _that_ ever happen? :-).  In that case, we will be
821 ;; in the middle of processing the file, ready to add new X-Gnus
822 ;; article number markers, and we'll run across a message with no ID
823 ;; yet - the active list _may_not_ be ready for us yet.
824
825 ;; To handle this, I'm modifying this routine to maintain the maximum
826 ;; ID seen so far, and when we hit a message with no ID, we will
827 ;; _manually_ scan the rest of the message looking for any more,
828 ;; possibly higher IDs.  We'll assume the maximum that we find is the
829 ;; highest active.  Note that this shouldn't cost us much extra time
830 ;; at all, but will be a lot less vulnerable to glitches between the
831 ;; mbox and the active file.
832
833 (defun nnfolder-read-folder (group)
834   (let* ((file (nnfolder-group-pathname group))
835          (nov  (nnfolder-group-nov-pathname group))
836          (buffer (set-buffer
837                   (let ((nnheader-file-coding-system
838                          nnfolder-file-coding-system))
839                     (nnheader-find-file-noselect file)))))
840     (if (equal (cadr (assoc group nnfolder-scantime-alist))
841                (nth 5 (file-attributes file)))
842         ;; This looks up-to-date, so we don't do any scanning.
843         (if (file-exists-p file)
844             buffer
845           (push (list group buffer) nnfolder-buffer-alist)
846           (set-buffer-modified-p t)
847           (nnfolder-save-buffer))
848       ;; Parse the damn thing.
849       (save-excursion
850         (goto-char (point-min))
851         ;; Remove any blank lines at the start.
852         (while (eq (following-char) ?\n)
853           (delete-char 1))
854         (nnmail-activate 'nnfolder)
855         ;; Read in the file.
856         (let ((delim "^From ")
857               (marker (concat "\n" nnfolder-article-marker))
858               (number "[0-9]+")
859               (active (or (cadr (assoc group nnfolder-group-alist))
860                           (cons 1 0)))
861               (scantime (assoc group nnfolder-scantime-alist))
862               (minid (lsh -1 -1))
863               maxid start end newscantime
864               novbuf articles newnum
865               buffer-read-only)
866           (buffer-disable-undo)
867           (setq maxid (cdr active))
868
869           (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
870                       (and (file-exists-p nov)
871                            (file-newer-than-file-p nov file)))
872             (unless (file-exists-p nov)
873               (gnus-make-directory (file-name-directory nov)))
874             (with-current-buffer
875                 (setq novbuf (nnfolder-open-nov group))
876               (goto-char (point-min))
877               (while (not (eobp))
878                 (push (read novbuf) articles)
879                 (forward-line 1))
880               (setq articles (nreverse articles))))
881           (goto-char (point-min))
882
883           ;; Anytime the active number is 1 or 0, it is suspect.  In that
884           ;; case, search the file manually to find the active number.  Or,
885           ;; of course, if we're being paranoid.  (This would also be the
886           ;; place to build other lists from the header markers, such as
887           ;; expunge lists, etc., if we ever desired to abandon the active
888           ;; file entirely for mboxes.)
889           (when (or nnfolder-ignore-active-file
890                     novbuf
891                     (< maxid 2))
892             (while (and (search-forward marker nil t)
893                         (looking-at number))
894               (setq newnum (string-to-number (match-string 0)))
895               (when (nnmail-within-headers-p)
896                 (setq maxid (max maxid newnum)
897                       minid (min minid newnum))
898                 (when novbuf
899                   (if (memq newnum articles)
900                       (setq articles (delq newnum articles))
901                     (let ((headers (nnfolder-parse-head newnum)))
902                       (with-current-buffer novbuf
903                         (nnheader-find-nov-line newnum)
904                         (nnheader-insert-nov headers)))))))
905             (when (and novbuf articles)
906               (with-current-buffer novbuf
907                 (dolist (article articles)
908                   (when (nnheader-find-nov-line article)
909                     (delete-region (point)
910                                    (progn (forward-line 1) (point)))))))
911             (setcar active (max 1 (min minid maxid)))
912             (setcdr active (max maxid (cdr active)))
913             (goto-char (point-min)))
914
915           ;; As long as we trust that the user will only insert unmarked mail
916           ;; at the end, go to the end and search backwards for the last
917           ;; marker.  Find the start of that message, and begin to search for
918           ;; unmarked messages from there.
919           (when (not (or nnfolder-distrust-mbox
920                          (< maxid 2)))
921             (goto-char (point-max))
922             (unless (re-search-backward marker nil t)
923               (goto-char (point-min)))
924             ;;(when (nnmail-search-unix-mail-delim)
925             ;;  (goto-char (point-min)))
926             )
927
928           ;; Keep track of the active number on our own, and insert it back
929           ;; into the active list when we're done.  Also, prime the pump to
930           ;; cut down on the number of searches we do.
931           (unless (nnmail-search-unix-mail-delim)
932             (goto-char (point-max)))
933           (setq end (point-marker))
934           (while (not (= end (point-max)))
935             (setq start (marker-position end))
936             (goto-char end)
937             ;; There may be more than one "From " line, so we skip past
938             ;; them.
939             (while (looking-at delim)
940               (forward-line 1))
941             (set-marker end (if (nnmail-search-unix-mail-delim)
942                                 (point)
943                               (point-max)))
944             (goto-char start)
945             (when (not (search-forward marker end t))
946               (narrow-to-region start end)
947               (nnmail-insert-lines)
948               (nnfolder-insert-newsgroup-line
949                (cons nil
950                      (setq newnum
951                            (nnfolder-active-number group))))
952               (when novbuf
953                 (let ((headers (nnfolder-parse-head newnum (point-min)
954                                                     (point-max))))
955                   (with-current-buffer novbuf
956                     (goto-char (point-max))
957                     (nnheader-insert-nov headers))))
958               (widen)))
959
960           (set-marker end nil)
961           ;; Make absolutely sure that the active list reflects reality!
962           (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
963
964           ;; Set the scantime for this group.
965           (setq newscantime (visited-file-modtime))
966           (if scantime
967               (setcdr scantime (list newscantime))
968             (push (list group newscantime)
969                   nnfolder-scantime-alist))
970           ;; Save nov.
971           (when novbuf
972             (nnfolder-save-nov))
973           (current-buffer))))))
974
975 ;;;###autoload
976 (defun nnfolder-generate-active-file ()
977   "Look for mbox folders in the nnfolder directory and make them into groups.
978 This command does not work if you use short group names."
979   (interactive)
980   (nnmail-activate 'nnfolder)
981   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
982     (dolist (file (directory-files (or nnfolder-nov-directory
983                                        nnfolder-directory)
984                                    t
985                                    (concat
986                                     (regexp-quote nnfolder-nov-file-suffix)
987                                     "$")))
988       (when (not (message-mail-file-mbox-p file))
989         (ignore-errors
990           (delete-file file)))))
991   (let ((files (directory-files nnfolder-directory))
992         file)
993     (while (setq file (pop files))
994       (when (and (not (backup-file-name-p file))
995                  (message-mail-file-mbox-p
996                   (nnheader-concat nnfolder-directory file)))
997         (let ((oldgroup (assoc file nnfolder-group-alist)))
998           (if oldgroup
999               (nnheader-message 5 "Refreshing group %s..." file)
1000             (nnheader-message 5 "Adding group %s..." file))
1001           (if oldgroup
1002               (setq nnfolder-group-alist
1003                     (delq oldgroup (copy-sequence nnfolder-group-alist))))
1004           (push (list file (cons 1 0)) nnfolder-group-alist)
1005           (nnfolder-possibly-change-folder file)
1006           (nnfolder-possibly-change-group file)
1007           (nnfolder-close-group file))))
1008     (nnheader-message 5 "")))
1009
1010 (defun nnfolder-group-pathname (group)
1011   "Make pathname for GROUP."
1012   (setq group
1013         (encode-coding-string group nnmail-pathname-coding-system))
1014   (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
1015     ;; If this file exists, we use it directly.
1016     (if (or nnmail-use-long-file-names
1017             (file-exists-p (concat dir group)))
1018         (concat dir group)
1019       ;; If not, we translate dots into slashes.
1020       (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
1021
1022 (defun nnfolder-group-nov-pathname (group)
1023   "Make pathname for GROUP NOV."
1024   (let ((nnfolder-directory
1025          (or nnfolder-nov-directory nnfolder-directory)))
1026     (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
1027
1028 (defun nnfolder-save-buffer ()
1029   "Save the buffer."
1030   (when (buffer-modified-p)
1031     (run-hooks 'nnfolder-save-buffer-hook)
1032     (gnus-make-directory (file-name-directory (buffer-file-name)))
1033     (let* ((coding-system-for-write
1034             (or nnfolder-file-coding-system-for-write
1035                 nnfolder-file-coding-system))
1036            (output-coding-system coding-system-for-write))
1037       (save-buffer)))
1038   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1039     (nnfolder-save-nov)))
1040
1041 (defun nnfolder-save-active (group-alist active-file)
1042   (let ((nnmail-active-file-coding-system
1043          (or nnfolder-active-file-coding-system-for-write
1044              nnfolder-active-file-coding-system)))
1045     (nnmail-save-active group-alist active-file)))
1046
1047 (defun nnfolder-open-nov (group)
1048   (or (cdr (assoc group nnfolder-nov-buffer-alist))
1049       (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1050         (save-excursion
1051           (set-buffer buffer)
1052           (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1053                (nnfolder-group-nov-pathname group))
1054           (erase-buffer)
1055           (when (file-exists-p nnfolder-nov-buffer-file-name)
1056             (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1057         (push (cons group buffer) nnfolder-nov-buffer-alist)
1058         buffer)))
1059
1060 (defun nnfolder-save-nov ()
1061   (save-excursion
1062     (while nnfolder-nov-buffer-alist
1063       (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1064         (set-buffer (cdar nnfolder-nov-buffer-alist))
1065         (when (buffer-modified-p)
1066           (gnus-make-directory (file-name-directory
1067                                 nnfolder-nov-buffer-file-name))
1068           (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1069                                nil 'nomesg))
1070         (set-buffer-modified-p nil)
1071         (kill-buffer (current-buffer)))
1072       (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1073
1074 (defun nnfolder-nov-delete-article (group article)
1075   (save-excursion
1076     (set-buffer (nnfolder-open-nov group))
1077     (when (nnheader-find-nov-line article)
1078       (delete-region (point) (progn (forward-line 1) (point))))
1079     t))
1080
1081 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1082   (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1083       nil
1084     (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1085       (when (file-exists-p nov)
1086         (save-excursion
1087           (set-buffer nntp-server-buffer)
1088           (erase-buffer)
1089           (nnheader-insert-file-contents nov)
1090           (if (and fetch-old
1091                    (not (numberp fetch-old)))
1092               t                         ; Don't remove anything.
1093             (nnheader-nov-delete-outside-range
1094              (if fetch-old (max 1 (- (car articles) fetch-old))
1095                (car articles))
1096              (car (last articles)))
1097             t))))))
1098
1099 (defun nnfolder-parse-head (&optional number b e)
1100   "Parse the head of the current buffer."
1101   (let ((buf (current-buffer))
1102         chars)
1103     (save-excursion
1104       (unless b
1105         (setq b (if (nnmail-search-unix-mail-delim-backward)
1106                     (point) (point-min)))
1107         (forward-line 1)
1108         (setq e (if (nnmail-search-unix-mail-delim)
1109                     (point) (point-max))))
1110       (setq chars (- e b))
1111       (unless (zerop chars)
1112         (goto-char b)
1113         (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1114       (with-temp-buffer
1115         (insert-buffer-substring buf b e)
1116         ;; Fold continuation lines.
1117         (goto-char (point-min))
1118         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1119           (replace-match " " t t))
1120         ;; Remove any tabs; they are too confusing.
1121         (subst-char-in-region (point-min) (point-max) ?\t ? )
1122         (let ((headers (nnheader-parse-head t)))
1123           (mail-header-set-chars headers chars)
1124           (mail-header-set-number headers number)
1125           headers)))))
1126
1127 (defun nnfolder-add-nov (group article headers)
1128   "Add a nov line for the GROUP base."
1129   (save-excursion
1130     (set-buffer (nnfolder-open-nov group))
1131     (goto-char (point-max))
1132     (mail-header-set-number headers article)
1133     (nnheader-insert-nov headers)))
1134
1135 (deffoo nnfolder-request-set-mark (group actions &optional server)
1136   (when (and server
1137              (not (nnfolder-server-opened server)))
1138     (nnfolder-open-server server))
1139   (unless nnfolder-marks-is-evil
1140     (nnfolder-open-marks group server)
1141     (dolist (action actions)
1142       (let ((range (nth 0 action))
1143             (what  (nth 1 action))
1144             (marks (nth 2 action)))
1145         (assert (or (eq what 'add) (eq what 'del)) t
1146                 "Unknown request-set-mark action: %s" what)
1147         (dolist (mark marks)
1148           (setq nnfolder-marks (gnus-update-alist-soft
1149                             mark
1150                             (funcall (if (eq what 'add) 'gnus-range-add
1151                                        'gnus-remove-from-range)
1152                                      (cdr (assoc mark nnfolder-marks)) range)
1153                             nnfolder-marks)))))
1154     (nnfolder-save-marks group server))
1155   nil)
1156
1157 (deffoo nnfolder-request-update-info (group info &optional server)
1158   ;; Change servers.
1159   (when (and server
1160              (not (nnfolder-server-opened server)))
1161     (nnfolder-open-server server))
1162   (when (and (not nnfolder-marks-is-evil) (nnfolder-marks-changed-p group))
1163     (nnheader-message 8 "Updating marks for %s..." group)
1164     (nnfolder-open-marks group server)
1165     ;; Update info using `nnfolder-marks'.
1166     (mapcar (lambda (pred)
1167               (gnus-info-set-marks
1168                info
1169                (gnus-update-alist-soft
1170                 (cdr pred)
1171                 (cdr (assq (cdr pred) nnfolder-marks))
1172                 (gnus-info-marks info))
1173                t))
1174             gnus-article-mark-lists)
1175     (let ((seen (cdr (assq 'read nnfolder-marks))))
1176       (gnus-info-set-read info
1177                           (if (and (integerp (car seen))
1178                                    (null (cdr seen)))
1179                               (list (cons (car seen) (car seen)))
1180                             seen)))
1181     (nnheader-message 8 "Updating marks for %s...done" group))
1182   info)
1183
1184 (defun nnfolder-group-marks-pathname (group)
1185   "Make pathname for GROUP NOV."
1186   (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory)))
1187     (concat (nnfolder-group-pathname group) nnfolder-marks-file-suffix)))
1188
1189 (defun nnfolder-marks-changed-p (group)
1190   (let ((file (nnfolder-group-marks-pathname group)))
1191     (if (null (gnus-gethash file nnfolder-marks-modtime))
1192         t ;; never looked at marks file, assume it has changed
1193       (not (equal (gnus-gethash file nnfolder-marks-modtime)
1194                   (nth 5 (file-attributes file)))))))
1195
1196 (defun nnfolder-save-marks (group server)
1197   (let ((file-name-coding-system nnmail-pathname-coding-system)
1198         (file (nnfolder-group-marks-pathname group)))
1199     (condition-case err
1200         (progn
1201           (with-temp-file file
1202             (erase-buffer)
1203             (gnus-prin1 nnfolder-marks)
1204             (insert "\n"))
1205           (gnus-sethash file
1206                         (nth 5 (file-attributes file))
1207                         nnfolder-marks-modtime))
1208       (error (or (gnus-yes-or-no-p
1209                   (format "Could not write to %s (%s).  Continue? " file err))
1210                  (error "Cannot write to %s (%s)" err))))))
1211
1212 (defun nnfolder-open-marks (group server)
1213   (let ((file (nnfolder-group-marks-pathname group)))
1214     (if (file-exists-p file)
1215         (condition-case err
1216             (with-temp-buffer
1217               (gnus-sethash file (nth 5 (file-attributes file))
1218                             nnfolder-marks-modtime)
1219               (nnheader-insert-file-contents file)
1220               (setq nnfolder-marks (read (current-buffer)))
1221               (dolist (el gnus-article-unpropagated-mark-lists)
1222                 (setq nnfolder-marks (gnus-remassoc el nnfolder-marks))))
1223           (error (or (gnus-yes-or-no-p
1224                       (format "Error reading nnfolder marks file %s (%s).  Continuing will use marks from .newsrc.eld.  Continue? " file err))
1225                      (error "Cannot read nnfolder marks file %s (%s)" file err))))
1226       ;; User didn't have a .marks file.  Probably first time
1227       ;; user of the .marks stuff.  Bootstrap it from .newsrc.eld.
1228       (let ((info (gnus-get-info
1229                    (gnus-group-prefixed-name
1230                     group
1231                     (gnus-server-to-method (format "nnfolder:%s" server))))))
1232         (nnheader-message 7 "Bootstrapping marks for %s..." group)
1233         (setq nnfolder-marks (gnus-info-marks info))
1234         (push (cons 'read (gnus-info-read info)) nnfolder-marks)
1235         (dolist (el gnus-article-unpropagated-mark-lists)
1236           (setq nnfolder-marks (gnus-remassoc el nnfolder-marks)))
1237         (nnfolder-save-marks group server)))))
1238
1239 (provide 'nnfolder)
1240
1241 ;;; nnfolder.el ends here