a200ad90692cd39d23b0d45514d4e2e56be023c9
[gnus] / lisp / nnfolder.el
1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
6 ;;      Scott Byer <byer@mv.us.adobe.com>
7 ;;      Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
9 ;; Keywords: mail
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;;; Code:
31
32 (require 'nnheader)
33 (require 'message)
34 (require 'nnmail)
35 (require 'nnoo)
36 (eval-when-compile (require 'cl))
37 (require 'gnus-util)
38 (require 'gnus-range)
39
40 (nnoo-declare nnfolder)
41
42 (defvoo nnfolder-directory (expand-file-name message-directory)
43   "The name of the nnfolder directory.")
44
45 (defvoo nnfolder-nov-directory nil
46   "The name of the nnfolder NOV directory.
47 If nil, `nnfolder-directory' is used.")
48
49 (defvoo nnfolder-active-file
50     (nnheader-concat nnfolder-directory "active")
51   "The name of the active file.")
52
53 ;; I renamed this variable to something more in keeping with the general GNU
54 ;; style. -SLB
55
56 (defvoo nnfolder-ignore-active-file nil
57   "If non-nil, the active file is ignored.
58 This causes nnfolder to do some extra work in order to determine the
59 true active ranges of an mbox file.  Note that the active file is
60 still saved, but its values are not used.  This costs some extra time
61 when scanning an mbox when opening it.")
62
63 (defvoo nnfolder-distrust-mbox nil
64   "If non-nil, the folder will be distrusted.
65 This means that nnfolder will not trust the user with respect to
66 inserting unaccounted for mail in the middle of an mbox file.  This
67 can greatly slow down scans, which now must scan the entire file for
68 unmarked messages.  When nil, scans occur forward from the last marked
69 message, a huge time saver for large mailboxes.")
70
71 (defvoo nnfolder-newsgroups-file
72     (concat (file-name-as-directory nnfolder-directory) "newsgroups")
73   "Mail newsgroups description file.")
74
75 (defvoo nnfolder-get-new-mail t
76   "If non-nil, nnfolder will check the incoming mail file and split the mail.")
77
78 (defvoo nnfolder-prepare-save-mail-hook nil
79   "Hook run narrowed to an article before saving.")
80
81 (defvoo nnfolder-save-buffer-hook nil
82   "Hook run before saving the nnfolder mbox buffer.")
83
84 (defvoo nnfolder-inhibit-expiry nil
85   "If non-nil, inhibit expiry.")
86
87 \f
88
89 (defconst nnfolder-version "nnfolder 2.0"
90   "nnfolder version.")
91
92 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
93   "String used to demarcate what the article number for a message is.")
94
95 (defvoo nnfolder-current-group nil)
96 (defvoo nnfolder-current-buffer nil)
97 (defvoo nnfolder-status-string "")
98 (defvoo nnfolder-group-alist nil)
99 (defvoo nnfolder-buffer-alist nil)
100 (defvoo nnfolder-scantime-alist nil)
101 (defvoo nnfolder-active-timestamp nil)
102 (defvoo nnfolder-active-file-coding-system mm-text-coding-system)
103 (defvoo nnfolder-active-file-coding-system-for-write 
104     nnmail-active-file-coding-system)
105 (defvoo nnfolder-file-coding-system mm-text-coding-system)
106 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
107   "Coding system for save nnfolder file.
108 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
109
110 (defvoo nnfolder-nov-is-evil nil
111   "If non-nil, Gnus will never generate and use nov databases for mail groups.
112 Using nov databases will speed up header fetching considerably.
113 This variable shouldn't be flipped much.  If you have, for some reason,
114 set this to t, and want to set it to nil again, you should always run
115 the `nnfolder-generate-active-file' command.  The function will go
116 through all nnfolder directories and generate nov databases for them
117 all.  This may very well take some time.")
118
119 (defvoo nnfolder-nov-file-suffix ".nov")
120
121 (defvoo nnfolder-nov-buffer-alist nil)
122
123 (defvar nnfolder-nov-buffer-file-name nil)
124
125 \f
126
127 ;;; Interface functions
128
129 (nnoo-define-basics nnfolder)
130
131 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
132   (save-excursion
133     (set-buffer nntp-server-buffer)
134     (erase-buffer)
135     (let (article start stop)
136       (nnfolder-possibly-change-group group server)
137       (when nnfolder-current-buffer
138         (set-buffer nnfolder-current-buffer)
139         (goto-char (point-min))
140         (if (stringp (car articles))
141             'headers
142           (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
143               'nov
144             (while (setq article (pop articles))
145               (set-buffer nnfolder-current-buffer)
146               (when (nnfolder-goto-article article)
147                 (setq start (point))
148                 (setq stop (if (search-forward "\n\n" nil t)
149                                (1- (point))
150                              (point-max)))
151                 (set-buffer nntp-server-buffer)
152                 (insert (format "221 %d Article retrieved.\n" article))
153                 (insert-buffer-substring nnfolder-current-buffer start stop)
154                 (goto-char (point-max))
155                 (insert ".\n")))
156             (set-buffer nntp-server-buffer)
157             (nnheader-fold-continuation-lines)
158             'headers))))))
159
160 (deffoo nnfolder-open-server (server &optional defs)
161   (nnoo-change-server 'nnfolder server defs)
162   (nnmail-activate 'nnfolder t)
163   (gnus-make-directory nnfolder-directory)
164   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
165     (and nnfolder-nov-directory
166          (gnus-make-directory nnfolder-nov-directory)))
167   (cond
168    ((not (file-exists-p nnfolder-directory))
169     (nnfolder-close-server)
170     (nnheader-report 'nnfolder "Couldn't create directory: %s"
171                      nnfolder-directory))
172    ((not (file-directory-p (file-truename nnfolder-directory)))
173     (nnfolder-close-server)
174     (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
175    (t
176     (nnmail-activate 'nnfolder)
177     (nnheader-report 'nnfolder "Opened server %s using directory %s"
178                      server nnfolder-directory)
179     t)))
180
181 (deffoo nnfolder-request-close ()
182   (let ((alist nnfolder-buffer-alist))
183     (while alist
184       (nnfolder-close-group (caar alist) nil t)
185       (setq alist (cdr alist))))
186   (nnoo-close-server 'nnfolder)
187   (setq nnfolder-buffer-alist nil
188         nnfolder-group-alist nil))
189
190 (deffoo nnfolder-request-article (article &optional group server buffer)
191   (nnfolder-possibly-change-group group server)
192   (save-excursion
193     (set-buffer nnfolder-current-buffer)
194     (goto-char (point-min))
195     (when (nnfolder-goto-article article)
196       (let (start stop)
197         (setq start (point))
198         (forward-line 1)
199         (unless (and (nnmail-search-unix-mail-delim)
200                      (forward-line -1))
201           (goto-char (point-max)))
202         (setq stop (point))
203         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
204           (set-buffer nntp-server-buffer)
205           (erase-buffer)
206           (insert-buffer-substring nnfolder-current-buffer start stop)
207           (goto-char (point-min))
208           (while (looking-at "From ")
209             (delete-char 5)
210             (insert "X-From-Line: ")
211             (forward-line 1))
212           (if (numberp article)
213               (cons nnfolder-current-group article)
214             (goto-char (point-min))
215             (cons nnfolder-current-group
216                   (if (search-forward (concat "\n" nnfolder-article-marker) 
217                                       nil t)
218                       (string-to-int
219                        (buffer-substring
220                         (point) (progn (end-of-line) (point))))
221                     -1))))))))
222
223 (deffoo nnfolder-request-group (group &optional server dont-check)
224   (nnfolder-possibly-change-group group server t)
225   (save-excursion
226     (if (not (assoc group nnfolder-group-alist))
227         (nnheader-report 'nnfolder "No such group: %s" group)
228       (if dont-check
229           (progn
230             (nnheader-report 'nnfolder "Selected group %s" group)
231             t)
232         (let* ((active (assoc group nnfolder-group-alist))
233                (group (car active))
234                (range (cadr active)))
235           (cond
236            ((null active)
237             (nnheader-report 'nnfolder "No such group: %s" group))
238            ((null nnfolder-current-group)
239             (nnheader-report 'nnfolder "Empty group: %s" group))
240            (t
241             (nnheader-report 'nnfolder "Selected group %s" group)
242             (nnheader-insert "211 %d %d %d %s\n"
243                              (1+ (- (cdr range) (car range)))
244                              (car range) (cdr range) group))))))))
245
246 (deffoo nnfolder-request-scan (&optional group server)
247   (nnfolder-possibly-change-group nil server)
248   (when nnfolder-get-new-mail
249     (nnfolder-possibly-change-group group server)
250     (nnmail-get-new-mail
251      'nnfolder
252      (lambda ()
253        (let ((bufs nnfolder-buffer-alist))
254          (save-excursion
255            (while bufs
256              (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
257                  (setq nnfolder-buffer-alist
258                        (delq (car bufs) nnfolder-buffer-alist))
259                (set-buffer (nth 1 (car bufs)))
260                (nnfolder-save-buffer)
261                (kill-buffer (current-buffer)))
262              (setq bufs (cdr bufs))))))
263      nnfolder-directory
264      group)))
265
266 ;; Don't close the buffer if we're not shutting down the server.  This way,
267 ;; we can keep the buffer in the group buffer cache, and not have to grovel
268 ;; over the buffer again unless we add new mail to it or modify it in some
269 ;; way.
270
271 (deffoo nnfolder-close-group (group &optional server force)
272   ;; Make sure we _had_ the group open.
273   (when (or (assoc group nnfolder-buffer-alist)
274             (equal group nnfolder-current-group))
275     (let ((inf (assoc group nnfolder-buffer-alist)))
276       (when inf
277         (when (and nnfolder-current-group
278                    nnfolder-current-buffer)
279           (push (list nnfolder-current-group nnfolder-current-buffer)
280                 nnfolder-buffer-alist))
281         (setq nnfolder-buffer-alist
282               (delq inf nnfolder-buffer-alist))
283         (setq nnfolder-current-buffer (cadr inf)
284               nnfolder-current-group (car inf))))
285     (when (and nnfolder-current-buffer
286                (buffer-name nnfolder-current-buffer))
287       (save-excursion
288         (set-buffer nnfolder-current-buffer)
289         ;; If the buffer was modified, write the file out now.
290         (nnfolder-save-buffer)
291         ;; If we're shutting the server down, we need to kill the
292         ;; buffer and remove it from the open buffer list.  Or, of
293         ;; course, if we're trying to minimize our space impact.
294         (kill-buffer (current-buffer))
295         (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
296                                           nnfolder-buffer-alist)))))
297   (setq nnfolder-current-group nil
298         nnfolder-current-buffer nil)
299   t)
300
301 (deffoo nnfolder-request-create-group (group &optional server args)
302   (nnfolder-possibly-change-group nil server)
303   (nnmail-activate 'nnfolder)
304   (when group
305     (unless (assoc group nnfolder-group-alist)
306       (push (list group (cons 1 0)) nnfolder-group-alist)
307       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
308       (nnfolder-read-folder group)))
309   t)
310
311 (deffoo nnfolder-request-list (&optional server)
312   (nnfolder-possibly-change-group nil server)
313   (save-excursion
314     (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
315       (nnmail-find-file nnfolder-active-file)
316       (setq nnfolder-group-alist (nnmail-get-active)))
317     t))
318
319 (deffoo nnfolder-request-newgroups (date &optional server)
320   (nnfolder-possibly-change-group nil server)
321   (nnfolder-request-list server))
322
323 (deffoo nnfolder-request-list-newsgroups (&optional server)
324   (nnfolder-possibly-change-group nil server)
325   (save-excursion
326     (let ((nnmail-file-coding-system nnfolder-file-coding-system))
327       (nnmail-find-file nnfolder-newsgroups-file))))
328
329 ;; Return a list consisting of all article numbers existing in the
330 ;; current folder.
331
332 (defun nnfolder-existing-articles ()
333   (save-excursion
334     (when nnfolder-current-buffer
335       (set-buffer nnfolder-current-buffer)
336       (goto-char (point-min))
337       (let ((marker (concat "\n" nnfolder-article-marker))
338             (number "[0-9]+")
339             numbers)
340       
341         (while (and (search-forward marker nil t)
342                     (re-search-forward number nil t))
343           (let ((newnum (string-to-number (match-string 0))))
344             (if (nnmail-within-headers-p)
345                 (push newnum numbers))))
346         numbers))))
347
348 (deffoo nnfolder-request-expire-articles
349     (articles newsgroup &optional server force)
350   (nnfolder-possibly-change-group newsgroup server)
351   (let* ((is-old t)
352          ;; The articles we have deleted so far.
353          (deleted-articles nil)
354          ;; The articles that really exist and will
355          ;; be expired if they are old enough.
356          (maybe-expirable
357           (gnus-intersection articles (nnfolder-existing-articles))))
358     (nnmail-activate 'nnfolder)
359
360     (save-excursion
361       (set-buffer nnfolder-current-buffer)
362       ;; Since messages are sorted in arrival order and expired in the
363       ;; same order, we can stop as soon as we find a message that is
364       ;; too old.
365       (while (and maybe-expirable is-old)
366         (goto-char (point-min))
367         (when (and (nnfolder-goto-article (car maybe-expirable))
368                    (search-forward (concat "\n" nnfolder-article-marker)
369                                    nil t))
370           (forward-sexp)
371           (when (setq is-old
372                       (nnmail-expired-article-p
373                        newsgroup
374                        (buffer-substring
375                         (point) (progn (end-of-line) (point)))
376                        force nnfolder-inhibit-expiry))
377             (unless (eq nnmail-expiry-target 'delete)
378               (with-temp-buffer
379                 (nnfolder-request-article (car maybe-expirable) 
380                                           newsgroup server (current-buffer))
381                 (let ((nnml-current-directory nil))
382                   (nnmail-expiry-target-group
383                    nnmail-expiry-target newsgroup))))
384             (nnheader-message 5 "Deleting article %d in %s..."
385                               (car maybe-expirable) newsgroup)
386             (nnfolder-delete-mail)
387             (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
388               (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
389             ;; Must remember which articles were actually deleted
390             (push (car maybe-expirable) deleted-articles)))
391         (setq maybe-expirable (cdr maybe-expirable)))
392       (unless nnfolder-inhibit-expiry
393         (nnheader-message 5 "Deleting articles...done"))
394       (nnfolder-save-buffer)
395       (nnfolder-adjust-min-active newsgroup)
396       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
397       (gnus-sorted-complement articles (nreverse deleted-articles)))))
398
399 (deffoo nnfolder-request-move-article (article group server
400                                                accept-form &optional last)
401   (save-excursion
402     (let ((buf (get-buffer-create " *nnfolder move*"))
403           result)
404       (and
405        (nnfolder-request-article article group server)
406        (save-excursion
407          (set-buffer buf)
408          (erase-buffer)
409          (insert-buffer-substring nntp-server-buffer)
410          (goto-char (point-min))
411          (while (re-search-forward
412                  (concat "^" nnfolder-article-marker)
413                  (save-excursion (and (search-forward "\n\n" nil t) (point))) 
414                  t)
415            (delete-region (progn (beginning-of-line) (point))
416                           (progn (forward-line 1) (point))))
417          (setq result (eval accept-form))
418          (kill-buffer buf)
419          result)
420        (save-excursion
421          (nnfolder-possibly-change-group group server)
422          (set-buffer nnfolder-current-buffer)
423          (goto-char (point-min))
424          (when (nnfolder-goto-article article)
425            (nnfolder-delete-mail))
426          (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
427            (nnfolder-nov-delete-article group article))
428          (when last
429            (nnfolder-save-buffer)
430            (nnfolder-adjust-min-active group)
431            (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
432       result)))
433
434 (deffoo nnfolder-request-accept-article (group &optional server last)
435   (save-excursion
436     (nnfolder-possibly-change-group group server)
437     (nnmail-check-syntax)
438     (let ((buf (current-buffer))
439           result art-group)
440       (goto-char (point-min))
441       (when (looking-at "X-From-Line: ")
442         (replace-match "From "))
443       (and
444        (nnfolder-request-list)
445        (save-excursion
446          (set-buffer buf)
447          (goto-char (point-min))
448          (if (search-forward "\n\n" nil t)
449              (forward-line -1)
450            (goto-char (point-max)))
451          (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
452            (delete-region (point) (progn (forward-line 1) (point))))
453          (when nnmail-cache-accepted-message-ids
454            (nnmail-cache-insert (nnmail-fetch-field "message-id")))
455          (setq result (if (stringp group)
456                           (list (cons group (nnfolder-active-number group)))
457                         (setq art-group
458                               (nnmail-article-group 'nnfolder-active-number))))
459          (if (and (null result)
460                   (yes-or-no-p "Moved to `junk' group; delete article? "))
461              (setq result 'junk)
462            (setq result
463                  (car (nnfolder-save-mail result)))))
464        (when last
465          (save-excursion
466            (nnfolder-possibly-change-folder (or (caar art-group) group))
467            (nnfolder-save-buffer)
468            (when nnmail-cache-accepted-message-ids
469              (nnmail-cache-close)))))
470       (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
471       (unless result
472         (nnheader-report 'nnfolder "Couldn't store article"))
473       result)))
474
475 (deffoo nnfolder-request-replace-article (article group buffer)
476   (nnfolder-possibly-change-group group)
477   (save-excursion
478     (set-buffer buffer)
479     (goto-char (point-min))
480     (let (xfrom)
481       (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
482         (setq xfrom (match-string 1))
483         (gnus-delete-line))
484       (goto-char (point-min))
485       (if xfrom
486           (insert "From " xfrom "\n")
487         (unless (looking-at "From ")
488           (insert "From nobody " (current-time-string) "\n"))))
489     (nnfolder-normalize-buffer)
490     (set-buffer nnfolder-current-buffer)
491     (goto-char (point-min))
492     (if (not (nnfolder-goto-article article))
493         nil
494       (nnfolder-delete-mail)
495       (insert-buffer-substring buffer)
496       (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
497         (save-excursion
498           (set-buffer buffer)
499           (let ((headers (nnfolder-parse-head article 
500                                               (point-min) (point-max))))
501             (with-current-buffer (nnfolder-open-nov group)
502               (if (nnheader-find-nov-line article)
503                   (delete-region (point) (progn (forward-line 1) (point))))
504               (nnheader-insert-nov headers)))))
505       (nnfolder-save-buffer)
506       t)))
507
508 (deffoo nnfolder-request-delete-group (group &optional force server)
509   (nnfolder-close-group group server t)
510   ;; Delete all articles in GROUP.
511   (if (not force)
512       ()                                ; Don't delete the articles.
513     ;; Delete the file that holds the group.
514     (ignore-errors
515       (delete-file (nnfolder-group-pathname group))
516       (delete-file (nnfolder-group-nov-pathname group))))
517   ;; Remove the group from all structures.
518   (setq nnfolder-group-alist
519         (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
520         nnfolder-current-group nil
521         nnfolder-current-buffer nil)
522   ;; Save the active file.
523   (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
524   t)
525
526 (deffoo nnfolder-request-rename-group (group new-name &optional server)
527   (nnfolder-possibly-change-group group server)
528   (save-excursion
529     (set-buffer nnfolder-current-buffer)
530     (and (file-writable-p buffer-file-name)
531          (ignore-errors
532            (let ((new-file (nnfolder-group-pathname new-name)))
533              (gnus-make-directory (file-name-directory new-file))
534              (rename-file buffer-file-name new-file)
535              (setq new-file (nnfolder-group-nov-pathname new-name))
536              (rename-file (nnfolder-group-nov-pathname group)
537                           new-file))
538            t)
539          ;; That went ok, so we change the internal structures.
540          (let ((entry (assoc group nnfolder-group-alist)))
541            (and entry (setcar entry new-name))
542            (setq nnfolder-current-buffer nil
543                  nnfolder-current-group nil)
544            ;; Save the new group alist.
545            (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
546            ;; We kill the buffer instead of renaming it and stuff.
547            (kill-buffer (current-buffer))
548            t))))
549
550 (defun nnfolder-request-regenerate (server)
551   (nnfolder-possibly-change-group nil server)
552   (nnfolder-generate-active-file)
553   t)
554
555 \f
556 ;;; Internal functions.
557
558 (defun nnfolder-adjust-min-active (group)
559   ;; Find the lowest active article in this group.
560   (let* ((active (cadr (assoc group nnfolder-group-alist)))
561          (marker (concat "\n" nnfolder-article-marker))
562          (number "[0-9]+")
563          (activemin (cdr active)))
564     (save-excursion
565       (set-buffer nnfolder-current-buffer)
566       (goto-char (point-min))
567       (while (and (search-forward marker nil t)
568                   (re-search-forward number nil t))
569         (let ((newnum (string-to-number (match-string 0))))
570           (if (nnmail-within-headers-p)
571               (setq activemin (min activemin newnum)))))
572       (setcar active activemin))))
573
574 (defun nnfolder-article-string (article)
575   (if (numberp article)
576       (concat "\n" nnfolder-article-marker (int-to-string article) " ")
577     (concat "\nMessage-ID: " article)))
578
579 (defun nnfolder-goto-article (article)
580   "Place point at the start of the headers of ARTICLE.
581 ARTICLE can be an article number or a Message-ID.
582 Returns t if successful, nil otherwise."
583   (let ((art-string (nnfolder-article-string article))
584         start found)
585     ;; It is likely that we are at or before the delimiter line.
586     ;; We therefore go to the end of the previous line, and start
587     ;; searching from there.
588     (beginning-of-line)
589     (unless (bobp)
590       (forward-char -1))
591     (setq start (point))
592     ;; First search forward.
593     (while (and (setq found (search-forward art-string nil t))
594                 (not (nnmail-within-headers-p))))
595     ;; If unsuccessful, search backward from where we started,
596     (unless found
597       (goto-char start)
598       (while (and (setq found (search-backward art-string nil t))
599                   (not (nnmail-within-headers-p)))))
600     (when found
601       (nnmail-search-unix-mail-delim-backward))))
602
603 (defun nnfolder-delete-mail (&optional leave-delim)
604   "Delete the message that point is in.
605 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
606 deleted.  Point is left where the deleted region was."
607   (save-restriction
608     (narrow-to-region
609      (save-excursion
610        ;; In case point is at the beginning of the message already.
611        (forward-line 1)
612        (nnmail-search-unix-mail-delim-backward)
613        (if leave-delim (progn (forward-line 1) (point))
614          (point)))
615      (progn
616        (forward-line 1)
617        (if (nnmail-search-unix-mail-delim)
618            (point)
619          (point-max))))
620     (run-hooks 'nnfolder-delete-mail-hook)
621     (delete-region (point-min) (point-max))))
622
623 (defun nnfolder-possibly-change-group (group &optional server dont-check)
624   ;; Change servers.
625   (when (and server
626              (not (nnfolder-server-opened server)))
627     (nnfolder-open-server server))
628   (unless (gnus-buffer-live-p nnfolder-current-buffer)
629     (setq nnfolder-current-buffer nil
630           nnfolder-current-group nil))
631   ;; Change group.
632   (when (and group
633              (not (equal group nnfolder-current-group)))
634     (let ((file-name-coding-system nnmail-pathname-coding-system))
635       (nnmail-activate 'nnfolder)
636       (when (and (not (assoc group nnfolder-group-alist))
637                  (not (file-exists-p
638                        (nnfolder-group-pathname group))))
639         ;; The group doesn't exist, so we create a new entry for it.
640         (push (list group (cons 1 0)) nnfolder-group-alist)
641         (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
642
643       (if dont-check
644           (setq nnfolder-current-group group
645                 nnfolder-current-buffer nil)
646         (let (inf file)
647           ;; If we have to change groups, see if we don't already have the
648           ;; folder in memory.  If we do, verify the modtime and destroy
649           ;; the folder if needed so we can rescan it.
650           (setq nnfolder-current-buffer
651                 (nth 1 (assoc group nnfolder-buffer-alist)))
652
653           ;; If the buffer is not live, make sure it isn't in the alist.  If it
654           ;; is live, verify that nobody else has touched the file since last
655           ;; time.
656           (when (and nnfolder-current-buffer
657                      (not (gnus-buffer-live-p nnfolder-current-buffer)))
658             (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
659                   nnfolder-current-buffer nil))
660
661           (setq nnfolder-current-group group)
662
663           (when (or (not nnfolder-current-buffer)
664                     (not (verify-visited-file-modtime
665                           nnfolder-current-buffer)))
666             (save-excursion
667               (setq file (nnfolder-group-pathname group))
668               ;; See whether we need to create the new file.
669               (unless (file-exists-p file)
670                 (gnus-make-directory (file-name-directory file))
671                 (let ((nnmail-file-coding-system 
672                        (or nnfolder-file-coding-system-for-write
673                            nnfolder-file-coding-system-for-write)))
674                   (nnmail-write-region 1 1 file t 'nomesg)))
675               (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
676                 (set-buffer nnfolder-current-buffer)
677                 (push (list group nnfolder-current-buffer)
678                       nnfolder-buffer-alist)))))))))
679
680 (defun nnfolder-save-mail (group-art-list)
681   "Called narrowed to an article."
682   (let* (save-list group-art)
683     (goto-char (point-min))
684     ;; The From line may have been quoted by movemail.
685     (when (looking-at ">From")
686       (delete-char 1))
687     ;; This might come from somewhere else.
688     (unless (looking-at "From ")
689       (insert "From nobody " (current-time-string) "\n")
690       (goto-char (point-min)))
691     ;; Quote all "From " lines in the article.
692     (forward-line 1)
693     (let (case-fold-search)
694       (while (re-search-forward "^From " nil t)
695         (beginning-of-line)
696         (insert "> ")))
697     (setq save-list group-art-list)
698     (nnmail-insert-lines)
699     (nnmail-insert-xref group-art-list)
700     (run-hooks 'nnmail-prepare-save-mail-hook)
701     (run-hooks 'nnfolder-prepare-save-mail-hook)
702
703     ;; Insert the mail into each of the destination groups.
704     (while (setq group-art (pop group-art-list))
705       ;; Kill any previous newsgroup markers.
706       (goto-char (point-min))
707       (if (search-forward "\n\n" nil t)
708           (forward-line -1)
709         (goto-char (point-max)))
710       (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
711         (delete-region (1+ (point)) (progn (forward-line 2) (point))))
712
713       ;; Insert the new newsgroup marker.
714       (nnfolder-insert-newsgroup-line group-art)
715
716       (save-excursion
717         (let ((beg (point-min))
718               (end (point-max))
719               (obuf (current-buffer)))
720           (nnfolder-possibly-change-folder (car group-art))
721           (let ((buffer-read-only nil))
722             (nnfolder-normalize-buffer)
723             (insert-buffer-substring obuf beg end))
724           (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
725             (set-buffer obuf)
726             (nnfolder-add-nov (car group-art) (cdr group-art)
727                               (nnfolder-parse-head nil beg end))))))
728
729     ;; Did we save it anywhere?
730     save-list))
731
732 (defun nnfolder-normalize-buffer ()
733   "Make sure there are two newlines at the end of the buffer."
734   (goto-char (point-max))
735   (skip-chars-backward "\n")
736   (delete-region (point) (point-max))
737   (insert "\n\n"))
738
739 (defun nnfolder-insert-newsgroup-line (group-art)
740   (save-excursion
741     (goto-char (point-min))
742     (unless (search-forward "\n\n" nil t)
743       (goto-char (point-max))
744       (insert "\n"))
745     (forward-char -1)
746     (insert (format (concat nnfolder-article-marker "%d   %s\n")
747                     (cdr group-art) (current-time-string)))))
748
749 (defun nnfolder-active-number (group)
750   ;; Find the next article number in GROUP.
751   (let ((active (cadr (assoc group nnfolder-group-alist))))
752     (if active
753         (setcdr active (1+ (cdr active)))
754       ;; This group is new, so we create a new entry for it.
755       ;; This might be a bit naughty... creating groups on the drop of
756       ;; a hat, but I don't know...
757       (push (list group (setq active (cons 1 1)))
758             nnfolder-group-alist))
759     (cdr active)))
760
761 (defun nnfolder-possibly-change-folder (group)
762   (let ((inf (assoc group nnfolder-buffer-alist)))
763     (if (and inf
764              (gnus-buffer-live-p (cadr inf)))
765         (set-buffer (cadr inf))
766       (when inf
767         (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
768       (when nnfolder-group-alist
769         (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
770       (push (list group (nnfolder-read-folder group))
771             nnfolder-buffer-alist))))
772
773 ;; This method has a problem if you've accidentally let the active list get
774 ;; out of sync with the files.  This could happen, say, if you've
775 ;; accidentally gotten new mail with something other than Gnus (but why
776 ;; would _that_ ever happen? :-).  In that case, we will be in the middle of
777 ;; processing the file, ready to add new X-Gnus article number markers, and
778 ;; we'll run across a message with no ID yet - the active list _may_not_ be
779 ;; ready for us yet.
780
781 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
782 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
783 ;; rest of the message looking for any more, possibly higher IDs.  We'll
784 ;; assume the maximum that we find is the highest active.  Note that this
785 ;; shouldn't cost us much extra time at all, but will be a lot less
786 ;; vulnerable to glitches between the mbox and the active file.
787
788 (defun nnfolder-read-folder (group)
789   (let* ((file (nnfolder-group-pathname group))
790          (nov  (nnfolder-group-nov-pathname group))
791          (buffer (set-buffer
792                   (let ((nnheader-file-coding-system 
793                          nnfolder-file-coding-system))
794                     (nnheader-find-file-noselect file)))))
795     (if (equal (cadr (assoc group nnfolder-scantime-alist))
796                (nth 5 (file-attributes file)))
797         ;; This looks up-to-date, so we don't do any scanning.
798         (if (file-exists-p file)
799             buffer
800           (push (list group buffer) nnfolder-buffer-alist)
801           (set-buffer-modified-p t)
802           (nnfolder-save-buffer))
803       ;; Parse the damn thing.
804       (save-excursion
805         (goto-char (point-min))
806         ;; Remove any blank lines at the start.
807         (while (eq (following-char) ?\n)
808           (delete-char 1))
809         (nnmail-activate 'nnfolder)
810         ;; Read in the file.
811         (let ((delim "^From ")
812               (marker (concat "\n" nnfolder-article-marker))
813               (number "[0-9]+")
814               (active (or (cadr (assoc group nnfolder-group-alist))
815                           (cons 1 0)))
816               (scantime (assoc group nnfolder-scantime-alist))
817               (minid (lsh -1 -1))
818               maxid start end newscantime
819               novbuf articles newnum
820               buffer-read-only)
821           (buffer-disable-undo)
822           (setq maxid (cdr active))
823
824           (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
825                       (and (file-exists-p nov)
826                            (file-newer-than-file-p nov file)))
827             (unless (file-exists-p nov)
828               (gnus-make-directory (file-name-directory nov)))
829             (with-current-buffer
830                 (setq novbuf (nnfolder-open-nov group))
831               (goto-char (point-min))
832               (while (not (eobp))
833                 (push (read novbuf) articles)
834                 (forward-line 1))
835               (setq articles (nreverse articles))))
836           (goto-char (point-min))
837
838           ;; Anytime the active number is 1 or 0, it is suspect.  In that
839           ;; case, search the file manually to find the active number.  Or,
840           ;; of course, if we're being paranoid.  (This would also be the
841           ;; place to build other lists from the header markers, such as
842           ;; expunge lists, etc., if we ever desired to abandon the active
843           ;; file entirely for mboxes.)
844           (when (or nnfolder-ignore-active-file
845                     novbuf
846                     (< maxid 2))
847             (while (and (search-forward marker nil t)
848                         (looking-at number))
849               (setq newnum (string-to-number (match-string 0)))
850               (when (nnmail-within-headers-p)
851                 (setq maxid (max maxid newnum)
852                       minid (min minid newnum))
853                 (when novbuf
854                   (if (memq newnum articles)
855                       (setq articles (delq newnum articles))
856                     (let ((headers (nnfolder-parse-head newnum)))
857                       (with-current-buffer novbuf
858                         (nnheader-find-nov-line newnum)
859                         (nnheader-insert-nov headers)))))))
860             (when (and novbuf articles)
861               (with-current-buffer novbuf
862                 (dolist (article articles)
863                   (when (nnheader-find-nov-line article)
864                     (delete-region (point) 
865                                    (progn (forward-line 1) (point)))))))
866             (setcar active (max 1 (min minid maxid)))
867             (setcdr active (max maxid (cdr active)))
868             (goto-char (point-min)))
869
870           ;; As long as we trust that the user will only insert unmarked mail
871           ;; at the end, go to the end and search backwards for the last
872           ;; marker.  Find the start of that message, and begin to search for
873           ;; unmarked messages from there.
874           (when (not (or nnfolder-distrust-mbox
875                          (< maxid 2)))
876             (goto-char (point-max))
877             (unless (re-search-backward marker nil t)
878               (goto-char (point-min)))
879             ;;(when (nnmail-search-unix-mail-delim)
880             ;;  (goto-char (point-min)))
881             )
882
883           ;; Keep track of the active number on our own, and insert it back
884           ;; into the active list when we're done.  Also, prime the pump to
885           ;; cut down on the number of searches we do.
886           (unless (nnmail-search-unix-mail-delim)
887             (goto-char (point-max)))
888           (setq end (point-marker))
889           (while (not (= end (point-max)))
890             (setq start (marker-position end))
891             (goto-char end)
892             ;; There may be more than one "From " line, so we skip past
893             ;; them.
894             (while (looking-at delim)
895               (forward-line 1))
896             (set-marker end (if (nnmail-search-unix-mail-delim)
897                                 (point)
898                               (point-max)))
899             (goto-char start)
900             (when (not (search-forward marker end t))
901               (narrow-to-region start end)
902               (nnmail-insert-lines)
903               (nnfolder-insert-newsgroup-line
904                (cons nil 
905                      (setq newnum
906                            (nnfolder-active-number nnfolder-current-group))))
907               (when novbuf
908                 (let ((headers (nnfolder-parse-head newnum (point-min)
909                                                       (point-max))))
910                   (with-current-buffer novbuf
911                     (goto-char (point-max))
912                     (nnheader-insert-nov headers))))
913               (widen)))
914
915           (set-marker end nil)
916           ;; Make absolutely sure that the active list reflects reality!
917           (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
918
919           ;; Set the scantime for this group.
920           (setq newscantime (visited-file-modtime))
921           (if scantime
922               (setcdr scantime (list newscantime))
923             (push (list nnfolder-current-group newscantime)
924                   nnfolder-scantime-alist))
925           ;; Save nov.
926           (when novbuf
927             (nnfolder-save-nov))
928           (current-buffer))))))
929
930 ;;;###autoload
931 (defun nnfolder-generate-active-file ()
932   "Look for mbox folders in the nnfolder directory and make them into groups.
933 This command does not work if you use short group names."
934   (interactive)
935   (nnmail-activate 'nnfolder)
936   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
937     (dolist (file (directory-files (or nnfolder-nov-directory 
938                                        nnfolder-directory)
939                                    t 
940                                    (concat 
941                                     (regexp-quote nnfolder-nov-file-suffix)
942                                     "$")))
943       (when (not (message-mail-file-mbox-p file))
944         (ignore-errors
945           (delete-file file)))))
946   (let ((files (directory-files nnfolder-directory))
947         file)
948     (while (setq file (pop files))
949       (when (and (not (backup-file-name-p file))
950                  (message-mail-file-mbox-p
951                   (nnheader-concat nnfolder-directory file)))
952         (let ((oldgroup (assoc file nnfolder-group-alist)))
953           (if oldgroup
954               (nnheader-message 5 "Refreshing group %s..." file)
955             (nnheader-message 5 "Adding group %s..." file))
956           (if oldgroup
957               (setq nnfolder-group-alist
958                     (delq oldgroup (copy-sequence nnfolder-group-alist))))
959           (push (list file (cons 1 0)) nnfolder-group-alist)
960           (nnfolder-possibly-change-folder file)
961           (nnfolder-possibly-change-group file)
962           (nnfolder-close-group file))))
963     (nnheader-message 5 "")))
964
965 (defun nnfolder-group-pathname (group)
966   "Make pathname for GROUP."
967   (setq group
968         (mm-encode-coding-string group nnmail-pathname-coding-system))
969   (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
970     ;; If this file exists, we use it directly.
971     (if (or nnmail-use-long-file-names
972             (file-exists-p (concat dir group)))
973         (concat dir group)
974       ;; If not, we translate dots into slashes.
975       (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
976
977 (defun nnfolder-group-nov-pathname (group)
978   "Make pathname for GROUP NOV."
979   (let ((nnfolder-directory
980          (or nnfolder-nov-directory nnfolder-directory)))
981     (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
982
983 (defun nnfolder-save-buffer ()
984   "Save the buffer."
985   (when (buffer-modified-p)
986     (run-hooks 'nnfolder-save-buffer-hook)
987     (gnus-make-directory (file-name-directory (buffer-file-name)))
988     (let ((coding-system-for-write 
989            (or nnfolder-file-coding-system-for-write
990                nnfolder-file-coding-system)))
991       (save-buffer)))
992   (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
993     (nnfolder-save-nov)))
994
995 (defun nnfolder-save-active (group-alist active-file)
996   (let ((nnmail-active-file-coding-system
997          (or nnfolder-active-file-coding-system-for-write
998              nnfolder-active-file-coding-system)))
999     (nnmail-save-active group-alist active-file)))
1000
1001 (defun nnfolder-open-nov (group)
1002   (or (cdr (assoc group nnfolder-nov-buffer-alist))
1003       (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1004         (save-excursion
1005           (set-buffer buffer)
1006           (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1007                (nnfolder-group-nov-pathname group))
1008           (erase-buffer)
1009           (when (file-exists-p nnfolder-nov-buffer-file-name)
1010             (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1011         (push (cons group buffer) nnfolder-nov-buffer-alist)
1012         buffer)))
1013
1014 (defun nnfolder-save-nov ()
1015   (save-excursion
1016     (while nnfolder-nov-buffer-alist
1017       (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1018         (set-buffer (cdar nnfolder-nov-buffer-alist))
1019         (when (buffer-modified-p)
1020           (gnus-make-directory (file-name-directory 
1021                                 nnfolder-nov-buffer-file-name))
1022           (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1023                                nil 'nomesg))
1024         (set-buffer-modified-p nil)
1025         (kill-buffer (current-buffer)))
1026       (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1027
1028 (defun nnfolder-nov-delete-article (group article)
1029   (save-excursion
1030     (set-buffer (nnfolder-open-nov group))
1031     (when (nnheader-find-nov-line article)
1032       (delete-region (point) (progn (forward-line 1) (point))))
1033     t))
1034
1035 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1036   (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1037       nil
1038     (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1039       (when (file-exists-p nov)
1040         (save-excursion
1041           (set-buffer nntp-server-buffer)
1042           (erase-buffer)
1043           (nnheader-insert-file-contents nov)
1044           (if (and fetch-old
1045                    (not (numberp fetch-old)))
1046               t                         ; Don't remove anything.
1047             (nnheader-nov-delete-outside-range
1048              (if fetch-old (max 1 (- (car articles) fetch-old))
1049                (car articles))
1050              (car (last articles)))
1051             t))))))
1052
1053 (defun nnfolder-parse-head (&optional number b e)
1054   "Parse the head of the current buffer."
1055   (let ((buf (current-buffer))
1056         chars)
1057   (save-excursion
1058     (unless b
1059       (setq b (if (nnmail-search-unix-mail-delim-backward)
1060                   (point) (point-min)))
1061       (forward-line 1)
1062       (setq e (if (nnmail-search-unix-mail-delim)
1063                   (point) (point-max))))
1064     (setq chars (- e b))
1065     (unless (zerop chars)
1066       (goto-char b)
1067       (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1068     (with-temp-buffer
1069       (insert-buffer-substring buf b e)
1070       ;; Fold continuation lines.
1071       (goto-char (point-min))
1072       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1073         (replace-match " " t t))
1074       ;; Remove any tabs; they are too confusing.
1075       (subst-char-in-region (point-min) (point-max) ?\t ? )
1076       (let ((headers (nnheader-parse-head t)))
1077         (mail-header-set-chars headers chars)
1078         (mail-header-set-number headers number)
1079         headers)))))
1080
1081 (defun nnfolder-add-nov (group article headers)
1082   "Add a nov line for the GROUP base."
1083   (save-excursion
1084     (set-buffer (nnfolder-open-nov group))
1085     (goto-char (point-max))
1086     (mail-header-set-number headers article)
1087     (nnheader-insert-nov headers)))
1088
1089 (provide 'nnfolder)
1090
1091 ;;; nnfolder.el ends here