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