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