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