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))
37 (defvoo nnmbox-mbox-file (expand-file-name "~/mbox")
38 "The name of the mail box file in the user's home directory.")
40 (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
41 "The name of the active file for the mail box.")
43 (defvoo nnmbox-get-new-mail t
44 "If non-nil, nnmbox will check the incoming mail file and split the mail.")
46 (defvoo nnmbox-prepare-save-mail-hook nil
47 "Hook run narrowed to an article before saving.")
51 (defconst nnmbox-version "nnmbox 1.0"
54 (defvoo nnmbox-current-group nil
55 "Current nnmbox news group directory.")
57 (defconst nnmbox-mbox-buffer nil)
59 (defvoo nnmbox-status-string "")
61 (defvoo nnmbox-group-alist nil)
62 (defvoo nnmbox-active-timestamp nil)
64 (defvoo nnmbox-file-coding-system nnheader-text-coding-system)
65 (defvoo nnmbox-file-coding-system-for-write nil)
66 (defvoo nnmbox-active-file-coding-system nnheader-text-coding-system)
67 (defvoo nnmbox-active-file-coding-system-for-write nil)
71 ;;; Interface functions
73 (nnoo-define-basics nnmbox)
75 (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
77 (set-buffer nntp-server-buffer)
79 (let ((number (length sequence))
81 article art-string start stop)
82 (nnmbox-possibly-change-newsgroup newsgroup server)
84 (setq article (car sequence))
85 (setq art-string (nnmbox-article-string article))
86 (set-buffer nnmbox-mbox-buffer)
87 (when (or (search-forward art-string nil t)
88 (progn (goto-char (point-min))
89 (search-forward art-string nil t)))
93 (concat "^" message-unix-mail-delimiter) nil t)
95 (search-forward "\n\n" nil t)
96 (setq stop (1- (point)))
97 (set-buffer nntp-server-buffer)
98 (insert (format "221 %d Article retrieved.\n" article))
99 (insert-buffer-substring nnmbox-mbox-buffer start stop)
100 (goto-char (point-max))
102 (setq sequence (cdr sequence))
103 (setq count (1+ count))
104 (and (numberp nnmail-large-newsgroup)
105 (> number nnmail-large-newsgroup)
107 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
108 (/ (* count 100) number))))
110 (and (numberp nnmail-large-newsgroup)
111 (> number nnmail-large-newsgroup)
112 (nnheader-message 5 "nnmbox: Receiving headers...done"))
114 (set-buffer nntp-server-buffer)
115 (nnheader-fold-continuation-lines)
118 (deffoo nnmbox-open-server (server &optional defs)
119 (nnoo-change-server 'nnmbox server defs)
122 ((not (file-exists-p nnmbox-mbox-file))
123 (nnmbox-close-server)
124 (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
125 ((file-directory-p nnmbox-mbox-file)
126 (nnmbox-close-server)
127 (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
129 (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
133 (deffoo nnmbox-close-server (&optional server)
134 (when (and nnmbox-mbox-buffer
135 (buffer-name nnmbox-mbox-buffer))
136 (kill-buffer nnmbox-mbox-buffer))
137 (nnoo-close-server 'nnmbox server)
140 (deffoo nnmbox-server-opened (&optional server)
141 (and (nnoo-current-server-p 'nnmbox server)
143 (buffer-name nnmbox-mbox-buffer)
145 (buffer-name nntp-server-buffer)))
147 (deffoo nnmbox-request-article (article &optional newsgroup server buffer)
148 (nnmbox-possibly-change-newsgroup newsgroup server)
150 (set-buffer nnmbox-mbox-buffer)
151 (goto-char (point-min))
152 (when (search-forward (nnmbox-article-string article) nil t)
154 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
157 (or (and (re-search-forward
158 (concat "^" message-unix-mail-delimiter) nil t)
160 (goto-char (point-max)))
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)))))))
175 (deffoo nnmbox-request-group (group &optional server dont-check)
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 (static-if (boundp 'MULE)
192 (defun nnmbox-save-buffer ()
193 (let ((output-coding-system
194 (or nnmbox-file-coding-system-for-write
195 nnmbox-file-coding-system)))
197 (defun nnmbox-save-buffer ()
198 (let ((coding-system-for-write
199 (or nnmbox-file-coding-system-for-write
200 nnmbox-file-coding-system)))
204 (defun nnmbox-save-active (group-alist active-file)
205 (let ((nnmail-active-file-coding-system
206 (or nnmbox-active-file-coding-system-for-write
207 nnmbox-active-file-coding-system)))
208 (nnmail-save-active group-alist active-file)))
210 (deffoo nnmbox-request-scan (&optional group server)
211 (nnmbox-possibly-change-newsgroup group server)
217 (set-buffer nnmbox-mbox-buffer)
218 (nnmbox-save-buffer)))
219 (file-name-directory nnmbox-mbox-file)
223 (let ((in-buf (current-buffer)))
224 (set-buffer nnmbox-mbox-buffer)
225 (goto-char (point-max))
226 (insert-buffer-substring in-buf)))
227 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
229 (deffoo nnmbox-close-group (group &optional server)
232 (deffoo nnmbox-request-create-group (group &optional server args)
233 (nnmail-activate 'nnmbox)
234 (unless (assoc group nnmbox-group-alist)
235 (push (list group (cons 1 0))
237 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
240 (deffoo nnmbox-request-list (&optional server)
242 (let ((nnmail-file-coding-system
243 nnmbox-active-file-coding-system))
244 (nnmail-find-file nnmbox-active-file))
245 (setq nnmbox-group-alist (nnmail-get-active))
248 (deffoo nnmbox-request-newgroups (date &optional server)
249 (nnmbox-request-list server))
251 (deffoo nnmbox-request-list-newsgroups (&optional server)
252 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
254 (deffoo nnmbox-request-expire-articles
255 (articles newsgroup &optional server force)
256 (nnmbox-possibly-change-newsgroup newsgroup server)
259 (nnmail-activate 'nnmbox)
262 (set-buffer nnmbox-mbox-buffer)
263 (while (and articles is-old)
264 (goto-char (point-min))
265 (when (search-forward (nnmbox-article-string (car articles)) nil t)
267 (nnmail-expired-article-p
270 (point) (progn (end-of-line) (point))) force))
272 (nnheader-message 5 "Deleting article %d in %s..."
273 (car articles) newsgroup)
274 (nnmbox-delete-mail))
275 (push (car articles) rest)))
276 (setq articles (cdr articles)))
278 ;; Find the lowest active article in this group.
279 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
280 (goto-char (point-min))
281 (while (and (not (search-forward
282 (nnmbox-article-string (car active)) nil t))
283 (<= (car active) (cdr active)))
284 (setcar active (1+ (car active)))
285 (goto-char (point-min))))
286 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
287 (nconc rest articles))))
289 (deffoo nnmbox-request-move-article
290 (article group server accept-form &optional last)
291 (let ((buf (get-buffer-create " *nnmbox move*"))
294 (nnmbox-request-article article group server)
298 (insert-buffer-substring nntp-server-buffer)
299 (goto-char (point-min))
300 (while (re-search-forward
302 (save-excursion (search-forward "\n\n" nil t) (point)) t)
303 (delete-region (progn (beginning-of-line) (point))
304 (progn (forward-line 1) (point))))
305 (setq result (eval accept-form))
309 (nnmbox-possibly-change-newsgroup group server)
310 (set-buffer nnmbox-mbox-buffer)
311 (goto-char (point-min))
312 (when (search-forward (nnmbox-article-string article) nil t)
313 (nnmbox-delete-mail))
314 (and last (nnmbox-save-buffer))))
317 (deffoo nnmbox-request-accept-article (group &optional server last)
318 (nnmbox-possibly-change-newsgroup group server)
319 (nnmail-check-syntax)
320 (let ((buf (current-buffer))
322 (goto-char (point-min))
323 ;; The From line may have been quoted by movemail.
324 (when (looking-at (concat ">" message-unix-mail-delimiter))
326 (if (looking-at "X-From-Line: ")
327 (replace-match "From ")
328 (insert "From nobody " (current-time-string) "\n"))
330 (nnmail-activate 'nnmbox)
333 (goto-char (point-min))
334 (search-forward "\n\n" nil t)
336 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
337 (delete-region (point) (progn (forward-line 1) (point))))
338 (when nnmail-cache-accepted-message-ids
339 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
340 (setq result (if (stringp group)
341 (list (cons group (nnmbox-active-number group)))
342 (nnmail-article-group 'nnmbox-active-number)))
343 (if (and (null result)
344 (yes-or-no-p "Moved to `junk' group; delete article? "))
346 (setq result (car (nnmbox-save-mail result)))))
348 (set-buffer nnmbox-mbox-buffer)
349 (goto-char (point-max))
350 (insert-buffer-substring buf)
352 (when nnmail-cache-accepted-message-ids
353 (nnmail-cache-close))
354 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
355 (nnmbox-save-buffer))))
358 (deffoo nnmbox-request-replace-article (article group buffer)
359 (nnmbox-possibly-change-newsgroup group)
361 (set-buffer nnmbox-mbox-buffer)
362 (goto-char (point-min))
363 (if (not (search-forward (nnmbox-article-string article) nil t))
365 (nnmbox-delete-mail t t)
366 (insert-buffer-substring buffer)
370 (deffoo nnmbox-request-delete-group (group &optional force server)
371 (nnmbox-possibly-change-newsgroup group server)
372 ;; Delete all articles in GROUP.
374 () ; Don't delete the articles.
376 (set-buffer nnmbox-mbox-buffer)
377 (goto-char (point-min))
378 ;; Delete all articles in this group.
379 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
381 (while (search-forward ident nil t)
383 (nnmbox-delete-mail))
385 (nnmbox-save-buffer)))))
386 ;; Remove the group from all structures.
387 (setq nnmbox-group-alist
388 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
389 nnmbox-current-group nil)
390 ;; Save the active file.
391 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
394 (deffoo nnmbox-request-rename-group (group new-name &optional server)
395 (nnmbox-possibly-change-newsgroup group server)
397 (set-buffer nnmbox-mbox-buffer)
398 (goto-char (point-min))
399 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
400 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
402 (while (search-forward ident nil t)
403 (replace-match new-ident t t)
406 (nnmbox-save-buffer))))
407 (let ((entry (assoc group nnmbox-group-alist)))
409 (setcar entry new-name))
410 (setq nnmbox-current-group nil)
411 ;; Save the new group alist.
412 (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
416 ;;; Internal functions.
418 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
419 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
421 (defun nnmbox-delete-mail (&optional force leave-delim)
422 ;; Delete the current X-Gnus-Newsgroup line.
425 (progn (beginning-of-line) (point))
426 (progn (forward-line 1) (point))))
427 ;; Beginning of the article.
432 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
433 (if leave-delim (progn (forward-line 1) (point))
434 (match-beginning 0)))
437 (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
439 (if (and (not (bobp)) leave-delim)
440 (progn (forward-line -2) (point))
441 (match-beginning 0)))
443 (goto-char (point-min))
444 ;; Only delete the article if no other groups owns it as well.
445 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
446 (delete-region (point-min) (point-max))))))
448 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
450 (not (nnmbox-server-opened server)))
451 (nnmbox-open-server server))
452 (when (or (not nnmbox-mbox-buffer)
453 (not (buffer-name nnmbox-mbox-buffer)))
455 (set-buffer (setq nnmbox-mbox-buffer
456 (let ((nnheader-file-coding-system
457 nnmbox-file-coding-system))
458 (nnheader-find-file-noselect
459 nnmbox-mbox-file nil t))))
460 (buffer-disable-undo)))
461 (when (not nnmbox-group-alist)
462 (nnmail-activate 'nnmbox))
464 (when (assoc newsgroup nnmbox-group-alist)
465 (setq nnmbox-current-group newsgroup))
468 (defun nnmbox-article-string (article)
469 (if (numberp article)
470 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
471 (int-to-string article) " ")
472 (concat "\nMessage-ID: " article)))
474 (defun nnmbox-article-group-number ()
476 (goto-char (point-min))
477 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
479 (cons (buffer-substring (match-beginning 1) (match-end 1))
481 (buffer-substring (match-beginning 2) (match-end 2)))))))
483 (defun nnmbox-save-mail (group-art)
484 "Called narrowed to an article."
485 (let ((delim (concat "^" message-unix-mail-delimiter)))
486 (goto-char (point-min))
487 ;; This might come from somewhere else.
488 (unless (looking-at delim)
489 (insert "From nobody " (current-time-string) "\n")
490 (goto-char (point-min)))
491 ;; Quote all "From " lines in the article.
493 (while (re-search-forward delim nil t)
496 (nnmail-insert-lines)
497 (nnmail-insert-xref group-art)
498 (nnmbox-insert-newsgroup-line group-art)
499 (run-hooks 'nnmail-prepare-save-mail-hook)
500 (run-hooks 'nnmbox-prepare-save-mail-hook)
503 (defun nnmbox-insert-newsgroup-line (group-art)
505 (goto-char (point-min))
506 (when (search-forward "\n\n" nil t)
509 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
510 (caar group-art) (cdar group-art)
511 (current-time-string)))
512 (setq group-art (cdr group-art))))
515 (defun nnmbox-active-number (group)
516 ;; Find the next article number in GROUP.
517 (let ((active (cadr (assoc group nnmbox-group-alist))))
519 (setcdr active (1+ (cdr active)))
520 ;; This group is new, so we create a new entry for it.
521 ;; This might be a bit naughty... creating groups on the drop of
522 ;; a hat, but I don't know...
523 (push (list group (setq active (cons 1 1)))
527 (defun nnmbox-create-mbox ()
528 (when (not (file-exists-p nnmbox-mbox-file))
529 (let ((nnmail-file-coding-system
530 (or nnmbox-file-coding-system-for-write
531 nnmbox-file-coding-system)))
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) 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 (unless (search-forward
573 "\nX-Gnus-Newsgroup: "
578 ;; skip to end of headers first, since mail
579 ;; which has been respooled has additional
580 ;; "From nobody" lines.
581 (search-forward "\n\n" nil t)
582 (re-search-forward delim nil t)
588 (narrow-to-region start end)
590 (nnmail-article-group 'nnmbox-active-number)))))
595 ;;; nnmbox.el ends here