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
34 (eval-when-compile (require 'cl))
38 (defvoo nnmbox-mbox-file (expand-file-name "~/mbox")
39 "The name of the mail box file in the user's home directory.
41 This variable is a virtual server slot. See the Gnus manual for details.")
43 (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
44 "The name of the active file for the mail box.
46 This variable is a virtual server slot. See the Gnus manual for details.")
48 (defvoo nnmbox-get-new-mail t
49 "If non-nil, nnmbox will check the incoming mail file and split the mail.
51 This variable is a virtual server slot. See the Gnus manual for details.")
53 (defvoo nnmbox-prepare-save-mail-hook nil
54 "Hook run narrowed to an article before saving.
56 This variable is a virtual server slot. See the Gnus manual for details.")
60 (defconst nnmbox-version "nnmbox 1.0"
63 (defvoo nnmbox-current-group nil
64 "Current nnmbox news group directory.")
66 (defconst nnmbox-mbox-buffer nil)
68 (defvoo nnmbox-status-string "")
70 (defvoo nnmbox-group-alist nil)
71 (defvoo nnmbox-active-timestamp nil)
73 (defvoo nnmbox-file-coding-system mm-binary-coding-system)
74 (defvoo nnmbox-file-coding-system-for-write nil)
75 (defvoo nnmbox-active-file-coding-system mm-binary-coding-system)
76 (defvoo nnmbox-active-file-coding-system-for-write nil)
78 (defvar nnmbox-group-building-active-articles nil)
79 (defvar nnmbox-group-active-articles nil)
82 ;;; Interface functions
84 (nnoo-define-basics nnmbox)
86 (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
88 (set-buffer nntp-server-buffer)
90 (let ((number (length sequence))
93 (nnmbox-possibly-change-newsgroup newsgroup server)
95 (setq article (car sequence))
96 (set-buffer nnmbox-mbox-buffer)
97 (when (nnmbox-find-article article)
101 (concat "^" message-unix-mail-delimiter) nil t)
103 (search-forward "\n\n" nil t)
104 (setq stop (1- (point)))
105 (set-buffer nntp-server-buffer)
106 (insert (format "221 %d Article retrieved.\n" article))
107 (insert-buffer-substring nnmbox-mbox-buffer start stop)
108 (goto-char (point-max))
110 (setq sequence (cdr sequence))
111 (setq count (1+ count))
112 (and (numberp nnmail-large-newsgroup)
113 (> number nnmail-large-newsgroup)
115 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
116 (/ (* count 100) number))))
118 (and (numberp nnmail-large-newsgroup)
119 (> number nnmail-large-newsgroup)
120 (nnheader-message 5 "nnmbox: Receiving headers...done"))
122 (set-buffer nntp-server-buffer)
123 (nnheader-fold-continuation-lines)
126 (deffoo nnmbox-open-server (server &optional defs)
127 (nnoo-change-server 'nnmbox server defs)
130 ((not (file-exists-p nnmbox-mbox-file))
131 (nnmbox-close-server)
132 (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
133 ((file-directory-p nnmbox-mbox-file)
134 (nnmbox-close-server)
135 (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
137 (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
141 (deffoo nnmbox-close-server (&optional server)
142 (when (and nnmbox-mbox-buffer
143 (buffer-name nnmbox-mbox-buffer))
144 (kill-buffer nnmbox-mbox-buffer))
145 (nnoo-close-server 'nnmbox server)
148 (deffoo nnmbox-server-opened (&optional server)
149 (and (nnoo-current-server-p 'nnmbox server)
151 (buffer-name nnmbox-mbox-buffer)
153 (buffer-name nntp-server-buffer)))
155 (deffoo nnmbox-request-article (article &optional newsgroup server buffer)
156 (nnmbox-possibly-change-newsgroup newsgroup server)
158 (set-buffer nnmbox-mbox-buffer)
159 (when (nnmbox-find-article article)
161 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
164 (or (and (re-search-forward
165 (concat "^" message-unix-mail-delimiter) nil t)
167 (goto-char (point-max)))
169 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
170 (set-buffer nntp-server-buffer)
172 (insert-buffer-substring nnmbox-mbox-buffer start stop)
173 (goto-char (point-min))
174 (while (looking-at "From ")
176 (insert "X-From-Line: ")
178 (if (numberp article)
179 (cons nnmbox-current-group article)
180 (nnmbox-article-group-number nil)))))))
182 (deffoo nnmbox-request-group (group &optional server dont-check)
183 (nnmbox-possibly-change-newsgroup nil server)
184 (let ((active (cadr (assoc group nnmbox-group-alist))))
187 (null (nnmbox-possibly-change-newsgroup group server)))
188 (nnheader-report 'nnmbox "No such group: %s" group))
190 (nnheader-report 'nnmbox "Selected group %s" group)
191 (nnheader-insert ""))
193 (nnheader-report 'nnmbox "Selected group %s" group)
194 (nnheader-insert "211 %d %d %d %s\n"
195 (1+ (- (cdr active) (car active)))
196 (car active) (cdr active) group)))))
198 (defun nnmbox-save-buffer ()
199 (let ((coding-system-for-write
200 (or nnmbox-file-coding-system-for-write
201 nnmbox-file-coding-system)))
204 (defun nnmbox-save-active (group-alist active-file)
205 (let ((nnmail-active-file-coding-system
206 (or nnmbox-active-file-coding-system-for-write
207 nnmbox-active-file-coding-system)))
208 (nnmail-save-active group-alist active-file)))
210 (deffoo nnmbox-request-scan (&optional group server)
211 (nnmbox-possibly-change-newsgroup group server)
217 (set-buffer nnmbox-mbox-buffer)
218 (nnmbox-save-buffer)))
219 (file-name-directory nnmbox-mbox-file)
223 (let ((in-buf (current-buffer)))
224 (set-buffer nnmbox-mbox-buffer)
225 (goto-char (point-max))
226 (insert-buffer-substring in-buf)))
227 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
229 (deffoo nnmbox-close-group (group &optional server)
232 (deffoo nnmbox-request-create-group (group &optional server args)
233 (nnmail-activate 'nnmbox)
234 (unless (assoc group nnmbox-group-alist)
235 (push (list group (cons 1 0))
237 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
240 (deffoo nnmbox-request-list (&optional server)
242 (let ((nnmail-file-coding-system
243 nnmbox-active-file-coding-system))
244 (nnmail-find-file nnmbox-active-file))
245 (setq nnmbox-group-alist (nnmail-get-active))
248 (deffoo nnmbox-request-newgroups (date &optional server)
249 (nnmbox-request-list server))
251 (deffoo nnmbox-request-list-newsgroups (&optional server)
252 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
254 (deffoo nnmbox-request-expire-articles
255 (articles newsgroup &optional server force)
256 (nnmbox-possibly-change-newsgroup newsgroup server)
259 (nnmail-activate 'nnmbox)
262 (set-buffer nnmbox-mbox-buffer)
263 (while (and articles is-old)
264 (when (nnmbox-find-article (car articles))
266 (nnmail-expired-article-p
269 (point) (progn (end-of-line) (point))) force))
271 (unless (eq nnmail-expiry-target 'delete)
273 (nnmbox-request-article (car articles)
276 (let ((nnml-current-directory nil))
277 (nnmail-expiry-target-group
278 nnmail-expiry-target newsgroup)))
279 (nnmbox-possibly-change-newsgroup newsgroup server))
280 (nnheader-message 5 "Deleting article %d in %s..."
281 (car articles) newsgroup)
282 (nnmbox-delete-mail))
283 (push (car articles) rest)))
284 (setq articles (cdr articles)))
286 ;; Find the lowest active article in this group.
287 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
288 (while (and (not (nnmbox-find-article (car active)))
289 (<= (car active) (cdr active)))
290 (setcar active (1+ (car active)))))
291 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
292 (nconc rest articles))))
294 (deffoo nnmbox-request-move-article
295 (article group server accept-form &optional last)
296 (let ((buf (get-buffer-create " *nnmbox move*"))
299 (nnmbox-request-article article group server)
303 (insert-buffer-substring nntp-server-buffer)
304 (goto-char (point-min))
305 (while (re-search-forward
307 (save-excursion (search-forward "\n\n" nil t) (point)) t)
308 (delete-region (progn (beginning-of-line) (point))
309 (progn (forward-line 1) (point))))
310 (setq result (eval accept-form))
314 (nnmbox-possibly-change-newsgroup group server)
315 (set-buffer nnmbox-mbox-buffer)
316 (when (nnmbox-find-article article)
317 (nnmbox-delete-mail))
318 (and last (nnmbox-save-buffer))))
321 (deffoo nnmbox-request-accept-article (group &optional server last)
322 (nnmbox-possibly-change-newsgroup group server)
323 (nnmail-check-syntax)
324 (let ((buf (current-buffer))
326 (goto-char (point-min))
327 ;; The From line may have been quoted by movemail.
328 (when (looking-at (concat ">" message-unix-mail-delimiter))
330 (if (looking-at "X-From-Line: ")
331 (replace-match "From ")
332 (insert "From nobody " (current-time-string) "\n"))
334 (nnmail-activate 'nnmbox)
337 (goto-char (point-min))
338 (search-forward "\n\n" nil t)
340 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
341 (delete-region (point) (progn (forward-line 1) (point))))
342 (when nnmail-cache-accepted-message-ids
343 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
344 (setq result (if (stringp group)
345 (list (cons group (nnmbox-active-number group)))
346 (nnmail-article-group 'nnmbox-active-number)))
347 (if (and (null result)
348 (yes-or-no-p "Moved to `junk' group; delete article? "))
350 (setq result (car (nnmbox-save-mail result)))))
352 (set-buffer nnmbox-mbox-buffer)
353 (goto-char (point-max))
354 (insert-buffer-substring buf)
356 (when nnmail-cache-accepted-message-ids
357 (nnmail-cache-close))
358 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
359 (nnmbox-save-buffer))))
362 (deffoo nnmbox-request-replace-article (article group buffer)
363 (nnmbox-possibly-change-newsgroup group)
365 (set-buffer nnmbox-mbox-buffer)
366 (if (not (nnmbox-find-article article))
368 (nnmbox-delete-mail t t)
369 (insert-buffer-substring buffer)
373 (deffoo nnmbox-request-delete-group (group &optional force server)
374 (nnmbox-possibly-change-newsgroup group server)
375 ;; Delete all articles in GROUP.
377 () ; Don't delete the articles.
379 (set-buffer nnmbox-mbox-buffer)
380 (goto-char (point-min))
381 ;; Delete all articles in this group.
382 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
384 (while (search-forward ident nil t)
386 (nnmbox-delete-mail))
388 (nnmbox-save-buffer)))))
389 ;; Remove the group from all structures.
390 (setq nnmbox-group-alist
391 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
392 nnmbox-current-group nil)
393 ;; Save the active file.
394 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
397 (deffoo nnmbox-request-rename-group (group new-name &optional server)
398 (nnmbox-possibly-change-newsgroup group server)
400 (set-buffer nnmbox-mbox-buffer)
401 (goto-char (point-min))
402 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
403 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
405 (while (search-forward ident nil t)
406 (replace-match new-ident t t)
409 (nnmbox-save-buffer))))
410 (let ((entry (assoc group nnmbox-group-active-articles)))
412 (setcar entry new-name)))
413 (let ((entry (assoc group nnmbox-group-alist)))
415 (setcar entry new-name))
416 (setq nnmbox-current-group nil)
417 ;; Save the new group alist.
418 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
422 ;;; Internal functions.
424 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
425 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
427 (defun nnmbox-delete-mail (&optional force leave-delim)
428 ;; Delete the current X-Gnus-Newsgroup line.
429 ;; First delete record of active article, unless the article is being
430 ;; replaced, indicated by FORCE being non-nil.
432 (nnmbox-record-deleted-article (nnmbox-article-group-number t)))
435 (progn (beginning-of-line) (point))
436 (progn (forward-line 1) (point))))
437 ;; Beginning of the article.
442 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
443 (if leave-delim (progn (forward-line 1) (point))
444 (match-beginning 0)))
447 (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
449 (if (and (not (bobp)) leave-delim)
450 (progn (forward-line -2) (point))
451 (match-beginning 0)))
453 (goto-char (point-min))
454 ;; Only delete the article if no other group owns it as well.
455 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
456 (delete-region (point-min) (point-max))))))
458 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
460 (not (nnmbox-server-opened server)))
461 (nnmbox-open-server server))
462 (when (or (not nnmbox-mbox-buffer)
463 (not (buffer-name nnmbox-mbox-buffer)))
465 (when (not nnmbox-group-alist)
466 (nnmail-activate 'nnmbox))
468 (when (assoc newsgroup nnmbox-group-alist)
469 (setq nnmbox-current-group newsgroup))
472 (defun nnmbox-article-string (article)
473 (if (numberp article)
474 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
475 (int-to-string article) " ")
476 (concat "\nMessage-ID: " article)))
478 (defun nnmbox-article-group-number (this-line)
482 (goto-char (point-min)))
483 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
485 (cons (buffer-substring (match-beginning 1) (match-end 1))
487 (buffer-substring (match-beginning 2) (match-end 2)))))))
489 (defun nnmbox-in-header-p (pos)
490 "Return non-nil if POS is in the header of an article."
493 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
494 (search-forward "\n\n" nil t)
497 (defun nnmbox-find-article (article)
498 "Leaves point on the relevant X-Gnus-Newsgroup line if found."
499 ;; Check that article is in the active range first, to avoid an
500 ;; expensive exhaustive search if it isn't.
501 (if (and (numberp article)
502 (not (nnmbox-is-article-active-p article)))
504 (let ((art-string (nnmbox-article-string article))
506 ;; There is the possibility that the X-Gnus-Newsgroup line appears
507 ;; in the body of an article (for instance, if an article has been
508 ;; forwarded from someone using Gnus as their mailer), so check
509 ;; that the line is actually part of the article header.
510 (or (and (search-forward art-string nil t)
511 (nnmbox-in-header-p (point)))
513 (goto-char (point-min))
514 (while (and (not found)
515 (search-forward art-string nil t))
516 (setq found (nnmbox-in-header-p (point))))
519 (defun nnmbox-record-active-article (group-art)
520 (let* ((group (car group-art))
521 (article (cdr group-art))
523 (or (assoc group nnmbox-group-active-articles)
526 nnmbox-group-active-articles)
527 (car nnmbox-group-active-articles)))))
528 ;; add article to index, either by building complete list
529 ;; in reverse order, or as a list of ranges.
530 (if (not nnmbox-group-building-active-articles)
531 (setcdr entry (gnus-add-to-range (cdr entry) (list article)))
532 (when (memq article (cdr entry))
533 (switch-to-buffer nnmbox-mbox-buffer)
534 (error "Article %s:%d already exists!" group article))
535 (when (and (cadr entry) (< article (cadr entry)))
536 (switch-to-buffer nnmbox-mbox-buffer)
537 (error "Article %s:%d out of order" group article))
538 (setcdr entry (cons article (cdr entry))))))
540 (defun nnmbox-record-deleted-article (group-art)
541 (let* ((group (car group-art))
542 (article (cdr group-art))
544 (or (assoc group nnmbox-group-active-articles)
547 nnmbox-group-active-articles)
548 (car nnmbox-group-active-articles)))))
549 ;; remove article from index
550 (setcdr entry (gnus-remove-from-range (cdr entry) (list article)))))
552 (defun nnmbox-is-article-active-p (article)
553 (gnus-member-of-range
555 (cdr (assoc nnmbox-current-group
556 nnmbox-group-active-articles))))
558 (defun nnmbox-save-mail (group-art)
559 "Called narrowed to an article."
560 (let ((delim (concat "^" message-unix-mail-delimiter)))
561 (goto-char (point-min))
562 ;; This might come from somewhere else.
563 (unless (looking-at delim)
564 (insert "From nobody " (current-time-string) "\n")
565 (goto-char (point-min)))
566 ;; Quote all "From " lines in the article.
568 (while (re-search-forward delim nil t)
571 (nnmail-insert-lines)
572 (nnmail-insert-xref group-art)
573 (nnmbox-insert-newsgroup-line group-art)
574 (let ((alist group-art))
576 (nnmbox-record-active-article (car alist))
577 (setq alist (cdr alist))))
578 (run-hooks 'nnmail-prepare-save-mail-hook)
579 (run-hooks 'nnmbox-prepare-save-mail-hook)
582 (defun nnmbox-insert-newsgroup-line (group-art)
584 (goto-char (point-min))
585 (when (search-forward "\n\n" nil t)
588 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
589 (caar group-art) (cdar group-art)
590 (current-time-string)))
591 (setq group-art (cdr group-art))))
594 (defun nnmbox-active-number (group)
595 ;; Find the next article number in GROUP.
596 (let ((active (cadr (assoc group nnmbox-group-alist))))
598 (setcdr active (1+ (cdr active)))
599 ;; This group is new, so we create a new entry for it.
600 ;; This might be a bit naughty... creating groups on the drop of
601 ;; a hat, but I don't know...
602 (push (list group (setq active (cons 1 1)))
606 (defun nnmbox-create-mbox ()
607 (when (not (file-exists-p nnmbox-mbox-file))
608 (let ((nnmail-file-coding-system
609 (or nnmbox-file-coding-system-for-write
610 nnmbox-file-coding-system)))
611 (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg))))
613 (defun nnmbox-read-mbox ()
614 (nnmail-activate 'nnmbox)
616 (if (and nnmbox-mbox-buffer
617 (buffer-name nnmbox-mbox-buffer)
619 (set-buffer nnmbox-mbox-buffer)
620 (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
623 (let ((delim (concat "^" message-unix-mail-delimiter))
624 (alist nnmbox-group-alist)
625 (nnmbox-group-building-active-articles t)
626 start end end-header number)
627 (set-buffer (setq nnmbox-mbox-buffer
628 (let ((nnheader-file-coding-system
629 nnmbox-file-coding-system))
630 (nnheader-find-file-noselect
631 nnmbox-mbox-file t t))))
632 (mm-enable-multibyte)
633 (buffer-disable-undo)
635 ;; Go through the group alist and compare against the mbox file.
637 (goto-char (point-max))
638 (when (and (re-search-backward
639 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
644 (match-beginning 1) (match-end 1))))
646 (setcdr (cadar alist) number))
647 (setq alist (cdr alist)))
649 ;; Examine all articles for our private X-Gnus-Newsgroup
650 ;; headers. This is done primarily as a consistency check, but
651 ;; it is convenient for building an index of the articles
652 ;; present, to avoid costly searches for missing articles
653 ;; (eg. when expiring articles).
654 (goto-char (point-min))
655 (setq nnmbox-group-active-articles nil)
656 (while (re-search-forward delim nil t)
657 (setq start (match-beginning 0))
659 (search-forward "\n\n" nil t)
660 (setq end-header (point))
662 (re-search-forward delim nil t)
665 (if (search-forward "\nX-Gnus-Newsgroup: " end-header t)
666 ;; Build a list of articles in each group, remembering
667 ;; that each article may be in more than one group.
669 (nnmbox-record-active-article (nnmbox-article-group-number t))
670 (while (search-forward "\nX-Gnus-Newsgroup: " end-header t)
671 (nnmbox-record-active-article (nnmbox-article-group-number t))))
672 ;; The article is either new, or for some other reason
673 ;; hasn't got our private headers, so add them now. The
674 ;; only situation I've encountered when the X-Gnus-Newsgroup
675 ;; header is missing is if the article contains a forwarded
676 ;; message which does contain that header line (earlier
677 ;; versions of Gnus didn't restrict their search to the
678 ;; headers). In this case, there is an Xref line which
679 ;; provides the relevant information to construct the
680 ;; missing header(s).
683 (narrow-to-region start end)
684 (if (re-search-forward "\nXref: [^ ]+" end-header t)
685 ;; generate headers from Xref:
687 (while (re-search-forward " \\([^:]+\\):\\([0-9]+\\)" end-header t)
688 (push (cons (match-string 1)
689 (string-to-int (match-string 2))) alist))
690 (nnmbox-insert-newsgroup-line alist))
691 ;; this is really a new article
693 (nnmail-article-group 'nnmbox-active-number))))))
695 ;; put article lists in order
696 (setq alist nnmbox-group-active-articles)
698 (setcdr (car alist) (gnus-compress-sequence (nreverse (cdar alist))))
699 (setq alist (cdr alist)))))))
703 ;;; nnmbox.el ends here