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