1 ;;; nndoc.el --- single file access for Gnus
2 ;; Copyright (C) 1995,96,97 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))
56 (article-begin-function . nndoc-mbox-article-begin)
57 (body-end-function . nndoc-mbox-body-end))
59 (article-begin . "\^_\^L *\n")
61 (body-begin-function . nndoc-babyl-body-begin)
62 (head-begin-function . nndoc-babyl-head-begin))
64 (article-begin . "^-+ Start of forwarded message -+\n+")
65 (body-end . "^-+ End of forwarded message -+$")
66 (prepare-body-function . nndoc-unquote-dashes))
68 (article-begin . "^ \\*")
69 (body-end . "^\t------*[ \t]^*\n^ \\*")
72 (generate-head-function . nndoc-generate-clari-briefs-head)
73 (article-transform-function . nndoc-transform-clari-briefs))
79 (subtype digest guess))
81 (first-article . ,(concat "^" (make-string 70 ?-) "\n\n+"))
82 (article-begin . ,(concat "^\n" (make-string 30 ?-) "\n\n+"))
83 (prepare-body-function . nndoc-unquote-dashes)
84 (body-end-function . nndoc-digest-body-end)
86 (body-begin . "^ ?\n")
87 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$")
88 (subtype digest guess))
90 (article-begin . "^------------------------------*[\n \t]+")
92 (body-end-function . nndoc-digest-body-end)
94 (file-end . "^End of")
95 (prepare-body-function . nndoc-unquote-dashes)
96 (subtype digest guess))
98 (article-begin . "^\\\\\\\\\n")
99 (head-begin . "^Paper.*:")
100 (head-end . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
102 (body-end . "-------------------------------------------------")
103 (file-end . "^Title: Recent Seminal")
104 (generate-head-function . nndoc-generate-lanl-gov-head)
105 (article-transform-function . nndoc-transform-lanl-gov-announce)
106 (subtype preprints guess))
120 (defvoo nndoc-file-begin nil)
121 (defvoo nndoc-first-article nil)
122 (defvoo nndoc-article-end nil)
123 (defvoo nndoc-article-begin nil)
124 (defvoo nndoc-head-begin nil)
125 (defvoo nndoc-head-end nil)
126 (defvoo nndoc-file-end nil)
127 (defvoo nndoc-body-begin nil)
128 (defvoo nndoc-body-end-function nil)
129 (defvoo nndoc-body-begin-function nil)
130 (defvoo nndoc-head-begin-function nil)
131 (defvoo nndoc-body-end nil)
132 (defvoo nndoc-dissection-alist nil)
133 (defvoo nndoc-prepare-body-function nil)
134 (defvoo nndoc-generate-head-function nil)
135 (defvoo nndoc-article-transform-function nil)
136 (defvoo nndoc-article-begin-function nil)
138 (defvoo nndoc-status-string "")
139 (defvoo nndoc-group-alist nil)
140 (defvoo nndoc-current-buffer nil
141 "Current nndoc news buffer.")
142 (defvoo nndoc-address nil)
144 (defconst nndoc-version "nndoc 1.0"
149 ;;; Interface functions
151 (nnoo-define-basics nndoc)
153 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
154 (when (nndoc-possibly-change-buffer newsgroup server)
156 (set-buffer nntp-server-buffer)
159 (if (stringp (car articles))
162 (when (setq entry (cdr (assq (setq article (pop articles))
163 nndoc-dissection-alist)))
164 (insert (format "221 %d Article retrieved.\n" article))
165 (if nndoc-generate-head-function
166 (funcall nndoc-generate-head-function article)
167 (insert-buffer-substring
168 nndoc-current-buffer (car entry) (nth 1 entry)))
169 (goto-char (point-max))
170 (unless (= (char-after (1- (point))) ?\n)
172 (insert (format "Lines: %d\n" (nth 4 entry)))
175 (nnheader-fold-continuation-lines)
178 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
179 (nndoc-possibly-change-buffer newsgroup server)
181 (let ((buffer (or buffer nntp-server-buffer))
182 (entry (cdr (assq article nndoc-dissection-alist)))
187 (if (stringp article)
189 (insert-buffer-substring
190 nndoc-current-buffer (car entry) (nth 1 entry))
193 (insert-buffer-substring
194 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
196 (when nndoc-prepare-body-function
197 (funcall nndoc-prepare-body-function))
198 (when nndoc-article-transform-function
199 (funcall nndoc-article-transform-function article))
202 (deffoo nndoc-request-group (group &optional server dont-check)
206 ((not (nndoc-possibly-change-buffer group server))
207 (nnheader-report 'nndoc "No such file or buffer: %s"
210 (nnheader-report 'nndoc "Selected group %s" group)
212 ((zerop (setq number (length nndoc-dissection-alist)))
213 (nndoc-close-group group)
214 (nnheader-report 'nndoc "No articles in group %s" group))
216 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
218 (deffoo nndoc-request-type (group &optional article)
219 (cond ((not article) 'unknown)
220 (nndoc-post-type nndoc-post-type)
223 (deffoo nndoc-close-group (group &optional server)
224 (nndoc-possibly-change-buffer group server)
225 (and nndoc-current-buffer
226 (buffer-name nndoc-current-buffer)
227 (kill-buffer nndoc-current-buffer))
228 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
230 (setq nndoc-current-buffer nil)
231 (nnoo-close-server 'nndoc server)
232 (setq nndoc-dissection-alist nil)
235 (deffoo nndoc-request-list (&optional server)
238 (deffoo nndoc-request-newgroups (date &optional server)
241 (deffoo nndoc-request-list-newsgroups (&optional server)
245 ;;; Internal functions.
247 (defun nndoc-possibly-change-buffer (group source)
250 ;; The current buffer is this group's buffer.
251 ((and nndoc-current-buffer
252 (buffer-name nndoc-current-buffer)
253 (eq nndoc-current-buffer
254 (setq buf (cdr (assoc group nndoc-group-alist))))))
255 ;; We change buffers by taking an old from the group alist.
256 ;; `source' is either a string (a file name) or a buffer object.
258 (setq nndoc-current-buffer buf))
259 ;; It's a totally new group.
260 ((or (and (bufferp nndoc-address)
261 (buffer-name nndoc-address))
262 (and (stringp nndoc-address)
263 (file-exists-p nndoc-address)
264 (not (file-directory-p nndoc-address))))
265 (push (cons group (setq nndoc-current-buffer
267 (concat " *nndoc " group "*"))))
269 (setq nndoc-dissection-alist nil)
271 (set-buffer nndoc-current-buffer)
272 (buffer-disable-undo (current-buffer))
274 (if (stringp nndoc-address)
275 (nnheader-insert-file-contents nndoc-address)
276 (insert-buffer-substring nndoc-address)))))
277 ;; Initialize the nndoc structures according to this new document.
278 (when (and nndoc-current-buffer
279 (not nndoc-dissection-alist))
281 (set-buffer nndoc-current-buffer)
283 (nndoc-dissect-buffer)))
284 (unless nndoc-current-buffer
285 (nndoc-close-server))
286 ;; Return whether we managed to select a file.
287 nndoc-current-buffer))
290 ;;; Deciding what document type we have
293 (defun nndoc-set-delims ()
294 "Set the nndoc delimiter variables according to the type of the document."
295 (let ((vars '(nndoc-file-begin
297 nndoc-article-end nndoc-head-begin nndoc-head-end
298 nndoc-file-end nndoc-article-begin
299 nndoc-body-begin nndoc-body-end-function nndoc-body-end
300 nndoc-prepare-body-function nndoc-article-transform-function
301 nndoc-generate-head-function nndoc-body-begin-function
302 nndoc-head-begin-function)))
304 (set (pop vars) nil)))
306 ;; Guess away until we find the real file type.
307 (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
309 (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
310 ;; Set the nndoc variables.
312 (set (intern (format "nndoc-%s" (caar defs)))
315 (defun nndoc-guess-type (subtype)
316 (let ((alist nndoc-type-alist)
317 results result entry)
318 (while (and (not result)
319 (setq entry (pop alist)))
320 (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
321 (goto-char (point-min))
322 (when (numberp (setq result (funcall (intern
323 (format "nndoc-%s-type-p"
325 (push (cons result entry) results)
327 (unless (or result results)
328 (error "Document is not of any recognized type"))
331 (cadar (sort results (lambda (r1 r2) (< (car r1) (car r2))))))))
334 ;;; Built-in type predicates and functions
337 (defun nndoc-mbox-type-p ()
338 (when (looking-at message-unix-mail-delimiter)
341 (defun nndoc-mbox-article-begin ()
342 (when (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
343 (goto-char (match-beginning 0))))
345 (defun nndoc-mbox-body-end ()
350 (and (re-search-backward
351 (concat "^" message-unix-mail-delimiter) nil t)
353 (search-forward "\n\n" beg t)
355 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
356 (setq len (string-to-int (match-string 1)))
357 (search-forward "\n\n" beg t)
358 (unless (= (setq len (+ (point) len)) (point-max))
359 (and (< len (point-max))
361 (looking-at message-unix-mail-delimiter)))))
364 (defun nndoc-mmdf-type-p ()
365 (when (looking-at "\^A\^A\^A\^A$")
368 (defun nndoc-news-type-p ()
369 (when (looking-at "^Path:.*\n")
372 (defun nndoc-rnews-type-p ()
373 (when (looking-at "#! *rnews")
376 (defun nndoc-rnews-body-end ()
377 (and (re-search-backward nndoc-article-begin nil t)
379 (goto-char (+ (point) (string-to-int (match-string 1))))))
381 (defun nndoc-babyl-type-p ()
382 (when (re-search-forward "\^_\^L *\n" nil t)
385 (defun nndoc-babyl-body-begin ()
386 (re-search-forward "^\n" nil t)
387 (when (looking-at "\*\*\* EOOH \*\*\*")
388 (let ((next (or (save-excursion
389 (re-search-forward nndoc-article-begin nil t))
391 (unless (re-search-forward "^\n" next t)
395 (forward-line -1)))))
397 (defun nndoc-babyl-head-begin ()
398 (when (re-search-forward "^[0-9].*\n" nil t)
399 (when (looking-at "\*\*\* EOOH \*\*\*")
403 (defun nndoc-forward-type-p ()
404 (when (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
405 (not (re-search-forward "^Subject:.*digest" nil t))
406 (not (re-search-backward "^From:" nil t 2))
407 (not (re-search-forward "^From:" nil t 2)))
410 (defun nndoc-clari-briefs-type-p ()
411 (when (let ((case-fold-search nil))
412 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
415 (defun nndoc-transform-clari-briefs (article)
416 (goto-char (point-min))
417 (when (looking-at " *\\*\\(.*\\)\n")
418 (replace-match "" t t))
419 (nndoc-generate-clari-briefs-head article))
421 (defun nndoc-generate-clari-briefs-head (article)
422 (let ((entry (cdr (assq article nndoc-dissection-alist)))
425 (set-buffer nndoc-current-buffer)
427 (narrow-to-region (car entry) (nth 3 entry))
428 (goto-char (point-min))
429 (when (looking-at " *\\*\\(.*\\)$")
430 (setq subject (match-string 1))
431 (when (string-match "[ \t]+$" subject)
432 (setq subject (substring subject 0 (match-beginning 0)))))
434 (let ((case-fold-search nil))
436 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
437 (setq from (match-string 1)))))
438 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
439 "\nSubject: " (or subject "(no subject)") "\n")))
441 (defun nndoc-mime-digest-type-p ()
442 (let ((case-fold-search t)
443 boundary-id b-delimiter entry)
446 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
447 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
450 (setq boundary-id (match-string 1)
451 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
452 (setq entry (assq 'mime-digest nndoc-type-alist))
455 (cons 'head-end "^ ?$")
456 (cons 'body-begin "^ ?\n")
457 (cons 'article-begin b-delimiter)
458 (cons 'body-end-function 'nndoc-digest-body-end)
459 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
462 (defun nndoc-standard-digest-type-p ()
463 (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
465 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
468 (defun nndoc-digest-body-end ()
469 (and (re-search-forward nndoc-article-begin nil t)
470 (goto-char (match-beginning 0))))
472 (defun nndoc-slack-digest-type-p ()
475 (defun nndoc-lanl-gov-announce-type-p ()
476 (when (let ((case-fold-search nil))
477 (re-search-forward "^\\\\\\\\\nPaper: [a-z-]+/[0-9]+" nil t))
480 (defun nndoc-transform-lanl-gov-announce (article)
481 (goto-char (point-max))
482 (when (re-search-backward "^\\\\\\\\ +(\\([^ ]*\\) , *\\([^ ]*\\))" nil t)
483 (replace-match "\n\nGet it at \\1 (\\2)" t nil))
484 ;; (when (re-search-backward "^\\\\\\\\$" nil t)
485 ;; (replace-match "" t t))
488 (defun nndoc-generate-lanl-gov-head (article)
489 (let ((entry (cdr (assq article nndoc-dissection-alist)))
490 (e-mail "no address given")
493 (set-buffer nndoc-current-buffer)
495 (narrow-to-region (car entry) (nth 1 entry))
496 (goto-char (point-min))
497 (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
498 (setq subject (concat " (" (match-string 1) ")"))
499 (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
500 (setq e-mail (match-string 1)))
501 (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
503 (setq subject (concat (match-string 1) subject))
504 (setq from (concat (match-string 2) " <" e-mail ">"))))
506 (while (and from (string-match "(\[^)\]*)" from))
507 (setq from (replace-match "" t t from)))
508 (insert "From: " (or from "unknown")
509 "\nSubject: " (or subject "(no subject)") "\n")))
514 ;;; Functions for dissecting the documents
517 (defun nndoc-search (regexp)
519 (re-search-forward regexp nil t)
520 (beginning-of-line)))
522 (defun nndoc-dissect-buffer ()
523 "Go through the document and partition it into heads/bodies/articles."
526 head-begin head-end body-begin body-end)
527 (setq nndoc-dissection-alist nil)
529 (set-buffer nndoc-current-buffer)
530 (goto-char (point-min))
531 ;; Find the beginning of the file.
532 (when nndoc-file-begin
533 (nndoc-search nndoc-file-begin))
534 ;; Go through the file.
535 (while (if (and first nndoc-first-article)
536 (nndoc-search nndoc-first-article)
537 (nndoc-article-begin))
539 (cond (nndoc-head-begin-function
540 (funcall nndoc-head-begin-function))
542 (nndoc-search nndoc-head-begin)))
543 (if (or (>= (point) (point-max))
545 (looking-at nndoc-file-end)))
546 (goto-char (point-max))
547 (setq head-begin (point))
548 (nndoc-search (or nndoc-head-end "^$"))
549 (setq head-end (point))
550 (if nndoc-body-begin-function
551 (funcall nndoc-body-begin-function)
552 (nndoc-search (or nndoc-body-begin "^\n")))
553 (setq body-begin (point))
554 (or (and nndoc-body-end-function
555 (funcall nndoc-body-end-function))
557 (nndoc-search nndoc-body-end))
558 (nndoc-article-begin)
560 (goto-char (point-max))
562 (and (re-search-backward nndoc-file-end nil t)
563 (beginning-of-line)))))
564 (setq body-end (point))
565 (push (list (incf i) head-begin head-end body-begin body-end
566 (count-lines body-begin body-end))
567 nndoc-dissection-alist))))))
569 (defun nndoc-article-begin ()
570 (if nndoc-article-begin-function
571 (funcall nndoc-article-begin-function)
573 (nndoc-search nndoc-article-begin))))
575 (defun nndoc-unquote-dashes ()
576 "Unquote quoted non-separators in digests."
577 (while (re-search-forward "^- -"nil t)
578 (replace-match "-" t t)))
581 (defun nndoc-add-type (definition &optional position)
582 "Add document DEFINITION to the list of nndoc document definitions.
583 If POSITION is nil or `last', the definition will be added
584 as the last checked definition, if t or `first', add as the
585 first definition, and if any other symbol, add after that
586 symbol in the alist."
587 ;; First remove any old instances.
588 (setq nndoc-type-alist
589 (delq (assq (car definition) nndoc-type-alist)
591 ;; Then enter the new definition in the proper place.
593 ((or (null position) (eq position 'last))
594 (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
595 ((or (eq position t) (eq position 'first))
596 (push definition nndoc-type-alist))
598 (let ((list (memq (assq position nndoc-type-alist)
601 (error "No such position: %s" position))
602 (setcdr list (cons definition (cdr list)))))))
606 ;;; nndoc.el ends here