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 `rfc934', `mime-digest', `standard-digest', `slack-digest',
41 `clari-briefs' or `guess'.")
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 . "^--.*\n+")
70 (prepare-body-function . nndoc-unquote-dashes))
72 (article-begin . "^ \\*")
73 (body-end . "^\t------*[ \t]^*\n^ \\*")
76 (generate-head-function . nndoc-generate-clari-briefs-head)
77 (article-transform-function . nndoc-transform-clari-briefs))
83 (subtype digest guess))
85 (first-article . ,(concat "^" (make-string 70 ?-) "\n\n+"))
86 (article-begin . ,(concat "^\n" (make-string 30 ?-) "\n\n+"))
87 (prepare-body-function . nndoc-unquote-dashes)
88 (body-end-function . nndoc-digest-body-end)
90 (body-begin . "^ ?\n")
91 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$")
92 (subtype digest guess))
94 (article-begin . "^------------------------------*[\n \t]+")
96 (body-end-function . nndoc-digest-body-end)
98 (file-end . "^End of")
99 (prepare-body-function . nndoc-unquote-dashes)
100 (subtype digest guess))
102 (article-begin . "^\\\\\\\\\n")
103 (head-begin . "^Paper.*:")
104 (head-end . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
106 (body-end . "-------------------------------------------------")
107 (file-end . "^Title: Recent Seminal")
108 (generate-head-function . nndoc-generate-lanl-gov-head)
109 (article-transform-function . nndoc-transform-lanl-gov-announce)
110 (subtype preprints guess))
124 (defvoo nndoc-file-begin nil)
125 (defvoo nndoc-first-article nil)
126 (defvoo nndoc-article-end nil)
127 (defvoo nndoc-article-begin nil)
128 (defvoo nndoc-head-begin nil)
129 (defvoo nndoc-head-end nil)
130 (defvoo nndoc-file-end nil)
131 (defvoo nndoc-body-begin nil)
132 (defvoo nndoc-body-end-function nil)
133 (defvoo nndoc-body-begin-function nil)
134 (defvoo nndoc-head-begin-function nil)
135 (defvoo nndoc-body-end nil)
136 (defvoo nndoc-dissection-alist nil)
137 (defvoo nndoc-prepare-body-function nil)
138 (defvoo nndoc-generate-head-function nil)
139 (defvoo nndoc-article-transform-function nil)
140 (defvoo nndoc-article-begin-function nil)
142 (defvoo nndoc-status-string "")
143 (defvoo nndoc-group-alist nil)
144 (defvoo nndoc-current-buffer nil
145 "Current nndoc news buffer.")
146 (defvoo nndoc-address nil)
148 (defconst nndoc-version "nndoc 1.0"
153 ;;; Interface functions
155 (nnoo-define-basics nndoc)
157 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
158 (when (nndoc-possibly-change-buffer newsgroup server)
160 (set-buffer nntp-server-buffer)
163 (if (stringp (car articles))
166 (when (setq entry (cdr (assq (setq article (pop articles))
167 nndoc-dissection-alist)))
168 (insert (format "221 %d Article retrieved.\n" article))
169 (if nndoc-generate-head-function
170 (funcall nndoc-generate-head-function article)
171 (insert-buffer-substring
172 nndoc-current-buffer (car entry) (nth 1 entry)))
173 (goto-char (point-max))
174 (unless (= (char-after (1- (point))) ?\n)
176 (insert (format "Lines: %d\n" (nth 4 entry)))
179 (nnheader-fold-continuation-lines)
182 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
183 (nndoc-possibly-change-buffer newsgroup server)
185 (let ((buffer (or buffer nntp-server-buffer))
186 (entry (cdr (assq article nndoc-dissection-alist)))
191 (if (stringp article)
193 (insert-buffer-substring
194 nndoc-current-buffer (car entry) (nth 1 entry))
197 (insert-buffer-substring
198 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
200 (when nndoc-prepare-body-function
201 (funcall nndoc-prepare-body-function))
202 (when nndoc-article-transform-function
203 (funcall nndoc-article-transform-function article))
206 (deffoo nndoc-request-group (group &optional server dont-check)
210 ((not (nndoc-possibly-change-buffer group server))
211 (nnheader-report 'nndoc "No such file or buffer: %s"
214 (nnheader-report 'nndoc "Selected group %s" group)
216 ((zerop (setq number (length nndoc-dissection-alist)))
217 (nndoc-close-group group)
218 (nnheader-report 'nndoc "No articles in group %s" group))
220 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
222 (deffoo nndoc-request-type (group &optional article)
223 (cond ((not article) 'unknown)
224 (nndoc-post-type nndoc-post-type)
227 (deffoo nndoc-close-group (group &optional server)
228 (nndoc-possibly-change-buffer group server)
229 (and nndoc-current-buffer
230 (buffer-name nndoc-current-buffer)
231 (kill-buffer nndoc-current-buffer))
232 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
234 (setq nndoc-current-buffer nil)
235 (nnoo-close-server 'nndoc server)
236 (setq nndoc-dissection-alist nil)
239 (deffoo nndoc-request-list (&optional server)
242 (deffoo nndoc-request-newgroups (date &optional server)
245 (deffoo nndoc-request-list-newsgroups (&optional server)
249 ;;; Internal functions.
251 (defun nndoc-possibly-change-buffer (group source)
254 ;; The current buffer is this group's buffer.
255 ((and nndoc-current-buffer
256 (buffer-name nndoc-current-buffer)
257 (eq nndoc-current-buffer
258 (setq buf (cdr (assoc group nndoc-group-alist))))))
259 ;; We change buffers by taking an old from the group alist.
260 ;; `source' is either a string (a file name) or a buffer object.
262 (setq nndoc-current-buffer buf))
263 ;; It's a totally new group.
264 ((or (and (bufferp nndoc-address)
265 (buffer-name nndoc-address))
266 (and (stringp nndoc-address)
267 (file-exists-p nndoc-address)
268 (not (file-directory-p nndoc-address))))
269 (push (cons group (setq nndoc-current-buffer
271 (concat " *nndoc " group "*"))))
273 (setq nndoc-dissection-alist nil)
275 (set-buffer nndoc-current-buffer)
276 (buffer-disable-undo (current-buffer))
278 (if (stringp nndoc-address)
279 (nnheader-insert-file-contents nndoc-address)
280 (insert-buffer-substring nndoc-address)))))
281 ;; Initialize the nndoc structures according to this new document.
282 (when (and nndoc-current-buffer
283 (not nndoc-dissection-alist))
285 (set-buffer nndoc-current-buffer)
287 (nndoc-dissect-buffer)))
288 (unless nndoc-current-buffer
289 (nndoc-close-server))
290 ;; Return whether we managed to select a file.
291 nndoc-current-buffer))
294 ;;; Deciding what document type we have
297 (defun nndoc-set-delims ()
298 "Set the nndoc delimiter variables according to the type of the document."
299 (let ((vars '(nndoc-file-begin
301 nndoc-article-end nndoc-head-begin nndoc-head-end
302 nndoc-file-end nndoc-article-begin
303 nndoc-body-begin nndoc-body-end-function nndoc-body-end
304 nndoc-prepare-body-function nndoc-article-transform-function
305 nndoc-generate-head-function nndoc-body-begin-function
306 nndoc-head-begin-function)))
308 (set (pop vars) nil)))
310 ;; Guess away until we find the real file type.
311 (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
313 (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
314 ;; Set the nndoc variables.
316 (set (intern (format "nndoc-%s" (caar defs)))
319 (defun nndoc-guess-type (subtype)
320 (let ((alist nndoc-type-alist)
321 results result entry)
322 (while (and (not result)
323 (setq entry (pop alist)))
324 (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
325 (goto-char (point-min))
326 (when (numberp (setq result (funcall (intern
327 (format "nndoc-%s-type-p"
329 (push (cons result entry) results)
331 (unless (or result results)
332 (error "Document is not of any recognized type"))
335 (cadar (sort results (lambda (r1 r2) (< (car r1) (car r2))))))))
338 ;;; Built-in type predicates and functions
341 (defun nndoc-mbox-type-p ()
342 (when (looking-at message-unix-mail-delimiter)
345 (defun nndoc-mbox-article-begin ()
346 (when (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
347 (goto-char (match-beginning 0))))
349 (defun nndoc-mbox-body-end ()
354 (and (re-search-backward
355 (concat "^" message-unix-mail-delimiter) nil t)
357 (search-forward "\n\n" beg t)
359 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
360 (setq len (string-to-int (match-string 1)))
361 (search-forward "\n\n" beg t)
362 (unless (= (setq len (+ (point) len)) (point-max))
363 (and (< len (point-max))
365 (looking-at message-unix-mail-delimiter)))))
368 (defun nndoc-mmdf-type-p ()
369 (when (looking-at "\^A\^A\^A\^A$")
372 (defun nndoc-news-type-p ()
373 (when (looking-at "^Path:.*\n")
376 (defun nndoc-rnews-type-p ()
377 (when (looking-at "#! *rnews")
380 (defun nndoc-rnews-body-end ()
381 (and (re-search-backward nndoc-article-begin nil t)
383 (goto-char (+ (point) (string-to-int (match-string 1))))))
385 (defun nndoc-babyl-type-p ()
386 (when (re-search-forward "\^_\^L *\n" nil t)
389 (defun nndoc-babyl-body-begin ()
390 (re-search-forward "^\n" nil t)
391 (when (looking-at "\*\*\* EOOH \*\*\*")
392 (let ((next (or (save-excursion
393 (re-search-forward nndoc-article-begin nil t))
395 (unless (re-search-forward "^\n" next t)
399 (forward-line -1)))))
401 (defun nndoc-babyl-head-begin ()
402 (when (re-search-forward "^[0-9].*\n" nil t)
403 (when (looking-at "\*\*\* EOOH \*\*\*")
407 (defun nndoc-forward-type-p ()
408 (when (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
409 (not (re-search-forward "^Subject:.*digest" nil t))
410 (not (re-search-backward "^From:" nil t 2))
411 (not (re-search-forward "^From:" nil t 2)))
414 (defun nndoc-rfc934-type-p ()
415 (when (and (re-search-forward "^-+ Start of forwarded.*\n+" nil t)
416 (not (re-search-forward "^Subject:.*digest" nil t))
417 (not (re-search-backward "^From:" nil t 2))
418 (not (re-search-forward "^From:" nil t 2)))
421 (defun nndoc-clari-briefs-type-p ()
422 (when (let ((case-fold-search nil))
423 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
426 (defun nndoc-transform-clari-briefs (article)
427 (goto-char (point-min))
428 (when (looking-at " *\\*\\(.*\\)\n")
429 (replace-match "" t t))
430 (nndoc-generate-clari-briefs-head article))
432 (defun nndoc-generate-clari-briefs-head (article)
433 (let ((entry (cdr (assq article nndoc-dissection-alist)))
436 (set-buffer nndoc-current-buffer)
438 (narrow-to-region (car entry) (nth 3 entry))
439 (goto-char (point-min))
440 (when (looking-at " *\\*\\(.*\\)$")
441 (setq subject (match-string 1))
442 (when (string-match "[ \t]+$" subject)
443 (setq subject (substring subject 0 (match-beginning 0)))))
445 (let ((case-fold-search nil))
447 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
448 (setq from (match-string 1)))))
449 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
450 "\nSubject: " (or subject "(no subject)") "\n")))
452 (defun nndoc-mime-digest-type-p ()
453 (let ((case-fold-search t)
454 boundary-id b-delimiter entry)
457 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
458 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
461 (setq boundary-id (match-string 1)
462 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
463 (setq entry (assq 'mime-digest nndoc-type-alist))
466 (cons 'head-end "^ ?$")
467 (cons 'body-begin "^ ?\n")
468 (cons 'article-begin b-delimiter)
469 (cons 'body-end-function 'nndoc-digest-body-end)
470 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
473 (defun nndoc-standard-digest-type-p ()
474 (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
476 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
479 (defun nndoc-digest-body-end ()
480 (and (re-search-forward nndoc-article-begin nil t)
481 (goto-char (match-beginning 0))))
483 (defun nndoc-slack-digest-type-p ()
486 (defun nndoc-lanl-gov-announce-type-p ()
487 (when (let ((case-fold-search nil))
488 (re-search-forward "^\\\\\\\\\nPaper: [a-z-]+/[0-9]+" nil t))
491 (defun nndoc-transform-lanl-gov-announce (article)
492 (goto-char (point-max))
493 (when (re-search-backward "^\\\\\\\\ +(\\([^ ]*\\) , *\\([^ ]*\\))" nil t)
494 (replace-match "\n\nGet it at \\1 (\\2)" t nil))
495 ;; (when (re-search-backward "^\\\\\\\\$" nil t)
496 ;; (replace-match "" t t))
499 (defun nndoc-generate-lanl-gov-head (article)
500 (let ((entry (cdr (assq article nndoc-dissection-alist)))
501 (e-mail "no address given")
504 (set-buffer nndoc-current-buffer)
506 (narrow-to-region (car entry) (nth 1 entry))
507 (goto-char (point-min))
508 (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
509 (setq subject (concat " (" (match-string 1) ")"))
510 (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
511 (setq e-mail (match-string 1)))
512 (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
514 (setq subject (concat (match-string 1) subject))
515 (setq from (concat (match-string 2) " <" e-mail ">"))))
517 (while (and from (string-match "(\[^)\]*)" from))
518 (setq from (replace-match "" t t from)))
519 (insert "From: " (or from "unknown")
520 "\nSubject: " (or subject "(no subject)") "\n")))
525 ;;; Functions for dissecting the documents
528 (defun nndoc-search (regexp)
530 (re-search-forward regexp nil t)
531 (beginning-of-line)))
533 (defun nndoc-dissect-buffer ()
534 "Go through the document and partition it into heads/bodies/articles."
537 head-begin head-end body-begin body-end)
538 (setq nndoc-dissection-alist nil)
540 (set-buffer nndoc-current-buffer)
541 (goto-char (point-min))
542 ;; Find the beginning of the file.
543 (when nndoc-file-begin
544 (nndoc-search nndoc-file-begin))
545 ;; Go through the file.
546 (while (if (and first nndoc-first-article)
547 (nndoc-search nndoc-first-article)
548 (nndoc-article-begin))
550 (cond (nndoc-head-begin-function
551 (funcall nndoc-head-begin-function))
553 (nndoc-search nndoc-head-begin)))
554 (if (or (>= (point) (point-max))
556 (looking-at nndoc-file-end)))
557 (goto-char (point-max))
558 (setq head-begin (point))
559 (nndoc-search (or nndoc-head-end "^$"))
560 (setq head-end (point))
561 (if nndoc-body-begin-function
562 (funcall nndoc-body-begin-function)
563 (nndoc-search (or nndoc-body-begin "^\n")))
564 (setq body-begin (point))
565 (or (and nndoc-body-end-function
566 (funcall nndoc-body-end-function))
568 (nndoc-search nndoc-body-end))
569 (nndoc-article-begin)
571 (goto-char (point-max))
573 (and (re-search-backward nndoc-file-end nil t)
574 (beginning-of-line)))))
575 (setq body-end (point))
576 (push (list (incf i) head-begin head-end body-begin body-end
577 (count-lines body-begin body-end))
578 nndoc-dissection-alist))))))
580 (defun nndoc-article-begin ()
581 (if nndoc-article-begin-function
582 (funcall nndoc-article-begin-function)
584 (nndoc-search nndoc-article-begin))))
586 (defun nndoc-unquote-dashes ()
587 "Unquote quoted non-separators in digests."
588 (while (re-search-forward "^- -"nil t)
589 (replace-match "-" t t)))
592 (defun nndoc-add-type (definition &optional position)
593 "Add document DEFINITION to the list of nndoc document definitions.
594 If POSITION is nil or `last', the definition will be added
595 as the last checked definition, if t or `first', add as the
596 first definition, and if any other symbol, add after that
597 symbol in the alist."
598 ;; First remove any old instances.
599 (setq nndoc-type-alist
600 (delq (assq (car definition) nndoc-type-alist)
602 ;; Then enter the new definition in the proper place.
604 ((or (null position) (eq position 'last))
605 (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
606 ((or (eq position t) (eq position 'first))
607 (push definition nndoc-type-alist))
609 (let ((list (memq (assq position nndoc-type-alist)
612 (error "No such position: %s" position))
613 (setcdr list (cons definition (cdr list)))))))
617 ;;; nndoc.el ends here