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 nnmbox-mbox-file group
200 (let ((in-buf (current-buffer)))
201 (set-buffer nnmbox-mbox-buffer)
202 (goto-char (point-max))
203 (insert-buffer-substring in-buf)))
204 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))))
206 (deffoo nnmbox-close-group (group &optional server)
209 (deffoo nnmbox-request-list (&optional server)
211 (nnmail-find-file nnmbox-active-file)
212 (setq nnmbox-group-alist (nnmail-get-active))
215 (deffoo nnmbox-request-newgroups (date &optional server)
216 (nnmbox-request-list server))
218 (deffoo nnmbox-request-list-newsgroups (&optional server)
219 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
221 (deffoo nnmbox-request-expire-articles
222 (articles newsgroup &optional server force)
223 (nnmbox-possibly-change-newsgroup newsgroup server)
226 (nnmail-activate 'nnmbox)
229 (set-buffer nnmbox-mbox-buffer)
230 (while (and articles is-old)
231 (goto-char (point-min))
232 (when (search-forward (nnmbox-article-string (car articles)) nil t)
234 (nnmail-expired-article-p
237 (point) (progn (end-of-line) (point))) force))
239 (nnheader-message 5 "Deleting article %d in %s..."
240 (car articles) newsgroup)
241 (nnmbox-delete-mail))
242 (push (car articles) rest)))
243 (setq articles (cdr articles)))
245 ;; Find the lowest active article in this group.
246 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
247 (goto-char (point-min))
248 (while (and (not (search-forward
249 (nnmbox-article-string (car active)) nil t))
250 (<= (car active) (cdr active)))
251 (setcar active (1+ (car active)))
252 (goto-char (point-min))))
253 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
254 (nconc rest articles))))
256 (deffoo nnmbox-request-move-article
257 (article group server accept-form &optional last)
258 (let ((buf (get-buffer-create " *nnmbox move*"))
261 (nnmbox-request-article article group server)
264 (buffer-disable-undo (current-buffer))
266 (insert-buffer-substring nntp-server-buffer)
267 (goto-char (point-min))
268 (while (re-search-forward
270 (save-excursion (search-forward "\n\n" nil t) (point)) t)
271 (delete-region (progn (beginning-of-line) (point))
272 (progn (forward-line 1) (point))))
273 (setq result (eval accept-form))
277 (nnmbox-possibly-change-newsgroup group server)
278 (set-buffer nnmbox-mbox-buffer)
279 (goto-char (point-min))
280 (when (search-forward (nnmbox-article-string article) nil t)
281 (nnmbox-delete-mail))
282 (and last (save-buffer))))
285 (deffoo nnmbox-request-accept-article (group &optional server last)
286 (nnmbox-possibly-change-newsgroup group server)
287 (nnmail-check-syntax)
288 (let ((buf (current-buffer))
290 (goto-char (point-min))
291 ;; The From line may have been quoted by movemail.
292 (when (looking-at (concat ">" message-unix-mail-delimiter))
294 (if (looking-at "X-From-Line: ")
295 (replace-match "From ")
296 (insert "From nobody " (current-time-string) "\n"))
298 (nnmail-activate 'nnmbox)
301 (goto-char (point-min))
302 (search-forward "\n\n" nil t)
304 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
305 (delete-region (point) (progn (forward-line 1) (point))))
306 (setq result (nnmbox-save-mail
308 (list (cons group (nnmbox-active-number group)))
309 (nnmail-article-group 'nnmbox-active-number)))))
311 (set-buffer nnmbox-mbox-buffer)
312 (goto-char (point-max))
313 (insert-buffer-substring buf)
314 (and last (save-buffer))
316 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
319 (deffoo nnmbox-request-replace-article (article group buffer)
320 (nnmbox-possibly-change-newsgroup group)
322 (set-buffer nnmbox-mbox-buffer)
323 (goto-char (point-min))
324 (if (not (search-forward (nnmbox-article-string article) nil t))
326 (nnmbox-delete-mail t t)
327 (insert-buffer-substring buffer)
331 (deffoo nnmbox-request-delete-group (group &optional force server)
332 (nnmbox-possibly-change-newsgroup group server)
333 ;; Delete all articles in GROUP.
335 () ; Don't delete the articles.
337 (set-buffer nnmbox-mbox-buffer)
338 (goto-char (point-min))
339 ;; Delete all articles in this group.
340 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
342 (while (search-forward ident nil t)
344 (nnmbox-delete-mail))
347 ;; Remove the group from all structures.
348 (setq nnmbox-group-alist
349 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
350 nnmbox-current-group nil)
351 ;; Save the active file.
352 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
355 (deffoo nnmbox-request-rename-group (group new-name &optional server)
356 (nnmbox-possibly-change-newsgroup group server)
358 (set-buffer nnmbox-mbox-buffer)
359 (goto-char (point-min))
360 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
361 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
363 (while (search-forward ident nil t)
364 (replace-match new-ident t t)
368 (let ((entry (assoc group nnmbox-group-alist)))
370 (setcar entry new-name))
371 (setq nnmbox-current-group nil)
372 ;; Save the new group alist.
373 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
377 ;;; Internal functions.
379 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
380 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
382 (defun nnmbox-delete-mail (&optional force leave-delim)
383 ;; Delete the current X-Gnus-Newsgroup line.
386 (progn (beginning-of-line) (point))
387 (progn (forward-line 1) (point))))
388 ;; Beginning of the article.
393 (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
394 (if leave-delim (progn (forward-line 1) (point))
395 (match-beginning 0)))
398 (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
400 (if (and (not (bobp)) leave-delim)
401 (progn (forward-line -2) (point))
402 (match-beginning 0)))
404 (goto-char (point-min))
405 ;; Only delete the article if no other groups owns it as well.
406 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
407 (delete-region (point-min) (point-max))))))
409 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
411 (not (nnmbox-server-opened server)))
412 (nnmbox-open-server server))
413 (when (or (not nnmbox-mbox-buffer)
414 (not (buffer-name nnmbox-mbox-buffer)))
416 (set-buffer (setq nnmbox-mbox-buffer
417 (nnheader-find-file-noselect
418 nnmbox-mbox-file nil 'raw)))
419 (buffer-disable-undo (current-buffer))))
420 (when (not nnmbox-group-alist)
421 (nnmail-activate 'nnmbox))
423 (when (assoc newsgroup nnmbox-group-alist)
424 (setq nnmbox-current-group newsgroup))
427 (defun nnmbox-article-string (article)
428 (if (numberp article)
429 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
430 (int-to-string article) " ")
431 (concat "\nMessage-ID: " article)))
433 (defun nnmbox-article-group-number ()
435 (goto-char (point-min))
436 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
438 (cons (buffer-substring (match-beginning 1) (match-end 1))
440 (buffer-substring (match-beginning 2) (match-end 2)))))))
442 (defun nnmbox-save-mail (group-art)
443 "Called narrowed to an article."
444 (let ((delim (concat "^" message-unix-mail-delimiter)))
445 (goto-char (point-min))
446 ;; This might come from somewhere else.
447 (unless (looking-at delim)
448 (insert "From nobody " (current-time-string) "\n")
449 (goto-char (point-min)))
450 ;; Quote all "From " lines in the article.
452 (while (re-search-forward delim nil t)
455 (nnmail-insert-lines)
456 (nnmail-insert-xref group-art)
457 (nnmbox-insert-newsgroup-line group-art)
458 (run-hooks 'nnmail-prepare-save-mail-hook)
459 (run-hooks 'nnmbox-prepare-save-mail-hook)
462 (defun nnmbox-insert-newsgroup-line (group-art)
464 (goto-char (point-min))
465 (when (search-forward "\n\n" nil t)
468 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
469 (caar group-art) (cdar group-art)
470 (current-time-string)))
471 (setq group-art (cdr group-art))))
474 (defun nnmbox-active-number (group)
475 ;; Find the next article number in GROUP.
476 (let ((active (cadr (assoc group nnmbox-group-alist))))
478 (setcdr active (1+ (cdr active)))
479 ;; This group is new, so we create a new entry for it.
480 ;; This might be a bit naughty... creating groups on the drop of
481 ;; a hat, but I don't know...
482 (push (list group (setq active (cons 1 1)))
486 (defun nnmbox-create-mbox ()
487 (when (not (file-exists-p nnmbox-mbox-file))
488 (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg)))
490 (defun nnmbox-read-mbox ()
491 (nnmail-activate 'nnmbox)
493 (if (and nnmbox-mbox-buffer
494 (buffer-name nnmbox-mbox-buffer)
496 (set-buffer nnmbox-mbox-buffer)
497 (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
500 (let ((delim (concat "^" message-unix-mail-delimiter))
501 (alist nnmbox-group-alist)
503 (set-buffer (setq nnmbox-mbox-buffer
504 (nnheader-find-file-noselect
505 nnmbox-mbox-file nil 'raw)))
506 (buffer-disable-undo (current-buffer))
508 ;; Go through the group alist and compare against
511 (goto-char (point-max))
512 (when (and (re-search-backward
513 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
518 (match-beginning 1) (match-end 1))))
520 (setcdr (cadar alist) (1+ number)))
521 (setq alist (cdr alist)))
523 (goto-char (point-min))
524 (while (re-search-forward delim nil t)
525 (setq start (match-beginning 0))
526 (when (not (search-forward "\nX-Gnus-Newsgroup: "
531 (re-search-forward delim nil t)
537 (narrow-to-region start end)
539 (nnmail-article-group 'nnmbox-active-number)))))
544 ;;; nnmbox.el ends here