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