1 ;;; nnmbox.el --- mail mbox access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
44 "The name of the active file for the mail box.")
46 (defvoo nnmbox-get-new-mail t
47 "If non-nil, nnmbox will check the incoming mail file and split the mail.")
49 (defvoo nnmbox-prepare-save-mail-hook nil
50 "Hook run narrowed to an article before saving.")
54 (defconst nnmbox-version "nnmbox 1.0"
57 (defvoo nnmbox-current-group nil
58 "Current nnmbox news group directory.")
60 (defvar nnmbox-mbox-buffer nil)
62 (defvoo nnmbox-status-string "")
64 (defvoo nnmbox-group-alist nil)
65 (defvoo nnmbox-active-timestamp nil)
67 (defvoo nnmbox-file-coding-system 'binary)
68 (defvoo nnmbox-file-coding-system-for-write nil)
69 (defvoo nnmbox-active-file-coding-system 'binary)
70 (defvoo nnmbox-active-file-coding-system-for-write nil)
72 (defvar nnmbox-group-building-active-articles nil)
73 (defvar nnmbox-group-active-articles nil)
76 ;;; Interface functions
78 (nnoo-define-basics nnmbox)
80 (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
82 (set-buffer nntp-server-buffer)
84 (let ((number (length sequence))
87 (nnmbox-possibly-change-newsgroup newsgroup server)
89 (setq article (car sequence))
90 (set-buffer nnmbox-mbox-buffer)
91 (when (nnmbox-find-article article)
95 (concat "^" message-unix-mail-delimiter) nil t)
97 (search-forward "\n\n" nil t)
98 (setq stop (1- (point)))
99 (set-buffer nntp-server-buffer)
100 (insert (format "221 %d Article retrieved.\n" article))
101 (insert-buffer-substring nnmbox-mbox-buffer start stop)
102 (goto-char (point-max))
104 (setq sequence (cdr sequence))
105 (setq count (1+ count))
106 (and (numberp nnmail-large-newsgroup)
107 (> number nnmail-large-newsgroup)
109 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
110 (/ (* count 100) number))))
112 (and (numberp nnmail-large-newsgroup)
113 (> number nnmail-large-newsgroup)
114 (nnheader-message 5 "nnmbox: Receiving headers...done"))
116 (set-buffer nntp-server-buffer)
117 (nnheader-fold-continuation-lines)
120 (deffoo nnmbox-open-server (server &optional defs)
121 (nnoo-change-server 'nnmbox server defs)
124 ((not (file-exists-p nnmbox-mbox-file))
125 (nnmbox-close-server)
126 (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
127 ((file-directory-p nnmbox-mbox-file)
128 (nnmbox-close-server)
129 (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
131 (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
135 (deffoo nnmbox-close-server (&optional server)
136 (when (and nnmbox-mbox-buffer
137 (buffer-name nnmbox-mbox-buffer))
138 (kill-buffer nnmbox-mbox-buffer))
139 (nnoo-close-server 'nnmbox server)
142 (deffoo nnmbox-server-opened (&optional server)
143 (and (nnoo-current-server-p 'nnmbox server)
145 (buffer-name nnmbox-mbox-buffer)
147 (buffer-name nntp-server-buffer)))
149 (deffoo nnmbox-request-article (article &optional newsgroup server buffer)
150 (nnmbox-possibly-change-newsgroup newsgroup server)
152 (set-buffer nnmbox-mbox-buffer)
153 (when (nnmbox-find-article article)
155 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
158 (or (and (re-search-forward
159 (concat "^" message-unix-mail-delimiter) nil t)
161 (goto-char (point-max)))
163 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
164 (set-buffer nntp-server-buffer)
166 (insert-buffer-substring nnmbox-mbox-buffer start stop)
167 (goto-char (point-min))
168 (while (looking-at "From ")
170 (insert "X-From-Line: ")
172 (if (numberp article)
173 (cons nnmbox-current-group article)
174 (nnmbox-article-group-number nil)))))))
176 (deffoo nnmbox-request-group (group &optional server dont-check)
177 (nnmbox-possibly-change-newsgroup nil server)
178 (let ((active (cadr (assoc group nnmbox-group-alist))))
181 (null (nnmbox-possibly-change-newsgroup group server)))
182 (nnheader-report 'nnmbox "No such group: %s" group))
184 (nnheader-report 'nnmbox "Selected group %s" group)
185 (nnheader-insert ""))
187 (nnheader-report 'nnmbox "Selected group %s" group)
188 (nnheader-insert "211 %d %d %d %s\n"
189 (1+ (- (cdr active) (car active)))
190 (car active) (cdr active) group)))))
192 (defun nnmbox-save-buffer ()
193 (let* ((coding-system-for-write (or nnmbox-file-coding-system-for-write
194 nnmbox-file-coding-system))
195 (output-coding-system coding-system-for-write))
198 (defun nnmbox-save-active (group-alist active-file)
199 (let ((nnmail-active-file-coding-system
200 (or nnmbox-active-file-coding-system-for-write
201 nnmbox-active-file-coding-system)))
202 (nnmail-save-active group-alist active-file)))
204 (deffoo nnmbox-request-scan (&optional group server)
205 (nnmbox-possibly-change-newsgroup group server)
211 (set-buffer nnmbox-mbox-buffer)
212 (nnmbox-save-buffer)))
213 (file-name-directory nnmbox-mbox-file)
217 (let ((in-buf (current-buffer)))
218 (set-buffer nnmbox-mbox-buffer)
219 (goto-char (point-max))
220 (insert-buffer-substring in-buf)))
221 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
223 (deffoo nnmbox-close-group (group &optional server)
226 (deffoo nnmbox-request-create-group (group &optional server args)
227 (nnmail-activate 'nnmbox)
228 (unless (assoc group nnmbox-group-alist)
229 (push (list group (cons 1 0))
231 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
234 (deffoo nnmbox-request-list (&optional server)
236 (let ((nnmail-file-coding-system
237 nnmbox-active-file-coding-system))
238 (nnmail-find-file nnmbox-active-file))
239 (setq nnmbox-group-alist (nnmail-get-active))
242 (deffoo nnmbox-request-newgroups (date &optional server)
243 (nnmbox-request-list server))
245 (deffoo nnmbox-request-list-newsgroups (&optional server)
246 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
248 (deffoo nnmbox-request-expire-articles
249 (articles newsgroup &optional server force)
250 (nnmbox-possibly-change-newsgroup newsgroup server)
253 (nnmail-activate 'nnmbox)
256 (set-buffer nnmbox-mbox-buffer)
257 (while (and articles is-old)
258 (when (nnmbox-find-article (car articles))
260 (nnmail-expired-article-p
263 (point) (progn (end-of-line) (point))) force))
265 (unless (eq nnmail-expiry-target 'delete)
267 (nnmbox-request-article (car articles)
270 (let ((nnml-current-directory nil))
271 (nnmail-expiry-target-group
272 nnmail-expiry-target newsgroup)))
273 (nnmbox-possibly-change-newsgroup newsgroup server))
274 (nnheader-message 5 "Deleting article %d in %s..."
275 (car articles) newsgroup)
276 (nnmbox-delete-mail))
277 (push (car articles) rest)))
278 (setq articles (cdr articles)))
280 ;; Find the lowest active article in this group.
281 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
282 (while (and (not (nnmbox-find-article (car active)))
283 (<= (car active) (cdr active)))
284 (setcar active (1+ (car active)))))
285 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
286 (nconc rest articles))))
288 (deffoo nnmbox-request-move-article
289 (article group server accept-form &optional last)
290 (let ((buf (get-buffer-create " *nnmbox move*"))
293 (nnmbox-request-article article group server)
297 (insert-buffer-substring nntp-server-buffer)
298 (goto-char (point-min))
299 (while (re-search-forward
301 (save-excursion (search-forward "\n\n" nil t) (point)) t)
303 (setq result (eval accept-form))
307 (nnmbox-possibly-change-newsgroup group server)
308 (set-buffer nnmbox-mbox-buffer)
309 (when (nnmbox-find-article article)
310 (nnmbox-delete-mail))
311 (and last (nnmbox-save-buffer))))
314 (deffoo nnmbox-request-accept-article (group &optional server last)
315 (nnmbox-possibly-change-newsgroup group server)
316 (nnmail-check-syntax)
317 (let ((buf (current-buffer))
319 (goto-char (point-min))
320 ;; The From line may have been quoted by movemail.
321 (when (looking-at (concat ">" message-unix-mail-delimiter))
323 (if (looking-at "X-From-Line: ")
324 (replace-match "From ")
325 (insert "From nobody " (current-time-string) "\n"))
327 (nnmail-activate 'nnmbox)
330 (goto-char (point-min))
331 (search-forward "\n\n" nil t)
333 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
334 (delete-region (point) (progn (forward-line 1) (point))))
335 (when nnmail-cache-accepted-message-ids
336 (nnmail-cache-insert (nnmail-fetch-field "message-id")
338 (nnmail-fetch-field "subject")
339 (nnmail-fetch-field "from")))
340 (setq result (if (stringp group)
341 (list (cons group (nnmbox-active-number group)))
342 (nnmail-article-group 'nnmbox-active-number)))
343 (if (and (null result)
344 (yes-or-no-p "Moved to `junk' group; delete article? "))
346 (setq result (car (nnmbox-save-mail result)))))
348 (set-buffer nnmbox-mbox-buffer)
349 (goto-char (point-max))
350 (insert-buffer-substring buf)
352 (when nnmail-cache-accepted-message-ids
353 (nnmail-cache-close))
354 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
355 (nnmbox-save-buffer))))
358 (deffoo nnmbox-request-replace-article (article group buffer)
359 (nnmbox-possibly-change-newsgroup group)
361 (set-buffer nnmbox-mbox-buffer)
362 (if (not (nnmbox-find-article article))
364 (nnmbox-delete-mail t t)
365 (insert-buffer-substring buffer)
369 (deffoo nnmbox-request-delete-group (group &optional force server)
370 (nnmbox-possibly-change-newsgroup group server)
371 ;; Delete all articles in GROUP.
373 () ; Don't delete the articles.
375 (set-buffer nnmbox-mbox-buffer)
376 (goto-char (point-min))
377 ;; Delete all articles in this group.
378 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
380 (while (search-forward ident nil t)
382 (nnmbox-delete-mail))
384 (nnmbox-save-buffer)))))
385 ;; Remove the group from all structures.
386 (setq nnmbox-group-alist
387 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
388 nnmbox-current-group nil)
389 ;; Save the active file.
390 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
393 (deffoo nnmbox-request-rename-group (group new-name &optional server)
394 (nnmbox-possibly-change-newsgroup group server)
396 (set-buffer nnmbox-mbox-buffer)
397 (goto-char (point-min))
398 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
399 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
401 (while (search-forward ident nil t)
402 (replace-match new-ident t t)
405 (nnmbox-save-buffer))))
406 (let ((entry (assoc group nnmbox-group-active-articles)))
408 (setcar entry new-name)))
409 (let ((entry (assoc group nnmbox-group-alist)))
411 (setcar entry new-name))
412 (setq nnmbox-current-group nil)
413 ;; Save the new group alist.
414 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
418 ;;; Internal functions.
420 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
421 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
423 (defun nnmbox-delete-mail (&optional force leave-delim)
424 ;; Delete the current X-Gnus-Newsgroup line.
425 ;; First delete record of active article, unless the article is being
426 ;; replaced, indicated by FORCE being non-nil.
428 (nnmbox-record-deleted-article (nnmbox-article-group-number t)))
431 ;; Beginning of the article.
436 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
437 (if leave-delim (progn (forward-line 1) (point))
438 (match-beginning 0)))
441 (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
443 (if (and (not (bobp)) leave-delim)
444 (progn (forward-line -2) (point))
445 (match-beginning 0)))
447 (goto-char (point-min))
448 ;; Only delete the article if no other group owns it as well.
449 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
450 (delete-region (point-min) (point-max))))))
452 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
454 (not (nnmbox-server-opened server)))
455 (nnmbox-open-server server))
456 (when (or (not nnmbox-mbox-buffer)
457 (not (buffer-name nnmbox-mbox-buffer)))
459 (when (not nnmbox-group-alist)
460 (nnmail-activate 'nnmbox))
462 (when (assoc newsgroup nnmbox-group-alist)
463 (setq nnmbox-current-group newsgroup))
466 (defun nnmbox-article-string (article)
467 (if (numberp article)
468 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
469 (int-to-string article) " ")
470 (concat "\nMessage-ID: " article)))
472 (defun nnmbox-article-group-number (this-line)
476 (goto-char (point-min)))
477 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
479 (cons (buffer-substring (match-beginning 1) (match-end 1))
481 (buffer-substring (match-beginning 2) (match-end 2)))))))
483 (defun nnmbox-in-header-p (pos)
484 "Return non-nil if POS is in the header of an article."
487 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
488 (search-forward "\n\n" nil t)
491 (defun nnmbox-find-article (article)
492 "Leaves point on the relevant X-Gnus-Newsgroup line if found."
493 ;; Check that article is in the active range first, to avoid an
494 ;; expensive exhaustive search if it isn't.
495 (if (and (numberp article)
496 (not (nnmbox-is-article-active-p article)))
498 (let ((art-string (nnmbox-article-string article))
500 ;; There is the possibility that the X-Gnus-Newsgroup line appears
501 ;; in the body of an article (for instance, if an article has been
502 ;; forwarded from someone using Gnus as their mailer), so check
503 ;; that the line is actually part of the article header.
504 (or (and (search-forward art-string nil t)
505 (nnmbox-in-header-p (point)))
507 (goto-char (point-min))
508 (while (and (not found)
509 (search-forward art-string nil t))
510 (setq found (nnmbox-in-header-p (point))))
513 (defun nnmbox-record-active-article (group-art)
514 (let* ((group (car group-art))
515 (article (cdr group-art))
517 (or (assoc group nnmbox-group-active-articles)
520 nnmbox-group-active-articles)
521 (car nnmbox-group-active-articles)))))
522 ;; add article to index, either by building complete list
523 ;; in reverse order, or as a list of ranges.
524 (if (not nnmbox-group-building-active-articles)
525 (setcdr entry (gnus-add-to-range (cdr entry) (list article)))
526 (when (memq article (cdr entry))
527 (switch-to-buffer nnmbox-mbox-buffer)
528 (error "Article %s:%d already exists!" group article))
529 (when (and (cadr entry) (< article (cadr entry)))
530 (switch-to-buffer nnmbox-mbox-buffer)
531 (error "Article %s:%d out of order" group article))
532 (setcdr entry (cons article (cdr entry))))))
534 (defun nnmbox-record-deleted-article (group-art)
535 (let* ((group (car group-art))
536 (article (cdr group-art))
538 (or (assoc group nnmbox-group-active-articles)
541 nnmbox-group-active-articles)
542 (car nnmbox-group-active-articles)))))
543 ;; remove article from index
544 (setcdr entry (gnus-remove-from-range (cdr entry) (list article)))))
546 (defun nnmbox-is-article-active-p (article)
547 (gnus-member-of-range
549 (cdr (assoc nnmbox-current-group
550 nnmbox-group-active-articles))))
552 (defun nnmbox-save-mail (group-art)
553 "Called narrowed to an article."
554 (let ((delim (concat "^" message-unix-mail-delimiter)))
555 (goto-char (point-min))
556 ;; This might come from somewhere else.
557 (unless (looking-at delim)
558 (insert "From nobody " (current-time-string) "\n")
559 (goto-char (point-min)))
560 ;; Quote all "From " lines in the article.
562 (while (re-search-forward delim nil t)
565 (nnmail-insert-lines)
566 (nnmail-insert-xref group-art)
567 (nnmbox-insert-newsgroup-line group-art)
568 (let ((alist group-art))
570 (nnmbox-record-active-article (car alist))
571 (setq alist (cdr alist))))
572 (run-hooks 'nnmail-prepare-save-mail-hook)
573 (run-hooks 'nnmbox-prepare-save-mail-hook)
576 (defun nnmbox-insert-newsgroup-line (group-art)
578 (goto-char (point-min))
579 (when (search-forward "\n\n" nil t)
582 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
583 (caar group-art) (cdar group-art)
584 (current-time-string)))
585 (setq group-art (cdr group-art))))
588 (defun nnmbox-active-number (group)
589 ;; Find the next article number in GROUP.
590 (let ((active (cadr (assoc group nnmbox-group-alist))))
592 (setcdr active (1+ (cdr active)))
593 ;; This group is new, so we create a new entry for it.
594 ;; This might be a bit naughty... creating groups on the drop of
595 ;; a hat, but I don't know...
596 (push (list group (setq active (cons 1 1)))
600 (defun nnmbox-create-mbox ()
601 (when (not (file-exists-p nnmbox-mbox-file))
602 (let ((nnmail-file-coding-system
603 (or nnmbox-file-coding-system-for-write
604 nnmbox-file-coding-system))
605 (dir (file-name-directory nnmbox-mbox-file)))
606 (and dir (gnus-make-directory dir))
607 (nnmail-write-region (point-min) (point-min)
608 nnmbox-mbox-file t 'nomesg))))
610 (defun nnmbox-read-mbox ()
611 (nnmail-activate 'nnmbox)
613 (if (and nnmbox-mbox-buffer
614 (buffer-name nnmbox-mbox-buffer)
616 (set-buffer nnmbox-mbox-buffer)
617 (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
620 (let ((delim (concat "^" message-unix-mail-delimiter))
621 (alist nnmbox-group-alist)
622 (nnmbox-group-building-active-articles t)
623 start end end-header number)
624 (set-buffer (setq nnmbox-mbox-buffer
625 (let ((nnheader-file-coding-system
626 nnmbox-file-coding-system))
627 (nnheader-find-file-noselect
628 nnmbox-mbox-file t t))))
629 (buffer-disable-undo)
631 ;; Go through the group alist and compare against the mbox file.
633 (goto-char (point-max))
634 (when (and (re-search-backward
635 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
640 (match-beginning 1) (match-end 1))))
642 (setcdr (cadar alist) number))
643 (setq alist (cdr alist)))
645 ;; Examine all articles for our private X-Gnus-Newsgroup
646 ;; headers. This is done primarily as a consistency check, but
647 ;; it is convenient for building an index of the articles
648 ;; present, to avoid costly searches for missing articles
649 ;; (eg. when expiring articles).
650 (goto-char (point-min))
651 (setq nnmbox-group-active-articles nil)
652 (while (re-search-forward delim nil t)
653 (setq start (match-beginning 0))
655 (search-forward "\n\n" nil t)
656 (setq end-header (point))
658 (re-search-forward delim nil t)
661 (if (search-forward "\nX-Gnus-Newsgroup: " end-header t)
662 ;; Build a list of articles in each group, remembering
663 ;; that each article may be in more than one group.
665 (nnmbox-record-active-article (nnmbox-article-group-number t))
666 (while (search-forward "\nX-Gnus-Newsgroup: " end-header t)
667 (nnmbox-record-active-article (nnmbox-article-group-number t))))
668 ;; The article is either new, or for some other reason
669 ;; hasn't got our private headers, so add them now. The
670 ;; only situation I've encountered when the X-Gnus-Newsgroup
671 ;; header is missing is if the article contains a forwarded
672 ;; message which does contain that header line (earlier
673 ;; versions of Gnus didn't restrict their search to the
674 ;; headers). In this case, there is an Xref line which
675 ;; provides the relevant information to construct the
676 ;; missing header(s).
679 (narrow-to-region start end)
680 (if (re-search-forward "\nXref: [^ ]+" end-header t)
681 ;; generate headers from Xref:
683 (while (re-search-forward " \\([^:]+\\):\\([0-9]+\\)" end-header t)
684 (push (cons (match-string 1)
685 (string-to-int (match-string 2))) alist))
686 (nnmbox-insert-newsgroup-line alist))
687 ;; this is really a new article
689 (nnmail-article-group 'nnmbox-active-number))))))
691 ;; put article lists in order
692 (setq alist nnmbox-group-active-articles)
694 (setcdr (car alist) (gnus-compress-sequence (nreverse (cdar alist))))
695 (setq alist (cdr alist)))))))
699 ;;; nnmbox.el ends here