1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
4 ;; Author: Scott Byer <byer@mv.us.adobe.com>
5 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; Keywords: news, mail
9 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; For an overview of what the interface functions do, please see the
31 ;; Various enhancements by byer@mv.us.adobe.com (Scott Byer).
39 (eval-when-compile (require 'cl))
41 (nnoo-declare nnfolder)
43 (defvoo nnfolder-directory (expand-file-name "~/Mail/")
44 "The name of the nnfolder directory.")
46 (defvoo nnfolder-active-file
47 (concat (file-name-as-directory nnfolder-directory) "active")
48 "The name of the active file.")
50 ;; I renamed this variable to something more in keeping with the general GNU
53 (defvoo nnfolder-ignore-active-file nil
54 "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.
55 Note that the active file is still saved, but it's values are not
56 used. This costs some extra time when scanning an mbox when opening
59 (defvoo nnfolder-newsgroups-file
60 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
61 "Mail newsgroups description file.")
63 (defvoo nnfolder-get-new-mail t
64 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
66 (defvoo nnfolder-prepare-save-mail-hook nil
67 "Hook run narrowed to an article before saving.")
69 (defvoo nnfolder-inhibit-expiry nil
70 "If non-nil, inhibit expiry.")
74 (defconst nnfolder-version "nnfolder 1.0"
77 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
78 "String used to demarcate what the article number for a message is.")
80 (defvoo nnfolder-current-group nil)
81 (defvoo nnfolder-current-buffer nil)
82 (defvoo nnfolder-status-string "")
83 (defvoo nnfolder-group-alist nil)
84 (defvoo nnfolder-buffer-alist nil)
85 (defvoo nnfolder-active-timestamp nil)
89 ;;; Interface functions
91 (nnoo-define-basics nnfolder)
93 (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
95 (set-buffer nntp-server-buffer)
97 (let ((delim-string (concat "^" message-unix-mail-delimiter))
98 article art-string start stop)
99 (nnfolder-possibly-change-group group server)
100 (set-buffer nnfolder-current-buffer)
101 (goto-char (point-min))
102 (if (stringp (car articles))
105 (setq article (car articles))
106 (setq art-string (nnfolder-article-string article))
107 (set-buffer nnfolder-current-buffer)
108 (if (or (search-forward art-string nil t)
109 ;; Don't search the whole file twice! Also, articles
110 ;; probably have some locality by number, so searching
111 ;; backwards will be faster. Especially if we're at the
112 ;; beginning of the buffer :-). -SLB
113 (search-backward art-string nil t))
115 (setq start (or (re-search-backward delim-string nil t)
117 (search-forward "\n\n" nil t)
118 (setq stop (1- (point)))
119 (set-buffer nntp-server-buffer)
120 (insert (format "221 %d Article retrieved.\n" article))
121 (insert-buffer-substring nnfolder-current-buffer start stop)
122 (goto-char (point-max))
124 (setq articles (cdr articles)))
126 (set-buffer nntp-server-buffer)
127 (nnheader-fold-continuation-lines)
130 (deffoo nnfolder-open-server (server &optional defs)
131 (nnoo-change-server 'nnfolder server defs)
132 (when (not (file-exists-p nnfolder-directory))
134 (make-directory nnfolder-directory t)
137 ((not (file-exists-p nnfolder-directory))
138 (nnfolder-close-server)
139 (nnheader-report 'nnfolder "Couldn't create directory: %s"
141 ((not (file-directory-p (file-truename nnfolder-directory)))
142 (nnfolder-close-server)
143 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
145 (nnheader-report 'nnfolder "Opened server %s using directory %s"
146 server nnfolder-directory)
149 (deffoo nnfolder-request-close ()
150 (let ((alist nnfolder-buffer-alist))
152 (nnfolder-close-group (caar alist) nil t)
153 (setq alist (cdr alist))))
154 (nnoo-close-server 'nnfolder)
155 (setq nnfolder-buffer-alist nil
156 nnfolder-group-alist nil))
158 (deffoo nnfolder-request-article (article &optional group server buffer)
159 (nnfolder-possibly-change-group group server)
161 (set-buffer nnfolder-current-buffer)
162 (goto-char (point-min))
163 (if (search-forward (nnfolder-article-string article) nil t)
165 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
168 (or (and (re-search-forward
169 (concat "^" message-unix-mail-delimiter) nil t)
171 (goto-char (point-max)))
173 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
174 (set-buffer nntp-server-buffer)
176 (insert-buffer-substring nnfolder-current-buffer start stop)
177 (goto-char (point-min))
178 (while (looking-at "From ")
180 (insert "X-From-Line: ")
182 (if (numberp article)
183 (cons nnfolder-current-group article)
184 (goto-char (point-min))
185 (search-forward (concat "\n" nnfolder-article-marker))
186 (cons nnfolder-current-group
189 (point) (progn (end-of-line) (point)))))))))))
191 (deffoo nnfolder-request-group (group &optional server dont-check)
193 (nnmail-activate 'nnfolder)
194 (if (not (assoc group nnfolder-group-alist))
195 (nnheader-report 'nnfolder "No such group: %s" group)
196 (nnfolder-possibly-change-group group server)
199 (nnheader-report 'nnfolder "Selected group %s" group)
201 (let* ((active (assoc group nnfolder-group-alist))
203 (range (cadr active)))
206 (nnheader-report 'nnfolder "No such group: %s" group))
208 (nnheader-report 'nnfolder "Selected group %s" group)
209 (nnheader-insert "211 %d %d %d %s\n"
210 (1+ (- (cdr range) (car range)))
211 (car range) (cdr range) group))))))))
213 (deffoo nnfolder-request-scan (&optional group server)
214 (nnfolder-possibly-change-group group server)
218 (let ((bufs nnfolder-buffer-alist))
221 (if (not (buffer-name (nth 1 (car bufs))))
222 (setq nnfolder-buffer-alist
223 (delq (car bufs) nnfolder-buffer-alist))
224 (set-buffer (nth 1 (car bufs)))
225 (and (buffer-modified-p) (save-buffer))
226 (kill-buffer (current-buffer)))
227 (setq bufs (cdr bufs))))))
231 ;; Don't close the buffer if we're not shutting down the server. This way,
232 ;; we can keep the buffer in the group buffer cache, and not have to grovel
233 ;; over the buffer again unless we add new mail to it or modify it in some
236 (deffoo nnfolder-close-group (group &optional server force)
237 ;; Make sure we _had_ the group open.
238 (when (or (assoc group nnfolder-buffer-alist)
239 (equal group nnfolder-current-group))
240 (nnfolder-possibly-change-group group server)
241 (when nnfolder-current-buffer
243 (set-buffer nnfolder-current-buffer)
244 ;; If the buffer was modified, write the file out now.
245 (and (buffer-modified-p) (save-buffer))
246 ;; If we're shutting the server down, we need to kill the
247 ;; buffer and remove it from the open buffer list. Or, of
248 ;; course, if we're trying to minimize our space impact.
249 (kill-buffer (current-buffer))
250 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
251 nnfolder-buffer-alist)))))
252 (setq nnfolder-current-group nil
253 nnfolder-current-buffer nil)
256 (deffoo nnfolder-request-create-group (group &optional server)
257 (nnfolder-possibly-change-group nil server)
258 (nnmail-activate 'nnfolder)
260 (unless (assoc group nnfolder-group-alist)
261 (push (list group (cons 1 0)) nnfolder-group-alist)
262 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
265 (deffoo nnfolder-request-list (&optional server)
266 (nnfolder-possibly-change-group nil server)
268 (nnmail-find-file nnfolder-active-file)
269 (setq nnfolder-group-alist (nnmail-get-active))))
271 (deffoo nnfolder-request-newgroups (date &optional server)
272 (nnfolder-possibly-change-group nil server)
273 (nnfolder-request-list server))
275 (deffoo nnfolder-request-list-newsgroups (&optional server)
276 (nnfolder-possibly-change-group nil server)
278 (nnmail-find-file nnfolder-newsgroups-file)))
280 (deffoo nnfolder-request-expire-articles
281 (articles newsgroup &optional server force)
282 (nnfolder-possibly-change-group newsgroup server)
285 (nnmail-activate 'nnfolder)
288 (set-buffer nnfolder-current-buffer)
289 (while (and articles is-old)
290 (goto-char (point-min))
291 (if (search-forward (nnfolder-article-string (car articles)) nil t)
293 (nnmail-expired-article-p
296 (point) (progn (end-of-line) (point)))
297 force nnfolder-inhibit-expiry))
299 (nnheader-message 5 "Deleting article %d..."
300 (car articles) newsgroup)
301 (nnfolder-delete-mail))
302 (setq rest (cons (car articles) rest))))
303 (setq articles (cdr articles)))
304 (and (buffer-modified-p) (save-buffer))
305 ;; Find the lowest active article in this group.
306 (let* ((active (cadr (assoc newsgroup nnfolder-group-alist)))
307 (marker (concat "\n" nnfolder-article-marker))
309 (activemin (cdr active)))
310 (goto-char (point-min))
311 (while (and (search-forward marker nil t)
312 (re-search-forward number nil t))
313 (setq activemin (min activemin
314 (string-to-number (buffer-substring
317 (setcar active activemin))
318 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
319 (nconc rest articles))))
321 (deffoo nnfolder-request-move-article
322 (article group server accept-form &optional last)
323 (nnfolder-possibly-change-group group server)
324 (let ((buf (get-buffer-create " *nnfolder move*"))
327 (nnfolder-request-article article group server)
330 (buffer-disable-undo (current-buffer))
332 (insert-buffer-substring nntp-server-buffer)
333 (goto-char (point-min))
334 (while (re-search-forward
335 (concat "^" nnfolder-article-marker)
336 (save-excursion (search-forward "\n\n" nil t) (point)) t)
337 (delete-region (progn (beginning-of-line) (point))
338 (progn (forward-line 1) (point))))
339 (setq result (eval accept-form))
343 (nnfolder-possibly-change-group group server)
344 (set-buffer nnfolder-current-buffer)
345 (goto-char (point-min))
346 (if (search-forward (nnfolder-article-string article) nil t)
347 (nnfolder-delete-mail))
353 (deffoo nnfolder-request-accept-article (group &optional server last)
354 (nnfolder-possibly-change-group group server)
355 (and (stringp group) (nnfolder-possibly-change-group group))
356 (let ((buf (current-buffer))
358 (goto-char (point-min))
359 (when (looking-at "X-From-Line: ")
360 (replace-match "From "))
362 (nnfolder-request-list)
365 (goto-char (point-min))
366 (search-forward "\n\n" nil t)
368 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
369 (delete-region (point) (progn (forward-line 1) (point))))
370 (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
372 (set-buffer nnfolder-current-buffer)
373 (and last (buffer-modified-p) (save-buffer))))
374 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
376 (nnheader-report 'nnfolder "Couldn't store article"))
379 (deffoo nnfolder-request-replace-article (article group buffer)
380 (nnfolder-possibly-change-group group)
382 (set-buffer nnfolder-current-buffer)
383 (goto-char (point-min))
384 (if (not (search-forward (nnfolder-article-string article) nil t))
386 (nnfolder-delete-mail t t)
387 (insert-buffer-substring buffer)
388 (and (buffer-modified-p) (save-buffer))
391 (deffoo nnfolder-request-delete-group (group &optional force server)
392 (nnfolder-close-group group server t)
393 ;; Delete all articles in GROUP.
395 () ; Don't delete the articles.
396 ;; Delete the file that holds the group.
398 (delete-file (nnfolder-group-pathname group))
400 ;; Remove the group from all structures.
401 (setq nnfolder-group-alist
402 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
403 nnfolder-current-group nil
404 nnfolder-current-buffer nil)
405 ;; Save the active file.
406 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
409 (deffoo nnfolder-request-rename-group (group new-name &optional server)
410 (nnfolder-possibly-change-group group server)
412 (set-buffer nnfolder-current-buffer)
413 (and (file-writable-p buffer-file-name)
418 (nnfolder-group-pathname new-name))
421 ;; That went ok, so we change the internal structures.
422 (let ((entry (assoc group nnfolder-group-alist)))
423 (and entry (setcar entry new-name))
424 (setq nnfolder-current-buffer nil
425 nnfolder-current-group nil)
426 ;; Save the new group alist.
427 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
428 ;; We kill the buffer instead of renaming it and stuff.
429 (kill-buffer (current-buffer))
433 ;;; Internal functions.
435 (defun nnfolder-article-string (article)
436 (if (numberp article)
437 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
438 (concat "\nMessage-ID: " article)))
440 (defun nnfolder-delete-mail (&optional force leave-delim)
441 "Delete the message that point is in."
445 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
446 (if leave-delim (progn (forward-line 1) (point))
447 (match-beginning 0)))
450 (if (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
451 (if (and (not (bobp)) leave-delim)
452 (progn (forward-line -2) (point))
456 (defun nnfolder-possibly-change-group (group &optional server)
458 (not (nnfolder-server-opened server)))
459 (nnfolder-open-server server))
461 (unless (file-exists-p nnfolder-directory)
462 (make-directory (directory-file-name nnfolder-directory) t))
463 (nnfolder-possibly-activate-groups nil)
464 (or (assoc group nnfolder-group-alist)
466 (nnfolder-group-pathname group)))
468 (setq nnfolder-group-alist
469 (cons (list group (cons 1 0)) nnfolder-group-alist))
470 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
472 (if (and (equal group nnfolder-current-group)
473 nnfolder-current-buffer
474 (buffer-name nnfolder-current-buffer))
476 (setq nnfolder-current-group group)
478 ;; If we have to change groups, see if we don't already have the mbox
479 ;; in memory. If we do, verify the modtime and destroy the mbox if
480 ;; needed so we can rescan it.
481 (if (setq inf (assoc group nnfolder-buffer-alist))
482 (setq nnfolder-current-buffer (nth 1 inf)))
484 ;; If the buffer is not live, make sure it isn't in the alist. If it
485 ;; is live, verify that nobody else has touched the file since last
487 (if (or (not (and nnfolder-current-buffer
488 (buffer-name nnfolder-current-buffer)))
489 (not (and (bufferp nnfolder-current-buffer)
490 (verify-visited-file-modtime
491 nnfolder-current-buffer))))
493 (if (and nnfolder-current-buffer
494 (buffer-name nnfolder-current-buffer)
495 (bufferp nnfolder-current-buffer))
496 (kill-buffer nnfolder-current-buffer))
497 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
503 (setq file (nnfolder-group-pathname group))
504 (if (file-directory-p (file-truename file))
506 (unless (file-exists-p file)
507 (unless (file-exists-p (file-name-directory file))
508 (make-directory (file-name-directory file) t))
509 (write-region 1 1 file t 'nomesg))
510 (setq nnfolder-current-buffer
511 (set-buffer (nnfolder-read-folder file)))
512 (setq nnfolder-buffer-alist (cons (list group (current-buffer))
513 nnfolder-buffer-alist)))))))
514 (setq nnfolder-current-group group)))
516 (defun nnfolder-save-mail (&optional group)
517 "Called narrowed to an article."
518 (let* ((nnmail-split-methods
519 (if group (list (list group "")) nnmail-split-methods))
521 (nreverse (nnmail-article-group 'nnfolder-active-number)))
522 (delim (concat "^" message-unix-mail-delimiter))
524 (goto-char (point-min))
525 ;; This might come from somewhere else.
526 (unless (looking-at delim)
527 (insert "From nobody " (current-time-string) "\n")
528 (goto-char (point-min)))
529 ;; Quote all "From " lines in the article.
531 (while (re-search-forward delim nil t)
534 (setq save-list group-art-list)
535 (nnmail-insert-lines)
536 (nnmail-insert-xref group-art-list)
537 (run-hooks 'nnmail-prepare-save-mail-hook)
538 (run-hooks 'nnfolder-prepare-save-mail-hook)
540 ;; Insert the mail into each of the destination groups.
541 (while group-art-list
542 (setq group-art (car group-art-list)
543 group-art-list (cdr group-art-list))
545 ;; Kill the previous newsgroup markers.
546 (goto-char (point-min))
547 (search-forward "\n\n" nil t)
549 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
550 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
552 (nnfolder-possibly-change-group (car group-art))
553 ;; Insert the new newsgroup marker.
554 (nnfolder-insert-newsgroup-line group-art)
555 (unless nnfolder-current-buffer
556 (nnfolder-request-create-group (car group-art))
557 (nnfolder-possibly-change-group (car group-art)))
558 (let ((beg (point-min))
560 (obuf (current-buffer)))
561 (set-buffer nnfolder-current-buffer)
562 (goto-char (point-max))
563 (insert-buffer-substring obuf beg end)
566 ;; Did we save it anywhere?
569 (defun nnfolder-insert-newsgroup-line (group-art)
571 (goto-char (point-min))
572 (if (search-forward "\n\n" nil t)
575 (insert (format (concat nnfolder-article-marker "%d %s\n")
576 (cdr group-art) (current-time-string)))))))
578 (defun nnfolder-possibly-activate-groups (&optional group)
580 ;; If we're looking for the activation of a specific group, find out
581 ;; its real name and switch to it.
582 (if group (nnfolder-possibly-change-group group))
583 ;; If the group alist isn't active, activate it now.
584 (nnmail-activate 'nnfolder)))
586 (defun nnfolder-active-number (group)
589 ;; Find the next article number in GROUP.
591 (let ((active (cadr (assoc group nnfolder-group-alist))))
593 (setcdr active (1+ (cdr active)))
594 ;; This group is new, so we create a new entry for it.
595 ;; This might be a bit naughty... creating groups on the drop of
596 ;; a hat, but I don't know...
597 (setq nnfolder-group-alist
598 (cons (list group (setq active (cons 1 1)))
599 nnfolder-group-alist)))
601 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
602 (nnfolder-possibly-activate-groups group)))))
605 ;; This method has a problem if you've accidentally let the active list get
606 ;; out of sync with the files. This could happen, say, if you've
607 ;; accidentally gotten new mail with something other than Gnus (but why
608 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
609 ;; processing the file, ready to add new X-Gnus article number markers, and
610 ;; we'll run across a message with no ID yet - the active list _may_not_ be
613 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
614 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
615 ;; rest of the message looking for any more, possibly higher IDs. We'll
616 ;; assume the maximum that we find is the highest active. Note that this
617 ;; shouldn't cost us much extra time at all, but will be a lot less
618 ;; vulnerable to glitches between the mbox and the active file.
620 (defun nnfolder-read-folder (file)
622 (nnfolder-possibly-activate-groups nil)
623 ;; We should be paranoid here and make sure the group is in the alist,
624 ;; and add it if it isn't.
625 ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
626 (set-buffer (setq nnfolder-current-buffer
627 (nnheader-find-file-noselect file nil 'raw)))
628 (buffer-disable-undo (current-buffer))
629 (let* ((delim (concat "^" message-unix-mail-delimiter))
630 (marker (concat "\n" nnfolder-article-marker))
632 (active (cadr (assoc nnfolder-current-group
633 nnfolder-group-alist)))
634 ;; Set min to Big Number.
635 (min (max (1- (lsh 1 23)) (1- (lsh 1 24)) (1- (lsh 1 25))))
638 (goto-char (point-min))
640 ;; Anytime the active number is 1 or 0, it is suspect. In that case,
641 ;; search the file manually to find the active number. Or, of course,
642 ;; if we're being paranoid. (This would also be the place to build
643 ;; other lists from the header markers, such as expunge lists, etc., if
644 ;; we ever desired to abandon the active file entirely for mboxes.)
645 (when (or nnfolder-ignore-active-file
647 (while (and (search-forward marker nil t)
648 (re-search-forward number nil t))
649 (let ((newnum (string-to-number (match-string 0))))
650 (setq max (max max newnum))
651 (setq min (min min newnum))))
652 (setcar active (max 1 (min min max)))
653 (setcdr active (max max (cdr active)))
654 (goto-char (point-min)))
656 ;; Keep track of the active number on our own, and insert it back into
657 ;; the active list when we're done. Also, prime the pump to cut down on
658 ;; the number of searches we do.
659 (setq end (point-marker))
660 (set-marker end (or (and (re-search-forward delim nil t)
663 (while (not (= end (point-max)))
664 (setq start (marker-position end))
666 ;; There may be more than one "From " line, so we skip past
668 (while (looking-at delim)
670 (set-marker end (or (and (re-search-forward delim nil t)
674 (if (not (search-forward marker end t))
676 (narrow-to-region start end)
677 (nnmail-insert-lines)
678 (nnfolder-insert-newsgroup-line
679 (cons nil (nnfolder-active-number nnfolder-current-group)))
682 ;; Make absolutely sure that the active list reflects reality!
683 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
687 (defun nnfolder-generate-active-file ()
688 "Look for mbox folders in the nnfolder directory and make them into groups."
690 (nnmail-activate 'nnfolder)
691 (let ((files (directory-files nnfolder-directory))
693 (while (setq file (pop files))
694 (when (nnheader-mail-file-mbox-p file)
695 (nnheader-message 5 "Adding group %s..." file)
696 (push (list file (cons 1 0)) nnfolder-group-alist)
697 (nnfolder-read-folder file)
698 (nnfolder-close-group file))
701 (defun nnfolder-group-pathname (group)
702 "Make pathname for GROUP."
703 (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
704 ;; If this file exists, we use it directly.
705 (if (or nnmail-use-long-file-names
706 (file-exists-p (concat dir group)))
708 ;; If not, we translate dots into slashes.
709 (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
713 ;;; nnfolder.el ends here