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