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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; For an overview of what the interface functions do, please see the
35 (defvar nnmbox-mbox-file (expand-file-name "~/mbox")
36 "The name of the mail box file in the user's home directory.")
38 (defvar nnmbox-active-file (expand-file-name "~/.mbox-active")
39 "The name of the active file for the mail box.")
41 (defvar nnmbox-get-new-mail t
42 "If non-nil, nnmbox will check the incoming mail file and split the mail.")
44 (defvar nnmbox-prepare-save-mail-hook nil
45 "Hook run narrowed to an article before saving.")
49 (defconst nnmbox-version "nnmbox 1.0"
52 (defvar nnmbox-current-group nil
53 "Current nnmbox news group directory.")
55 (defconst nnmbox-mbox-buffer nil)
57 (defvar nnmbox-status-string "")
59 (defvar nnmbox-group-alist nil)
60 (defvar nnmbox-active-timestamp nil)
64 (defvar nnmbox-current-server nil)
65 (defvar nnmbox-server-alist nil)
66 (defvar nnmbox-server-variables
68 (list 'nnmbox-mbox-file nnmbox-mbox-file)
69 (list 'nnmbox-active-file nnmbox-active-file)
70 (list 'nnmbox-get-new-mail nnmbox-get-new-mail)
71 '(nnmbox-current-group nil)
72 '(nnmbox-status-string "")
73 '(nnmbox-group-alist nil)))
77 ;;; Interface functions
79 (defun nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
81 (set-buffer nntp-server-buffer)
83 (let ((number (length sequence))
85 article art-string start stop)
86 (nnmbox-possibly-change-newsgroup newsgroup)
88 (setq article (car sequence))
89 (setq art-string (nnmbox-article-string article))
90 (set-buffer nnmbox-mbox-buffer)
91 (if (or (search-forward art-string nil t)
92 (progn (goto-char (point-min))
93 (search-forward art-string nil t)))
98 (concat "^" rmail-unix-mail-delimiter) nil t)
100 (search-forward "\n\n" nil t)
101 (setq stop (1- (point)))
102 (set-buffer nntp-server-buffer)
103 (insert (format "221 %d Article retrieved.\n" article))
104 (insert-buffer-substring nnmbox-mbox-buffer start stop)
105 (goto-char (point-max))
107 (setq sequence (cdr sequence))
108 (setq count (1+ count))
109 (and (numberp nnmail-large-newsgroup)
110 (> number nnmail-large-newsgroup)
112 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
113 (/ (* count 100) number))))
115 (and (numberp nnmail-large-newsgroup)
116 (> number nnmail-large-newsgroup)
117 (nnheader-message 5 "nnmbox: Receiving headers...done"))
119 (set-buffer nntp-server-buffer)
120 (nnheader-fold-continuation-lines)
123 (defun nnmbox-open-server (server &optional defs)
124 (nnheader-init-server-buffer)
125 (if (equal server nnmbox-current-server)
127 (if nnmbox-current-server
128 (setq nnmbox-server-alist
129 (cons (list nnmbox-current-server
130 (nnheader-save-variables nnmbox-server-variables))
131 nnmbox-server-alist)))
132 (let ((state (assoc server nnmbox-server-alist)))
135 (nnheader-restore-variables (nth 1 state))
136 (setq nnmbox-server-alist (delq state nnmbox-server-alist)))
137 (nnheader-set-init-variables nnmbox-server-variables defs)))
138 (setq nnmbox-current-server server)))
140 (defun nnmbox-close-server (&optional server)
141 (setq nnmbox-current-server nil)
144 (defun nnmbox-server-opened (&optional server)
145 (and (equal server nnmbox-current-server)
147 (buffer-name nnmbox-mbox-buffer)
149 (buffer-name nntp-server-buffer)))
151 (defun nnmbox-status-message (&optional server)
152 nnmbox-status-string)
154 (defun nnmbox-request-article (article &optional newsgroup server buffer)
155 (nnmbox-possibly-change-newsgroup newsgroup)
157 (set-buffer nnmbox-mbox-buffer)
158 (goto-char (point-min))
159 (if (search-forward (nnmbox-article-string article) nil t)
161 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
164 (or (and (re-search-forward
165 (concat "^" rmail-unix-mail-delimiter) nil t)
167 (goto-char (point-max)))
169 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
170 (set-buffer nntp-server-buffer)
172 (insert-buffer-substring nnmbox-mbox-buffer start stop)
173 (goto-char (point-min))
174 (while (looking-at "From ")
176 (insert "X-From-Line: ")
178 (if (numberp article)
179 (cons nnmbox-current-group article)
180 (nnmbox-article-group-number)))))))
182 (defun nnmbox-request-group (group &optional server dont-check)
183 (let ((active (assoc group nnmbox-group-alist)))
185 ((null (nnmbox-possibly-change-newsgroup group))
186 (nnheader-report 'nnmbox "No such group: %s" group))
188 (nnheader-report 'nnmbox "Selected group %s" group)
189 (nnheader-insert ""))
190 ((> (car active) (cdr active))
191 (nnheader-report 'nnmbox "Empty group %s" group)
192 (nnheader-insert "211 0 0 0 %s\n" group))
194 (nnheader-report 'nnmbox "Selected group %s" group)
197 (1+ (- (cdadr active) (caadr active)))
198 (cadr active) (cdadr active) (car active))
201 (defun nnmbox-request-scan (&optional group server)
207 (set-buffer nnmbox-mbox-buffer)
209 nnmbox-mbox-file group
212 (let ((in-buf (current-buffer)))
213 (set-buffer nnmbox-mbox-buffer)
214 (goto-char (point-max))
215 (insert-buffer-substring in-buf))))))
217 (defun nnmbox-close-group (group &optional server)
220 (defun nnmbox-request-list (&optional server)
222 (or (nnmail-find-file nnmbox-active-file)
224 (setq nnmbox-group-alist (nnmail-get-active))
225 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
226 (nnmail-find-file nnmbox-active-file)))))
228 (defun nnmbox-request-newgroups (date &optional server)
229 (nnmbox-request-list server))
231 (defun nnmbox-request-list-newsgroups (&optional server)
232 (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
235 (defun nnmbox-request-post (&optional server)
236 (mail-send-and-exit nil))
238 (defun nnmbox-request-expire-articles
239 (articles newsgroup &optional server force)
240 (nnmbox-possibly-change-newsgroup newsgroup)
243 (nnmail-activate 'nnmbox)
246 (set-buffer nnmbox-mbox-buffer)
247 (while (and articles is-old)
248 (goto-char (point-min))
249 (if (search-forward (nnmbox-article-string (car articles)) nil t)
251 (nnmail-expired-article-p
254 (point) (progn (end-of-line) (point))) force))
256 (nnheader-message 5 "Deleting article %d in %s..."
257 (car articles) newsgroup)
258 (nnmbox-delete-mail))
259 (setq rest (cons (car articles) rest))))
260 (setq articles (cdr articles)))
262 ;; Find the lowest active article in this group.
263 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
264 (goto-char (point-min))
265 (while (and (not (search-forward
266 (nnmbox-article-string (car active)) nil t))
267 (<= (car active) (cdr active)))
268 (setcar active (1+ (car active)))
269 (goto-char (point-min))))
270 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
271 (nconc rest articles))))
273 (defun nnmbox-request-move-article
274 (article group server accept-form &optional last)
275 (nnmbox-possibly-change-newsgroup group)
276 (let ((buf (get-buffer-create " *nnmbox move*"))
279 (nnmbox-request-article article group server)
282 (buffer-disable-undo (current-buffer))
284 (insert-buffer-substring nntp-server-buffer)
285 (goto-char (point-min))
286 (while (re-search-forward
288 (save-excursion (search-forward "\n\n" nil t) (point)) t)
289 (delete-region (progn (beginning-of-line) (point))
290 (progn (forward-line 1) (point))))
291 (setq result (eval accept-form))
295 (set-buffer nnmbox-mbox-buffer)
296 (goto-char (point-min))
297 (if (search-forward (nnmbox-article-string article) nil t)
298 (nnmbox-delete-mail))
299 (and last (save-buffer))))
302 (defun nnmbox-request-accept-article (group &optional last)
303 (let ((buf (current-buffer))
305 (goto-char (point-min))
306 (if (looking-at "X-From-Line: ")
307 (replace-match "From ")
308 (insert "From nobody " (current-time-string) "\n"))
310 (nnmail-activate 'nnmbox)
313 (goto-char (point-min))
314 (search-forward "\n\n" nil t)
316 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
317 (delete-region (point) (progn (forward-line 1) (point))))
318 (setq result (nnmbox-save-mail (and (stringp group) group))))
320 (set-buffer nnmbox-mbox-buffer)
321 (insert-buffer-substring buf)
322 (and last (save-buffer))
324 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
327 (defun nnmbox-request-replace-article (article group buffer)
328 (nnmbox-possibly-change-newsgroup group)
330 (set-buffer nnmbox-mbox-buffer)
331 (goto-char (point-min))
332 (if (not (search-forward (nnmbox-article-string article) nil t))
334 (nnmbox-delete-mail t t)
335 (insert-buffer-substring buffer)
339 (defun nnmbox-request-delete-group (group &optional force server)
340 (nnmbox-possibly-change-newsgroup group)
341 ;; Delete all articles in GROUP.
343 () ; Don't delete the articles.
345 (set-buffer nnmbox-mbox-buffer)
346 (goto-char (point-min))
347 ;; Delete all articles in this group.
348 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
350 (while (search-forward ident nil t)
352 (nnmbox-delete-mail))
353 (and found (save-buffer)))))
354 ;; Remove the group from all structures.
355 (setq nnmbox-group-alist
356 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
357 nnmbox-current-group nil)
358 ;; Save the active file.
359 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
362 (defun nnmbox-request-rename-group (group new-name &optional server)
363 (nnmbox-possibly-change-newsgroup group)
365 (set-buffer nnmbox-mbox-buffer)
366 (goto-char (point-min))
367 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
368 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
370 (while (search-forward ident nil t)
371 (replace-match new-ident t t)
373 (and found (save-buffer))))
374 (let ((entry (assoc group nnmbox-group-alist)))
375 (and entry (setcar entry new-name))
376 (setq nnmbox-current-group nil)
377 ;; Save the new group alist.
378 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
382 ;;; Internal functions.
384 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
385 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
387 (defun nnmbox-delete-mail (&optional force leave-delim)
388 ;; Delete the current X-Gnus-Newsgroup line.
391 (progn (beginning-of-line) (point))
392 (progn (forward-line 1) (point))))
393 ;; Beginning of the article.
398 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
399 (if leave-delim (progn (forward-line 1) (point))
400 (match-beginning 0)))
403 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
405 (if (and (not (bobp)) leave-delim)
406 (progn (forward-line -2) (point))
407 (match-beginning 0)))
409 (goto-char (point-min))
410 ;; Only delete the article if no other groups owns it as well.
411 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
412 (delete-region (point-min) (point-max))))))
414 (defun nnmbox-possibly-change-newsgroup (newsgroup)
415 (if (or (not nnmbox-mbox-buffer)
416 (not (buffer-name nnmbox-mbox-buffer)))
418 (set-buffer (setq nnmbox-mbox-buffer
419 (find-file-noselect nnmbox-mbox-file nil 'raw)))
420 (buffer-disable-undo (current-buffer))))
421 (if (not nnmbox-group-alist)
422 (nnmail-activate 'nnmbox))
424 (if (assoc newsgroup nnmbox-group-alist)
425 (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 (and (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 (&optional group)
443 "Called narrowed to an article."
444 (let* ((nnmail-split-methods
445 (if group (list (list group "")) nnmail-split-methods))
446 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number)))
447 (delim (concat "^" rmail-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 'nnmbox-prepare-save-mail-hook)
464 (defun nnmbox-insert-newsgroup-line (group-art)
466 (goto-char (point-min))
467 (if (search-forward "\n\n" nil t)
471 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
472 (car (car group-art)) (cdr (car 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 (car (cdr (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 (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
486 nnmbox-group-alist)))
489 (defun nnmbox-read-mbox ()
490 (nnmail-activate 'nnmbox)
491 (if (not (file-exists-p nnmbox-mbox-file))
492 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
493 (if (and nnmbox-mbox-buffer
494 (buffer-name nnmbox-mbox-buffer)
496 (set-buffer nnmbox-mbox-buffer)
497 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
500 (let ((delim (concat "^" rmail-unix-mail-delimiter))
502 (set-buffer (setq nnmbox-mbox-buffer
503 (find-file-noselect nnmbox-mbox-file nil 'raw)))
504 (buffer-disable-undo (current-buffer))
505 (goto-char (point-min))
506 (while (re-search-forward delim nil t)
507 (setq start (match-beginning 0))
508 (if (not (search-forward "\nX-Gnus-Newsgroup: "
513 (re-search-forward delim nil t)
519 (narrow-to-region start end)
520 (nnmbox-save-mail))))
525 ;;; nnmbox.el ends here