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 (defvar nnmbox-mbox-file (expand-file-name "~/mbox")
37 "The name of the mail box file in the user's home directory.")
39 (defvar nnmbox-active-file (expand-file-name "~/.mbox-active")
40 "The name of the active file for the mail box.")
42 (defvar nnmbox-get-new-mail t
43 "If non-nil, nnmbox will check the incoming mail file and split the mail.")
45 (defvar nnmbox-prepare-save-mail-hook nil
46 "Hook run narrowed to an article before saving.")
50 (defconst nnmbox-version "nnmbox 1.0"
53 (defvar nnmbox-current-group nil
54 "Current nnmbox news group directory.")
56 (defconst nnmbox-mbox-buffer nil)
58 (defvar nnmbox-status-string "")
60 (defvar nnmbox-group-alist nil)
61 (defvar nnmbox-active-timestamp nil)
65 (defvar nnmbox-current-server nil)
66 (defvar nnmbox-server-alist nil)
67 (defvar nnmbox-server-variables
69 (list 'nnmbox-mbox-file nnmbox-mbox-file)
70 (list 'nnmbox-active-file nnmbox-active-file)
71 (list 'nnmbox-get-new-mail nnmbox-get-new-mail)
72 '(nnmbox-current-group nil)
73 '(nnmbox-status-string "")
74 '(nnmbox-group-alist nil)))
78 ;;; Interface functions
80 (defun nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
82 (set-buffer nntp-server-buffer)
84 (let ((number (length sequence))
86 article art-string start stop)
87 (nnmbox-possibly-change-newsgroup newsgroup)
89 (setq article (car sequence))
90 (setq art-string (nnmbox-article-string article))
91 (set-buffer nnmbox-mbox-buffer)
92 (if (or (search-forward art-string nil t)
93 (progn (goto-char (point-min))
94 (search-forward art-string nil t)))
99 (concat "^" rmail-unix-mail-delimiter) nil t)
101 (search-forward "\n\n" nil t)
102 (setq stop (1- (point)))
103 (set-buffer nntp-server-buffer)
104 (insert (format "221 %d Article retrieved.\n" article))
105 (insert-buffer-substring nnmbox-mbox-buffer start stop)
106 (goto-char (point-max))
108 (setq sequence (cdr sequence))
109 (setq count (1+ count))
110 (and (numberp nnmail-large-newsgroup)
111 (> number nnmail-large-newsgroup)
113 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
114 (/ (* count 100) number))))
116 (and (numberp nnmail-large-newsgroup)
117 (> number nnmail-large-newsgroup)
118 (nnheader-message 5 "nnmbox: Receiving headers...done"))
120 (set-buffer nntp-server-buffer)
121 (nnheader-fold-continuation-lines)
124 (defun nnmbox-open-server (server &optional defs)
125 (nnheader-init-server-buffer)
126 (if (equal server nnmbox-current-server)
128 (if nnmbox-current-server
129 (setq nnmbox-server-alist
130 (cons (list nnmbox-current-server
131 (nnheader-save-variables nnmbox-server-variables))
132 nnmbox-server-alist)))
133 (let ((state (assoc server nnmbox-server-alist)))
136 (nnheader-restore-variables (nth 1 state))
137 (setq nnmbox-server-alist (delq state nnmbox-server-alist)))
138 (nnheader-set-init-variables nnmbox-server-variables defs)))
139 (setq nnmbox-current-server server)))
141 (defun nnmbox-close-server (&optional server)
142 (setq nnmbox-current-server nil)
145 (defun nnmbox-server-opened (&optional server)
146 (and (equal server nnmbox-current-server)
148 (buffer-name nnmbox-mbox-buffer)
150 (buffer-name nntp-server-buffer)))
152 (defun nnmbox-status-message (&optional server)
153 nnmbox-status-string)
155 (defun nnmbox-request-article (article &optional newsgroup server buffer)
156 (nnmbox-possibly-change-newsgroup newsgroup)
158 (set-buffer nnmbox-mbox-buffer)
159 (goto-char (point-min))
160 (if (search-forward (nnmbox-article-string article) nil t)
162 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
165 (or (and (re-search-forward
166 (concat "^" rmail-unix-mail-delimiter) nil t)
168 (goto-char (point-max)))
170 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
171 (set-buffer nntp-server-buffer)
173 (insert-buffer-substring nnmbox-mbox-buffer start stop)
174 (goto-char (point-min))
175 (while (looking-at "From ")
177 (insert "X-From-Line: ")
179 (if (numberp article)
180 (cons nnmbox-current-group article)
181 (nnmbox-article-group-number)))))))
183 (defun nnmbox-request-group (group &optional server dont-check)
184 (let ((active (cadr (assoc group nnmbox-group-alist))))
187 (nnheader-report 'nnmbox "No such group: %s" group))
188 ((null (nnmbox-possibly-change-newsgroup group))
189 (nnheader-report 'nnmbox "No such group: %s" group))
191 (nnheader-report 'nnmbox "Selected group %s" group)
192 (nnheader-insert ""))
194 (nnheader-report 'nnmbox "Selected group %s" group)
195 (nnheader-insert "211 %d %d %d %s\n"
196 (1+ (- (cdr active) (car active)))
197 (car active) (cdr active) group)
200 (defun nnmbox-request-scan (&optional group server)
206 (set-buffer nnmbox-mbox-buffer)
208 nnmbox-mbox-file group
211 (let ((in-buf (current-buffer)))
212 (set-buffer nnmbox-mbox-buffer)
213 (goto-char (point-max))
214 (insert-buffer-substring in-buf))))))
216 (defun nnmbox-close-group (group &optional server)
219 (defun nnmbox-request-list (&optional server)
221 (or (nnmail-find-file nnmbox-active-file)
223 (setq nnmbox-group-alist (nnmail-get-active))
224 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
225 (nnmail-find-file nnmbox-active-file)))))
227 (defun nnmbox-request-newgroups (date &optional server)
228 (nnmbox-request-list server))
230 (defun nnmbox-request-list-newsgroups (&optional server)
231 (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
234 (defun nnmbox-request-post (&optional server)
235 (mail-send-and-exit nil))
237 (defun nnmbox-request-expire-articles
238 (articles newsgroup &optional server force)
239 (nnmbox-possibly-change-newsgroup newsgroup)
242 (nnmail-activate 'nnmbox)
245 (set-buffer nnmbox-mbox-buffer)
246 (while (and articles is-old)
247 (goto-char (point-min))
248 (if (search-forward (nnmbox-article-string (car articles)) nil t)
250 (nnmail-expired-article-p
253 (point) (progn (end-of-line) (point))) force))
255 (nnheader-message 5 "Deleting article %d in %s..."
256 (car articles) newsgroup)
257 (nnmbox-delete-mail))
258 (setq rest (cons (car articles) rest))))
259 (setq articles (cdr articles)))
261 ;; Find the lowest active article in this group.
262 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
263 (goto-char (point-min))
264 (while (and (not (search-forward
265 (nnmbox-article-string (car active)) nil t))
266 (<= (car active) (cdr active)))
267 (setcar active (1+ (car active)))
268 (goto-char (point-min))))
269 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
270 (nconc rest articles))))
272 (defun nnmbox-request-move-article
273 (article group server accept-form &optional last)
274 (nnmbox-possibly-change-newsgroup group)
275 (let ((buf (get-buffer-create " *nnmbox move*"))
278 (nnmbox-request-article article group server)
281 (buffer-disable-undo (current-buffer))
283 (insert-buffer-substring nntp-server-buffer)
284 (goto-char (point-min))
285 (while (re-search-forward
287 (save-excursion (search-forward "\n\n" nil t) (point)) t)
288 (delete-region (progn (beginning-of-line) (point))
289 (progn (forward-line 1) (point))))
290 (setq result (eval accept-form))
294 (set-buffer nnmbox-mbox-buffer)
295 (goto-char (point-min))
296 (if (search-forward (nnmbox-article-string article) nil t)
297 (nnmbox-delete-mail))
298 (and last (save-buffer))))
301 (defun nnmbox-request-accept-article (group &optional last)
302 (let ((buf (current-buffer))
304 (goto-char (point-min))
305 (if (looking-at "X-From-Line: ")
306 (replace-match "From ")
307 (insert "From nobody " (current-time-string) "\n"))
309 (nnmail-activate 'nnmbox)
312 (goto-char (point-min))
313 (search-forward "\n\n" nil t)
315 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
316 (delete-region (point) (progn (forward-line 1) (point))))
317 (setq result (nnmbox-save-mail (and (stringp group) group))))
319 (set-buffer nnmbox-mbox-buffer)
320 (insert-buffer-substring buf)
321 (and last (save-buffer))
323 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
326 (defun nnmbox-request-replace-article (article group buffer)
327 (nnmbox-possibly-change-newsgroup group)
329 (set-buffer nnmbox-mbox-buffer)
330 (goto-char (point-min))
331 (if (not (search-forward (nnmbox-article-string article) nil t))
333 (nnmbox-delete-mail t t)
334 (insert-buffer-substring buffer)
338 (defun nnmbox-request-delete-group (group &optional force server)
339 (nnmbox-possibly-change-newsgroup group)
340 ;; Delete all articles in GROUP.
342 () ; Don't delete the articles.
344 (set-buffer nnmbox-mbox-buffer)
345 (goto-char (point-min))
346 ;; Delete all articles in this group.
347 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
349 (while (search-forward ident nil t)
351 (nnmbox-delete-mail))
352 (and found (save-buffer)))))
353 ;; Remove the group from all structures.
354 (setq nnmbox-group-alist
355 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
356 nnmbox-current-group nil)
357 ;; Save the active file.
358 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
361 (defun nnmbox-request-rename-group (group new-name &optional server)
362 (nnmbox-possibly-change-newsgroup group)
364 (set-buffer nnmbox-mbox-buffer)
365 (goto-char (point-min))
366 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
367 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
369 (while (search-forward ident nil t)
370 (replace-match new-ident t t)
372 (and found (save-buffer))))
373 (let ((entry (assoc group nnmbox-group-alist)))
374 (and entry (setcar entry new-name))
375 (setq nnmbox-current-group nil)
376 ;; Save the new group alist.
377 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
381 ;;; Internal functions.
383 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
384 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
386 (defun nnmbox-delete-mail (&optional force leave-delim)
387 ;; Delete the current X-Gnus-Newsgroup line.
390 (progn (beginning-of-line) (point))
391 (progn (forward-line 1) (point))))
392 ;; Beginning of the article.
397 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
398 (if leave-delim (progn (forward-line 1) (point))
399 (match-beginning 0)))
402 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
404 (if (and (not (bobp)) leave-delim)
405 (progn (forward-line -2) (point))
406 (match-beginning 0)))
408 (goto-char (point-min))
409 ;; Only delete the article if no other groups owns it as well.
410 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
411 (delete-region (point-min) (point-max))))))
413 (defun nnmbox-possibly-change-newsgroup (newsgroup)
414 (if (or (not nnmbox-mbox-buffer)
415 (not (buffer-name nnmbox-mbox-buffer)))
417 (set-buffer (setq nnmbox-mbox-buffer
418 (nnheader-find-file-noselect
419 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))
428 (defun nnmbox-article-string (article)
429 (if (numberp article)
430 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
431 (int-to-string article) " ")
432 (concat "\nMessage-ID: " article)))
434 (defun nnmbox-article-group-number ()
436 (goto-char (point-min))
437 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
439 (cons (buffer-substring (match-beginning 1) (match-end 1))
441 (buffer-substring (match-beginning 2) (match-end 2)))))))
443 (defun nnmbox-save-mail (&optional group)
444 "Called narrowed to an article."
445 (let* ((nnmail-split-methods
446 (if group (list (list group "")) nnmail-split-methods))
447 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number)))
448 (delim (concat "^" rmail-unix-mail-delimiter)))
449 (goto-char (point-min))
450 ;; This might come from somewhere else.
451 (unless (looking-at delim)
452 (insert "From nobody " (current-time-string) "\n")
453 (goto-char (point-min)))
454 ;; Quote all "From " lines in the article.
456 (while (re-search-forward delim nil t)
459 (nnmail-insert-lines)
460 (nnmail-insert-xref group-art)
461 (nnmbox-insert-newsgroup-line group-art)
462 (run-hooks 'nnmbox-prepare-save-mail-hook)
465 (defun nnmbox-insert-newsgroup-line (group-art)
467 (goto-char (point-min))
468 (if (search-forward "\n\n" nil t)
472 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
473 (car (car group-art)) (cdr (car group-art))
474 (current-time-string)))
475 (setq group-art (cdr group-art)))))
478 (defun nnmbox-active-number (group)
479 ;; Find the next article number in GROUP.
480 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
482 (setcdr active (1+ (cdr active)))
483 ;; This group is new, so we create a new entry for it.
484 ;; This might be a bit naughty... creating groups on the drop of
485 ;; a hat, but I don't know...
486 (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
487 nnmbox-group-alist)))
490 (defun nnmbox-read-mbox ()
491 (nnmail-activate 'nnmbox)
492 (if (not (file-exists-p nnmbox-mbox-file))
493 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
494 (if (and nnmbox-mbox-buffer
495 (buffer-name nnmbox-mbox-buffer)
497 (set-buffer nnmbox-mbox-buffer)
498 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
501 (let ((delim (concat "^" rmail-unix-mail-delimiter))
503 (set-buffer (setq nnmbox-mbox-buffer
504 (nnheader-find-file-noselect
505 nnmbox-mbox-file nil 'raw)))
506 (buffer-disable-undo (current-buffer))
507 (goto-char (point-min))
508 (while (re-search-forward delim nil t)
509 (setq start (match-beginning 0))
510 (if (not (search-forward "\nX-Gnus-Newsgroup: "
515 (re-search-forward delim nil t)
521 (narrow-to-region start end)
522 (nnmbox-save-mail))))
527 ;;; nnmbox.el ends here