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.")
71 (defconst nnfolder-version "nnfolder 0.2"
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)
83 (defmacro nnfolder-article-string (article)
84 (` (concat "\n" nnfolder-article-marker (int-to-string (, article)) " ")))
88 (defvar nnfolder-current-server nil)
89 (defvar nnfolder-server-alist nil)
90 (defvar nnfolder-server-variables
92 (list 'nnfolder-directory nnfolder-directory)
93 (list 'nnfolder-active-file nnfolder-active-file)
94 (list 'nnfolder-newsgroups-file nnfolder-newsgroups-file)
95 (list 'nnfolder-get-new-mail nnfolder-get-new-mail)
96 '(nnfolder-current-group nil)
97 '(nnfolder-current-buffer nil)
98 '(nnfolder-status-string "")
99 '(nnfolder-group-alist nil)
100 '(nnfolder-buffer-alist nil)))
104 ;;; Interface functions
106 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server)
108 (set-buffer nntp-server-buffer)
111 (number (length sequence))
112 (delim-string (concat "^" rmail-unix-mail-delimiter))
113 beg article art-string start stop)
114 (nnfolder-possibly-change-group newsgroup)
115 (set-buffer nnfolder-current-buffer)
116 (goto-char (point-min))
118 (setq article (car sequence))
119 (setq art-string (nnfolder-article-string article))
120 (set-buffer nnfolder-current-buffer)
121 (if (or (search-forward art-string nil t)
122 ;; Don't search the whole file twice! Also, articles
123 ;; probably have some locality by number, so searching
124 ;; backwards will be faster. Especially if we're at the
125 ;; beginning of the buffer :-). -SLB
126 (search-backward art-string nil t))
128 (setq start (or (re-search-backward delim-string nil t)
130 (search-forward "\n\n" nil t)
131 (setq stop (1- (point)))
132 (set-buffer nntp-server-buffer)
133 (insert (format "221 %d Article retrieved.\n" article))
135 (insert-buffer-substring nnfolder-current-buffer start stop)
136 (goto-char (point-max))
138 (setq sequence (cdr sequence)))
140 ;; Fold continuation lines.
141 (set-buffer nntp-server-buffer)
142 (goto-char (point-min))
143 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
144 (replace-match " " t t))
147 (defun nnfolder-open-server (server &optional defs)
148 (nnheader-init-server-buffer)
149 (if (equal server nnfolder-current-server)
151 (if nnfolder-current-server
152 (setq nnfolder-server-alist
153 (cons (list nnfolder-current-server
154 (nnheader-save-variables nnfolder-server-variables))
155 nnfolder-server-alist)))
156 (let ((state (assoc server nnfolder-server-alist)))
159 (nnheader-restore-variables (nth 1 state))
160 (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
161 (nnheader-set-init-variables nnfolder-server-variables defs)))
162 (setq nnfolder-current-server server)))
164 (defun nnfolder-close-server (&optional server)
167 (defun nnfolder-server-opened (&optional server)
168 (and (equal server nnfolder-current-server)
170 (buffer-name nntp-server-buffer)))
172 (defun nnfolder-request-close ()
173 (let ((alist nnfolder-buffer-alist))
175 (nnfolder-close-group (car (car alist)) nil t)
176 (setq alist (cdr alist))))
177 (setq nnfolder-buffer-alist 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)
185 (if (stringp article)
188 (set-buffer nnfolder-current-buffer)
189 (goto-char (point-min))
190 (if (search-forward (nnfolder-article-string article) nil t)
192 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
195 (or (and (re-search-forward
196 (concat "^" rmail-unix-mail-delimiter) nil t)
198 (goto-char (point-max)))
200 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
201 (set-buffer nntp-server-buffer)
203 (insert-buffer-substring nnfolder-current-buffer start stop)
204 (goto-char (point-min))
205 (while (looking-at "From ")
207 (insert "X-From-Line: ")
211 (defun nnfolder-request-group (group &optional server dont-check)
213 (nnfolder-possibly-change-group group)
214 (and (assoc group nnfolder-group-alist)
218 (nnfolder-get-new-mail))
219 (let* ((active (assoc group nnfolder-group-alist))
221 (range (car (cdr active)))
222 (minactive (car range))
223 (maxactive (cdr range)))
224 ;; I've been getting stray 211 lines in my nnfolder active
225 ;; file. So, let's make sure that doesn't happen. -SLB
226 (set-buffer nntp-server-buffer)
228 (insert (format "211 %d %d %d %s\n"
229 (1+ (- maxactive minactive))
230 minactive maxactive group))
233 ;; Don't close the buffer if we're not shutting down the server. This way,
234 ;; we can keep the buffer in the group buffer cache, and not have to grovel
235 ;; over the buffer again unless we add new mail to it or modify it in some
238 (defun nnfolder-close-group (group &optional server force)
239 (nnfolder-possibly-change-group group)
241 (set-buffer nnfolder-current-buffer)
242 ;; If the buffer was modified, write the file out now.
243 (and (buffer-modified-p) (save-buffer))
245 nnfolder-always-close)
246 ;; If we're shutting the server down, we need to kill the buffer and
247 ;; remove it from the open buffer list. Or, of course, if we're
248 ;; trying to minimize our space impact.
250 (kill-buffer (current-buffer))
251 (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
252 nnfolder-buffer-alist))))
253 (setq nnfolder-current-group nil
254 nnfolder-current-buffer nil))
257 (defun nnfolder-request-list (&optional server)
258 (if server (nnfolder-get-new-mail))
260 (or nnfolder-group-alist
261 (nnmail-find-file nnfolder-active-file)
263 (setq nnfolder-group-alist (nnmail-get-active))
264 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
265 (nnmail-find-file nnfolder-active-file)))))
267 (defun nnfolder-request-newgroups (date &optional server)
268 (nnfolder-request-list server))
270 (defun nnfolder-request-list-newsgroups (&optional server)
272 (nnmail-find-file nnfolder-newsgroups-file)))
274 (defun nnfolder-request-post (&optional server)
275 (mail-send-and-exit nil))
277 (fset 'nnfolder-request-post-buffer 'nnmail-request-post-buffer)
279 (defun nnfolder-request-expire-articles (articles newsgroup &optional server force)
280 (nnfolder-possibly-change-group newsgroup)
281 (let* ((days (or (and nnmail-expiry-wait-function
282 (funcall nnmail-expiry-wait-function newsgroup))
286 (set-buffer nnfolder-current-buffer)
288 (goto-char (point-min))
289 (if (search-forward (nnfolder-article-string (car articles)) nil t)
291 (> (nnmail-days-between
292 (current-time-string)
294 (point) (progn (end-of-line) (point))))
297 (and gnus-verbose-backends
298 (message "Deleting: %s" (car articles)))
299 (nnfolder-delete-mail))
300 (setq rest (cons (car articles) rest))))
301 (setq articles (cdr articles)))
302 (and (buffer-modified-p) (save-buffer))
303 ;; Find the lowest active article in this group.
304 (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
305 (marker (concat "\n" nnfolder-article-marker))
307 (activemin (cdr active)))
308 (goto-char (point-min))
309 (while (and (search-forward marker nil t)
310 (re-search-forward number nil t))
311 (setq activemin (min activemin
312 (string-to-number (buffer-substring
315 (setcar active activemin))
316 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
319 (defun nnfolder-request-move-article
320 (article group server accept-form &optional last)
321 (nnfolder-possibly-change-group group)
322 (let ((buf (get-buffer-create " *nnfolder move*"))
325 (nnfolder-request-article article group server)
328 (buffer-disable-undo (current-buffer))
330 (insert-buffer-substring nntp-server-buffer)
331 (goto-char (point-min))
332 (while (re-search-forward
334 (save-excursion (search-forward "\n\n" nil t) (point)) t)
335 (delete-region (progn (beginning-of-line) (point))
336 (progn (forward-line 1) (point))))
337 (setq result (eval accept-form))
341 (nnfolder-possibly-change-group group)
342 (set-buffer nnfolder-current-buffer)
343 (goto-char (point-min))
344 (if (search-forward (nnfolder-article-string article) nil t)
345 (nnfolder-delete-mail))
351 (defun nnfolder-request-accept-article (group &optional last)
352 (nnfolder-possibly-change-group group)
353 (let ((buf (current-buffer))
355 (goto-char (point-min))
356 (if (looking-at "X-From-Line: ")
357 (replace-match "From ")
358 (insert "From nobody " (current-time-string) "\n"))
360 (nnfolder-request-list)
363 (goto-char (point-min))
364 (search-forward "\n\n" nil t)
366 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
367 (delete-region (point) (progn (forward-line 1) (point))))
368 (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
370 (set-buffer nnfolder-current-buffer)
371 (insert-buffer-substring buf)
372 (and last (buffer-modified-p) (save-buffer))
374 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
377 (defun nnfolder-request-replace-article (article group buffer)
378 (nnfolder-possibly-change-group group)
380 (set-buffer nnfolder-current-buffer)
381 (goto-char (point-min))
382 (if (not (search-forward (nnfolder-article-string article) nil t))
384 (nnfolder-delete-mail t t)
385 (insert-buffer-substring buffer)
386 (and (buffer-modified-p) (save-buffer))
390 ;;; Internal functions.
392 (defun nnfolder-delete-mail (&optional force leave-delim)
393 ;; Beginning of the article.
398 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
399 (if leave-delim (progn (forward-line 1) (point))
400 (match-beginning 0)))
403 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
405 (if (and (not (bobp)) leave-delim)
406 (progn (forward-line -2) (point))
407 (match-beginning 0)))
409 (delete-region (point-min) (point-max)))))
411 (defun nnfolder-possibly-change-group (group)
412 (or (file-exists-p nnfolder-directory)
413 (make-directory (directory-file-name nnfolder-directory)))
414 (if (not nnfolder-group-alist)
416 (nnfolder-request-list)
417 (setq nnfolder-group-alist (nnmail-get-active))))
418 (or (assoc group nnfolder-group-alist)
419 (not (file-exists-p (concat nnfolder-directory group)))
421 (setq nnfolder-group-alist
422 (cons (list group (cons 1 0)) nnfolder-group-alist))
423 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
425 (if (and (equal group nnfolder-current-group)
426 (buffer-name nnfolder-current-buffer))
428 (setq nnfolder-current-group group)
430 ;; If we have to change groups, see if we don't already have the mbox
431 ;; in memory. If we do, verify the modtime and destroy the mbox if
432 ;; needed so we can rescan it.
433 (if (setq inf (assoc group nnfolder-buffer-alist))
434 (setq nnfolder-current-buffer (nth 1 inf)))
436 ;; If the buffer is not live, make sure it isn't in the alist. If it
437 ;; is live, verify that nobody else has touched the file since last
439 (if (or (not (buffer-name nnfolder-current-buffer))
440 (not (and (bufferp nnfolder-current-buffer)
441 (verify-visited-file-modtime nnfolder-current-buffer))))
443 (if (and (buffer-name nnfolder-current-buffer)
444 (bufferp nnfolder-current-buffer))
445 (kill-buffer nnfolder-current-buffer))
446 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
452 (setq file (concat nnfolder-directory group))
453 (if (not (file-exists-p file))
454 (write-region 1 1 file t 'nomesg))
455 (set-buffer (nnfolder-read-folder file))
456 (setq nnfolder-buffer-alist (cons (list group (current-buffer))
457 nnfolder-buffer-alist))))))
458 (setq nnfolder-current-group group))
460 (defun nnfolder-save-mail (&optional group)
461 "Called narrowed to an article."
462 (let* ((nnmail-split-methods
463 (if group (list (list group "")) nnmail-split-methods))
465 (nreverse (nnmail-article-group 'nnfolder-active-number)))
467 (setq save-list group-art-list)
468 (nnmail-insert-lines)
469 (nnmail-insert-xref group-art-list)
471 ;; Insert the mail into each of the destination groups.
472 (while group-art-list
473 (setq group-art (car group-art-list)
474 group-art-list (cdr group-art-list))
476 ;; Kill the previous newsgroup markers.
477 (goto-char (point-min))
478 (search-forward "\n\n" nil t)
480 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
481 (delete-region (point) (progn (forward-line 1) (point))))
483 ;; Insert the new newsgroup marker.
484 (nnfolder-possibly-change-group (car group-art))
485 (nnfolder-insert-newsgroup-line group-art)
486 (let ((beg (point-min))
488 (obuf (current-buffer)))
489 (set-buffer nnfolder-current-buffer)
490 (goto-char (point-max))
491 (insert-buffer-substring obuf beg end)
494 ;; Did we save it anywhere?
497 (defun nnfolder-insert-newsgroup-line (group-art)
499 (goto-char (point-min))
500 (if (search-forward "\n\n" nil t)
503 (insert (format (concat nnfolder-article-marker "%d %s\n")
504 (cdr group-art) (current-time-string)))))))
506 (defun nnfolder-active-number (group)
507 (if (not nnfolder-group-alist)
509 (nnfolder-request-list)
510 (setq nnfolder-group-alist (nnmail-get-active))))
511 (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
512 (setcdr active (1+ (cdr active)))
516 ;; This method has a problem if you've accidentally let the active list get
517 ;; out of sync with the files. This could happen, say, if you've
518 ;; accidentally gotten new mail with something other than (ding) (but why
519 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
520 ;; processing the file, ready to add new X-Gnus article number markers, and
521 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
524 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
525 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
526 ;; rest of the message looking for any more, possibly higher IDs. We'll
527 ;; assume the maximum that we find is the highest active. Note that this
528 ;; shouldn't cost us much extra time at all, but will be a lot less
529 ;; vulnerable to glitches between the mbox and the active file.
531 (defun nnfolder-read-folder (file)
533 (if (not nnfolder-group-alist)
535 (nnfolder-request-list)
536 (setq nnfolder-group-alist (nnmail-get-active))))
537 ;; We should be paranoid here and make sure the group is in the alist,
538 ;; and add it if it isn't.
539 ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
540 (set-buffer (setq nnfolder-current-buffer (find-file-noselect file)))
541 (buffer-disable-undo (current-buffer))
542 (let ((delim (concat "^" rmail-unix-mail-delimiter))
543 (marker (concat "\n" nnfolder-article-marker))
545 (active (car (cdr (assoc nnfolder-current-group
546 nnfolder-group-alist))))
547 activenumber activemin start end)
548 (goto-char (point-min))
550 ;; Anytime the active number is 1 or 0, it is supect. In that case,
551 ;; search the file manually to find the active number. Or, of course,
552 ;; if we're being paranoid. (This would also be the place to build
553 ;; other lists from the header markers, such as expunge lists, etc., if
554 ;; we ever desired to abandon the active file entirely for mboxes.)
555 (setq activenumber (cdr active))
556 (if (or nnfolder-ignore-active-file
559 (setq activemin (max (1- (lsh 1 23))
562 (while (and (search-forward marker nil t)
563 (re-search-forward number nil t))
564 (let ((newnum (string-to-number (buffer-substring
567 (setq activenumber (max activenumber newnum))
568 (setq activemin (min activemin newnum))))
569 (setcar active (min activemin activenumber))
570 (setcdr active activenumber)
571 (goto-char (point-min))))
573 ;; Keep track of the active number on our own, and insert it back into
574 ;; the active list when we're done. Also, prime the pump to cut down on
575 ;; the number of searches we do.
576 (setq end (point-marker))
577 (set-marker end (or (and (re-search-forward delim nil t)
580 (while (not (= end (point-max)))
581 (setq start (marker-position end))
584 (set-marker end (or (and (re-search-forward delim nil t)
588 (if (not (search-forward marker end t))
590 (narrow-to-region start end)
591 (nnmail-insert-lines)
592 (setq activenumber (1+ activenumber))
593 (nnfolder-insert-newsgroup-line (cons nil activenumber))
596 ;; Make absolutely sure that the active list reflects reality!
597 (setcdr active activenumber)
598 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
601 (defun nnfolder-get-new-mail ()
602 "Read new incoming mail."
603 (let ((spools (if (listp nnmail-spool-file) nnmail-spool-file
604 (list nnmail-spool-file)))
606 (if (or (not nnfolder-get-new-mail) (not nnmail-spool-file))
608 ;; We first activate all the groups.
609 (nnfolder-request-list)
610 (setq nnfolder-group-alist (nnmail-get-active))
611 ;; The we go through all the existing spool files and split the
615 (file-exists-p (car spools))
616 (> (nth 7 (file-attributes (car spools))) 0)
618 (and gnus-verbose-backends
619 (message "nnfolder: Reading incoming mail..."))
622 (car spools) (concat nnfolder-directory "Incoming")))
623 (setq incomings (cons incoming incomings))
624 (nnmail-split-incoming incoming 'nnfolder-save-mail)))
625 (setq spools (cdr spools)))
626 ;; If we did indeed read any incoming spools, we save all info.
629 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
630 (run-hooks 'nnmail-read-incoming-hook)
631 (and gnus-verbose-backends
632 (message "nnfolder: Reading incoming mail...done"))))
633 (let ((bufs nnfolder-buffer-alist))
636 (if (not (buffer-name (nth 1 (car bufs))))
637 (setq nnfolder-buffer-alist
638 (delq (car bufs) nnfolder-buffer-alist))
639 (set-buffer (nth 1 (car bufs)))
640 (and (buffer-modified-p) (save-buffer)))
641 (setq bufs (cdr bufs)))))
643 ;; The following has been commented away, just to make sure
644 ;; that nobody ever loses any mail. If you feel safe that
645 ;; nnfolder will never do anything strange, just remove those
646 ;; two semicolons, and avoid having lots of "Incoming*"
648 ;; (and (file-writable-p incoming) (delete-file incoming))
649 (setq incomings (cdr incomings))))))
653 ;;; nnfolder.el ends here