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, nnml 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)
88 (defmacro nnfolder-article-string (article)
89 (` (concat "\n" nnfolder-article-marker (int-to-string (, article)) " ")))
93 (defvar nnfolder-current-server nil)
94 (defvar nnfolder-server-alist nil)
95 (defvar nnfolder-server-variables
97 (list 'nnfolder-directory nnfolder-directory)
98 (list 'nnfolder-active-file nnfolder-active-file)
99 (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
100 (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
101 '(nnfolder-current-group nil)
102 '(nnfolder-current-buffer nil)
103 '(nnfolder-status-string "")
104 '(nnfolder-group-alist nil)
105 '(nnfolder-buffer-alist nil)))
109 ;;; Interface functions
111 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server)
113 (set-buffer nntp-server-buffer)
115 (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
116 article art-string start stop)
117 (nnfolder-possibly-change-group newsgroup)
118 (set-buffer nnfolder-current-buffer)
119 (goto-char (point-min))
120 (if (stringp (car sequence))
123 (setq article (car sequence))
124 (setq art-string (nnfolder-article-string article))
125 (set-buffer nnfolder-current-buffer)
126 (if (or (search-forward art-string nil t)
127 ;; Don't search the whole file twice! Also, articles
128 ;; probably have some locality by number, so searching
129 ;; backwards will be faster. Especially if we're at the
130 ;; beginning of the buffer :-). -SLB
131 (search-backward art-string nil t))
133 (setq start (or (re-search-backward delim-string nil t)
135 (search-forward "\n\n" nil t)
136 (setq stop (1- (point)))
137 (set-buffer nntp-server-buffer)
138 (insert (format "221 %d Article retrieved.\n" article))
139 (insert-buffer-substring nnfolder-current-buffer start stop)
140 (goto-char (point-max))
142 (setq sequence (cdr sequence)))
144 ;; Fold continuation lines.
145 (set-buffer nntp-server-buffer)
146 (goto-char (point-min))
147 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
148 (replace-match " " t t))
151 (defun nnfolder-open-server (server &optional defs)
152 (nnheader-init-server-buffer)
153 (if (equal server nnfolder-current-server)
155 (if nnfolder-current-server
156 (setq nnfolder-server-alist
157 (cons (list nnfolder-current-server
158 (nnheader-save-variables nnfolder-server-variables))
159 nnfolder-server-alist)))
160 (let ((state (assoc server nnfolder-server-alist)))
163 (nnheader-restore-variables (nth 1 state))
164 (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
165 (nnheader-set-init-variables nnfolder-server-variables defs)))
166 (setq nnfolder-current-server server)))
168 (defun nnfolder-close-server (&optional server)
171 (defun nnfolder-server-opened (&optional server)
172 (and (equal server nnfolder-current-server)
174 (buffer-name nntp-server-buffer)))
176 (defun nnfolder-request-close ()
177 (let ((alist nnfolder-buffer-alist))
179 (nnfolder-close-group (car (car alist)) nil t)
180 (setq alist (cdr alist))))
181 (setq nnfolder-buffer-alist nil
182 nnfolder-group-alist nil))
184 (defun nnfolder-status-message (&optional server)
185 nnfolder-status-string)
187 (defun nnfolder-request-article (article &optional newsgroup server buffer)
188 (nnfolder-possibly-change-group newsgroup)
189 (if (stringp article)
192 (set-buffer nnfolder-current-buffer)
193 (goto-char (point-min))
194 (if (search-forward (nnfolder-article-string article) nil t)
196 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
199 (or (and (re-search-forward
200 (concat "^" rmail-unix-mail-delimiter) nil t)
202 (goto-char (point-max)))
204 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
205 (set-buffer nntp-server-buffer)
207 (insert-buffer-substring nnfolder-current-buffer start stop)
208 (goto-char (point-min))
209 (while (looking-at "From ")
211 (insert "X-From-Line: ")
215 (defun nnfolder-request-group (group &optional server dont-check)
217 (nnfolder-possibly-change-group group)
218 (and (assoc group nnfolder-group-alist)
222 (nnfolder-get-new-mail group))
223 (let* ((active (assoc group nnfolder-group-alist))
225 (range (car (cdr active)))
226 (minactive (car range))
227 (maxactive (cdr range)))
228 ;; I've been getting stray 211 lines in my nnfolder active
229 ;; file. So, let's make sure that doesn't happen. -SLB
230 (set-buffer nntp-server-buffer)
232 (insert (format "211 %d %d %d %s\n"
233 (1+ (- maxactive minactive))
234 minactive maxactive group))
237 ;; Don't close the buffer if we're not shutting down the server. This way,
238 ;; we can keep the buffer in the group buffer cache, and not have to grovel
239 ;; over the buffer again unless we add new mail to it or modify it in some
242 (defun nnfolder-close-group (group &optional server force)
243 ;; Make sure we _had_ the group open.
244 (if (or (assoc group nnfolder-buffer-alist)
245 (equal group nnfolder-current-group))
247 (nnfolder-possibly-change-group group)
249 (set-buffer nnfolder-current-buffer)
250 ;; If the buffer was modified, write the file out now.
251 (and (buffer-modified-p) (save-buffer))
253 nnfolder-always-close)
254 ;; If we're shutting the server down, we need to kill the
255 ;; buffer and remove it from the open buffer list. Or, of
256 ;; course, if we're trying to minimize our space impact.
258 (kill-buffer (current-buffer))
259 (setq nnfolder-buffer-alist (delq (assoc group
260 nnfolder-buffer-alist)
261 nnfolder-buffer-alist)))))))
262 (setq nnfolder-current-group nil
263 nnfolder-current-buffer nil)
266 (defun nnfolder-request-create-group (group &optional server)
267 (nnfolder-request-list)
268 (setq nnfolder-group-alist (nnmail-get-active))
269 (or (assoc group nnfolder-group-alist)
271 (setq nnfolder-group-alist
272 (cons (list group (cons 0 0)) nnfolder-group-alist))
273 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
276 (defun nnfolder-request-list (&optional server)
277 (if server (nnfolder-get-new-mail))
279 (or nnfolder-group-alist
280 (nnmail-find-file nnfolder-active-file)
282 (setq nnfolder-group-alist (nnmail-get-active))
283 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
284 (nnmail-find-file nnfolder-active-file)))))
286 (defun nnfolder-request-newgroups (date &optional server)
287 (nnfolder-request-list server))
289 (defun nnfolder-request-list-newsgroups (&optional server)
291 (nnmail-find-file nnfolder-newsgroups-file)))
293 (defun nnfolder-request-post (&optional server)
294 (mail-send-and-exit nil))
296 (defalias 'nnfolder-request-post-buffer 'nnmail-request-post-buffer)
298 (defun nnfolder-request-expire-articles
299 (articles newsgroup &optional server force)
300 (nnfolder-possibly-change-group newsgroup)
301 (let* ((days (or (and nnmail-expiry-wait-function
302 (funcall nnmail-expiry-wait-function newsgroup))
306 (nnfolder-request-list)
307 (setq nnfolder-group-alist (nnmail-get-active))
310 (set-buffer nnfolder-current-buffer)
312 (goto-char (point-min))
313 (if (search-forward (nnfolder-article-string (car articles)) nil t)
316 (> (nnmail-days-between
317 (current-time-string)
319 (point) (progn (end-of-line) (point))))
322 (and gnus-verbose-backends
323 (message "Deleting: %s" (car articles)))
324 (nnfolder-delete-mail))
325 (setq rest (cons (car articles) rest))))
326 (setq articles (cdr articles)))
327 (and (buffer-modified-p) (save-buffer))
328 ;; Find the lowest active article in this group.
329 (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
330 (marker (concat "\n" nnfolder-article-marker))
332 (activemin (cdr active)))
333 (goto-char (point-min))
334 (while (and (search-forward marker nil t)
335 (re-search-forward number nil t))
336 (setq activemin (min activemin
337 (string-to-number (buffer-substring
340 (setcar active activemin))
341 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
342 (nconc rest articles))))
344 (defun nnfolder-request-move-article
345 (article group server accept-form &optional last)
346 (nnfolder-possibly-change-group group)
347 (let ((buf (get-buffer-create " *nnfolder move*"))
350 (nnfolder-request-article article group server)
353 (buffer-disable-undo (current-buffer))
355 (insert-buffer-substring nntp-server-buffer)
356 (goto-char (point-min))
357 (while (re-search-forward
359 (save-excursion (search-forward "\n\n" nil t) (point)) t)
360 (delete-region (progn (beginning-of-line) (point))
361 (progn (forward-line 1) (point))))
362 (setq result (eval accept-form))
366 (nnfolder-possibly-change-group group)
367 (set-buffer nnfolder-current-buffer)
368 (goto-char (point-min))
369 (if (search-forward (nnfolder-article-string article) nil t)
370 (nnfolder-delete-mail))
376 (defun nnfolder-request-accept-article (group &optional last)
377 (nnfolder-possibly-change-group group)
378 (let ((buf (current-buffer))
380 (goto-char (point-min))
381 (if (looking-at "X-From-Line: ")
382 (replace-match "From ")
383 (insert "From nobody " (current-time-string) "\n"))
385 (nnfolder-request-list)
388 (goto-char (point-min))
389 (search-forward "\n\n" nil t)
391 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
392 (delete-region (point) (progn (forward-line 1) (point))))
393 (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
395 (set-buffer nnfolder-current-buffer)
396 (and last (buffer-modified-p) (save-buffer))))
397 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
400 (defun nnfolder-request-replace-article (article group buffer)
401 (nnfolder-possibly-change-group group)
403 (set-buffer nnfolder-current-buffer)
404 (goto-char (point-min))
405 (if (not (search-forward (nnfolder-article-string article) nil t))
407 (nnfolder-delete-mail t t)
408 (insert-buffer-substring buffer)
409 (and (buffer-modified-p) (save-buffer))
413 ;;; Internal functions.
415 (defun nnfolder-delete-mail (&optional force leave-delim)
416 ;; Beginning of the article.
421 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
422 (if leave-delim (progn (forward-line 1) (point))
423 (match-beginning 0)))
426 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
428 (if (and (not (bobp)) leave-delim)
429 (progn (forward-line -2) (point))
430 (match-beginning 0)))
432 (delete-region (point-min) (point-max)))))
434 (defun nnfolder-possibly-change-group (group)
435 (or (file-exists-p nnfolder-directory)
436 (make-directory (directory-file-name nnfolder-directory)))
437 (nnfolder-possibly-activate-groups nil)
438 (or (assoc group nnfolder-group-alist)
439 (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
442 (setq nnfolder-group-alist
443 (cons (list group (cons 1 0)) nnfolder-group-alist))
444 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
446 (if (and (equal group nnfolder-current-group)
447 nnfolder-current-buffer
448 (buffer-name nnfolder-current-buffer))
450 (setq nnfolder-current-group group)
452 ;; If we have to change groups, see if we don't already have the mbox
453 ;; in memory. If we do, verify the modtime and destroy the mbox if
454 ;; needed so we can rescan it.
455 (if (setq inf (assoc group nnfolder-buffer-alist))
456 (setq nnfolder-current-buffer (nth 1 inf)))
458 ;; If the buffer is not live, make sure it isn't in the alist. If it
459 ;; is live, verify that nobody else has touched the file since last
461 (if (or (not (and nnfolder-current-buffer
462 (buffer-name nnfolder-current-buffer)))
463 (not (and (bufferp nnfolder-current-buffer)
464 (verify-visited-file-modtime
465 nnfolder-current-buffer))))
467 (if (and nnfolder-current-buffer
468 (buffer-name nnfolder-current-buffer)
469 (bufferp nnfolder-current-buffer))
470 (kill-buffer nnfolder-current-buffer))
471 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
477 (setq file (concat (file-name-as-directory nnfolder-directory)
479 (if (file-directory-p (file-truename file))
481 (if (not (file-exists-p file))
482 (write-region 1 1 file t 'nomesg))
483 (setq nnfolder-current-buffer
484 (set-buffer (nnfolder-read-folder file)))
485 (setq nnfolder-buffer-alist (cons (list group (current-buffer))
486 nnfolder-buffer-alist)))))))
487 (setq nnfolder-current-group group))
489 (defun nnfolder-save-mail (&optional group)
490 "Called narrowed to an article."
491 (let* ((nnmail-split-methods
492 (if group (list (list group "")) nnmail-split-methods))
494 (nreverse (nnmail-article-group 'nnfolder-active-number)))
496 (setq save-list group-art-list)
497 (nnmail-insert-lines)
498 (nnmail-insert-xref group-art-list)
499 (run-hooks 'nnfolder-prepare-save-mail-hook)
501 ;; Insert the mail into each of the destination groups.
502 (while group-art-list
503 (setq group-art (car group-art-list)
504 group-art-list (cdr group-art-list))
506 ;; Kill the previous newsgroup markers.
507 (goto-char (point-min))
508 (search-forward "\n\n" nil t)
510 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
511 (delete-region (point) (progn (forward-line 1) (point))))
513 ;; Insert the new newsgroup marker.
514 (nnfolder-possibly-change-group (car group-art))
515 (nnfolder-insert-newsgroup-line group-art)
516 (let ((beg (point-min))
518 (obuf (current-buffer)))
519 (set-buffer nnfolder-current-buffer)
520 (goto-char (point-max))
521 (insert-buffer-substring obuf beg end)
524 ;; Did we save it anywhere?
527 (defun nnfolder-insert-newsgroup-line (group-art)
529 (goto-char (point-min))
530 (if (search-forward "\n\n" nil t)
533 (insert (format (concat nnfolder-article-marker "%d %s\n")
534 (cdr group-art) (current-time-string)))))))
536 (defun nnfolder-possibly-activate-groups (&optional group)
538 ;; If we're looking for the activation of a specific group, find out
539 ;; it's real name and switch to it.
540 (if group (nnfolder-possibly-change-group group))
541 ;; If the group alist isn't active, activate it now.
542 (if (not nnfolder-group-alist)
544 (nnfolder-request-list)
545 (setq nnfolder-group-alist (nnmail-get-active))))))
547 (defun nnfolder-active-number (group)
549 (nnfolder-possibly-activate-groups group)
550 (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
551 (setcdr active (1+ (cdr active)))
554 ;; This method has a problem if you've accidentally let the active list get
555 ;; out of sync with the files. This could happen, say, if you've
556 ;; accidentally gotten new mail with something other than (ding) (but why
557 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
558 ;; processing the file, ready to add new X-Gnus article number markers, and
559 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
562 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
563 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
564 ;; rest of the message looking for any more, possibly higher IDs. We'll
565 ;; assume the maximum that we find is the highest active. Note that this
566 ;; shouldn't cost us much extra time at all, but will be a lot less
567 ;; vulnerable to glitches between the mbox and the active file.
569 (defun nnfolder-read-folder (file)
571 (nnfolder-possibly-activate-groups nil)
572 ;; We should be paranoid here and make sure the group is in the alist,
573 ;; and add it if it isn't.
574 ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
575 (set-buffer (setq nnfolder-current-buffer (find-file-noselect file)))
576 (buffer-disable-undo (current-buffer))
577 (let ((delim (concat "^" rmail-unix-mail-delimiter))
578 (marker (concat "\n" nnfolder-article-marker))
580 (active (car (cdr (assoc nnfolder-current-group
581 nnfolder-group-alist))))
582 activenumber activemin start end)
583 (goto-char (point-min))
585 ;; Anytime the active number is 1 or 0, it is supect. In that case,
586 ;; search the file manually to find the active number. Or, of course,
587 ;; if we're being paranoid. (This would also be the place to build
588 ;; other lists from the header markers, such as expunge lists, etc., if
589 ;; we ever desired to abandon the active file entirely for mboxes.)
590 (setq activenumber (cdr active))
591 (if (or nnfolder-ignore-active-file
594 (setq activemin (max (1- (lsh 1 23))
597 (while (and (search-forward marker nil t)
598 (re-search-forward number nil t))
599 (let ((newnum (string-to-number (buffer-substring
602 (setq activenumber (max activenumber newnum))
603 (setq activemin (min activemin newnum))))
604 (setcar active (min activemin activenumber))
605 (setcdr active activenumber)
606 (goto-char (point-min))))
608 ;; Keep track of the active number on our own, and insert it back into
609 ;; the active list when we're done. Also, prime the pump to cut down on
610 ;; the number of searches we do.
611 (setq end (point-marker))
612 (set-marker end (or (and (re-search-forward delim nil t)
615 (while (not (= end (point-max)))
616 (setq start (marker-position end))
619 (set-marker end (or (and (re-search-forward delim nil t)
623 (if (not (search-forward marker end t))
625 (narrow-to-region start end)
626 (nnmail-insert-lines)
627 (setq activenumber (1+ activenumber))
628 (nnfolder-insert-newsgroup-line (cons nil activenumber))
631 ;; Make absolutely sure that the active list reflects reality!
632 (setcdr active activenumber)
633 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
636 (defun nnfolder-get-new-mail (&optional group)
637 "Read new incoming mail."
638 (let* ((spools (nnmail-get-spool-files group))
641 (if (or (not nnfolder-get-new-mail) (not nnmail-spool-file))
643 ;; We first activate all the groups.
644 (nnfolder-possibly-activate-groups nil)
645 ;; The we go through all the existing spool files and split the
649 (file-exists-p (car spools))
650 (> (nth 7 (file-attributes (car spools))) 0)
652 (and gnus-verbose-backends
653 (message "nnfolder: Reading incoming mail..."))
657 (concat (file-name-as-directory nnfolder-directory)
659 (setq incomings (cons incoming incomings))
660 (setq group (nnmail-get-split-group (car spools) group-in))
661 (nnmail-split-incoming incoming 'nnfolder-save-mail nil group)))
662 (setq spools (cdr spools)))
663 ;; If we did indeed read any incoming spools, we save all info.
666 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
667 (run-hooks 'nnmail-read-incoming-hook)
668 (and gnus-verbose-backends
669 (message "nnfolder: Reading incoming mail...done"))))
670 (let ((bufs nnfolder-buffer-alist))
673 (if (not (buffer-name (nth 1 (car bufs))))
674 (setq nnfolder-buffer-alist
675 (delq (car bufs) nnfolder-buffer-alist))
676 (set-buffer (nth 1 (car bufs)))
677 (and (buffer-modified-p) (save-buffer)))
678 (setq bufs (cdr bufs)))))
681 nnmail-delete-incoming
682 (file-writable-p incoming)
683 (delete-file incoming))
684 (setq incomings (cdr incomings))))))
688 ;;; nnfolder.el ends here