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 (nnmbox-possibly-change-newsgroup newsgroup server))
289 (nnheader-message 5 "Deleting article %d in %s..."
290 (car articles) newsgroup)
291 (nnmbox-delete-mail))
292 (push (car articles) rest)))
293 (setq articles (cdr articles)))
295 ;; Find the lowest active article in this group.
296 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
297 (while (and (not (nnmbox-find-article (car active)))
298 (<= (car active) (cdr active)))
299 (setcar active (1+ (car active)))))
300 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
301 (nconc rest articles))))
303 (deffoo nnmbox-request-move-article
304 (article group server accept-form &optional last)
305 (let ((buf (get-buffer-create " *nnmbox move*"))
308 (nnmbox-request-article article group server)
312 (insert-buffer-substring nntp-server-buffer)
313 (goto-char (point-min))
314 (while (re-search-forward
316 (save-excursion (search-forward "\n\n" nil t) (point)) t)
317 (delete-region (progn (beginning-of-line) (point))
318 (progn (forward-line 1) (point))))
319 (setq result (eval accept-form))
323 (nnmbox-possibly-change-newsgroup group server)
324 (set-buffer nnmbox-mbox-buffer)
325 (when (nnmbox-find-article article)
326 (nnmbox-delete-mail))
327 (and last (nnmbox-save-buffer))))
330 (deffoo nnmbox-request-accept-article (group &optional server last)
331 (nnmbox-possibly-change-newsgroup group server)
332 (nnmail-check-syntax)
333 (let ((buf (current-buffer))
335 (goto-char (point-min))
336 ;; The From line may have been quoted by movemail.
337 (when (looking-at (concat ">" message-unix-mail-delimiter))
339 (if (looking-at "X-From-Line: ")
340 (replace-match "From ")
341 (insert "From nobody " (current-time-string) "\n"))
343 (nnmail-activate 'nnmbox)
346 (goto-char (point-min))
347 (search-forward "\n\n" nil t)
349 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
350 (delete-region (point) (progn (forward-line 1) (point))))
351 (when nnmail-cache-accepted-message-ids
352 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
353 (setq result (if (stringp group)
354 (list (cons group (nnmbox-active-number group)))
355 (nnmail-article-group 'nnmbox-active-number)))
356 (if (and (null result)
357 (yes-or-no-p "Moved to `junk' group; delete article? "))
359 (setq result (car (nnmbox-save-mail result)))))
361 (set-buffer nnmbox-mbox-buffer)
362 (goto-char (point-max))
363 (insert-buffer-substring buf)
365 (when nnmail-cache-accepted-message-ids
366 (nnmail-cache-close))
367 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
368 (nnmbox-save-buffer))))
371 (deffoo nnmbox-request-replace-article (article group buffer)
372 (nnmbox-possibly-change-newsgroup group)
374 (set-buffer nnmbox-mbox-buffer)
375 (if (not (nnmbox-find-article article))
377 (nnmbox-delete-mail t t)
378 (insert-buffer-substring buffer)
382 (deffoo nnmbox-request-delete-group (group &optional force server)
383 (nnmbox-possibly-change-newsgroup group server)
384 ;; Delete all articles in GROUP.
386 () ; Don't delete the articles.
388 (set-buffer nnmbox-mbox-buffer)
389 (goto-char (point-min))
390 ;; Delete all articles in this group.
391 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
393 (while (search-forward ident nil t)
395 (nnmbox-delete-mail))
397 (nnmbox-save-buffer)))))
398 ;; Remove the group from all structures.
399 (setq nnmbox-group-alist
400 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
401 nnmbox-current-group nil)
402 ;; Save the active file.
403 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
406 (deffoo nnmbox-request-rename-group (group new-name &optional server)
407 (nnmbox-possibly-change-newsgroup group server)
409 (set-buffer nnmbox-mbox-buffer)
410 (goto-char (point-min))
411 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
412 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
414 (while (search-forward ident nil t)
415 (replace-match new-ident t t)
418 (nnmbox-save-buffer))))
419 (let ((entry (assoc group nnmbox-group-active-articles)))
421 (setcar entry new-name)))
422 (let ((entry (assoc group nnmbox-group-alist)))
424 (setcar entry new-name))
425 (setq nnmbox-current-group nil)
426 ;; Save the new group alist.
427 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
431 ;;; Internal functions.
433 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
434 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
436 (defun nnmbox-delete-mail (&optional force leave-delim)
437 ;; Delete the current X-Gnus-Newsgroup line.
438 ;; First delete record of active article, unless the article is being
439 ;; replaced, indicated by FORCE being non-nil.
441 (nnmbox-record-deleted-article (nnmbox-article-group-number t)))
444 (progn (beginning-of-line) (point))
445 (progn (forward-line 1) (point))))
446 ;; Beginning of the article.
451 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
452 (if leave-delim (progn (forward-line 1) (point))
453 (match-beginning 0)))
456 (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
458 (if (and (not (bobp)) leave-delim)
459 (progn (forward-line -2) (point))
460 (match-beginning 0)))
462 (goto-char (point-min))
463 ;; Only delete the article if no other group owns it as well.
464 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
465 (delete-region (point-min) (point-max))))))
467 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
469 (not (nnmbox-server-opened server)))
470 (nnmbox-open-server server))
471 (when (or (not nnmbox-mbox-buffer)
472 (not (buffer-name nnmbox-mbox-buffer)))
474 (when (not nnmbox-group-alist)
475 (nnmail-activate 'nnmbox))
477 (when (assoc newsgroup nnmbox-group-alist)
478 (setq nnmbox-current-group newsgroup))
481 (defun nnmbox-article-string (article)
482 (if (numberp article)
483 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
484 (int-to-string article) " ")
485 (concat "\nMessage-ID: " article)))
487 (defun nnmbox-article-group-number (this-line)
491 (goto-char (point-min)))
492 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
494 (cons (buffer-substring (match-beginning 1) (match-end 1))
496 (buffer-substring (match-beginning 2) (match-end 2)))))))
498 (defun nnmbox-in-header-p (pos)
499 "Return non-nil if POS is in the header of an article."
502 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
503 (search-forward "\n\n" nil t)
506 (defun nnmbox-find-article (article)
507 "Leaves point on the relevant X-Gnus-Newsgroup line if found."
508 ;; Check that article is in the active range first, to avoid an
509 ;; expensive exhaustive search if it isn't.
510 (if (and (numberp article)
511 (not (nnmbox-is-article-active-p article)))
513 (let ((art-string (nnmbox-article-string article))
515 ;; There is the possibility that the X-Gnus-Newsgroup line appears
516 ;; in the body of an article (for instance, if an article has been
517 ;; forwarded from someone using Gnus as their mailer), so check
518 ;; that the line is actually part of the article header.
519 (or (and (search-forward art-string nil t)
520 (nnmbox-in-header-p (point)))
522 (goto-char (point-min))
523 (while (and (not found)
524 (search-forward art-string nil t))
525 (setq found (nnmbox-in-header-p (point))))
528 (defun nnmbox-record-active-article (group-art)
529 (let* ((group (car group-art))
530 (article (cdr group-art))
532 (or (assoc group nnmbox-group-active-articles)
535 nnmbox-group-active-articles)
536 (car nnmbox-group-active-articles)))))
537 ;; add article to index, either by building complete list
538 ;; in reverse order, or as a list of ranges.
539 (if (not nnmbox-group-building-active-articles)
540 (setcdr entry (gnus-add-to-range (cdr entry) (list article)))
541 (when (memq article (cdr entry))
542 (switch-to-buffer nnmbox-mbox-buffer)
543 (error "Article %s:%d already exists!" group article))
544 (when (and (cadr entry) (< article (cadr entry)))
545 (switch-to-buffer nnmbox-mbox-buffer)
546 (error "Article %s:%d out of order" group article))
547 (setcdr entry (cons article (cdr entry))))))
549 (defun nnmbox-record-deleted-article (group-art)
550 (let* ((group (car group-art))
551 (article (cdr group-art))
553 (or (assoc group nnmbox-group-active-articles)
556 nnmbox-group-active-articles)
557 (car nnmbox-group-active-articles)))))
558 ;; remove article from index
559 (setcdr entry (gnus-remove-from-range (cdr entry) (list article)))))
561 (defun nnmbox-is-article-active-p (article)
562 (gnus-member-of-range
564 (cdr (assoc nnmbox-current-group
565 nnmbox-group-active-articles))))
567 (defun nnmbox-save-mail (group-art)
568 "Called narrowed to an article."
569 (let ((delim (concat "^" message-unix-mail-delimiter)))
570 (goto-char (point-min))
571 ;; This might come from somewhere else.
572 (unless (looking-at delim)
573 (insert "From nobody " (current-time-string) "\n")
574 (goto-char (point-min)))
575 ;; Quote all "From " lines in the article.
577 (while (re-search-forward delim nil t)
580 (nnmail-insert-lines)
581 (nnmail-insert-xref group-art)
582 (nnmbox-insert-newsgroup-line group-art)
583 (let ((alist group-art))
585 (nnmbox-record-active-article (car alist))
586 (setq alist (cdr alist))))
587 (run-hooks 'nnmail-prepare-save-mail-hook)
588 (run-hooks 'nnmbox-prepare-save-mail-hook)
591 (defun nnmbox-insert-newsgroup-line (group-art)
593 (goto-char (point-min))
594 (when (search-forward "\n\n" nil t)
597 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
598 (caar group-art) (cdar group-art)
599 (current-time-string)))
600 (setq group-art (cdr group-art))))
603 (defun nnmbox-active-number (group)
604 ;; Find the next article number in GROUP.
605 (let ((active (cadr (assoc group nnmbox-group-alist))))
607 (setcdr active (1+ (cdr active)))
608 ;; This group is new, so we create a new entry for it.
609 ;; This might be a bit naughty... creating groups on the drop of
610 ;; a hat, but I don't know...
611 (push (list group (setq active (cons 1 1)))
615 (defun nnmbox-create-mbox ()
616 (when (not (file-exists-p nnmbox-mbox-file))
617 (let ((nnmail-file-coding-system
618 (or nnmbox-file-coding-system-for-write
619 nnmbox-file-coding-system))
620 (dir (file-name-directory nnmbox-mbox-file)))
621 (and dir (gnus-make-directory dir))
622 (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg))))
624 (defun nnmbox-read-mbox ()
625 (nnmail-activate 'nnmbox)
627 (if (and nnmbox-mbox-buffer
628 (buffer-name nnmbox-mbox-buffer)
630 (set-buffer nnmbox-mbox-buffer)
631 (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
634 (let ((delim (concat "^" message-unix-mail-delimiter))
635 (alist nnmbox-group-alist)
636 (nnmbox-group-building-active-articles t)
637 start end end-header number)
638 (set-buffer (setq nnmbox-mbox-buffer
639 (let ((nnheader-file-coding-system
640 nnmbox-file-coding-system))
641 (nnheader-find-file-noselect
642 nnmbox-mbox-file t t))))
643 (buffer-disable-undo)
645 ;; Go through the group alist and compare against the mbox file.
647 (goto-char (point-max))
648 (when (and (re-search-backward
649 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
654 (match-beginning 1) (match-end 1))))
656 (setcdr (cadar alist) number))
657 (setq alist (cdr alist)))
659 ;; Examine all articles for our private X-Gnus-Newsgroup
660 ;; headers. This is done primarily as a consistency check, but
661 ;; it is convenient for building an index of the articles
662 ;; present, to avoid costly searches for missing articles
663 ;; (eg. when expiring articles).
664 (goto-char (point-min))
665 (setq nnmbox-group-active-articles nil)
666 (while (re-search-forward delim nil t)
667 (setq start (match-beginning 0))
669 (search-forward "\n\n" nil t)
670 (setq end-header (point))
672 (re-search-forward delim nil t)
675 (if (search-forward "\nX-Gnus-Newsgroup: " end-header t)
676 ;; Build a list of articles in each group, remembering
677 ;; that each article may be in more than one group.
679 (nnmbox-record-active-article (nnmbox-article-group-number t))
680 (while (search-forward "\nX-Gnus-Newsgroup: " end-header t)
681 (nnmbox-record-active-article (nnmbox-article-group-number t))))
682 ;; The article is either new, or for some other reason
683 ;; hasn't got our private headers, so add them now. The
684 ;; only situation I've encountered when the X-Gnus-Newsgroup
685 ;; header is missing is if the article contains a forwarded
686 ;; message which does contain that header line (earlier
687 ;; versions of Gnus didn't restrict their search to the
688 ;; headers). In this case, there is an Xref line which
689 ;; provides the relevant information to construct the
690 ;; missing header(s).
693 (narrow-to-region start end)
694 (if (re-search-forward "\nXref: [^ ]+" end-header t)
695 ;; generate headers from Xref:
697 (while (re-search-forward " \\([^:]+\\):\\([0-9]+\\)" end-header t)
698 (push (cons (match-string 1)
699 (string-to-int (match-string 2))) alist))
700 (nnmbox-insert-newsgroup-line alist))
701 ;; this is really a new article
703 (nnmail-article-group 'nnmbox-active-number))))))
705 ;; put article lists in order
706 (setq alist nnmbox-group-active-articles)
708 (setcdr (car alist) (gnus-compress-sequence (nreverse (cdar alist))))
709 (setq alist (cdr alist)))))))
713 ;;; nnmbox.el ends here