1 ;;; nnmbox.el --- mail mbox access for Gnus
2 ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; For an overview of what the interface functions do, please see the
36 (eval-when-compile (require 'cl))
40 (defvoo nnmbox-mbox-file (expand-file-name "~/mbox")
41 "The name of the mail box file in the user's home directory.")
43 (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
44 "The name of the active file for the mail box.")
46 (defvoo nnmbox-get-new-mail t
47 "If non-nil, nnmbox will check the incoming mail file and split the mail.")
49 (defvoo nnmbox-prepare-save-mail-hook nil
50 "Hook run narrowed to an article before saving.")
54 (defconst nnmbox-version "nnmbox 1.0"
57 (defvoo nnmbox-current-group nil
58 "Current nnmbox news group directory.")
60 (defconst nnmbox-mbox-buffer nil)
62 (defvoo nnmbox-status-string "")
64 (defvoo nnmbox-group-alist nil)
65 (defvoo nnmbox-active-timestamp 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 (let ((active (cadr (assoc group nnmbox-group-alist))))
177 (null (nnmbox-possibly-change-newsgroup group server)))
178 (nnheader-report 'nnmbox "No such group: %s" group))
180 (nnheader-report 'nnmbox "Selected group %s" group)
181 (nnheader-insert ""))
183 (nnheader-report 'nnmbox "Selected group %s" group)
184 (nnheader-insert "211 %d %d %d %s\n"
185 (1+ (- (cdr active) (car active)))
186 (car active) (cdr active) group)))))
188 (deffoo nnmbox-request-scan (&optional group server)
189 (nnmbox-possibly-change-newsgroup group server)
195 (set-buffer nnmbox-mbox-buffer)
197 (file-name-directory nnmbox-mbox-file)
201 (let ((in-buf (current-buffer)))
202 (set-buffer nnmbox-mbox-buffer)
203 (goto-char (point-max))
204 (insert-buffer-substring in-buf)))
205 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))))
207 (deffoo nnmbox-close-group (group &optional server)
210 (deffoo nnmbox-request-list (&optional server)
212 (nnmail-find-file nnmbox-active-file)
213 (setq nnmbox-group-alist (nnmail-get-active))
216 (deffoo nnmbox-request-newgroups (date &optional server)
217 (nnmbox-request-list server))
219 (deffoo nnmbox-request-list-newsgroups (&optional server)
220 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
222 (deffoo nnmbox-request-expire-articles
223 (articles newsgroup &optional server force)
224 (nnmbox-possibly-change-newsgroup newsgroup server)
227 (nnmail-activate 'nnmbox)
230 (set-buffer nnmbox-mbox-buffer)
231 (while (and articles is-old)
232 (goto-char (point-min))
233 (when (search-forward (nnmbox-article-string (car articles)) nil t)
235 (nnmail-expired-article-p
238 (point) (progn (end-of-line) (point))) force))
240 (nnheader-message 5 "Deleting article %d in %s..."
241 (car articles) newsgroup)
242 (nnmbox-delete-mail))
243 (push (car articles) rest)))
244 (setq articles (cdr articles)))
246 ;; Find the lowest active article in this group.
247 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
248 (goto-char (point-min))
249 (while (and (not (search-forward
250 (nnmbox-article-string (car active)) nil t))
251 (<= (car active) (cdr active)))
252 (setcar active (1+ (car active)))
253 (goto-char (point-min))))
254 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
255 (nconc rest articles))))
257 (deffoo nnmbox-request-move-article
258 (article group server accept-form &optional last)
259 (let ((buf (get-buffer-create " *nnmbox move*"))
262 (nnmbox-request-article article group server)
265 (buffer-disable-undo (current-buffer))
267 (insert-buffer-substring nntp-server-buffer)
268 (goto-char (point-min))
269 (while (re-search-forward
271 (save-excursion (search-forward "\n\n" nil t) (point)) t)
272 (delete-region (progn (beginning-of-line) (point))
273 (progn (forward-line 1) (point))))
274 (setq result (eval accept-form))
278 (nnmbox-possibly-change-newsgroup group server)
279 (set-buffer nnmbox-mbox-buffer)
280 (goto-char (point-min))
281 (when (search-forward (nnmbox-article-string article) nil t)
282 (nnmbox-delete-mail))
283 (and last (save-buffer))))
286 (deffoo nnmbox-request-accept-article (group &optional server last)
287 (nnmbox-possibly-change-newsgroup group server)
288 (nnmail-check-syntax)
289 (let ((buf (current-buffer))
291 (goto-char (point-min))
292 ;; The From line may have been quoted by movemail.
293 (when (looking-at (concat ">" message-unix-mail-delimiter))
295 (if (looking-at "X-From-Line: ")
296 (replace-match "From ")
297 (insert "From nobody " (current-time-string) "\n"))
299 (nnmail-activate 'nnmbox)
302 (goto-char (point-min))
303 (search-forward "\n\n" nil t)
305 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
306 (delete-region (point) (progn (forward-line 1) (point))))
307 (nnmail-cache-insert (nnmail-fetch-field "message-id"))
308 (setq result (nnmbox-save-mail
310 (list (cons group (nnmbox-active-number group)))
311 (nnmail-article-group 'nnmbox-active-number)))))
313 (set-buffer nnmbox-mbox-buffer)
314 (goto-char (point-max))
315 (insert-buffer-substring buf)
318 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
322 (deffoo nnmbox-request-replace-article (article group buffer)
323 (nnmbox-possibly-change-newsgroup group)
325 (set-buffer nnmbox-mbox-buffer)
326 (goto-char (point-min))
327 (if (not (search-forward (nnmbox-article-string article) nil t))
329 (nnmbox-delete-mail t t)
330 (insert-buffer-substring buffer)
334 (deffoo nnmbox-request-delete-group (group &optional force server)
335 (nnmbox-possibly-change-newsgroup group server)
336 ;; Delete all articles in GROUP.
338 () ; Don't delete the articles.
340 (set-buffer nnmbox-mbox-buffer)
341 (goto-char (point-min))
342 ;; Delete all articles in this group.
343 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
345 (while (search-forward ident nil t)
347 (nnmbox-delete-mail))
350 ;; Remove the group from all structures.
351 (setq nnmbox-group-alist
352 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
353 nnmbox-current-group nil)
354 ;; Save the active file.
355 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
358 (deffoo nnmbox-request-rename-group (group new-name &optional server)
359 (nnmbox-possibly-change-newsgroup group server)
361 (set-buffer nnmbox-mbox-buffer)
362 (goto-char (point-min))
363 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
364 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
366 (while (search-forward ident nil t)
367 (replace-match new-ident t t)
371 (let ((entry (assoc group nnmbox-group-alist)))
373 (setcar entry new-name))
374 (setq nnmbox-current-group nil)
375 ;; Save the new group alist.
376 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
380 ;;; Internal functions.
382 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
383 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
385 (defun nnmbox-delete-mail (&optional force leave-delim)
386 ;; Delete the current X-Gnus-Newsgroup line.
389 (progn (beginning-of-line) (point))
390 (progn (forward-line 1) (point))))
391 ;; Beginning of the article.
396 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
397 (if leave-delim (progn (forward-line 1) (point))
398 (match-beginning 0)))
401 (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
403 (if (and (not (bobp)) leave-delim)
404 (progn (forward-line -2) (point))
405 (match-beginning 0)))
407 (goto-char (point-min))
408 ;; Only delete the article if no other groups owns it as well.
409 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
410 (delete-region (point-min) (point-max))))))
412 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
414 (not (nnmbox-server-opened server)))
415 (nnmbox-open-server server))
416 (when (or (not nnmbox-mbox-buffer)
417 (not (buffer-name nnmbox-mbox-buffer)))
419 (set-buffer (setq nnmbox-mbox-buffer
420 (nnheader-find-file-noselect
421 nnmbox-mbox-file nil 'raw)))
422 (buffer-disable-undo (current-buffer))))
423 (when (not nnmbox-group-alist)
424 (nnmail-activate 'nnmbox))
426 (when (assoc newsgroup nnmbox-group-alist)
427 (setq nnmbox-current-group newsgroup))
430 (defun nnmbox-article-string (article)
431 (if (numberp article)
432 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
433 (int-to-string article) " ")
434 (concat "\nMessage-ID: " article)))
436 (defun nnmbox-article-group-number ()
438 (goto-char (point-min))
439 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
441 (cons (buffer-substring (match-beginning 1) (match-end 1))
443 (buffer-substring (match-beginning 2) (match-end 2)))))))
445 (defun nnmbox-save-mail (group-art)
446 "Called narrowed to an article."
447 (let ((delim (concat "^" message-unix-mail-delimiter)))
448 (goto-char (point-min))
449 ;; This might come from somewhere else.
450 (unless (looking-at delim)
451 (insert "From nobody " (current-time-string) "\n")
452 (goto-char (point-min)))
453 ;; Quote all "From " lines in the article.
455 (while (re-search-forward delim nil t)
458 (nnmail-insert-lines)
459 (nnmail-insert-xref group-art)
460 (nnmbox-insert-newsgroup-line group-art)
461 (run-hooks 'nnmail-prepare-save-mail-hook)
462 (run-hooks 'nnmbox-prepare-save-mail-hook)
465 (defun nnmbox-insert-newsgroup-line (group-art)
467 (goto-char (point-min))
468 (when (search-forward "\n\n" nil t)
471 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
472 (caar group-art) (cdar group-art)
473 (current-time-string)))
474 (setq group-art (cdr group-art))))
477 (defun nnmbox-active-number (group)
478 ;; Find the next article number in GROUP.
479 (let ((active (cadr (assoc group nnmbox-group-alist))))
481 (setcdr active (1+ (cdr active)))
482 ;; This group is new, so we create a new entry for it.
483 ;; This might be a bit naughty... creating groups on the drop of
484 ;; a hat, but I don't know...
485 (push (list group (setq active (cons 1 1)))
489 (defun nnmbox-create-mbox ()
490 (when (not (file-exists-p nnmbox-mbox-file))
491 (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg)))
493 (defun nnmbox-read-mbox ()
494 (nnmail-activate 'nnmbox)
496 (if (and nnmbox-mbox-buffer
497 (buffer-name nnmbox-mbox-buffer)
499 (set-buffer nnmbox-mbox-buffer)
500 (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
503 (let ((delim (concat "^" message-unix-mail-delimiter))
504 (alist nnmbox-group-alist)
506 (set-buffer (setq nnmbox-mbox-buffer
507 (nnheader-find-file-noselect
508 nnmbox-mbox-file nil 'raw)))
509 (buffer-disable-undo (current-buffer))
511 ;; Go through the group alist and compare against
514 (goto-char (point-max))
515 (when (and (re-search-backward
516 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
521 (match-beginning 1) (match-end 1))))
523 (setcdr (cadar alist) (1+ number)))
524 (setq alist (cdr alist)))
526 (goto-char (point-min))
527 (while (re-search-forward delim nil t)
528 (setq start (match-beginning 0))
529 (when (not (search-forward "\nX-Gnus-Newsgroup: "
534 (re-search-forward delim nil t)
540 (narrow-to-region start end)
542 (nnmail-article-group 'nnmbox-active-number)))))
547 ;;; nnmbox.el ends here