1 ;;; nnmbox.el --- mail mbox access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: news, mail
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
24 ;; For an overview of what the interface functions do, please see the
29 (eval-when-compile (require 'cl))
30 (eval-when-compile (require 'static))
40 (defvoo nnmbox-mbox-file (expand-file-name "~/mbox")
41 "The name of the mail box file in the user's home directory.
43 This variable is a virtual server slot. See the Gnus manual for details.")
45 (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
46 "The name of the active file for the mail box.
48 This variable is a virtual server slot. See the Gnus manual for details.")
50 (defvoo nnmbox-get-new-mail t
51 "If non-nil, nnmbox will check the incoming mail file and split the mail.
53 This variable is a virtual server slot. See the Gnus manual for details.")
55 (defvoo nnmbox-prepare-save-mail-hook nil
56 "Hook run narrowed to an article before saving.
58 This variable is a virtual server slot. See the Gnus manual for details.")
62 (defconst nnmbox-version "nnmbox 1.0"
65 (defvoo nnmbox-current-group nil
66 "Current nnmbox news group directory.")
68 (defconst nnmbox-mbox-buffer nil)
70 (defvoo nnmbox-status-string "")
72 (defvoo nnmbox-group-alist nil)
73 (defvoo nnmbox-active-timestamp nil)
75 (defvoo nnmbox-file-coding-system 'binary)
76 (defvoo nnmbox-file-coding-system-for-write nil)
77 (defvoo nnmbox-active-file-coding-system 'binary)
78 (defvoo nnmbox-active-file-coding-system-for-write nil)
80 (defvar nnmbox-group-building-active-articles nil)
81 (defvar nnmbox-group-active-articles nil)
84 ;;; Interface functions
86 (nnoo-define-basics nnmbox)
88 (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
90 (set-buffer nntp-server-buffer)
92 (let ((number (length sequence))
95 (nnmbox-possibly-change-newsgroup newsgroup server)
97 (setq article (car sequence))
98 (set-buffer nnmbox-mbox-buffer)
99 (when (nnmbox-find-article article)
103 (concat "^" message-unix-mail-delimiter) nil t)
105 (search-forward "\n\n" nil t)
106 (setq stop (1- (point)))
107 (set-buffer nntp-server-buffer)
108 (insert (format "221 %d Article retrieved.\n" article))
109 (insert-buffer-substring nnmbox-mbox-buffer start stop)
110 (goto-char (point-max))
112 (setq sequence (cdr sequence))
113 (setq count (1+ count))
114 (and (numberp nnmail-large-newsgroup)
115 (> number nnmail-large-newsgroup)
117 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
118 (/ (* count 100) number))))
120 (and (numberp nnmail-large-newsgroup)
121 (> number nnmail-large-newsgroup)
122 (nnheader-message 5 "nnmbox: Receiving headers...done"))
124 (set-buffer nntp-server-buffer)
125 (nnheader-fold-continuation-lines)
128 (deffoo nnmbox-open-server (server &optional defs)
129 (nnoo-change-server 'nnmbox server defs)
132 ((not (file-exists-p nnmbox-mbox-file))
133 (nnmbox-close-server)
134 (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
135 ((file-directory-p nnmbox-mbox-file)
136 (nnmbox-close-server)
137 (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
139 (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
143 (deffoo nnmbox-close-server (&optional server)
144 (when (and nnmbox-mbox-buffer
145 (buffer-name nnmbox-mbox-buffer))
146 (kill-buffer nnmbox-mbox-buffer))
147 (nnoo-close-server 'nnmbox server)
150 (deffoo nnmbox-server-opened (&optional server)
151 (and (nnoo-current-server-p 'nnmbox server)
153 (buffer-name nnmbox-mbox-buffer)
155 (buffer-name nntp-server-buffer)))
157 (deffoo nnmbox-request-article (article &optional newsgroup server buffer)
158 (nnmbox-possibly-change-newsgroup newsgroup server)
160 (set-buffer nnmbox-mbox-buffer)
161 (when (nnmbox-find-article article)
163 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
166 (or (and (re-search-forward
167 (concat "^" message-unix-mail-delimiter) nil t)
169 (goto-char (point-max)))
171 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
172 (set-buffer nntp-server-buffer)
174 (insert-buffer-substring nnmbox-mbox-buffer start stop)
175 (goto-char (point-min))
176 (while (looking-at "From ")
178 (insert "X-From-Line: ")
180 (if (numberp article)
181 (cons nnmbox-current-group article)
182 (nnmbox-article-group-number nil)))))))
184 (deffoo nnmbox-request-group (group &optional server dont-check)
185 (nnmbox-possibly-change-newsgroup nil server)
186 (let ((active (cadr (assoc group nnmbox-group-alist))))
189 (null (nnmbox-possibly-change-newsgroup group server)))
190 (nnheader-report 'nnmbox "No such group: %s" group))
192 (nnheader-report 'nnmbox "Selected group %s" group)
193 (nnheader-insert ""))
195 (nnheader-report 'nnmbox "Selected group %s" group)
196 (nnheader-insert "211 %d %d %d %s\n"
197 (1+ (- (cdr active) (car active)))
198 (car active) (cdr active) group)))))
200 (static-if (boundp 'MULE)
201 (defun nnmbox-save-buffer ()
202 (let ((output-coding-system
203 (or nnmbox-file-coding-system-for-write
204 nnmbox-file-coding-system)))
206 (defun nnmbox-save-buffer ()
207 (let ((coding-system-for-write
208 (or nnmbox-file-coding-system-for-write
209 nnmbox-file-coding-system)))
213 (defun nnmbox-save-active (group-alist active-file)
214 (let ((nnmail-active-file-coding-system
215 (or nnmbox-active-file-coding-system-for-write
216 nnmbox-active-file-coding-system)))
217 (nnmail-save-active group-alist active-file)))
219 (deffoo nnmbox-request-scan (&optional group server)
220 (nnmbox-possibly-change-newsgroup group server)
226 (set-buffer nnmbox-mbox-buffer)
227 (nnmbox-save-buffer)))
228 (file-name-directory nnmbox-mbox-file)
232 (let ((in-buf (current-buffer)))
233 (set-buffer nnmbox-mbox-buffer)
234 (goto-char (point-max))
235 (insert-buffer-substring in-buf)))
236 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
238 (deffoo nnmbox-close-group (group &optional server)
241 (deffoo nnmbox-request-create-group (group &optional server args)
242 (nnmail-activate 'nnmbox)
243 (unless (assoc group nnmbox-group-alist)
244 (push (list group (cons 1 0))
246 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
249 (deffoo nnmbox-request-list (&optional server)
251 (let ((nnmail-file-coding-system
252 nnmbox-active-file-coding-system))
253 (nnmail-find-file nnmbox-active-file))
254 (setq nnmbox-group-alist (nnmail-get-active))
257 (deffoo nnmbox-request-newgroups (date &optional server)
258 (nnmbox-request-list server))
260 (deffoo nnmbox-request-list-newsgroups (&optional server)
261 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
263 (deffoo nnmbox-request-expire-articles
264 (articles newsgroup &optional server force)
265 (nnmbox-possibly-change-newsgroup newsgroup server)
268 (nnmail-activate 'nnmbox)
271 (set-buffer nnmbox-mbox-buffer)
272 (while (and articles is-old)
273 (when (nnmbox-find-article (car articles))
275 (nnmail-expired-article-p
278 (point) (progn (end-of-line) (point))) force))
280 (unless (eq nnmail-expiry-target 'delete)
282 (nnmbox-request-article (car articles)
285 (let ((nnml-current-directory nil))
286 (nnmail-expiry-target-group
287 nnmail-expiry-target newsgroup))))
288 (nnheader-message 5 "Deleting article %d in %s..."
289 (car articles) newsgroup)
290 (nnmbox-delete-mail))
291 (push (car articles) rest)))
292 (setq articles (cdr articles)))
294 ;; Find the lowest active article in this group.
295 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
296 (while (and (not (nnmbox-find-article (car active)))
297 (<= (car active) (cdr active)))
298 (setcar active (1+ (car active)))))
299 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
300 (nconc rest articles))))
302 (deffoo nnmbox-request-move-article
303 (article group server accept-form &optional last)
304 (let ((buf (get-buffer-create " *nnmbox move*"))
307 (nnmbox-request-article article group server)
311 (insert-buffer-substring nntp-server-buffer)
312 (goto-char (point-min))
313 (while (re-search-forward
315 (save-excursion (search-forward "\n\n" nil t) (point)) t)
316 (delete-region (progn (beginning-of-line) (point))
317 (progn (forward-line 1) (point))))
318 (setq result (eval accept-form))
322 (nnmbox-possibly-change-newsgroup group server)
323 (set-buffer nnmbox-mbox-buffer)
324 (when (nnmbox-find-article article)
325 (nnmbox-delete-mail))
326 (and last (nnmbox-save-buffer))))
329 (deffoo nnmbox-request-accept-article (group &optional server last)
330 (nnmbox-possibly-change-newsgroup group server)
331 (nnmail-check-syntax)
332 (let ((buf (current-buffer))
334 (goto-char (point-min))
335 ;; The From line may have been quoted by movemail.
336 (when (looking-at (concat ">" message-unix-mail-delimiter))
338 (if (looking-at "X-From-Line: ")
339 (replace-match "From ")
340 (insert "From nobody " (current-time-string) "\n"))
342 (nnmail-activate 'nnmbox)
345 (goto-char (point-min))
346 (search-forward "\n\n" nil t)
348 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
349 (delete-region (point) (progn (forward-line 1) (point))))
350 (when nnmail-cache-accepted-message-ids
351 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
352 (setq result (if (stringp group)
353 (list (cons group (nnmbox-active-number group)))
354 (nnmail-article-group 'nnmbox-active-number)))
355 (if (and (null result)
356 (yes-or-no-p "Moved to `junk' group; delete article? "))
358 (setq result (car (nnmbox-save-mail result)))))
360 (set-buffer nnmbox-mbox-buffer)
361 (goto-char (point-max))
362 (insert-buffer-substring buf)
364 (when nnmail-cache-accepted-message-ids
365 (nnmail-cache-close))
366 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
367 (nnmbox-save-buffer))))
370 (deffoo nnmbox-request-replace-article (article group buffer)
371 (nnmbox-possibly-change-newsgroup group)
373 (set-buffer nnmbox-mbox-buffer)
374 (if (not (nnmbox-find-article article))
376 (nnmbox-delete-mail t t)
377 (insert-buffer-substring buffer)
381 (deffoo nnmbox-request-delete-group (group &optional force server)
382 (nnmbox-possibly-change-newsgroup group server)
383 ;; Delete all articles in GROUP.
385 () ; Don't delete the articles.
387 (set-buffer nnmbox-mbox-buffer)
388 (goto-char (point-min))
389 ;; Delete all articles in this group.
390 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
392 (while (search-forward ident nil t)
394 (nnmbox-delete-mail))
396 (nnmbox-save-buffer)))))
397 ;; Remove the group from all structures.
398 (setq nnmbox-group-alist
399 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
400 nnmbox-current-group nil)
401 ;; Save the active file.
402 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
405 (deffoo nnmbox-request-rename-group (group new-name &optional server)
406 (nnmbox-possibly-change-newsgroup group server)
408 (set-buffer nnmbox-mbox-buffer)
409 (goto-char (point-min))
410 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
411 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
413 (while (search-forward ident nil t)
414 (replace-match new-ident t t)
417 (nnmbox-save-buffer))))
418 (let ((entry (assoc group nnmbox-group-active-articles)))
420 (setcar entry new-name)))
421 (let ((entry (assoc group nnmbox-group-alist)))
423 (setcar entry new-name))
424 (setq nnmbox-current-group nil)
425 ;; Save the new group alist.
426 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
430 ;;; Internal functions.
432 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
433 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
435 (defun nnmbox-delete-mail (&optional force leave-delim)
436 ;; Delete the current X-Gnus-Newsgroup line.
437 ;; First delete record of active article, unless the article is being
438 ;; replaced, indicated by FORCE being non-nil.
440 (nnmbox-record-deleted-article (nnmbox-article-group-number t)))
443 (progn (beginning-of-line) (point))
444 (progn (forward-line 1) (point))))
445 ;; Beginning of the article.
450 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
451 (if leave-delim (progn (forward-line 1) (point))
452 (match-beginning 0)))
455 (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
457 (if (and (not (bobp)) leave-delim)
458 (progn (forward-line -2) (point))
459 (match-beginning 0)))
461 (goto-char (point-min))
462 ;; Only delete the article if no other group owns it as well.
463 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
464 (delete-region (point-min) (point-max))))))
466 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
468 (not (nnmbox-server-opened server)))
469 (nnmbox-open-server server))
470 (when (or (not nnmbox-mbox-buffer)
471 (not (buffer-name nnmbox-mbox-buffer)))
473 (when (not nnmbox-group-alist)
474 (nnmail-activate 'nnmbox))
476 (when (assoc newsgroup nnmbox-group-alist)
477 (setq nnmbox-current-group newsgroup))
480 (defun nnmbox-article-string (article)
481 (if (numberp article)
482 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
483 (int-to-string article) " ")
484 (concat "\nMessage-ID: " article)))
486 (defun nnmbox-article-group-number (this-line)
490 (goto-char (point-min)))
491 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
493 (cons (buffer-substring (match-beginning 1) (match-end 1))
495 (buffer-substring (match-beginning 2) (match-end 2)))))))
497 (defun nnmbox-in-header-p (pos)
498 "Return non-nil if POS is in the header of an article."
501 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
502 (search-forward "\n\n" nil t)
505 (defun nnmbox-find-article (article)
506 "Leaves point on the relevant X-Gnus-Newsgroup line if found."
507 ;; Check that article is in the active range first, to avoid an
508 ;; expensive exhaustive search if it isn't.
509 (if (and (numberp article)
510 (not (nnmbox-is-article-active-p article)))
512 (let ((art-string (nnmbox-article-string article))
514 ;; There is the possibility that the X-Gnus-Newsgroup line appears
515 ;; in the body of an article (for instance, if an article has been
516 ;; forwarded from someone using Gnus as their mailer), so check
517 ;; that the line is actually part of the article header.
518 (or (and (search-forward art-string nil t)
519 (nnmbox-in-header-p (point)))
521 (goto-char (point-min))
523 (setq found (and (search-forward art-string nil t)
524 (nnmbox-in-header-p (point)))))
527 (defun nnmbox-record-active-article (group-art)
528 (let* ((group (car group-art))
529 (article (cdr group-art))
531 (or (assoc group nnmbox-group-active-articles)
534 nnmbox-group-active-articles)
535 (car nnmbox-group-active-articles)))))
536 ;; add article to index, either by building complete list
537 ;; in reverse order, or as a list of ranges.
538 (if (not nnmbox-group-building-active-articles)
539 (setcdr entry (gnus-add-to-range (cdr entry) (list article)))
540 (when (memq article (cdr entry))
541 (switch-to-buffer nnmbox-mbox-buffer)
542 (error "Article %s:%d already exists!" group article))
543 (when (and (cadr entry) (< article (cadr entry)))
544 (switch-to-buffer nnmbox-mbox-buffer)
545 (error "Article %s:%d out of order" group article))
546 (setcdr entry (cons article (cdr entry))))))
548 (defun nnmbox-record-deleted-article (group-art)
549 (let* ((group (car group-art))
550 (article (cdr group-art))
552 (or (assoc group nnmbox-group-active-articles)
555 nnmbox-group-active-articles)
556 (car nnmbox-group-active-articles)))))
557 ;; remove article from index
558 (setcdr entry (gnus-remove-from-range (cdr entry) (list article)))))
560 (defun nnmbox-is-article-active-p (article)
561 (gnus-member-of-range
563 (cdr (assoc nnmbox-current-group
564 nnmbox-group-active-articles))))
566 (defun nnmbox-save-mail (group-art)
567 "Called narrowed to an article."
568 (let ((delim (concat "^" message-unix-mail-delimiter)))
569 (goto-char (point-min))
570 ;; This might come from somewhere else.
571 (unless (looking-at delim)
572 (insert "From nobody " (current-time-string) "\n")
573 (goto-char (point-min)))
574 ;; Quote all "From " lines in the article.
576 (while (re-search-forward delim nil t)
579 (nnmail-insert-lines)
580 (nnmail-insert-xref group-art)
581 (nnmbox-insert-newsgroup-line group-art)
582 (let ((alist group-art))
584 (nnmbox-record-active-article (car alist))
585 (setq alist (cdr alist))))
586 (run-hooks 'nnmail-prepare-save-mail-hook)
587 (run-hooks 'nnmbox-prepare-save-mail-hook)
590 (defun nnmbox-insert-newsgroup-line (group-art)
592 (goto-char (point-min))
593 (when (search-forward "\n\n" nil t)
596 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
597 (caar group-art) (cdar group-art)
598 (current-time-string)))
599 (setq group-art (cdr group-art))))
602 (defun nnmbox-active-number (group)
603 ;; Find the next article number in GROUP.
604 (let ((active (cadr (assoc group nnmbox-group-alist))))
606 (setcdr active (1+ (cdr active)))
607 ;; This group is new, so we create a new entry for it.
608 ;; This might be a bit naughty... creating groups on the drop of
609 ;; a hat, but I don't know...
610 (push (list group (setq active (cons 1 1)))
614 (defun nnmbox-create-mbox ()
615 (when (not (file-exists-p nnmbox-mbox-file))
616 (let ((nnmail-file-coding-system
617 (or nnmbox-file-coding-system-for-write
618 nnmbox-file-coding-system)))
619 (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg))))
621 (defun nnmbox-read-mbox ()
622 (nnmail-activate 'nnmbox)
624 (if (and nnmbox-mbox-buffer
625 (buffer-name nnmbox-mbox-buffer)
627 (set-buffer nnmbox-mbox-buffer)
628 (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
631 (let ((delim (concat "^" message-unix-mail-delimiter))
632 (alist nnmbox-group-alist)
633 (nnmbox-group-building-active-articles t)
634 start end end-header number)
635 (set-buffer (setq nnmbox-mbox-buffer
636 (let ((nnheader-file-coding-system
637 nnmbox-file-coding-system))
638 (nnheader-find-file-noselect
639 nnmbox-mbox-file t t))))
640 (buffer-disable-undo)
642 ;; Go through the group alist and compare against the mbox file.
644 (goto-char (point-max))
645 (when (and (re-search-backward
646 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
651 (match-beginning 1) (match-end 1))))
653 (setcdr (cadar alist) number))
654 (setq alist (cdr alist)))
656 ;; Examine all articles for our private X-Gnus-Newsgroup
657 ;; headers. This is done primarily as a consistency check, but
658 ;; it is convenient for building an index of the articles
659 ;; present, to avoid costly searches for missing articles
660 ;; (eg. when expiring articles).
661 (goto-char (point-min))
662 (setq nnmbox-group-active-articles nil)
663 (while (re-search-forward delim nil t)
664 (setq start (match-beginning 0))
666 (search-forward "\n\n" nil t)
667 (setq end-header (point))
669 (re-search-forward delim nil t)
672 (if (search-forward "\nX-Gnus-Newsgroup: " end-header t)
673 ;; Build a list of articles in each group, remembering
674 ;; that each article may be in more than one group.
676 (nnmbox-record-active-article (nnmbox-article-group-number t))
677 (while (search-forward "\nX-Gnus-Newsgroup: " end-header t)
678 (nnmbox-record-active-article (nnmbox-article-group-number t))))
679 ;; The article is either new, or for some other reason
680 ;; hasn't got our private headers, so add them now. The
681 ;; only situation I've encountered when the X-Gnus-Newsgroup
682 ;; header is missing is if the article contains a forwarded
683 ;; message which does contain that header line (earlier
684 ;; versions of Gnus didn't restrict their search to the
685 ;; headers). In this case, there is an Xref line which
686 ;; provides the relevant information to construct the
687 ;; missing header(s).
690 (narrow-to-region start end)
691 (if (re-search-forward "\nXref: [^ ]+" end-header t)
692 ;; generate headers from Xref:
694 (while (re-search-forward " \\([^:]+\\):\\([0-9]+\\)" end-header t)
695 (push (cons (match-string 1)
696 (string-to-int (match-string 2))) alist))
697 (nnmbox-insert-newsgroup-line alist))
698 ;; this is really a new article
700 (nnmail-article-group 'nnmbox-active-number))))))
702 ;; put article lists in order
703 (setq alist nnmbox-group-active-articles)
705 (setcdr (car alist) (gnus-compress-sequence (nreverse (cdar alist))))
706 (setq alist (cdr alist)))))))
710 ;;; nnmbox.el ends here