1 ;;; nnmbox.el --- mail mbox access for Gnus
2 ;; Copyright (C) 1995 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)
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 gnus-verbose-backends
113 (message "nnmbox: Receiving headers... %d%%"
114 (/ (* count 100) number))))
116 (and (numberp nnmail-large-newsgroup)
117 (> number nnmail-large-newsgroup)
118 gnus-verbose-backends
119 (message "nnmbox: Receiving headers...done"))
121 ;; Fold continuation lines.
122 (set-buffer nntp-server-buffer)
123 (goto-char (point-min))
124 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
125 (replace-match " " t t))
128 (defun nnmbox-open-server (server &optional defs)
129 (nnheader-init-server-buffer)
130 (if (equal server nnmbox-current-server)
132 (if nnmbox-current-server
133 (setq nnmbox-server-alist
134 (cons (list nnmbox-current-server
135 (nnheader-save-variables nnmbox-server-variables))
136 nnmbox-server-alist)))
137 (let ((state (assoc server nnmbox-server-alist)))
140 (nnheader-restore-variables (nth 1 state))
141 (setq nnmbox-server-alist (delq state nnmbox-server-alist)))
142 (nnheader-set-init-variables nnmbox-server-variables defs)))
143 (setq nnmbox-current-server server)))
145 (defun nnmbox-close-server (&optional server)
148 (defun nnmbox-server-opened (&optional server)
149 (and (equal server nnmbox-current-server)
151 (buffer-name nnmbox-mbox-buffer)
153 (buffer-name nntp-server-buffer)))
155 (defun nnmbox-status-message (&optional server)
156 nnmbox-status-string)
158 (defun nnmbox-request-article (article &optional newsgroup server buffer)
159 (nnmbox-possibly-change-newsgroup newsgroup)
161 (set-buffer nnmbox-mbox-buffer)
162 (goto-char (point-min))
163 (if (search-forward (nnmbox-article-string article) nil t)
165 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
168 (or (and (re-search-forward
169 (concat "^" rmail-unix-mail-delimiter) nil t)
171 (goto-char (point-max)))
173 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
174 (set-buffer nntp-server-buffer)
176 (insert-buffer-substring nnmbox-mbox-buffer start stop)
177 (goto-char (point-min))
178 (while (looking-at "From ")
180 (insert "X-From-Line: ")
182 (if (numberp article)
183 (cons nnmbox-current-group article)
184 (nnmbox-article-group-number)))))))
186 (defun nnmbox-request-group (group &optional server dont-check)
188 (if (nnmbox-possibly-change-newsgroup group)
192 (set-buffer nntp-server-buffer)
194 (let ((active (assoc group nnmbox-group-alist)))
195 (insert (format "211 %d %d %d %s\n"
196 (1+ (- (cdr (car (cdr active)))
197 (car (car (cdr active)))))
198 (car (car (cdr active)))
199 (cdr (car (cdr active)))
203 (defun nnmbox-request-scan (&optional group server)
209 (set-buffer nnmbox-mbox-buffer)
211 nnmbox-mbox-file group
214 (let ((in-buf (current-buffer)))
215 (set-buffer nnmbox-mbox-buffer)
216 (goto-char (point-max))
217 (insert-buffer-substring in-buf))))))
219 (defun nnmbox-close-group (group &optional server)
222 (defun nnmbox-request-list (&optional server)
224 (or (nnmail-find-file nnmbox-active-file)
226 (setq nnmbox-group-alist (nnmail-get-active))
227 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
228 (nnmail-find-file nnmbox-active-file)))))
230 (defun nnmbox-request-newgroups (date &optional server)
231 (nnmbox-request-list server))
233 (defun nnmbox-request-list-newsgroups (&optional server)
234 (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
237 (defun nnmbox-request-post (&optional server)
238 (mail-send-and-exit nil))
240 (defalias 'nnmbox-request-post-buffer 'nnmail-request-post-buffer)
242 (defun nnmbox-request-expire-articles
243 (articles newsgroup &optional server force)
244 (nnmbox-possibly-change-newsgroup newsgroup)
245 (let* ((days (or (and nnmail-expiry-wait-function
246 (funcall nnmail-expiry-wait-function newsgroup))
250 (nnmail-activate 'nnmbox)
253 (set-buffer nnmbox-mbox-buffer)
254 (while (and articles is-old)
255 (goto-char (point-min))
256 (if (search-forward (nnmbox-article-string (car articles)) nil t)
259 (> (nnmail-days-between
260 (current-time-string)
262 (point) (progn (end-of-line) (point))))
265 (and gnus-verbose-backends
266 (message "Deleting article %d in %s..."
267 (car articles) newsgroup))
268 (nnmbox-delete-mail))
269 (setq rest (cons (car articles) rest))))
270 (setq articles (cdr articles)))
272 ;; Find the lowest active article in this group.
273 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
274 (goto-char (point-min))
275 (while (and (not (search-forward
276 (nnmbox-article-string (car active)) nil t))
277 (<= (car active) (cdr active)))
278 (setcar active (1+ (car active)))
279 (goto-char (point-min))))
280 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
281 (nconc rest articles))))
283 (defun nnmbox-request-move-article
284 (article group server accept-form &optional last)
285 (nnmbox-possibly-change-newsgroup group)
286 (let ((buf (get-buffer-create " *nnmbox move*"))
289 (nnmbox-request-article article group server)
292 (buffer-disable-undo (current-buffer))
294 (insert-buffer-substring nntp-server-buffer)
295 (goto-char (point-min))
296 (while (re-search-forward
298 (save-excursion (search-forward "\n\n" nil t) (point)) t)
299 (delete-region (progn (beginning-of-line) (point))
300 (progn (forward-line 1) (point))))
301 (setq result (eval accept-form))
305 (set-buffer nnmbox-mbox-buffer)
306 (goto-char (point-min))
307 (if (search-forward (nnmbox-article-string article) nil t)
308 (nnmbox-delete-mail))
309 (and last (save-buffer))))
312 (defun nnmbox-request-accept-article (group &optional last)
313 (let ((buf (current-buffer))
315 (goto-char (point-min))
316 (if (looking-at "X-From-Line: ")
317 (replace-match "From ")
318 (insert "From nobody " (current-time-string) "\n"))
320 (nnmail-activate 'nnmbox)
323 (goto-char (point-min))
324 (search-forward "\n\n" nil t)
326 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
327 (delete-region (point) (progn (forward-line 1) (point))))
328 (setq result (nnmbox-save-mail (and (stringp group) group))))
330 (set-buffer nnmbox-mbox-buffer)
331 (insert-buffer-substring buf)
332 (and last (save-buffer))
334 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
337 (defun nnmbox-request-replace-article (article group buffer)
338 (nnmbox-possibly-change-newsgroup group)
340 (set-buffer nnmbox-mbox-buffer)
341 (goto-char (point-min))
342 (if (not (search-forward (nnmbox-article-string article) nil t))
344 (nnmbox-delete-mail t t)
345 (insert-buffer-substring buffer)
350 ;;; Internal functions.
352 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
353 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
355 (defun nnmbox-delete-mail (&optional force leave-delim)
356 ;; Delete the current X-Gnus-Newsgroup line.
359 (progn (beginning-of-line) (point))
360 (progn (forward-line 1) (point))))
361 ;; Beginning of the article.
366 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
367 (if leave-delim (progn (forward-line 1) (point))
368 (match-beginning 0)))
371 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
373 (if (and (not (bobp)) leave-delim)
374 (progn (forward-line -2) (point))
375 (match-beginning 0)))
377 (goto-char (point-min))
378 ;; Only delete the article if no other groups owns it as well.
379 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
380 (delete-region (point-min) (point-max))))))
382 (defun nnmbox-possibly-change-newsgroup (newsgroup)
383 (if (or (not nnmbox-mbox-buffer)
384 (not (buffer-name nnmbox-mbox-buffer)))
386 (set-buffer (setq nnmbox-mbox-buffer
387 (nnheader-find-file-noselect
388 nnmbox-mbox-file nil 'raw)))
389 (buffer-disable-undo (current-buffer))))
390 (if (not nnmbox-group-alist)
391 (nnmail-activate 'nnmbox))
393 (if (assoc newsgroup nnmbox-group-alist)
394 (setq nnmbox-current-group newsgroup))))
396 (defun nnmbox-article-string (article)
397 (if (numberp article)
398 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
399 (int-to-string article) " ")
400 (concat "\nMessage-ID: " article)))
402 (defun nnmbox-article-group-number ()
404 (goto-char (point-min))
405 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
407 (cons (buffer-substring (match-beginning 1) (match-end 1))
409 (buffer-substring (match-beginning 2) (match-end 2)))))))
411 (defun nnmbox-save-mail (&optional group)
412 "Called narrowed to an article."
413 (let* ((nnmail-split-methods
414 (if group (list (list group "")) nnmail-split-methods))
415 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
416 (nnmail-insert-lines)
417 (nnmail-insert-xref group-art)
418 (nnmbox-insert-newsgroup-line group-art)
419 (run-hooks 'nnml-prepare-save-mail-hook)
422 (defun nnmbox-insert-newsgroup-line (group-art)
424 (goto-char (point-min))
425 (if (search-forward "\n\n" nil t)
429 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
430 (car (car group-art)) (cdr (car group-art))
431 (current-time-string)))
432 (setq group-art (cdr group-art)))))
435 (defun nnmbox-active-number (group)
436 ;; Find the next article number in GROUP.
437 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
439 (setcdr active (1+ (cdr active)))
440 ;; This group is new, so we create a new entry for it.
441 ;; This might be a bit naughty... creating groups on the drop of
442 ;; a hat, but I don't know...
443 (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
444 nnmbox-group-alist)))
447 (defun nnmbox-read-mbox ()
448 (nnmail-activate 'nnmbox)
449 (if (not (file-exists-p nnmbox-mbox-file))
450 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
451 (if (and nnmbox-mbox-buffer
452 (buffer-name nnmbox-mbox-buffer)
454 (set-buffer nnmbox-mbox-buffer)
455 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
458 (let ((delim (concat "^" rmail-unix-mail-delimiter))
460 (set-buffer (setq nnmbox-mbox-buffer
461 (nnheader-find-file-noselect
462 nnmbox-mbox-file nil 'raw)))
463 (buffer-disable-undo (current-buffer))
464 (goto-char (point-min))
465 (while (re-search-forward delim nil t)
466 (setq start (match-beginning 0))
467 (if (not (search-forward "\nX-Gnus-Newsgroup: "
472 (re-search-forward delim nil t)
478 (narrow-to-region start end)
479 (nnmbox-save-mail))))
484 ;;; nnmbox.el ends here