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 1.0"
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)
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)
104 '(nnfolder-active-timestamp nil)))
108 ;;; Interface functions
110 (defun nnfolder-retrieve-headers (sequence &optional newsgroup server)
112 (set-buffer nntp-server-buffer)
114 (let ((delim-string (concat "^" rmail-unix-mail-delimiter))
115 article art-string start stop)
116 (nnfolder-possibly-change-group newsgroup)
117 (set-buffer nnfolder-current-buffer)
118 (goto-char (point-min))
119 (if (stringp (car sequence))
122 (setq article (car sequence))
123 (setq art-string (nnfolder-article-string article))
124 (set-buffer nnfolder-current-buffer)
125 (if (or (search-forward art-string nil t)
126 ;; Don't search the whole file twice! Also, articles
127 ;; probably have some locality by number, so searching
128 ;; backwards will be faster. Especially if we're at the
129 ;; beginning of the buffer :-). -SLB
130 (search-backward art-string nil t))
132 (setq start (or (re-search-backward delim-string nil t)
134 (search-forward "\n\n" nil t)
135 (setq stop (1- (point)))
136 (set-buffer nntp-server-buffer)
137 (insert (format "221 %d Article retrieved.\n" article))
138 (insert-buffer-substring nnfolder-current-buffer start stop)
139 (goto-char (point-max))
141 (setq sequence (cdr sequence)))
143 ;; Fold continuation lines.
144 (set-buffer nntp-server-buffer)
145 (goto-char (point-min))
146 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
147 (replace-match " " t t))
150 (defun nnfolder-open-server (server &optional defs)
151 (nnheader-init-server-buffer)
152 (if (equal server nnfolder-current-server)
154 (if nnfolder-current-server
155 (setq nnfolder-server-alist
156 (cons (list nnfolder-current-server
157 (nnheader-save-variables nnfolder-server-variables))
158 nnfolder-server-alist)))
159 (let ((state (assoc server nnfolder-server-alist)))
162 (nnheader-restore-variables (nth 1 state))
163 (setq nnfolder-server-alist (delq state nnfolder-server-alist)))
164 (nnheader-set-init-variables nnfolder-server-variables defs)))
165 (setq nnfolder-current-server server)))
167 (defun nnfolder-close-server (&optional server)
170 (defun nnfolder-server-opened (&optional server)
171 (and (equal server nnfolder-current-server)
173 (buffer-name nntp-server-buffer)))
175 (defun nnfolder-request-close ()
176 (let ((alist nnfolder-buffer-alist))
178 (nnfolder-close-group (car (car alist)) nil t)
179 (setq alist (cdr alist))))
180 (setq nnfolder-buffer-alist nil
181 nnfolder-group-alist nil))
183 (defun nnfolder-status-message (&optional server)
184 nnfolder-status-string)
186 (defun nnfolder-request-article (article &optional newsgroup server buffer)
187 (nnfolder-possibly-change-group newsgroup)
189 (set-buffer nnfolder-current-buffer)
190 (goto-char (point-min))
191 (if (search-forward (nnfolder-article-string article) nil t)
193 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
196 (or (and (re-search-forward
197 (concat "^" rmail-unix-mail-delimiter) nil t)
199 (goto-char (point-max)))
201 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
202 (set-buffer nntp-server-buffer)
204 (insert-buffer-substring nnfolder-current-buffer start stop)
205 (goto-char (point-min))
206 (while (looking-at "From ")
208 (insert "X-From-Line: ")
210 (if (numberp article)
211 (cons nnfolder-current-group article)
212 (goto-char (point-min))
213 (search-forward (concat "\n" nnfolder-article-marker))
214 (cons nnfolder-current-group
217 (point) (progn (end-of-line) (point)))))))))))
219 (defun nnfolder-request-group (group &optional server dont-check)
221 (nnmail-activate 'nnfolder)
222 (nnfolder-possibly-change-group group)
223 (and (assoc group nnfolder-group-alist)
227 (let* ((active (assoc group nnfolder-group-alist))
229 (range (car (cdr active)))
230 (minactive (car range))
231 (maxactive (cdr range)))
232 ;; I've been getting stray 211 lines in my nnfolder active
233 ;; file. So, let's make sure that doesn't happen. -SLB
234 (set-buffer nntp-server-buffer)
238 (insert (format "211 %d %d %d %s\n"
239 (1+ (- maxactive minactive))
240 minactive maxactive group))
243 (defun nnfolder-request-scan (&optional group server)
247 (let ((bufs nnfolder-buffer-alist))
250 (if (not (buffer-name (nth 1 (car bufs))))
251 (setq nnfolder-buffer-alist
252 (delq (car bufs) nnfolder-buffer-alist))
253 (set-buffer (nth 1 (car bufs)))
254 (and (buffer-modified-p) (save-buffer)))
255 (setq bufs (cdr bufs))))))
259 ;; Don't close the buffer if we're not shutting down the server. This way,
260 ;; we can keep the buffer in the group buffer cache, and not have to grovel
261 ;; over the buffer again unless we add new mail to it or modify it in some
264 (defun nnfolder-close-group (group &optional server force)
265 ;; Make sure we _had_ the group open.
266 (if (or (assoc group nnfolder-buffer-alist)
267 (equal group nnfolder-current-group))
269 (nnfolder-possibly-change-group group)
271 (set-buffer nnfolder-current-buffer)
272 ;; If the buffer was modified, write the file out now.
273 (and (buffer-modified-p) (save-buffer))
275 nnfolder-always-close)
276 ;; If we're shutting the server down, we need to kill the
277 ;; buffer and remove it from the open buffer list. Or, of
278 ;; course, if we're trying to minimize our space impact.
280 (kill-buffer (current-buffer))
281 (setq nnfolder-buffer-alist (delq (assoc group
282 nnfolder-buffer-alist)
283 nnfolder-buffer-alist)))))))
284 (setq nnfolder-current-group nil
285 nnfolder-current-buffer nil)
288 (defun nnfolder-request-create-group (group &optional server)
289 (nnmail-activate 'nnfolder)
290 (or (assoc group nnfolder-group-alist)
292 (setq nnfolder-group-alist
293 (cons (list group (setq active (cons 1 0)))
294 nnfolder-group-alist))
295 (nnfolder-possibly-change-group group)
296 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
299 (defun nnfolder-request-list (&optional server)
301 (nnmail-find-file nnfolder-active-file)
302 (setq nnfolder-group-alist (nnmail-get-active))))
304 (defun nnfolder-request-newgroups (date &optional server)
305 (nnfolder-request-list server))
307 (defun nnfolder-request-list-newsgroups (&optional server)
309 (nnmail-find-file nnfolder-newsgroups-file)))
311 (defun nnfolder-request-post (&optional server)
312 (mail-send-and-exit nil))
314 (defalias 'nnfolder-request-post-buffer 'nnmail-request-post-buffer)
316 (defun nnfolder-request-expire-articles
317 (articles newsgroup &optional server force)
318 (nnfolder-possibly-change-group newsgroup)
319 (let* ((days (or (and nnmail-expiry-wait-function
320 (funcall nnmail-expiry-wait-function newsgroup))
324 (nnmail-activate 'nnfolder)
327 (set-buffer nnfolder-current-buffer)
328 (while (and articles is-old)
329 (goto-char (point-min))
330 (if (search-forward (nnfolder-article-string (car articles)) nil t)
333 (> (nnmail-days-between
334 (current-time-string)
336 (point) (progn (end-of-line) (point))))
339 (and gnus-verbose-backends
340 (message "Deleting article %d..."
341 (car articles) newsgroup))
342 (nnfolder-delete-mail))
343 (setq rest (cons (car articles) rest))))
344 (setq articles (cdr articles)))
345 (and (buffer-modified-p) (save-buffer))
346 ;; Find the lowest active article in this group.
347 (let* ((active (car (cdr (assoc newsgroup nnfolder-group-alist))))
348 (marker (concat "\n" nnfolder-article-marker))
350 (activemin (cdr active)))
351 (goto-char (point-min))
352 (while (and (search-forward marker nil t)
353 (re-search-forward number nil t))
354 (setq activemin (min activemin
355 (string-to-number (buffer-substring
358 (setcar active activemin))
359 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
360 (nconc rest articles))))
362 (defun nnfolder-request-move-article
363 (article group server accept-form &optional last)
364 (nnfolder-possibly-change-group group)
365 (let ((buf (get-buffer-create " *nnfolder move*"))
368 (nnfolder-request-article article group server)
371 (buffer-disable-undo (current-buffer))
373 (insert-buffer-substring nntp-server-buffer)
374 (goto-char (point-min))
375 (while (re-search-forward
376 (concat "^" nnfolder-article-marker)
377 (save-excursion (search-forward "\n\n" nil t) (point)) t)
378 (delete-region (progn (beginning-of-line) (point))
379 (progn (forward-line 1) (point))))
380 (setq result (eval accept-form))
384 (nnfolder-possibly-change-group group)
385 (set-buffer nnfolder-current-buffer)
386 (goto-char (point-min))
387 (if (search-forward (nnfolder-article-string article) nil t)
388 (nnfolder-delete-mail))
394 (defun nnfolder-request-accept-article (group &optional last)
395 (and (stringp group) (nnfolder-possibly-change-group group))
396 (let ((buf (current-buffer))
398 (goto-char (point-min))
399 (cond ((looking-at "X-From-Line: ")
400 (replace-match "From "))
401 ((not (looking-at "From "))
402 (insert "From nobody " (current-time-string) "\n")))
404 (nnfolder-request-list)
407 (goto-char (point-min))
408 (search-forward "\n\n" nil t)
410 (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
411 (delete-region (point) (progn (forward-line 1) (point))))
412 (setq result (car (nnfolder-save-mail (and (stringp group) group)))))
414 (set-buffer nnfolder-current-buffer)
415 (and last (buffer-modified-p) (save-buffer))))
416 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
419 (defun nnfolder-request-replace-article (article group buffer)
420 (nnfolder-possibly-change-group group)
422 (set-buffer nnfolder-current-buffer)
423 (goto-char (point-min))
424 (if (not (search-forward (nnfolder-article-string article) nil t))
426 (nnfolder-delete-mail t t)
427 (insert-buffer-substring buffer)
428 (and (buffer-modified-p) (save-buffer))
432 ;;; Internal functions.
434 (defun nnfolder-article-string (article)
435 (if (numberp article)
436 (concat "\n" nnfolder-article-marker (int-to-string article) " ")
437 (concat "\nMessage-ID: " article)))
439 (defun nnfolder-delete-mail (&optional force leave-delim)
440 ;; Beginning of the article.
445 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
446 (if leave-delim (progn (forward-line 1) (point))
447 (match-beginning 0)))
450 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
452 (if (and (not (bobp)) leave-delim)
453 (progn (forward-line -2) (point))
454 (match-beginning 0)))
456 (delete-region (point-min) (point-max)))))
458 (defun nnfolder-possibly-change-group (group)
459 (or (file-exists-p nnfolder-directory)
460 (make-directory (directory-file-name nnfolder-directory)))
461 (nnfolder-possibly-activate-groups nil)
462 (or (assoc group nnfolder-group-alist)
463 (not (file-exists-p (concat (file-name-as-directory nnfolder-directory)
466 (setq nnfolder-group-alist
467 (cons (list group (cons 1 0)) nnfolder-group-alist))
468 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)))
470 (if (and (equal group nnfolder-current-group)
471 nnfolder-current-buffer
472 (buffer-name nnfolder-current-buffer))
474 (setq nnfolder-current-group group)
476 ;; If we have to change groups, see if we don't already have the mbox
477 ;; in memory. If we do, verify the modtime and destroy the mbox if
478 ;; needed so we can rescan it.
479 (if (setq inf (assoc group nnfolder-buffer-alist))
480 (setq nnfolder-current-buffer (nth 1 inf)))
482 ;; If the buffer is not live, make sure it isn't in the alist. If it
483 ;; is live, verify that nobody else has touched the file since last
485 (if (or (not (and nnfolder-current-buffer
486 (buffer-name nnfolder-current-buffer)))
487 (not (and (bufferp nnfolder-current-buffer)
488 (verify-visited-file-modtime
489 nnfolder-current-buffer))))
491 (if (and nnfolder-current-buffer
492 (buffer-name nnfolder-current-buffer)
493 (bufferp nnfolder-current-buffer))
494 (kill-buffer nnfolder-current-buffer))
495 (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist))
501 (setq file (concat (file-name-as-directory nnfolder-directory)
503 (if (file-directory-p (file-truename file))
505 (if (not (file-exists-p file))
506 (write-region 1 1 file t 'nomesg))
507 (setq nnfolder-current-buffer
508 (set-buffer (nnfolder-read-folder file)))
509 (setq nnfolder-buffer-alist (cons (list group (current-buffer))
510 nnfolder-buffer-alist)))))))
511 (setq nnfolder-current-group group))
513 (defun nnfolder-save-mail (&optional group)
514 "Called narrowed to an article."
515 (let* ((nnmail-split-methods
516 (if group (list (list group "")) nnmail-split-methods))
518 (nreverse (nnmail-article-group 'nnfolder-active-number)))
520 (setq save-list group-art-list)
521 (nnmail-insert-lines)
522 (nnmail-insert-xref group-art-list)
523 (run-hooks 'nnfolder-prepare-save-mail-hook)
525 ;; Insert the mail into each of the destination groups.
526 (while group-art-list
527 (setq group-art (car group-art-list)
528 group-art-list (cdr group-art-list))
530 ;; Kill the previous newsgroup markers.
531 (goto-char (point-min))
532 (search-forward "\n\n" nil t)
534 (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
535 (delete-region (1+ (point)) (progn (forward-line 2) (point))))
537 ;; Insert the new newsgroup marker.
538 (nnfolder-possibly-change-group (car group-art))
539 (nnfolder-insert-newsgroup-line group-art)
540 (let ((beg (point-min))
542 (obuf (current-buffer)))
543 (set-buffer nnfolder-current-buffer)
544 (goto-char (point-max))
545 (insert-buffer-substring obuf beg end)
548 ;; Did we save it anywhere?
551 (defun nnfolder-insert-newsgroup-line (group-art)
553 (goto-char (point-min))
554 (if (search-forward "\n\n" nil t)
557 (insert (format (concat nnfolder-article-marker "%d %s\n")
558 (cdr group-art) (current-time-string)))))))
560 (defun nnfolder-possibly-activate-groups (&optional group)
562 ;; If we're looking for the activation of a specific group, find out
563 ;; its real name and switch to it.
564 (if group (nnfolder-possibly-change-group group))
565 ;; If the group alist isn't active, activate it now.
566 (nnmail-activate 'nnfolder)))
568 (defun nnfolder-active-number (group)
570 ;; Find the next article number in GROUP.
572 (let ((active (car (cdr (assoc group nnfolder-group-alist)))))
574 (setcdr active (1+ (cdr active)))
575 ;; This group is new, so we create a new entry for it.
576 ;; This might be a bit naughty... creating groups on the drop of
577 ;; a hat, but I don't know...
578 (setq nnfolder-group-alist
579 (cons (list group (setq active (cons 1 1)))
580 nnfolder-group-alist)))
582 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
583 (nnfolder-possibly-activate-groups group)
587 ;; This method has a problem if you've accidentally let the active list get
588 ;; out of sync with the files. This could happen, say, if you've
589 ;; accidentally gotten new mail with something other than Gnus (but why
590 ;; would _that_ ever happen? :-). In that case, we will be in the middle of
591 ;; processing the file, ready to add new X-Gnus article number markers, and
592 ;; we'll run accross a message with no ID yet - the active list _may_not_ be
595 ;; To handle this, I'm modifying this routine to maintain the maximum ID seen
596 ;; so far, and when we hit a message with no ID, we will _manually_ scan the
597 ;; rest of the message looking for any more, possibly higher IDs. We'll
598 ;; assume the maximum that we find is the highest active. Note that this
599 ;; shouldn't cost us much extra time at all, but will be a lot less
600 ;; vulnerable to glitches between the mbox and the active file.
602 (defun nnfolder-read-folder (file)
604 (nnfolder-possibly-activate-groups nil)
605 ;; We should be paranoid here and make sure the group is in the alist,
606 ;; and add it if it isn't.
607 ;;(if (not (assoc nnfoler-current-group nnfolder-group-alist)
608 (set-buffer (setq nnfolder-current-buffer
609 (nnheader-find-file-noselect file nil 'raw)))
610 (buffer-disable-undo (current-buffer))
611 (let ((delim (concat "^" rmail-unix-mail-delimiter))
612 (marker (concat "\n" nnfolder-article-marker))
614 (active (car (cdr (assoc nnfolder-current-group
615 nnfolder-group-alist))))
616 activenumber activemin start end)
617 (goto-char (point-min))
619 ;; Anytime the active number is 1 or 0, it is supect. In that case,
620 ;; search the file manually to find the active number. Or, of course,
621 ;; if we're being paranoid. (This would also be the place to build
622 ;; other lists from the header markers, such as expunge lists, etc., if
623 ;; we ever desired to abandon the active file entirely for mboxes.)
624 (setq activenumber (cdr active))
625 (if (or nnfolder-ignore-active-file
628 (setq activemin (max (1- (lsh 1 23))
631 (while (and (search-forward marker nil t)
632 (re-search-forward number nil t))
633 (let ((newnum (string-to-number (buffer-substring
636 (setq activenumber (max activenumber newnum))
637 (setq activemin (min activemin newnum))))
638 (setcar active (min activemin activenumber))
639 (setcdr active (max activenumber (cdr active)))
640 (goto-char (point-min))))
642 ;; Keep track of the active number on our own, and insert it back into
643 ;; the active list when we're done. Also, prime the pump to cut down on
644 ;; the number of searches we do.
645 (setq end (point-marker))
646 (set-marker end (or (and (re-search-forward delim nil t)
649 (while (not (= end (point-max)))
650 (setq start (marker-position end))
652 ;; There may be more than one "From " line, so we skip past
654 (while (looking-at delim)
656 (set-marker end (or (and (re-search-forward delim nil t)
660 (if (not (search-forward marker end t))
662 (narrow-to-region start end)
663 (nnmail-insert-lines)
664 (nnfolder-insert-newsgroup-line
665 (cons nil (nnfolder-active-number nnfolder-current-group)))
668 ;; Make absolutely sure that the active list reflects reality!
669 (nnmail-save-active nnfolder-group-alist nnfolder-active-file)
674 ;;; nnfolder.el ends here