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