1 ;;; nnbabyl.el --- rmail 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 (eval-when-compile (require 'cl))
38 (nnoo-declare nnbabyl)
40 (defvoo nnbabyl-mbox-file (expand-file-name "~/RMAIL")
41 "The name of the rmail box file in the users home directory.")
43 (defvoo nnbabyl-active-file (expand-file-name "~/.rmail-active")
44 "The name of the active file for the rmail box.")
46 (defvoo nnbabyl-get-new-mail t
47 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
49 (defvoo nnbabyl-prepare-save-mail-hook nil
50 "Hook run narrowed to an article before saving.")
54 (defvar nnbabyl-mail-delimiter "\^_")
56 (defconst nnbabyl-version "nnbabyl 1.0"
59 (defvoo nnbabyl-mbox-buffer nil)
60 (defvoo nnbabyl-current-group nil)
61 (defvoo nnbabyl-status-string "")
62 (defvoo nnbabyl-group-alist nil)
63 (defvoo nnbabyl-active-timestamp nil)
65 (defvoo nnbabyl-previous-buffer-mode nil)
68 (autoload 'gnus-set-text-properties "gnus-ems"))
72 ;;; Interface functions
74 (nnoo-define-basics nnbabyl)
76 (deffoo nnbabyl-retrieve-headers (articles &optional group server fetch-old)
78 (set-buffer nntp-server-buffer)
80 (let ((number (length articles))
82 (delim (concat "^" nnbabyl-mail-delimiter))
83 article art-string start stop)
84 (nnbabyl-possibly-change-newsgroup group server)
85 (while (setq article (pop articles))
86 (setq art-string (nnbabyl-article-string article))
87 (set-buffer nnbabyl-mbox-buffer)
89 (when (or (search-forward art-string nil t)
90 (search-backward art-string nil t))
91 (re-search-backward delim nil t)
92 (while (and (not (looking-at ".+:"))
93 (zerop (forward-line 1))))
95 (search-forward "\n\n" nil t)
96 (setq stop (1- (point)))
97 (set-buffer nntp-server-buffer)
99 (princ article (current-buffer))
100 (insert " Article retrieved.\n")
101 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
102 (goto-char (point-max))
104 (and (numberp nnmail-large-newsgroup)
105 (> number nnmail-large-newsgroup)
106 (zerop (% (incf count) 20))
107 (nnheader-message 5 "nnbabyl: Receiving headers... %d%%"
108 (/ (* count 100) number))))
110 (and (numberp nnmail-large-newsgroup)
111 (> number nnmail-large-newsgroup)
112 (nnheader-message 5 "nnbabyl: Receiving headers...done"))
114 (set-buffer nntp-server-buffer)
115 (nnheader-fold-continuation-lines)
118 (deffoo nnbabyl-open-server (server &optional defs)
119 (nnoo-change-server 'nnbabyl server defs)
121 ((not (file-exists-p nnbabyl-mbox-file))
122 (nnbabyl-close-server)
123 (nnheader-report 'nnbabyl "No such file: %s" nnbabyl-mbox-file))
124 ((file-directory-p nnbabyl-mbox-file)
125 (nnbabyl-close-server)
126 (nnheader-report 'nnbabyl "Not a regular file: %s" nnbabyl-mbox-file))
128 (nnheader-report 'nnbabyl "Opened server %s using mbox %s" server
132 (deffoo nnbabyl-close-server (&optional server)
133 ;; Restore buffer mode.
134 (when (and (nnbabyl-server-opened)
135 nnbabyl-previous-buffer-mode)
137 (set-buffer nnbabyl-mbox-buffer)
139 (caar nnbabyl-previous-buffer-mode)
140 (cdar nnbabyl-previous-buffer-mode))
141 (funcall (cdr nnbabyl-previous-buffer-mode))))
142 (nnoo-close-server 'nnbabyl server)
143 (setq nnbabyl-mbox-buffer nil)
146 (deffoo nnbabyl-server-opened (&optional server)
147 (and (nnoo-current-server-p 'nnbabyl server)
149 (buffer-name nnbabyl-mbox-buffer)
151 (buffer-name nntp-server-buffer)))
153 (deffoo nnbabyl-request-article (article &optional newsgroup server buffer)
154 (nnbabyl-possibly-change-newsgroup newsgroup server)
156 (set-buffer nnbabyl-mbox-buffer)
157 (goto-char (point-min))
158 (when (search-forward (nnbabyl-article-string article) nil t)
159 (let (start stop summary-line)
160 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
161 (while (and (not (looking-at ".+:"))
162 (zerop (forward-line 1))))
164 (or (and (re-search-forward
165 (concat "^" nnbabyl-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 nnbabyl-mbox-buffer start stop)
173 (goto-char (point-min))
174 ;; If there is an EOOH header, then we have to remove some
175 ;; duplicated headers.
176 (setq summary-line (looking-at "Summary-line:"))
177 (when (search-forward "\n*** EOOH ***" nil t)
179 ;; The headers to be deleted are located before the
181 (delete-region (point-min) (progn (forward-line 1)
184 (delete-region (progn (beginning-of-line) (point))
185 (or (search-forward "\n\n" nil t)
187 (if (numberp article)
188 (cons nnbabyl-current-group article)
189 (nnbabyl-article-group-number)))))))
191 (deffoo nnbabyl-request-group (group &optional server dont-check)
192 (let ((active (cadr (assoc group nnbabyl-group-alist))))
196 (null (nnbabyl-possibly-change-newsgroup group server)))
197 (nnheader-report 'nnbabyl "No such group: %s" group))
199 (nnheader-report 'nnbabyl "Selected group %s" group)
200 (nnheader-insert ""))
202 (nnheader-report 'nnbabyl "Selected group %s" group)
203 (nnheader-insert "211 %d %d %d %s\n"
204 (1+ (- (cdr active) (car active)))
205 (car active) (cdr active) group))))))
207 (deffoo nnbabyl-request-scan (&optional group server)
213 (set-buffer nnbabyl-mbox-buffer)
215 nnbabyl-mbox-file group
218 (let ((in-buf (current-buffer)))
219 (goto-char (point-min))
220 (while (search-forward "\n\^_\n" nil t)
222 (set-buffer nnbabyl-mbox-buffer)
223 (goto-char (point-max))
224 (search-backward "\n\^_" nil t)
225 (goto-char (match-end 0))
226 (insert-buffer-substring in-buf)))
227 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))))
229 (deffoo nnbabyl-close-group (group &optional server)
232 (deffoo nnbabyl-request-create-group (group &optional server)
233 (nnmail-activate 'nnbabyl)
234 (unless (assoc group nnbabyl-group-alist)
235 (setq nnbabyl-group-alist (cons (list group (cons 1 0))
236 nnbabyl-group-alist))
237 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
240 (deffoo nnbabyl-request-list (&optional server)
242 (nnmail-find-file nnbabyl-active-file)
243 (setq nnbabyl-group-alist (nnmail-get-active))))
245 (deffoo nnbabyl-request-newgroups (date &optional server)
246 (nnbabyl-request-list server))
248 (deffoo nnbabyl-request-list-newsgroups (&optional server)
249 (nnheader-report 'nnbabyl "nnbabyl: LIST NEWSGROUPS is not implemented."))
251 (deffoo nnbabyl-request-expire-articles
252 (articles newsgroup &optional server force)
253 (nnbabyl-possibly-change-newsgroup newsgroup server)
256 (nnmail-activate 'nnbabyl)
259 (set-buffer nnbabyl-mbox-buffer)
260 (gnus-set-text-properties (point-min) (point-max) nil)
261 (while (and articles is-old)
262 (goto-char (point-min))
263 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
265 (nnmail-expired-article-p
268 (point) (progn (end-of-line) (point))) force))
270 (nnheader-message 5 "Deleting article %d in %s..."
271 (car articles) newsgroup)
272 (nnbabyl-delete-mail))
273 (setq rest (cons (car articles) rest))))
274 (setq articles (cdr articles)))
276 ;; Find the lowest active article in this group.
277 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
278 (goto-char (point-min))
279 (while (and (not (search-forward
280 (nnbabyl-article-string (car active)) nil t))
281 (<= (car active) (cdr active)))
282 (setcar active (1+ (car active)))
283 (goto-char (point-min))))
284 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
285 (nconc rest articles))))
287 (deffoo nnbabyl-request-move-article
288 (article group server accept-form &optional last)
289 (nnbabyl-possibly-change-newsgroup group server)
290 (let ((buf (get-buffer-create " *nnbabyl move*"))
293 (nnbabyl-request-article article group server)
296 (insert-buffer-substring nntp-server-buffer)
297 (goto-char (point-min))
298 (if (re-search-forward
300 (save-excursion (search-forward "\n\n" nil t) (point)) t)
301 (delete-region (progn (beginning-of-line) (point))
302 (progn (forward-line 1) (point))))
303 (setq result (eval accept-form))
304 (kill-buffer (current-buffer))
307 (set-buffer nnbabyl-mbox-buffer)
308 (goto-char (point-min))
309 (if (search-forward (nnbabyl-article-string article) nil t)
310 (nnbabyl-delete-mail))
311 (and last (save-buffer))))
314 (deffoo nnbabyl-request-accept-article (group &optional server last)
315 (nnbabyl-possibly-change-newsgroup group server)
316 (let ((buf (current-buffer))
319 (nnmail-activate 'nnbabyl)
321 (goto-char (point-min))
322 (search-forward "\n\n" nil t)
325 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
326 (delete-region (point) (progn (forward-line 1) (point)))))
327 (let ((nnmail-split-methods
328 (if (stringp group) (list (list group ""))
329 nnmail-split-methods)))
330 (setq result (car (nnbabyl-save-mail))))
331 (set-buffer nnbabyl-mbox-buffer)
332 (goto-char (point-max))
333 (search-backward "\n\^_")
334 (goto-char (match-end 0))
335 (insert-buffer-substring buf)
338 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
341 (deffoo nnbabyl-request-replace-article (article group buffer)
342 (nnbabyl-possibly-change-newsgroup group)
344 (set-buffer nnbabyl-mbox-buffer)
345 (goto-char (point-min))
346 (if (not (search-forward (nnbabyl-article-string article) nil t))
348 (nnbabyl-delete-mail t t)
349 (insert-buffer-substring buffer)
353 (deffoo nnbabyl-request-delete-group (group &optional force server)
354 (nnbabyl-possibly-change-newsgroup group server)
355 ;; Delete all articles in GROUP.
357 () ; Don't delete the articles.
359 (set-buffer nnbabyl-mbox-buffer)
360 (goto-char (point-min))
361 ;; Delete all articles in this group.
362 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
364 (while (search-forward ident nil t)
366 (nnbabyl-delete-mail))
367 (and found (save-buffer)))))
368 ;; Remove the group from all structures.
369 (setq nnbabyl-group-alist
370 (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
371 nnbabyl-current-group nil)
372 ;; Save the active file.
373 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
376 (deffoo nnbabyl-request-rename-group (group new-name &optional server)
377 (nnbabyl-possibly-change-newsgroup group server)
379 (set-buffer nnbabyl-mbox-buffer)
380 (goto-char (point-min))
381 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
382 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
384 (while (search-forward ident nil t)
385 (replace-match new-ident t t)
387 (and found (save-buffer))))
388 (let ((entry (assoc group nnbabyl-group-alist)))
389 (and entry (setcar entry new-name))
390 (setq nnbabyl-current-group nil)
391 ;; Save the new group alist.
392 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
396 ;;; Internal functions.
398 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
399 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
401 (defun nnbabyl-delete-mail (&optional force leave-delim)
402 ;; Delete the current X-Gnus-Newsgroup line.
405 (progn (beginning-of-line) (point))
406 (progn (forward-line 1) (point))))
407 ;; Beginning of the article.
413 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
414 (if leave-delim (progn (forward-line 1) (point))
415 (match-beginning 0)))
418 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
420 (if (and (not (bobp)) leave-delim)
421 (progn (forward-line -2) (point))
422 (match-beginning 0)))
424 (goto-char (point-min))
425 ;; Only delete the article if no other groups owns it as well.
426 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
427 (delete-region (point-min) (point-max))))))
429 (defun nnbabyl-possibly-change-newsgroup (newsgroup &optional server)
431 (not (nnbabyl-server-opened server)))
432 (nnbabyl-open-server server))
433 (if (or (not nnbabyl-mbox-buffer)
434 (not (buffer-name nnbabyl-mbox-buffer)))
435 (save-excursion (nnbabyl-read-mbox)))
436 (or nnbabyl-group-alist
437 (nnmail-activate 'nnbabyl))
439 (if (assoc newsgroup nnbabyl-group-alist)
440 (setq nnbabyl-current-group newsgroup)
441 (nnheader-report 'nnbabyl "No such group in file"))
444 (defun nnbabyl-article-string (article)
445 (if (numberp article)
446 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
447 (int-to-string article) " ")
448 (concat "\nMessage-ID: " article)))
450 (defun nnbabyl-article-group-number ()
452 (goto-char (point-min))
453 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
455 (cons (buffer-substring (match-beginning 1) (match-end 1))
457 (buffer-substring (match-beginning 2) (match-end 2)))))))
459 (defun nnbabyl-insert-lines ()
460 "Insert how many lines and chars there are in the body of the mail."
463 (goto-char (point-min))
464 (when (search-forward "\n\n" nil t)
465 ;; There may be an EOOH line here...
466 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
467 (search-forward "\n\n" nil t))
468 (setq chars (- (point-max) (point))
469 lines (max (- (count-lines (point) (point-max)) 1) 0))
470 ;; Move back to the end of the headers.
471 (goto-char (point-min))
472 (search-forward "\n\n" nil t)
475 (when (re-search-backward "^Lines: " nil t)
476 (delete-region (point) (progn (forward-line 1) (point)))))
477 (insert (format "Lines: %d\n" lines))
480 (defun nnbabyl-save-mail ()
481 ;; Called narrowed to an article.
482 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
483 (nnbabyl-insert-lines)
484 (nnmail-insert-xref group-art)
485 (nnbabyl-insert-newsgroup-line group-art)
486 (run-hooks 'nnbabyl-prepare-save-mail-hook)
489 (defun nnbabyl-insert-newsgroup-line (group-art)
491 (goto-char (point-min))
492 (while (looking-at "From ")
493 (replace-match "Mail-from: From " t t)
495 ;; If there is a C-l at the beginning of the narrowed region, this
496 ;; isn't really a "save", but rather a "scan".
497 (goto-char (point-min))
498 (or (looking-at "\^L")
500 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
501 (goto-char (point-max))
503 (if (search-forward "\n\n" nil t)
507 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
508 (caar group-art) (cdar group-art)
509 (current-time-string)))
510 (setq group-art (cdr group-art)))))
513 (defun nnbabyl-active-number (group)
514 ;; Find the next article number in GROUP.
515 (let ((active (cadr (assoc group nnbabyl-group-alist))))
517 (setcdr active (1+ (cdr active)))
518 ;; This group is new, so we create a new entry for it.
519 ;; This might be a bit naughty... creating groups on the drop of
520 ;; a hat, but I don't know...
521 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 1)))
522 nnbabyl-group-alist)))
525 (defun nnbabyl-read-mbox ()
526 (nnmail-activate 'nnbabyl)
527 (unless (file-exists-p nnbabyl-mbox-file)
528 ;; Create a new, empty RMAIL mbox file.
530 (set-buffer (setq nnbabyl-mbox-buffer
531 (create-file-buffer nnbabyl-mbox-file)))
532 (setq buffer-file-name nnbabyl-mbox-file)
533 (insert "BABYL OPTIONS:\n\n\^_")
534 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
536 (if (and nnbabyl-mbox-buffer
537 (buffer-name nnbabyl-mbox-buffer)
539 (set-buffer nnbabyl-mbox-buffer)
540 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
541 () ; This buffer hasn't changed since we read it last. Possibly.
543 (let ((delim (concat "^" nnbabyl-mail-delimiter))
544 (alist nnbabyl-group-alist)
546 (set-buffer (setq nnbabyl-mbox-buffer
547 (nnheader-find-file-noselect
548 nnbabyl-mbox-file nil 'raw)))
549 ;; Save previous buffer mode.
550 (setq nnbabyl-previous-buffer-mode
551 (cons (cons (point-min) (point-max))
554 (buffer-disable-undo (current-buffer))
556 (setq buffer-read-only nil)
559 ;; Go through the group alist and compare against
562 (goto-char (point-max))
563 (when (and (re-search-backward
564 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
569 (match-beginning 1) (match-end 1))))
571 (setcdr (cadar alist) (1+ number)))
572 (setq alist (cdr alist)))
574 ;; We go through the mbox and make sure that each and
575 ;; every mail belongs to some group or other.
576 (goto-char (point-min))
577 (re-search-forward delim nil t)
578 (setq start (match-end 0))
579 (while (re-search-forward delim nil t)
580 (setq end (match-end 0))
581 (unless (search-backward "\nX-Gnus-Newsgroup: " start t)
585 (narrow-to-region (goto-char start) end)
587 (setq end (point-max)))))
588 (goto-char (setq start end)))
589 (when (buffer-modified-p (current-buffer))
591 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
593 (defun nnbabyl-remove-incoming-delims ()
594 (goto-char (point-min))
595 (while (search-forward "\^_" nil t)
596 (replace-match "?" t t)))
598 (defun nnbabyl-check-mbox ()
599 "Go through the nnbabyl mbox and make sure that no article numbers are reused."
601 (let ((idents (make-vector 1000 0))
604 (when (or (not nnbabyl-mbox-buffer)
605 (not (buffer-name nnbabyl-mbox-buffer)))
607 (set-buffer nnbabyl-mbox-buffer)
608 (goto-char (point-min))
609 (while (re-search-forward "^X-Gnus-Newsgroup: \\([^ ]+\\) " nil t)
610 (if (intern-soft (setq id (match-string 1)) idents)
612 (delete-region (progn (beginning-of-line) (point))
613 (progn (forward-line 1) (point)))
614 (nnheader-message 7 "Moving %s..." id)
617 (when (buffer-modified-p (current-buffer))
619 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
624 ;;; nnbabyl.el ends here