1 ;;; nnfolder.el --- mail folder access for Gnus
2 ;; Copyright (C) 1995 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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 ;; For an overview of what the interface functions do, please see the
30 ;; Various enhancements by byer@mv.us.adobe.com (Scott Byer).
38 (defvar nnfolder-directory (expand-file-name "~/Mail/")
39 "The name of the mail box file in the users home directory.")
41 (defvar nnfolder-active-file
42 (concat (file-name-as-directory nnfolder-directory) "active")
43 "The name of the active file.")
45 ;; I renamed this variable to somehting more in keeping with the general GNU
48 (defvar nnfolder-ignore-active-file nil
49 "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.
50 Note that the active file is still saved, but it's values are not
51 used. This costs some extra time when scanning an mbox when opening
54 ;; Note that this variable may not be completely implemented yet. -SLB
56 (defvar nnfolder-always-close nil
57 "If non-nil, nnfolder attempts to only ever have one mbox open at a time.
58 This is a straight space/performance trade off, as the mboxes will have to
59 be scanned every time they are read in. If nil (default), nnfolder will
60 attempt to keep the buffers around (saving the nnfolder's buffer upon group
61 close, but not killing it), speeding some things up tremendously, especially
62 such things as moving mail. All buffers always get killed upon server close.")
64 (defvar nnfolder-newsgroups-file
65 (concat (file-name-as-directory nnfolder-directory) "newsgroups")
66 "Mail newsgroups description file.")
68 (defvar nnfolder-get-new-mail t
69 "If non-nil, nnfolder will check the incoming mail file and split the mail.")
71 (defvar nnfolder-prepare-save-mail-hook nil
72 "Hook run narrowed to an article before saving.")
76 (defconst nnfolder-version "nnfolder 0.2"
79 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
80 "String used to demarcate what the article number for a message is.")
82 (defvar nnfolder-current-group nil)
83 (defvar nnfolder-current-buffer nil)
84 (defvar nnfolder-status-string "")
85 (defvar nnfolder-group-alist nil)
86 (defvar nnfolder-buffer-alist nil)
87 (defvar nnfolder-active-timestamp nil)
89 (defmacro nnfolder-article-string (article)
90 (` (concat "\n" nnfolder-article-marker (int-to-string (, article)) " ")))
94 (defvar nnfolder-current-server nil)
95 (defvar nnfolder-server-alist nil)
96 (defvar nnfolder-server-variables
98 (list 'nnfolder-directory nnfolder-directory)
99 (list 'nnfolder-active-file nnfolder-active-file)
100 (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
101 (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
102 '(nnfolder-current-group nil)
103 '(nnfolder-current-buffer nil)
104 '(nnfolder-status-string "")
105 '(nnfolder-group-alist nil)
106 '(nnfolder-buffer-alist nil)
107 '(nnfolder-active-timestamp nil)))
111 ;;; Interface functions
113 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server)
115 (set-buffer nntp-server-buffer)
117 (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
118 article art-string start stop)
119 (nnfolder-possibly-change-group newsgroup)
120 (set-buffer nnfolder-current-buffer)
121 (goto-char (point-min))
122 (if (stringp (car sequence))
125 (setq article (car sequence))
126 (setq art-string (nnfolder-article-string article))
127 (set-buffer nnfolder-current-buffer)
128 (if (or (search-forward art-string nil t)
129 ;; Don't search the whole file twice! Also, articles
130 ;; probably have some locality by number, so searching
131 ;; backwards will be faster. Especially if we're at the
132 ;; beginning of the buffer :-). -SLB
133 (search-backward art-string nil t))
135 (setq start (or (re-search-backward delim-string nil t)
137 (search-forward "\n\n" nil t)
138 (setq stop (1- (point)))
139 (set-buffer nntp-server-buffer)
140 (insert (format "221 %d Article retrieved.\n" article))
141 (insert-buffer-substring nnfolder-current-buffer start stop)
142 (goto-char (point-max))
144 (setq sequence (cdr sequence)))
146 ;; Fold continuation lines.
147 (set-buffer nntp-server-buffer)
148 (goto-char (point-min))
149 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
150 (replace-match " " t t))
153 (defun nnfolder-open-server (server &optional defs)
154 (nnheader-init-server-buffer)
155 (if (equal server nnfolder-current-server)
157 (if nnfolder-current-server
158 (setq nnfolder-server-alist
159 (cons (list nnfolder-current-server
160 (nnheader-save-variables nnfolder-server-variables))
161 nnfolder-server-alist)))
162 (let ((state (assoc server nnfolder-server-alist)))
165 (nnheader-restore-variables (nth 1 state))
166 (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
167 (nnheader-set-init-variables nnfolder-server-variables defs)))
168 (setq nnfolder-current-server server)))
170 (defun nnfolder-close-server (&optional server)
173 (defun nnfolder-server-opened (&optional server)
174 (and (equal server nnfolder-current-server)
176 (buffer-name nntp-server-buffer)))
178 (defun nnfolder-request-close ()
179 (let ((alist nnfolder-buffer-alist))
181 (nnfolder-close-group (car (car alist)) nil t)
182 (setq alist (cdr alist))))
183 (setq nnfolder-buffer-alist nil
184 nnfolder-group-alist nil))
186 (defun nnfolder-status-message (&optional server)
187 nnfolder-status-string)
189 (defun nnfolder-request-article (article &optional newsgroup server buffer)
190 (nnfolder-possibly-change-group newsgroup)
191 (if (stringp article)
194 (set-buffer nnfolder-current-buffer)
195 (goto-char (point-min))
196 (if (search-forward (nnfolder-article-string article) nil t)
198 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
201 (or (and (re-search-forward
202 (concat "^" rmail-unix-mail-delimiter) nil t)
204 (goto-char (point-max)))
206 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
207 (set-buffer nntp-server-buffer)
209 (insert-buffer-substring nnfolder-current-buffer start stop)
210 (goto-char (point-min))
211 (while (looking-at "From ")
213 (insert "X-From-Line: ")
217 (defun nnfolder-request-group (group &optional server dont-check)
219 (nnfolder-possibly-change-group group)
220 (and (assoc group nnfolder-group-alist)
224 (nnfolder-get-new-mail group))
225 (let ((timestamp (nth 5 (file-attributes nnfolder-active-file))))
226 ;; Make sure we get the latest active file
227 (if (or (not nnfolder-active-timestamp)
228 (> (nth 0 timestamp) (nth 0 nnfolder-active-timestamp))
229 (> (nth 1 timestamp) (nth 1 nnfolder-active-timestamp)))
231 (setq nnfolder-active-timestamp timestamp)
232 (nnmail-activate 'nnfolder)))
233 (let* ((active (assoc group nnfolder-group-alist))
235 (range (car (cdr active)))
236 (minactive (car range))
237 (maxactive (cdr range)))
238 ;; I've been getting stray 211 lines in my nnfolder active
239 ;; file. So, let's make sure that doesn't happen. -SLB
240 (set-buffer nntp-server-buffer)
244 (insert (format "211 %d %d %d %s\n"
245 (1+ (- maxactive minactive))
246 minactive maxactive group))
249 ;; Don't close the buffer if we're not shutting down the server. This way,
250 ;; we can keep the buffer in the group buffer cache, and not have to grovel
251 ;; over the buffer again unless we add new mail to it or modify it in some
254 (defun nnfolder-close-group (group &optional server force)
255 ;; Make sure we _had_ the group open.
256 (if (or (assoc group nnfolder-buffer-alist)
257 (equal group nnfolder-current-group))
259 (nnfolder-possibly-change-group group)
261 (set-buffer nnfolder-current-buffer)
262 ;; If the buffer was modified, write the file out now.
263 (and (buffer-modified-p) (save-buffer))
265 nnfolder-always-close)
266 ;; If we're shutting the server down, we need to kill the
267 ;; buffer and remove it from the open buffer list. Or, of
268 ;; course, if we're trying to minimize our space impact.
270 (kill-buffer (current-buffer))
271 (setq nnfolder-buffer-alist (delq (assoc group
272 nnfolder-buffer-alist)
273 nnfolder-buffer-alist)))))))
274 (setq nnfolder-current-group nil
275 nnfolder-current-buffer nil)
278 (defun nnfolder-request-create-group (group &optional server)
279 (nnmail-activate 'nnfolder)
280 (or (assoc group nnfolder-group-alist)
282 (setq nnfolder-group-alist
283 (cons (list group (setq active (cons 1 0)))
284 nnfolder-group-alist))
285 (nnfolder-possibly-change-group group)
286 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
289 (defun nnfolder-request-list (&optional server)
290 (if server (nnfolder-get-new-mail))
292 (nnmail-find-file nnfolder-active-file)
293 (setq nnfolder-group-alist (nnmail-get-active))))
295 (defun nnfolder-request-newgroups (date &optional server)
296 (nnfolder-request-list server))
298 (defun nnfolder-request-list-newsgroups (&optional server)
300 (nnmail-find-file nnfolder-newsgroups-file)))
302 (defun nnfolder-request-post (&optional server)
303 (mail-send-and-exit nil))
305 (defalias 'nnfolder-request-post-buffer 'nnmail-request-post-buffer)
307 (defun nnfolder-request-expire-articles
308 (articles newsgroup &optional server force)
309 (nnfolder-possibly-change-group newsgroup)
310 (let* ((days (or (and nnmail-expiry-wait-function
311 (funcall nnmail-expiry-wait-function newsgroup))
315 (nnmail-activate 'nnfolder)
318 (set-buffer nnfolder-current-buffer)
319 (while (and articles is-old)
320 (goto-char (point-min))
321 (if (search-forward (nnfolder-article-string (car articles)) nil t)
324 (> (nnmail-days-between
325 (current-time-string)
327 (point) (progn (end-of-line) (point))))
330 (and gnus-verbose-backends
331 (message "Deleting article %s..." (car articles)))
332 (nnfolder-delete-mail))
333 (setq rest (cons (car articles) rest))))
334 (setq articles (cdr articles)))
335 (and (buffer-modified-p) (save-buffer))
336 ;; Find the lowest active article in this group.
337 (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
338 (marker (concat "\n" nnfolder-article-marker))
340 (activemin (cdr active)))
341 (goto-char (point-min))
342 (while (and (search-forward marker nil t)
343 (re-search-forward number nil t))
344 (setq activemin (min activemin
345 (string-to-number (buffer-substring
348 (setcar active activemin))
349 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
350 (nconc rest articles))))
352 (defun nnfolder-request-move-article
353 (article group server accept-form &optional last)
354 (nnfolder-possibly-change-group group)
355 (let ((buf (get-buffer-create " *nnfolder move*"))
358 (nnfolder-request-article article group server)
361 (buffer-disable-undo (current-buffer))
363 (insert-buffer-substring nntp-server-buffer)
364 (goto-char (point-min))
365 (while (re-search-forward
367 (save-excursion (search-forward "\n\n" nil t) (point)) t)
368 (delete-region (progn (beginning-of-line) (point))
369 (progn (forward-line 1) (point))))
370 (setq result (eval accept-form))
374 (nnfolder-possibly-change-group group)
375 (set-buffer nnfolder-current-buffer)
376 (goto-char (point-min))
377 (if (search-forward (nnfolder-article-string article) nil t)
378 (nnfolder-delete-mail))
384 (defun nnfolder-request-accept-article (group &optional last)
385 (and (stringp group) (nnfolder-possibly-change-group group))
386 (let ((buf (current-buffer))
388 (goto-char (point-min))
389 (if (looking-at "X-From-Line: ")
390 (replace-match "From ")
391 (insert "From nobody " (current-time-string) "\n"))
393 (nnfolder-request-list)
396 (goto-char (point-min))
397 (search-forward "\n\n" nil t)
399 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
400 (delete-region (point) (progn (forward-line 1) (point))))
401 (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
403 (set-buffer nnfolder-current-buffer)
404 (and last (buffer-modified-p) (save-buffer))))
405 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
408 (defun nnfolder-request-replace-article (article group buffer)
409 (nnfolder-possibly-change-group group)
411 (set-buffer nnfolder-current-buffer)
412 (goto-char (point-min))
413 (if (not (search-forward (nnfolder-article-string article) nil t))
415 (nnfolder-delete-mail t t)
416 (insert-buffer-substring buffer)
417 (and (buffer-modified-p) (save-buffer))
421 ;;; Internal functions.
423 (defun nnfolder-delete-mail (&optional force leave-delim)
424 ;; Beginning of the article.
429 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
430 (if leave-delim (progn (forward-line 1) (point))
431 (match-beginning 0)))
434 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
436 (if (and (not (bobp)) leave-delim)
437 (progn (forward-line -2) (point))
438 (match-beginning 0)))
440 (delete-region (point-min) (point-max)))))
442 (defun nnfolder-possibly-change-group (group)
443 (or (file-exists-p nnfolder-directory)
444 (make-directory (directory-file-name nnfolder-directory)))
445 (nnfolder-possibly-activate-groups nil)
446 (or (assoc group nnfolder-group-alist)
447 (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
450 (setq nnfolder-group-alist
451 (cons (list group (cons 1 0)) nnfolder-group-alist))
452 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
454 (if (and (equal group nnfolder-current-group)
455 nnfolder-current-buffer
456 (buffer-name nnfolder-current-buffer))
458 (setq nnfolder-current-group group)
460 ;; If we have to change groups, see if we don't already have the mbox
461 ;; in memory. If we do, verify the modtime and destroy the mbox if
462 ;; needed so we can rescan it.
463 (if (setq inf (assoc group nnfolder-buffer-alist))
464 (setq nnfolder-current-buffer (nth 1 inf)))
466 ;; If the buffer is not live, make sure it isn't in the alist. If it
467 ;; is live, verify that nobody else has touched the file since last
469 (if (or (not (and nnfolder-current-buffer
470 (buffer-name nnfolder-current-buffer)))
471 (not (and (bufferp nnfolder-current-buffer)
472 (verify-visited-file-modtime
473 nnfolder-current-buffer))))
475 (if (and nnfolder-current-buffer
476 (buffer-name nnfolder-current-buffer)
477 (bufferp nnfolder-current-buffer))
478 (kill-buffer nnfolder-current-buffer))
479 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
485 (setq file (concat (file-name-as-directory nnfolder-directory)
487 (if (file-directory-p (file-truename file))
489 (if (not (file-exists-p file))
490 (write-region 1 1 file t 'nomesg))
491 (setq nnfolder-current-buffer
492 (set-buffer (nnfolder-read-folder file)))
493 (setq nnfolder-buffer-alist (cons (list group (current-buffer))
494 nnfolder-buffer-alist)))))))
495 (setq nnfolder-current-group group))
497 (defun nnfolder-save-mail (&optional group)
498 "Called narrowed to an article."
499 (let* ((nnmail-split-methods
500 (if group (list (list group "")) nnmail-split-methods))
502 (nreverse (nnmail-article-group 'nnfolder-active-number)))
504 (setq save-list group-art-list)
505 (nnmail-insert-lines)
506 (nnmail-insert-xref group-art-list)
507 (run-hooks 'nnfolder-prepare-save-mail-hook)
509 ;; Insert the mail into each of the destination groups.
510 (while group-art-list
511 (setq group-art (car group-art-list)
512 group-art-list (cdr group-art-list))
514 ;; Kill the previous newsgroup markers.
515 (goto-char (point-min))
516 (search-forward "\n\n" nil t)
518 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
519 (delete-region (point) (progn (forward-line 1) (point))))
521 ;; Insert the new newsgroup marker.
522 (nnfolder-possibly-change-group (car group-art))
523 (nnfolder-insert-newsgroup-line group-art)
524 (let ((beg (point-min))
526 (obuf (current-buffer)))
527 (set-buffer nnfolder-current-buffer)
528 (goto-char (point-max))
529 (insert-buffer-substring obuf beg end)
532 ;; Did we save it anywhere?
535 (defun nnfolder-insert-newsgroup-line (group-art)
537 (goto-char (point-min))
538 (if (search-forward "\n\n" nil t)
541 (insert (format (concat nnfolder-article-marker "%d %s\n")
542 (cdr group-art) (current-time-string)))))))
544 (defun nnfolder-possibly-activate-groups (&optional group)
546 ;; If we're looking for the activation of a specific group, find out
547 ;; its real name and switch to it.
548 (if group (nnfolder-possibly-change-group group))
549 ;; If the group alist isn't active, activate it now.
550 (nnmail-activate 'nnfolder)))
552 (defun nnfolder-active-number (group)
554 ;; Find the next article number in GROUP.
556 (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
558 (setcdr active (1+ (cdr active)))
559 ;; This group is new, so we create a new entry for it.
560 ;; This might be a bit naughty... creating groups on the drop of
561 ;; a hat, but I don't know...
562 (setq nnfolder-group-alist
563 (cons (list group (setq active (cons 1 1)))
564 nnfolder-group-alist)))
566 (nnfolder-possibly-activate-groups group))))
569 ;; This method has a problem if you've accidentally let the active list get
570 ;; out of sync with the files. This could happen, say, if you've
571 ;; accidentally gotten new mail with something other than (ding) (but why
572 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
573 ;; processing the file, ready to add new X-Gnus article number markers, and
574 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
577 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
578 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
579 ;; rest of the message looking for any more, possibly higher IDs. We'll
580 ;; assume the maximum that we find is the highest active. Note that this
581 ;; shouldn't cost us much extra time at all, but will be a lot less
582 ;; vulnerable to glitches between the mbox and the active file.
584 (defun nnfolder-read-folder (file)
586 (nnfolder-possibly-activate-groups nil)
587 ;; We should be paranoid here and make sure the group is in the alist,
588 ;; and add it if it isn't.
589 ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
590 (set-buffer (setq nnfolder-current-buffer
591 (nnheader-find-file-noselect file)))
592 (buffer-disable-undo (current-buffer))
593 (let ((delim (concat "^" rmail-unix-mail-delimiter))
594 (marker (concat "\n" nnfolder-article-marker))
596 (active (car (cdr (assoc nnfolder-current-group
597 nnfolder-group-alist))))
598 activenumber activemin start end)
599 (goto-char (point-min))
601 ;; Anytime the active number is 1 or 0, it is supect. In that case,
602 ;; search the file manually to find the active number. Or, of course,
603 ;; if we're being paranoid. (This would also be the place to build
604 ;; other lists from the header markers, such as expunge lists, etc., if
605 ;; we ever desired to abandon the active file entirely for mboxes.)
606 (setq activenumber (cdr active))
607 (if (or nnfolder-ignore-active-file
610 (setq activemin (max (1- (lsh 1 23))
613 (while (and (search-forward marker nil t)
614 (re-search-forward number nil t))
615 (let ((newnum (string-to-number (buffer-substring
618 (setq activenumber (max activenumber newnum))
619 (setq activemin (min activemin newnum))))
620 (setcar active (min activemin activenumber))
621 (setcdr active activenumber)
622 (goto-char (point-min))))
624 ;; Keep track of the active number on our own, and insert it back into
625 ;; the active list when we're done. Also, prime the pump to cut down on
626 ;; the number of searches we do.
627 (setq end (point-marker))
628 (set-marker end (or (and (re-search-forward delim nil t)
631 (while (not (= end (point-max)))
632 (setq start (marker-position end))
635 (set-marker end (or (and (re-search-forward delim nil t)
639 (if (not (search-forward marker end t))
641 (narrow-to-region start end)
642 (nnmail-insert-lines)
643 (setq activenumber (1+ activenumber))
644 (nnfolder-insert-newsgroup-line (cons nil activenumber))
647 ;; Make absolutely sure that the active list reflects reality!
648 (setcdr active activenumber)
649 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
652 (defun nnfolder-get-new-mail (&optional group)
653 "Read new incoming mail."
654 (let* ((spools (nnmail-get-spool-files group))
657 (if (or (not nnfolder-get-new-mail) (not nnmail-spool-file))
659 ;; We first activate all the groups.
660 (nnfolder-possibly-activate-groups nil)
661 ;; The we go through all the existing spool files and split the
665 (file-exists-p (car spools))
666 (> (nth 7 (file-attributes (car spools))) 0)
668 (and gnus-verbose-backends
669 (message "nnfolder: Reading incoming mail..."))
673 (concat (file-name-as-directory nnfolder-directory)
675 (setq incomings (cons incoming incomings))
676 (setq group (nnmail-get-split-group (car spools) group-in))
677 (nnmail-split-incoming incoming 'nnfolder-save-mail nil group)))
678 (setq spools (cdr spools)))
679 ;; If we did indeed read any incoming spools, we save all info.
682 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
683 (run-hooks 'nnmail-read-incoming-hook)
684 (and gnus-verbose-backends
685 (message "nnfolder: Reading incoming mail...done"))))
686 (let ((bufs nnfolder-buffer-alist))
689 (if (not (buffer-name (nth 1 (car bufs))))
690 (setq nnfolder-buffer-alist
691 (delq (car bufs) nnfolder-buffer-alist))
692 (set-buffer (nth 1 (car bufs)))
693 (and (buffer-modified-p) (save-buffer)))
694 (setq bufs (cdr bufs)))))
696 (setq incoming (car incomings))
698 nnmail-delete-incoming
699 (file-writable-p incoming)
700 (file-exists-p incoming)
701 (delete-file incoming))
702 (setq incomings (cdr incomings))))))
706 ;;; nnfolder.el ends here