1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;; Free Software Foundation, Inc.
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>
11 ;; This file is part of GNU Emacs.
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)
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.
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.
36 (eval-when-compile (require 'cl))
41 (autoload 'gnus-intersection "gnus-range"))
43 (nnoo-declare nnfolder)
45 (defvoo nnfolder-directory (expand-file-name message-directory)
46 "The name of the nnfolder directory.")
48 (defvoo nnfolder-nov-directory nil
49 "The name of the nnfolder NOV directory.
50 If nil, `nnfolder-directory' is used.")
52 (defvoo nnfolder-active-file
53 (nnheader-concat nnfolder-directory "active")
54 "The name of the active file.")
56 ;; I renamed this variable to something more in keeping with the general GNU
59 (defvoo nnfolder-ignore-active-file nil
60 "If non-nil, the active file is ignored.
61 This causes nnfolder to do some extra work in order to determine the
62 true active ranges of an mbox file. Note that the active file is
63 still saved, but its values are not used. This costs some extra time
64 when scanning an mbox when opening it.")
66 (defvoo nnfolder-distrust-mbox nil
67 "If non-nil, the folder will be distrusted.
68 This means that nnfolder will not trust the user with respect to
69 inserting unaccounted for mail in the middle of an mbox file. This
70 can greatly slow down scans, which now must scan the entire file for
71 unmarked messages. When nil, scans occur forward from the last marked
72 message, a huge time saver for large mailboxes.")
74 (defvoo nnfolder-newsgroups-file
75 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
76 "Mail newsgroups description file.")
78 (defvoo nnfolder-get-new-mail t
79 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
81 (defvoo nnfolder-prepare-save-mail-hook nil
82 "Hook run narrowed to an article before saving.")
84 (defvoo nnfolder-save-buffer-hook nil
85 "Hook run before saving the nnfolder mbox buffer.")
87 (defvoo nnfolder-inhibit-expiry nil
88 "If non-nil, inhibit expiry.")
92 (defconst nnfolder-version "nnfolder 2.0"
95 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
96 "String used to demarcate what the article number for a message is.")
98 (defvoo nnfolder-current-group nil)
99 (defvoo nnfolder-current-buffer nil)
100 (defvoo nnfolder-status-string "")
101 (defvoo nnfolder-group-alist nil)
102 (defvoo nnfolder-buffer-alist nil)
103 (defvoo nnfolder-scantime-alist nil)
104 (defvoo nnfolder-active-timestamp nil)
105 (defvoo nnfolder-active-file-coding-system mm-text-coding-system)
106 (defvoo nnfolder-active-file-coding-system-for-write
107 nnmail-active-file-coding-system)
108 (defvoo nnfolder-file-coding-system mm-text-coding-system)
109 (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
110 "Coding system for save nnfolder file.
111 If NIL, NNFOLDER-FILE-CODING-SYSTEM is used.")
113 (defvoo nnfolder-nov-is-evil nil
114 "If non-nil, Gnus will never generate and use nov databases for mail groups.
115 Using nov databases will speed up header fetching considerably.
116 This variable shouldn't be flipped much. If you have, for some reason,
117 set this to t, and want to set it to nil again, you should always run
118 the `nnfolder-generate-active-file' command. The function will go
119 through all nnfolder directories and generate nov databases for them
120 all. This may very well take some time.")
122 (defvoo nnfolder-nov-file-suffix ".nov")
124 (defvoo nnfolder-nov-buffer-alist nil)
126 (defvar nnfolder-nov-buffer-file-name nil)
130 ;;; Interface functions
132 (nnoo-define-basics nnfolder)
134 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
136 (set-buffer nntp-server-buffer)
138 (let (article start stop)
139 (nnfolder-possibly-change-group group server)
140 (when nnfolder-current-buffer
141 (set-buffer nnfolder-current-buffer)
142 (goto-char (point-min))
143 (if (stringp (car articles))
145 (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
147 (setq articles (gnus-sorted-intersection
148 ;; Is ARTICLES sorted?
150 (nnfolder-existing-articles)))
151 (while (setq article (pop articles))
152 (set-buffer nnfolder-current-buffer)
153 (when (nnfolder-goto-article article)
155 (setq stop (if (search-forward "\n\n" nil t)
158 (set-buffer nntp-server-buffer)
159 (insert (format "221 %d Article retrieved.\n" article))
160 (insert-buffer-substring nnfolder-current-buffer start stop)
161 (goto-char (point-max))
163 (set-buffer nntp-server-buffer)
164 (nnheader-fold-continuation-lines)
167 (deffoo nnfolder-open-server (server &optional defs)
168 (nnoo-change-server 'nnfolder server defs)
169 (nnmail-activate 'nnfolder t)
170 (gnus-make-directory nnfolder-directory)
171 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
172 (and nnfolder-nov-directory
173 (gnus-make-directory nnfolder-nov-directory)))
175 ((not (file-exists-p nnfolder-directory))
176 (nnfolder-close-server)
177 (nnheader-report 'nnfolder "Couldn't create directory: %s"
179 ((not (file-directory-p (file-truename nnfolder-directory)))
180 (nnfolder-close-server)
181 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
183 (nnmail-activate 'nnfolder)
184 (nnheader-report 'nnfolder "Opened server %s using directory %s"
185 server nnfolder-directory)
188 (deffoo nnfolder-request-close ()
189 (let ((alist nnfolder-buffer-alist))
191 (nnfolder-close-group (caar alist) nil t)
192 (setq alist (cdr alist))))
193 (nnoo-close-server 'nnfolder)
194 (setq nnfolder-buffer-alist nil
195 nnfolder-group-alist nil))
197 (deffoo nnfolder-request-article (article &optional group server buffer)
198 (nnfolder-possibly-change-group group server)
200 (set-buffer nnfolder-current-buffer)
201 (goto-char (point-min))
202 (when (nnfolder-goto-article article)
206 (unless (and (nnmail-search-unix-mail-delim)
208 (goto-char (point-max)))
210 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
211 (set-buffer nntp-server-buffer)
213 (insert-buffer-substring nnfolder-current-buffer start stop)
214 (goto-char (point-min))
215 (while (looking-at "From ")
217 (insert "X-From-Line: ")
219 (if (numberp article)
220 (cons nnfolder-current-group article)
221 (goto-char (point-min))
222 (cons nnfolder-current-group
223 (if (search-forward (concat "\n" nnfolder-article-marker)
227 (point) (progn (end-of-line) (point))))
230 (deffoo nnfolder-request-group (group &optional server dont-check)
231 (nnfolder-possibly-change-group group server t)
233 (if (not (assoc group nnfolder-group-alist))
234 (nnheader-report 'nnfolder "No such group: %s" group)
237 (nnheader-report 'nnfolder "Selected group %s" group)
239 (let* ((active (assoc group nnfolder-group-alist))
241 (range (cadr active)))
244 (nnheader-report 'nnfolder "No such group: %s" group))
245 ((null nnfolder-current-group)
246 (nnheader-report 'nnfolder "Empty group: %s" group))
248 (nnheader-report 'nnfolder "Selected group %s" group)
249 (nnheader-insert "211 %d %d %d %s\n"
250 (1+ (- (cdr range) (car range)))
251 (car range) (cdr range) group))))))))
253 (deffoo nnfolder-request-scan (&optional group server)
254 (nnfolder-possibly-change-group nil server)
255 (when nnfolder-get-new-mail
256 (nnfolder-possibly-change-group group server)
260 (let ((bufs nnfolder-buffer-alist))
263 (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
264 (setq nnfolder-buffer-alist
265 (delq (car bufs) nnfolder-buffer-alist))
266 (set-buffer (nth 1 (car bufs)))
267 (nnfolder-save-buffer)
268 (kill-buffer (current-buffer)))
269 (setq bufs (cdr bufs))))))
273 ;; Don't close the buffer if we're not shutting down the server. This way,
274 ;; we can keep the buffer in the group buffer cache, and not have to grovel
275 ;; over the buffer again unless we add new mail to it or modify it in some
278 (deffoo nnfolder-close-group (group &optional server force)
279 ;; Make sure we _had_ the group open.
280 (when (or (assoc group nnfolder-buffer-alist)
281 (equal group nnfolder-current-group))
282 (let ((inf (assoc group nnfolder-buffer-alist)))
284 (when (and nnfolder-current-group
285 nnfolder-current-buffer)
286 (push (list nnfolder-current-group nnfolder-current-buffer)
287 nnfolder-buffer-alist))
288 (setq nnfolder-buffer-alist
289 (delq inf nnfolder-buffer-alist))
290 (setq nnfolder-current-buffer (cadr inf)
291 nnfolder-current-group (car inf))))
292 (when (and nnfolder-current-buffer
293 (buffer-name nnfolder-current-buffer))
295 (set-buffer nnfolder-current-buffer)
296 ;; If the buffer was modified, write the file out now.
297 (nnfolder-save-buffer)
298 ;; If we're shutting the server down, we need to kill the
299 ;; buffer and remove it from the open buffer list. Or, of
300 ;; course, if we're trying to minimize our space impact.
301 (kill-buffer (current-buffer))
302 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
303 nnfolder-buffer-alist)))))
304 (setq nnfolder-current-group nil
305 nnfolder-current-buffer nil)
308 (deffoo nnfolder-request-create-group (group &optional server args)
309 (nnfolder-possibly-change-group nil server)
310 (nnmail-activate 'nnfolder)
312 (unless (assoc group nnfolder-group-alist)
313 (push (list group (cons 1 0)) nnfolder-group-alist)
314 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
315 (nnfolder-read-folder group)))
318 (deffoo nnfolder-request-list (&optional server)
319 (nnfolder-possibly-change-group nil server)
321 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
322 (nnmail-find-file nnfolder-active-file)
323 (setq nnfolder-group-alist (nnmail-get-active)))
326 (deffoo nnfolder-request-newgroups (date &optional server)
327 (nnfolder-possibly-change-group nil server)
328 (nnfolder-request-list server))
330 (deffoo nnfolder-request-list-newsgroups (&optional server)
331 (nnfolder-possibly-change-group nil server)
333 (let ((nnmail-file-coding-system nnfolder-file-coding-system))
334 (nnmail-find-file nnfolder-newsgroups-file))))
336 ;; Return a list consisting of all article numbers existing in the
339 (defun nnfolder-existing-articles ()
341 (when nnfolder-current-buffer
342 (set-buffer nnfolder-current-buffer)
343 (goto-char (point-min))
344 (let ((marker (concat "\n" nnfolder-article-marker))
347 (while (and (search-forward marker nil t)
348 (re-search-forward number nil t))
349 (let ((newnum (string-to-number (match-string 0))))
350 (if (nnmail-within-headers-p)
351 (push newnum numbers))))
352 ;; The article numbers are increasing, so this result is sorted.
353 (nreverse numbers)))))
355 (deffoo nnfolder-request-expire-articles
356 (articles newsgroup &optional server force)
357 (nnfolder-possibly-change-group newsgroup server)
359 ;; The articles we have deleted so far.
360 (deleted-articles nil)
361 ;; The articles that really exist and will
362 ;; be expired if they are old enough.
364 (gnus-sorted-intersection articles (nnfolder-existing-articles))))
365 (nnmail-activate 'nnfolder)
368 (set-buffer nnfolder-current-buffer)
369 ;; Since messages are sorted in arrival order and expired in the
370 ;; same order, we can stop as soon as we find a message that is
372 (while (and maybe-expirable is-old)
373 (goto-char (point-min))
374 (when (and (nnfolder-goto-article (car maybe-expirable))
375 (search-forward (concat "\n" nnfolder-article-marker)
379 (nnmail-expired-article-p
382 (point) (progn (end-of-line) (point)))
383 force nnfolder-inhibit-expiry))
384 (unless (eq nnmail-expiry-target 'delete)
386 (nnfolder-request-article (car maybe-expirable)
387 newsgroup server (current-buffer))
388 (let ((nnml-current-directory nil))
389 (nnmail-expiry-target-group
390 nnmail-expiry-target newsgroup))))
391 (nnheader-message 5 "Deleting article %d in %s..."
392 (car maybe-expirable) newsgroup)
393 (nnfolder-delete-mail)
394 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
395 (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
396 ;; Must remember which articles were actually deleted
397 (push (car maybe-expirable) deleted-articles)))
398 (setq maybe-expirable (cdr maybe-expirable)))
399 (unless nnfolder-inhibit-expiry
400 (nnheader-message 5 "Deleting articles...done"))
401 (nnfolder-save-buffer)
402 (nnfolder-adjust-min-active newsgroup)
403 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
404 (gnus-sorted-complement articles (nreverse deleted-articles)))))
406 (deffoo nnfolder-request-move-article (article group server
407 accept-form &optional last)
409 (let ((buf (get-buffer-create " *nnfolder move*"))
412 (nnfolder-request-article article group server)
416 (insert-buffer-substring nntp-server-buffer)
417 (goto-char (point-min))
418 (while (re-search-forward
419 (concat "^" nnfolder-article-marker)
420 (save-excursion (and (search-forward "\n\n" nil t) (point)))
422 (delete-region (progn (beginning-of-line) (point))
423 (progn (forward-line 1) (point))))
424 (setq result (eval accept-form))
428 (nnfolder-possibly-change-group group server)
429 (set-buffer nnfolder-current-buffer)
430 (goto-char (point-min))
431 (when (nnfolder-goto-article article)
432 (nnfolder-delete-mail))
433 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
434 (nnfolder-nov-delete-article group article))
436 (nnfolder-save-buffer)
437 (nnfolder-adjust-min-active group)
438 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
441 (deffoo nnfolder-request-accept-article (group &optional server last)
443 (nnfolder-possibly-change-group group server)
444 (nnmail-check-syntax)
445 (let ((buf (current-buffer))
447 (goto-char (point-min))
448 (when (looking-at "X-From-Line: ")
449 (replace-match "From "))
451 (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
452 (nntp-server-buffer (current-buffer)))
453 (nnmail-find-file nnfolder-active-file)
454 (setq nnfolder-group-alist (nnmail-parse-active))))
456 (goto-char (point-min))
457 (if (search-forward "\n\n" nil t)
459 (goto-char (point-max)))
460 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
461 (delete-region (point) (progn (forward-line 1) (point))))
462 (when nnmail-cache-accepted-message-ids
463 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
464 (setq result (if (stringp group)
465 (list (cons group (nnfolder-active-number group)))
467 (nnmail-article-group 'nnfolder-active-number))))
468 (if (and (null result)
469 (yes-or-no-p "Moved to `junk' group; delete article? "))
472 (car (nnfolder-save-mail result)))))
475 (nnfolder-possibly-change-folder (or (caar art-group) group))
476 (nnfolder-save-buffer)
477 (when nnmail-cache-accepted-message-ids
478 (nnmail-cache-close))))
479 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
481 (nnheader-report 'nnfolder "Couldn't store article"))
484 (deffoo nnfolder-request-replace-article (article group buffer)
485 (nnfolder-possibly-change-group group)
488 (goto-char (point-min))
490 (while (re-search-forward "^X-From-Line: \\(.*\\)$" nil t)
491 (setq xfrom (match-string 1))
493 (goto-char (point-min))
495 (insert "From " xfrom "\n")
496 (unless (looking-at "From ")
497 (insert "From nobody " (current-time-string) "\n"))))
498 (nnfolder-normalize-buffer)
499 (set-buffer nnfolder-current-buffer)
500 (goto-char (point-min))
501 (if (not (nnfolder-goto-article article))
503 (nnfolder-delete-mail)
504 (insert-buffer-substring buffer)
505 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
508 (let ((headers (nnfolder-parse-head article
509 (point-min) (point-max))))
510 (with-current-buffer (nnfolder-open-nov group)
511 (if (nnheader-find-nov-line article)
512 (delete-region (point) (progn (forward-line 1) (point))))
513 (nnheader-insert-nov headers)))))
514 (nnfolder-save-buffer)
517 (deffoo nnfolder-request-delete-group (group &optional force server)
518 (nnfolder-close-group group server t)
519 ;; Delete all articles in GROUP.
521 () ; Don't delete the articles.
522 ;; Delete the file that holds the group.
524 (delete-file (nnfolder-group-pathname group))
525 (delete-file (nnfolder-group-nov-pathname group))))
526 ;; Remove the group from all structures.
527 (setq nnfolder-group-alist
528 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
529 nnfolder-current-group nil
530 nnfolder-current-buffer nil)
531 ;; Save the active file.
532 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
535 (deffoo nnfolder-request-rename-group (group new-name &optional server)
536 (nnfolder-possibly-change-group group server)
538 (set-buffer nnfolder-current-buffer)
539 (and (file-writable-p buffer-file-name)
541 (let ((new-file (nnfolder-group-pathname new-name)))
542 (gnus-make-directory (file-name-directory new-file))
543 (rename-file buffer-file-name new-file)
544 (setq new-file (nnfolder-group-nov-pathname new-name))
545 (rename-file (nnfolder-group-nov-pathname group)
548 ;; That went ok, so we change the internal structures.
549 (let ((entry (assoc group nnfolder-group-alist)))
550 (and entry (setcar entry new-name))
551 (setq nnfolder-current-buffer nil
552 nnfolder-current-group nil)
553 ;; Save the new group alist.
554 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
555 ;; We kill the buffer instead of renaming it and stuff.
556 (kill-buffer (current-buffer))
559 (deffoo nnfolder-request-regenerate (server)
560 (nnfolder-possibly-change-group nil server)
561 (nnfolder-generate-active-file)
565 ;;; Internal functions.
567 (defun nnfolder-adjust-min-active (group)
568 ;; Find the lowest active article in this group.
569 (let* ((active (cadr (assoc group nnfolder-group-alist)))
570 (marker (concat "\n" nnfolder-article-marker))
572 (activemin (cdr active)))
574 (set-buffer nnfolder-current-buffer)
575 (goto-char (point-min))
576 (while (and (search-forward marker nil t)
577 (re-search-forward number nil t))
578 (let ((newnum (string-to-number (match-string 0))))
579 (if (nnmail-within-headers-p)
580 (setq activemin (min activemin newnum)))))
581 (setcar active activemin))))
583 (defun nnfolder-article-string (article)
584 (if (numberp article)
585 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
586 (concat "\nMessage-ID: " article)))
588 (defun nnfolder-goto-article (article)
589 "Place point at the start of the headers of ARTICLE.
590 ARTICLE can be an article number or a Message-ID.
591 Returns t if successful, nil otherwise."
592 (let ((art-string (nnfolder-article-string article))
594 ;; It is likely that we are at or before the delimiter line.
595 ;; We therefore go to the end of the previous line, and start
596 ;; searching from there.
601 ;; First search forward.
602 (while (and (setq found (search-forward art-string nil t))
603 (not (nnmail-within-headers-p))))
604 ;; If unsuccessful, search backward from where we started,
607 (while (and (setq found (search-backward art-string nil t))
608 (not (nnmail-within-headers-p)))))
610 (nnmail-search-unix-mail-delim-backward))))
612 (defun nnfolder-delete-mail (&optional leave-delim)
613 "Delete the message that point is in.
614 If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
615 deleted. Point is left where the deleted region was."
619 ;; In case point is at the beginning of the message already.
621 (nnmail-search-unix-mail-delim-backward)
622 (if leave-delim (progn (forward-line 1) (point))
626 (if (nnmail-search-unix-mail-delim)
629 (run-hooks 'nnfolder-delete-mail-hook)
630 (delete-region (point-min) (point-max))))
632 (defun nnfolder-possibly-change-group (group &optional server dont-check)
635 (not (nnfolder-server-opened server)))
636 (nnfolder-open-server server))
637 (unless (gnus-buffer-live-p nnfolder-current-buffer)
638 (setq nnfolder-current-buffer nil
639 nnfolder-current-group nil))
641 (let ((file-name-coding-system nnmail-pathname-coding-system))
643 (not (equal group nnfolder-current-group))
645 (nnmail-activate 'nnfolder)
646 (and (assoc group nnfolder-group-alist)
647 (file-exists-p (nnfolder-group-pathname group)))))
649 (setq nnfolder-current-group group
650 nnfolder-current-buffer nil)
652 ;; If we have to change groups, see if we don't already have
653 ;; the folder in memory. If we do, verify the modtime and
654 ;; destroy the folder if needed so we can rescan it.
655 (setq nnfolder-current-buffer
656 (nth 1 (assoc group nnfolder-buffer-alist)))
658 ;; If the buffer is not live, make sure it isn't in the
659 ;; alist. If it is live, verify that nobody else has
660 ;; touched the file since last time.
661 (when (and nnfolder-current-buffer
662 (not (gnus-buffer-live-p nnfolder-current-buffer)))
663 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
664 nnfolder-current-buffer nil))
666 (setq nnfolder-current-group group)
668 (when (or (not nnfolder-current-buffer)
669 (not (verify-visited-file-modtime
670 nnfolder-current-buffer)))
672 (setq file (nnfolder-group-pathname group))
673 ;; See whether we need to create the new file.
674 (unless (file-exists-p file)
675 (gnus-make-directory (file-name-directory file))
676 (let ((nnmail-file-coding-system
677 (or nnfolder-file-coding-system-for-write
678 nnfolder-file-coding-system-for-write)))
679 (nnmail-write-region 1 1 file t 'nomesg)))
680 (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
681 (set-buffer nnfolder-current-buffer)
682 (push (list group nnfolder-current-buffer)
683 nnfolder-buffer-alist)))))))))
685 (defun nnfolder-save-mail (group-art-list)
686 "Called narrowed to an article."
687 (let* (save-list group-art)
688 (goto-char (point-min))
689 ;; The From line may have been quoted by movemail.
690 (when (looking-at ">From")
692 ;; This might come from somewhere else.
693 (unless (looking-at "From ")
694 (insert "From nobody " (current-time-string) "\n")
695 (goto-char (point-min)))
696 ;; Quote all "From " lines in the article.
698 (let (case-fold-search)
699 (while (re-search-forward "^From " nil t)
702 (setq save-list group-art-list)
703 (nnmail-insert-lines)
704 (nnmail-insert-xref group-art-list)
705 (run-hooks 'nnmail-prepare-save-mail-hook)
706 (run-hooks 'nnfolder-prepare-save-mail-hook)
708 ;; Insert the mail into each of the destination groups.
709 (while (setq group-art (pop group-art-list))
710 ;; Kill any previous newsgroup markers.
711 (goto-char (point-min))
712 (if (search-forward "\n\n" nil t)
714 (goto-char (point-max)))
715 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
716 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
718 ;; Insert the new newsgroup marker.
719 (nnfolder-insert-newsgroup-line group-art)
722 (let ((beg (point-min))
724 (obuf (current-buffer)))
725 (nnfolder-possibly-change-folder (car group-art))
726 (let ((buffer-read-only nil))
727 (nnfolder-normalize-buffer)
728 (insert-buffer-substring obuf beg end))
729 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
731 (nnfolder-add-nov (car group-art) (cdr group-art)
732 (nnfolder-parse-head nil beg end))))))
734 ;; Did we save it anywhere?
737 (defun nnfolder-normalize-buffer ()
738 "Make sure there are two newlines at the end of the buffer."
739 (goto-char (point-max))
740 (skip-chars-backward "\n")
741 (delete-region (point) (point-max))
744 (defun nnfolder-insert-newsgroup-line (group-art)
746 (goto-char (point-min))
747 (unless (search-forward "\n\n" nil t)
748 (goto-char (point-max))
751 (insert (format (concat nnfolder-article-marker "%d %s\n")
752 (cdr group-art) (current-time-string)))))
754 (defun nnfolder-active-number (group)
755 ;; Find the next article number in GROUP.
756 (let ((active (cadr (assoc group nnfolder-group-alist))))
758 (setcdr active (1+ (cdr active)))
759 ;; This group is new, so we create a new entry for it.
760 ;; This might be a bit naughty... creating groups on the drop of
761 ;; a hat, but I don't know...
762 (push (list group (setq active (cons 1 1)))
763 nnfolder-group-alist))
766 (defun nnfolder-possibly-change-folder (group)
767 (let ((inf (assoc group nnfolder-buffer-alist)))
769 (gnus-buffer-live-p (cadr inf)))
770 (set-buffer (cadr inf))
772 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
773 (when nnfolder-group-alist
774 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
775 (push (list group (nnfolder-read-folder group))
776 nnfolder-buffer-alist))))
778 ;; This method has a problem if you've accidentally let the active
779 ;; list get out of sync with the files. This could happen, say, if
780 ;; you've accidentally gotten new mail with something other than Gnus
781 ;; (but why would _that_ ever happen? :-). In that case, we will be
782 ;; in the middle of processing the file, ready to add new X-Gnus
783 ;; article number markers, and we'll run across a message with no ID
784 ;; yet - the active list _may_not_ be ready for us yet.
786 ;; To handle this, I'm modifying this routine to maintain the maximum
787 ;; ID seen so far, and when we hit a message with no ID, we will
788 ;; _manually_ scan the rest of the message looking for any more,
789 ;; possibly higher IDs. We'll assume the maximum that we find is the
790 ;; highest active. Note that this shouldn't cost us much extra time
791 ;; at all, but will be a lot less vulnerable to glitches between the
792 ;; mbox and the active file.
794 (defun nnfolder-read-folder (group)
795 (let* ((file (nnfolder-group-pathname group))
796 (nov (nnfolder-group-nov-pathname group))
798 (let ((nnheader-file-coding-system
799 nnfolder-file-coding-system))
800 (nnheader-find-file-noselect file)))))
801 (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
802 (if (equal (cadr (assoc group nnfolder-scantime-alist))
803 (nth 5 (file-attributes file)))
804 ;; This looks up-to-date, so we don't do any scanning.
805 (if (file-exists-p file)
807 (push (list group buffer) nnfolder-buffer-alist)
808 (set-buffer-modified-p t)
809 (nnfolder-save-buffer))
810 ;; Parse the damn thing.
812 (goto-char (point-min))
813 ;; Remove any blank lines at the start.
814 (while (eq (following-char) ?\n)
816 (nnmail-activate 'nnfolder)
818 (let ((delim "^From ")
819 (marker (concat "\n" nnfolder-article-marker))
821 (active (or (cadr (assoc group nnfolder-group-alist))
823 (scantime (assoc group nnfolder-scantime-alist))
825 maxid start end newscantime
826 novbuf articles newnum
828 (buffer-disable-undo)
829 (setq maxid (cdr active))
831 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
832 (and (file-exists-p nov)
833 (file-newer-than-file-p nov file)))
834 (unless (file-exists-p nov)
835 (gnus-make-directory (file-name-directory nov)))
837 (setq novbuf (nnfolder-open-nov group))
838 (goto-char (point-min))
840 (push (read novbuf) articles)
842 (setq articles (nreverse articles))))
843 (goto-char (point-min))
845 ;; Anytime the active number is 1 or 0, it is suspect. In
846 ;; that case, search the file manually to find the active
847 ;; number. Or, of course, if we're being paranoid. (This
848 ;; would also be the place to build other lists from the
849 ;; header markers, such as expunge lists, etc., if we ever
850 ;; desired to abandon the active file entirely for mboxes.)
851 (when (or nnfolder-ignore-active-file
854 (while (and (search-forward marker nil t)
856 (setq newnum (string-to-number (match-string 0)))
857 (when (nnmail-within-headers-p)
858 (setq maxid (max maxid newnum)
859 minid (min minid newnum))
861 (if (memq newnum articles)
862 (setq articles (delq newnum articles))
863 (let ((headers (nnfolder-parse-head newnum)))
864 (with-current-buffer novbuf
865 (nnheader-find-nov-line newnum)
866 (nnheader-insert-nov headers)))))))
867 (when (and novbuf articles)
868 (with-current-buffer novbuf
869 (dolist (article articles)
870 (when (nnheader-find-nov-line article)
871 (delete-region (point)
872 (progn (forward-line 1) (point)))))))
873 (setcar active (max 1 (min minid maxid)))
874 (setcdr active (max maxid (cdr active)))
875 (goto-char (point-min)))
877 ;; As long as we trust that the user will only insert
878 ;; unmarked mail at the end, go to the end and search
879 ;; backwards for the last marker. Find the start of that
880 ;; message, and begin to search for unmarked messages from
882 (when (not (or nnfolder-distrust-mbox
884 (goto-char (point-max))
885 (unless (re-search-backward marker nil t)
886 (goto-char (point-min)))
887 ;;(when (nnmail-search-unix-mail-delim)
888 ;; (goto-char (point-min)))
891 ;; Keep track of the active number on our own, and insert it
892 ;; back into the active list when we're done. Also, prime
893 ;; the pump to cut down on the number of searches we do.
894 (unless (nnmail-search-unix-mail-delim)
895 (goto-char (point-max)))
896 (setq end (point-marker))
897 (while (not (= end (point-max)))
898 (setq start (marker-position end))
900 ;; There may be more than one "From " line, so we skip past
902 (while (looking-at delim)
904 (set-marker end (if (nnmail-search-unix-mail-delim)
908 (when (not (search-forward marker end t))
909 (narrow-to-region start end)
910 (nnmail-insert-lines)
911 (nnfolder-insert-newsgroup-line
914 (nnfolder-active-number nnfolder-current-group))))
916 (let ((headers (nnfolder-parse-head newnum (point-min)
918 (with-current-buffer novbuf
919 (goto-char (point-max))
920 (nnheader-insert-nov headers))))
924 ;; Make absolutely sure that the active list reflects
926 (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
928 ;; Set the scantime for this group.
929 (setq newscantime (visited-file-modtime))
931 (setcdr scantime (list newscantime))
932 (push (list nnfolder-current-group newscantime)
933 nnfolder-scantime-alist))
937 (current-buffer))))))
940 (defun nnfolder-generate-active-file ()
941 "Look for mbox folders in the nnfolder directory and make them into groups.
942 This command does not work if you use short group names."
944 (nnmail-activate 'nnfolder)
945 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
946 (dolist (file (directory-files (or nnfolder-nov-directory
950 (regexp-quote nnfolder-nov-file-suffix)
952 (when (not (message-mail-file-mbox-p file))
954 (delete-file file)))))
955 (let ((files (directory-files nnfolder-directory))
957 (while (setq file (pop files))
958 (when (and (not (backup-file-name-p file))
959 (message-mail-file-mbox-p
960 (nnheader-concat nnfolder-directory file)))
961 (let ((oldgroup (assoc file nnfolder-group-alist)))
963 (nnheader-message 5 "Refreshing group %s..." file)
964 (nnheader-message 5 "Adding group %s..." file))
966 (setq nnfolder-group-alist
967 (delq oldgroup (copy-sequence nnfolder-group-alist))))
968 (push (list file (cons 1 0)) nnfolder-group-alist)
969 (nnfolder-possibly-change-folder file)
970 (nnfolder-possibly-change-group file)
971 (nnfolder-close-group file))))
972 (nnheader-message 5 "")))
974 (defun nnfolder-group-pathname (group)
975 "Make pathname for GROUP."
977 (mm-encode-coding-string group nnmail-pathname-coding-system))
978 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
979 ;; If this file exists, we use it directly.
980 (if (or nnmail-use-long-file-names
981 (file-exists-p (concat dir group)))
983 ;; If not, we translate dots into slashes.
984 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
986 (defun nnfolder-group-nov-pathname (group)
987 "Make pathname for GROUP NOV."
988 (let ((nnfolder-directory
989 (or nnfolder-nov-directory nnfolder-directory)))
990 (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
992 (defun nnfolder-save-buffer ()
994 (when (buffer-modified-p)
995 (run-hooks 'nnfolder-save-buffer-hook)
996 (gnus-make-directory (file-name-directory (buffer-file-name)))
997 (let ((coding-system-for-write
998 (or nnfolder-file-coding-system-for-write
999 nnfolder-file-coding-system)))
1001 (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
1002 (nnfolder-save-nov)))
1004 (defun nnfolder-save-active (group-alist active-file)
1005 (let ((nnmail-active-file-coding-system
1006 (or nnfolder-active-file-coding-system-for-write
1007 nnfolder-active-file-coding-system)))
1008 (nnmail-save-active group-alist active-file)))
1010 (defun nnfolder-open-nov (group)
1011 (or (cdr (assoc group nnfolder-nov-buffer-alist))
1012 (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
1015 (set (make-local-variable 'nnfolder-nov-buffer-file-name)
1016 (nnfolder-group-nov-pathname group))
1018 (when (file-exists-p nnfolder-nov-buffer-file-name)
1019 (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
1020 (push (cons group buffer) nnfolder-nov-buffer-alist)
1023 (defun nnfolder-save-nov ()
1025 (while nnfolder-nov-buffer-alist
1026 (when (buffer-name (cdar nnfolder-nov-buffer-alist))
1027 (set-buffer (cdar nnfolder-nov-buffer-alist))
1028 (when (buffer-modified-p)
1029 (gnus-make-directory (file-name-directory
1030 nnfolder-nov-buffer-file-name))
1031 (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
1033 (set-buffer-modified-p nil)
1034 (kill-buffer (current-buffer)))
1035 (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
1037 (defun nnfolder-nov-delete-article (group article)
1039 (set-buffer (nnfolder-open-nov group))
1040 (when (nnheader-find-nov-line article)
1041 (delete-region (point) (progn (forward-line 1) (point))))
1044 (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
1045 (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
1047 (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
1048 (when (file-exists-p nov)
1050 (set-buffer nntp-server-buffer)
1052 (nnheader-insert-file-contents nov)
1054 (not (numberp fetch-old)))
1055 t ; Don't remove anything.
1056 (nnheader-nov-delete-outside-range
1057 (if fetch-old (max 1 (- (car articles) fetch-old))
1059 (car (last articles)))
1062 (defun nnfolder-parse-head (&optional number b e)
1063 "Parse the head of the current buffer."
1064 (let ((buf (current-buffer))
1068 (setq b (if (nnmail-search-unix-mail-delim-backward)
1069 (point) (point-min)))
1071 (setq e (if (nnmail-search-unix-mail-delim)
1072 (point) (point-max))))
1073 (setq chars (- e b))
1074 (unless (zerop chars)
1076 (if (search-forward "\n\n" e t) (setq e (1- (point)))))
1078 (insert-buffer-substring buf b e)
1079 ;; Fold continuation lines.
1080 (goto-char (point-min))
1081 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
1082 (replace-match " " t t))
1083 ;; Remove any tabs; they are too confusing.
1084 (subst-char-in-region (point-min) (point-max) ?\t ? )
1085 (let ((headers (nnheader-parse-head t)))
1086 (mail-header-set-chars headers chars)
1087 (mail-header-set-number headers number)
1090 (defun nnfolder-add-nov (group article headers)
1091 "Add a nov line for the GROUP base."
1093 (set-buffer (nnfolder-open-nov group))
1094 (goto-char (point-max))
1095 (mail-header-set-number headers article)
1096 (nnheader-insert-nov headers)))
1100 ;;; nnfolder.el ends here