1 ;;; nnbabyl.el --- rmail mbox access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1099, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: news, mail
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; For an overview of what the interface functions do, please see the
38 5 "Ignore rmail errors from this file, you don't have rmail")))
41 (eval-when-compile (require 'cl
))
43 (nnoo-declare nnbabyl
)
45 (defvoo nnbabyl-mbox-file
(expand-file-name "~/RMAIL")
46 "The name of the rmail box file in the users home directory.")
48 (defvoo nnbabyl-active-file
(expand-file-name "~/.rmail-active")
49 "The name of the active file for the rmail box.")
51 (defvoo nnbabyl-get-new-mail t
52 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
54 (defvoo nnbabyl-prepare-save-mail-hook nil
55 "Hook run narrowed to an article before saving.")
59 (defvar nnbabyl-mail-delimiter
"\^_")
61 (defconst nnbabyl-version
"nnbabyl 1.0"
64 (defvoo nnbabyl-mbox-buffer nil
)
65 (defvoo nnbabyl-current-group nil
)
66 (defvoo nnbabyl-status-string
"")
67 (defvoo nnbabyl-group-alist nil
)
68 (defvoo nnbabyl-active-timestamp nil
)
70 (defvoo nnbabyl-previous-buffer-mode nil
)
73 (autoload 'gnus-set-text-properties
"gnus-ems"))
77 ;;; Interface functions
79 (nnoo-define-basics nnbabyl
)
81 (deffoo nnbabyl-retrieve-headers
(articles &optional group server fetch-old
)
83 (set-buffer nntp-server-buffer
)
85 (let ((number (length articles
))
87 (delim (concat "^" nnbabyl-mail-delimiter
))
88 article art-string start stop
)
89 (nnbabyl-possibly-change-newsgroup group server
)
90 (while (setq article
(pop articles
))
91 (setq art-string
(nnbabyl-article-string article
))
92 (set-buffer nnbabyl-mbox-buffer
)
94 (when (or (search-forward art-string nil t
)
95 (search-backward art-string nil t
))
96 (unless (re-search-backward delim nil t
)
97 (goto-char (point-min)))
98 (while (and (not (looking-at ".+:"))
99 (zerop (forward-line 1))))
101 (search-forward "\n\n" nil t
)
102 (setq stop
(1- (point)))
103 (set-buffer nntp-server-buffer
)
105 (princ article
(current-buffer))
106 (insert " Article retrieved.\n")
107 (insert-buffer-substring nnbabyl-mbox-buffer start stop
)
108 (goto-char (point-max))
110 (and (numberp nnmail-large-newsgroup
)
111 (> number nnmail-large-newsgroup
)
112 (zerop (%
(incf count
) 20))
113 (nnheader-message 5 "nnbabyl: Receiving headers... %d%%"
114 (/ (* count
100) number
))))
116 (and (numberp nnmail-large-newsgroup
)
117 (> number nnmail-large-newsgroup
)
118 (nnheader-message 5 "nnbabyl: Receiving headers...done"))
120 (set-buffer nntp-server-buffer
)
121 (nnheader-fold-continuation-lines)
124 (deffoo nnbabyl-open-server
(server &optional defs
)
125 (nnoo-change-server 'nnbabyl server defs
)
126 (nnbabyl-create-mbox)
128 ((not (file-exists-p nnbabyl-mbox-file
))
129 (nnbabyl-close-server)
130 (nnheader-report 'nnbabyl
"No such file: %s" nnbabyl-mbox-file
))
131 ((file-directory-p nnbabyl-mbox-file
)
132 (nnbabyl-close-server)
133 (nnheader-report 'nnbabyl
"Not a regular file: %s" nnbabyl-mbox-file
))
135 (nnheader-report 'nnbabyl
"Opened server %s using mbox %s" server
139 (deffoo nnbabyl-close-server
(&optional server
)
140 ;; Restore buffer mode.
141 (when (and (nnbabyl-server-opened)
142 nnbabyl-previous-buffer-mode
)
144 (set-buffer nnbabyl-mbox-buffer
)
146 (caar nnbabyl-previous-buffer-mode
)
147 (cdar nnbabyl-previous-buffer-mode
))
148 (funcall (cdr nnbabyl-previous-buffer-mode
))))
149 (nnoo-close-server 'nnbabyl server
)
150 (setq nnbabyl-mbox-buffer nil
)
153 (deffoo nnbabyl-server-opened
(&optional server
)
154 (and (nnoo-current-server-p 'nnbabyl server
)
156 (buffer-name nnbabyl-mbox-buffer
)
158 (buffer-name nntp-server-buffer
)))
160 (deffoo nnbabyl-request-article
(article &optional newsgroup server buffer
)
161 (nnbabyl-possibly-change-newsgroup newsgroup server
)
163 (set-buffer nnbabyl-mbox-buffer
)
164 (goto-char (point-min))
165 (when (search-forward (nnbabyl-article-string article
) nil t
)
166 (let (start stop summary-line
)
167 (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter
) nil t
)
168 (goto-char (point-min))
170 (while (and (not (looking-at ".+:"))
171 (zerop (forward-line 1))))
173 (or (when (re-search-forward
174 (concat "^" nnbabyl-mail-delimiter
) nil t
)
177 (goto-char (point-max)))
179 (let ((nntp-server-buffer (or buffer nntp-server-buffer
)))
180 (set-buffer nntp-server-buffer
)
182 (insert-buffer-substring nnbabyl-mbox-buffer start stop
)
183 (goto-char (point-min))
184 ;; If there is an EOOH header, then we have to remove some
185 ;; duplicated headers.
186 (setq summary-line
(looking-at "Summary-line:"))
187 (when (search-forward "\n*** EOOH ***" nil t
)
189 ;; The headers to be deleted are located before the
191 (delete-region (point-min) (progn (forward-line 1)
194 (delete-region (progn (beginning-of-line) (point))
195 (or (search-forward "\n\n" nil t
)
197 (if (numberp article
)
198 (cons nnbabyl-current-group article
)
199 (nnbabyl-article-group-number)))))))
201 (deffoo nnbabyl-request-group
(group &optional server dont-check
)
202 (let ((active (cadr (assoc group nnbabyl-group-alist
))))
206 (null (nnbabyl-possibly-change-newsgroup group server
)))
207 (nnheader-report 'nnbabyl
"No such group: %s" group
))
209 (nnheader-report 'nnbabyl
"Selected group %s" group
)
210 (nnheader-insert ""))
212 (nnheader-report 'nnbabyl
"Selected group %s" group
)
213 (nnheader-insert "211 %d %d %d %s\n"
214 (1+ (- (cdr active
) (car active
)))
215 (car active
) (cdr active
) group
))))))
217 (deffoo nnbabyl-request-scan
(&optional group server
)
218 (nnbabyl-possibly-change-newsgroup group server
)
224 (set-buffer nnbabyl-mbox-buffer
)
226 (file-name-directory nnbabyl-mbox-file
)
230 (let ((in-buf (current-buffer)))
231 (goto-char (point-min))
232 (while (search-forward "\n\^_\n" nil t
)
234 (set-buffer nnbabyl-mbox-buffer
)
235 (goto-char (point-max))
236 (search-backward "\n\^_" nil t
)
237 (goto-char (match-end 0))
238 (insert-buffer-substring in-buf
)))
239 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
))))
241 (deffoo nnbabyl-close-group
(group &optional server
)
244 (deffoo nnbabyl-request-create-group
(group &optional server args
)
245 (nnmail-activate 'nnbabyl
)
246 (unless (assoc group nnbabyl-group-alist
)
247 (push (list group
(cons 1 0))
249 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
))
252 (deffoo nnbabyl-request-list
(&optional server
)
254 (nnmail-find-file nnbabyl-active-file
)
255 (setq nnbabyl-group-alist
(nnmail-get-active))
258 (deffoo nnbabyl-request-newgroups
(date &optional server
)
259 (nnbabyl-request-list server
))
261 (deffoo nnbabyl-request-list-newsgroups
(&optional server
)
262 (nnheader-report 'nnbabyl
"nnbabyl: LIST NEWSGROUPS is not implemented."))
264 (deffoo nnbabyl-request-expire-articles
265 (articles newsgroup
&optional server force
)
266 (nnbabyl-possibly-change-newsgroup newsgroup server
)
269 (nnmail-activate 'nnbabyl
)
272 (set-buffer nnbabyl-mbox-buffer
)
273 (gnus-set-text-properties (point-min) (point-max) nil
)
274 (while (and articles is-old
)
275 (goto-char (point-min))
276 (when (search-forward (nnbabyl-article-string (car articles
)) nil t
)
278 (nnmail-expired-article-p
281 (point) (progn (end-of-line) (point))) force
))
283 (unless (eq nnmail-expiry-target
'delete
)
285 (nnbabyl-request-article (car articles
)
288 (let ((nnml-current-directory nil
))
289 (nnmail-expiry-target-group
290 nnmail-expiry-target newsgroup
))))
291 (nnheader-message 5 "Deleting article %d in %s..."
292 (car articles
) newsgroup
)
293 (nnbabyl-delete-mail))
294 (push (car articles
) rest
)))
295 (setq articles
(cdr articles
)))
297 ;; Find the lowest active article in this group.
298 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist
))))
299 (goto-char (point-min))
300 (while (and (not (search-forward
301 (nnbabyl-article-string (car active
)) nil t
))
302 (<= (car active
) (cdr active
)))
303 (setcar active
(1+ (car active
)))
304 (goto-char (point-min))))
305 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
)
306 (nconc rest articles
))))
308 (deffoo nnbabyl-request-move-article
309 (article group server accept-form
&optional last
)
310 (let ((buf (get-buffer-create " *nnbabyl move*"))
313 (nnbabyl-request-article article group server
)
316 (insert-buffer-substring nntp-server-buffer
)
317 (goto-char (point-min))
318 (while (re-search-forward
320 (save-excursion (search-forward "\n\n" nil t
) (point)) t
)
321 (delete-region (progn (beginning-of-line) (point))
322 (progn (forward-line 1) (point))))
323 (setq result
(eval accept-form
))
324 (kill-buffer (current-buffer))
327 (nnbabyl-possibly-change-newsgroup group server
)
328 (set-buffer nnbabyl-mbox-buffer
)
329 (goto-char (point-min))
330 (if (search-forward (nnbabyl-article-string article
) nil t
)
331 (nnbabyl-delete-mail))
332 (and last
(save-buffer))))
335 (deffoo nnbabyl-request-accept-article
(group &optional server last
)
336 (nnbabyl-possibly-change-newsgroup group server
)
337 (nnmail-check-syntax)
338 (let ((buf (current-buffer))
341 (nnmail-activate 'nnbabyl
)
343 (goto-char (point-min))
344 (search-forward "\n\n" nil t
)
347 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t
)
348 (delete-region (point) (progn (forward-line 1) (point)))))
349 (when nnmail-cache-accepted-message-ids
350 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
353 (list (cons group
(nnbabyl-active-number group
)))
354 (nnmail-article-group 'nnbabyl-active-number
)))
355 (if (and (null result
)
356 (yes-or-no-p "Moved to `junk' group; delete article? "))
358 (setq result
(car (nnbabyl-save-mail result
))))
359 (set-buffer nnbabyl-mbox-buffer
)
360 (goto-char (point-max))
361 (search-backward "\n\^_")
362 (goto-char (match-end 0))
363 (insert-buffer-substring buf
)
365 (when nnmail-cache-accepted-message-ids
366 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
368 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
))
371 (deffoo nnbabyl-request-replace-article
(article group buffer
)
372 (nnbabyl-possibly-change-newsgroup group
)
374 (set-buffer nnbabyl-mbox-buffer
)
375 (goto-char (point-min))
376 (if (not (search-forward (nnbabyl-article-string article
) nil t
))
378 (nnbabyl-delete-mail t t
)
379 (insert-buffer-substring buffer
)
383 (deffoo nnbabyl-request-delete-group
(group &optional force server
)
384 (nnbabyl-possibly-change-newsgroup group server
)
385 ;; Delete all articles in GROUP.
387 () ; Don't delete the articles.
389 (set-buffer nnbabyl-mbox-buffer
)
390 (goto-char (point-min))
391 ;; Delete all articles in this group.
392 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group
":"))
394 (while (search-forward ident nil t
)
396 (nnbabyl-delete-mail))
399 ;; Remove the group from all structures.
400 (setq nnbabyl-group-alist
401 (delq (assoc group nnbabyl-group-alist
) nnbabyl-group-alist
)
402 nnbabyl-current-group nil
)
403 ;; Save the active file.
404 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
)
407 (deffoo nnbabyl-request-rename-group
(group new-name
&optional server
)
408 (nnbabyl-possibly-change-newsgroup group server
)
410 (set-buffer nnbabyl-mbox-buffer
)
411 (goto-char (point-min))
412 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group
":"))
413 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name
":"))
415 (while (search-forward ident nil t
)
416 (replace-match new-ident t t
)
420 (let ((entry (assoc group nnbabyl-group-alist
)))
421 (and entry
(setcar entry new-name
))
422 (setq nnbabyl-current-group nil
)
423 ;; Save the new group alist.
424 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
)
428 ;;; Internal functions.
430 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
431 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
433 (defun nnbabyl-delete-mail (&optional force leave-delim
)
434 ;; Delete the current X-Gnus-Newsgroup line.
437 (progn (beginning-of-line) (point))
438 (progn (forward-line 1) (point))))
439 ;; Beginning of the article.
445 (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter
) nil t
)
446 (goto-char (point-min))
448 (if leave-delim
(progn (forward-line 1) (point))
449 (match-beginning 0)))
452 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter
)
456 (goto-char (point-min))
457 ;; Only delete the article if no other groups owns it as well.
458 (when (or force
(not (re-search-forward "^X-Gnus-Newsgroup: " nil t
)))
459 (delete-region (point-min) (point-max))))))
461 (defun nnbabyl-possibly-change-newsgroup (newsgroup &optional server
)
463 (not (nnbabyl-server-opened server
)))
464 (nnbabyl-open-server server
))
465 (when (or (not nnbabyl-mbox-buffer
)
466 (not (buffer-name nnbabyl-mbox-buffer
)))
467 (save-excursion (nnbabyl-read-mbox)))
468 (unless nnbabyl-group-alist
469 (nnmail-activate 'nnbabyl
))
471 (if (assoc newsgroup nnbabyl-group-alist
)
472 (setq nnbabyl-current-group newsgroup
)
473 (nnheader-report 'nnbabyl
"No such group in file"))
476 (defun nnbabyl-article-string (article)
477 (if (numberp article
)
478 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group
":"
479 (int-to-string article
) " ")
480 (concat "\nMessage-ID: " article
)))
482 (defun nnbabyl-article-group-number ()
484 (goto-char (point-min))
485 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
487 (cons (buffer-substring (match-beginning 1) (match-end 1))
489 (buffer-substring (match-beginning 2) (match-end 2)))))))
491 (defun nnbabyl-insert-lines ()
492 "Insert how many lines and chars there are in the body of the mail."
495 (goto-char (point-min))
496 (when (search-forward "\n\n" nil t
)
497 ;; There may be an EOOH line here...
498 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
499 (search-forward "\n\n" nil t
))
500 (setq chars
(- (point-max) (point))
501 lines
(max (- (count-lines (point) (point-max)) 1) 0))
502 ;; Move back to the end of the headers.
503 (goto-char (point-min))
504 (search-forward "\n\n" nil t
)
507 (when (re-search-backward "^Lines: " nil t
)
508 (delete-region (point) (progn (forward-line 1) (point)))))
509 (insert (format "Lines: %d\n" lines
))
512 (defun nnbabyl-save-mail (group-art)
513 ;; Called narrowed to an article.
514 (nnbabyl-insert-lines)
515 (nnmail-insert-xref group-art
)
516 (nnbabyl-insert-newsgroup-line group-art
)
517 (run-hooks 'nnbabyl-prepare-save-mail-hook
)
520 (defun nnbabyl-insert-newsgroup-line (group-art)
522 (goto-char (point-min))
523 (while (looking-at "From ")
524 (replace-match "Mail-from: From " t t
)
526 ;; If there is a C-l at the beginning of the narrowed region, this
527 ;; isn't really a "save", but rather a "scan".
528 (goto-char (point-min))
529 (unless (looking-at "\^L")
531 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
532 (goto-char (point-max))
534 (when (search-forward "\n\n" nil t
)
537 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
538 (caar group-art
) (cdar group-art
)
539 (current-time-string)))
540 (setq group-art
(cdr group-art
))))
543 (defun nnbabyl-active-number (group)
544 ;; Find the next article number in GROUP.
545 (let ((active (cadr (assoc group nnbabyl-group-alist
))))
547 (setcdr active
(1+ (cdr active
)))
548 ;; This group is new, so we create a new entry for it.
549 ;; This might be a bit naughty... creating groups on the drop of
550 ;; a hat, but I don't know...
551 (push (list group
(setq active
(cons 1 1)))
552 nnbabyl-group-alist
))
555 (defun nnbabyl-create-mbox ()
556 (unless (file-exists-p nnbabyl-mbox-file
)
557 ;; Create a new, empty RMAIL mbox file.
559 (set-buffer (setq nnbabyl-mbox-buffer
560 (create-file-buffer nnbabyl-mbox-file
)))
561 (setq buffer-file-name nnbabyl-mbox-file
)
562 (insert "BABYL OPTIONS:\n\n\^_")
564 (point-min) (point-max) nnbabyl-mbox-file t
'nomesg
))))
566 (defun nnbabyl-read-mbox ()
567 (nnmail-activate 'nnbabyl
)
568 (nnbabyl-create-mbox)
570 (unless (and nnbabyl-mbox-buffer
571 (buffer-name nnbabyl-mbox-buffer
)
573 (set-buffer nnbabyl-mbox-buffer
)
574 (= (buffer-size) (nnheader-file-size nnbabyl-mbox-file
))))
575 ;; This buffer has changed since we read it last. Possibly.
577 (let ((delim (concat "^" nnbabyl-mail-delimiter
))
578 (alist nnbabyl-group-alist
)
580 (set-buffer (setq nnbabyl-mbox-buffer
581 (nnheader-find-file-noselect
582 nnbabyl-mbox-file nil t
)))
583 ;; Save previous buffer mode.
584 (setq nnbabyl-previous-buffer-mode
585 (cons (cons (point-min) (point-max))
588 (buffer-disable-undo)
590 (setq buffer-read-only nil
)
593 ;; Go through the group alist and compare against
596 (goto-char (point-max))
597 (when (and (re-search-backward
598 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
604 (match-beginning 1) (match-end 1))))
606 (setcdr (cadar alist
) number
))
607 (setq alist
(cdr alist
)))
609 ;; We go through the mbox and make sure that each and
610 ;; every mail belongs to some group or other.
611 (goto-char (point-min))
612 (if (looking-at "\^L")
614 (re-search-forward delim nil t
)
615 (setq start
(match-end 0)))
616 (while (re-search-forward delim nil t
)
617 (setq end
(match-end 0))
618 (unless (search-backward "\nX-Gnus-Newsgroup: " start t
)
622 (narrow-to-region (goto-char start
) end
)
624 (nnmail-article-group 'nnbabyl-active-number
))
625 (setq end
(point-max)))))
626 (goto-char (setq start end
)))
627 (when (buffer-modified-p (current-buffer))
629 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
)))))
631 (defun nnbabyl-remove-incoming-delims ()
632 (goto-char (point-min))
633 (while (search-forward "\^_" nil t
)
634 (replace-match "?" t t
)))
636 (defun nnbabyl-check-mbox ()
637 "Go through the nnbabyl mbox and make sure that no article numbers are reused."
639 (let ((idents (make-vector 1000 0))
642 (when (or (not nnbabyl-mbox-buffer
)
643 (not (buffer-name nnbabyl-mbox-buffer
)))
645 (set-buffer nnbabyl-mbox-buffer
)
646 (goto-char (point-min))
647 (while (re-search-forward "^X-Gnus-Newsgroup: \\([^ ]+\\) " nil t
)
648 (if (intern-soft (setq id
(match-string 1)) idents
)
650 (delete-region (progn (beginning-of-line) (point))
651 (progn (forward-line 1) (point)))
652 (nnheader-message 7 "Moving %s..." id
)
654 (nnmail-article-group 'nnbabyl-active-number
)))
656 (when (buffer-modified-p (current-buffer))
658 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
)
659 (nnheader-message 5 ""))))
663 ;;; nnbabyl.el ends here