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