1 ;;; nndoc.el --- single file access for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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.
34 (eval-when-compile (require 'cl))
38 (defvoo nndoc-article-type 'guess
40 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
41 `rfc934', `rfc822-forward', `mime-digest', `standard-digest',
42 `slack-digest', `clari-briefs' or `guess'.")
44 (defvoo nndoc-post-type 'mail
45 "*Whether the nndoc group is `mail' or `post'.")
47 (defvoo nndoc-open-document-hook 'nnheader-ms-strip-cr
48 "Hook run after opening a document.
49 The default function removes all trailing carriage returns
52 (defvar nndoc-type-alist
54 (article-begin . "^\^A\^A\^A\^A\n")
55 (body-end . "^\^A\^A\^A\^A\n"))
57 (article-begin . "^Path:"))
59 (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
60 (body-end-function . nndoc-rnews-body-end))
62 (article-begin-function . nndoc-mbox-article-begin)
63 (body-end-function . nndoc-mbox-body-end))
65 (article-begin . "\^_\^L *\n")
67 (body-begin-function . nndoc-babyl-body-begin)
68 (head-begin-function . nndoc-babyl-head-begin))
70 (article-begin . "^-+ Start of forwarded message -+\n+")
71 (body-end . "^-+ End of forwarded message -+$")
72 (prepare-body-function . nndoc-unquote-dashes))
74 (article-begin . "^--.*\n+")
76 (prepare-body-function . nndoc-unquote-dashes))
78 (article-begin . "^ \\*")
79 (body-end . "^\t------*[ \t]^*\n^ \\*")
82 (generate-head-function . nndoc-generate-clari-briefs-head)
83 (article-transform-function . nndoc-transform-clari-briefs))
89 (subtype digest guess))
91 (first-article . ,(concat "^" (make-string 70 ?-) "\n *\n+"))
92 (article-begin . ,(concat "^\n" (make-string 30 ?-) "\n *\n+"))
93 (prepare-body-function . 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 *$")
98 (subtype digest guess))
100 (article-begin . "^------------------------------*[\n \t]+")
102 (body-end-function . nndoc-digest-body-end)
103 (body-begin . "^ ?$")
104 (file-end . "^End of")
105 (prepare-body-function . nndoc-unquote-dashes)
106 (subtype digest guess))
108 (article-begin . "^\\\\\\\\\n")
109 (head-begin . "^Paper.*:")
110 (head-end . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
112 (body-end . "-------------------------------------------------")
113 (file-end . "^Title: Recent Seminal")
114 (generate-head-function . nndoc-generate-lanl-gov-head)
115 (article-transform-function . nndoc-transform-lanl-gov-announce)
116 (subtype preprints guess))
118 (article-begin . "^\n")
119 (body-end-function . nndoc-rfc822-forward-body-end-function))
132 (defvoo nndoc-file-begin nil)
133 (defvoo nndoc-first-article nil)
134 (defvoo nndoc-article-end nil)
135 (defvoo nndoc-article-begin nil)
136 (defvoo nndoc-head-begin nil)
137 (defvoo nndoc-head-end nil)
138 (defvoo nndoc-file-end nil)
139 (defvoo nndoc-body-begin nil)
140 (defvoo nndoc-body-end-function nil)
141 (defvoo nndoc-body-begin-function nil)
142 (defvoo nndoc-head-begin-function nil)
143 (defvoo nndoc-body-end nil)
144 (defvoo nndoc-dissection-alist nil)
145 (defvoo nndoc-prepare-body-function nil)
146 (defvoo nndoc-generate-head-function nil)
147 (defvoo nndoc-article-transform-function nil)
148 (defvoo nndoc-article-begin-function nil)
150 (defvoo nndoc-status-string "")
151 (defvoo nndoc-group-alist nil)
152 (defvoo nndoc-current-buffer nil
153 "Current nndoc news buffer.")
154 (defvoo nndoc-address nil)
156 (defconst nndoc-version "nndoc 1.0"
161 ;;; Interface functions
163 (nnoo-define-basics nndoc)
165 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
166 (when (nndoc-possibly-change-buffer newsgroup server)
168 (set-buffer nntp-server-buffer)
171 (if (stringp (car articles))
174 (when (setq entry (cdr (assq (setq article (pop articles))
175 nndoc-dissection-alist)))
176 (insert (format "221 %d Article retrieved.\n" article))
177 (if nndoc-generate-head-function
178 (funcall nndoc-generate-head-function article)
179 (insert-buffer-substring
180 nndoc-current-buffer (car entry) (nth 1 entry)))
181 (goto-char (point-max))
182 (unless (= (char-after (1- (point))) ?\n)
184 (insert (format "Lines: %d\n" (nth 4 entry)))
187 (nnheader-fold-continuation-lines)
190 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
191 (nndoc-possibly-change-buffer newsgroup server)
193 (let ((buffer (or buffer nntp-server-buffer))
194 (entry (cdr (assq article nndoc-dissection-alist)))
199 (if (stringp article)
201 (insert-buffer-substring
202 nndoc-current-buffer (car entry) (nth 1 entry))
205 (insert-buffer-substring
206 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
208 (when nndoc-prepare-body-function
209 (funcall nndoc-prepare-body-function))
210 (when nndoc-article-transform-function
211 (funcall nndoc-article-transform-function article))
214 (deffoo nndoc-request-group (group &optional server dont-check)
218 ((not (nndoc-possibly-change-buffer group server))
219 (nnheader-report 'nndoc "No such file or buffer: %s"
222 (nnheader-report 'nndoc "Selected group %s" group)
224 ((zerop (setq number (length nndoc-dissection-alist)))
225 (nndoc-close-group group)
226 (nnheader-report 'nndoc "No articles in group %s" group))
228 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
230 (deffoo nndoc-request-type (group &optional article)
231 (cond ((not article) 'unknown)
232 (nndoc-post-type nndoc-post-type)
235 (deffoo nndoc-close-group (group &optional server)
236 (nndoc-possibly-change-buffer group server)
237 (and nndoc-current-buffer
238 (buffer-name nndoc-current-buffer)
239 (kill-buffer nndoc-current-buffer))
240 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
242 (setq nndoc-current-buffer nil)
243 (nnoo-close-server 'nndoc server)
244 (setq nndoc-dissection-alist nil)
247 (deffoo nndoc-request-list (&optional server)
250 (deffoo nndoc-request-newgroups (date &optional server)
253 (deffoo nndoc-request-list-newsgroups (&optional server)
257 ;;; Internal functions.
259 (defun nndoc-possibly-change-buffer (group source)
262 ;; The current buffer is this group's buffer.
263 ((and nndoc-current-buffer
264 (buffer-name nndoc-current-buffer)
265 (eq nndoc-current-buffer
266 (setq buf (cdr (assoc group nndoc-group-alist))))))
267 ;; We change buffers by taking an old from the group alist.
268 ;; `source' is either a string (a file name) or a buffer object.
270 (setq nndoc-current-buffer buf))
271 ;; It's a totally new group.
272 ((or (and (bufferp nndoc-address)
273 (buffer-name nndoc-address))
274 (and (stringp nndoc-address)
275 (file-exists-p nndoc-address)
276 (not (file-directory-p nndoc-address))))
277 (push (cons group (setq nndoc-current-buffer
279 (concat " *nndoc " group "*"))))
281 (setq nndoc-dissection-alist nil)
283 (set-buffer nndoc-current-buffer)
284 (buffer-disable-undo (current-buffer))
286 (if (stringp nndoc-address)
287 (nnheader-insert-file-contents nndoc-address)
288 (insert-buffer-substring nndoc-address))
289 (run-hooks 'nndoc-open-document-hook))))
290 ;; Initialize the nndoc structures according to this new document.
291 (when (and nndoc-current-buffer
292 (not nndoc-dissection-alist))
294 (set-buffer nndoc-current-buffer)
296 (nndoc-dissect-buffer)))
297 (unless nndoc-current-buffer
298 (nndoc-close-server))
299 ;; Return whether we managed to select a file.
300 nndoc-current-buffer))
303 ;;; Deciding what document type we have
306 (defun nndoc-set-delims ()
307 "Set the nndoc delimiter variables according to the type of the document."
308 (let ((vars '(nndoc-file-begin
310 nndoc-article-end nndoc-head-begin nndoc-head-end
311 nndoc-file-end nndoc-article-begin
312 nndoc-body-begin nndoc-body-end-function nndoc-body-end
313 nndoc-prepare-body-function nndoc-article-transform-function
314 nndoc-generate-head-function nndoc-body-begin-function
315 nndoc-head-begin-function)))
317 (set (pop vars) nil)))
319 ;; Guess away until we find the real file type.
320 (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
322 (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
323 ;; Set the nndoc variables.
325 (set (intern (format "nndoc-%s" (caar defs)))
328 (defun nndoc-guess-type (subtype)
329 (let ((alist nndoc-type-alist)
330 results result entry)
331 (while (and (not result)
332 (setq entry (pop alist)))
333 (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
334 (goto-char (point-min))
335 (when (numberp (setq result (funcall (intern
336 (format "nndoc-%s-type-p"
338 (push (cons result entry) results)
340 (unless (or result results)
341 (error "Document is not of any recognized type"))
344 (cadar (sort results 'car-less-than-car)))))
347 ;;; Built-in type predicates and functions
350 (defun nndoc-mbox-type-p ()
351 (when (looking-at message-unix-mail-delimiter)
354 (defun nndoc-mbox-article-begin ()
355 (when (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
356 (goto-char (match-beginning 0))))
358 (defun nndoc-mbox-body-end ()
363 (and (re-search-backward
364 (concat "^" message-unix-mail-delimiter) nil t)
366 (search-forward "\n\n" beg t)
368 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
369 (setq len (string-to-int (match-string 1)))
370 (search-forward "\n\n" beg t)
371 (unless (= (setq len (+ (point) len)) (point-max))
372 (and (< len (point-max))
374 (looking-at message-unix-mail-delimiter)))))
377 (defun nndoc-mmdf-type-p ()
378 (when (looking-at "\^A\^A\^A\^A$")
381 (defun nndoc-news-type-p ()
382 (when (looking-at "^Path:.*\n")
385 (defun nndoc-rnews-type-p ()
386 (when (looking-at "#! *rnews")
389 (defun nndoc-rnews-body-end ()
390 (and (re-search-backward nndoc-article-begin nil t)
392 (goto-char (+ (point) (string-to-int (match-string 1))))))
394 (defun nndoc-babyl-type-p ()
395 (when (re-search-forward "\^_\^L *\n" nil t)
398 (defun nndoc-babyl-body-begin ()
399 (re-search-forward "^\n" nil t)
400 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
401 (let ((next (or (save-excursion
402 (re-search-forward nndoc-article-begin nil t))
404 (unless (re-search-forward "^\n" next t)
408 (forward-line -1)))))
410 (defun nndoc-babyl-head-begin ()
411 (when (re-search-forward "^[0-9].*\n" nil t)
412 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
416 (defun nndoc-forward-type-p ()
417 (when (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
418 (not (re-search-forward "^Subject:.*digest" nil t))
419 (not (re-search-backward "^From:" nil t 2))
420 (not (re-search-forward "^From:" nil t 2)))
423 (defun nndoc-rfc934-type-p ()
424 (when (and (re-search-forward "^-+ Start of forwarded.*\n+" nil t)
425 (not (re-search-forward "^Subject:.*digest" nil t))
426 (not (re-search-backward "^From:" nil t 2))
427 (not (re-search-forward "^From:" nil t 2)))
430 (defun nndoc-rfc822-forward-type-p ()
432 (message-narrow-to-head)
433 (when (re-search-forward "^Content-Type: *message/rfc822" nil t)
436 (defun nndoc-rfc822-forward-body-end-function ()
437 (goto-char (point-max)))
439 (defun nndoc-clari-briefs-type-p ()
440 (when (let ((case-fold-search nil))
441 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
444 (defun nndoc-transform-clari-briefs (article)
445 (goto-char (point-min))
446 (when (looking-at " *\\*\\(.*\\)\n")
447 (replace-match "" t t))
448 (nndoc-generate-clari-briefs-head article))
450 (defun nndoc-generate-clari-briefs-head (article)
451 (let ((entry (cdr (assq article nndoc-dissection-alist)))
454 (set-buffer nndoc-current-buffer)
456 (narrow-to-region (car entry) (nth 3 entry))
457 (goto-char (point-min))
458 (when (looking-at " *\\*\\(.*\\)$")
459 (setq subject (match-string 1))
460 (when (string-match "[ \t]+$" subject)
461 (setq subject (substring subject 0 (match-beginning 0)))))
463 (let ((case-fold-search nil))
465 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
466 (setq from (match-string 1)))))
467 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
468 "\nSubject: " (or subject "(no subject)") "\n")))
470 (defun nndoc-mime-digest-type-p ()
471 (let ((case-fold-search t)
472 boundary-id b-delimiter entry)
475 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
476 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
479 (setq boundary-id (match-string 1)
480 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
481 (setq entry (assq 'mime-digest nndoc-type-alist))
484 (cons 'head-end "^ ?$")
485 (cons 'body-begin "^ ?\n")
486 (cons 'article-begin b-delimiter)
487 (cons 'body-end-function 'nndoc-digest-body-end)
488 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
491 (defun nndoc-standard-digest-type-p ()
492 (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
494 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
497 (defun nndoc-digest-body-end ()
498 (and (re-search-forward nndoc-article-begin nil t)
499 (goto-char (match-beginning 0))))
501 (defun nndoc-slack-digest-type-p ()
504 (defun nndoc-lanl-gov-announce-type-p ()
505 (when (let ((case-fold-search nil))
506 (re-search-forward "^\\\\\\\\\nPaper: [a-z-]+/[0-9]+" nil t))
509 (defun nndoc-transform-lanl-gov-announce (article)
510 (goto-char (point-max))
511 (when (re-search-backward "^\\\\\\\\ +(\\([^ ]*\\) , *\\([^ ]*\\))" nil t)
512 (replace-match "\n\nGet it at \\1 (\\2)" t nil))
513 ;; (when (re-search-backward "^\\\\\\\\$" nil t)
514 ;; (replace-match "" t t))
517 (defun nndoc-generate-lanl-gov-head (article)
518 (let ((entry (cdr (assq article nndoc-dissection-alist)))
519 (e-mail "no address given")
522 (set-buffer nndoc-current-buffer)
524 (narrow-to-region (car entry) (nth 1 entry))
525 (goto-char (point-min))
526 (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
527 (setq subject (concat " (" (match-string 1) ")"))
528 (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
529 (setq e-mail (match-string 1)))
530 (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
532 (setq subject (concat (match-string 1) subject))
533 (setq from (concat (match-string 2) " <" e-mail ">"))))
535 (while (and from (string-match "(\[^)\]*)" from))
536 (setq from (replace-match "" t t from)))
537 (insert "From: " (or from "unknown")
538 "\nSubject: " (or subject "(no subject)") "\n")))
540 (deffoo nndoc-request-accept-article (group &optional server last)
546 ;;; Functions for dissecting the documents
549 (defun nndoc-search (regexp)
551 (re-search-forward regexp nil t)
552 (beginning-of-line)))
554 (defun nndoc-dissect-buffer ()
555 "Go through the document and partition it into heads/bodies/articles."
558 head-begin head-end body-begin body-end)
559 (setq nndoc-dissection-alist nil)
561 (set-buffer nndoc-current-buffer)
562 (goto-char (point-min))
563 ;; Find the beginning of the file.
564 (when nndoc-file-begin
565 (nndoc-search nndoc-file-begin))
566 ;; Go through the file.
567 (while (if (and first nndoc-first-article)
568 (nndoc-search nndoc-first-article)
569 (nndoc-article-begin))
571 (cond (nndoc-head-begin-function
572 (funcall nndoc-head-begin-function))
574 (nndoc-search nndoc-head-begin)))
575 (if (or (>= (point) (point-max))
577 (looking-at nndoc-file-end)))
578 (goto-char (point-max))
579 (setq head-begin (point))
580 (nndoc-search (or nndoc-head-end "^$"))
581 (setq head-end (point))
582 (if nndoc-body-begin-function
583 (funcall nndoc-body-begin-function)
584 (nndoc-search (or nndoc-body-begin "^\n")))
585 (setq body-begin (point))
586 (or (and nndoc-body-end-function
587 (funcall nndoc-body-end-function))
589 (nndoc-search nndoc-body-end))
590 (nndoc-article-begin)
592 (goto-char (point-max))
594 (and (re-search-backward nndoc-file-end nil t)
595 (beginning-of-line)))))
596 (setq body-end (point))
597 (push (list (incf i) head-begin head-end body-begin body-end
598 (count-lines body-begin body-end))
599 nndoc-dissection-alist))))))
601 (defun nndoc-article-begin ()
602 (if nndoc-article-begin-function
603 (funcall nndoc-article-begin-function)
605 (nndoc-search nndoc-article-begin))))
607 (defun nndoc-unquote-dashes ()
608 "Unquote quoted non-separators in digests."
609 (while (re-search-forward "^- -"nil t)
610 (replace-match "-" t t)))
613 (defun nndoc-add-type (definition &optional position)
614 "Add document DEFINITION to the list of nndoc document definitions.
615 If POSITION is nil or `last', the definition will be added
616 as the last checked definition, if t or `first', add as the
617 first definition, and if any other symbol, add after that
618 symbol in the alist."
619 ;; First remove any old instances.
620 (gnus-pull (car definition) nndoc-type-alist)
621 ;; Then enter the new definition in the proper place.
623 ((or (null position) (eq position 'last))
624 (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
625 ((or (eq position t) (eq position 'first))
626 (push definition nndoc-type-alist))
628 (let ((list (memq (assq position nndoc-type-alist)
631 (error "No such position: %s" position))
632 (setcdr list (cons definition (cdr list)))))))
636 ;;; nndoc.el ends here