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 0 0))) nnfolder-group-alist))
284 (nnfolder-possibly-change-group group)
285 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
288 (defun nnfolder-request-list (&optional server)
289 (if server (nnfolder-get-new-mail))
291 (nnmail-find-file nnfolder-active-file)
292 (setq nnfolder-group-alist (nnmail-get-active))))
294 ;; (or nnfolder-group-alist
295 ;; (nnmail-find-file nnfolder-active-file)
297 ;; (setq nnfolder-group-alist (nnmail-get-active))
298 ;; (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
299 ;; (nnmail-find-file nnfolder-active-file)))))
301 (defun nnfolder-request-newgroups (date &optional server)
302 (nnfolder-request-list server))
304 (defun nnfolder-request-list-newsgroups (&optional server)
306 (nnmail-find-file nnfolder-newsgroups-file)))
308 (defun nnfolder-request-post (&optional server)
309 (mail-send-and-exit nil))
311 (defalias 'nnfolder-request-post-buffer 'nnmail-request-post-buffer)
313 (defun nnfolder-request-expire-articles
314 (articles newsgroup &optional server force)
315 (nnfolder-possibly-change-group newsgroup)
316 (let* ((days (or (and nnmail-expiry-wait-function
317 (funcall nnmail-expiry-wait-function newsgroup))
321 (nnmail-activate 'nnfolder)
324 (set-buffer nnfolder-current-buffer)
325 (while (and articles is-old)
326 (goto-char (point-min))
327 (if (search-forward (nnfolder-article-string (car articles)) nil t)
330 (> (nnmail-days-between
331 (current-time-string)
333 (point) (progn (end-of-line) (point))))
336 (and gnus-verbose-backends
337 (message "Deleting article %s..." (car articles)))
338 (nnfolder-delete-mail))
339 (setq rest (cons (car articles) rest))))
340 (setq articles (cdr articles)))
341 (and (buffer-modified-p) (save-buffer))
342 ;; Find the lowest active article in this group.
343 (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
344 (marker (concat "\n" nnfolder-article-marker))
346 (activemin (cdr active)))
347 (goto-char (point-min))
348 (while (and (search-forward marker nil t)
349 (re-search-forward number nil t))
350 (setq activemin (min activemin
351 (string-to-number (buffer-substring
354 (setcar active activemin))
355 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
356 (nconc rest articles))))
358 (defun nnfolder-request-move-article
359 (article group server accept-form &optional last)
360 (nnfolder-possibly-change-group group)
361 (let ((buf (get-buffer-create " *nnfolder move*"))
364 (nnfolder-request-article article group server)
367 (buffer-disable-undo (current-buffer))
369 (insert-buffer-substring nntp-server-buffer)
370 (goto-char (point-min))
371 (while (re-search-forward
373 (save-excursion (search-forward "\n\n" nil t) (point)) t)
374 (delete-region (progn (beginning-of-line) (point))
375 (progn (forward-line 1) (point))))
376 (setq result (eval accept-form))
380 (nnfolder-possibly-change-group group)
381 (set-buffer nnfolder-current-buffer)
382 (goto-char (point-min))
383 (if (search-forward (nnfolder-article-string article) nil t)
384 (nnfolder-delete-mail))
390 (defun nnfolder-request-accept-article (group &optional last)
391 (and (stringp group) (nnfolder-possibly-change-group group))
392 (let ((buf (current-buffer))
394 (goto-char (point-min))
395 (if (looking-at "X-From-Line: ")
396 (replace-match "From ")
397 (insert "From nobody " (current-time-string) "\n"))
399 (nnfolder-request-list)
402 (goto-char (point-min))
403 (search-forward "\n\n" nil t)
405 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
406 (delete-region (point) (progn (forward-line 1) (point))))
407 (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
409 (set-buffer nnfolder-current-buffer)
410 (and last (buffer-modified-p) (save-buffer))))
411 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
414 (defun nnfolder-request-replace-article (article group buffer)
415 (nnfolder-possibly-change-group group)
417 (set-buffer nnfolder-current-buffer)
418 (goto-char (point-min))
419 (if (not (search-forward (nnfolder-article-string article) nil t))
421 (nnfolder-delete-mail t t)
422 (insert-buffer-substring buffer)
423 (and (buffer-modified-p) (save-buffer))
427 ;;; Internal functions.
429 (defun nnfolder-delete-mail (&optional force leave-delim)
430 ;; Beginning of the article.
435 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
436 (if leave-delim (progn (forward-line 1) (point))
437 (match-beginning 0)))
440 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
442 (if (and (not (bobp)) leave-delim)
443 (progn (forward-line -2) (point))
444 (match-beginning 0)))
446 (delete-region (point-min) (point-max)))))
448 (defun nnfolder-possibly-change-group (group)
449 (or (file-exists-p nnfolder-directory)
450 (make-directory (directory-file-name nnfolder-directory)))
451 (nnfolder-possibly-activate-groups nil)
452 (or (assoc group nnfolder-group-alist)
453 (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
456 (setq nnfolder-group-alist
457 (cons (list group (cons 1 0)) nnfolder-group-alist))
458 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
460 (if (and (equal group nnfolder-current-group)
461 nnfolder-current-buffer
462 (buffer-name nnfolder-current-buffer))
464 (setq nnfolder-current-group group)
466 ;; If we have to change groups, see if we don't already have the mbox
467 ;; in memory. If we do, verify the modtime and destroy the mbox if
468 ;; needed so we can rescan it.
469 (if (setq inf (assoc group nnfolder-buffer-alist))
470 (setq nnfolder-current-buffer (nth 1 inf)))
472 ;; If the buffer is not live, make sure it isn't in the alist. If it
473 ;; is live, verify that nobody else has touched the file since last
475 (if (or (not (and nnfolder-current-buffer
476 (buffer-name nnfolder-current-buffer)))
477 (not (and (bufferp nnfolder-current-buffer)
478 (verify-visited-file-modtime
479 nnfolder-current-buffer))))
481 (if (and nnfolder-current-buffer
482 (buffer-name nnfolder-current-buffer)
483 (bufferp nnfolder-current-buffer))
484 (kill-buffer nnfolder-current-buffer))
485 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
491 (setq file (concat (file-name-as-directory nnfolder-directory)
493 (if (file-directory-p (file-truename file))
495 (if (not (file-exists-p file))
496 (write-region 1 1 file t 'nomesg))
497 (setq nnfolder-current-buffer
498 (set-buffer (nnfolder-read-folder file)))
499 (setq nnfolder-buffer-alist (cons (list group (current-buffer))
500 nnfolder-buffer-alist)))))))
501 (setq nnfolder-current-group group))
503 (defun nnfolder-save-mail (&optional group)
504 "Called narrowed to an article."
505 (let* ((nnmail-split-methods
506 (if group (list (list group "")) nnmail-split-methods))
508 (nreverse (nnmail-article-group 'nnfolder-active-number)))
510 (setq save-list group-art-list)
511 (nnmail-insert-lines)
512 (nnmail-insert-xref group-art-list)
513 (run-hooks 'nnfolder-prepare-save-mail-hook)
515 ;; Insert the mail into each of the destination groups.
516 (while group-art-list
517 (setq group-art (car group-art-list)
518 group-art-list (cdr group-art-list))
520 ;; Kill the previous newsgroup markers.
521 (goto-char (point-min))
522 (search-forward "\n\n" nil t)
524 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
525 (delete-region (point) (progn (forward-line 1) (point))))
527 ;; Insert the new newsgroup marker.
528 (nnfolder-possibly-change-group (car group-art))
529 (nnfolder-insert-newsgroup-line group-art)
530 (let ((beg (point-min))
532 (obuf (current-buffer)))
533 (set-buffer nnfolder-current-buffer)
534 (goto-char (point-max))
535 (insert-buffer-substring obuf beg end)
538 ;; Did we save it anywhere?
541 (defun nnfolder-insert-newsgroup-line (group-art)
543 (goto-char (point-min))
544 (if (search-forward "\n\n" nil t)
547 (insert (format (concat nnfolder-article-marker "%d %s\n")
548 (cdr group-art) (current-time-string)))))))
550 (defun nnfolder-possibly-activate-groups (&optional group)
552 ;; If we're looking for the activation of a specific group, find out
553 ;; it's real name and switch to it.
554 (if group (nnfolder-possibly-change-group group))
555 ;; If the group alist isn't active, activate it now.
556 (or nnfolder-group-alist
557 (nnmail-activate 'nnfolder))))
559 (defun nnfolder-active-number (group)
561 (nnfolder-possibly-activate-groups group)
562 ;; Find the next article number in GROUP.
563 (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
565 (setcdr active (1+ (cdr active)))
566 ;; This group is new, so we create a new entry for it.
567 ;; This might be a bit naughty... creating groups on the drop of
568 ;; a hat, but I don't know...
569 (setq nnfolder-group-alist (cons (list group (setq active (cons 1 1)))
570 nnfolder-group-alist)))
573 ;; This method has a problem if you've accidentally let the active list get
574 ;; out of sync with the files. This could happen, say, if you've
575 ;; accidentally gotten new mail with something other than (ding) (but why
576 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
577 ;; processing the file, ready to add new X-Gnus article number markers, and
578 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
581 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
582 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
583 ;; rest of the message looking for any more, possibly higher IDs. We'll
584 ;; assume the maximum that we find is the highest active. Note that this
585 ;; shouldn't cost us much extra time at all, but will be a lot less
586 ;; vulnerable to glitches between the mbox and the active file.
588 (defun nnfolder-read-folder (file)
590 (nnfolder-possibly-activate-groups nil)
591 ;; We should be paranoid here and make sure the group is in the alist,
592 ;; and add it if it isn't.
593 ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
594 (set-buffer (setq nnfolder-current-buffer (find-file-noselect file)))
595 (buffer-disable-undo (current-buffer))
596 (let ((delim (concat "^" rmail-unix-mail-delimiter))
597 (marker (concat "\n" nnfolder-article-marker))
599 (active (car (cdr (assoc nnfolder-current-group
600 nnfolder-group-alist))))
601 activenumber activemin start end)
602 (goto-char (point-min))
604 ;; Anytime the active number is 1 or 0, it is supect. In that case,
605 ;; search the file manually to find the active number. Or, of course,
606 ;; if we're being paranoid. (This would also be the place to build
607 ;; other lists from the header markers, such as expunge lists, etc., if
608 ;; we ever desired to abandon the active file entirely for mboxes.)
609 (setq activenumber (cdr active))
610 (if (or nnfolder-ignore-active-file
613 (setq activemin (max (1- (lsh 1 23))
616 (while (and (search-forward marker nil t)
617 (re-search-forward number nil t))
618 (let ((newnum (string-to-number (buffer-substring
621 (setq activenumber (max activenumber newnum))
622 (setq activemin (min activemin newnum))))
623 (setcar active (min activemin activenumber))
624 (setcdr active activenumber)
625 (goto-char (point-min))))
627 ;; Keep track of the active number on our own, and insert it back into
628 ;; the active list when we're done. Also, prime the pump to cut down on
629 ;; the number of searches we do.
630 (setq end (point-marker))
631 (set-marker end (or (and (re-search-forward delim nil t)
634 (while (not (= end (point-max)))
635 (setq start (marker-position end))
638 (set-marker end (or (and (re-search-forward delim nil t)
642 (if (not (search-forward marker end t))
644 (narrow-to-region start end)
645 (nnmail-insert-lines)
646 (setq activenumber (1+ activenumber))
647 (nnfolder-insert-newsgroup-line (cons nil activenumber))
650 ;; Make absolutely sure that the active list reflects reality!
651 (setcdr active activenumber)
652 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
655 (defun nnfolder-get-new-mail (&optional group)
656 "Read new incoming mail."
657 (let* ((spools (nnmail-get-spool-files group))
660 (if (or (not nnfolder-get-new-mail) (not nnmail-spool-file))
662 ;; We first activate all the groups.
663 (nnfolder-possibly-activate-groups nil)
664 ;; The we go through all the existing spool files and split the
668 (file-exists-p (car spools))
669 (> (nth 7 (file-attributes (car spools))) 0)
671 (and gnus-verbose-backends
672 (message "nnfolder: Reading incoming mail..."))
676 (concat (file-name-as-directory nnfolder-directory)
678 (setq incomings (cons incoming incomings))
679 (setq group (nnmail-get-split-group (car spools) group-in))
680 (nnmail-split-incoming incoming 'nnfolder-save-mail nil group)))
681 (setq spools (cdr spools)))
682 ;; If we did indeed read any incoming spools, we save all info.
685 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
686 (run-hooks 'nnmail-read-incoming-hook)
687 (and gnus-verbose-backends
688 (message "nnfolder: Reading incoming mail...done"))))
689 (let ((bufs nnfolder-buffer-alist))
692 (if (not (buffer-name (nth 1 (car bufs))))
693 (setq nnfolder-buffer-alist
694 (delq (car bufs) nnfolder-buffer-alist))
695 (set-buffer (nth 1 (car bufs)))
696 (and (buffer-modified-p) (save-buffer)))
697 (setq bufs (cdr bufs)))))
700 nnmail-delete-incoming
701 (file-writable-p incoming)
702 (delete-file incoming))
703 (setq incomings (cdr incomings))))))
707 ;;; nnfolder.el ends here