1 ;;; nnmbox.el --- mail mbox access for Gnus
2 ;; Copyright (C) 1995,96,97,98 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)
64 ;;; Interface functions
66 (nnoo-define-basics nnmbox)
68 (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
70 (set-buffer nntp-server-buffer)
72 (let ((number (length sequence))
74 article art-string start stop)
75 (nnmbox-possibly-change-newsgroup newsgroup server)
77 (setq article (car sequence))
78 (setq art-string (nnmbox-article-string article))
79 (set-buffer nnmbox-mbox-buffer)
80 (when (or (search-forward art-string nil t)
81 (progn (goto-char (point-min))
82 (search-forward art-string nil t)))
86 (concat "^" message-unix-mail-delimiter) nil t)
88 (search-forward "\n\n" nil t)
89 (setq stop (1- (point)))
90 (set-buffer nntp-server-buffer)
91 (insert (format "221 %d Article retrieved.\n" article))
92 (insert-buffer-substring nnmbox-mbox-buffer start stop)
93 (goto-char (point-max))
95 (setq sequence (cdr sequence))
96 (setq count (1+ count))
97 (and (numberp nnmail-large-newsgroup)
98 (> number nnmail-large-newsgroup)
100 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
101 (/ (* count 100) number))))
103 (and (numberp nnmail-large-newsgroup)
104 (> number nnmail-large-newsgroup)
105 (nnheader-message 5 "nnmbox: Receiving headers...done"))
107 (set-buffer nntp-server-buffer)
108 (nnheader-fold-continuation-lines)
111 (deffoo nnmbox-open-server (server &optional defs)
112 (nnoo-change-server 'nnmbox server defs)
115 ((not (file-exists-p nnmbox-mbox-file))
116 (nnmbox-close-server)
117 (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
118 ((file-directory-p nnmbox-mbox-file)
119 (nnmbox-close-server)
120 (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
122 (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
126 (deffoo nnmbox-close-server (&optional server)
127 (when (and nnmbox-mbox-buffer
128 (buffer-name nnmbox-mbox-buffer))
129 (kill-buffer nnmbox-mbox-buffer))
130 (nnoo-close-server 'nnmbox server)
133 (deffoo nnmbox-server-opened (&optional server)
134 (and (nnoo-current-server-p 'nnmbox server)
136 (buffer-name nnmbox-mbox-buffer)
138 (buffer-name nntp-server-buffer)))
140 (deffoo nnmbox-request-article (article &optional newsgroup server buffer)
141 (nnmbox-possibly-change-newsgroup newsgroup server)
143 (set-buffer nnmbox-mbox-buffer)
144 (goto-char (point-min))
145 (when (search-forward (nnmbox-article-string article) nil t)
147 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
150 (or (and (re-search-forward
151 (concat "^" message-unix-mail-delimiter) nil t)
153 (goto-char (point-max)))
155 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
156 (set-buffer nntp-server-buffer)
158 (insert-buffer-substring nnmbox-mbox-buffer start stop)
159 (goto-char (point-min))
160 (while (looking-at "From ")
162 (insert "X-From-Line: ")
164 (if (numberp article)
165 (cons nnmbox-current-group article)
166 (nnmbox-article-group-number)))))))
168 (deffoo nnmbox-request-group (group &optional server dont-check)
169 (nnmbox-possibly-change-newsgroup nil server)
170 (let ((active (cadr (assoc group nnmbox-group-alist))))
173 (null (nnmbox-possibly-change-newsgroup group server)))
174 (nnheader-report 'nnmbox "No such group: %s" group))
176 (nnheader-report 'nnmbox "Selected group %s" group)
177 (nnheader-insert ""))
179 (nnheader-report 'nnmbox "Selected group %s" group)
180 (nnheader-insert "211 %d %d %d %s\n"
181 (1+ (- (cdr active) (car active)))
182 (car active) (cdr active) group)))))
184 (deffoo nnmbox-request-scan (&optional group server)
185 (nnmbox-possibly-change-newsgroup group server)
191 (set-buffer nnmbox-mbox-buffer)
193 (file-name-directory nnmbox-mbox-file)
197 (let ((in-buf (current-buffer)))
198 (set-buffer nnmbox-mbox-buffer)
199 (goto-char (point-max))
200 (insert-buffer-substring in-buf)))
201 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))))
203 (deffoo nnmbox-close-group (group &optional server)
206 (deffoo nnmbox-request-create-group (group &optional server args)
207 (nnmail-activate 'nnmbox)
208 (unless (assoc group nnmbox-group-alist)
209 (push (list group (cons 1 0))
211 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
214 (deffoo nnmbox-request-list (&optional server)
216 (nnmail-find-file nnmbox-active-file)
217 (setq nnmbox-group-alist (nnmail-get-active))
220 (deffoo nnmbox-request-newgroups (date &optional server)
221 (nnmbox-request-list server))
223 (deffoo nnmbox-request-list-newsgroups (&optional server)
224 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
226 (deffoo nnmbox-request-expire-articles
227 (articles newsgroup &optional server force)
228 (nnmbox-possibly-change-newsgroup newsgroup server)
231 (nnmail-activate 'nnmbox)
234 (set-buffer nnmbox-mbox-buffer)
235 (while (and articles is-old)
236 (goto-char (point-min))
237 (when (search-forward (nnmbox-article-string (car articles)) nil t)
239 (nnmail-expired-article-p
242 (point) (progn (end-of-line) (point))) force))
244 (nnheader-message 5 "Deleting article %d in %s..."
245 (car articles) newsgroup)
246 (nnmbox-delete-mail))
247 (push (car articles) rest)))
248 (setq articles (cdr articles)))
250 ;; Find the lowest active article in this group.
251 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
252 (goto-char (point-min))
253 (while (and (not (search-forward
254 (nnmbox-article-string (car active)) nil t))
255 (<= (car active) (cdr active)))
256 (setcar active (1+ (car active)))
257 (goto-char (point-min))))
258 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
259 (nconc rest articles))))
261 (deffoo nnmbox-request-move-article
262 (article group server accept-form &optional last)
263 (let ((buf (get-buffer-create " *nnmbox move*"))
266 (nnmbox-request-article article group server)
270 (insert-buffer-substring nntp-server-buffer)
271 (goto-char (point-min))
272 (while (re-search-forward
274 (save-excursion (search-forward "\n\n" nil t) (point)) t)
275 (delete-region (progn (beginning-of-line) (point))
276 (progn (forward-line 1) (point))))
277 (setq result (eval accept-form))
281 (nnmbox-possibly-change-newsgroup group server)
282 (set-buffer nnmbox-mbox-buffer)
283 (goto-char (point-min))
284 (when (search-forward (nnmbox-article-string article) nil t)
285 (nnmbox-delete-mail))
286 (and last (save-buffer))))
289 (deffoo nnmbox-request-accept-article (group &optional server last)
290 (nnmbox-possibly-change-newsgroup group server)
291 (nnmail-check-syntax)
292 (let ((buf (current-buffer))
294 (goto-char (point-min))
295 ;; The From line may have been quoted by movemail.
296 (when (looking-at (concat ">" message-unix-mail-delimiter))
298 (if (looking-at "X-From-Line: ")
299 (replace-match "From ")
300 (insert "From nobody " (current-time-string) "\n"))
302 (nnmail-activate 'nnmbox)
305 (goto-char (point-min))
306 (search-forward "\n\n" nil t)
308 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
309 (delete-region (point) (progn (forward-line 1) (point))))
310 (when nnmail-cache-accepted-message-ids
311 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
312 (setq result (if (stringp group)
313 (list (cons group (nnmbox-active-number group)))
314 (nnmail-article-group 'nnmbox-active-number)))
315 (if (and (null result)
316 (yes-or-no-p "Moved to `junk' group; delete article? "))
318 (setq result (car (nnmbox-save-mail result)))))
320 (set-buffer nnmbox-mbox-buffer)
321 (goto-char (point-max))
322 (insert-buffer-substring buf)
324 (when nnmail-cache-accepted-message-ids
325 (nnmail-cache-close))
326 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
330 (deffoo nnmbox-request-replace-article (article group buffer)
331 (nnmbox-possibly-change-newsgroup group)
333 (set-buffer nnmbox-mbox-buffer)
334 (goto-char (point-min))
335 (if (not (search-forward (nnmbox-article-string article) nil t))
337 (nnmbox-delete-mail t t)
338 (insert-buffer-substring buffer)
342 (deffoo nnmbox-request-delete-group (group &optional force server)
343 (nnmbox-possibly-change-newsgroup group server)
344 ;; Delete all articles in GROUP.
346 () ; Don't delete the articles.
348 (set-buffer nnmbox-mbox-buffer)
349 (goto-char (point-min))
350 ;; Delete all articles in this group.
351 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
353 (while (search-forward ident nil t)
355 (nnmbox-delete-mail))
358 ;; Remove the group from all structures.
359 (setq nnmbox-group-alist
360 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
361 nnmbox-current-group nil)
362 ;; Save the active file.
363 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
366 (deffoo nnmbox-request-rename-group (group new-name &optional server)
367 (nnmbox-possibly-change-newsgroup group server)
369 (set-buffer nnmbox-mbox-buffer)
370 (goto-char (point-min))
371 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
372 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
374 (while (search-forward ident nil t)
375 (replace-match new-ident t t)
379 (let ((entry (assoc group nnmbox-group-alist)))
381 (setcar entry new-name))
382 (setq nnmbox-current-group nil)
383 ;; Save the new group alist.
384 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
388 ;;; Internal functions.
390 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
391 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
393 (defun nnmbox-delete-mail (&optional force leave-delim)
394 ;; Delete the current X-Gnus-Newsgroup line.
397 (progn (beginning-of-line) (point))
398 (progn (forward-line 1) (point))))
399 ;; Beginning of the article.
404 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
405 (if leave-delim (progn (forward-line 1) (point))
406 (match-beginning 0)))
409 (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
411 (if (and (not (bobp)) leave-delim)
412 (progn (forward-line -2) (point))
413 (match-beginning 0)))
415 (goto-char (point-min))
416 ;; Only delete the article if no other groups owns it as well.
417 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
418 (delete-region (point-min) (point-max))))))
420 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
422 (not (nnmbox-server-opened server)))
423 (nnmbox-open-server server))
424 (when (or (not nnmbox-mbox-buffer)
425 (not (buffer-name nnmbox-mbox-buffer)))
427 (set-buffer (setq nnmbox-mbox-buffer
428 (nnheader-find-file-noselect
429 nnmbox-mbox-file nil 'raw)))
430 (mm-enable-multibyte)
431 (buffer-disable-undo)))
432 (when (not nnmbox-group-alist)
433 (nnmail-activate 'nnmbox))
435 (when (assoc newsgroup nnmbox-group-alist)
436 (setq nnmbox-current-group newsgroup))
439 (defun nnmbox-article-string (article)
440 (if (numberp article)
441 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
442 (int-to-string article) " ")
443 (concat "\nMessage-ID: " article)))
445 (defun nnmbox-article-group-number ()
447 (goto-char (point-min))
448 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
450 (cons (buffer-substring (match-beginning 1) (match-end 1))
452 (buffer-substring (match-beginning 2) (match-end 2)))))))
454 (defun nnmbox-save-mail (group-art)
455 "Called narrowed to an article."
456 (let ((delim (concat "^" message-unix-mail-delimiter)))
457 (goto-char (point-min))
458 ;; This might come from somewhere else.
459 (unless (looking-at delim)
460 (insert "From nobody " (current-time-string) "\n")
461 (goto-char (point-min)))
462 ;; Quote all "From " lines in the article.
464 (while (re-search-forward delim nil t)
467 (nnmail-insert-lines)
468 (nnmail-insert-xref group-art)
469 (nnmbox-insert-newsgroup-line group-art)
470 (run-hooks 'nnmail-prepare-save-mail-hook)
471 (run-hooks 'nnmbox-prepare-save-mail-hook)
474 (defun nnmbox-insert-newsgroup-line (group-art)
476 (goto-char (point-min))
477 (when (search-forward "\n\n" nil t)
480 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
481 (caar group-art) (cdar group-art)
482 (current-time-string)))
483 (setq group-art (cdr group-art))))
486 (defun nnmbox-active-number (group)
487 ;; Find the next article number in GROUP.
488 (let ((active (cadr (assoc group nnmbox-group-alist))))
490 (setcdr active (1+ (cdr active)))
491 ;; This group is new, so we create a new entry for it.
492 ;; This might be a bit naughty... creating groups on the drop of
493 ;; a hat, but I don't know...
494 (push (list group (setq active (cons 1 1)))
498 (defun nnmbox-create-mbox ()
499 (when (not (file-exists-p nnmbox-mbox-file))
500 (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg)))
502 (defun nnmbox-read-mbox ()
503 (nnmail-activate 'nnmbox)
505 (if (and nnmbox-mbox-buffer
506 (buffer-name nnmbox-mbox-buffer)
508 (set-buffer nnmbox-mbox-buffer)
509 (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
512 (let ((delim (concat "^" message-unix-mail-delimiter))
513 (alist nnmbox-group-alist)
515 (set-buffer (setq nnmbox-mbox-buffer
516 (nnheader-find-file-noselect
517 nnmbox-mbox-file nil 'raw)))
518 (mm-enable-multibyte)
519 (buffer-disable-undo)
521 ;; Go through the group alist and compare against
524 (goto-char (point-max))
525 (when (and (re-search-backward
526 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
531 (match-beginning 1) (match-end 1))))
533 (setcdr (cadar alist) (1+ number)))
534 (setq alist (cdr alist)))
536 (goto-char (point-min))
537 (while (re-search-forward delim nil t)
538 (setq start (match-beginning 0))
539 (when (not (search-forward "\nX-Gnus-Newsgroup: "
544 (re-search-forward delim nil t)
550 (narrow-to-region start end)
552 (nnmail-article-group 'nnmbox-active-number)))))
557 ;;; nnmbox.el ends here