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).
38 (eval-when-compile (require 'cl))
40 (defvar nnfolder-directory (expand-file-name "~/Mail/")
41 "The name of the nnfolder directory.")
43 (defvar nnfolder-active-file
44 (concat (file-name-as-directory nnfolder-directory) "active")
45 "The name of the active file.")
47 ;; I renamed this variable to something more in keeping with the general GNU
50 (defvar nnfolder-ignore-active-file nil
51 "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.
52 Note that the active file is still saved, but it's values are not
53 used. This costs some extra time when scanning an mbox when opening
56 (defvar nnfolder-newsgroups-file
57 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
58 "Mail newsgroups description file.")
60 (defvar nnfolder-get-new-mail t
61 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
63 (defvar nnfolder-prepare-save-mail-hook nil
64 "Hook run narrowed to an article before saving.")
66 (defvar nnfolder-inhibit-expiry nil
67 "If non-nil, inhibit expiry.")
71 (defconst nnfolder-version "nnfolder 1.0"
74 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
75 "String used to demarcate what the article number for a message is.")
77 (defvar nnfolder-current-group nil)
78 (defvar nnfolder-current-buffer nil)
79 (defvar nnfolder-status-string "")
80 (defvar nnfolder-group-alist nil)
81 (defvar nnfolder-buffer-alist nil)
82 (defvar nnfolder-active-timestamp nil)
86 (defvar nnfolder-current-server nil)
87 (defvar nnfolder-server-alist nil)
88 (defvar nnfolder-server-variables
89 `((nnfolder-directory ,nnfolder-directory)
90 (nnfolder-active-file ,nnfolder-active-file)
91 (nnfolder-newsgroups-file ,nnfolder-newsgroups-file)
92 (nnfolder-get-new-mail ,nnfolder-get-new-mail)
93 (nnfolder-inhibit-expiry ,nnfolder-inhibit-expiry)
94 (nnfolder-current-group nil)
95 (nnfolder-current-buffer nil)
96 (nnfolder-status-string "")
97 (nnfolder-group-alist nil)
98 (nnfolder-buffer-alist nil)
99 (nnfolder-active-timestamp nil)))
103 ;;; Interface functions
105 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server fetch-old)
107 (set-buffer nntp-server-buffer)
109 (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
110 article art-string start stop)
111 (nnfolder-possibly-change-group newsgroup server)
112 (set-buffer nnfolder-current-buffer)
113 (goto-char (point-min))
114 (if (stringp (car sequence))
117 (setq article (car sequence))
118 (setq art-string (nnfolder-article-string article))
119 (set-buffer nnfolder-current-buffer)
120 (if (or (search-forward art-string nil t)
121 ;; Don't search the whole file twice! Also, articles
122 ;; probably have some locality by number, so searching
123 ;; backwards will be faster. Especially if we're at the
124 ;; beginning of the buffer :-). -SLB
125 (search-backward art-string nil t))
127 (setq start (or (re-search-backward delim-string nil t)
129 (search-forward "\n\n" nil t)
130 (setq stop (1- (point)))
131 (set-buffer nntp-server-buffer)
132 (insert (format "221 %d Article retrieved.\n" article))
133 (insert-buffer-substring nnfolder-current-buffer start stop)
134 (goto-char (point-max))
136 (setq sequence (cdr sequence)))
138 (set-buffer nntp-server-buffer)
139 (nnheader-fold-continuation-lines)
142 (defun nnfolder-open-server (server &optional defs)
143 (nnheader-change-server 'nnfolder server defs)
144 (when (not (file-exists-p nnfolder-directory))
146 (make-directory nnfolder-directory t)
149 ((not (file-exists-p nnfolder-directory))
150 (nnfolder-close-server)
151 (nnheader-report 'nnfolder "Couldn't create directory: %s"
153 ((not (file-directory-p (file-truename nnfolder-directory)))
154 (nnfolder-close-server)
155 (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
157 (nnheader-report 'nnfolder "Opened server %s using directory %s"
158 server nnfolder-directory)
161 (defun nnfolder-close-server (&optional server)
162 (setq nnfolder-current-server nil
163 nnfolder-group-alist nil)
166 (defun nnfolder-server-opened (&optional server)
167 (and (equal server nnfolder-current-server)
169 (buffer-name nntp-server-buffer)))
171 (defun nnfolder-request-close ()
172 (let ((alist nnfolder-buffer-alist))
174 (nnfolder-close-group (car (car alist)) nil t)
175 (setq alist (cdr alist))))
176 (setq nnfolder-buffer-alist nil
177 nnfolder-current-server nil
178 nnfolder-group-alist nil))
180 (defun nnfolder-status-message (&optional server)
181 nnfolder-status-string)
183 (defun nnfolder-request-article (article &optional newsgroup server buffer)
184 (nnfolder-possibly-change-group newsgroup server)
186 (set-buffer nnfolder-current-buffer)
187 (goto-char (point-min))
188 (if (search-forward (nnfolder-article-string article) nil t)
190 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
193 (or (and (re-search-forward
194 (concat "^" rmail-unix-mail-delimiter) nil t)
196 (goto-char (point-max)))
198 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
199 (set-buffer nntp-server-buffer)
201 (insert-buffer-substring nnfolder-current-buffer start stop)
202 (goto-char (point-min))
203 (while (looking-at "From ")
205 (insert "X-From-Line: ")
207 (if (numberp article)
208 (cons nnfolder-current-group article)
209 (goto-char (point-min))
210 (search-forward (concat "\n" nnfolder-article-marker))
211 (cons nnfolder-current-group
214 (point) (progn (end-of-line) (point)))))))))))
216 (defun nnfolder-request-group (group &optional server dont-check)
218 (nnmail-activate 'nnfolder)
219 (if (not (assoc group nnfolder-group-alist))
220 (nnheader-report 'nnfolder "No such group: %s" group)
221 (nnfolder-possibly-change-group group server)
224 (nnheader-report 'nnfolder "Selected group %s" group)
226 (let* ((active (assoc group nnfolder-group-alist))
228 (range (car (cdr active)))
229 (minactive (car range))
230 (maxactive (cdr range)))
233 (nnheader-report 'nnfolder "No such group: %s" group))
235 (nnheader-report 'nnfolder "Selected group %s" group)
236 (nnheader-insert "211 %d %d %d %s\n"
237 (1+ (- maxactive minactive))
238 minactive maxactive group))))))))
240 (defun nnfolder-request-scan (&optional group server)
244 (let ((bufs nnfolder-buffer-alist))
247 (if (not (buffer-name (nth 1 (car bufs))))
248 (setq nnfolder-buffer-alist
249 (delq (car bufs) nnfolder-buffer-alist))
250 (set-buffer (nth 1 (car bufs)))
251 (and (buffer-modified-p) (save-buffer)))
252 (setq bufs (cdr bufs))))))
256 ;; Don't close the buffer if we're not shutting down the server. This way,
257 ;; we can keep the buffer in the group buffer cache, and not have to grovel
258 ;; over the buffer again unless we add new mail to it or modify it in some
261 (defun nnfolder-close-group (group &optional server force)
262 ;; Make sure we _had_ the group open.
263 (when (or (assoc group nnfolder-buffer-alist)
264 (equal group nnfolder-current-group))
265 (nnfolder-possibly-change-group group server)
267 (set-buffer nnfolder-current-buffer)
268 ;; If the buffer was modified, write the file out now.
269 (and (buffer-modified-p) (save-buffer))
270 ;; If we're shutting the server down, we need to kill the
271 ;; buffer and remove it from the open buffer list. Or, of
272 ;; course, if we're trying to minimize our space impact.
273 (kill-buffer (current-buffer))
274 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
275 nnfolder-buffer-alist))))
276 (setq nnfolder-current-group nil
277 nnfolder-current-buffer nil)
280 (defun nnfolder-request-create-group (group &optional server)
281 (nnmail-activate 'nnfolder)
282 (unless (assoc group nnfolder-group-alist)
283 (push (list group (cons 1 0)) nnfolder-group-alist)
284 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
287 (defun nnfolder-request-list (&optional server)
289 (nnmail-find-file nnfolder-active-file)
290 (setq nnfolder-group-alist (nnmail-get-active))))
292 (defun nnfolder-request-newgroups (date &optional server)
293 (nnfolder-request-list server))
295 (defun nnfolder-request-list-newsgroups (&optional server)
297 (nnmail-find-file nnfolder-newsgroups-file)))
299 (defun nnfolder-request-expire-articles
300 (articles newsgroup &optional server force)
301 (nnfolder-possibly-change-group newsgroup server)
304 (nnmail-activate 'nnfolder)
307 (set-buffer nnfolder-current-buffer)
308 (while (and articles is-old)
309 (goto-char (point-min))
310 (if (search-forward (nnfolder-article-string (car articles)) nil t)
312 (nnmail-expired-article-p
315 (point) (progn (end-of-line) (point)))
316 force nnfolder-inhibit-expiry))
318 (nnheader-message 5 "Deleting article %d..."
319 (car articles) newsgroup)
320 (nnfolder-delete-mail))
321 (setq rest (cons (car articles) rest))))
322 (setq articles (cdr articles)))
323 (and (buffer-modified-p) (save-buffer))
324 ;; Find the lowest active article in this group.
325 (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
326 (marker (concat "\n" nnfolder-article-marker))
328 (activemin (cdr active)))
329 (goto-char (point-min))
330 (while (and (search-forward marker nil t)
331 (re-search-forward number nil t))
332 (setq activemin (min activemin
333 (string-to-number (buffer-substring
336 (setcar active activemin))
337 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
338 (nconc rest articles))))
340 (defun nnfolder-request-move-article
341 (article group server accept-form &optional last)
342 (nnfolder-possibly-change-group group server)
343 (let ((buf (get-buffer-create " *nnfolder move*"))
346 (nnfolder-request-article article group server)
349 (buffer-disable-undo (current-buffer))
351 (insert-buffer-substring nntp-server-buffer)
352 (goto-char (point-min))
353 (while (re-search-forward
354 (concat "^" nnfolder-article-marker)
355 (save-excursion (search-forward "\n\n" nil t) (point)) t)
356 (delete-region (progn (beginning-of-line) (point))
357 (progn (forward-line 1) (point))))
358 (setq result (eval accept-form))
362 (nnfolder-possibly-change-group group server)
363 (set-buffer nnfolder-current-buffer)
364 (goto-char (point-min))
365 (if (search-forward (nnfolder-article-string article) nil t)
366 (nnfolder-delete-mail))
372 (defun nnfolder-request-accept-article (group &optional last)
373 (and (stringp group) (nnfolder-possibly-change-group group))
374 (let ((buf (current-buffer))
376 (goto-char (point-min))
377 (when (looking-at "X-From-Line: ")
378 (replace-match "From "))
380 (nnfolder-request-list)
383 (goto-char (point-min))
384 (search-forward "\n\n" nil t)
386 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
387 (delete-region (point) (progn (forward-line 1) (point))))
388 (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
390 (set-buffer nnfolder-current-buffer)
391 (and last (buffer-modified-p) (save-buffer))))
392 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
395 (defun nnfolder-request-replace-article (article group buffer)
396 (nnfolder-possibly-change-group group)
398 (set-buffer nnfolder-current-buffer)
399 (goto-char (point-min))
400 (if (not (search-forward (nnfolder-article-string article) nil t))
402 (nnfolder-delete-mail t t)
403 (insert-buffer-substring buffer)
404 (and (buffer-modified-p) (save-buffer))
407 (defun nnfolder-request-delete-group (group &optional force server)
408 (nnfolder-close-group group server t)
409 ;; Delete all articles in GROUP.
411 () ; Don't delete the articles.
412 ;; Delete the file that holds the group.
414 (delete-file (concat (file-name-as-directory nnfolder-directory)
417 ;; Remove the group from all structures.
418 (setq nnfolder-group-alist
419 (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
420 nnfolder-current-group nil
421 nnfolder-current-buffer nil)
422 ;; Save the active file.
423 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
426 (defun nnfolder-request-rename-group (group new-name &optional server)
427 (nnfolder-possibly-change-group group server)
429 (set-buffer nnfolder-current-buffer)
430 (and (file-writable-p buffer-file-name)
433 (rename-file buffer-file-name
434 (concat (file-name-as-directory nnfolder-directory)
438 ;; That went ok, so we change the internal structures.
439 (let ((entry (assoc group nnfolder-group-alist)))
440 (and entry (setcar entry new-name))
441 (setq nnfolder-current-buffer nil
442 nnfolder-current-group nil)
443 ;; Save the new group alist.
444 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
445 ;; We kill the buffer instead of renaming it and stuff.
446 (kill-buffer (current-buffer))
450 ;;; Internal functions.
452 (defun nnfolder-article-string (article)
453 (if (numberp article)
454 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
455 (concat "\nMessage-ID: " article)))
457 (defun nnfolder-delete-mail (&optional force leave-delim)
458 ;; Beginning of the article.
463 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
464 (if leave-delim (progn (forward-line 1) (point))
465 (match-beginning 0)))
468 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
470 (if (and (not (bobp)) leave-delim)
471 (progn (forward-line -2) (point))
472 (match-beginning 0)))
474 (delete-region (point-min) (point-max)))))
476 (defun nnfolder-possibly-change-group (group &optional server)
478 (not (nnfolder-server-opened server)))
479 (nnfolder-open-server server))
480 (unless (file-exists-p nnfolder-directory)
481 (make-directory (directory-file-name nnfolder-directory) t))
482 (nnfolder-possibly-activate-groups nil)
483 (or (assoc group nnfolder-group-alist)
484 (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
487 (setq nnfolder-group-alist
488 (cons (list group (cons 1 0)) nnfolder-group-alist))
489 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
491 (if (and (equal group nnfolder-current-group)
492 nnfolder-current-buffer
493 (buffer-name nnfolder-current-buffer))
495 (setq nnfolder-current-group group)
497 ;; If we have to change groups, see if we don't already have the mbox
498 ;; in memory. If we do, verify the modtime and destroy the mbox if
499 ;; needed so we can rescan it.
500 (if (setq inf (assoc group nnfolder-buffer-alist))
501 (setq nnfolder-current-buffer (nth 1 inf)))
503 ;; If the buffer is not live, make sure it isn't in the alist. If it
504 ;; is live, verify that nobody else has touched the file since last
506 (if (or (not (and nnfolder-current-buffer
507 (buffer-name nnfolder-current-buffer)))
508 (not (and (bufferp nnfolder-current-buffer)
509 (verify-visited-file-modtime
510 nnfolder-current-buffer))))
512 (if (and nnfolder-current-buffer
513 (buffer-name nnfolder-current-buffer)
514 (bufferp nnfolder-current-buffer))
515 (kill-buffer nnfolder-current-buffer))
516 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
522 (setq file (concat (file-name-as-directory nnfolder-directory)
524 (if (file-directory-p (file-truename file))
526 (if (not (file-exists-p file))
527 (write-region 1 1 file t 'nomesg))
528 (setq nnfolder-current-buffer
529 (set-buffer (nnfolder-read-folder file)))
530 (setq nnfolder-buffer-alist (cons (list group (current-buffer))
531 nnfolder-buffer-alist)))))))
532 (setq nnfolder-current-group group))
534 (defun nnfolder-save-mail (&optional group)
535 "Called narrowed to an article."
536 (let* ((nnmail-split-methods
537 (if group (list (list group "")) nnmail-split-methods))
539 (nreverse (nnmail-article-group 'nnfolder-active-number)))
540 (delim (concat "^" rmail-unix-mail-delimiter))
542 (goto-char (point-min))
543 ;; This might come from somewhere else.
544 (unless (looking-at delim)
545 (insert "From nobody " (current-time-string) "\n")
546 (goto-char (point-min)))
547 ;; Quote all "From " lines in the article.
549 (while (re-search-forward delim nil t)
552 (setq save-list group-art-list)
553 (nnmail-insert-lines)
554 (nnmail-insert-xref group-art-list)
555 (run-hooks 'nnfolder-prepare-save-mail-hook)
557 ;; Insert the mail into each of the destination groups.
558 (while group-art-list
559 (setq group-art (car group-art-list)
560 group-art-list (cdr group-art-list))
562 ;; Kill the previous newsgroup markers.
563 (goto-char (point-min))
564 (search-forward "\n\n" nil t)
566 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
567 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
569 ;; Insert the new newsgroup marker.
570 (nnfolder-possibly-change-group (car group-art))
571 (nnfolder-insert-newsgroup-line group-art)
572 (let ((beg (point-min))
574 (obuf (current-buffer)))
575 (set-buffer nnfolder-current-buffer)
576 (goto-char (point-max))
577 (insert-buffer-substring obuf beg end)
580 ;; Did we save it anywhere?
583 (defun nnfolder-insert-newsgroup-line (group-art)
585 (goto-char (point-min))
586 (if (search-forward "\n\n" nil t)
589 (insert (format (concat nnfolder-article-marker "%d %s\n")
590 (cdr group-art) (current-time-string)))))))
592 (defun nnfolder-possibly-activate-groups (&optional group)
594 ;; If we're looking for the activation of a specific group, find out
595 ;; its real name and switch to it.
596 (if group (nnfolder-possibly-change-group group))
597 ;; If the group alist isn't active, activate it now.
598 (nnmail-activate 'nnfolder)))
600 (defun nnfolder-active-number (group)
602 ;; Find the next article number in GROUP.
604 (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
606 (setcdr active (1+ (cdr active)))
607 ;; This group is new, so we create a new entry for it.
608 ;; This might be a bit naughty... creating groups on the drop of
609 ;; a hat, but I don't know...
610 (setq nnfolder-group-alist
611 (cons (list group (setq active (cons 1 1)))
612 nnfolder-group-alist)))
614 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
615 (nnfolder-possibly-activate-groups group))))
618 ;; This method has a problem if you've accidentally let the active list get
619 ;; out of sync with the files. This could happen, say, if you've
620 ;; accidentally gotten new mail with something other than Gnus (but why
621 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
622 ;; processing the file, ready to add new X-Gnus article number markers, and
623 ;; we'll run across a message with no ID yet - the active list _may_not_ be
626 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
627 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
628 ;; rest of the message looking for any more, possibly higher IDs. We'll
629 ;; assume the maximum that we find is the highest active. Note that this
630 ;; shouldn't cost us much extra time at all, but will be a lot less
631 ;; vulnerable to glitches between the mbox and the active file.
633 (defun nnfolder-read-folder (file)
635 (nnfolder-possibly-activate-groups nil)
636 ;; We should be paranoid here and make sure the group is in the alist,
637 ;; and add it if it isn't.
638 ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
639 (set-buffer (setq nnfolder-current-buffer
640 (nnheader-find-file-noselect file nil 'raw)))
641 (buffer-disable-undo (current-buffer))
642 (let ((delim (concat "^" rmail-unix-mail-delimiter))
643 (marker (concat "\n" nnfolder-article-marker))
645 (active (car (cdr (assoc nnfolder-current-group
646 nnfolder-group-alist))))
647 activenumber activemin start end)
648 (goto-char (point-min))
650 ;; Anytime the active number is 1 or 0, it is suspect. In that case,
651 ;; search the file manually to find the active number. Or, of course,
652 ;; if we're being paranoid. (This would also be the place to build
653 ;; other lists from the header markers, such as expunge lists, etc., if
654 ;; we ever desired to abandon the active file entirely for mboxes.)
655 (setq activenumber (cdr active))
656 (if (or nnfolder-ignore-active-file
659 (setq activemin (max (1- (lsh 1 23))
662 (while (and (search-forward marker nil t)
663 (re-search-forward number nil t))
664 (let ((newnum (string-to-number (buffer-substring
667 (setq activenumber (max activenumber newnum))
668 (setq activemin (min activemin newnum))))
669 (setcar active (max 1 (min activemin activenumber)))
670 (setcdr active (max activenumber (cdr active)))
671 (goto-char (point-min))))
673 ;; Keep track of the active number on our own, and insert it back into
674 ;; the active list when we're done. Also, prime the pump to cut down on
675 ;; the number of searches we do.
676 (setq end (point-marker))
677 (set-marker end (or (and (re-search-forward delim nil t)
680 (while (not (= end (point-max)))
681 (setq start (marker-position end))
683 ;; There may be more than one "From " line, so we skip past
685 (while (looking-at delim)
687 (set-marker end (or (and (re-search-forward delim nil t)
691 (if (not (search-forward marker end t))
693 (narrow-to-region start end)
694 (nnmail-insert-lines)
695 (nnfolder-insert-newsgroup-line
696 (cons nil (nnfolder-active-number nnfolder-current-group)))
699 ;; Make absolutely sure that the active list reflects reality!
700 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
704 (defun nnfolder-generate-active-file ()
705 "Look for mbox folders in the nnfolder directory and make them into groups."
707 (nnmail-activate 'nnfolder)
708 (let ((files (directory-files nnfolder-directory))
710 (while (setq file (pop files))
711 (when (nnheader-mail-file-mbox-p file)
712 (nnheader-message 5 "Adding group %s..." file)
713 (push (list file (cons 1 0)) nnfolder-group-alist)
714 (nnfolder-read-folder file)
715 (nnfolder-close-group file))
720 ;;; nnfolder.el ends here