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 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 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)
146 (setq nnmbox-current-server nil)
149 (defun nnmbox-server-opened (&optional server)
150 (and (equal server nnmbox-current-server)
152 (buffer-name nnmbox-mbox-buffer)
154 (buffer-name nntp-server-buffer)))
156 (defun nnmbox-status-message (&optional server)
157 nnmbox-status-string)
159 (defun nnmbox-request-article (article &optional newsgroup server buffer)
160 (nnmbox-possibly-change-newsgroup newsgroup)
162 (set-buffer nnmbox-mbox-buffer)
163 (goto-char (point-min))
164 (if (search-forward (nnmbox-article-string article) nil t)
166 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
169 (or (and (re-search-forward
170 (concat "^" rmail-unix-mail-delimiter) nil t)
172 (goto-char (point-max)))
174 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
175 (set-buffer nntp-server-buffer)
177 (insert-buffer-substring nnmbox-mbox-buffer start stop)
178 (goto-char (point-min))
179 (while (looking-at "From ")
181 (insert "X-From-Line: ")
183 (if (numberp article)
184 (cons nnmbox-current-group article)
185 (nnmbox-article-group-number)))))))
187 (defun nnmbox-request-group (group &optional server dont-check)
189 (if (nnmbox-possibly-change-newsgroup group)
193 (set-buffer nntp-server-buffer)
195 (let ((active (assoc group nnmbox-group-alist)))
196 (insert (format "211 %d %d %d %s\n"
197 (1+ (- (cdr (car (cdr active)))
198 (car (car (cdr active)))))
199 (car (car (cdr active)))
200 (cdr (car (cdr active)))
204 (defun nnmbox-request-scan (&optional group server)
210 (set-buffer nnmbox-mbox-buffer)
212 nnmbox-mbox-file group
215 (let ((in-buf (current-buffer)))
216 (set-buffer nnmbox-mbox-buffer)
217 (goto-char (point-max))
218 (insert-buffer-substring in-buf))))))
220 (defun nnmbox-close-group (group &optional server)
223 (defun nnmbox-request-list (&optional server)
225 (or (nnmail-find-file nnmbox-active-file)
227 (setq nnmbox-group-alist (nnmail-get-active))
228 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
229 (nnmail-find-file nnmbox-active-file)))))
231 (defun nnmbox-request-newgroups (date &optional server)
232 (nnmbox-request-list server))
234 (defun nnmbox-request-list-newsgroups (&optional server)
235 (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
238 (defun nnmbox-request-post (&optional server)
239 (mail-send-and-exit nil))
241 (defun nnmbox-request-expire-articles
242 (articles newsgroup &optional server force)
243 (nnmbox-possibly-change-newsgroup newsgroup)
244 (let* ((days (or (and nnmail-expiry-wait-function
245 (funcall nnmail-expiry-wait-function newsgroup))
249 (nnmail-activate 'nnmbox)
252 (set-buffer nnmbox-mbox-buffer)
253 (while (and articles is-old)
254 (goto-char (point-min))
255 (if (search-forward (nnmbox-article-string (car articles)) nil t)
258 (> (nnmail-days-between
259 (current-time-string)
261 (point) (progn (end-of-line) (point))))
264 (and gnus-verbose-backends
265 (message "Deleting article %d in %s..."
266 (car articles) newsgroup))
267 (nnmbox-delete-mail))
268 (setq rest (cons (car articles) rest))))
269 (setq articles (cdr articles)))
271 ;; Find the lowest active article in this group.
272 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
273 (goto-char (point-min))
274 (while (and (not (search-forward
275 (nnmbox-article-string (car active)) nil t))
276 (<= (car active) (cdr active)))
277 (setcar active (1+ (car active)))
278 (goto-char (point-min))))
279 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
280 (nconc rest articles))))
282 (defun nnmbox-request-move-article
283 (article group server accept-form &optional last)
284 (nnmbox-possibly-change-newsgroup group)
285 (let ((buf (get-buffer-create " *nnmbox move*"))
288 (nnmbox-request-article article group server)
291 (buffer-disable-undo (current-buffer))
293 (insert-buffer-substring nntp-server-buffer)
294 (goto-char (point-min))
295 (while (re-search-forward
297 (save-excursion (search-forward "\n\n" nil t) (point)) t)
298 (delete-region (progn (beginning-of-line) (point))
299 (progn (forward-line 1) (point))))
300 (setq result (eval accept-form))
304 (set-buffer nnmbox-mbox-buffer)
305 (goto-char (point-min))
306 (if (search-forward (nnmbox-article-string article) nil t)
307 (nnmbox-delete-mail))
308 (and last (save-buffer))))
311 (defun nnmbox-request-accept-article (group &optional last)
312 (let ((buf (current-buffer))
314 (goto-char (point-min))
315 (if (looking-at "X-From-Line: ")
316 (replace-match "From ")
317 (insert "From nobody " (current-time-string) "\n"))
319 (nnmail-activate 'nnmbox)
322 (goto-char (point-min))
323 (search-forward "\n\n" nil t)
325 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
326 (delete-region (point) (progn (forward-line 1) (point))))
327 (setq result (nnmbox-save-mail (and (stringp group) group))))
329 (set-buffer nnmbox-mbox-buffer)
330 (insert-buffer-substring buf)
331 (and last (save-buffer))
333 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
336 (defun nnmbox-request-replace-article (article group buffer)
337 (nnmbox-possibly-change-newsgroup group)
339 (set-buffer nnmbox-mbox-buffer)
340 (goto-char (point-min))
341 (if (not (search-forward (nnmbox-article-string article) nil t))
343 (nnmbox-delete-mail t t)
344 (insert-buffer-substring buffer)
348 (defun nnmbox-request-delete-group (group &optional force server)
349 (nnmbox-possibly-change-newsgroup group)
350 ;; Delete all articles in GROUP.
352 () ; Don't delete the articles.
354 (set-buffer nnmbox-mbox-buffer)
355 (goto-char (point-min))
356 ;; Delete all articles in this group.
357 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
359 (while (search-forward ident nil t)
361 (nnmbox-delete-mail))
362 (and found (save-buffer)))))
363 ;; Remove the group from all structures.
364 (setq nnmbox-group-alist
365 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
366 nnmbox-current-group nil)
367 ;; Save the active file.
368 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
371 (defun nnmbox-request-rename-group (group new-name &optional server)
372 (nnmbox-possibly-change-newsgroup group)
374 (set-buffer nnmbox-mbox-buffer)
375 (goto-char (point-min))
376 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
377 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
379 (while (search-forward ident nil t)
380 (replace-match new-ident t t)
382 (and found (save-buffer))))
383 (let ((entry (assoc group nnmbox-group-alist)))
384 (and entry (setcar entry new-name))
385 (setq nnmbox-current-group nil)
386 ;; Save the new group alist.
387 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
391 ;;; Internal functions.
393 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
394 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
396 (defun nnmbox-delete-mail (&optional force leave-delim)
397 ;; Delete the current X-Gnus-Newsgroup line.
400 (progn (beginning-of-line) (point))
401 (progn (forward-line 1) (point))))
402 ;; Beginning of the article.
407 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
408 (if leave-delim (progn (forward-line 1) (point))
409 (match-beginning 0)))
412 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
414 (if (and (not (bobp)) leave-delim)
415 (progn (forward-line -2) (point))
416 (match-beginning 0)))
418 (goto-char (point-min))
419 ;; Only delete the article if no other groups owns it as well.
420 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
421 (delete-region (point-min) (point-max))))))
423 (defun nnmbox-possibly-change-newsgroup (newsgroup)
424 (if (or (not nnmbox-mbox-buffer)
425 (not (buffer-name nnmbox-mbox-buffer)))
427 (set-buffer (setq nnmbox-mbox-buffer
428 (nnheader-find-file-noselect
429 nnmbox-mbox-file nil 'raw)))
430 (buffer-disable-undo (current-buffer))))
431 (if (not nnmbox-group-alist)
432 (nnmail-activate 'nnmbox))
434 (if (assoc newsgroup nnmbox-group-alist)
435 (setq nnmbox-current-group newsgroup))))
437 (defun nnmbox-article-string (article)
438 (if (numberp article)
439 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
440 (int-to-string article) " ")
441 (concat "\nMessage-ID: " article)))
443 (defun nnmbox-article-group-number ()
445 (goto-char (point-min))
446 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
448 (cons (buffer-substring (match-beginning 1) (match-end 1))
450 (buffer-substring (match-beginning 2) (match-end 2)))))))
452 (defun nnmbox-save-mail (&optional group)
453 "Called narrowed to an article."
454 (let* ((nnmail-split-methods
455 (if group (list (list group "")) nnmail-split-methods))
456 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number)))
457 (delim (concat "^" rmail-unix-mail-delimiter)))
458 (goto-char (point-min))
459 ;; This might come from somewhere else.
460 (unless (looking-at delim)
461 (insert "From nobody " (current-time-string) "\n")
462 (goto-char (point-min)))
463 ;; Quote all "From " lines in the article.
465 (while (re-search-forward delim nil t)
468 (nnmail-insert-lines)
469 (nnmail-insert-xref group-art)
470 (nnmbox-insert-newsgroup-line group-art)
471 (run-hooks 'nnmbox-prepare-save-mail-hook)
474 (defun nnmbox-insert-newsgroup-line (group-art)
476 (goto-char (point-min))
477 (if (search-forward "\n\n" nil t)
481 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
482 (car (car group-art)) (cdr (car group-art))
483 (current-time-string)))
484 (setq group-art (cdr group-art)))))
487 (defun nnmbox-active-number (group)
488 ;; Find the next article number in GROUP.
489 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
491 (setcdr active (1+ (cdr active)))
492 ;; This group is new, so we create a new entry for it.
493 ;; This might be a bit naughty... creating groups on the drop of
494 ;; a hat, but I don't know...
495 (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
496 nnmbox-group-alist)))
499 (defun nnmbox-read-mbox ()
500 (nnmail-activate 'nnmbox)
501 (if (not (file-exists-p nnmbox-mbox-file))
502 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
503 (if (and nnmbox-mbox-buffer
504 (buffer-name nnmbox-mbox-buffer)
506 (set-buffer nnmbox-mbox-buffer)
507 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
510 (let ((delim (concat "^" rmail-unix-mail-delimiter))
512 (set-buffer (setq nnmbox-mbox-buffer
513 (nnheader-find-file-noselect
514 nnmbox-mbox-file nil 'raw)))
515 (buffer-disable-undo (current-buffer))
516 (goto-char (point-min))
517 (while (re-search-forward delim nil t)
518 (setq start (match-beginning 0))
519 (if (not (search-forward "\nX-Gnus-Newsgroup: "
524 (re-search-forward delim nil t)
530 (narrow-to-region start end)
531 (nnmbox-save-mail))))
536 ;;; nnmbox.el ends here