1 ;;; nnmbox.el --- mail mbox access for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs; see the file COPYING. If not, write to the
17 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
22 ;; For an overview of what the interface functions do, please see the
27 (eval-when-compile (require 'cl))
28 (eval-when-compile (require 'static))
36 (defvoo nnmbox-mbox-file (expand-file-name "~/mbox")
37 "The name of the mail box file in the user's home directory.")
39 (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
40 "The name of the active file for the mail box.")
42 (defvoo nnmbox-get-new-mail t
43 "If non-nil, nnmbox will check the incoming mail file and split the mail.")
45 (defvoo nnmbox-prepare-save-mail-hook nil
46 "Hook run narrowed to an article before saving.")
50 (defconst nnmbox-version "nnmbox 1.0"
53 (defvoo nnmbox-current-group nil
54 "Current nnmbox news group directory.")
56 (defconst nnmbox-mbox-buffer nil)
58 (defvoo nnmbox-status-string "")
60 (defvoo nnmbox-group-alist nil)
61 (defvoo nnmbox-active-timestamp nil)
63 (defvoo nnmbox-file-coding-system
64 (if (memq system-type '(windows-nt ms-dos ms-windows))
65 'raw-text-dos 'raw-text))
66 (defvoo nnmbox-file-coding-system-for-write nil)
67 (defvoo nnmbox-active-file-coding-system nnmbox-file-coding-system)
68 (defvoo nnmbox-active-file-coding-system-for-write nil)
72 ;;; Interface functions
74 (nnoo-define-basics nnmbox)
76 (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
78 (set-buffer nntp-server-buffer)
80 (let ((number (length sequence))
82 article art-string start stop)
83 (nnmbox-possibly-change-newsgroup newsgroup server)
85 (setq article (car sequence))
86 (setq art-string (nnmbox-article-string article))
87 (set-buffer nnmbox-mbox-buffer)
88 (when (or (search-forward art-string nil t)
89 (progn (goto-char (point-min))
90 (search-forward art-string nil t)))
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)
151 (set-buffer nnmbox-mbox-buffer)
152 (goto-char (point-min))
153 (when (search-forward (nnmbox-article-string article) nil t)
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)))))))
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 (static-if (boundp 'MULE)
193 (defun nnmbox-save-buffer ()
194 (let ((output-coding-system
195 (or nnmbox-file-coding-system-for-write
196 nnmbox-file-coding-system)))
198 (defun nnmbox-save-buffer ()
199 (let ((coding-system-for-write
200 (or nnmbox-file-coding-system-for-write
201 nnmbox-file-coding-system)))
205 (defun nnmbox-save-active (group-alist active-file)
206 (let ((nnmail-active-file-coding-system
207 (or nnmbox-active-file-coding-system-for-write
208 nnmbox-active-file-coding-system)))
209 (nnmail-save-active group-alist active-file)))
211 (deffoo nnmbox-request-scan (&optional group server)
212 (nnmbox-possibly-change-newsgroup group server)
218 (set-buffer nnmbox-mbox-buffer)
219 (nnmbox-save-buffer)))
220 (file-name-directory nnmbox-mbox-file)
224 (let ((in-buf (current-buffer)))
225 (set-buffer nnmbox-mbox-buffer)
226 (goto-char (point-max))
227 (insert-buffer-substring in-buf)))
228 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
230 (deffoo nnmbox-close-group (group &optional server)
233 (deffoo nnmbox-request-create-group (group &optional server args)
234 (nnmail-activate 'nnmbox)
235 (unless (assoc group nnmbox-group-alist)
236 (push (list group (cons 1 0))
238 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
241 (deffoo nnmbox-request-list (&optional server)
243 (let ((nnmail-file-coding-system
244 nnmbox-active-file-coding-system))
245 (nnmail-find-file nnmbox-active-file))
246 (setq nnmbox-group-alist (nnmail-get-active))
249 (deffoo nnmbox-request-newgroups (date &optional server)
250 (nnmbox-request-list server))
252 (deffoo nnmbox-request-list-newsgroups (&optional server)
253 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
255 (deffoo nnmbox-request-expire-articles
256 (articles newsgroup &optional server force)
257 (nnmbox-possibly-change-newsgroup newsgroup server)
260 (nnmail-activate 'nnmbox)
263 (set-buffer nnmbox-mbox-buffer)
264 (while (and articles is-old)
265 (goto-char (point-min))
266 (when (search-forward (nnmbox-article-string (car articles)) nil t)
268 (nnmail-expired-article-p
271 (point) (progn (end-of-line) (point))) force))
273 (nnheader-message 5 "Deleting article %d in %s..."
274 (car articles) newsgroup)
275 (nnmbox-delete-mail))
276 (push (car articles) rest)))
277 (setq articles (cdr articles)))
279 ;; Find the lowest active article in this group.
280 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
281 (goto-char (point-min))
282 (while (and (not (search-forward
283 (nnmbox-article-string (car active)) nil t))
284 (<= (car active) (cdr active)))
285 (setcar active (1+ (car active)))
286 (goto-char (point-min))))
287 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
288 (nconc rest articles))))
290 (deffoo nnmbox-request-move-article
291 (article group server accept-form &optional last)
292 (let ((buf (get-buffer-create " *nnmbox move*"))
295 (nnmbox-request-article article group server)
299 (insert-buffer-substring nntp-server-buffer)
300 (goto-char (point-min))
301 (while (re-search-forward
303 (save-excursion (search-forward "\n\n" nil t) (point)) t)
304 (delete-region (progn (beginning-of-line) (point))
305 (progn (forward-line 1) (point))))
306 (setq result (eval accept-form))
310 (nnmbox-possibly-change-newsgroup group server)
311 (set-buffer nnmbox-mbox-buffer)
312 (goto-char (point-min))
313 (when (search-forward (nnmbox-article-string article) nil t)
314 (nnmbox-delete-mail))
315 (and last (nnmbox-save-buffer))))
318 (deffoo nnmbox-request-accept-article (group &optional server last)
319 (nnmbox-possibly-change-newsgroup group server)
320 (nnmail-check-syntax)
321 (let ((buf (current-buffer))
323 (goto-char (point-min))
324 ;; The From line may have been quoted by movemail.
325 (when (looking-at (concat ">" message-unix-mail-delimiter))
327 (if (looking-at "X-From-Line: ")
328 (replace-match "From ")
329 (insert "From nobody " (current-time-string) "\n"))
331 (nnmail-activate 'nnmbox)
334 (goto-char (point-min))
335 (search-forward "\n\n" nil t)
337 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
338 (delete-region (point) (progn (forward-line 1) (point))))
339 (when nnmail-cache-accepted-message-ids
340 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
341 (setq result (if (stringp group)
342 (list (cons group (nnmbox-active-number group)))
343 (nnmail-article-group 'nnmbox-active-number)))
344 (if (and (null result)
345 (yes-or-no-p "Moved to `junk' group; delete article? "))
347 (setq result (car (nnmbox-save-mail result)))))
349 (set-buffer nnmbox-mbox-buffer)
350 (goto-char (point-max))
351 (insert-buffer-substring buf)
353 (when nnmail-cache-accepted-message-ids
354 (nnmail-cache-close))
355 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
356 (nnmbox-save-buffer))))
359 (deffoo nnmbox-request-replace-article (article group buffer)
360 (nnmbox-possibly-change-newsgroup group)
362 (set-buffer nnmbox-mbox-buffer)
363 (goto-char (point-min))
364 (if (not (search-forward (nnmbox-article-string article) nil t))
366 (nnmbox-delete-mail t t)
367 (insert-buffer-substring buffer)
371 (deffoo nnmbox-request-delete-group (group &optional force server)
372 (nnmbox-possibly-change-newsgroup group server)
373 ;; Delete all articles in GROUP.
375 () ; Don't delete the articles.
377 (set-buffer nnmbox-mbox-buffer)
378 (goto-char (point-min))
379 ;; Delete all articles in this group.
380 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
382 (while (search-forward ident nil t)
384 (nnmbox-delete-mail))
386 (nnmbox-save-buffer)))))
387 ;; Remove the group from all structures.
388 (setq nnmbox-group-alist
389 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
390 nnmbox-current-group nil)
391 ;; Save the active file.
392 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
395 (deffoo nnmbox-request-rename-group (group new-name &optional server)
396 (nnmbox-possibly-change-newsgroup group server)
398 (set-buffer nnmbox-mbox-buffer)
399 (goto-char (point-min))
400 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
401 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
403 (while (search-forward ident nil t)
404 (replace-match new-ident t t)
407 (nnmbox-save-buffer))))
408 (let ((entry (assoc group nnmbox-group-alist)))
410 (setcar entry new-name))
411 (setq nnmbox-current-group nil)
412 ;; Save the new group alist.
413 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
417 ;;; Internal functions.
419 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
420 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
422 (defun nnmbox-delete-mail (&optional force leave-delim)
423 ;; Delete the current X-Gnus-Newsgroup line.
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 groups 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 (set-buffer (setq nnmbox-mbox-buffer
457 (let ((nnheader-file-coding-system
458 nnmbox-file-coding-system))
459 (nnheader-find-file-noselect
460 nnmbox-mbox-file nil t))))
461 (buffer-disable-undo)))
462 (when (not nnmbox-group-alist)
463 (nnmail-activate 'nnmbox))
465 (when (assoc newsgroup nnmbox-group-alist)
466 (setq nnmbox-current-group newsgroup))
469 (defun nnmbox-article-string (article)
470 (if (numberp article)
471 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
472 (int-to-string article) " ")
473 (concat "\nMessage-ID: " article)))
475 (defun nnmbox-article-group-number ()
477 (goto-char (point-min))
478 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
480 (cons (buffer-substring (match-beginning 1) (match-end 1))
482 (buffer-substring (match-beginning 2) (match-end 2)))))))
484 (defun nnmbox-save-mail (group-art)
485 "Called narrowed to an article."
486 (let ((delim (concat "^" message-unix-mail-delimiter)))
487 (goto-char (point-min))
488 ;; This might come from somewhere else.
489 (unless (looking-at delim)
490 (insert "From nobody " (current-time-string) "\n")
491 (goto-char (point-min)))
492 ;; Quote all "From " lines in the article.
494 (while (re-search-forward delim nil t)
497 (nnmail-insert-lines)
498 (nnmail-insert-xref group-art)
499 (nnmbox-insert-newsgroup-line group-art)
500 (run-hooks 'nnmail-prepare-save-mail-hook)
501 (run-hooks 'nnmbox-prepare-save-mail-hook)
504 (defun nnmbox-insert-newsgroup-line (group-art)
506 (goto-char (point-min))
507 (when (search-forward "\n\n" nil t)
510 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
511 (caar group-art) (cdar group-art)
512 (current-time-string)))
513 (setq group-art (cdr group-art))))
516 (defun nnmbox-active-number (group)
517 ;; Find the next article number in GROUP.
518 (let ((active (cadr (assoc group nnmbox-group-alist))))
520 (setcdr active (1+ (cdr active)))
521 ;; This group is new, so we create a new entry for it.
522 ;; This might be a bit naughty... creating groups on the drop of
523 ;; a hat, but I don't know...
524 (push (list group (setq active (cons 1 1)))
528 (defun nnmbox-create-mbox ()
529 (when (not (file-exists-p nnmbox-mbox-file))
530 (let ((nnmail-file-coding-system
531 nnmbox-file-coding-system-for-write))
532 (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg))))
534 (defun nnmbox-read-mbox ()
535 (nnmail-activate 'nnmbox)
537 (if (and nnmbox-mbox-buffer
538 (buffer-name nnmbox-mbox-buffer)
540 (set-buffer nnmbox-mbox-buffer)
541 (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
544 (let ((delim (concat "^" message-unix-mail-delimiter))
545 (alist nnmbox-group-alist)
547 (set-buffer (setq nnmbox-mbox-buffer
548 (let ((nnheader-file-coding-system
549 nnmbox-file-coding-system))
550 (nnheader-find-file-noselect
551 nnmbox-mbox-file nil t))))
552 (buffer-disable-undo)
554 ;; Go through the group alist and compare against
557 (goto-char (point-max))
558 (when (and (re-search-backward
559 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
564 (match-beginning 1) (match-end 1))))
566 (setcdr (cadar alist) (1+ number)))
567 (setq alist (cdr alist)))
569 (goto-char (point-min))
570 (while (re-search-forward delim nil t)
571 (setq start (match-beginning 0))
572 (when (not (search-forward "\nX-Gnus-Newsgroup: "
577 (re-search-forward delim nil t)
583 (narrow-to-region start end)
585 (nnmail-article-group 'nnmbox-active-number)))))
590 ;;; nnmbox.el ends here