1 ;;; nndoc.el --- single file 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>
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.
33 (eval-when-compile (require 'cl
))
37 (defvoo nndoc-article-type
'guess
39 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
40 `mime-digest', `standard-digest', `slack-digest', `clari-briefs' or
43 (defvoo nndoc-post-type
'mail
44 "*Whether the nndoc group is `mail' or `post'.")
46 (defvar nndoc-type-alist
48 (article-begin .
"^\^A\^A\^A\^A\n")
49 (body-end .
"^\^A\^A\^A\^A\n"))
51 (article-begin .
"^Path:"))
53 (article-begin .
"^#! *rnews +\\([0-9]+\\) *\n")
54 (body-end-function . nndoc-rnews-body-end
))
57 ,(let ((delim (concat "^" message-unix-mail-delimiter
)))
58 (if (string-match "\n\\'" delim
)
59 (substring delim
0 (match-beginning 0))
61 (body-end-function . nndoc-mbox-body-end
))
63 (article-begin .
"\^_\^L *\n")
65 (body-begin-function . nndoc-babyl-body-begin
)
66 (head-begin-function . nndoc-babyl-head-begin
))
68 (article-begin .
"^-+ Start of forwarded message -+\n+")
69 (body-end .
"^-+ End of forwarded message -+$")
70 (prepare-body . nndoc-unquote-dashes
))
72 (article-begin .
"^ \\*")
73 (body-end .
"^\t------*[ \t]^*\n^ \\*")
76 (generate-head . nndoc-generate-clari-briefs-head
)
77 (article-transform . nndoc-transform-clari-briefs
))
79 (article-begin .
"^------------------------------*[\n \t]+")
81 (body-end-function . nndoc-digest-body-end
)
83 (file-end .
"^End of")
84 (prepare-body . nndoc-unquote-dashes
))
91 (first-article .
,(concat "^" (make-string 70 ?-
) "\n\n+"))
92 (article-begin .
,(concat "\n\n" (make-string 30 ?-
) "\n\n+"))
93 (prepare-body . nndoc-unquote-dashes
)
94 (body-end-function . nndoc-digest-body-end
)
96 (body-begin .
"^ ?\n")
97 (file-end .
"^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$"))
99 (guess . nndoc-guess-type
))
101 (guess . nndoc-guess-digest-type
))
106 (defvoo nndoc-file-begin nil
)
107 (defvoo nndoc-first-article nil
)
108 (defvoo nndoc-article-end nil
)
109 (defvoo nndoc-article-begin nil
)
110 (defvoo nndoc-head-begin nil
)
111 (defvoo nndoc-head-end nil
)
112 (defvoo nndoc-file-end nil
)
113 (defvoo nndoc-body-begin nil
)
114 (defvoo nndoc-body-end-function nil
)
115 (defvoo nndoc-body-begin-function nil
)
116 (defvoo nndoc-head-begin-function nil
)
117 (defvoo nndoc-body-end nil
)
118 (defvoo nndoc-dissection-alist nil
)
119 (defvoo nndoc-prepare-body nil
)
120 (defvoo nndoc-generate-head nil
)
121 (defvoo nndoc-article-transform nil
)
123 (defvoo nndoc-status-string
"")
124 (defvoo nndoc-group-alist nil
)
125 (defvoo nndoc-current-buffer nil
126 "Current nndoc news buffer.")
127 (defvoo nndoc-address nil
)
129 (defconst nndoc-version
"nndoc 1.0"
134 ;;; Interface functions
136 (nnoo-define-basics nndoc
)
138 (deffoo nndoc-retrieve-headers
(articles &optional newsgroup server fetch-old
)
139 (when (nndoc-possibly-change-buffer newsgroup server
)
141 (set-buffer nntp-server-buffer
)
144 (if (stringp (car articles
))
147 (when (setq entry
(cdr (assq (setq article
(pop articles
))
148 nndoc-dissection-alist
)))
149 (insert (format "221 %d Article retrieved.\n" article
))
150 (if nndoc-generate-head
151 (funcall nndoc-generate-head article
)
152 (insert-buffer-substring
153 nndoc-current-buffer
(car entry
) (nth 1 entry
)))
154 (goto-char (point-max))
155 (or (= (char-after (1- (point))) ?
\n) (insert "\n"))
156 (insert (format "Lines: %d\n" (nth 4 entry
)))
159 (nnheader-fold-continuation-lines)
162 (deffoo nndoc-request-article
(article &optional newsgroup server buffer
)
163 (nndoc-possibly-change-buffer newsgroup server
)
165 (let ((buffer (or buffer nntp-server-buffer
))
166 (entry (cdr (assq article nndoc-dissection-alist
)))
170 (if (stringp article
)
172 (insert-buffer-substring
173 nndoc-current-buffer
(car entry
) (nth 1 entry
))
176 (insert-buffer-substring
177 nndoc-current-buffer
(nth 2 entry
) (nth 3 entry
))
179 (when nndoc-prepare-body
180 (funcall nndoc-prepare-body
))
181 (when nndoc-article-transform
182 (funcall nndoc-article-transform article
))
185 (deffoo nndoc-request-group
(group &optional server dont-check
)
189 ((not (nndoc-possibly-change-buffer group server
))
190 (nnheader-report 'nndoc
"No such file or buffer: %s"
193 (nnheader-report 'nndoc
"Selected group %s" group
)
195 ((zerop (setq number
(length nndoc-dissection-alist
)))
196 (nndoc-close-group group
)
197 (nnheader-report 'nndoc
"No articles in group %s" group
))
199 (nnheader-insert "211 %d %d %d %s\n" number
1 number group
)))))
201 (deffoo nndoc-request-type
(group &optional article
)
202 (cond ((not article
) 'unknown
)
203 (nndoc-post-type nndoc-post-type
)
206 (deffoo nndoc-close-group
(group &optional server
)
207 (nndoc-possibly-change-buffer group server
)
208 (and nndoc-current-buffer
209 (buffer-name nndoc-current-buffer
)
210 (kill-buffer nndoc-current-buffer
))
211 (setq nndoc-group-alist
(delq (assoc group nndoc-group-alist
)
213 (setq nndoc-current-buffer nil
)
214 (nnoo-close-server 'nndoc server
)
215 (setq nndoc-dissection-alist nil
)
218 (deffoo nndoc-request-list
(&optional server
)
221 (deffoo nndoc-request-newgroups
(date &optional server
)
224 (deffoo nndoc-request-list-newsgroups
(&optional server
)
228 ;;; Internal functions.
230 (defun nndoc-possibly-change-buffer (group source
)
233 ;; The current buffer is this group's buffer.
234 ((and nndoc-current-buffer
235 (buffer-name nndoc-current-buffer
)
236 (eq nndoc-current-buffer
237 (setq buf
(cdr (assoc group nndoc-group-alist
))))))
238 ;; We change buffers by taking an old from the group alist.
239 ;; `source' is either a string (a file name) or a buffer object.
241 (setq nndoc-current-buffer buf
))
242 ;; It's a totally new group.
243 ((or (and (bufferp nndoc-address
)
244 (buffer-name nndoc-address
))
245 (and (stringp nndoc-address
)
246 (file-exists-p nndoc-address
)
247 (not (file-directory-p nndoc-address
))))
248 (push (cons group
(setq nndoc-current-buffer
250 (concat " *nndoc " group
"*"))))
252 (setq nndoc-dissection-alist nil
)
254 (set-buffer nndoc-current-buffer
)
255 (buffer-disable-undo (current-buffer))
257 (if (stringp nndoc-address
)
258 (insert-file-contents nndoc-address
)
259 (insert-buffer-substring nndoc-address
)))))
260 ;; Initialize the nndoc structures according to this new document.
261 (when (and nndoc-current-buffer
262 (not nndoc-dissection-alist
))
264 (set-buffer nndoc-current-buffer
)
266 (nndoc-dissect-buffer)))
267 (unless nndoc-current-buffer
268 (nndoc-close-server))
269 ;; Return whether we managed to select a file.
270 nndoc-current-buffer
))
272 ;; MIME (RFC 1341) digest hack by Ulrik Dickow <dickow@nbi.dk>.
273 (defun nndoc-guess-digest-type ()
274 "Guess what digest type the current document is."
275 (let ((case-fold-search t
) ; We match a bit too much, keep it simple.
276 boundary-id b-delimiter entry
)
277 (goto-char (point-min))
282 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
283 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
286 (setq boundary-id
(match-string 1)
287 b-delimiter
(concat "\n--" boundary-id
"[\n \t]+"))
288 (setq entry
(assq 'mime-digest nndoc-type-alist
))
291 (cons 'head-end
"^ ?$")
292 (cons 'body-begin
"^ ?\n")
293 (cons 'article-begin b-delimiter
)
294 (cons 'body-end-function
'nndoc-digest-body-end
)
296 ; (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
297 (cons 'file-end
(concat "\n--" boundary-id
"--[ \t]*$"))))
300 ((and (re-search-forward (concat "^" (make-string 70 ?-
) "\n\n") nil t
)
302 (concat "\n\n" (make-string 30 ?-
) "\n\n") nil t
))
308 (defun nndoc-guess-type ()
309 "Guess what document type is in the current buffer."
310 (goto-char (point-min))
312 ((looking-at message-unix-mail-delimiter
)
314 ((looking-at "\^A\^A\^A\^A$")
316 ((looking-at "^Path:.*\n")
318 ((looking-at "#! *rnews")
320 ((re-search-forward "\^_\^L *\n" nil t
)
323 (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t
)
324 (not (re-search-forward "^Subject:.*digest" nil t
))))
326 ((let ((case-fold-search nil
))
327 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t
))
332 (defun nndoc-set-delims ()
333 "Set the nndoc delimiter variables according to the type of the document."
334 (let ((vars '(nndoc-file-begin
336 nndoc-article-end nndoc-head-begin nndoc-head-end
337 nndoc-file-end nndoc-article-begin
338 nndoc-body-begin nndoc-body-end-function nndoc-body-end
339 nndoc-prepare-body nndoc-article-transform
340 nndoc-generate-head nndoc-body-begin-function
341 nndoc-head-begin-function
)))
343 (set (pop vars
) nil
)))
345 ;; Guess away until we find the real file type.
346 (while (setq defs
(cdr (assq nndoc-article-type nndoc-type-alist
))
347 guess
(assq 'guess defs
))
348 (setq nndoc-article-type
(funcall (cdr guess
))))
349 ;; Set the nndoc variables.
351 (set (intern (format "nndoc-%s" (caar defs
)))
354 (defun nndoc-search (regexp)
356 (re-search-forward regexp nil t
)
357 (beginning-of-line)))
359 (defun nndoc-dissect-buffer ()
360 "Go through the document and partition it into heads/bodies/articles."
363 head-begin head-end body-begin body-end
)
364 (setq nndoc-dissection-alist nil
)
366 (set-buffer nndoc-current-buffer
)
367 (goto-char (point-min))
368 ;; Find the beginning of the file.
369 (when nndoc-file-begin
370 (nndoc-search nndoc-file-begin
))
371 ;; Go through the file.
372 (while (if (and first nndoc-first-article
)
373 (nndoc-search nndoc-first-article
)
374 (nndoc-search nndoc-article-begin
))
376 (cond (nndoc-head-begin-function
377 (funcall nndoc-head-begin-function
))
379 (nndoc-search nndoc-head-begin
)))
380 (if (and nndoc-file-end
381 (looking-at nndoc-file-end
))
382 (goto-char (point-max))
383 (setq head-begin
(point))
384 (nndoc-search (or nndoc-head-end
"^$"))
385 (setq head-end
(point))
386 (if nndoc-body-begin-function
387 (funcall nndoc-body-begin-function
)
388 (nndoc-search (or nndoc-body-begin
"^\n")))
389 (setq body-begin
(point))
390 (or (and nndoc-body-end-function
391 (funcall nndoc-body-end-function
))
393 (nndoc-search nndoc-body-end
))
394 (nndoc-search nndoc-article-begin
)
396 (goto-char (point-max))
398 (and (re-search-backward nndoc-file-end nil t
)
399 (beginning-of-line)))))
400 (setq body-end
(point))
401 (push (list (incf i
) head-begin head-end body-begin body-end
402 (count-lines body-begin body-end
))
403 nndoc-dissection-alist
))))))
405 (defun nndoc-unquote-dashes ()
406 "Unquote quoted non-separators in digests."
407 (while (re-search-forward "^- -"nil t
)
408 (replace-match "-" t t
)))
410 (defun nndoc-digest-body-end ()
411 (and (re-search-forward nndoc-article-begin nil t
)
412 (goto-char (match-beginning 0))))
414 (defun nndoc-mbox-body-end ()
419 (and (re-search-backward nndoc-article-begin nil t
)
421 (search-forward "\n\n" beg t
)
423 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t
)
424 (setq len
(string-to-int (match-string 1)))
425 (search-forward "\n\n" beg t
)
426 (or (= (setq len
(+ (point) len
)) (point-max))
427 (and (< len
(point-max))
429 (looking-at nndoc-article-begin
)))))
432 (defun nndoc-rnews-body-end ()
433 (and (re-search-backward nndoc-article-begin nil t
)
435 (goto-char (+ (point) (string-to-int (match-string 1))))))
437 (defun nndoc-transform-clari-briefs (article)
438 (goto-char (point-min))
439 (when (looking-at " *\\*\\(.*\\)\n")
440 (replace-match "" t t
))
441 (nndoc-generate-clari-briefs-head article
))
443 (defun nndoc-generate-clari-briefs-head (article)
444 (let ((entry (cdr (assq article nndoc-dissection-alist
)))
447 (set-buffer nndoc-current-buffer
)
449 (narrow-to-region (car entry
) (nth 3 entry
))
450 (goto-char (point-min))
451 (when (looking-at " *\\*\\(.*\\)$")
452 (setq subject
(match-string 1))
453 (when (string-match "[ \t]+$" subject
)
454 (setq subject
(substring subject
0 (match-beginning 0)))))
456 (let ((case-fold-search nil
))
458 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t
))
459 (setq from
(match-string 1)))))
460 (insert "From: " "clari@clari.net (" (or from
"unknown") ")"
461 "\nSubject: " (or subject
"(no subject)") "\n")))
463 (defun nndoc-babyl-body-begin ()
464 (re-search-forward "^\n" nil t
)
465 (when (looking-at "\*\*\* EOOH \*\*\*")
466 (re-search-forward "^\n" nil t
)))
468 (defun nndoc-babyl-head-begin ()
469 (when (re-search-forward "^[0-9].*\n" nil t
)
470 (when (looking-at "\*\*\* EOOH \*\*\*")
476 ;;; nndoc.el ends here