1 ;;; nnmbox.el --- mail mbox access for Gnus
3 ;; Copyright (C) 1995-2014 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; Keywords: news, mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; For an overview of what the interface functions do, please see the
36 (eval-when-compile (require 'cl
))
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 mm-binary-coding-system
)
68 (defvoo nnmbox-file-coding-system-for-write nil
)
69 (defvoo nnmbox-active-file-coding-system mm-binary-coding-system
)
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
)
81 (with-current-buffer nntp-server-buffer
83 (let ((number (length sequence
))
86 (nnmbox-possibly-change-newsgroup newsgroup server
)
88 (setq article
(car sequence
))
89 (set-buffer nnmbox-mbox-buffer
)
90 (when (nnmbox-find-article article
)
94 (concat "^" message-unix-mail-delimiter
) nil t
)
96 (search-forward "\n\n" nil t
)
97 (setq stop
(1- (point)))
98 (set-buffer nntp-server-buffer
)
99 (insert (format "221 %d Article retrieved.\n" article
))
100 (insert-buffer-substring nnmbox-mbox-buffer start stop
)
101 (goto-char (point-max))
103 (setq sequence
(cdr sequence
))
104 (setq count
(1+ count
))
105 (and (numberp nnmail-large-newsgroup
)
106 (> number nnmail-large-newsgroup
)
108 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
109 (/ (* count
100) number
))))
111 (and (numberp nnmail-large-newsgroup
)
112 (> number nnmail-large-newsgroup
)
113 (nnheader-message 5 "nnmbox: Receiving headers...done"))
115 (set-buffer nntp-server-buffer
)
116 (nnheader-fold-continuation-lines)
119 (deffoo nnmbox-open-server
(server &optional defs
)
120 (nnoo-change-server 'nnmbox server defs
)
123 ((not (file-exists-p nnmbox-mbox-file
))
124 (nnmbox-close-server)
125 (nnheader-report 'nnmbox
"No such file: %s" nnmbox-mbox-file
))
126 ((file-directory-p nnmbox-mbox-file
)
127 (nnmbox-close-server)
128 (nnheader-report 'nnmbox
"Not a regular file: %s" nnmbox-mbox-file
))
130 (nnheader-report 'nnmbox
"Opened server %s using mbox %s" server
134 (deffoo nnmbox-close-server
(&optional server
)
135 (when (and nnmbox-mbox-buffer
136 (buffer-name nnmbox-mbox-buffer
))
137 (kill-buffer nnmbox-mbox-buffer
))
138 (nnoo-close-server 'nnmbox server
)
141 (deffoo nnmbox-server-opened
(&optional server
)
142 (and (nnoo-current-server-p 'nnmbox server
)
144 (buffer-name nnmbox-mbox-buffer
)
146 (buffer-name nntp-server-buffer
)))
148 (deffoo nnmbox-request-article
(article &optional newsgroup server buffer
)
149 (nnmbox-possibly-change-newsgroup newsgroup server
)
150 (with-current-buffer nnmbox-mbox-buffer
152 (when (nnmbox-find-article article
)
154 (re-search-backward (concat "^" message-unix-mail-delimiter
) nil t
)
157 (setq stop
(if (re-search-forward (concat "^"
158 message-unix-mail-delimiter
)
162 (let ((nntp-server-buffer (or buffer nntp-server-buffer
)))
163 (set-buffer nntp-server-buffer
)
165 (insert-buffer-substring nnmbox-mbox-buffer start stop
)
166 (goto-char (point-min))
167 (while (looking-at "From ")
169 (insert "X-From-Line: ")
171 (if (numberp article
)
172 (cons nnmbox-current-group article
)
173 (nnmbox-article-group-number nil
))))))))
175 (deffoo nnmbox-request-group
(group &optional server dont-check info
)
176 (nnmbox-possibly-change-newsgroup nil server
)
177 (let ((active (cadr (assoc group nnmbox-group-alist
))))
180 (null (nnmbox-possibly-change-newsgroup group server
)))
181 (nnheader-report 'nnmbox
"No such group: %s" group
))
183 (nnheader-report 'nnmbox
"Selected group %s" group
)
184 (nnheader-insert ""))
186 (nnheader-report 'nnmbox
"Selected group %s" group
)
187 (nnheader-insert "211 %d %d %d %s\n"
188 (1+ (- (cdr active
) (car active
)))
189 (car active
) (cdr active
) group
)))))
191 (defun nnmbox-save-buffer ()
192 (let ((coding-system-for-write
193 (or nnmbox-file-coding-system-for-write
194 nnmbox-file-coding-system
)))
197 (defun nnmbox-save-active (group-alist active-file
)
198 (let ((nnmail-active-file-coding-system
199 (or nnmbox-active-file-coding-system-for-write
200 nnmbox-active-file-coding-system
)))
201 (nnmail-save-active group-alist active-file
)))
203 (deffoo nnmbox-request-scan
(&optional group server
)
204 (nnmbox-possibly-change-newsgroup group server
)
209 (with-current-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
)
253 (with-current-buffer nnmbox-mbox-buffer
254 (while (and articles is-old
)
255 (when (nnmbox-find-article (car articles
))
257 (nnmail-expired-article-p
259 (buffer-substring (point) (line-end-position))
262 (unless (eq nnmail-expiry-target
'delete
)
264 (nnmbox-request-article (car articles
)
267 (let ((nnml-current-directory nil
))
268 (nnmail-expiry-target-group
269 nnmail-expiry-target newsgroup
)))
270 (nnmbox-possibly-change-newsgroup newsgroup server
))
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 move-is-internal
)
287 (let ((buf (get-buffer-create " *nnmbox move*"))
290 (nnmbox-request-article article group server
)
291 (with-current-buffer buf
293 (insert-buffer-substring nntp-server-buffer
)
294 (goto-char (point-min))
295 (while (re-search-forward
297 (save-excursion (search-forward "\n\n" nil t
) (point)) t
)
299 (setq result
(eval accept-form
))
303 (nnmbox-possibly-change-newsgroup group server
)
304 (set-buffer nnmbox-mbox-buffer
)
305 (when (nnmbox-find-article article
)
306 (nnmbox-delete-mail))
307 (and last
(nnmbox-save-buffer))))
310 (deffoo nnmbox-request-accept-article
(group &optional server last
)
311 (nnmbox-possibly-change-newsgroup group server
)
312 (nnmail-check-syntax)
313 (let ((buf (current-buffer))
316 (nnmail-activate 'nnmbox
)
318 (insert-buffer-substring buf
)
319 (goto-char (point-min))
320 (cond (;; The From line may have been quoted by movemail.
321 (looking-at (concat ">" message-unix-mail-delimiter
))
324 ((looking-at "X-From-Line: ")
325 (replace-match "From ")
328 (insert "From nobody " (current-time-string) "\n")))
329 (narrow-to-region (point)
330 (if (search-forward "\n\n" nil
'move
)
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 (message-fetch-field "message-id")
338 (message-fetch-field "subject")
339 (message-fetch-field "from")))
341 (setq result
(if (stringp group
)
342 (list (cons group
(nnmbox-active-number group
)))
343 (nnmail-article-group 'nnmbox-active-number
)))
345 (if (and (null result
)
346 (yes-or-no-p "Moved to `junk' group; delete article? "))
348 (setq result
(car (nnmbox-save-mail result
))))
349 (setq cont
(buffer-string))))
350 (with-current-buffer nnmbox-mbox-buffer
351 (goto-char (point-max))
354 (when nnmail-cache-accepted-message-ids
355 (nnmail-cache-close))
356 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file
)
357 (nnmbox-save-buffer))))
360 (deffoo nnmbox-request-replace-article
(article group buffer
)
361 (nnmbox-possibly-change-newsgroup group
)
362 (with-current-buffer nnmbox-mbox-buffer
363 (if (not (nnmbox-find-article article
))
365 (nnmbox-delete-mail t t
)
368 (insert-buffer-substring buffer
)
369 (goto-char (point-min))
370 (when (looking-at "X-From-Line:")
371 (delete-region (point) (progn (forward-line 1) (point))))
372 (while (re-search-forward (concat "^" message-unix-mail-delimiter
)
374 (goto-char (match-beginning 0))
376 (goto-char (point-max))
383 (deffoo nnmbox-request-delete-group
(group &optional force server
)
384 (nnmbox-possibly-change-newsgroup group server
)
385 ;; Delete all articles in GROUP.
387 () ; Don't delete the articles.
388 (with-current-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
)
408 (with-current-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 ;; Beginning of the article.
448 (re-search-backward (concat "^" message-unix-mail-delimiter
) nil t
)
449 (if leave-delim
(progn (forward-line 1) (point))
452 (or (and (re-search-forward (concat "^" message-unix-mail-delimiter
)
456 (goto-char (point-min))
457 ;; Only delete the article if no other group owns it as well.
459 (not (re-search-forward "^X-Gnus-Newsgroup: " nil t
))
460 (search-backward "\n\n" nil t
))
461 (delete-region (point-min) (point-max))))))
463 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server
)
465 (not (nnmbox-server-opened server
)))
466 (nnmbox-open-server server
))
467 (when (or (not nnmbox-mbox-buffer
)
468 (not (buffer-name nnmbox-mbox-buffer
)))
470 (when (not nnmbox-group-alist
)
471 (nnmail-activate 'nnmbox
))
473 (when (assoc newsgroup nnmbox-group-alist
)
474 (setq nnmbox-current-group newsgroup
))
477 (defun nnmbox-article-string (article)
478 (if (numberp article
)
479 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group
":"
480 (int-to-string article
) " ")
481 (concat "\nMessage-ID: " article
)))
483 (defun nnmbox-article-group-number (this-line)
487 (goto-char (point-min)))
488 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
490 (cons (buffer-substring (match-beginning 1) (match-end 1))
492 (buffer-substring (match-beginning 2) (match-end 2)))))))
494 (defun nnmbox-in-header-p (pos)
495 "Return non-nil if POS is in the header of an article."
498 (re-search-backward (concat "^" message-unix-mail-delimiter
) nil t
)
499 (search-forward "\n\n" nil t
)
502 (defun nnmbox-find-article (article)
503 "Leaves point on the relevant X-Gnus-Newsgroup line if found."
504 ;; Check that article is in the active range first, to avoid an
505 ;; expensive exhaustive search if it isn't.
506 (if (and (numberp article
)
507 (not (nnmbox-is-article-active-p article
)))
509 (let ((art-string (nnmbox-article-string article
))
511 ;; There is the possibility that the X-Gnus-Newsgroup line appears
512 ;; in the body of an article (for instance, if an article has been
513 ;; forwarded from someone using Gnus as their mailer), so check
514 ;; that the line is actually part of the article header.
515 (or (and (search-forward art-string nil t
)
516 (nnmbox-in-header-p (point)))
518 (goto-char (point-min))
519 (while (and (not found
)
520 (search-forward art-string nil t
))
521 (setq found
(nnmbox-in-header-p (point))))
524 (defun nnmbox-record-active-article (group-art)
525 (let* ((group (car group-art
))
526 (article (cdr group-art
))
528 (or (assoc group nnmbox-group-active-articles
)
531 nnmbox-group-active-articles
)
532 (car nnmbox-group-active-articles
)))))
533 ;; add article to index, either by building complete list
534 ;; in reverse order, or as a list of ranges.
535 (if (not nnmbox-group-building-active-articles
)
536 (setcdr entry
(gnus-add-to-range (cdr entry
) (list article
)))
537 (when (memq article
(cdr entry
))
538 (switch-to-buffer nnmbox-mbox-buffer
)
539 (error "Article %s:%d already exists!" group article
))
540 (when (and (cadr entry
) (< article
(cadr entry
)))
541 (switch-to-buffer nnmbox-mbox-buffer
)
542 (error "Article %s:%d out of order" group article
))
543 (setcdr entry
(cons article
(cdr entry
))))))
545 (defun nnmbox-record-deleted-article (group-art)
546 (let* ((group (car group-art
))
547 (article (cdr group-art
))
549 (or (assoc group nnmbox-group-active-articles
)
552 nnmbox-group-active-articles
)
553 (car nnmbox-group-active-articles
)))))
554 ;; remove article from index
555 (setcdr entry
(gnus-remove-from-range (cdr entry
) (list article
)))))
557 (defun nnmbox-is-article-active-p (article)
558 (gnus-member-of-range
560 (cdr (assoc nnmbox-current-group
561 nnmbox-group-active-articles
))))
563 (defun nnmbox-save-mail (group-art)
564 "Called narrowed to an article."
565 (let ((delim (concat "^" message-unix-mail-delimiter
)))
566 (goto-char (point-min))
567 ;; This might come from somewhere else.
568 (if (looking-at delim
)
570 (insert "From nobody " (current-time-string) "\n"))
571 ;; Quote all "From " lines in the article.
572 (while (re-search-forward delim nil t
)
573 (goto-char (match-beginning 0))
575 (goto-char (point-max))
578 (nnmail-insert-lines)
579 (nnmail-insert-xref group-art
)
580 (nnmbox-insert-newsgroup-line group-art
)
581 (let ((alist group-art
))
583 (nnmbox-record-active-article (car alist
))
584 (setq alist
(cdr alist
))))
585 (run-hooks 'nnmail-prepare-save-mail-hook
)
586 (run-hooks 'nnmbox-prepare-save-mail-hook
)
589 (defun nnmbox-insert-newsgroup-line (group-art)
591 (goto-char (point-min))
592 (when (search-forward "\n\n" nil t
)
595 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
596 (caar group-art
) (cdar group-art
)
597 (current-time-string)))
598 (setq group-art
(cdr group-art
))))
601 (defun nnmbox-active-number (group)
602 ;; Find the next article number in GROUP.
603 (let ((active (cadr (assoc group nnmbox-group-alist
))))
605 (setcdr active
(1+ (cdr active
)))
606 ;; This group is new, so we create a new entry for it.
607 ;; This might be a bit naughty... creating groups on the drop of
608 ;; a hat, but I don't know...
609 (push (list group
(setq active
(cons 1 1)))
613 (defun nnmbox-create-mbox ()
614 (when (not (file-exists-p nnmbox-mbox-file
))
615 (let ((nnmail-file-coding-system
616 (or nnmbox-file-coding-system-for-write
617 nnmbox-file-coding-system
))
618 (dir (file-name-directory nnmbox-mbox-file
)))
619 (and dir
(gnus-make-directory dir
))
620 (nnmail-write-region (point-min) (point-min)
621 nnmbox-mbox-file t
'nomesg
))))
623 (defun nnmbox-read-mbox ()
624 (nnmail-activate 'nnmbox
)
626 (if (and nnmbox-mbox-buffer
627 (buffer-name nnmbox-mbox-buffer
)
628 (with-current-buffer nnmbox-mbox-buffer
629 (= (buffer-size) (nnheader-file-size nnmbox-mbox-file
))))
632 (let ((delim (concat "^" message-unix-mail-delimiter
))
633 (alist nnmbox-group-alist
)
634 (nnmbox-group-building-active-articles t
)
635 start end end-header number
)
636 (set-buffer (setq nnmbox-mbox-buffer
637 (let ((nnheader-file-coding-system
638 nnmbox-file-coding-system
))
639 (nnheader-find-file-noselect
640 nnmbox-mbox-file t t
))))
641 (mm-enable-multibyte)
642 (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-number (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