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 (concat nnfolder-directory "active")
42 "The name of the active file.")
44 ;; I renamed this variable to somehting more in keeping with the general GNU
47 (defvar nnfolder-ignore-active-file nil
48 "If non-nil, causes nnfolder to do some extra work in order to determine the true active ranges of an mbox file.
49 Note that the active file is still saved, but it's values are not
50 used. This costs some extra time when scanning an mbox when opening
53 ;; Note that this variable may not be completely implemented yet. -SLB
55 (defvar nnfolder-always-close nil
56 "If non-nil, nnfolder attempts to only ever have one mbox open at a time.
57 This is a straight space/performance trade off, as the mboxes will have to
58 be scaned every time they are read in. If nil (default), nnfolder will
59 attempt to keep the buffers around (saving the nnfolder's buffer upon group
60 close, but not killing it), speeding some things up tremendously, especially
61 such things as moving mail. All buffers always get killed upon server close.")
63 (defvar nnfolder-newsgroups-file (concat nnfolder-directory "newsgroups")
64 "Mail newsgroups description file.")
66 (defvar nnfolder-get-new-mail t
67 "If non-nil, nnml will check the incoming mail file and split the mail.")
69 (defvar nnfolder-prepare-save-mail-hook nil
70 "Hook run narrowed to an article before saving.")
74 (defconst nnfolder-version "nnfolder 0.2"
77 (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
78 "String used to demarcate what the article number for a message is.")
80 (defvar nnfolder-current-group nil)
81 (defvar nnfolder-current-buffer nil)
82 (defvar nnfolder-status-string "")
83 (defvar nnfolder-group-alist nil)
84 (defvar nnfolder-buffer-alist nil)
86 (defmacro nnfolder-article-string (article)
87 (` (concat "\n" nnfolder-article-marker (int-to-string (, article)) " ")))
91 (defvar nnfolder-current-server nil)
92 (defvar nnfolder-server-alist nil)
93 (defvar nnfolder-server-variables
95 (list 'nnfolder-directory nnfolder-directory)
96 (list 'nnfolder-active-file nnfolder-active-file)
97 (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
98 (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
99 '(nnfolder-current-group nil)
100 '(nnfolder-current-buffer nil)
101 '(nnfolder-status-string "")
102 '(nnfolder-group-alist nil)
103 '(nnfolder-buffer-alist nil)))
107 ;;; Interface functions
109 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server)
111 (set-buffer nntp-server-buffer)
114 (number (length sequence))
115 (delim-string (concat "^" rmail-unix-mail-delimiter))
116 beg 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))
140 (insert-buffer-substring nnfolder-current-buffer start stop)
141 (goto-char (point-max))
143 (setq sequence (cdr sequence)))
145 ;; Fold continuation lines.
146 (set-buffer nntp-server-buffer)
147 (goto-char (point-min))
148 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
149 (replace-match " " t t))
152 (defun nnfolder-open-server (server &optional defs)
153 (nnheader-init-server-buffer)
154 (if (equal server nnfolder-current-server)
156 (if nnfolder-current-server
157 (setq nnfolder-server-alist
158 (cons (list nnfolder-current-server
159 (nnheader-save-variables nnfolder-server-variables))
160 nnfolder-server-alist)))
161 (let ((state (assoc server nnfolder-server-alist)))
164 (nnheader-restore-variables (nth 1 state))
165 (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
166 (nnheader-set-init-variables nnfolder-server-variables defs)))
167 (setq nnfolder-current-server server)))
169 (defun nnfolder-close-server (&optional server)
172 (defun nnfolder-server-opened (&optional server)
173 (and (equal server nnfolder-current-server)
175 (buffer-name nntp-server-buffer)))
177 (defun nnfolder-request-close ()
178 (let ((alist nnfolder-buffer-alist))
180 (nnfolder-close-group (car (car alist)) nil t)
181 (setq alist (cdr alist))))
182 (setq nnfolder-buffer-alist nil
183 nnfolder-group-alist nil))
185 (defun nnfolder-status-message (&optional server)
186 nnfolder-status-string)
188 (defun nnfolder-request-article (article &optional newsgroup server buffer)
189 (nnfolder-possibly-change-group newsgroup)
190 (if (stringp article)
193 (set-buffer nnfolder-current-buffer)
194 (goto-char (point-min))
195 (if (search-forward (nnfolder-article-string article) nil t)
197 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
200 (or (and (re-search-forward
201 (concat "^" rmail-unix-mail-delimiter) nil t)
203 (goto-char (point-max)))
205 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
206 (set-buffer nntp-server-buffer)
208 (insert-buffer-substring nnfolder-current-buffer start stop)
209 (goto-char (point-min))
210 (while (looking-at "From ")
212 (insert "X-From-Line: ")
216 (defun nnfolder-request-group (group &optional server dont-check)
218 (nnfolder-possibly-change-group group)
219 (and (assoc group nnfolder-group-alist)
223 (nnfolder-get-new-mail group))
224 (let* ((active (assoc group nnfolder-group-alist))
226 (range (car (cdr active)))
227 (minactive (car range))
228 (maxactive (cdr range)))
229 ;; I've been getting stray 211 lines in my nnfolder active
230 ;; file. So, let's make sure that doesn't happen. -SLB
231 (set-buffer nntp-server-buffer)
233 (insert (format "211 %d %d %d %s\n"
234 (1+ (- maxactive minactive))
235 minactive maxactive group))
238 ;; Don't close the buffer if we're not shutting down the server. This way,
239 ;; we can keep the buffer in the group buffer cache, and not have to grovel
240 ;; over the buffer again unless we add new mail to it or modify it in some
243 (defun nnfolder-close-group (group &optional server force)
244 ;; Make sure we _had_ the group open.
245 (if (or (assoc group nnfolder-buffer-alist)
246 (equal group nnfolder-current-group))
248 (nnfolder-possibly-change-group group)
250 (set-buffer nnfolder-current-buffer)
251 ;; If the buffer was modified, write the file out now.
252 (and (buffer-modified-p) (save-buffer))
254 nnfolder-always-close)
255 ;; If we're shutting the server down, we need to kill the
256 ;; buffer and remove it from the open buffer list. Or, of
257 ;; course, if we're trying to minimize our space impact.
259 (kill-buffer (current-buffer))
260 (setq nnfolder-buffer-alist (delq (assoc group
261 nnfolder-buffer-alist)
262 nnfolder-buffer-alist)))))))
263 (setq nnfolder-current-group nil
264 nnfolder-current-buffer nil)
267 (defun nnfolder-request-list (&optional server)
268 (if server (nnfolder-get-new-mail))
270 (or nnfolder-group-alist
271 (nnmail-find-file nnfolder-active-file)
273 (setq nnfolder-group-alist (nnmail-get-active))
274 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
275 (nnmail-find-file nnfolder-active-file)))))
277 (defun nnfolder-request-newgroups (date &optional server)
278 (nnfolder-request-list server))
280 (defun nnfolder-request-list-newsgroups (&optional server)
282 (nnmail-find-file nnfolder-newsgroups-file)))
284 (defun nnfolder-request-post (&optional server)
285 (mail-send-and-exit nil))
287 (defalias 'nnfolder-request-post-buffer 'nnmail-request-post-buffer)
289 (defun nnfolder-request-expire-articles (articles newsgroup &optional server force)
290 (nnfolder-possibly-change-group newsgroup)
291 (let* ((days (or (and nnmail-expiry-wait-function
292 (funcall nnmail-expiry-wait-function newsgroup))
296 (set-buffer nnfolder-current-buffer)
298 (goto-char (point-min))
299 (if (search-forward (nnfolder-article-string (car articles)) nil t)
301 (> (nnmail-days-between
302 (current-time-string)
304 (point) (progn (end-of-line) (point))))
307 (and gnus-verbose-backends
308 (message "Deleting: %s" (car articles)))
309 (nnfolder-delete-mail))
310 (setq rest (cons (car articles) rest))))
311 (setq articles (cdr articles)))
312 (and (buffer-modified-p) (save-buffer))
313 ;; Find the lowest active article in this group.
314 (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
315 (marker (concat "\n" nnfolder-article-marker))
317 (activemin (cdr active)))
318 (goto-char (point-min))
319 (while (and (search-forward marker nil t)
320 (re-search-forward number nil t))
321 (setq activemin (min activemin
322 (string-to-number (buffer-substring
325 (setcar active activemin))
326 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
329 (defun nnfolder-request-move-article
330 (article group server accept-form &optional last)
331 (nnfolder-possibly-change-group group)
332 (let ((buf (get-buffer-create " *nnfolder move*"))
335 (nnfolder-request-article article group server)
338 (buffer-disable-undo (current-buffer))
340 (insert-buffer-substring nntp-server-buffer)
341 (goto-char (point-min))
342 (while (re-search-forward
344 (save-excursion (search-forward "\n\n" nil t) (point)) t)
345 (delete-region (progn (beginning-of-line) (point))
346 (progn (forward-line 1) (point))))
347 (setq result (eval accept-form))
351 (nnfolder-possibly-change-group group)
352 (set-buffer nnfolder-current-buffer)
353 (goto-char (point-min))
354 (if (search-forward (nnfolder-article-string article) nil t)
355 (nnfolder-delete-mail))
361 (defun nnfolder-request-accept-article (group &optional last)
362 (nnfolder-possibly-change-group group)
363 (let ((buf (current-buffer))
365 (goto-char (point-min))
366 (if (looking-at "X-From-Line: ")
367 (replace-match "From ")
368 (insert "From nobody " (current-time-string) "\n"))
370 (nnfolder-request-list)
373 (goto-char (point-min))
374 (search-forward "\n\n" nil t)
376 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
377 (delete-region (point) (progn (forward-line 1) (point))))
378 (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
380 (set-buffer nnfolder-current-buffer)
381 (insert-buffer-substring buf)
382 (and last (buffer-modified-p) (save-buffer))
384 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
387 (defun nnfolder-request-replace-article (article group buffer)
388 (nnfolder-possibly-change-group group)
390 (set-buffer nnfolder-current-buffer)
391 (goto-char (point-min))
392 (if (not (search-forward (nnfolder-article-string article) nil t))
394 (nnfolder-delete-mail t t)
395 (insert-buffer-substring buffer)
396 (and (buffer-modified-p) (save-buffer))
400 ;;; Internal functions.
402 (defun nnfolder-delete-mail (&optional force leave-delim)
403 ;; Beginning of the article.
408 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
409 (if leave-delim (progn (forward-line 1) (point))
410 (match-beginning 0)))
413 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
415 (if (and (not (bobp)) leave-delim)
416 (progn (forward-line -2) (point))
417 (match-beginning 0)))
419 (delete-region (point-min) (point-max)))))
421 (defun nnfolder-possibly-change-group (group)
422 (or (file-exists-p nnfolder-directory)
423 (make-directory (directory-file-name nnfolder-directory)))
424 (nnfolder-possibly-activate-groups nil)
425 (or (assoc group nnfolder-group-alist)
426 (not (file-exists-p (concat nnfolder-directory group)))
428 (setq nnfolder-group-alist
429 (cons (list group (cons 1 0)) nnfolder-group-alist))
430 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
432 (if (and (equal group nnfolder-current-group)
433 (buffer-name nnfolder-current-buffer))
435 (setq nnfolder-current-group group)
437 ;; If we have to change groups, see if we don't already have the mbox
438 ;; in memory. If we do, verify the modtime and destroy the mbox if
439 ;; needed so we can rescan it.
440 (if (setq inf (assoc group nnfolder-buffer-alist))
441 (setq nnfolder-current-buffer (nth 1 inf)))
443 ;; If the buffer is not live, make sure it isn't in the alist. If it
444 ;; is live, verify that nobody else has touched the file since last
446 (if (or (not (buffer-name nnfolder-current-buffer))
447 (not (and (bufferp nnfolder-current-buffer)
448 (verify-visited-file-modtime nnfolder-current-buffer))))
450 (if (and (buffer-name nnfolder-current-buffer)
451 (bufferp nnfolder-current-buffer))
452 (kill-buffer nnfolder-current-buffer))
453 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
459 (setq file (concat nnfolder-directory group))
460 (if (not (file-exists-p file))
461 (write-region 1 1 file t 'nomesg))
462 (set-buffer (nnfolder-read-folder file))
463 (setq nnfolder-buffer-alist (cons (list group (current-buffer))
464 nnfolder-buffer-alist))))))
465 (setq nnfolder-current-group group))
467 (defun nnfolder-save-mail (&optional group)
468 "Called narrowed to an article."
469 (let* ((nnmail-split-methods
470 (if group (list (list group "")) nnmail-split-methods))
472 (nreverse (nnmail-article-group 'nnfolder-active-number)))
474 (setq save-list group-art-list)
475 (nnmail-insert-lines)
476 (nnmail-insert-xref group-art-list)
477 (run-hooks 'nnfolder-prepare-save-mail-hook)
479 ;; Insert the mail into each of the destination groups.
480 (while group-art-list
481 (setq group-art (car group-art-list)
482 group-art-list (cdr group-art-list))
484 ;; Kill the previous newsgroup markers.
485 (goto-char (point-min))
486 (search-forward "\n\n" nil t)
488 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
489 (delete-region (point) (progn (forward-line 1) (point))))
491 ;; Insert the new newsgroup marker.
492 (nnfolder-possibly-change-group (car group-art))
493 (nnfolder-insert-newsgroup-line group-art)
494 (let ((beg (point-min))
496 (obuf (current-buffer)))
497 (set-buffer nnfolder-current-buffer)
498 (goto-char (point-max))
499 (insert-buffer-substring obuf beg end)
502 ;; Did we save it anywhere?
505 (defun nnfolder-insert-newsgroup-line (group-art)
507 (goto-char (point-min))
508 (if (search-forward "\n\n" nil t)
511 (insert (format (concat nnfolder-article-marker "%d %s\n")
512 (cdr group-art) (current-time-string)))))))
514 (defun nnfolder-possibly-activate-groups (&optional group)
516 ;; If we're looking for the activation of a specific group, find out
517 ;; it's real name and switch to it.
518 (if group (nnfolder-possibly-change-group group))
519 ;; If the group alist isn't active, activate it now.
520 (if (not nnfolder-group-alist)
522 (nnfolder-request-list)
523 (setq nnfolder-group-alist (nnmail-get-active))))))
525 (defun nnfolder-active-number (group)
527 (nnfolder-possibly-activate-groups group)
528 (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
529 (setcdr active (1+ (cdr active)))
532 ;; This method has a problem if you've accidentally let the active list get
533 ;; out of sync with the files. This could happen, say, if you've
534 ;; accidentally gotten new mail with something other than (ding) (but why
535 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
536 ;; processing the file, ready to add new X-Gnus article number markers, and
537 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
540 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
541 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
542 ;; rest of the message looking for any more, possibly higher IDs. We'll
543 ;; assume the maximum that we find is the highest active. Note that this
544 ;; shouldn't cost us much extra time at all, but will be a lot less
545 ;; vulnerable to glitches between the mbox and the active file.
547 (defun nnfolder-read-folder (file)
549 (nnfolder-possibly-activate-groups nil)
550 ;; We should be paranoid here and make sure the group is in the alist,
551 ;; and add it if it isn't.
552 ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
553 (set-buffer (setq nnfolder-current-buffer (find-file-noselect file)))
554 (buffer-disable-undo (current-buffer))
555 (let ((delim (concat "^" rmail-unix-mail-delimiter))
556 (marker (concat "\n" nnfolder-article-marker))
558 (active (car (cdr (assoc nnfolder-current-group
559 nnfolder-group-alist))))
560 activenumber activemin start end)
561 (goto-char (point-min))
563 ;; Anytime the active number is 1 or 0, it is supect. In that case,
564 ;; search the file manually to find the active number. Or, of course,
565 ;; if we're being paranoid. (This would also be the place to build
566 ;; other lists from the header markers, such as expunge lists, etc., if
567 ;; we ever desired to abandon the active file entirely for mboxes.)
568 (setq activenumber (cdr active))
569 (if (or nnfolder-ignore-active-file
572 (setq activemin (max (1- (lsh 1 23))
575 (while (and (search-forward marker nil t)
576 (re-search-forward number nil t))
577 (let ((newnum (string-to-number (buffer-substring
580 (setq activenumber (max activenumber newnum))
581 (setq activemin (min activemin newnum))))
582 (setcar active (min activemin activenumber))
583 (setcdr active activenumber)
584 (goto-char (point-min))))
586 ;; Keep track of the active number on our own, and insert it back into
587 ;; the active list when we're done. Also, prime the pump to cut down on
588 ;; the number of searches we do.
589 (setq end (point-marker))
590 (set-marker end (or (and (re-search-forward delim nil t)
593 (while (not (= end (point-max)))
594 (setq start (marker-position end))
597 (set-marker end (or (and (re-search-forward delim nil t)
601 (if (not (search-forward marker end t))
603 (narrow-to-region start end)
604 (nnmail-insert-lines)
605 (setq activenumber (1+ activenumber))
606 (nnfolder-insert-newsgroup-line (cons nil activenumber))
609 ;; Make absolutely sure that the active list reflects reality!
610 (setcdr active activenumber)
611 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
614 (defun nnfolder-get-new-mail (&optional group)
615 "Read new incoming mail."
616 (let ((spools (nnmail-get-spool-files group))
618 (if (or (not nnfolder-get-new-mail) (not nnmail-spool-file))
620 ;; We first activate all the groups.
621 (nnfolder-possibly-activate-groups nil)
622 ;; The we go through all the existing spool files and split the
626 (file-exists-p (car spools))
627 (> (nth 7 (file-attributes (car spools))) 0)
629 (and gnus-verbose-backends
630 (message "nnfolder: Reading incoming mail..."))
633 (car spools) (concat nnfolder-directory "Incoming")))
634 (setq incomings (cons incoming incomings))
635 (nnmail-split-incoming incoming 'nnfolder-save-mail nil group)))
636 (setq spools (cdr spools)))
637 ;; If we did indeed read any incoming spools, we save all info.
640 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
641 (run-hooks 'nnmail-read-incoming-hook)
642 (and gnus-verbose-backends
643 (message "nnfolder: Reading incoming mail...done"))))
644 (let ((bufs nnfolder-buffer-alist))
647 (if (not (buffer-name (nth 1 (car bufs))))
648 (setq nnfolder-buffer-alist
649 (delq (car bufs) nnfolder-buffer-alist))
650 (set-buffer (nth 1 (car bufs)))
651 (and (buffer-modified-p) (save-buffer)))
652 (setq bufs (cdr bufs)))))
654 ;; The following has been commented away, just to make sure
655 ;; that nobody ever loses any mail. If you feel safe that
656 ;; nnfolder will never do anything strange, just remove those
657 ;; two semicolons, and avoid having lots of "Incoming*"
659 ;; (and (file-writable-p incoming) (delete-file incoming))
660 (setq incomings (cdr incomings))))))
664 ;;; nnfolder.el ends here