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)
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))
259 (or nnfolder-group-alist
260 (nnmail-find-file nnfolder-active-file)
262 (setq nnfolder-group-alist (nnmail-get-active))
263 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
264 (nnmail-find-file nnfolder-active-file))))
266 (defun nnfolder-request-newgroups (date &optional server)
267 (nnfolder-request-list server))
269 (defun nnfolder-request-list-newsgroups (&optional server)
270 (nnmail-find-file nnfolder-newsgroups-file))
272 (defun nnfolder-request-post (&optional server)
273 (mail-send-and-exit nil))
275 (fset 'nnfolder-request-post-buffer 'nnmail-request-post-buffer)
277 (defun nnfolder-request-expire-articles (articles newsgroup &optional server force)
278 (nnfolder-possibly-change-group newsgroup)
279 (let* ((days (or (and nnmail-expiry-wait-function
280 (funcall nnmail-expiry-wait-function newsgroup))
284 (set-buffer nnfolder-current-buffer)
287 (if (search-forward (nnfolder-article-string (car articles)) nil t)
289 (> (nnmail-days-between
290 (current-time-string)
292 (point) (progn (end-of-line) (point))))
295 (and gnus-verbose-backends
296 (message "Deleting: %s" (car articles)))
297 (nnfolder-delete-mail))
298 (setq rest (cons (car articles) rest))))
299 (setq articles (cdr articles)))
300 (and (buffer-modified-p) (save-buffer))
301 ;; Find the lowest active article in this group.
302 (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
303 (marker (concat "\n" nnfolder-article-marker))
305 (activemin (cdr active)))
306 (goto-char (point-min))
307 (while (and (search-forward marker nil t)
308 (re-search-forward number nil t))
309 (setq activemin (min activemin
310 (string-to-number (buffer-substring
313 (setcar active activemin))
314 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
317 (defun nnfolder-request-move-article
318 (article group server accept-form &optional last)
319 (nnfolder-possibly-change-group group)
320 (let ((buf (get-buffer-create " *nnfolder move*"))
323 (nnfolder-request-article article group server)
326 (buffer-disable-undo (current-buffer))
328 (insert-buffer-substring nntp-server-buffer)
329 (goto-char (point-min))
330 (while (re-search-forward
332 (save-excursion (search-forward "\n\n" nil t) (point)) t)
333 (delete-region (progn (beginning-of-line) (point))
334 (progn (forward-line 1) (point))))
335 (setq result (eval accept-form))
339 (nnfolder-possibly-change-group group)
340 (set-buffer nnfolder-current-buffer)
342 (if (search-forward (nnfolder-article-string article) nil t)
343 (nnfolder-delete-mail))
349 (defun nnfolder-request-accept-article (group &optional last)
350 (nnfolder-possibly-change-group group)
351 (let ((buf (current-buffer))
353 (goto-char (point-min))
354 (if (looking-at "X-From-Line: ")
355 (replace-match "From ")
356 (insert "From nobody " (current-time-string) "\n"))
358 (nnfolder-request-list)
361 (goto-char (point-min))
362 (search-forward "\n\n" nil t)
364 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
365 (delete-region (point) (progn (forward-line 1) (point))))
366 (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
368 (set-buffer nnfolder-current-buffer)
369 (insert-buffer-substring buf)
370 (and last (buffer-modified-p) (save-buffer))
372 (nnmail-save-active nnfolder-group-alist nnfolder-active-file))
375 (defun nnfolder-request-replace-article (article group buffer)
376 (nnfolder-possibly-change-group group)
378 (set-buffer nnfolder-current-buffer)
380 (if (not (search-forward (nnfolder-article-string article) nil t))
382 (nnfolder-delete-mail t t)
383 (insert-buffer-substring buffer)
384 (and (buffer-modified-p) (save-buffer))
388 ;;; Internal functions.
390 (defun nnfolder-delete-mail (&optional force leave-delim)
391 ;; Beginning of the article.
396 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
397 (if leave-delim (progn (forward-line 1) (point))
398 (match-beginning 0)))
401 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
403 (if (and (not (bobp)) leave-delim)
404 (progn (forward-line -2) (point))
405 (match-beginning 0)))
407 (delete-region (point-min) (point-max)))))
409 (defun nnfolder-possibly-change-group (group)
410 (or (file-exists-p nnfolder-directory)
411 (make-directory (directory-file-name nnfolder-directory)))
412 (if (not nnfolder-group-alist)
414 (nnfolder-request-list)
415 (setq nnfolder-group-alist (nnmail-get-active))))
416 (or (assoc group nnfolder-group-alist)
417 (not (file-exists-p (concat nnfolder-directory group)))
419 (setq nnfolder-group-alist
420 (cons (list group (cons 1 0)) nnfolder-group-alist))
421 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
423 (if (and (equal group nnfolder-current-group)
424 (buffer-name nnfolder-current-buffer))
426 (setq nnfolder-current-group group)
428 ;; If we have to change groups, see if we don't already have the mbox
429 ;; in memory. If we do, verify the modtime and destroy the mbox if
430 ;; needed so we can rescan it.
431 (if (setq inf (assoc group nnfolder-buffer-alist))
432 (setq nnfolder-current-buffer (nth 1 inf)))
434 ;; If the buffer is not live, make sure it isn't in the alist. If it
435 ;; is live, verify that nobody else has touched the file since last
437 (if (or (not (buffer-name nnfolder-current-buffer))
438 (not (and (bufferp nnfolder-current-buffer)
439 (verify-visited-file-modtime nnfolder-current-buffer))))
441 (if (and (buffer-name nnfolder-current-buffer)
442 (bufferp nnfolder-current-buffer))
443 (kill-buffer nnfolder-current-buffer))
444 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
450 (setq file (concat nnfolder-directory group))
451 (if (not (file-exists-p file))
452 (write-region 1 1 file t 'nomesg))
453 (set-buffer (nnfolder-read-folder file))
454 (setq nnfolder-buffer-alist (cons (list group (current-buffer))
455 nnfolder-buffer-alist))))))
456 (setq nnfolder-current-group group))
458 (defun nnfolder-save-mail (&optional group)
459 "Called narrowed to an article."
460 (let* ((nnmail-split-methods
461 (if group (list (list group "")) nnmail-split-methods))
463 (nreverse (nnmail-article-group 'nnfolder-active-number)))
465 (setq save-list group-art-list)
466 (nnmail-insert-lines)
467 (nnmail-insert-xref group-art-list)
469 ;; Insert the mail into each of the destination groups.
470 (while group-art-list
471 (setq group-art (car group-art-list)
472 group-art-list (cdr group-art-list))
474 ;; Kill the previous newsgroup markers.
475 (goto-char (point-min))
476 (search-forward "\n\n" nil t)
478 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
479 (delete-region (point) (progn (forward-line 1) (point))))
481 ;; Insert the new newsgroup marker.
482 (nnfolder-possibly-change-group (car group-art))
483 (nnfolder-insert-newsgroup-line group-art)
484 (let ((beg (point-min))
486 (obuf (current-buffer)))
487 (set-buffer nnfolder-current-buffer)
488 (goto-char (point-max))
489 (insert-buffer-substring obuf beg end)
492 ;; Did we save it anywhere?
495 (defun nnfolder-insert-newsgroup-line (group-art)
497 (goto-char (point-min))
498 (if (search-forward "\n\n" nil t)
501 (insert (format (concat nnfolder-article-marker "%d %s\n")
502 (cdr group-art) (current-time-string)))))))
504 (defun nnfolder-active-number (group)
505 (if (not nnfolder-group-alist)
507 (nnfolder-request-list)
508 (setq nnfolder-group-alist (nnmail-get-active))))
509 (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
510 (setcdr active (1+ (cdr active)))
514 ;; This method has a problem if you've accidentally let the active list get
515 ;; out of sync with the files. This could happen, say, if you've
516 ;; accidentally gotten new mail with something other than (ding) (but why
517 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
518 ;; processing the file, ready to add new X-Gnus article number markers, and
519 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
522 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
523 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
524 ;; rest of the message looking for any more, possibly higher IDs. We'll
525 ;; assume the maximum that we find is the highest active. Note that this
526 ;; shouldn't cost us much extra time at all, but will be a lot less
527 ;; vulnerable to glitches between the mbox and the active file.
529 (defun nnfolder-read-folder (file)
531 (if (not nnfolder-group-alist)
533 (nnfolder-request-list)
534 (setq nnfolder-group-alist (nnmail-get-active))))
535 ;; We should be paranoid here and make sure the group is in the alist,
536 ;; and add it if it isn't.
537 ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
538 (set-buffer (setq nnfolder-current-buffer (find-file-noselect file)))
539 (buffer-disable-undo (current-buffer))
540 (let ((delim (concat "^" rmail-unix-mail-delimiter))
541 (marker (concat "\n" nnfolder-article-marker))
543 (active (car (cdr (assoc nnfolder-current-group
544 nnfolder-group-alist))))
545 activenumber activemin start end)
546 (goto-char (point-min))
548 ;; Anytime the active number is 1 or 0, it is supect. In that case,
549 ;; search the file manually to find the active number. Or, of course,
550 ;; if we're being paranoid. (This would also be the place to build
551 ;; other lists from the header markers, such as expunge lists, etc., if
552 ;; we ever desired to abandon the active file entirely for mboxes.)
553 (setq activenumber (cdr active))
554 (if (or nnfolder-ignore-active-file
557 (setq activemin (max (1- (lsh 1 23))
560 (while (and (search-forward marker nil t)
561 (re-search-forward number nil t))
562 (let ((newnum (string-to-number (buffer-substring
565 (setq activenumber (max activenumber newnum))
566 (setq activemin (min activemin newnum))))
567 (setcar active (min activemin activenumber))
568 (setcdr active activenumber)
569 (goto-char (point-min))))
571 ;; Keep track of the active number on our own, and insert it back into
572 ;; the active list when we're done. Also, prime the pump to cut down on
573 ;; the number of searches we do.
574 (setq end (point-marker))
575 (set-marker end (or (and (re-search-forward delim nil t)
578 (while (not (= end (point-max)))
579 (setq start (marker-position end))
582 (set-marker end (or (and (re-search-forward delim nil t)
586 (if (not (search-forward marker end t))
588 (narrow-to-region start end)
589 (nnmail-insert-lines)
590 (setq activenumber (1+ activenumber))
591 (nnfolder-insert-newsgroup-line (cons nil activenumber))
594 ;; Make absolutely sure that the active list reflects reality!
595 (setcdr active activenumber)
596 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
599 (defun nnfolder-get-new-mail ()
601 (if (and nnmail-spool-file
602 nnfolder-get-new-mail
603 (file-exists-p nnmail-spool-file)
604 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
606 (and gnus-verbose-backends
607 (message "nnfolder: Reading incoming mail..."))
609 (nnmail-move-inbox nnmail-spool-file
610 (concat nnfolder-directory "Incoming")))
611 (nnmail-split-incoming incoming 'nnfolder-save-mail)
612 (run-hooks 'nnmail-read-incoming-hook)
613 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
614 (and gnus-verbose-backends
615 (message "nnfolder: Reading incoming mail...done"))))
616 (let ((bufs nnfolder-buffer-alist))
619 (if (not (buffer-name (nth 1 (car bufs))))
620 (setq nnfolder-buffer-alist
621 (delq (car bufs) nnfolder-buffer-alist))
622 (set-buffer (nth 1 (car bufs)))
623 (and (buffer-modified-p) (save-buffer)))
624 (setq bufs (cdr bufs)))))
625 ;; (if incoming (delete-file incoming))
630 ;;; nnfolder.el ends here