1 ;;; nnbabyl.el --- rmail mbox access for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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
32 (eval-when-compile (require 'cl))
38 5 "Ignore rmail errors from this file, you don't have rmail")))
42 (nnoo-declare nnbabyl)
44 (defvoo nnbabyl-mbox-file (expand-file-name "~/RMAIL")
45 "The name of the rmail box file in the users home directory.")
47 (defvoo nnbabyl-active-file (expand-file-name "~/.rmail-active")
48 "The name of the active file for the rmail box.")
50 (defvoo nnbabyl-get-new-mail t
51 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
53 (defvoo nnbabyl-prepare-save-mail-hook nil
54 "Hook run narrowed to an article before saving.")
58 (defvar nnbabyl-mail-delimiter "\^_")
60 (defconst nnbabyl-version "nnbabyl 1.0"
63 (defvoo nnbabyl-mbox-buffer nil)
64 (defvoo nnbabyl-current-group nil)
65 (defvoo nnbabyl-status-string "")
66 (defvoo nnbabyl-group-alist nil)
67 (defvoo nnbabyl-active-timestamp nil)
69 (defvoo nnbabyl-previous-buffer-mode nil)
72 (autoload 'gnus-set-text-properties "gnus-ems"))
76 ;;; Interface functions
78 (nnoo-define-basics nnbabyl)
80 (deffoo nnbabyl-retrieve-headers (articles &optional group server fetch-old)
82 (set-buffer nntp-server-buffer)
84 (let ((number (length articles))
86 (delim (concat "^" nnbabyl-mail-delimiter))
87 article art-string start stop)
88 (nnbabyl-possibly-change-newsgroup group server)
89 (while (setq article (pop articles))
90 (setq art-string (nnbabyl-article-string article))
91 (set-buffer nnbabyl-mbox-buffer)
93 (when (or (search-forward art-string nil t)
94 (search-backward art-string nil t))
95 (unless (re-search-backward delim nil t)
96 (goto-char (point-min)))
97 (while (and (not (looking-at ".+:"))
98 (zerop (forward-line 1))))
100 (search-forward "\n\n" nil t)
101 (setq stop (1- (point)))
102 (set-buffer nntp-server-buffer)
104 (princ article (current-buffer))
105 (insert " Article retrieved.\n")
106 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
107 (goto-char (point-max))
109 (and (numberp nnmail-large-newsgroup)
110 (> number nnmail-large-newsgroup)
111 (zerop (% (incf count) 20))
112 (nnheader-message 5 "nnbabyl: Receiving headers... %d%%"
113 (/ (* count 100) number))))
115 (and (numberp nnmail-large-newsgroup)
116 (> number nnmail-large-newsgroup)
117 (nnheader-message 5 "nnbabyl: Receiving headers...done"))
119 (set-buffer nntp-server-buffer)
120 (nnheader-fold-continuation-lines)
123 (deffoo nnbabyl-open-server (server &optional defs)
124 (nnoo-change-server 'nnbabyl server defs)
125 (nnbabyl-create-mbox)
127 ((not (file-exists-p nnbabyl-mbox-file))
128 (nnbabyl-close-server)
129 (nnheader-report 'nnbabyl "No such file: %s" nnbabyl-mbox-file))
130 ((file-directory-p nnbabyl-mbox-file)
131 (nnbabyl-close-server)
132 (nnheader-report 'nnbabyl "Not a regular file: %s" nnbabyl-mbox-file))
134 (nnheader-report 'nnbabyl "Opened server %s using mbox %s" server
138 (deffoo nnbabyl-close-server (&optional server)
139 ;; Restore buffer mode.
140 (when (and (nnbabyl-server-opened)
141 nnbabyl-previous-buffer-mode)
143 (set-buffer nnbabyl-mbox-buffer)
145 (caar nnbabyl-previous-buffer-mode)
146 (cdar nnbabyl-previous-buffer-mode))
147 (funcall (cdr nnbabyl-previous-buffer-mode))))
148 (nnoo-close-server 'nnbabyl server)
149 (setq nnbabyl-mbox-buffer nil)
152 (deffoo nnbabyl-server-opened (&optional server)
153 (and (nnoo-current-server-p 'nnbabyl server)
155 (buffer-name nnbabyl-mbox-buffer)
157 (buffer-name nntp-server-buffer)))
159 (deffoo nnbabyl-request-article (article &optional newsgroup server buffer)
160 (nnbabyl-possibly-change-newsgroup newsgroup server)
162 (set-buffer nnbabyl-mbox-buffer)
163 (goto-char (point-min))
164 (when (search-forward (nnbabyl-article-string article) nil t)
165 (let (start stop summary-line)
166 (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
167 (goto-char (point-min))
169 (while (and (not (looking-at ".+:"))
170 (zerop (forward-line 1))))
172 (or (when (re-search-forward
173 (concat "^" nnbabyl-mail-delimiter) nil t)
176 (goto-char (point-max)))
178 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
179 (set-buffer nntp-server-buffer)
181 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
182 (goto-char (point-min))
183 ;; If there is an EOOH header, then we have to remove some
184 ;; duplicated headers.
185 (setq summary-line (looking-at "Summary-line:"))
186 (when (search-forward "\n*** EOOH ***" nil t)
188 ;; The headers to be deleted are located before the
190 (delete-region (point-min) (progn (forward-line 1)
193 (delete-region (progn (beginning-of-line) (point))
194 (or (search-forward "\n\n" nil t)
196 (if (numberp article)
197 (cons nnbabyl-current-group article)
198 (nnbabyl-article-group-number)))))))
200 (deffoo nnbabyl-request-group (group &optional server dont-check)
201 (let ((active (cadr (assoc group nnbabyl-group-alist))))
205 (null (nnbabyl-possibly-change-newsgroup group server)))
206 (nnheader-report 'nnbabyl "No such group: %s" group))
208 (nnheader-report 'nnbabyl "Selected group %s" group)
209 (nnheader-insert ""))
211 (nnheader-report 'nnbabyl "Selected group %s" group)
212 (nnheader-insert "211 %d %d %d %s\n"
213 (1+ (- (cdr active) (car active)))
214 (car active) (cdr active) group))))))
216 (deffoo nnbabyl-request-scan (&optional group server)
217 (nnbabyl-possibly-change-newsgroup group server)
223 (set-buffer nnbabyl-mbox-buffer)
225 (file-name-directory nnbabyl-mbox-file)
229 (let ((in-buf (current-buffer)))
230 (goto-char (point-min))
231 (while (search-forward "\n\^_\n" nil t)
233 (set-buffer nnbabyl-mbox-buffer)
234 (goto-char (point-max))
235 (search-backward "\n\^_" nil t)
236 (goto-char (match-end 0))
237 (insert-buffer-substring in-buf)))
238 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))))
240 (deffoo nnbabyl-close-group (group &optional server)
243 (deffoo nnbabyl-request-create-group (group &optional server args)
244 (nnmail-activate 'nnbabyl)
245 (unless (assoc group nnbabyl-group-alist)
246 (push (list group (cons 1 0))
248 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
251 (deffoo nnbabyl-request-list (&optional server)
253 (nnmail-find-file nnbabyl-active-file)
254 (setq nnbabyl-group-alist (nnmail-get-active))
257 (deffoo nnbabyl-request-newgroups (date &optional server)
258 (nnbabyl-request-list server))
260 (deffoo nnbabyl-request-list-newsgroups (&optional server)
261 (nnheader-report 'nnbabyl "nnbabyl: LIST NEWSGROUPS is not implemented."))
263 (deffoo nnbabyl-request-expire-articles
264 (articles newsgroup &optional server force)
265 (nnbabyl-possibly-change-newsgroup newsgroup server)
268 (nnmail-activate 'nnbabyl)
271 (set-buffer nnbabyl-mbox-buffer)
272 (gnus-set-text-properties (point-min) (point-max) nil)
273 (while (and articles is-old)
274 (goto-char (point-min))
275 (when (search-forward (nnbabyl-article-string (car articles)) nil t)
277 (nnmail-expired-article-p
280 (point) (progn (end-of-line) (point))) force))
282 (nnheader-message 5 "Deleting article %d in %s..."
283 (car articles) newsgroup)
284 (nnbabyl-delete-mail))
285 (push (car articles) rest)))
286 (setq articles (cdr articles)))
288 ;; Find the lowest active article in this group.
289 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
290 (goto-char (point-min))
291 (while (and (not (search-forward
292 (nnbabyl-article-string (car active)) nil t))
293 (<= (car active) (cdr active)))
294 (setcar active (1+ (car active)))
295 (goto-char (point-min))))
296 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
297 (nconc rest articles))))
299 (deffoo nnbabyl-request-move-article
300 (article group server accept-form &optional last)
301 (let ((buf (get-buffer-create " *nnbabyl move*"))
304 (nnbabyl-request-article article group server)
307 (insert-buffer-substring nntp-server-buffer)
308 (goto-char (point-min))
309 (while (re-search-forward
311 (save-excursion (search-forward "\n\n" nil t) (point)) t)
312 (delete-region (progn (beginning-of-line) (point))
313 (progn (forward-line 1) (point))))
314 (setq result (eval accept-form))
315 (kill-buffer (current-buffer))
318 (nnbabyl-possibly-change-newsgroup group server)
319 (set-buffer nnbabyl-mbox-buffer)
320 (goto-char (point-min))
321 (if (search-forward (nnbabyl-article-string article) nil t)
322 (nnbabyl-delete-mail))
323 (and last (save-buffer))))
326 (deffoo nnbabyl-request-accept-article (group &optional server last)
327 (nnbabyl-possibly-change-newsgroup group server)
328 (nnmail-check-syntax)
329 (let ((buf (current-buffer))
332 (nnmail-activate 'nnbabyl)
334 (goto-char (point-min))
335 (search-forward "\n\n" nil t)
338 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
339 (delete-region (point) (progn (forward-line 1) (point)))))
340 (when nnmail-cache-accepted-message-ids
341 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
344 (list (cons group (nnbabyl-active-number group)))
345 (nnmail-article-group 'nnbabyl-active-number)))
346 (if (and (null result)
347 (yes-or-no-p "Moved to `junk' group; delete article? "))
349 (setq result (car (nnbabyl-save-mail result))))
350 (set-buffer nnbabyl-mbox-buffer)
351 (goto-char (point-max))
352 (search-backward "\n\^_")
353 (goto-char (match-end 0))
354 (insert-buffer-substring buf)
356 (when nnmail-cache-accepted-message-ids
357 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
359 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
362 (deffoo nnbabyl-request-replace-article (article group buffer)
363 (nnbabyl-possibly-change-newsgroup group)
365 (set-buffer nnbabyl-mbox-buffer)
366 (goto-char (point-min))
367 (if (not (search-forward (nnbabyl-article-string article) nil t))
369 (nnbabyl-delete-mail t t)
370 (insert-buffer-substring buffer)
374 (deffoo nnbabyl-request-delete-group (group &optional force server)
375 (nnbabyl-possibly-change-newsgroup group server)
376 ;; Delete all articles in GROUP.
378 () ; Don't delete the articles.
380 (set-buffer nnbabyl-mbox-buffer)
381 (goto-char (point-min))
382 ;; Delete all articles in this group.
383 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
385 (while (search-forward ident nil t)
387 (nnbabyl-delete-mail))
390 ;; Remove the group from all structures.
391 (setq nnbabyl-group-alist
392 (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
393 nnbabyl-current-group nil)
394 ;; Save the active file.
395 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
398 (deffoo nnbabyl-request-rename-group (group new-name &optional server)
399 (nnbabyl-possibly-change-newsgroup group server)
401 (set-buffer nnbabyl-mbox-buffer)
402 (goto-char (point-min))
403 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
404 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
406 (while (search-forward ident nil t)
407 (replace-match new-ident t t)
411 (let ((entry (assoc group nnbabyl-group-alist)))
412 (and entry (setcar entry new-name))
413 (setq nnbabyl-current-group nil)
414 ;; Save the new group alist.
415 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
419 ;;; Internal functions.
421 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
422 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
424 (defun nnbabyl-delete-mail (&optional force leave-delim)
425 ;; Delete the current X-Gnus-Newsgroup line.
428 (progn (beginning-of-line) (point))
429 (progn (forward-line 1) (point))))
430 ;; Beginning of the article.
436 (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
437 (goto-char (point-min))
439 (if leave-delim (progn (forward-line 1) (point))
440 (match-beginning 0)))
443 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
447 (goto-char (point-min))
448 ;; Only delete the article if no other groups owns it as well.
449 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
450 (delete-region (point-min) (point-max))))))
452 (defun nnbabyl-possibly-change-newsgroup (newsgroup &optional server)
454 (not (nnbabyl-server-opened server)))
455 (nnbabyl-open-server server))
456 (when (or (not nnbabyl-mbox-buffer)
457 (not (buffer-name nnbabyl-mbox-buffer)))
458 (save-excursion (nnbabyl-read-mbox)))
459 (unless nnbabyl-group-alist
460 (nnmail-activate 'nnbabyl))
462 (if (assoc newsgroup nnbabyl-group-alist)
463 (setq nnbabyl-current-group newsgroup)
464 (nnheader-report 'nnbabyl "No such group in file"))
467 (defun nnbabyl-article-string (article)
468 (if (numberp article)
469 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
470 (int-to-string article) " ")
471 (concat "\nMessage-ID: " article)))
473 (defun nnbabyl-article-group-number ()
475 (goto-char (point-min))
476 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
478 (cons (buffer-substring (match-beginning 1) (match-end 1))
480 (buffer-substring (match-beginning 2) (match-end 2)))))))
482 (defun nnbabyl-insert-lines ()
483 "Insert how many lines and chars there are in the body of the mail."
486 (goto-char (point-min))
487 (when (search-forward "\n\n" nil t)
488 ;; There may be an EOOH line here...
489 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
490 (search-forward "\n\n" nil t))
491 (setq chars (- (point-max) (point))
492 lines (max (- (count-lines (point) (point-max)) 1) 0))
493 ;; Move back to the end of the headers.
494 (goto-char (point-min))
495 (search-forward "\n\n" nil t)
498 (when (re-search-backward "^Lines: " nil t)
499 (delete-region (point) (progn (forward-line 1) (point)))))
500 (insert (format "Lines: %d\n" lines))
503 (defun nnbabyl-save-mail (group-art)
504 ;; Called narrowed to an article.
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 (unless (looking-at "\^L")
522 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
523 (goto-char (point-max))
525 (when (search-forward "\n\n" nil t)
528 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
529 (caar group-art) (cdar group-art)
530 (current-time-string)))
531 (setq group-art (cdr group-art))))
534 (defun nnbabyl-active-number (group)
535 ;; Find the next article number in GROUP.
536 (let ((active (cadr (assoc group nnbabyl-group-alist))))
538 (setcdr active (1+ (cdr active)))
539 ;; This group is new, so we create a new entry for it.
540 ;; This might be a bit naughty... creating groups on the drop of
541 ;; a hat, but I don't know...
542 (push (list group (setq active (cons 1 1)))
543 nnbabyl-group-alist))
546 (defun nnbabyl-create-mbox ()
547 (unless (file-exists-p nnbabyl-mbox-file)
548 ;; Create a new, empty RMAIL mbox file.
550 (set-buffer (setq nnbabyl-mbox-buffer
551 (create-file-buffer nnbabyl-mbox-file)))
552 (setq buffer-file-name nnbabyl-mbox-file)
553 (insert "BABYL OPTIONS:\n\n\^_")
555 (point-min) (point-max) nnbabyl-mbox-file t 'nomesg))))
557 (defun nnbabyl-read-mbox ()
558 (nnmail-activate 'nnbabyl)
559 (nnbabyl-create-mbox)
561 (unless (and nnbabyl-mbox-buffer
562 (buffer-name nnbabyl-mbox-buffer)
564 (set-buffer nnbabyl-mbox-buffer)
565 (= (buffer-size) (nnheader-file-size nnbabyl-mbox-file))))
566 ;; This buffer has changed since we read it last. Possibly.
568 (let ((delim (concat "^" nnbabyl-mail-delimiter))
569 (alist nnbabyl-group-alist)
571 (set-buffer (setq nnbabyl-mbox-buffer
572 (nnheader-find-file-noselect
573 nnbabyl-mbox-file nil t)))
574 ;; Save previous buffer mode.
575 (setq nnbabyl-previous-buffer-mode
576 (cons (cons (point-min) (point-max))
579 (buffer-disable-undo)
581 (setq buffer-read-only nil)
584 ;; Go through the group alist and compare against
587 (goto-char (point-max))
588 (when (and (re-search-backward
589 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
595 (match-beginning 1) (match-end 1))))
597 (setcdr (cadar alist) number))
598 (setq alist (cdr alist)))
600 ;; We go through the mbox and make sure that each and
601 ;; every mail belongs to some group or other.
602 (goto-char (point-min))
603 (if (looking-at "\^L")
605 (re-search-forward delim nil t)
606 (setq start (match-end 0)))
607 (while (re-search-forward delim nil t)
608 (setq end (match-end 0))
609 (unless (search-backward "\nX-Gnus-Newsgroup: " start t)
613 (narrow-to-region (goto-char start) end)
615 (nnmail-article-group 'nnbabyl-active-number))
616 (setq end (point-max)))))
617 (goto-char (setq start end)))
618 (when (buffer-modified-p (current-buffer))
620 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
622 (defun nnbabyl-remove-incoming-delims ()
623 (goto-char (point-min))
624 (while (search-forward "\^_" nil t)
625 (replace-match "?" t t)))
627 (defun nnbabyl-check-mbox ()
628 "Go through the nnbabyl mbox and make sure that no article numbers are reused."
630 (let ((idents (make-vector 1000 0))
633 (when (or (not nnbabyl-mbox-buffer)
634 (not (buffer-name nnbabyl-mbox-buffer)))
636 (set-buffer nnbabyl-mbox-buffer)
637 (goto-char (point-min))
638 (while (re-search-forward "^X-Gnus-Newsgroup: \\([^ ]+\\) " nil t)
639 (if (intern-soft (setq id (match-string 1)) idents)
641 (delete-region (progn (beginning-of-line) (point))
642 (progn (forward-line 1) (point)))
643 (nnheader-message 7 "Moving %s..." id)
645 (nnmail-article-group 'nnbabyl-active-number)))
647 (when (buffer-modified-p (current-buffer))
649 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
650 (nnheader-message 5 ""))))
654 ;;; nnbabyl.el ends here