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
31 (eval-when-compile (require 'cl))
35 (defvoo nnmbox-mbox-file (expand-file-name "~/mbox")
36 "The name of the mail box file in the user's home directory.")
38 (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
39 "The name of the active file for the mail box.")
41 (defvoo nnmbox-get-new-mail t
42 "If non-nil, nnmbox will check the incoming mail file and split the mail.")
44 (defvoo nnmbox-prepare-save-mail-hook nil
45 "Hook run narrowed to an article before saving.")
49 (defconst nnmbox-version "nnmbox 1.0"
52 (defvoo nnmbox-current-group nil
53 "Current nnmbox news group directory.")
55 (defconst nnmbox-mbox-buffer nil)
57 (defvoo nnmbox-status-string "")
59 (defvoo nnmbox-group-alist nil)
60 (defvoo nnmbox-active-timestamp nil)
62 (defvoo nnmbox-file-coding-system mm-text-coding-system)
63 (defvoo nnmbox-file-coding-system-for-write nil)
64 (defvoo nnmbox-active-file-coding-system mm-text-coding-system)
65 (defvoo nnmbox-active-file-coding-system-for-write nil)
69 ;;; Interface functions
71 (nnoo-define-basics nnmbox)
73 (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
75 (set-buffer nntp-server-buffer)
77 (let ((number (length sequence))
79 article art-string start stop)
80 (nnmbox-possibly-change-newsgroup newsgroup server)
82 (setq article (car sequence))
83 (setq art-string (nnmbox-article-string article))
84 (set-buffer nnmbox-mbox-buffer)
85 (when (or (search-forward art-string nil t)
86 (progn (goto-char (point-min))
87 (search-forward art-string nil t)))
91 (concat "^" message-unix-mail-delimiter) nil t)
93 (search-forward "\n\n" nil t)
94 (setq stop (1- (point)))
95 (set-buffer nntp-server-buffer)
96 (insert (format "221 %d Article retrieved.\n" article))
97 (insert-buffer-substring nnmbox-mbox-buffer start stop)
98 (goto-char (point-max))
100 (setq sequence (cdr sequence))
101 (setq count (1+ count))
102 (and (numberp nnmail-large-newsgroup)
103 (> number nnmail-large-newsgroup)
105 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
106 (/ (* count 100) number))))
108 (and (numberp nnmail-large-newsgroup)
109 (> number nnmail-large-newsgroup)
110 (nnheader-message 5 "nnmbox: Receiving headers...done"))
112 (set-buffer nntp-server-buffer)
113 (nnheader-fold-continuation-lines)
116 (deffoo nnmbox-open-server (server &optional defs)
117 (nnoo-change-server 'nnmbox server defs)
120 ((not (file-exists-p nnmbox-mbox-file))
121 (nnmbox-close-server)
122 (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
123 ((file-directory-p nnmbox-mbox-file)
124 (nnmbox-close-server)
125 (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
127 (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
131 (deffoo nnmbox-close-server (&optional server)
132 (when (and nnmbox-mbox-buffer
133 (buffer-name nnmbox-mbox-buffer))
134 (kill-buffer nnmbox-mbox-buffer))
135 (nnoo-close-server 'nnmbox server)
138 (deffoo nnmbox-server-opened (&optional server)
139 (and (nnoo-current-server-p 'nnmbox server)
141 (buffer-name nnmbox-mbox-buffer)
143 (buffer-name nntp-server-buffer)))
145 (deffoo nnmbox-request-article (article &optional newsgroup server buffer)
146 (nnmbox-possibly-change-newsgroup newsgroup server)
148 (set-buffer nnmbox-mbox-buffer)
149 (goto-char (point-min))
150 (when (search-forward (nnmbox-article-string article) nil t)
152 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
155 (or (and (re-search-forward
156 (concat "^" message-unix-mail-delimiter) nil t)
158 (goto-char (point-max)))
160 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
161 (set-buffer nntp-server-buffer)
163 (insert-buffer-substring nnmbox-mbox-buffer start stop)
164 (goto-char (point-min))
165 (while (looking-at "From ")
167 (insert "X-From-Line: ")
169 (if (numberp article)
170 (cons nnmbox-current-group article)
171 (nnmbox-article-group-number)))))))
173 (deffoo nnmbox-request-group (group &optional server dont-check)
174 (nnmbox-possibly-change-newsgroup nil server)
175 (let ((active (cadr (assoc group nnmbox-group-alist))))
178 (null (nnmbox-possibly-change-newsgroup group server)))
179 (nnheader-report 'nnmbox "No such group: %s" group))
181 (nnheader-report 'nnmbox "Selected group %s" group)
182 (nnheader-insert ""))
184 (nnheader-report 'nnmbox "Selected group %s" group)
185 (nnheader-insert "211 %d %d %d %s\n"
186 (1+ (- (cdr active) (car active)))
187 (car active) (cdr active) group)))))
189 (defun nnmbox-save-buffer ()
190 (let ((coding-system-for-write
191 (or nnmbox-file-coding-system-for-write
192 nnmbox-file-coding-system)))
195 (defun nnmbox-save-active (group-alist active-file)
196 (let ((nnmail-active-file-coding-system
197 (or nnmbox-active-file-coding-system-for-write
198 nnmbox-active-file-coding-system)))
199 (nnmail-save-active group-alist active-file)))
201 (deffoo nnmbox-request-scan (&optional group server)
202 (nnmbox-possibly-change-newsgroup group server)
208 (set-buffer nnmbox-mbox-buffer)
209 (nnmbox-save-buffer)))
210 (file-name-directory nnmbox-mbox-file)
214 (let ((in-buf (current-buffer)))
215 (set-buffer nnmbox-mbox-buffer)
216 (goto-char (point-max))
217 (insert-buffer-substring in-buf)))
218 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
220 (deffoo nnmbox-close-group (group &optional server)
223 (deffoo nnmbox-request-create-group (group &optional server args)
224 (nnmail-activate 'nnmbox)
225 (unless (assoc group nnmbox-group-alist)
226 (push (list group (cons 1 0))
228 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
231 (deffoo nnmbox-request-list (&optional server)
233 (let ((nnmail-file-coding-system
234 nnmbox-active-file-coding-system))
235 (nnmail-find-file nnmbox-active-file))
236 (setq nnmbox-group-alist (nnmail-get-active))
239 (deffoo nnmbox-request-newgroups (date &optional server)
240 (nnmbox-request-list server))
242 (deffoo nnmbox-request-list-newsgroups (&optional server)
243 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
245 (deffoo nnmbox-request-expire-articles
246 (articles newsgroup &optional server force)
247 (nnmbox-possibly-change-newsgroup newsgroup server)
250 (nnmail-activate 'nnmbox)
253 (set-buffer nnmbox-mbox-buffer)
254 (while (and articles is-old)
255 (goto-char (point-min))
256 (when (search-forward (nnmbox-article-string (car articles)) nil t)
258 (nnmail-expired-article-p
261 (point) (progn (end-of-line) (point))) force))
263 (nnheader-message 5 "Deleting article %d in %s..."
264 (car articles) newsgroup)
265 (nnmbox-delete-mail))
266 (push (car articles) rest)))
267 (setq articles (cdr articles)))
269 ;; Find the lowest active article in this group.
270 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
271 (goto-char (point-min))
272 (while (and (not (search-forward
273 (nnmbox-article-string (car active)) nil t))
274 (<= (car active) (cdr active)))
275 (setcar active (1+ (car active)))
276 (goto-char (point-min))))
277 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
278 (nconc rest articles))))
280 (deffoo nnmbox-request-move-article
281 (article group server accept-form &optional last)
282 (let ((buf (get-buffer-create " *nnmbox move*"))
285 (nnmbox-request-article article group server)
289 (insert-buffer-substring nntp-server-buffer)
290 (goto-char (point-min))
291 (while (re-search-forward
293 (save-excursion (search-forward "\n\n" nil t) (point)) t)
294 (delete-region (progn (beginning-of-line) (point))
295 (progn (forward-line 1) (point))))
296 (setq result (eval accept-form))
300 (nnmbox-possibly-change-newsgroup group server)
301 (set-buffer nnmbox-mbox-buffer)
302 (goto-char (point-min))
303 (when (search-forward (nnmbox-article-string article) nil t)
304 (nnmbox-delete-mail))
305 (and last (nnmbox-save-buffer))))
308 (deffoo nnmbox-request-accept-article (group &optional server last)
309 (nnmbox-possibly-change-newsgroup group server)
310 (nnmail-check-syntax)
311 (let ((buf (current-buffer))
313 (goto-char (point-min))
314 ;; The From line may have been quoted by movemail.
315 (when (looking-at (concat ">" message-unix-mail-delimiter))
317 (if (looking-at "X-From-Line: ")
318 (replace-match "From ")
319 (insert "From nobody " (current-time-string) "\n"))
321 (nnmail-activate 'nnmbox)
324 (goto-char (point-min))
325 (search-forward "\n\n" nil t)
327 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
328 (delete-region (point) (progn (forward-line 1) (point))))
329 (when nnmail-cache-accepted-message-ids
330 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
331 (setq result (if (stringp group)
332 (list (cons group (nnmbox-active-number group)))
333 (nnmail-article-group 'nnmbox-active-number)))
334 (if (and (null result)
335 (yes-or-no-p "Moved to `junk' group; delete article? "))
337 (setq result (car (nnmbox-save-mail result)))))
339 (set-buffer nnmbox-mbox-buffer)
340 (goto-char (point-max))
341 (insert-buffer-substring buf)
343 (when nnmail-cache-accepted-message-ids
344 (nnmail-cache-close))
345 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
346 (nnmbox-save-buffer))))
349 (deffoo nnmbox-request-replace-article (article group buffer)
350 (nnmbox-possibly-change-newsgroup group)
352 (set-buffer nnmbox-mbox-buffer)
353 (goto-char (point-min))
354 (if (not (search-forward (nnmbox-article-string article) nil t))
356 (nnmbox-delete-mail t t)
357 (insert-buffer-substring buffer)
361 (deffoo nnmbox-request-delete-group (group &optional force server)
362 (nnmbox-possibly-change-newsgroup group server)
363 ;; Delete all articles in GROUP.
365 () ; Don't delete the articles.
367 (set-buffer nnmbox-mbox-buffer)
368 (goto-char (point-min))
369 ;; Delete all articles in this group.
370 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
372 (while (search-forward ident nil t)
374 (nnmbox-delete-mail))
376 (nnmbox-save-buffer)))))
377 ;; Remove the group from all structures.
378 (setq nnmbox-group-alist
379 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
380 nnmbox-current-group nil)
381 ;; Save the active file.
382 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
385 (deffoo nnmbox-request-rename-group (group new-name &optional server)
386 (nnmbox-possibly-change-newsgroup group server)
388 (set-buffer nnmbox-mbox-buffer)
389 (goto-char (point-min))
390 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
391 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
393 (while (search-forward ident nil t)
394 (replace-match new-ident t t)
397 (nnmbox-save-buffer))))
398 (let ((entry (assoc group nnmbox-group-alist)))
400 (setcar entry new-name))
401 (setq nnmbox-current-group nil)
402 ;; Save the new group alist.
403 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
407 ;;; Internal functions.
409 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
410 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
412 (defun nnmbox-delete-mail (&optional force leave-delim)
413 ;; Delete the current X-Gnus-Newsgroup line.
416 (progn (beginning-of-line) (point))
417 (progn (forward-line 1) (point))))
418 ;; Beginning of the article.
423 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
424 (if leave-delim (progn (forward-line 1) (point))
425 (match-beginning 0)))
428 (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
430 (if (and (not (bobp)) leave-delim)
431 (progn (forward-line -2) (point))
432 (match-beginning 0)))
434 (goto-char (point-min))
435 ;; Only delete the article if no other groups owns it as well.
436 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
437 (delete-region (point-min) (point-max))))))
439 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
441 (not (nnmbox-server-opened server)))
442 (nnmbox-open-server server))
443 (when (or (not nnmbox-mbox-buffer)
444 (not (buffer-name nnmbox-mbox-buffer)))
446 (set-buffer (setq nnmbox-mbox-buffer
447 (let ((nnheader-file-coding-system
448 nnmbox-file-coding-system))
449 (nnheader-find-file-noselect
450 nnmbox-mbox-file nil t))))
451 (mm-enable-multibyte)
452 (buffer-disable-undo)))
453 (when (not nnmbox-group-alist)
454 (nnmail-activate 'nnmbox))
456 (when (assoc newsgroup nnmbox-group-alist)
457 (setq nnmbox-current-group newsgroup))
460 (defun nnmbox-article-string (article)
461 (if (numberp article)
462 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
463 (int-to-string article) " ")
464 (concat "\nMessage-ID: " article)))
466 (defun nnmbox-article-group-number ()
468 (goto-char (point-min))
469 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
471 (cons (buffer-substring (match-beginning 1) (match-end 1))
473 (buffer-substring (match-beginning 2) (match-end 2)))))))
475 (defun nnmbox-save-mail (group-art)
476 "Called narrowed to an article."
477 (let ((delim (concat "^" message-unix-mail-delimiter)))
478 (goto-char (point-min))
479 ;; This might come from somewhere else.
480 (unless (looking-at delim)
481 (insert "From nobody " (current-time-string) "\n")
482 (goto-char (point-min)))
483 ;; Quote all "From " lines in the article.
485 (while (re-search-forward delim nil t)
488 (nnmail-insert-lines)
489 (nnmail-insert-xref group-art)
490 (nnmbox-insert-newsgroup-line group-art)
491 (run-hooks 'nnmail-prepare-save-mail-hook)
492 (run-hooks 'nnmbox-prepare-save-mail-hook)
495 (defun nnmbox-insert-newsgroup-line (group-art)
497 (goto-char (point-min))
498 (when (search-forward "\n\n" nil t)
501 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
502 (caar group-art) (cdar group-art)
503 (current-time-string)))
504 (setq group-art (cdr group-art))))
507 (defun nnmbox-active-number (group)
508 ;; Find the next article number in GROUP.
509 (let ((active (cadr (assoc group nnmbox-group-alist))))
511 (setcdr active (1+ (cdr active)))
512 ;; This group is new, so we create a new entry for it.
513 ;; This might be a bit naughty... creating groups on the drop of
514 ;; a hat, but I don't know...
515 (push (list group (setq active (cons 1 1)))
519 (defun nnmbox-create-mbox ()
520 (when (not (file-exists-p nnmbox-mbox-file))
521 (let ((nnmail-file-coding-system
522 nnmbox-file-coding-system-for-write))
523 (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg))))
525 (defun nnmbox-read-mbox ()
526 (nnmail-activate 'nnmbox)
528 (if (and nnmbox-mbox-buffer
529 (buffer-name nnmbox-mbox-buffer)
531 (set-buffer nnmbox-mbox-buffer)
532 (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
535 (let ((delim (concat "^" message-unix-mail-delimiter))
536 (alist nnmbox-group-alist)
538 (set-buffer (setq nnmbox-mbox-buffer
539 (let ((nnheader-file-coding-system
540 nnmbox-file-coding-system))
541 (nnheader-find-file-noselect
542 nnmbox-mbox-file nil t))))
543 (mm-enable-multibyte)
544 (buffer-disable-undo)
546 ;; Go through the group alist and compare against
549 (goto-char (point-max))
550 (when (and (re-search-backward
551 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
556 (match-beginning 1) (match-end 1))))
558 (setcdr (cadar alist) (1+ number)))
559 (setq alist (cdr alist)))
561 (goto-char (point-min))
562 (while (re-search-forward delim nil t)
563 (setq start (match-beginning 0))
564 (when (not (search-forward "\nX-Gnus-Newsgroup: "
569 (re-search-forward delim nil t)
575 (narrow-to-region start end)
577 (nnmail-article-group 'nnmbox-active-number)))))
582 ;;; nnmbox.el ends here