1 ;;; nnbabyl.el --- rmail 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 nnbabyl-mbox-file (expand-file-name "~/RMAIL")
36 "The name of the rmail box file in the users home directory.")
38 (defvar nnbabyl-active-file (expand-file-name "~/.rmail-active")
39 "The name of the active file for the rmail box.")
41 (defvar nnbabyl-get-new-mail t
42 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
44 (defvar nnbabyl-prepare-save-mail-hook nil
45 "Hook run narrowed to an article before saving.")
49 (defvar nnbabyl-mail-delimiter "\^_")
51 (defconst nnbabyl-version "nnbabyl 1.0"
54 (defvar nnbabyl-mbox-buffer nil)
55 (defvar nnbabyl-current-group nil)
56 (defvar nnbabyl-status-string "")
57 (defvar nnbabyl-group-alist nil)
58 (defvar nnbabyl-active-timestamp nil)
60 (defvar nnbabyl-previous-buffer-mode nil)
64 (defvar nnbabyl-current-server nil)
65 (defvar nnbabyl-server-alist nil)
66 (defvar nnbabyl-server-variables
68 (list 'nnbabyl-mbox-file nnbabyl-mbox-file)
69 (list 'nnbabyl-active-file nnbabyl-active-file)
70 (list 'nnbabyl-get-new-mail nnbabyl-get-new-mail)
71 '(nnbabyl-current-group nil)
72 '(nnbabyl-status-string "")
73 '(nnbabyl-previous-buffer-mode nil)
74 '(nnbabyl-group-alist nil)))
78 ;;; Interface functions
80 (defun nnbabyl-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 (nnbabyl-possibly-change-newsgroup newsgroup)
89 (setq article (car sequence))
90 (setq art-string (nnbabyl-article-string article))
91 (set-buffer nnbabyl-mbox-buffer)
92 (if (or (search-forward art-string nil t)
93 (search-backward art-string nil t))
95 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
96 (while (and (not (looking-at ".+:"))
97 (zerop (forward-line 1))))
99 (search-forward "\n\n" nil t)
100 (setq stop (1- (point)))
101 (set-buffer nntp-server-buffer)
102 (insert "221 " (int-to-string article) " Article retrieved.\n")
103 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
104 (goto-char (point-max))
106 (setq sequence (cdr sequence))
107 (setq count (1+ count))
108 (and (numberp nnmail-large-newsgroup)
109 (> number nnmail-large-newsgroup)
111 gnus-verbose-backends
112 (message "nnbabyl: Receiving headers... %d%%"
113 (/ (* count 100) number))))
115 (and (numberp nnmail-large-newsgroup)
116 (> number nnmail-large-newsgroup)
117 gnus-verbose-backends
118 (message "nnbabyl: Receiving headers...done"))
120 ;; Fold continuation lines.
121 (set-buffer nntp-server-buffer)
122 (goto-char (point-min))
123 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
124 (replace-match " " t t))
127 (defun nnbabyl-open-server (server &optional defs)
128 (nnheader-init-server-buffer)
129 (if (equal server nnbabyl-current-server)
131 (if nnbabyl-current-server
132 (setq nnbabyl-server-alist
133 (cons (list nnbabyl-current-server
134 (nnheader-save-variables nnbabyl-server-variables))
135 nnbabyl-server-alist)))
136 (let ((state (assoc server nnbabyl-server-alist)))
139 (nnheader-restore-variables (nth 1 state))
140 (setq nnbabyl-server-alist (delq state nnbabyl-server-alist)))
141 (nnheader-set-init-variables nnbabyl-server-variables defs)))
142 (setq nnbabyl-current-server server)))
144 (defun nnbabyl-close-server (&optional server)
145 ;; Restore buffer mode.
146 (when (and (nnbabyl-server-opened)
147 nnbabyl-previous-buffer-mode)
149 (set-buffer nnbabyl-mbox-buffer)
151 (car (car nnbabyl-previous-buffer-mode))
152 (cdr (car nnbabyl-previous-buffer-mode)))
153 (funcall (cdr nnbabyl-previous-buffer-mode))))
154 (setq nnbabyl-current-server nil
155 nnbabyl-mbox-buffer nil)
158 (defun nnbabyl-server-opened (&optional server)
159 (and (equal server nnbabyl-current-server)
161 (buffer-name nnbabyl-mbox-buffer)
163 (buffer-name nntp-server-buffer)))
165 (defun nnbabyl-status-message (&optional server)
166 nnbabyl-status-string)
168 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
169 (nnbabyl-possibly-change-newsgroup newsgroup)
171 (set-buffer nnbabyl-mbox-buffer)
172 (goto-char (point-min))
173 (if (search-forward (nnbabyl-article-string article) nil t)
174 (let (start stop summary-line)
175 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
176 (while (and (not (looking-at ".+:"))
177 (zerop (forward-line 1))))
179 (or (and (re-search-forward
180 (concat "^" nnbabyl-mail-delimiter) nil t)
182 (goto-char (point-max)))
184 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
185 (set-buffer nntp-server-buffer)
187 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
188 (goto-char (point-min))
189 ;; If there is an EOOH header, then we have to remove some
190 ;; duplicated headers.
191 (setq summary-line (looking-at "Summary-line:"))
192 (when (search-forward "\n*** EOOH ***" nil t)
194 ;; The headers to be deleted are located before the
196 (delete-region (point-min) (progn (forward-line 1)
199 (delete-region (progn (beginning-of-line) (point))
200 (or (search-forward "\n\n" nil t)
202 (if (numberp article)
203 (cons nnbabyl-current-group article)
204 (nnbabyl-article-group-number)))))))
206 (defun nnbabyl-request-group (group &optional server dont-check)
208 (if (nnbabyl-possibly-change-newsgroup group)
212 (set-buffer nntp-server-buffer)
214 (let ((active (assoc group nnbabyl-group-alist)))
215 (insert (format "211 %d %d %d %s\n"
216 (1+ (- (cdr (car (cdr active)))
217 (car (car (cdr active)))))
218 (car (car (cdr active)))
219 (cdr (car (cdr active)))
223 (defun nnbabyl-request-scan (&optional group server)
229 (set-buffer nnbabyl-mbox-buffer)
231 nnbabyl-mbox-file group
234 (let ((in-buf (current-buffer)))
235 (goto-char (point-min))
236 (while (search-forward "\n\^_\n" nil t)
238 (set-buffer nnbabyl-mbox-buffer)
239 (goto-char (point-max))
240 (search-backward "\n\^_" nil t)
241 (goto-char (match-end 0))
242 (insert-buffer-substring in-buf))))))
244 (defun nnbabyl-close-group (group &optional server)
247 (defun nnbabyl-request-create-group (group &optional server)
248 (nnmail-activate 'nnbabyl)
249 (or (assoc group nnbabyl-group-alist)
251 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 0)))
252 nnbabyl-group-alist))
253 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))
256 (defun nnbabyl-request-list (&optional server)
258 (or (nnmail-find-file nnbabyl-active-file)
260 (setq nnbabyl-group-alist (nnmail-get-active))
261 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
262 (nnmail-find-file nnbabyl-active-file)))))
264 (defun nnbabyl-request-newgroups (date &optional server)
265 (nnbabyl-request-list server))
267 (defun nnbabyl-request-list-newsgroups (&optional server)
268 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
271 (defun nnbabyl-request-post (&optional server)
272 (mail-send-and-exit nil))
274 (defun nnbabyl-request-expire-articles
275 (articles newsgroup &optional server force)
276 (nnbabyl-possibly-change-newsgroup newsgroup)
279 (nnmail-activate 'nnbabyl)
282 (set-buffer nnbabyl-mbox-buffer)
283 (set-text-properties (point-min) (point-max) nil)
284 (while (and articles is-old)
285 (goto-char (point-min))
286 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
288 (nnmail-expired-article-p
291 (point) (progn (end-of-line) (point))) force))
293 (and gnus-verbose-backends
294 (message "Deleting article %d in %s..."
295 (car articles) newsgroup))
296 (nnbabyl-delete-mail))
297 (setq rest (cons (car articles) rest))))
298 (setq articles (cdr articles)))
300 ;; Find the lowest active article in this group.
301 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
302 (goto-char (point-min))
303 (while (and (not (search-forward
304 (nnbabyl-article-string (car active)) nil t))
305 (<= (car active) (cdr active)))
306 (setcar active (1+ (car active)))
307 (goto-char (point-min))))
308 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
309 (nconc rest articles))))
311 (defun nnbabyl-request-move-article
312 (article group server accept-form &optional last)
313 (nnbabyl-possibly-change-newsgroup group)
314 (let ((buf (get-buffer-create " *nnbabyl move*"))
317 (nnbabyl-request-article article group server)
320 (insert-buffer-substring nntp-server-buffer)
321 (goto-char (point-min))
322 (if (re-search-forward
324 (save-excursion (search-forward "\n\n" nil t) (point)) t)
325 (delete-region (progn (beginning-of-line) (point))
326 (progn (forward-line 1) (point))))
327 (setq result (eval accept-form))
328 (kill-buffer (current-buffer))
331 (set-buffer nnbabyl-mbox-buffer)
332 (goto-char (point-min))
333 (if (search-forward (nnbabyl-article-string article) nil t)
334 (nnbabyl-delete-mail))
335 (and last (save-buffer))))
338 (defun nnbabyl-request-accept-article (group &optional last)
339 (let ((buf (current-buffer))
342 (nnmail-activate 'nnbabyl)
344 (goto-char (point-min))
345 (search-forward "\n\n" nil t)
348 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
349 (delete-region (point) (progn (forward-line 1) (point)))))
350 (let ((nnmail-split-methods
351 (if (stringp group) (list (list group ""))
352 nnmail-split-methods)))
353 (setq result (car (nnbabyl-save-mail))))
354 (set-buffer nnbabyl-mbox-buffer)
355 (goto-char (point-max))
356 (search-backward "\n\^_")
357 (goto-char (match-end 0))
358 (insert-buffer-substring buf)
362 nnbabyl-group-alist nnbabyl-active-file)))
365 (defun nnbabyl-request-replace-article (article group buffer)
366 (nnbabyl-possibly-change-newsgroup group)
368 (set-buffer nnbabyl-mbox-buffer)
369 (goto-char (point-min))
370 (if (not (search-forward (nnbabyl-article-string article) nil t))
372 (nnbabyl-delete-mail t t)
373 (insert-buffer-substring buffer)
377 (defun nnbabyl-request-delete-group (group &optional force server)
378 (nnbabyl-possibly-change-newsgroup group)
379 ;; Delete all articles in GROUP.
381 () ; Don't delete the articles.
383 (set-buffer nnbabyl-mbox-buffer)
384 (goto-char (point-min))
385 ;; Delete all articles in this group.
386 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
388 (while (search-forward ident nil t)
390 (nnbabyl-delete-mail))
391 (and found (save-buffer)))))
392 ;; Remove the group from all structures.
393 (setq nnbabyl-group-alist
394 (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
395 nnbabyl-current-group nil)
396 ;; Save the active file.
397 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
400 (defun nnbabyl-request-rename-group (group new-name &optional server)
401 (nnbabyl-possibly-change-newsgroup group)
403 (set-buffer nnbabyl-mbox-buffer)
404 (goto-char (point-min))
405 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
406 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
408 (while (search-forward ident nil t)
409 (replace-match new-ident t t)
411 (and found (save-buffer))))
412 (let ((entry (assoc group nnbabyl-group-alist)))
413 (and entry (setcar entry new-name))
414 (setq nnbabyl-current-group nil)
415 ;; Save the new group alist.
416 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
420 ;;; Internal functions.
422 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
423 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
425 (defun nnbabyl-delete-mail (&optional force leave-delim)
426 ;; Delete the current X-Gnus-Newsgroup line.
429 (progn (beginning-of-line) (point))
430 (progn (forward-line 1) (point))))
431 ;; Beginning of the article.
437 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
438 (if leave-delim (progn (forward-line 1) (point))
439 (match-beginning 0)))
442 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
444 (if (and (not (bobp)) leave-delim)
445 (progn (forward-line -2) (point))
446 (match-beginning 0)))
448 (goto-char (point-min))
449 ;; Only delete the article if no other groups owns it as well.
450 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
451 (delete-region (point-min) (point-max))))))
453 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
454 (if (or (not nnbabyl-mbox-buffer)
455 (not (buffer-name nnbabyl-mbox-buffer)))
456 (save-excursion (nnbabyl-read-mbox)))
457 (or nnbabyl-group-alist
458 (nnmail-activate 'nnbabyl))
460 (if (assoc newsgroup nnbabyl-group-alist)
461 (setq nnbabyl-current-group newsgroup)
462 (setq nnbabyl-status-string "No such group in file")
465 (defun nnbabyl-article-string (article)
466 (if (numberp article)
467 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
468 (int-to-string article) " ")
469 (concat "\nMessage-ID: " article)))
471 (defun nnbabyl-article-group-number ()
473 (goto-char (point-min))
474 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
476 (cons (buffer-substring (match-beginning 1) (match-end 1))
478 (buffer-substring (match-beginning 2) (match-end 2)))))))
480 (defun nnbabyl-insert-lines ()
481 "Insert how many lines and chars there are in the body of the mail."
484 (goto-char (point-min))
485 (if (search-forward "\n\n" nil t)
487 ;; There may be an EOOH line here...
488 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
489 (search-forward "\n\n" nil t))
490 (setq chars (- (point-max) (point)))
491 (setq lines (- (count-lines (point) (point-max)) 1))
492 ;; Move back to the end of the headers.
493 (goto-char (point-min))
494 (search-forward "\n\n" nil t)
497 (if (re-search-backward "^Lines: " nil t)
498 (delete-region (point) (progn (forward-line 1) (point)))))
499 (insert (format "Lines: %d\n" lines))
502 (defun nnbabyl-save-mail ()
503 ;; Called narrowed to an article.
504 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
505 (nnbabyl-insert-lines)
506 (nnmail-insert-xref group-art)
507 (nnbabyl-insert-newsgroup-line group-art)
508 (run-hooks 'nnbabyl-prepare-save-mail-hook)
511 (defun nnbabyl-insert-newsgroup-line (group-art)
513 (goto-char (point-min))
514 (while (looking-at "From ")
515 (replace-match "Mail-from: From " t t)
517 ;; If there is a C-l at the beginning of the narrowed region, this
518 ;; isn't really a "save", but rather a "scan".
519 (goto-char (point-min))
520 (or (looking-at "\^L")
522 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
523 (goto-char (point-max))
525 (if (search-forward "\n\n" nil t)
529 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
530 (car (car group-art)) (cdr (car group-art))
531 (current-time-string)))
532 (setq group-art (cdr group-art)))))
535 (defun nnbabyl-active-number (group)
536 ;; Find the next article number in GROUP.
537 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
539 (setcdr active (1+ (cdr active)))
540 ;; This group is new, so we create a new entry for it.
541 ;; This might be a bit naughty... creating groups on the drop of
542 ;; a hat, but I don't know...
543 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 1)))
544 nnbabyl-group-alist)))
547 (defun nnbabyl-read-mbox ()
548 (nnmail-activate 'nnbabyl)
549 (or (file-exists-p nnbabyl-mbox-file)
551 (set-buffer (setq nnbabyl-mbox-buffer
552 (create-file-buffer nnbabyl-mbox-file)))
553 (setq buffer-file-name nnbabyl-mbox-file)
554 (insert "BABYL OPTIONS:\n\n\^_")
555 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
557 (if (and nnbabyl-mbox-buffer
558 (buffer-name nnbabyl-mbox-buffer)
560 (set-buffer nnbabyl-mbox-buffer)
561 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
564 (let ((delim (concat "^" nnbabyl-mail-delimiter))
566 (set-buffer (setq nnbabyl-mbox-buffer
567 (nnheader-find-file-noselect
568 nnbabyl-mbox-file nil 'raw)))
570 (setq nnbabyl-previous-buffer-mode
571 (cons (cons (point-min) (point-max))
574 (buffer-disable-undo (current-buffer))
576 (setq buffer-read-only nil)
579 (goto-char (point-min))
580 (re-search-forward delim nil t)
581 (setq start (match-end 0))
582 (while (re-search-forward delim nil t)
583 (setq end (match-end 0))
584 (or (search-backward "\nX-Gnus-Newsgroup: " start t)
590 (narrow-to-region start end)
592 (setq end (point-max))))))
593 (goto-char (setq start end)))
594 (and (buffer-modified-p (current-buffer)) (save-buffer))
595 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
597 (defun nnbabyl-remove-incoming-delims ()
598 (goto-char (point-min))
599 (while (search-forward "\^_" nil t)
600 (replace-match "?" t t)))
604 ;;; nnbabyl.el ends here