1 ;;; nnmbox.el --- mail mbox access for Gnus
2 ;; Copyright (C) 1995,96 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 (if (or (search-forward art-string nil t)
86 (progn (goto-char (point-min))
87 (search-forward art-string nil t)))
92 (concat "^" rmail-unix-mail-delimiter) nil t)
94 (search-forward "\n\n" nil t)
95 (setq stop (1- (point)))
96 (set-buffer nntp-server-buffer)
97 (insert (format "221 %d Article retrieved.\n" article))
98 (insert-buffer-substring nnmbox-mbox-buffer start stop)
99 (goto-char (point-max))
101 (setq sequence (cdr sequence))
102 (setq count (1+ count))
103 (and (numberp nnmail-large-newsgroup)
104 (> number nnmail-large-newsgroup)
106 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
107 (/ (* count 100) number))))
109 (and (numberp nnmail-large-newsgroup)
110 (> number nnmail-large-newsgroup)
111 (nnheader-message 5 "nnmbox: Receiving headers...done"))
113 (set-buffer nntp-server-buffer)
114 (nnheader-fold-continuation-lines)
117 (deffoo nnmbox-open-server (server &optional defs)
118 (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 (if (search-forward (nnmbox-article-string article) nil t)
152 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
155 (or (and (re-search-forward
156 (concat "^" rmail-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)
194 (set-buffer nnmbox-mbox-buffer)
196 nnmbox-mbox-file group
199 (let ((in-buf (current-buffer)))
200 (set-buffer nnmbox-mbox-buffer)
201 (goto-char (point-max))
202 (insert-buffer-substring in-buf)))
203 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))))
205 (deffoo nnmbox-close-group (group &optional server)
208 (deffoo nnmbox-request-list (&optional server)
210 (nnmail-find-file nnmbox-active-file)
211 (setq nnmbox-group-alist (nnmail-get-active))))
213 (deffoo nnmbox-request-newgroups (date &optional server)
214 (nnmbox-request-list server))
216 (deffoo nnmbox-request-list-newsgroups (&optional server)
217 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
219 (deffoo nnmbox-request-expire-articles
220 (articles newsgroup &optional server force)
221 (nnmbox-possibly-change-newsgroup newsgroup server)
224 (nnmail-activate 'nnmbox)
227 (set-buffer nnmbox-mbox-buffer)
228 (while (and articles is-old)
229 (goto-char (point-min))
230 (if (search-forward (nnmbox-article-string (car articles)) nil t)
232 (nnmail-expired-article-p
235 (point) (progn (end-of-line) (point))) force))
237 (nnheader-message 5 "Deleting article %d in %s..."
238 (car articles) newsgroup)
239 (nnmbox-delete-mail))
240 (setq rest (cons (car articles) rest))))
241 (setq articles (cdr articles)))
243 ;; Find the lowest active article in this group.
244 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
245 (goto-char (point-min))
246 (while (and (not (search-forward
247 (nnmbox-article-string (car active)) nil t))
248 (<= (car active) (cdr active)))
249 (setcar active (1+ (car active)))
250 (goto-char (point-min))))
251 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
252 (nconc rest articles))))
254 (deffoo nnmbox-request-move-article
255 (article group server accept-form &optional last)
256 (nnmbox-possibly-change-newsgroup group server)
257 (let ((buf (get-buffer-create " *nnmbox move*"))
260 (nnmbox-request-article article group server)
263 (buffer-disable-undo (current-buffer))
265 (insert-buffer-substring nntp-server-buffer)
266 (goto-char (point-min))
267 (while (re-search-forward
269 (save-excursion (search-forward "\n\n" nil t) (point)) t)
270 (delete-region (progn (beginning-of-line) (point))
271 (progn (forward-line 1) (point))))
272 (setq result (eval accept-form))
276 (set-buffer nnmbox-mbox-buffer)
277 (goto-char (point-min))
278 (if (search-forward (nnmbox-article-string article) nil t)
279 (nnmbox-delete-mail))
280 (and last (save-buffer))))
283 (deffoo nnmbox-request-accept-article (group &optional server last)
284 (nnmbox-possibly-change-newsgroup group server)
285 (let ((buf (current-buffer))
287 (goto-char (point-min))
288 (if (looking-at "X-From-Line: ")
289 (replace-match "From ")
290 (insert "From nobody " (current-time-string) "\n"))
292 (nnmail-activate 'nnmbox)
295 (goto-char (point-min))
296 (search-forward "\n\n" nil t)
298 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
299 (delete-region (point) (progn (forward-line 1) (point))))
300 (setq result (nnmbox-save-mail (and (stringp group) group))))
302 (set-buffer nnmbox-mbox-buffer)
303 (goto-char (point-max))
304 (insert-buffer-substring buf)
305 (and last (save-buffer))
307 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
310 (deffoo nnmbox-request-replace-article (article group buffer)
311 (nnmbox-possibly-change-newsgroup group)
313 (set-buffer nnmbox-mbox-buffer)
314 (goto-char (point-min))
315 (if (not (search-forward (nnmbox-article-string article) nil t))
317 (nnmbox-delete-mail t t)
318 (insert-buffer-substring buffer)
322 (deffoo nnmbox-request-delete-group (group &optional force server)
323 (nnmbox-possibly-change-newsgroup group server)
324 ;; Delete all articles in GROUP.
326 () ; Don't delete the articles.
328 (set-buffer nnmbox-mbox-buffer)
329 (goto-char (point-min))
330 ;; Delete all articles in this group.
331 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
333 (while (search-forward ident nil t)
335 (nnmbox-delete-mail))
336 (and found (save-buffer)))))
337 ;; Remove the group from all structures.
338 (setq nnmbox-group-alist
339 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
340 nnmbox-current-group nil)
341 ;; Save the active file.
342 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
345 (deffoo nnmbox-request-rename-group (group new-name &optional server)
346 (nnmbox-possibly-change-newsgroup group server)
348 (set-buffer nnmbox-mbox-buffer)
349 (goto-char (point-min))
350 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
351 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
353 (while (search-forward ident nil t)
354 (replace-match new-ident t t)
356 (and found (save-buffer))))
357 (let ((entry (assoc group nnmbox-group-alist)))
358 (and entry (setcar entry new-name))
359 (setq nnmbox-current-group nil)
360 ;; Save the new group alist.
361 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
365 ;;; Internal functions.
367 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
368 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
370 (defun nnmbox-delete-mail (&optional force leave-delim)
371 ;; Delete the current X-Gnus-Newsgroup line.
374 (progn (beginning-of-line) (point))
375 (progn (forward-line 1) (point))))
376 ;; Beginning of the article.
381 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
382 (if leave-delim (progn (forward-line 1) (point))
383 (match-beginning 0)))
386 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
388 (if (and (not (bobp)) leave-delim)
389 (progn (forward-line -2) (point))
390 (match-beginning 0)))
392 (goto-char (point-min))
393 ;; Only delete the article if no other groups owns it as well.
394 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
395 (delete-region (point-min) (point-max))))))
397 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
399 (not (nnmbox-server-opened server)))
400 (nnmbox-open-server server))
401 (if (or (not nnmbox-mbox-buffer)
402 (not (buffer-name nnmbox-mbox-buffer)))
404 (set-buffer (setq nnmbox-mbox-buffer
405 (nnheader-find-file-noselect
406 nnmbox-mbox-file nil 'raw)))
407 (buffer-disable-undo (current-buffer))))
408 (if (not nnmbox-group-alist)
409 (nnmail-activate 'nnmbox))
411 (if (assoc newsgroup nnmbox-group-alist)
412 (setq nnmbox-current-group newsgroup))
415 (defun nnmbox-article-string (article)
416 (if (numberp article)
417 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
418 (int-to-string article) " ")
419 (concat "\nMessage-ID: " article)))
421 (defun nnmbox-article-group-number ()
423 (goto-char (point-min))
424 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
426 (cons (buffer-substring (match-beginning 1) (match-end 1))
428 (buffer-substring (match-beginning 2) (match-end 2)))))))
430 (defun nnmbox-save-mail (&optional group)
431 "Called narrowed to an article."
432 (let* ((nnmail-split-methods
433 (if group (list (list group "")) nnmail-split-methods))
434 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number)))
435 (delim (concat "^" rmail-unix-mail-delimiter)))
436 (goto-char (point-min))
437 ;; This might come from somewhere else.
438 (unless (looking-at delim)
439 (insert "From nobody " (current-time-string) "\n")
440 (goto-char (point-min)))
441 ;; Quote all "From " lines in the article.
443 (while (re-search-forward delim nil t)
446 (nnmail-insert-lines)
447 (nnmail-insert-xref group-art)
448 (nnmbox-insert-newsgroup-line group-art)
449 (run-hooks 'nnmbox-prepare-save-mail-hook)
452 (defun nnmbox-insert-newsgroup-line (group-art)
454 (goto-char (point-min))
455 (if (search-forward "\n\n" nil t)
459 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
460 (caar group-art) (cdar group-art)
461 (current-time-string)))
462 (setq group-art (cdr group-art)))))
465 (defun nnmbox-active-number (group)
466 ;; Find the next article number in GROUP.
467 (let ((active (cadr (assoc group nnmbox-group-alist))))
469 (setcdr active (1+ (cdr active)))
470 ;; This group is new, so we create a new entry for it.
471 ;; This might be a bit naughty... creating groups on the drop of
472 ;; a hat, but I don't know...
473 (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
474 nnmbox-group-alist)))
477 (defun nnmbox-read-mbox ()
478 (nnmail-activate 'nnmbox)
479 (if (not (file-exists-p nnmbox-mbox-file))
480 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
481 (if (and nnmbox-mbox-buffer
482 (buffer-name nnmbox-mbox-buffer)
484 (set-buffer nnmbox-mbox-buffer)
485 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
488 (let ((delim (concat "^" rmail-unix-mail-delimiter))
489 (alist nnmbox-group-alist)
491 (set-buffer (setq nnmbox-mbox-buffer
492 (nnheader-find-file-noselect
493 nnmbox-mbox-file nil 'raw)))
494 (buffer-disable-undo (current-buffer))
496 ;; Go through the group alist and compare against
499 (goto-char (point-max))
500 (when (and (re-search-backward
501 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
506 (match-beginning 1) (match-end 1))))
508 (setcdr (cadar alist) (1+ number)))
509 (setq alist (cdr alist)))
511 (goto-char (point-min))
512 (while (re-search-forward delim nil t)
513 (setq start (match-beginning 0))
514 (if (not (search-forward "\nX-Gnus-Newsgroup: "
519 (re-search-forward delim nil t)
525 (narrow-to-region start end)
526 (nnmbox-save-mail))))
531 ;;; nnmbox.el ends here