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