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 (set-buffer nntp-server-buffer)
122 (nnheader-fold-continuation-lines)
125 (defun nnmbox-open-server (server &optional defs)
126 (nnheader-init-server-buffer)
127 (if (equal server nnmbox-current-server)
129 (if nnmbox-current-server
130 (setq nnmbox-server-alist
131 (cons (list nnmbox-current-server
132 (nnheader-save-variables nnmbox-server-variables))
133 nnmbox-server-alist)))
134 (let ((state (assoc server nnmbox-server-alist)))
137 (nnheader-restore-variables (nth 1 state))
138 (setq nnmbox-server-alist (delq state nnmbox-server-alist)))
139 (nnheader-set-init-variables nnmbox-server-variables defs)))
140 (setq nnmbox-current-server server)))
142 (defun nnmbox-close-server (&optional server)
143 (setq nnmbox-current-server nil)
146 (defun nnmbox-server-opened (&optional server)
147 (and (equal server nnmbox-current-server)
149 (buffer-name nnmbox-mbox-buffer)
151 (buffer-name nntp-server-buffer)))
153 (defun nnmbox-status-message (&optional server)
154 nnmbox-status-string)
156 (defun nnmbox-request-article (article &optional newsgroup server buffer)
157 (nnmbox-possibly-change-newsgroup newsgroup)
159 (set-buffer nnmbox-mbox-buffer)
160 (goto-char (point-min))
161 (if (search-forward (nnmbox-article-string article) nil t)
163 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
166 (or (and (re-search-forward
167 (concat "^" rmail-unix-mail-delimiter) nil t)
169 (goto-char (point-max)))
171 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
172 (set-buffer nntp-server-buffer)
174 (insert-buffer-substring nnmbox-mbox-buffer start stop)
175 (goto-char (point-min))
176 (while (looking-at "From ")
178 (insert "X-From-Line: ")
180 (if (numberp article)
181 (cons nnmbox-current-group article)
182 (nnmbox-article-group-number)))))))
184 (defun nnmbox-request-group (group &optional server dont-check)
186 (if (nnmbox-possibly-change-newsgroup group)
190 (set-buffer nntp-server-buffer)
192 (let ((active (assoc group nnmbox-group-alist)))
193 (insert (format "211 %d %d %d %s\n"
194 (1+ (- (cdr (car (cdr active)))
195 (car (car (cdr active)))))
196 (car (car (cdr active)))
197 (cdr (car (cdr 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 (and gnus-verbose-backends
257 (message "Deleting article %d in %s..."
258 (car articles) newsgroup))
259 (nnmbox-delete-mail))
260 (setq rest (cons (car articles) rest))))
261 (setq articles (cdr articles)))
263 ;; Find the lowest active article in this group.
264 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
265 (goto-char (point-min))
266 (while (and (not (search-forward
267 (nnmbox-article-string (car active)) nil t))
268 (<= (car active) (cdr active)))
269 (setcar active (1+ (car active)))
270 (goto-char (point-min))))
271 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
272 (nconc rest articles))))
274 (defun nnmbox-request-move-article
275 (article group server accept-form &optional last)
276 (nnmbox-possibly-change-newsgroup group)
277 (let ((buf (get-buffer-create " *nnmbox move*"))
280 (nnmbox-request-article article group server)
283 (buffer-disable-undo (current-buffer))
285 (insert-buffer-substring nntp-server-buffer)
286 (goto-char (point-min))
287 (while (re-search-forward
289 (save-excursion (search-forward "\n\n" nil t) (point)) t)
290 (delete-region (progn (beginning-of-line) (point))
291 (progn (forward-line 1) (point))))
292 (setq result (eval accept-form))
296 (set-buffer nnmbox-mbox-buffer)
297 (goto-char (point-min))
298 (if (search-forward (nnmbox-article-string article) nil t)
299 (nnmbox-delete-mail))
300 (and last (save-buffer))))
303 (defun nnmbox-request-accept-article (group &optional last)
304 (let ((buf (current-buffer))
306 (goto-char (point-min))
307 (if (looking-at "X-From-Line: ")
308 (replace-match "From ")
309 (insert "From nobody " (current-time-string) "\n"))
311 (nnmail-activate 'nnmbox)
314 (goto-char (point-min))
315 (search-forward "\n\n" nil t)
317 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
318 (delete-region (point) (progn (forward-line 1) (point))))
319 (setq result (nnmbox-save-mail (and (stringp group) group))))
321 (set-buffer nnmbox-mbox-buffer)
322 (insert-buffer-substring buf)
323 (and last (save-buffer))
325 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
328 (defun nnmbox-request-replace-article (article group buffer)
329 (nnmbox-possibly-change-newsgroup group)
331 (set-buffer nnmbox-mbox-buffer)
332 (goto-char (point-min))
333 (if (not (search-forward (nnmbox-article-string article) nil t))
335 (nnmbox-delete-mail t t)
336 (insert-buffer-substring buffer)
340 (defun nnmbox-request-delete-group (group &optional force server)
341 (nnmbox-possibly-change-newsgroup group)
342 ;; Delete all articles in GROUP.
344 () ; Don't delete the articles.
346 (set-buffer nnmbox-mbox-buffer)
347 (goto-char (point-min))
348 ;; Delete all articles in this group.
349 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
351 (while (search-forward ident nil t)
353 (nnmbox-delete-mail))
354 (and found (save-buffer)))))
355 ;; Remove the group from all structures.
356 (setq nnmbox-group-alist
357 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
358 nnmbox-current-group nil)
359 ;; Save the active file.
360 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
363 (defun nnmbox-request-rename-group (group new-name &optional server)
364 (nnmbox-possibly-change-newsgroup group)
366 (set-buffer nnmbox-mbox-buffer)
367 (goto-char (point-min))
368 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
369 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
371 (while (search-forward ident nil t)
372 (replace-match new-ident t t)
374 (and found (save-buffer))))
375 (let ((entry (assoc group nnmbox-group-alist)))
376 (and entry (setcar entry new-name))
377 (setq nnmbox-current-group nil)
378 ;; Save the new group alist.
379 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
383 ;;; Internal functions.
385 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
386 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
388 (defun nnmbox-delete-mail (&optional force leave-delim)
389 ;; Delete the current X-Gnus-Newsgroup line.
392 (progn (beginning-of-line) (point))
393 (progn (forward-line 1) (point))))
394 ;; Beginning of the article.
399 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
400 (if leave-delim (progn (forward-line 1) (point))
401 (match-beginning 0)))
404 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
406 (if (and (not (bobp)) leave-delim)
407 (progn (forward-line -2) (point))
408 (match-beginning 0)))
410 (goto-char (point-min))
411 ;; Only delete the article if no other groups owns it as well.
412 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
413 (delete-region (point-min) (point-max))))))
415 (defun nnmbox-possibly-change-newsgroup (newsgroup)
416 (if (or (not nnmbox-mbox-buffer)
417 (not (buffer-name nnmbox-mbox-buffer)))
419 (set-buffer (setq nnmbox-mbox-buffer
420 (nnheader-find-file-noselect
421 nnmbox-mbox-file nil 'raw)))
422 (buffer-disable-undo (current-buffer))))
423 (if (not nnmbox-group-alist)
424 (nnmail-activate 'nnmbox))
426 (if (assoc newsgroup nnmbox-group-alist)
427 (setq nnmbox-current-group newsgroup))))
429 (defun nnmbox-article-string (article)
430 (if (numberp article)
431 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
432 (int-to-string article) " ")
433 (concat "\nMessage-ID: " article)))
435 (defun nnmbox-article-group-number ()
437 (goto-char (point-min))
438 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
440 (cons (buffer-substring (match-beginning 1) (match-end 1))
442 (buffer-substring (match-beginning 2) (match-end 2)))))))
444 (defun nnmbox-save-mail (&optional group)
445 "Called narrowed to an article."
446 (let* ((nnmail-split-methods
447 (if group (list (list group "")) nnmail-split-methods))
448 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number)))
449 (delim (concat "^" rmail-unix-mail-delimiter)))
450 (goto-char (point-min))
451 ;; This might come from somewhere else.
452 (unless (looking-at delim)
453 (insert "From nobody " (current-time-string) "\n")
454 (goto-char (point-min)))
455 ;; Quote all "From " lines in the article.
457 (while (re-search-forward delim nil t)
460 (nnmail-insert-lines)
461 (nnmail-insert-xref group-art)
462 (nnmbox-insert-newsgroup-line group-art)
463 (run-hooks 'nnmbox-prepare-save-mail-hook)
466 (defun nnmbox-insert-newsgroup-line (group-art)
468 (goto-char (point-min))
469 (if (search-forward "\n\n" nil t)
473 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
474 (car (car group-art)) (cdr (car group-art))
475 (current-time-string)))
476 (setq group-art (cdr group-art)))))
479 (defun nnmbox-active-number (group)
480 ;; Find the next article number in GROUP.
481 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
483 (setcdr active (1+ (cdr active)))
484 ;; This group is new, so we create a new entry for it.
485 ;; This might be a bit naughty... creating groups on the drop of
486 ;; a hat, but I don't know...
487 (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
488 nnmbox-group-alist)))
491 (defun nnmbox-read-mbox ()
492 (nnmail-activate 'nnmbox)
493 (if (not (file-exists-p nnmbox-mbox-file))
494 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
495 (if (and nnmbox-mbox-buffer
496 (buffer-name nnmbox-mbox-buffer)
498 (set-buffer nnmbox-mbox-buffer)
499 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
502 (let ((delim (concat "^" rmail-unix-mail-delimiter))
504 (set-buffer (setq nnmbox-mbox-buffer
505 (nnheader-find-file-noselect
506 nnmbox-mbox-file nil 'raw)))
507 (buffer-disable-undo (current-buffer))
508 (goto-char (point-min))
509 (while (re-search-forward delim nil t)
510 (setq start (match-beginning 0))
511 (if (not (search-forward "\nX-Gnus-Newsgroup: "
516 (re-search-forward delim nil t)
522 (narrow-to-region start end)
523 (nnmbox-save-mail))))
528 ;;; nnmbox.el ends here