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