7edef4ead32823eef6a5eff485265d07719fa8ba
[gnus] / lisp / nnfolder.el
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Simon Josefsson <simon@josefsson.org> (adding MARKS)
6 ;;      ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
7 ;;      Scott Byer <byer@mv.us.adobe.com>
8 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; Keywords: mail
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
26 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
28
29 ;;; Commentary:
30
31 ;;; Code:
32
33 (require 'nnheader)
34 (require 'message)
35 (require 'nnmail)
36 (require 'nnoo)
37 (eval-when-compile (require 'cl))
38 (require 'gnus)
39 (require 'gnus-util)
40 (require 'gnus-range)
41
42 (eval-and-compile
43   (autoload 'gnus-intersection "gnus-range"))
44
45 (nnoo-declare nnfolder)
46
47 (defvoo nnfolder-directory (expand-file-name message-directory)
48   "The name of the nnfolder directory.
49
50 This variable is a virtual server slot.  See the Gnus manual for details.")
51
52 (defvoo nnfolder-nov-directory nil
53   "The name of the nnfolder NOV directory.
54 If nil, `nnfolder-directory' is used.")
55
56 (defvoo nnfolder-marks-directory nil
57   "The name of the nnfolder MARKS directory.
58 If nil, `nnfolder-directory' is used.")
59
60 (defvoo nnfolder-active-file
61     (nnheader-concat nnfolder-directory "active")
62   "The name of the active file.
63
64 This variable is a virtual server slot.  See the Gnus manual for details.")
65
66 ;; I renamed this variable to something more in keeping with the general GNU
67 ;; style. -SLB
68
69 (defvoo nnfolder-ignore-active-file nil
70   "If non-nil, the active file is ignored.
71 This causes nnfolder to do some extra work in order to determine the
72 true active ranges of an mbox file.  Note that the active file is
73 still saved, but its values are not used.  This costs some extra time
74 when scanning an mbox when opening it.")
75
76 (defvoo nnfolder-distrust-mbox nil
77   "If non-nil, the folder will be distrusted.
78 This means that nnfolder will not trust the user with respect to
79 inserting unaccounted for mail in the middle of an mbox file.  This
80 can greatly slow down scans, which now must scan the entire file for
81 unmarked messages.  When nil, scans occur forward from the last marked
82 message, a huge time saver for large mailboxes.")
83
84 (defvoo nnfolder-newsgroups-file
85     (concat (file-name-as-directory nnfolder-directory) "newsgroups")
86   "Mail newsgroups description file.
87
88 This variable is a virtual server slot.  See the Gnus manual for details.")
89
90 (defvoo nnfolder-get-new-mail t
91   "If non-nil, nnfolder will check the incoming mail file and split the mail.
92
93 This variable is a virtual server slot.  See the Gnus manual for details.")
94
95 (defvoo nnfolder-prepare-save-mail-hook nil
96   "Hook run narrowed to an article before saving.")
97
98 (defvoo nnfolder-save-buffer-hook nil
99   "Hook run before saving the nnfolder mbox buffer.
100
101 This variable is a virtual server slot.  See the Gnus manual for details.")
102
103
104 (defvoo nnfolder-inhibit-expiry nil
105   "If non-nil, inhibit expiry.
106
107 This variable is a virtual server slot.  See the Gnus manual for details.")
108
109 \f
110
111 (defconst nnfolder-version "nnfolder 2.0"
112   "nnfolder version.")
113
114 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
115   "String used to demarcate what the article number for a message is.")
116
117 (defvoo nnfolder-current-group nil)
118 (defvoo nnfolder-current-buffer nil)
119 (defvoo nnfolder-status-string "")
120 (defvoo nnfolder-group-alist nil)
121 (defvoo nnfolder-buffer-alist nil)
122 (defvoo nnfolder-scantime-alist nil)
123 (defvoo nnfolder-active-timestamp nil)
124 (defvoo nnfolder-active-file-coding-system mm-text-coding-system)
125 (defvoo nnfolder-active-file-coding-system-for-write
126     nnmail-active-file-coding-system)
127 (defvoo nnfolder-file-coding-system mm-text-coding-system)
128 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
129   "Coding system for save nnfolder file.
130 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
131
132 (defvoo nnfolder-nov-is-evil nil
133   "If non-nil, Gnus will never generate and use nov databases for mail groups.
134 Using nov databases will speed up header fetching considerably.
135 This variable shouldn't be flipped much.  If you have, for some reason,
136 set this to t, and want to set it to nil again, you should always run
137 the `nnfolder-generate-active-file' command.  The function will go
138 through all nnfolder directories and generate nov databases for them
139 all.  This may very well take some time.
140
141 This variable is a virtual server slot.  See the Gnus manual for details.")
142
143 (defvoo nnfolder-nov-file-suffix ".nov")
144
145 (defvoo nnfolder-nov-buffer-alist nil)
146
147 (defvar nnfolder-nov-buffer-file-name nil)
148
149 (defvoo nnfolder-marks-is-evil nil
150   "If non-nil, Gnus will never generate and use marks file for mail groups.
151 Using marks files makes it possible to backup and restore mail groups
152 separately from `.newsrc.eld'.  If you have, for some reason, set
153 this to t, and want to set it to nil again, you should always remove
154 the corresponding marks file (usually base nnfolder file name
155 concatenated with `.mrk', but see `nnfolder-marks-file-suffix') for
156 the group.  Then the marks file will be regenerated properly by Gnus.
157
158 This variable is a virtual server slot.  See the Gnus manual for details.")
159
160 (defvoo nnfolder-marks nil)
161
162 (defvoo nnfolder-marks-file-suffix ".mrk")
163
164 (defvar nnfolder-marks-modtime (makehash))
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     (when (and group
692                (not (equal group nnfolder-current-group))
693                (progn
694                  (nnmail-activate 'nnfolder)
695                  (and (assoc group nnfolder-group-alist)
696                       (file-exists-p (nnfolder-group-pathname group)))))
697       (if dont-check
698           (setq nnfolder-current-group group
699                 nnfolder-current-buffer nil)
700         (let (inf file)
701           ;; If we have to change groups, see if we don't already have
702           ;; the folder in memory.  If we do, verify the modtime and
703           ;; destroy the folder if needed so we can rescan it.
704           (setq nnfolder-current-buffer
705                 (nth 1 (assoc group nnfolder-buffer-alist)))
706
707           ;; If the buffer is not live, make sure it isn't in the
708           ;; alist.  If it is live, verify that nobody else has
709           ;; touched the file since last time.
710           (when (and nnfolder-current-buffer
711                      (not (gnus-buffer-live-p nnfolder-current-buffer)))
712             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
713                   nnfolder-current-buffer nil))
714
715           (setq nnfolder-current-group group)
716
717           (when (or (not nnfolder-current-buffer)
718                     (not (verify-visited-file-modtime
719                           nnfolder-current-buffer)))
720             (save-excursion
721               (setq file (nnfolder-group-pathname group))
722               ;; See whether we need to create the new file.
723               (unless (file-exists-p file)
724                 (gnus-make-directory (file-name-directory file))
725                 (let ((nnmail-file-coding-system
726                        (or nnfolder-file-coding-system-for-write
727                            nnfolder-file-coding-system-for-write)))
728                   (nnmail-write-region 1 1 file t 'nomesg)))
729               (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
730                 (set-buffer nnfolder-current-buffer)
731                 (push (list group nnfolder-current-buffer)
732                       nnfolder-buffer-alist)))))))))
733
734 (defun nnfolder-save-mail (group-art-list)
735   "Called narrowed to an article."
736   (let* (save-list group-art)
737     (goto-char (point-min))
738     ;; The From line may have been quoted by movemail.
739     (when (looking-at ">From")
740       (delete-char 1))
741     ;; This might come from somewhere else.
742     (unless (looking-at "From ")
743       (insert "From nobody " (current-time-string) "\n")
744       (goto-char (point-min)))
745     ;; Quote all "From " lines in the article.
746     (forward-line 1)
747     (let (case-fold-search)
748       (while (re-search-forward "^From " nil t)
749         (beginning-of-line)
750         (insert "> ")))
751     (setq save-list group-art-list)
752     (nnmail-insert-lines)
753     (nnmail-insert-xref group-art-list)
754     (run-hooks 'nnmail-prepare-save-mail-hook)
755     (run-hooks 'nnfolder-prepare-save-mail-hook)
756
757     ;; Insert the mail into each of the destination groups.
758     (while (setq group-art (pop group-art-list))
759       ;; Kill any previous newsgroup markers.
760       (goto-char (point-min))
761       (if (search-forward "\n\n" nil t)
762           (forward-line -1)
763         (goto-char (point-max)))
764       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
765         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
766
767       ;; Insert the new newsgroup marker.
768       (nnfolder-insert-newsgroup-line group-art)
769
770       (save-excursion
771         (let ((beg (point-min))
772               (end (point-max))
773               (obuf (current-buffer)))
774           (nnfolder-possibly-change-folder (car group-art))
775           (let ((buffer-read-only nil))
776             (nnfolder-normalize-buffer)
777             (insert-buffer-substring obuf beg end))
778           (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
779             (set-buffer obuf)
780             (nnfolder-add-nov (car group-art) (cdr group-art)
781                               (nnfolder-parse-head nil beg end))))))
782
783     ;; Did we save it anywhere?
784     save-list))
785
786 (defun nnfolder-normalize-buffer ()
787   "Make sure there are two newlines at the end of the buffer."
788   (goto-char (point-max))
789   (skip-chars-backward "\n")
790   (delete-region (point) (point-max))
791   (insert "\n\n"))
792
793 (defun nnfolder-insert-newsgroup-line (group-art)
794   (save-excursion
795     (goto-char (point-min))
796     (unless (search-forward "\n\n" nil t)
797       (goto-char (point-max))
798       (insert "\n"))
799     (forward-char -1)
800     (insert (format (concat nnfolder-article-marker "%d   %s\n")
801                     (cdr group-art) (current-time-string)))))
802
803 (defun nnfolder-active-number (group)
804   ;; Find the next article number in GROUP.
805   (let ((active (cadr (assoc group nnfolder-group-alist))))
806     (if active
807         (setcdr active (1+ (cdr active)))
808       ;; This group is new, so we create a new entry for it.
809       ;; This might be a bit naughty... creating groups on the drop of
810       ;; a hat, but I don't know...
811       (push (list group (setq active (cons 1 1)))
812             nnfolder-group-alist))
813     (cdr active)))
814
815 (defun nnfolder-possibly-change-folder (group)
816   (let ((inf (assoc group nnfolder-buffer-alist)))
817     (if (and inf
818              (gnus-buffer-live-p (cadr inf)))
819         (set-buffer (cadr inf))
820       (when inf
821         (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
822       (when nnfolder-group-alist
823         (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
824       (push (list group (nnfolder-read-folder group))
825             nnfolder-buffer-alist))))
826
827 ;; This method has a problem if you've accidentally let the active
828 ;; list get out of sync with the files.  This could happen, say, if
829 ;; you've accidentally gotten new mail with something other than Gnus
830 ;; (but why would _that_ ever happen? :-).  In that case, we will be
831 ;; in the middle of processing the file, ready to add new X-Gnus
832 ;; article number markers, and we'll run across a message with no ID
833 ;; yet - the active list _may_not_ be ready for us yet.
834
835 ;; To handle this, I'm modifying this routine to maintain the maximum
836 ;; ID seen so far, and when we hit a message with no ID, we will
837 ;; _manually_ scan the rest of the message looking for any more,
838 ;; possibly higher IDs.  We'll assume the maximum that we find is the
839 ;; highest active.  Note that this shouldn't cost us much extra time
840 ;; at all, but will be a lot less vulnerable to glitches between the
841 ;; mbox and the active file.
842
843 (defun nnfolder-read-folder (group)
844   (let* ((file (nnfolder-group-pathname group))
845          (nov  (nnfolder-group-nov-pathname group))
846          (buffer (set-buffer
847                   (let ((nnheader-file-coding-system
848                          nnfolder-file-coding-system))
849                     (nnheader-find-file-noselect file)))))
850     (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
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
895           ;; that case, search the file manually to find the active
896           ;; number.  Or, of course, if we're being paranoid.  (This
897           ;; would also be the place to build other lists from the
898           ;; header markers, such as expunge lists, etc., if we ever
899           ;; desired to abandon the active 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
927           ;; unmarked mail at the end, go to the end and search
928           ;; backwards for the last marker.  Find the start of that
929           ;; message, and begin to search for unmarked messages from
930           ;; there.
931           (when (not (or nnfolder-distrust-mbox
932                          (< maxid 2)))
933             (goto-char (point-max))
934             (unless (re-search-backward marker nil t)
935               (goto-char (point-min)))
936             ;;(when (nnmail-search-unix-mail-delim)
937             ;;  (goto-char (point-min)))
938             )
939
940           ;; Keep track of the active number on our own, and insert it
941           ;; back into the active list when we're done.  Also, prime
942           ;; the pump to cut down on the number of searches we do.
943           (unless (nnmail-search-unix-mail-delim)
944             (goto-char (point-max)))
945           (setq end (point-marker))
946           (while (not (= end (point-max)))
947             (setq start (marker-position end))
948             (goto-char end)
949            ;; There may be more than one "From " line, so we skip past
950             ;; them.
951             (while (looking-at delim)
952               (forward-line 1))
953             (set-marker end (if (nnmail-search-unix-mail-delim)
954                                 (point)
955                               (point-max)))
956             (goto-char start)
957             (when (not (search-forward marker end t))
958               (narrow-to-region start end)
959               (nnmail-insert-lines)
960               (nnfolder-insert-newsgroup-line
961                (cons nil
962                      (setq newnum
963                            (nnfolder-active-number nnfolder-current-group))))
964               (when novbuf
965                 (let ((headers (nnfolder-parse-head newnum (point-min)
966                                                     (point-max))))
967                   (with-current-buffer novbuf
968                     (goto-char (point-max))
969                     (nnheader-insert-nov headers))))
970               (widen)))
971
972           (set-marker end nil)
973           ;; Make absolutely sure that the active list reflects
974           ;; reality!
975           (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
976
977           ;; Set the scantime for this group.
978           (setq newscantime (visited-file-modtime))
979           (if scantime
980               (setcdr scantime (list newscantime))
981             (push (list nnfolder-current-group newscantime)
982                   nnfolder-scantime-alist))
983           ;; Save nov.
984           (when novbuf
985             (nnfolder-save-nov))
986           (current-buffer))))))
987
988 ;;;###autoload
989 (defun nnfolder-generate-active-file ()
990   "Look for mbox folders in the nnfolder directory and make them into groups.
991 This command does not work if you use short group names."
992   (interactive)
993   (nnmail-activate 'nnfolder)
994   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
995     (dolist (file (directory-files (or nnfolder-nov-directory
996                                        nnfolder-directory)
997                                    t
998                                    (concat
999                                     (regexp-quote nnfolder-nov-file-suffix)
1000                                     "$")))
1001       (when (not (message-mail-file-mbox-p file))
1002         (ignore-errors
1003           (delete-file file)))))
1004   (let ((files (directory-files nnfolder-directory))
1005         file)
1006     (while (setq file (pop files))
1007       (when (and (not (backup-file-name-p file))
1008                  (message-mail-file-mbox-p
1009                   (nnheader-concat nnfolder-directory file)))
1010         (let ((oldgroup (assoc file nnfolder-group-alist)))
1011           (if oldgroup
1012               (nnheader-message 5 "Refreshing group %s..." file)
1013             (nnheader-message 5 "Adding group %s..." file))
1014           (if oldgroup
1015               (setq nnfolder-group-alist
1016                     (delq oldgroup (copy-sequence nnfolder-group-alist))))
1017           (push (list file (cons 1 0)) nnfolder-group-alist)
1018           (nnfolder-possibly-change-folder file)
1019           (nnfolder-possibly-change-group file)
1020           (nnfolder-close-group file))))
1021     (nnheader-message 5 "")))
1022
1023 (defun nnfolder-group-pathname (group)
1024   "Make pathname for GROUP."
1025   (setq group
1026         (mm-encode-coding-string group nnmail-pathname-coding-system))
1027   (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
1028     ;; If this file exists, we use it directly.
1029     (if (or nnmail-use-long-file-names
1030             (file-exists-p (concat dir group)))
1031         (concat dir group)
1032       ;; If not, we translate dots into slashes.
1033       (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
1034
1035 (defun nnfolder-group-nov-pathname (group)
1036   "Make pathname for GROUP NOV."
1037   (let ((nnfolder-directory
1038          (or nnfolder-nov-directory nnfolder-directory)))
1039     (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
1040
1041 (defun nnfolder-save-buffer ()
1042   "Save the buffer."
1043   (when (buffer-modified-p)
1044     (run-hooks 'nnfolder-save-buffer-hook)
1045     (gnus-make-directory (file-name-directory (buffer-file-name)))
1046     (let ((coding-system-for-write
1047            (or nnfolder-file-coding-system-for-write
1048                nnfolder-file-coding-system)))
1049       (save-buffer)))
1050   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1051     (nnfolder-save-nov)))
1052
1053 (defun nnfolder-save-active (group-alist active-file)
1054   (let ((nnmail-active-file-coding-system
1055          (or nnfolder-active-file-coding-system-for-write
1056              nnfolder-active-file-coding-system)))
1057     (nnmail-save-active group-alist active-file)))
1058
1059 (defun nnfolder-open-nov (group)
1060   (or (cdr (assoc group nnfolder-nov-buffer-alist))
1061       (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1062         (save-excursion
1063           (set-buffer buffer)
1064           (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1065                (nnfolder-group-nov-pathname group))
1066           (erase-buffer)
1067           (when (file-exists-p nnfolder-nov-buffer-file-name)
1068             (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1069         (push (cons group buffer) nnfolder-nov-buffer-alist)
1070         buffer)))
1071
1072 (defun nnfolder-save-nov ()
1073   (save-excursion
1074     (while nnfolder-nov-buffer-alist
1075       (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1076         (set-buffer (cdar nnfolder-nov-buffer-alist))
1077         (when (buffer-modified-p)
1078           (gnus-make-directory (file-name-directory
1079                                 nnfolder-nov-buffer-file-name))
1080           (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1081                                nil 'nomesg))
1082         (set-buffer-modified-p nil)
1083         (kill-buffer (current-buffer)))
1084       (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1085
1086 (defun nnfolder-nov-delete-article (group article)
1087   (save-excursion
1088     (set-buffer (nnfolder-open-nov group))
1089     (when (nnheader-find-nov-line article)
1090       (delete-region (point) (progn (forward-line 1) (point))))
1091     t))
1092
1093 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1094   (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1095       nil
1096     (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1097       (when (file-exists-p nov)
1098         (save-excursion
1099           (set-buffer nntp-server-buffer)
1100           (erase-buffer)
1101           (nnheader-insert-file-contents nov)
1102           (if (and fetch-old
1103                    (not (numberp fetch-old)))
1104               t                         ; Don't remove anything.
1105             (nnheader-nov-delete-outside-range
1106              (if fetch-old (max 1 (- (car articles) fetch-old))
1107                (car articles))
1108              (car (last articles)))
1109             t))))))
1110
1111 (defun nnfolder-parse-head (&optional number b e)
1112   "Parse the head of the current buffer."
1113   (let ((buf (current-buffer))
1114         chars)
1115     (save-excursion
1116       (unless b
1117         (setq b (if (nnmail-search-unix-mail-delim-backward)
1118                     (point) (point-min)))
1119         (forward-line 1)
1120         (setq e (if (nnmail-search-unix-mail-delim)
1121                     (point) (point-max))))
1122       (setq chars (- e b))
1123       (unless (zerop chars)
1124         (goto-char b)
1125         (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1126       (with-temp-buffer
1127         (insert-buffer-substring buf b e)
1128         ;; Fold continuation lines.
1129         (goto-char (point-min))
1130         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1131           (replace-match " " t t))
1132         ;; Remove any tabs; they are too confusing.
1133         (subst-char-in-region (point-min) (point-max) ?\t ? )
1134         (let ((headers (nnheader-parse-head t)))
1135           (mail-header-set-chars headers chars)
1136           (mail-header-set-number headers number)
1137           headers)))))
1138
1139 (defun nnfolder-add-nov (group article headers)
1140   "Add a nov line for the GROUP base."
1141   (save-excursion
1142     (set-buffer (nnfolder-open-nov group))
1143     (goto-char (point-max))
1144     (mail-header-set-number headers article)
1145     (nnheader-insert-nov headers)))
1146
1147 (deffoo nnfolder-request-set-mark (group actions &optional server)
1148   (when (and server
1149              (not (nnfolder-server-opened server)))
1150     (nnfolder-open-server server))
1151   (unless nnfolder-marks-is-evil
1152     (nnfolder-open-marks group server)
1153     (dolist (action actions)
1154       (let ((range (nth 0 action))
1155             (what  (nth 1 action))
1156             (marks (nth 2 action)))
1157         (assert (or (eq what 'add) (eq what 'del)) t
1158                 "Unknown request-set-mark action: %s" what)
1159         (dolist (mark marks)
1160           (setq nnfolder-marks (gnus-update-alist-soft
1161                             mark
1162                             (funcall (if (eq what 'add) 'gnus-range-add
1163                                        'gnus-remove-from-range)
1164                                      (cdr (assoc mark nnfolder-marks)) range)
1165                             nnfolder-marks)))))
1166     (nnfolder-save-marks group server))
1167   nil)
1168
1169 (deffoo nnfolder-request-update-info (group info &optional server)
1170   ;; Change servers.
1171   (when (and server
1172              (not (nnfolder-server-opened server)))
1173     (nnfolder-open-server server))
1174   (when (and (not nnfolder-marks-is-evil) (nnfolder-marks-changed-p group))
1175     (nnheader-message 8 "Updating marks for %s..." group)
1176     (nnfolder-open-marks group server)
1177     ;; Update info using `nnfolder-marks'.
1178     (mapcar (lambda (pred)
1179               (gnus-info-set-marks
1180                info
1181                (gnus-update-alist-soft
1182                 (cdr pred)
1183                 (cdr (assq (cdr pred) nnfolder-marks))
1184                 (gnus-info-marks info))
1185                t))
1186             gnus-article-mark-lists)
1187     (let ((seen (cdr (assq 'read nnfolder-marks))))
1188       (gnus-info-set-read info
1189                           (if (and (integerp (car seen))
1190                                    (null (cdr seen)))
1191                               (list (cons (car seen) (car seen)))
1192                             seen)))
1193     (nnheader-message 8 "Updating marks for %s...done" group))
1194   info)
1195
1196 (defun nnfolder-group-marks-pathname (group)
1197   "Make pathname for GROUP NOV."
1198   (let ((nnfolder-directory (or nnfolder-marks-directory nnfolder-directory)))
1199     (concat (nnfolder-group-pathname group) nnfolder-marks-file-suffix)))
1200
1201 (defun nnfolder-marks-changed-p (group)
1202   (let ((file (nnfolder-group-marks-pathname group)))
1203     (if (null (gethash file nnfolder-marks-modtime))
1204         t ;; never looked at marks file, assume it has changed
1205       (not (eq (gethash fil nnfolder-marks-modtime)
1206                (nth 5 (file-attributes file)))))))
1207
1208 (defun nnfolder-save-marks (group server)
1209   (let ((file-name-coding-system nnmail-pathname-coding-system)
1210         (file (nnfolder-group-marks-pathname group)))
1211     (condition-case err
1212         (progn
1213           (with-temp-file file
1214             (erase-buffer)
1215             (princ nnfolder-marks (current-buffer))
1216             (insert "\n"))
1217           (puthash file
1218                    (nth 5 (file-attributes file))
1219                    nnml-marks-modtime))
1220       (error (or (gnus-yes-or-no-p
1221                   (format "Could not write to %s (%s).  Continue? " file err))
1222                  (error "Cannot write to %s (%s)" err))))))
1223
1224 (defun nnfolder-open-marks (group server)
1225   (let ((file (nnfolder-group-marks-pathname group)))
1226     (if (file-exists-p file)
1227         (setq nnfolder-marks (condition-case err
1228                                  (with-temp-buffer
1229                                    (puthash file
1230                                             (nth 5 (file-attributes file))
1231                                             nnml-marks-modtime)
1232                                    (nnheader-insert-file-contents file)
1233                                    (read (current-buffer)))
1234                                (error (or (gnus-yes-or-no-p
1235                                            (format "Error reading nnfolder marks file %s (%s).  Continuing will use marks from .newsrc.eld.  Continue? " file err))
1236                                           (error "Cannot read nnfolder marks file %s (%s)" file err)))))
1237       ;; User didn't have a .marks file.  Probably first time
1238       ;; user of the .marks stuff.  Bootstrap it from .newsrc.eld.
1239       (let ((info (gnus-get-info
1240                    (gnus-group-prefixed-name
1241                     group
1242                     (gnus-server-to-method (format "nnfolder:%s" server))))))
1243         (nnheader-message 7 "Bootstrapping marks for %s..." group)
1244         (setq nnfolder-marks (gnus-info-marks info))
1245         (push (cons 'read (gnus-info-read info)) nnfolder-marks)
1246         (nnfolder-save-marks group server)))))
1247
1248 (provide 'nnfolder)
1249
1250 ;;; nnfolder.el ends here