0
|
1 ;;; nnbabyl.el --- rmail mbox access for Gnus
|
16
|
2 ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
|
0
|
3
|
|
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
|
|
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
|
|
6 ;; Keywords: news, mail
|
|
7
|
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
|
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)
|
|
13 ;; any later version.
|
|
14
|
|
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.
|
|
19
|
|
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.
|
|
24
|
|
25 ;;; Commentary:
|
|
26
|
|
27 ;; For an overview of what the interface functions do, please see the
|
|
28 ;; Gnus sources.
|
|
29
|
|
30 ;;; Code:
|
|
31
|
|
32 (require 'nnheader)
|
|
33 (require 'rmail)
|
|
34 (require 'nnmail)
|
|
35 (require 'nnoo)
|
|
36 (eval-when-compile (require 'cl))
|
|
37
|
|
38 (nnoo-declare nnbabyl)
|
|
39
|
|
40 (defvoo nnbabyl-mbox-file (expand-file-name "~/RMAIL")
|
|
41 "The name of the rmail box file in the users home directory.")
|
|
42
|
|
43 (defvoo nnbabyl-active-file (expand-file-name "~/.rmail-active")
|
|
44 "The name of the active file for the rmail box.")
|
|
45
|
|
46 (defvoo nnbabyl-get-new-mail t
|
|
47 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
|
|
48
|
|
49 (defvoo nnbabyl-prepare-save-mail-hook nil
|
|
50 "Hook run narrowed to an article before saving.")
|
|
51
|
|
52
|
|
53
|
|
54 (defvar nnbabyl-mail-delimiter "\^_")
|
|
55
|
|
56 (defconst nnbabyl-version "nnbabyl 1.0"
|
|
57 "nnbabyl version.")
|
|
58
|
|
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)
|
|
64
|
|
65 (defvoo nnbabyl-previous-buffer-mode nil)
|
|
66
|
|
67 (eval-and-compile
|
|
68 (autoload 'gnus-set-text-properties "gnus-ems"))
|
|
69
|
|
70
|
|
71
|
|
72 ;;; Interface functions
|
|
73
|
|
74 (nnoo-define-basics nnbabyl)
|
|
75
|
|
76 (deffoo nnbabyl-retrieve-headers (articles &optional group server fetch-old)
|
|
77 (save-excursion
|
|
78 (set-buffer nntp-server-buffer)
|
|
79 (erase-buffer)
|
|
80 (let ((number (length articles))
|
|
81 (count 0)
|
|
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)
|
16
|
88 (end-of-line)
|
0
|
89 (when (or (search-forward art-string nil t)
|
|
90 (search-backward art-string nil t))
|
16
|
91 (unless (re-search-backward delim nil t)
|
|
92 (goto-char (point-min)))
|
0
|
93 (while (and (not (looking-at ".+:"))
|
|
94 (zerop (forward-line 1))))
|
|
95 (setq start (point))
|
|
96 (search-forward "\n\n" nil t)
|
|
97 (setq stop (1- (point)))
|
|
98 (set-buffer nntp-server-buffer)
|
|
99 (insert "221 ")
|
|
100 (princ article (current-buffer))
|
|
101 (insert " Article retrieved.\n")
|
|
102 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
|
|
103 (goto-char (point-max))
|
|
104 (insert ".\n"))
|
|
105 (and (numberp nnmail-large-newsgroup)
|
|
106 (> number nnmail-large-newsgroup)
|
|
107 (zerop (% (incf count) 20))
|
|
108 (nnheader-message 5 "nnbabyl: Receiving headers... %d%%"
|
|
109 (/ (* count 100) number))))
|
|
110
|
|
111 (and (numberp nnmail-large-newsgroup)
|
|
112 (> number nnmail-large-newsgroup)
|
|
113 (nnheader-message 5 "nnbabyl: Receiving headers...done"))
|
|
114
|
|
115 (set-buffer nntp-server-buffer)
|
|
116 (nnheader-fold-continuation-lines)
|
|
117 'headers)))
|
|
118
|
|
119 (deffoo nnbabyl-open-server (server &optional defs)
|
|
120 (nnoo-change-server 'nnbabyl server defs)
|
16
|
121 (nnbabyl-create-mbox)
|
0
|
122 (cond
|
|
123 ((not (file-exists-p nnbabyl-mbox-file))
|
|
124 (nnbabyl-close-server)
|
|
125 (nnheader-report 'nnbabyl "No such file: %s" nnbabyl-mbox-file))
|
|
126 ((file-directory-p nnbabyl-mbox-file)
|
|
127 (nnbabyl-close-server)
|
|
128 (nnheader-report 'nnbabyl "Not a regular file: %s" nnbabyl-mbox-file))
|
|
129 (t
|
|
130 (nnheader-report 'nnbabyl "Opened server %s using mbox %s" server
|
|
131 nnbabyl-mbox-file)
|
|
132 t)))
|
|
133
|
|
134 (deffoo nnbabyl-close-server (&optional server)
|
|
135 ;; Restore buffer mode.
|
|
136 (when (and (nnbabyl-server-opened)
|
|
137 nnbabyl-previous-buffer-mode)
|
|
138 (save-excursion
|
|
139 (set-buffer nnbabyl-mbox-buffer)
|
|
140 (narrow-to-region
|
|
141 (caar nnbabyl-previous-buffer-mode)
|
|
142 (cdar nnbabyl-previous-buffer-mode))
|
|
143 (funcall (cdr nnbabyl-previous-buffer-mode))))
|
|
144 (nnoo-close-server 'nnbabyl server)
|
|
145 (setq nnbabyl-mbox-buffer nil)
|
|
146 t)
|
|
147
|
|
148 (deffoo nnbabyl-server-opened (&optional server)
|
|
149 (and (nnoo-current-server-p 'nnbabyl server)
|
|
150 nnbabyl-mbox-buffer
|
|
151 (buffer-name nnbabyl-mbox-buffer)
|
|
152 nntp-server-buffer
|
|
153 (buffer-name nntp-server-buffer)))
|
|
154
|
|
155 (deffoo nnbabyl-request-article (article &optional newsgroup server buffer)
|
|
156 (nnbabyl-possibly-change-newsgroup newsgroup server)
|
|
157 (save-excursion
|
|
158 (set-buffer nnbabyl-mbox-buffer)
|
|
159 (goto-char (point-min))
|
|
160 (when (search-forward (nnbabyl-article-string article) nil t)
|
|
161 (let (start stop summary-line)
|
16
|
162 (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
|
|
163 (goto-char (point-min))
|
|
164 (end-of-line))
|
0
|
165 (while (and (not (looking-at ".+:"))
|
|
166 (zerop (forward-line 1))))
|
|
167 (setq start (point))
|
16
|
168 (or (when (re-search-forward
|
|
169 (concat "^" nnbabyl-mail-delimiter) nil t)
|
|
170 (beginning-of-line)
|
|
171 t)
|
0
|
172 (goto-char (point-max)))
|
|
173 (setq stop (point))
|
|
174 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
|
|
175 (set-buffer nntp-server-buffer)
|
|
176 (erase-buffer)
|
|
177 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
|
|
178 (goto-char (point-min))
|
|
179 ;; If there is an EOOH header, then we have to remove some
|
|
180 ;; duplicated headers.
|
|
181 (setq summary-line (looking-at "Summary-line:"))
|
|
182 (when (search-forward "\n*** EOOH ***" nil t)
|
|
183 (if summary-line
|
|
184 ;; The headers to be deleted are located before the
|
|
185 ;; EOOH line...
|
|
186 (delete-region (point-min) (progn (forward-line 1)
|
|
187 (point)))
|
|
188 ;; ...or after.
|
|
189 (delete-region (progn (beginning-of-line) (point))
|
|
190 (or (search-forward "\n\n" nil t)
|
|
191 (point)))))
|
16
|
192 (if (numberp article)
|
0
|
193 (cons nnbabyl-current-group article)
|
|
194 (nnbabyl-article-group-number)))))))
|
|
195
|
|
196 (deffoo nnbabyl-request-group (group &optional server dont-check)
|
|
197 (let ((active (cadr (assoc group nnbabyl-group-alist))))
|
|
198 (save-excursion
|
|
199 (cond
|
|
200 ((or (null active)
|
|
201 (null (nnbabyl-possibly-change-newsgroup group server)))
|
|
202 (nnheader-report 'nnbabyl "No such group: %s" group))
|
|
203 (dont-check
|
|
204 (nnheader-report 'nnbabyl "Selected group %s" group)
|
|
205 (nnheader-insert ""))
|
|
206 (t
|
|
207 (nnheader-report 'nnbabyl "Selected group %s" group)
|
|
208 (nnheader-insert "211 %d %d %d %s\n"
|
|
209 (1+ (- (cdr active) (car active)))
|
|
210 (car active) (cdr active) group))))))
|
|
211
|
|
212 (deffoo nnbabyl-request-scan (&optional group server)
|
16
|
213 (nnbabyl-possibly-change-newsgroup group server)
|
0
|
214 (nnbabyl-read-mbox)
|
|
215 (nnmail-get-new-mail
|
|
216 'nnbabyl
|
|
217 (lambda ()
|
|
218 (save-excursion
|
|
219 (set-buffer nnbabyl-mbox-buffer)
|
|
220 (save-buffer)))
|
|
221 nnbabyl-mbox-file group
|
|
222 (lambda ()
|
|
223 (save-excursion
|
|
224 (let ((in-buf (current-buffer)))
|
|
225 (goto-char (point-min))
|
|
226 (while (search-forward "\n\^_\n" nil t)
|
|
227 (delete-char -1))
|
|
228 (set-buffer nnbabyl-mbox-buffer)
|
|
229 (goto-char (point-max))
|
|
230 (search-backward "\n\^_" nil t)
|
|
231 (goto-char (match-end 0))
|
|
232 (insert-buffer-substring in-buf)))
|
|
233 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))))
|
|
234
|
|
235 (deffoo nnbabyl-close-group (group &optional server)
|
|
236 t)
|
|
237
|
16
|
238 (deffoo nnbabyl-request-create-group (group &optional server args)
|
0
|
239 (nnmail-activate 'nnbabyl)
|
|
240 (unless (assoc group nnbabyl-group-alist)
|
16
|
241 (push (list group (cons 1 0))
|
|
242 nnbabyl-group-alist)
|
0
|
243 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
|
|
244 t)
|
|
245
|
|
246 (deffoo nnbabyl-request-list (&optional server)
|
|
247 (save-excursion
|
|
248 (nnmail-find-file nnbabyl-active-file)
|
16
|
249 (setq nnbabyl-group-alist (nnmail-get-active))
|
|
250 t))
|
0
|
251
|
|
252 (deffoo nnbabyl-request-newgroups (date &optional server)
|
|
253 (nnbabyl-request-list server))
|
|
254
|
|
255 (deffoo nnbabyl-request-list-newsgroups (&optional server)
|
|
256 (nnheader-report 'nnbabyl "nnbabyl: LIST NEWSGROUPS is not implemented."))
|
|
257
|
|
258 (deffoo nnbabyl-request-expire-articles
|
|
259 (articles newsgroup &optional server force)
|
|
260 (nnbabyl-possibly-change-newsgroup newsgroup server)
|
|
261 (let* ((is-old t)
|
|
262 rest)
|
|
263 (nnmail-activate 'nnbabyl)
|
|
264
|
|
265 (save-excursion
|
|
266 (set-buffer nnbabyl-mbox-buffer)
|
|
267 (gnus-set-text-properties (point-min) (point-max) nil)
|
|
268 (while (and articles is-old)
|
|
269 (goto-char (point-min))
|
16
|
270 (when (search-forward (nnbabyl-article-string (car articles)) nil t)
|
|
271 (if (setq is-old
|
|
272 (nnmail-expired-article-p
|
|
273 newsgroup
|
|
274 (buffer-substring
|
|
275 (point) (progn (end-of-line) (point))) force))
|
|
276 (progn
|
|
277 (nnheader-message 5 "Deleting article %d in %s..."
|
|
278 (car articles) newsgroup)
|
|
279 (nnbabyl-delete-mail))
|
|
280 (push (car articles) rest)))
|
0
|
281 (setq articles (cdr articles)))
|
|
282 (save-buffer)
|
|
283 ;; Find the lowest active article in this group.
|
|
284 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
|
|
285 (goto-char (point-min))
|
|
286 (while (and (not (search-forward
|
|
287 (nnbabyl-article-string (car active)) nil t))
|
|
288 (<= (car active) (cdr active)))
|
|
289 (setcar active (1+ (car active)))
|
|
290 (goto-char (point-min))))
|
|
291 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
|
|
292 (nconc rest articles))))
|
|
293
|
|
294 (deffoo nnbabyl-request-move-article
|
|
295 (article group server accept-form &optional last)
|
|
296 (let ((buf (get-buffer-create " *nnbabyl move*"))
|
|
297 result)
|
|
298 (and
|
|
299 (nnbabyl-request-article article group server)
|
|
300 (save-excursion
|
|
301 (set-buffer buf)
|
|
302 (insert-buffer-substring nntp-server-buffer)
|
|
303 (goto-char (point-min))
|
16
|
304 (while (re-search-forward
|
|
305 "^X-Gnus-Newsgroup:"
|
|
306 (save-excursion (search-forward "\n\n" nil t) (point)) t)
|
|
307 (delete-region (progn (beginning-of-line) (point))
|
|
308 (progn (forward-line 1) (point))))
|
0
|
309 (setq result (eval accept-form))
|
|
310 (kill-buffer (current-buffer))
|
|
311 result)
|
|
312 (save-excursion
|
16
|
313 (nnbabyl-possibly-change-newsgroup group server)
|
0
|
314 (set-buffer nnbabyl-mbox-buffer)
|
|
315 (goto-char (point-min))
|
|
316 (if (search-forward (nnbabyl-article-string article) nil t)
|
|
317 (nnbabyl-delete-mail))
|
|
318 (and last (save-buffer))))
|
|
319 result))
|
|
320
|
|
321 (deffoo nnbabyl-request-accept-article (group &optional server last)
|
|
322 (nnbabyl-possibly-change-newsgroup group server)
|
|
323 (nnmail-check-syntax)
|
|
324 (let ((buf (current-buffer))
|
|
325 result beg)
|
|
326 (and
|
|
327 (nnmail-activate 'nnbabyl)
|
|
328 (save-excursion
|
|
329 (goto-char (point-min))
|
|
330 (search-forward "\n\n" nil t)
|
|
331 (forward-line -1)
|
|
332 (save-excursion
|
|
333 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
|
|
334 (delete-region (point) (progn (forward-line 1) (point)))))
|
16
|
335 (setq result (car (nnbabyl-save-mail
|
|
336 (if (stringp group)
|
|
337 (list (cons group (nnbabyl-active-number group)))
|
|
338 (nnmail-article-group 'nnbabyl-active-number)))))
|
0
|
339 (set-buffer nnbabyl-mbox-buffer)
|
|
340 (goto-char (point-max))
|
|
341 (search-backward "\n\^_")
|
|
342 (goto-char (match-end 0))
|
|
343 (insert-buffer-substring buf)
|
|
344 (when last
|
|
345 (save-buffer)
|
|
346 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
|
|
347 result))))
|
|
348
|
|
349 (deffoo nnbabyl-request-replace-article (article group buffer)
|
|
350 (nnbabyl-possibly-change-newsgroup group)
|
|
351 (save-excursion
|
|
352 (set-buffer nnbabyl-mbox-buffer)
|
|
353 (goto-char (point-min))
|
|
354 (if (not (search-forward (nnbabyl-article-string article) nil t))
|
|
355 nil
|
|
356 (nnbabyl-delete-mail t t)
|
|
357 (insert-buffer-substring buffer)
|
|
358 (save-buffer)
|
|
359 t)))
|
|
360
|
|
361 (deffoo nnbabyl-request-delete-group (group &optional force server)
|
|
362 (nnbabyl-possibly-change-newsgroup group server)
|
|
363 ;; Delete all articles in GROUP.
|
|
364 (if (not force)
|
|
365 () ; Don't delete the articles.
|
|
366 (save-excursion
|
|
367 (set-buffer nnbabyl-mbox-buffer)
|
|
368 (goto-char (point-min))
|
|
369 ;; Delete all articles in this group.
|
|
370 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
|
|
371 found)
|
|
372 (while (search-forward ident nil t)
|
|
373 (setq found t)
|
|
374 (nnbabyl-delete-mail))
|
16
|
375 (when found
|
|
376 (save-buffer)))))
|
0
|
377 ;; Remove the group from all structures.
|
|
378 (setq nnbabyl-group-alist
|
|
379 (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
|
|
380 nnbabyl-current-group nil)
|
|
381 ;; Save the active file.
|
|
382 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
|
|
383 t)
|
|
384
|
|
385 (deffoo nnbabyl-request-rename-group (group new-name &optional server)
|
|
386 (nnbabyl-possibly-change-newsgroup group server)
|
|
387 (save-excursion
|
|
388 (set-buffer nnbabyl-mbox-buffer)
|
|
389 (goto-char (point-min))
|
|
390 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
|
|
391 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
|
|
392 found)
|
|
393 (while (search-forward ident nil t)
|
|
394 (replace-match new-ident t t)
|
|
395 (setq found t))
|
16
|
396 (when found
|
|
397 (save-buffer))))
|
0
|
398 (let ((entry (assoc group nnbabyl-group-alist)))
|
|
399 (and entry (setcar entry new-name))
|
|
400 (setq nnbabyl-current-group nil)
|
|
401 ;; Save the new group alist.
|
|
402 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
|
|
403 t))
|
|
404
|
|
405
|
|
406 ;;; Internal functions.
|
|
407
|
|
408 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
|
16
|
409 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
|
0
|
410 ;; delimiter line.
|
|
411 (defun nnbabyl-delete-mail (&optional force leave-delim)
|
|
412 ;; Delete the current X-Gnus-Newsgroup line.
|
16
|
413 (unless force
|
|
414 (delete-region
|
|
415 (progn (beginning-of-line) (point))
|
|
416 (progn (forward-line 1) (point))))
|
0
|
417 ;; Beginning of the article.
|
|
418 (save-excursion
|
|
419 (save-restriction
|
|
420 (widen)
|
|
421 (narrow-to-region
|
|
422 (save-excursion
|
16
|
423 (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
|
|
424 (goto-char (point-min))
|
|
425 (end-of-line))
|
0
|
426 (if leave-delim (progn (forward-line 1) (point))
|
|
427 (match-beginning 0)))
|
|
428 (progn
|
|
429 (forward-line 1)
|
16
|
430 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
|
0
|
431 nil t)
|
16
|
432 (match-beginning 0))
|
0
|
433 (point-max))))
|
|
434 (goto-char (point-min))
|
|
435 ;; Only delete the article if no other groups owns it as well.
|
16
|
436 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
|
|
437 (delete-region (point-min) (point-max))))))
|
0
|
438
|
|
439 (defun nnbabyl-possibly-change-newsgroup (newsgroup &optional server)
|
|
440 (when (and server
|
|
441 (not (nnbabyl-server-opened server)))
|
|
442 (nnbabyl-open-server server))
|
16
|
443 (when (or (not nnbabyl-mbox-buffer)
|
|
444 (not (buffer-name nnbabyl-mbox-buffer)))
|
|
445 (save-excursion (nnbabyl-read-mbox)))
|
|
446 (unless nnbabyl-group-alist
|
|
447 (nnmail-activate 'nnbabyl))
|
0
|
448 (if newsgroup
|
|
449 (if (assoc newsgroup nnbabyl-group-alist)
|
|
450 (setq nnbabyl-current-group newsgroup)
|
|
451 (nnheader-report 'nnbabyl "No such group in file"))
|
|
452 t))
|
|
453
|
|
454 (defun nnbabyl-article-string (article)
|
|
455 (if (numberp article)
|
|
456 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
|
|
457 (int-to-string article) " ")
|
|
458 (concat "\nMessage-ID: " article)))
|
|
459
|
|
460 (defun nnbabyl-article-group-number ()
|
|
461 (save-excursion
|
|
462 (goto-char (point-min))
|
16
|
463 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
|
|
464 nil t)
|
|
465 (cons (buffer-substring (match-beginning 1) (match-end 1))
|
|
466 (string-to-int
|
|
467 (buffer-substring (match-beginning 2) (match-end 2)))))))
|
0
|
468
|
|
469 (defun nnbabyl-insert-lines ()
|
|
470 "Insert how many lines and chars there are in the body of the mail."
|
|
471 (let (lines chars)
|
|
472 (save-excursion
|
|
473 (goto-char (point-min))
|
16
|
474 (when (search-forward "\n\n" nil t)
|
0
|
475 ;; There may be an EOOH line here...
|
|
476 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
|
|
477 (search-forward "\n\n" nil t))
|
|
478 (setq chars (- (point-max) (point))
|
|
479 lines (max (- (count-lines (point) (point-max)) 1) 0))
|
|
480 ;; Move back to the end of the headers.
|
|
481 (goto-char (point-min))
|
|
482 (search-forward "\n\n" nil t)
|
|
483 (forward-char -1)
|
|
484 (save-excursion
|
|
485 (when (re-search-backward "^Lines: " nil t)
|
|
486 (delete-region (point) (progn (forward-line 1) (point)))))
|
|
487 (insert (format "Lines: %d\n" lines))
|
|
488 chars))))
|
|
489
|
16
|
490 (defun nnbabyl-save-mail (group-art)
|
0
|
491 ;; Called narrowed to an article.
|
16
|
492 (nnbabyl-insert-lines)
|
|
493 (nnmail-insert-xref group-art)
|
|
494 (nnbabyl-insert-newsgroup-line group-art)
|
|
495 (run-hooks 'nnbabyl-prepare-save-mail-hook)
|
|
496 group-art)
|
0
|
497
|
|
498 (defun nnbabyl-insert-newsgroup-line (group-art)
|
|
499 (save-excursion
|
|
500 (goto-char (point-min))
|
|
501 (while (looking-at "From ")
|
|
502 (replace-match "Mail-from: From " t t)
|
|
503 (forward-line 1))
|
|
504 ;; If there is a C-l at the beginning of the narrowed region, this
|
|
505 ;; isn't really a "save", but rather a "scan".
|
|
506 (goto-char (point-min))
|
16
|
507 (unless (looking-at "\^L")
|
|
508 (save-excursion
|
|
509 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
|
|
510 (goto-char (point-max))
|
|
511 (insert "\^_\n")))
|
|
512 (when (search-forward "\n\n" nil t)
|
|
513 (forward-char -1)
|
|
514 (while group-art
|
|
515 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
|
|
516 (caar group-art) (cdar group-art)
|
|
517 (current-time-string)))
|
|
518 (setq group-art (cdr group-art))))
|
0
|
519 t))
|
|
520
|
|
521 (defun nnbabyl-active-number (group)
|
|
522 ;; Find the next article number in GROUP.
|
|
523 (let ((active (cadr (assoc group nnbabyl-group-alist))))
|
|
524 (if active
|
|
525 (setcdr active (1+ (cdr active)))
|
|
526 ;; This group is new, so we create a new entry for it.
|
|
527 ;; This might be a bit naughty... creating groups on the drop of
|
|
528 ;; a hat, but I don't know...
|
16
|
529 (push (list group (setq active (cons 1 1)))
|
|
530 nnbabyl-group-alist))
|
0
|
531 (cdr active)))
|
|
532
|
16
|
533 (defun nnbabyl-create-mbox ()
|
0
|
534 (unless (file-exists-p nnbabyl-mbox-file)
|
|
535 ;; Create a new, empty RMAIL mbox file.
|
|
536 (save-excursion
|
|
537 (set-buffer (setq nnbabyl-mbox-buffer
|
|
538 (create-file-buffer nnbabyl-mbox-file)))
|
|
539 (setq buffer-file-name nnbabyl-mbox-file)
|
|
540 (insert "BABYL OPTIONS:\n\n\^_")
|
16
|
541 (nnmail-write-region
|
|
542 (point-min) (point-max) nnbabyl-mbox-file t 'nomesg))))
|
0
|
543
|
16
|
544 (defun nnbabyl-read-mbox ()
|
|
545 (nnmail-activate 'nnbabyl)
|
|
546 (nnbabyl-create-mbox)
|
|
547
|
|
548 (unless (and nnbabyl-mbox-buffer
|
0
|
549 (buffer-name nnbabyl-mbox-buffer)
|
|
550 (save-excursion
|
|
551 (set-buffer nnbabyl-mbox-buffer)
|
2
|
552 (= (buffer-size) (nnheader-file-size nnbabyl-mbox-file))))
|
16
|
553 ;; This buffer has changed since we read it last. Possibly.
|
0
|
554 (save-excursion
|
|
555 (let ((delim (concat "^" nnbabyl-mail-delimiter))
|
|
556 (alist nnbabyl-group-alist)
|
|
557 start end number)
|
|
558 (set-buffer (setq nnbabyl-mbox-buffer
|
|
559 (nnheader-find-file-noselect
|
|
560 nnbabyl-mbox-file nil 'raw)))
|
|
561 ;; Save previous buffer mode.
|
|
562 (setq nnbabyl-previous-buffer-mode
|
|
563 (cons (cons (point-min) (point-max))
|
|
564 major-mode))
|
|
565
|
|
566 (buffer-disable-undo (current-buffer))
|
|
567 (widen)
|
|
568 (setq buffer-read-only nil)
|
|
569 (fundamental-mode)
|
|
570
|
|
571 ;; Go through the group alist and compare against
|
|
572 ;; the rmail file.
|
|
573 (while alist
|
|
574 (goto-char (point-max))
|
|
575 (when (and (re-search-backward
|
|
576 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
|
16
|
577 (caar alist))
|
|
578 nil t)
|
0
|
579 (> (setq number
|
|
580 (string-to-number
|
|
581 (buffer-substring
|
|
582 (match-beginning 1) (match-end 1))))
|
|
583 (cdadar alist)))
|
16
|
584 (setcdr (cadar alist) number))
|
0
|
585 (setq alist (cdr alist)))
|
|
586
|
|
587 ;; We go through the mbox and make sure that each and
|
|
588 ;; every mail belongs to some group or other.
|
|
589 (goto-char (point-min))
|
16
|
590 (if (looking-at "\^L")
|
|
591 (setq start (point))
|
|
592 (re-search-forward delim nil t)
|
|
593 (setq start (match-end 0)))
|
0
|
594 (while (re-search-forward delim nil t)
|
|
595 (setq end (match-end 0))
|
|
596 (unless (search-backward "\nX-Gnus-Newsgroup: " start t)
|
|
597 (goto-char end)
|
|
598 (save-excursion
|
|
599 (save-restriction
|
|
600 (narrow-to-region (goto-char start) end)
|
16
|
601 (nnbabyl-save-mail
|
|
602 (nnmail-article-group 'nnbabyl-active-number))
|
0
|
603 (setq end (point-max)))))
|
|
604 (goto-char (setq start end)))
|
|
605 (when (buffer-modified-p (current-buffer))
|
|
606 (save-buffer))
|
|
607 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
|
|
608
|
|
609 (defun nnbabyl-remove-incoming-delims ()
|
|
610 (goto-char (point-min))
|
|
611 (while (search-forward "\^_" nil t)
|
|
612 (replace-match "?" t t)))
|
|
613
|
|
614 (defun nnbabyl-check-mbox ()
|
|
615 "Go through the nnbabyl mbox and make sure that no article numbers are reused."
|
|
616 (interactive)
|
|
617 (let ((idents (make-vector 1000 0))
|
|
618 id)
|
|
619 (save-excursion
|
|
620 (when (or (not nnbabyl-mbox-buffer)
|
|
621 (not (buffer-name nnbabyl-mbox-buffer)))
|
|
622 (nnbabyl-read-mbox))
|
|
623 (set-buffer nnbabyl-mbox-buffer)
|
|
624 (goto-char (point-min))
|
|
625 (while (re-search-forward "^X-Gnus-Newsgroup: \\([^ ]+\\) " nil t)
|
|
626 (if (intern-soft (setq id (match-string 1)) idents)
|
|
627 (progn
|
|
628 (delete-region (progn (beginning-of-line) (point))
|
|
629 (progn (forward-line 1) (point)))
|
|
630 (nnheader-message 7 "Moving %s..." id)
|
16
|
631 (nnbabyl-save-mail
|
|
632 (nnmail-article-group 'nnbabyl-active-number)))
|
0
|
633 (intern id idents)))
|
|
634 (when (buffer-modified-p (current-buffer))
|
|
635 (save-buffer))
|
|
636 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
|
|
637 (message ""))))
|
|
638
|
|
639 (provide 'nnbabyl)
|
|
640
|
|
641 ;;; nnbabyl.el ends here
|