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 (let ((active (cadr (assoc group nnmbox-group-alist))))
172 (null (nnmbox-possibly-change-newsgroup group server)))
173 (nnheader-report 'nnmbox "No such group: %s" group))
175 (nnheader-report 'nnmbox "Selected group %s" group)
176 (nnheader-insert ""))
178 (nnheader-report 'nnmbox "Selected group %s" group)
179 (nnheader-insert "211 %d %d %d %s\n"
180 (1+ (- (cdr active) (car active)))
181 (car active) (cdr active) group)))))
183 (deffoo nnmbox-request-scan (&optional group server)
184 (nnmbox-possibly-change-newsgroup group server)
190 (set-buffer nnmbox-mbox-buffer)
192 (file-name-directory nnmbox-mbox-file)
196 (let ((in-buf (current-buffer)))
197 (set-buffer nnmbox-mbox-buffer)
198 (goto-char (point-max))
199 (insert-buffer-substring in-buf)))
200 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))))
202 (deffoo nnmbox-close-group (group &optional server)
205 (deffoo nnmbox-request-create-group (group &optional server args)
206 (nnmail-activate 'nnmbox)
207 (unless (assoc group nnmbox-group-alist)
208 (push (list group (cons 1 0))
210 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
213 (deffoo nnmbox-request-list (&optional server)
215 (nnmail-find-file nnmbox-active-file)
216 (setq nnmbox-group-alist (nnmail-get-active))
219 (deffoo nnmbox-request-newgroups (date &optional server)
220 (nnmbox-request-list server))
222 (deffoo nnmbox-request-list-newsgroups (&optional server)
223 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
225 (deffoo nnmbox-request-expire-articles
226 (articles newsgroup &optional server force)
227 (nnmbox-possibly-change-newsgroup newsgroup server)
230 (nnmail-activate 'nnmbox)
233 (set-buffer nnmbox-mbox-buffer)
234 (while (and articles is-old)
235 (goto-char (point-min))
236 (when (search-forward (nnmbox-article-string (car articles)) nil t)
238 (nnmail-expired-article-p
241 (point) (progn (end-of-line) (point))) force))
243 (nnheader-message 5 "Deleting article %d in %s..."
244 (car articles) newsgroup)
245 (nnmbox-delete-mail))
246 (push (car articles) rest)))
247 (setq articles (cdr articles)))
249 ;; Find the lowest active article in this group.
250 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
251 (goto-char (point-min))
252 (while (and (not (search-forward
253 (nnmbox-article-string (car active)) nil t))
254 (<= (car active) (cdr active)))
255 (setcar active (1+ (car active)))
256 (goto-char (point-min))))
257 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
258 (nconc rest articles))))
260 (deffoo nnmbox-request-move-article
261 (article group server accept-form &optional last)
262 (let ((buf (get-buffer-create " *nnmbox move*"))
265 (nnmbox-request-article article group server)
269 (insert-buffer-substring nntp-server-buffer)
270 (goto-char (point-min))
271 (while (re-search-forward
273 (save-excursion (search-forward "\n\n" nil t) (point)) t)
274 (delete-region (progn (beginning-of-line) (point))
275 (progn (forward-line 1) (point))))
276 (setq result (eval accept-form))
280 (nnmbox-possibly-change-newsgroup group server)
281 (set-buffer nnmbox-mbox-buffer)
282 (goto-char (point-min))
283 (when (search-forward (nnmbox-article-string article) nil t)
284 (nnmbox-delete-mail))
285 (and last (save-buffer))))
288 (deffoo nnmbox-request-accept-article (group &optional server last)
289 (nnmbox-possibly-change-newsgroup group server)
290 (nnmail-check-syntax)
291 (let ((buf (current-buffer))
293 (goto-char (point-min))
294 ;; The From line may have been quoted by movemail.
295 (when (looking-at (concat ">" message-unix-mail-delimiter))
297 (if (looking-at "X-From-Line: ")
298 (replace-match "From ")
299 (insert "From nobody " (current-time-string) "\n"))
301 (nnmail-activate 'nnmbox)
304 (goto-char (point-min))
305 (search-forward "\n\n" nil t)
307 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
308 (delete-region (point) (progn (forward-line 1) (point))))
309 (when nnmail-cache-accepted-message-ids
310 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
311 (setq result (if (stringp group)
312 (list (cons group (nnmbox-active-number group)))
313 (nnmail-article-group 'nnmbox-active-number)))
314 (if (and (null result)
315 (yes-or-no-p "Moved to `junk' group; delete article? "))
317 (setq result (car (nnmbox-save-mail result)))))
319 (set-buffer nnmbox-mbox-buffer)
320 (goto-char (point-max))
321 (insert-buffer-substring buf)
323 (when nnmail-cache-accepted-message-ids
324 (nnmail-cache-close))
325 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
329 (deffoo nnmbox-request-replace-article (article group buffer)
330 (nnmbox-possibly-change-newsgroup group)
332 (set-buffer nnmbox-mbox-buffer)
333 (goto-char (point-min))
334 (if (not (search-forward (nnmbox-article-string article) nil t))
336 (nnmbox-delete-mail t t)
337 (insert-buffer-substring buffer)
341 (deffoo nnmbox-request-delete-group (group &optional force server)
342 (nnmbox-possibly-change-newsgroup group server)
343 ;; Delete all articles in GROUP.
345 () ; Don't delete the articles.
347 (set-buffer nnmbox-mbox-buffer)
348 (goto-char (point-min))
349 ;; Delete all articles in this group.
350 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
352 (while (search-forward ident nil t)
354 (nnmbox-delete-mail))
357 ;; Remove the group from all structures.
358 (setq nnmbox-group-alist
359 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
360 nnmbox-current-group nil)
361 ;; Save the active file.
362 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
365 (deffoo nnmbox-request-rename-group (group new-name &optional server)
366 (nnmbox-possibly-change-newsgroup group server)
368 (set-buffer nnmbox-mbox-buffer)
369 (goto-char (point-min))
370 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
371 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
373 (while (search-forward ident nil t)
374 (replace-match new-ident t t)
378 (let ((entry (assoc group nnmbox-group-alist)))
380 (setcar entry new-name))
381 (setq nnmbox-current-group nil)
382 ;; Save the new group alist.
383 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
387 ;;; Internal functions.
389 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
390 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
392 (defun nnmbox-delete-mail (&optional force leave-delim)
393 ;; Delete the current X-Gnus-Newsgroup line.
396 (progn (beginning-of-line) (point))
397 (progn (forward-line 1) (point))))
398 ;; Beginning of the article.
403 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
404 (if leave-delim (progn (forward-line 1) (point))
405 (match-beginning 0)))
408 (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
410 (if (and (not (bobp)) leave-delim)
411 (progn (forward-line -2) (point))
412 (match-beginning 0)))
414 (goto-char (point-min))
415 ;; Only delete the article if no other groups owns it as well.
416 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
417 (delete-region (point-min) (point-max))))))
419 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
421 (not (nnmbox-server-opened server)))
422 (nnmbox-open-server server))
423 (when (or (not nnmbox-mbox-buffer)
424 (not (buffer-name nnmbox-mbox-buffer)))
426 (set-buffer (setq nnmbox-mbox-buffer
427 (nnheader-find-file-noselect
428 nnmbox-mbox-file nil 'raw)))
429 (buffer-disable-undo (current-buffer))))
430 (when (not nnmbox-group-alist)
431 (nnmail-activate 'nnmbox))
433 (when (assoc newsgroup nnmbox-group-alist)
434 (setq nnmbox-current-group newsgroup))
437 (defun nnmbox-article-string (article)
438 (if (numberp article)
439 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
440 (int-to-string article) " ")
441 (concat "\nMessage-ID: " article)))
443 (defun nnmbox-article-group-number ()
445 (goto-char (point-min))
446 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
448 (cons (buffer-substring (match-beginning 1) (match-end 1))
450 (buffer-substring (match-beginning 2) (match-end 2)))))))
452 (defun nnmbox-save-mail (group-art)
453 "Called narrowed to an article."
454 (let ((delim (concat "^" message-unix-mail-delimiter)))
455 (goto-char (point-min))
456 ;; This might come from somewhere else.
457 (unless (looking-at delim)
458 (insert "From nobody " (current-time-string) "\n")
459 (goto-char (point-min)))
460 ;; Quote all "From " lines in the article.
462 (while (re-search-forward delim nil t)
465 (nnmail-insert-lines)
466 (nnmail-insert-xref group-art)
467 (nnmbox-insert-newsgroup-line group-art)
468 (run-hooks 'nnmail-prepare-save-mail-hook)
469 (run-hooks 'nnmbox-prepare-save-mail-hook)
472 (defun nnmbox-insert-newsgroup-line (group-art)
474 (goto-char (point-min))
475 (when (search-forward "\n\n" nil t)
478 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
479 (caar group-art) (cdar group-art)
480 (current-time-string)))
481 (setq group-art (cdr group-art))))
484 (defun nnmbox-active-number (group)
485 ;; Find the next article number in GROUP.
486 (let ((active (cadr (assoc group nnmbox-group-alist))))
488 (setcdr active (1+ (cdr active)))
489 ;; This group is new, so we create a new entry for it.
490 ;; This might be a bit naughty... creating groups on the drop of
491 ;; a hat, but I don't know...
492 (push (list group (setq active (cons 1 1)))
496 (defun nnmbox-create-mbox ()
497 (when (not (file-exists-p nnmbox-mbox-file))
498 (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg)))
500 (defun nnmbox-read-mbox ()
501 (nnmail-activate 'nnmbox)
503 (if (and nnmbox-mbox-buffer
504 (buffer-name nnmbox-mbox-buffer)
506 (set-buffer nnmbox-mbox-buffer)
507 (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
510 (let ((delim (concat "^" message-unix-mail-delimiter))
511 (alist nnmbox-group-alist)
513 (set-buffer (setq nnmbox-mbox-buffer
514 (nnheader-find-file-noselect
515 nnmbox-mbox-file nil 'raw)))
516 (buffer-disable-undo (current-buffer))
518 ;; Go through the group alist and compare against
521 (goto-char (point-max))
522 (when (and (re-search-backward
523 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
528 (match-beginning 1) (match-end 1))))
530 (setcdr (cadar alist) (1+ number)))
531 (setq alist (cdr alist)))
533 (goto-char (point-min))
534 (while (re-search-forward delim nil t)
535 (setq start (match-beginning 0))
536 (when (not (search-forward "\nX-Gnus-Newsgroup: "
541 (re-search-forward delim nil t)
547 (narrow-to-region start end)
549 (nnmail-article-group 'nnmbox-active-number)))))
554 ;;; nnmbox.el ends here