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 (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
42 "The name of the active file for the mail box.")
44 (defvoo nnmbox-get-new-mail t
45 "If non-nil, nnmbox will check the incoming mail file and split the mail.")
47 (defvoo nnmbox-prepare-save-mail-hook nil
48 "Hook run narrowed to an article before saving.")
52 (defconst nnmbox-version "nnmbox 1.0"
55 (defvoo nnmbox-current-group nil
56 "Current nnmbox news group directory.")
58 (defconst nnmbox-mbox-buffer nil)
60 (defvoo nnmbox-status-string "")
62 (defvoo nnmbox-group-alist nil)
63 (defvoo nnmbox-active-timestamp nil)
65 (defvoo nnmbox-file-coding-system mm-binary-coding-system)
66 (defvoo nnmbox-file-coding-system-for-write nil)
67 (defvoo nnmbox-active-file-coding-system mm-binary-coding-system)
68 (defvoo nnmbox-active-file-coding-system-for-write nil)
70 (defvar nnmbox-group-building-active-articles nil)
71 (defvar nnmbox-group-active-articles nil)
74 ;;; Interface functions
76 (nnoo-define-basics nnmbox)
78 (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
80 (set-buffer nntp-server-buffer)
82 (let ((number (length sequence))
85 (nnmbox-possibly-change-newsgroup newsgroup server)
87 (setq article (car sequence))
88 (set-buffer nnmbox-mbox-buffer)
89 (when (nnmbox-find-article article)
93 (concat "^" message-unix-mail-delimiter) nil t)
95 (search-forward "\n\n" nil t)
96 (setq stop (1- (point)))
97 (set-buffer nntp-server-buffer)
98 (insert (format "221 %d Article retrieved.\n" article))
99 (insert-buffer-substring nnmbox-mbox-buffer start stop)
100 (goto-char (point-max))
102 (setq sequence (cdr sequence))
103 (setq count (1+ count))
104 (and (numberp nnmail-large-newsgroup)
105 (> number nnmail-large-newsgroup)
107 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
108 (/ (* count 100) number))))
110 (and (numberp nnmail-large-newsgroup)
111 (> number nnmail-large-newsgroup)
112 (nnheader-message 5 "nnmbox: Receiving headers...done"))
114 (set-buffer nntp-server-buffer)
115 (nnheader-fold-continuation-lines)
118 (deffoo nnmbox-open-server (server &optional defs)
119 (nnoo-change-server 'nnmbox server defs)
122 ((not (file-exists-p nnmbox-mbox-file))
123 (nnmbox-close-server)
124 (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
125 ((file-directory-p nnmbox-mbox-file)
126 (nnmbox-close-server)
127 (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
129 (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
133 (deffoo nnmbox-close-server (&optional server)
134 (when (and nnmbox-mbox-buffer
135 (buffer-name nnmbox-mbox-buffer))
136 (kill-buffer nnmbox-mbox-buffer))
137 (nnoo-close-server 'nnmbox server)
140 (deffoo nnmbox-server-opened (&optional server)
141 (and (nnoo-current-server-p 'nnmbox server)
143 (buffer-name nnmbox-mbox-buffer)
145 (buffer-name nntp-server-buffer)))
147 (deffoo nnmbox-request-article (article &optional newsgroup server buffer)
148 (nnmbox-possibly-change-newsgroup newsgroup server)
150 (set-buffer nnmbox-mbox-buffer)
151 (when (nnmbox-find-article article)
153 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
156 (or (and (re-search-forward
157 (concat "^" message-unix-mail-delimiter) nil t)
159 (goto-char (point-max)))
161 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
162 (set-buffer nntp-server-buffer)
164 (insert-buffer-substring nnmbox-mbox-buffer start stop)
165 (goto-char (point-min))
166 (while (looking-at "From ")
168 (insert "X-From-Line: ")
170 (if (numberp article)
171 (cons nnmbox-current-group article)
172 (nnmbox-article-group-number nil)))))))
174 (deffoo nnmbox-request-group (group &optional server dont-check)
175 (nnmbox-possibly-change-newsgroup nil server)
176 (let ((active (cadr (assoc group nnmbox-group-alist))))
179 (null (nnmbox-possibly-change-newsgroup group server)))
180 (nnheader-report 'nnmbox "No such group: %s" group))
182 (nnheader-report 'nnmbox "Selected group %s" group)
183 (nnheader-insert ""))
185 (nnheader-report 'nnmbox "Selected group %s" group)
186 (nnheader-insert "211 %d %d %d %s\n"
187 (1+ (- (cdr active) (car active)))
188 (car active) (cdr active) group)))))
190 (defun nnmbox-save-buffer ()
191 (let ((coding-system-for-write
192 (or nnmbox-file-coding-system-for-write
193 nnmbox-file-coding-system)))
196 (defun nnmbox-save-active (group-alist active-file)
197 (let ((nnmail-active-file-coding-system
198 (or nnmbox-active-file-coding-system-for-write
199 nnmbox-active-file-coding-system)))
200 (nnmail-save-active group-alist active-file)))
202 (deffoo nnmbox-request-scan (&optional group server)
203 (nnmbox-possibly-change-newsgroup group server)
209 (set-buffer nnmbox-mbox-buffer)
210 (nnmbox-save-buffer)))
211 (file-name-directory nnmbox-mbox-file)
215 (let ((in-buf (current-buffer)))
216 (set-buffer nnmbox-mbox-buffer)
217 (goto-char (point-max))
218 (insert-buffer-substring in-buf)))
219 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
221 (deffoo nnmbox-close-group (group &optional server)
224 (deffoo nnmbox-request-create-group (group &optional server args)
225 (nnmail-activate 'nnmbox)
226 (unless (assoc group nnmbox-group-alist)
227 (push (list group (cons 1 0))
229 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
232 (deffoo nnmbox-request-list (&optional server)
234 (let ((nnmail-file-coding-system
235 nnmbox-active-file-coding-system))
236 (nnmail-find-file nnmbox-active-file))
237 (setq nnmbox-group-alist (nnmail-get-active))
240 (deffoo nnmbox-request-newgroups (date &optional server)
241 (nnmbox-request-list server))
243 (deffoo nnmbox-request-list-newsgroups (&optional server)
244 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
246 (deffoo nnmbox-request-expire-articles
247 (articles newsgroup &optional server force)
248 (nnmbox-possibly-change-newsgroup newsgroup server)
251 (nnmail-activate 'nnmbox)
254 (set-buffer nnmbox-mbox-buffer)
255 (while (and articles is-old)
256 (when (nnmbox-find-article (car articles))
258 (nnmail-expired-article-p
261 (point) (progn (end-of-line) (point))) force))
263 (unless (eq nnmail-expiry-target 'delete)
265 (nnmbox-request-article (car articles)
268 (let ((nnml-current-directory nil))
269 (nnmail-expiry-target-group
270 nnmail-expiry-target newsgroup))))
271 (nnheader-message 5 "Deleting article %d in %s..."
272 (car articles) newsgroup)
273 (nnmbox-delete-mail))
274 (push (car articles) rest)))
275 (setq articles (cdr articles)))
277 ;; Find the lowest active article in this group.
278 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
279 (while (and (not (nnmbox-find-article (car active)))
280 (<= (car active) (cdr active)))
281 (setcar active (1+ (car active)))))
282 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
283 (nconc rest articles))))
285 (deffoo nnmbox-request-move-article
286 (article group server accept-form &optional last)
287 (let ((buf (get-buffer-create " *nnmbox move*"))
290 (nnmbox-request-article article group server)
294 (insert-buffer-substring nntp-server-buffer)
295 (goto-char (point-min))
296 (while (re-search-forward
298 (save-excursion (search-forward "\n\n" nil t) (point)) t)
299 (delete-region (progn (beginning-of-line) (point))
300 (progn (forward-line 1) (point))))
301 (setq result (eval accept-form))
305 (nnmbox-possibly-change-newsgroup group server)
306 (set-buffer nnmbox-mbox-buffer)
307 (when (nnmbox-find-article article)
308 (nnmbox-delete-mail))
309 (and last (nnmbox-save-buffer))))
312 (deffoo nnmbox-request-accept-article (group &optional server last)
313 (nnmbox-possibly-change-newsgroup group server)
314 (nnmail-check-syntax)
315 (let ((buf (current-buffer))
317 (goto-char (point-min))
318 ;; The From line may have been quoted by movemail.
319 (when (looking-at (concat ">" message-unix-mail-delimiter))
321 (if (looking-at "X-From-Line: ")
322 (replace-match "From ")
323 (insert "From nobody " (current-time-string) "\n"))
325 (nnmail-activate 'nnmbox)
328 (goto-char (point-min))
329 (search-forward "\n\n" nil t)
331 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
332 (delete-region (point) (progn (forward-line 1) (point))))
333 (when nnmail-cache-accepted-message-ids
334 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
335 (setq result (if (stringp group)
336 (list (cons group (nnmbox-active-number group)))
337 (nnmail-article-group 'nnmbox-active-number)))
338 (if (and (null result)
339 (yes-or-no-p "Moved to `junk' group; delete article? "))
341 (setq result (car (nnmbox-save-mail result)))))
343 (set-buffer nnmbox-mbox-buffer)
344 (goto-char (point-max))
345 (insert-buffer-substring buf)
347 (when nnmail-cache-accepted-message-ids
348 (nnmail-cache-close))
349 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
350 (nnmbox-save-buffer))))
353 (deffoo nnmbox-request-replace-article (article group buffer)
354 (nnmbox-possibly-change-newsgroup group)
356 (set-buffer nnmbox-mbox-buffer)
357 (if (not (nnmbox-find-article article))
359 (nnmbox-delete-mail t t)
360 (insert-buffer-substring buffer)
364 (deffoo nnmbox-request-delete-group (group &optional force server)
365 (nnmbox-possibly-change-newsgroup group server)
366 ;; Delete all articles in GROUP.
368 () ; Don't delete the articles.
370 (set-buffer nnmbox-mbox-buffer)
371 (goto-char (point-min))
372 ;; Delete all articles in this group.
373 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
375 (while (search-forward ident nil t)
377 (nnmbox-delete-mail))
379 (nnmbox-save-buffer)))))
380 ;; Remove the group from all structures.
381 (setq nnmbox-group-alist
382 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
383 nnmbox-current-group nil)
384 ;; Save the active file.
385 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
388 (deffoo nnmbox-request-rename-group (group new-name &optional server)
389 (nnmbox-possibly-change-newsgroup group server)
391 (set-buffer nnmbox-mbox-buffer)
392 (goto-char (point-min))
393 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
394 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
396 (while (search-forward ident nil t)
397 (replace-match new-ident t t)
400 (nnmbox-save-buffer))))
401 (let ((entry (assoc group nnmbox-group-active-articles)))
403 (setcar entry new-name)))
404 (let ((entry (assoc group nnmbox-group-alist)))
406 (setcar entry new-name))
407 (setq nnmbox-current-group nil)
408 ;; Save the new group alist.
409 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
413 ;;; Internal functions.
415 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
416 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
418 (defun nnmbox-delete-mail (&optional force leave-delim)
419 ;; Delete the current X-Gnus-Newsgroup line.
420 ;; First delete record of active article, unless the article is being
421 ;; replaced, indicated by FORCE being non-nil.
423 (nnmbox-record-deleted-article (nnmbox-article-group-number t)))
426 (progn (beginning-of-line) (point))
427 (progn (forward-line 1) (point))))
428 ;; Beginning of the article.
433 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
434 (if leave-delim (progn (forward-line 1) (point))
435 (match-beginning 0)))
438 (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
440 (if (and (not (bobp)) leave-delim)
441 (progn (forward-line -2) (point))
442 (match-beginning 0)))
444 (goto-char (point-min))
445 ;; Only delete the article if no other group owns it as well.
446 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
447 (delete-region (point-min) (point-max))))))
449 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
451 (not (nnmbox-server-opened server)))
452 (nnmbox-open-server server))
453 (when (or (not nnmbox-mbox-buffer)
454 (not (buffer-name nnmbox-mbox-buffer)))
456 (when (not nnmbox-group-alist)
457 (nnmail-activate 'nnmbox))
459 (when (assoc newsgroup nnmbox-group-alist)
460 (setq nnmbox-current-group newsgroup))
463 (defun nnmbox-article-string (article)
464 (if (numberp article)
465 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
466 (int-to-string article) " ")
467 (concat "\nMessage-ID: " article)))
469 (defun nnmbox-article-group-number (this-line)
473 (goto-char (point-min)))
474 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
476 (cons (buffer-substring (match-beginning 1) (match-end 1))
478 (buffer-substring (match-beginning 2) (match-end 2)))))))
480 (defun nnmbox-in-header-p (pos)
481 "Return non-nil if POS is in the header of an article."
484 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
485 (search-forward "\n\n" nil t)
488 (defun nnmbox-find-article (article)
489 "Leaves point on the relevant X-Gnus-Newsgroup line if found."
490 ;; Check that article is in the active range first, to avoid an
491 ;; expensive exhaustive search if it isn't.
492 (if (and (numberp article)
493 (not (nnmbox-is-article-active-p article)))
495 (let ((art-string (nnmbox-article-string article))
497 ;; There is the possibility that the X-Gnus-Newsgroup line appears
498 ;; in the body of an article (for instance, if an article has been
499 ;; forwarded from someone using Gnus as their mailer), so check
500 ;; that the line is actually part of the article header.
501 (or (and (search-forward art-string nil t)
502 (nnmbox-in-header-p (point)))
504 (goto-char (point-min))
506 (setq found (and (search-forward art-string nil t)
507 (nnmbox-in-header-p (point)))))
510 (defun nnmbox-record-active-article (group-art)
511 (let* ((group (car group-art))
512 (article (cdr group-art))
514 (or (assoc group nnmbox-group-active-articles)
517 nnmbox-group-active-articles)
518 (car nnmbox-group-active-articles)))))
519 ;; add article to index, either by building complete list
520 ;; in reverse order, or as a list of ranges.
521 (if (not nnmbox-group-building-active-articles)
522 (setcdr entry (gnus-add-to-range (cdr entry) (list article)))
523 (when (memq article (cdr entry))
524 (switch-to-buffer nnmbox-mbox-buffer)
525 (error "Article %s:%d already exists!" group article))
526 (when (and (cadr entry) (< article (cadr entry)))
527 (switch-to-buffer nnmbox-mbox-buffer)
528 (error "Article %s:%d out of order" group article))
529 (setcdr entry (cons article (cdr entry))))))
531 (defun nnmbox-record-deleted-article (group-art)
532 (let* ((group (car group-art))
533 (article (cdr group-art))
535 (or (assoc group nnmbox-group-active-articles)
538 nnmbox-group-active-articles)
539 (car nnmbox-group-active-articles)))))
540 ;; remove article from index
541 (setcdr entry (gnus-remove-from-range (cdr entry) (list article)))))
543 (defun nnmbox-is-article-active-p (article)
544 (gnus-member-of-range
546 (cdr (assoc nnmbox-current-group
547 nnmbox-group-active-articles))))
549 (defun nnmbox-save-mail (group-art)
550 "Called narrowed to an article."
551 (let ((delim (concat "^" message-unix-mail-delimiter)))
552 (goto-char (point-min))
553 ;; This might come from somewhere else.
554 (unless (looking-at delim)
555 (insert "From nobody " (current-time-string) "\n")
556 (goto-char (point-min)))
557 ;; Quote all "From " lines in the article.
559 (while (re-search-forward delim nil t)
562 (nnmail-insert-lines)
563 (nnmail-insert-xref group-art)
564 (nnmbox-insert-newsgroup-line group-art)
565 (let ((alist group-art))
567 (nnmbox-record-active-article (car alist))
568 (setq alist (cdr alist))))
569 (run-hooks 'nnmail-prepare-save-mail-hook)
570 (run-hooks 'nnmbox-prepare-save-mail-hook)
573 (defun nnmbox-insert-newsgroup-line (group-art)
575 (goto-char (point-min))
576 (when (search-forward "\n\n" nil t)
579 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
580 (caar group-art) (cdar group-art)
581 (current-time-string)))
582 (setq group-art (cdr group-art))))
585 (defun nnmbox-active-number (group)
586 ;; Find the next article number in GROUP.
587 (let ((active (cadr (assoc group nnmbox-group-alist))))
589 (setcdr active (1+ (cdr active)))
590 ;; This group is new, so we create a new entry for it.
591 ;; This might be a bit naughty... creating groups on the drop of
592 ;; a hat, but I don't know...
593 (push (list group (setq active (cons 1 1)))
597 (defun nnmbox-create-mbox ()
598 (when (not (file-exists-p nnmbox-mbox-file))
599 (let ((nnmail-file-coding-system
600 (or nnmbox-file-coding-system-for-write
601 nnmbox-file-coding-system)))
602 (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg))))
604 (defun nnmbox-read-mbox ()
605 (nnmail-activate 'nnmbox)
607 (if (and nnmbox-mbox-buffer
608 (buffer-name nnmbox-mbox-buffer)
610 (set-buffer nnmbox-mbox-buffer)
611 (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
614 (let ((delim (concat "^" message-unix-mail-delimiter))
615 (alist nnmbox-group-alist)
616 (nnmbox-group-building-active-articles t)
617 start end end-header number)
618 (set-buffer (setq nnmbox-mbox-buffer
619 (let ((nnheader-file-coding-system
620 nnmbox-file-coding-system))
621 (nnheader-find-file-noselect
622 nnmbox-mbox-file nil t))))
623 (mm-enable-multibyte)
624 (buffer-disable-undo)
626 ;; Go through the group alist and compare against the mbox file.
628 (goto-char (point-max))
629 (when (and (re-search-backward
630 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
635 (match-beginning 1) (match-end 1))))
637 (setcdr (cadar alist) number))
638 (setq alist (cdr alist)))
640 ;; Examine all articles for our private X-Gnus-Newsgroup
641 ;; headers. This is done primarily as a consistency check, but
642 ;; it is convenient for building an index of the articles
643 ;; present, to avoid costly searches for missing articles
644 ;; (eg. when expiring articles).
645 (goto-char (point-min))
646 (setq nnmbox-group-active-articles nil)
647 (while (re-search-forward delim nil t)
648 (setq start (match-beginning 0))
650 (search-forward "\n\n" nil t)
651 (setq end-header (point))
653 (re-search-forward delim nil t)
656 (if (search-forward "\nX-Gnus-Newsgroup: " end-header t)
657 ;; Build a list of articles in each group, remembering
658 ;; that each article may be in more than one group.
660 (nnmbox-record-active-article (nnmbox-article-group-number t))
661 (while (search-forward "\nX-Gnus-Newsgroup: " end-header t)
662 (nnmbox-record-active-article (nnmbox-article-group-number t))))
663 ;; The article is either new, or for some other reason
664 ;; hasn't got our private headers, so add them now. The
665 ;; only situation I've encountered when the X-Gnus-Newsgroup
666 ;; header is missing is if the article contains a forwarded
667 ;; message which does contain that header line (earlier
668 ;; versions of Gnus didn't restrict their search to the
669 ;; headers). In this case, there is an Xref line which
670 ;; provides the relevant information to construct the
671 ;; missing header(s).
674 (narrow-to-region start end)
675 (if (re-search-forward "\nXref: [^ ]+" end-header t)
676 ;; generate headers from Xref:
678 (while (re-search-forward " \\([^:]+\\):\\([0-9]+\\)" end-header t)
679 (push (cons (match-string 1)
680 (string-to-int (match-string 2))) alist))
681 (nnmbox-insert-newsgroup-line alist))
682 ;; this is really a new article
684 (nnmail-article-group 'nnmbox-active-number))))))
686 ;; put article lists in order
687 (setq alist nnmbox-group-active-articles)
689 (setcdr (car alist) (gnus-compress-sequence (nreverse (cdar alist))))
690 (setq alist (cdr alist)))))))
694 ;;; nnmbox.el ends here