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