1 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
4 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
32 (eval-when-compile (require 'cl))
36 (defvoo nndoc-article-type 'guess
38 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
39 `mime-digest', `standard-digest', `slack-digest', `clari-briefs' or
42 (defvoo nndoc-post-type 'mail
43 "*Whether the nndoc group is `mail' or `post'.")
45 (defvar nndoc-type-alist
47 (article-begin . "^\^A\^A\^A\^A\n")
48 (body-end . "^\^A\^A\^A\^A\n"))
50 (article-begin . "^Path:"))
52 (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
53 (body-end-function . nndoc-rnews-body-end))
55 (article-begin-function . nndoc-mbox-article-begin)
56 (body-end-function . nndoc-mbox-body-end))
58 (article-begin . "\^_\^L *\n")
60 (body-begin-function . nndoc-babyl-body-begin)
61 (head-begin-function . nndoc-babyl-head-begin))
63 (article-begin . "^-+ Start of forwarded message -+\n+")
64 (body-end . "^-+ End of forwarded message -+$")
65 (prepare-body-function . nndoc-unquote-dashes))
67 (article-begin . "^ \\*")
68 (body-end . "^\t------*[ \t]^*\n^ \\*")
71 (generate-head-function . nndoc-generate-clari-briefs-head)
72 (article-transform-function . nndoc-transform-clari-briefs))
78 (subtype digest guess))
80 (first-article . ,(concat "^" (make-string 70 ?-) "\n\n+"))
81 (article-begin . ,(concat "^\n" (make-string 30 ?-) "\n\n+"))
82 (prepare-body-function . nndoc-unquote-dashes)
83 (body-end-function . nndoc-digest-body-end)
85 (body-begin . "^ ?\n")
86 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$")
87 (subtype digest guess))
89 (article-begin . "^------------------------------*[\n \t]+")
91 (body-end-function . nndoc-digest-body-end)
93 (file-end . "^End of")
94 (prepare-body-function . nndoc-unquote-dashes)
95 (subtype digest guess))
97 (article-begin . "^\\\\\\\\\n")
98 (head-begin . "^Paper.*:")
99 (head-end . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
101 (body-end . "-------------------------------------------------")
102 (file-end . "^Title: Recent Seminal")
103 (generate-head-function . nndoc-generate-lanl-gov-head)
104 (article-transform-function . nndoc-transform-lanl-gov-announce)
105 (subtype preprints guess))
119 (defvoo nndoc-file-begin nil)
120 (defvoo nndoc-first-article nil)
121 (defvoo nndoc-article-end nil)
122 (defvoo nndoc-article-begin nil)
123 (defvoo nndoc-head-begin nil)
124 (defvoo nndoc-head-end nil)
125 (defvoo nndoc-file-end nil)
126 (defvoo nndoc-body-begin nil)
127 (defvoo nndoc-body-end-function nil)
128 (defvoo nndoc-body-begin-function nil)
129 (defvoo nndoc-head-begin-function nil)
130 (defvoo nndoc-body-end nil)
131 (defvoo nndoc-dissection-alist nil)
132 (defvoo nndoc-prepare-body-function nil)
133 (defvoo nndoc-generate-head-function nil)
134 (defvoo nndoc-article-transform-function nil)
136 (defvoo nndoc-status-string "")
137 (defvoo nndoc-group-alist nil)
138 (defvoo nndoc-current-buffer nil
139 "Current nndoc news buffer.")
140 (defvoo nndoc-address nil)
142 (defconst nndoc-version "nndoc 1.0"
147 ;;; Interface functions
149 (nnoo-define-basics nndoc)
151 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
152 (when (nndoc-possibly-change-buffer newsgroup server)
154 (set-buffer nntp-server-buffer)
157 (if (stringp (car articles))
160 (when (setq entry (cdr (assq (setq article (pop articles))
161 nndoc-dissection-alist)))
162 (insert (format "221 %d Article retrieved.\n" article))
163 (if nndoc-generate-head-function
164 (funcall nndoc-generate-head-function article)
165 (insert-buffer-substring
166 nndoc-current-buffer (car entry) (nth 1 entry)))
167 (goto-char (point-max))
168 (or (= (char-after (1- (point))) ?\n) (insert "\n"))
169 (insert (format "Lines: %d\n" (nth 4 entry)))
172 (nnheader-fold-continuation-lines)
175 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
176 (nndoc-possibly-change-buffer newsgroup server)
178 (let ((buffer (or buffer nntp-server-buffer))
179 (entry (cdr (assq article nndoc-dissection-alist)))
183 (if (stringp article)
185 (insert-buffer-substring
186 nndoc-current-buffer (car entry) (nth 1 entry))
189 (insert-buffer-substring
190 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
192 (when nndoc-prepare-body-function
193 (funcall nndoc-prepare-body-function))
194 (when nndoc-article-transform-function
195 (funcall nndoc-article-transform-function article))
198 (deffoo nndoc-request-group (group &optional server dont-check)
202 ((not (nndoc-possibly-change-buffer group server))
203 (nnheader-report 'nndoc "No such file or buffer: %s"
206 (nnheader-report 'nndoc "Selected group %s" group)
208 ((zerop (setq number (length nndoc-dissection-alist)))
209 (nndoc-close-group group)
210 (nnheader-report 'nndoc "No articles in group %s" group))
212 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
214 (deffoo nndoc-request-type (group &optional article)
215 (cond ((not article) 'unknown)
216 (nndoc-post-type nndoc-post-type)
219 (deffoo nndoc-close-group (group &optional server)
220 (nndoc-possibly-change-buffer group server)
221 (and nndoc-current-buffer
222 (buffer-name nndoc-current-buffer)
223 (kill-buffer nndoc-current-buffer))
224 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
226 (setq nndoc-current-buffer nil)
227 (nnoo-close-server 'nndoc server)
228 (setq nndoc-dissection-alist nil)
231 (deffoo nndoc-request-list (&optional server)
234 (deffoo nndoc-request-newgroups (date &optional server)
237 (deffoo nndoc-request-list-newsgroups (&optional server)
241 ;;; Internal functions.
243 (defun nndoc-possibly-change-buffer (group source)
246 ;; The current buffer is this group's buffer.
247 ((and nndoc-current-buffer
248 (buffer-name nndoc-current-buffer)
249 (eq nndoc-current-buffer
250 (setq buf (cdr (assoc group nndoc-group-alist))))))
251 ;; We change buffers by taking an old from the group alist.
252 ;; `source' is either a string (a file name) or a buffer object.
254 (setq nndoc-current-buffer buf))
255 ;; It's a totally new group.
256 ((or (and (bufferp nndoc-address)
257 (buffer-name nndoc-address))
258 (and (stringp nndoc-address)
259 (file-exists-p nndoc-address)
260 (not (file-directory-p nndoc-address))))
261 (push (cons group (setq nndoc-current-buffer
263 (concat " *nndoc " group "*"))))
265 (setq nndoc-dissection-alist nil)
267 (set-buffer nndoc-current-buffer)
268 (buffer-disable-undo (current-buffer))
270 (if (stringp nndoc-address)
271 (insert-file-contents nndoc-address)
272 (insert-buffer-substring nndoc-address)))))
273 ;; Initialize the nndoc structures according to this new document.
274 (when (and nndoc-current-buffer
275 (not nndoc-dissection-alist))
277 (set-buffer nndoc-current-buffer)
279 (nndoc-dissect-buffer)))
280 (unless nndoc-current-buffer
281 (nndoc-close-server))
282 ;; Return whether we managed to select a file.
283 nndoc-current-buffer))
286 ;;; Deciding what document type we have
289 (defun nndoc-set-delims ()
290 "Set the nndoc delimiter variables according to the type of the document."
291 (let ((vars '(nndoc-file-begin
293 nndoc-article-end nndoc-head-begin nndoc-head-end
294 nndoc-file-end nndoc-article-begin
295 nndoc-body-begin nndoc-body-end-function nndoc-body-end
296 nndoc-prepare-body-function nndoc-article-transform-function
297 nndoc-generate-head-function nndoc-body-begin-function
298 nndoc-head-begin-function)))
300 (set (pop vars) nil)))
302 ;; Guess away until we find the real file type.
303 (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
305 (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
306 ;; Set the nndoc variables.
308 (set (intern (format "nndoc-%s" (caar defs)))
311 (defun nndoc-guess-type (subtype)
312 (let ((alist nndoc-type-alist)
313 results result entry)
314 (while (and (not result)
315 (setq entry (pop alist)))
316 (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
317 (goto-char (point-min))
318 (when (numberp (setq result (funcall (intern
319 (format "nndoc-%s-type-p"
321 (push (cons result entry) results)
323 (unless (or result results)
324 (error "Document is not of any recognized type"))
327 (cadar (sort results (lambda (r1 r2) (< (car r1) (car r2))))))))
330 ;;; Built-in type predicates and functions
333 (defun nndoc-mbox-type-p ()
334 (when (looking-at message-unix-mail-delimiter)
337 (defun nndoc-mbox-article-begin ()
338 (when (re-search-forward (concat "^" message-unix-mail-delimiter))
339 (goto-char (match-beginning 0))))
341 (defun nndoc-mbox-body-end ()
346 (and (re-search-backward nndoc-article-begin nil t)
348 (search-forward "\n\n" beg t)
350 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
351 (setq len (string-to-int (match-string 1)))
352 (search-forward "\n\n" beg t)
353 (or (= (setq len (+ (point) len)) (point-max))
354 (and (< len (point-max))
356 (looking-at nndoc-article-begin)))))
359 (defun nndoc-mmdf-type-p ()
360 (when (looking-at "\^A\^A\^A\^A$")
363 (defun nndoc-news-type-p ()
364 (when (looking-at "^Path:.*\n")
367 (defun nndoc-rnews-type-p ()
368 (when (looking-at "#! *rnews")
371 (defun nndoc-rnews-body-end ()
372 (and (re-search-backward nndoc-article-begin nil t)
374 (goto-char (+ (point) (string-to-int (match-string 1))))))
376 (defun nndoc-babyl-type-p ()
377 (when (re-search-forward "\^_\^L *\n" nil t)
380 (defun nndoc-babyl-body-begin ()
381 (re-search-forward "^\n" nil t)
382 (when (looking-at "\*\*\* EOOH \*\*\*")
383 (let ((next (or (save-excursion
384 (re-search-forward nndoc-article-begin nil t))
386 (unless (re-search-forward "^\n" next t)
390 (forward-line -1)))))
392 (defun nndoc-babyl-head-begin ()
393 (when (re-search-forward "^[0-9].*\n" nil t)
394 (when (looking-at "\*\*\* EOOH \*\*\*")
398 (defun nndoc-forward-type-p ()
399 (when (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
400 (not (re-search-forward "^Subject:.*digest" nil t)))
403 (defun nndoc-clari-briefs-type-p ()
404 (when (let ((case-fold-search nil))
405 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
408 (defun nndoc-transform-clari-briefs (article)
409 (goto-char (point-min))
410 (when (looking-at " *\\*\\(.*\\)\n")
411 (replace-match "" t t))
412 (nndoc-generate-clari-briefs-head article))
414 (defun nndoc-generate-clari-briefs-head (article)
415 (let ((entry (cdr (assq article nndoc-dissection-alist)))
418 (set-buffer nndoc-current-buffer)
420 (narrow-to-region (car entry) (nth 3 entry))
421 (goto-char (point-min))
422 (when (looking-at " *\\*\\(.*\\)$")
423 (setq subject (match-string 1))
424 (when (string-match "[ \t]+$" subject)
425 (setq subject (substring subject 0 (match-beginning 0)))))
427 (let ((case-fold-search nil))
429 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
430 (setq from (match-string 1)))))
431 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
432 "\nSubject: " (or subject "(no subject)") "\n")))
434 (defun nndoc-mime-digest-type-p ()
435 (let ((case-fold-search t)
436 boundary-id b-delimiter entry)
439 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
440 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
443 (setq boundary-id (match-string 1)
444 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
445 (setq entry (assq 'mime-digest nndoc-type-alist))
448 (cons 'head-end "^ ?$")
449 (cons 'body-begin "^ ?\n")
450 (cons 'article-begin b-delimiter)
451 (cons 'body-end-function 'nndoc-digest-body-end)
452 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
455 (defun nndoc-standard-digest-type-p ()
456 (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
458 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
461 (defun nndoc-digest-body-end ()
462 (and (re-search-forward nndoc-article-begin nil t)
463 (goto-char (match-beginning 0))))
465 (defun nndoc-slack-digest-type-p ()
468 (defun nndoc-lanl-gov-announce-type-p ()
469 (when (let ((case-fold-search nil))
470 (re-search-forward "^\\\\\\\\\nPaper: [a-z-]+/[0-9]+" nil t))
473 (defun nndoc-transform-lanl-gov-announce (article)
474 (goto-char (point-max))
475 (when (re-search-backward "^\\\\\\\\ +(\\([^ ]*\\) , *\\([^ ]*\\))" nil t)
476 (replace-match "\n\nGet it at \\1 (\\2)" t nil))
477 ;; (when (re-search-backward "^\\\\\\\\$" nil t)
478 ;; (replace-match "" t t))
481 (defun nndoc-generate-lanl-gov-head (article)
482 (let ((entry (cdr (assq article nndoc-dissection-alist)))
483 (e-mail "no address given")
486 (set-buffer nndoc-current-buffer)
488 (narrow-to-region (car entry) (nth 1 entry))
489 (goto-char (point-min))
490 (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
491 (setq subject (concat " (" (match-string 1) ")"))
492 (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
493 (setq e-mail (match-string 1)))
494 (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
496 (setq subject (concat (match-string 1) subject))
497 (setq from (concat (match-string 2) " <" e-mail ">"))))
499 (while (and from (string-match "(\[^)\]*)" from))
500 (setq from (replace-match "" t t from)))
501 (insert "From: " (or from "unknown")
502 "\nSubject: " (or subject "(no subject)") "\n")))
507 ;;; Functions for dissecting the documents
510 (defun nndoc-search (regexp)
512 (re-search-forward regexp nil t)
513 (beginning-of-line)))
515 (defun nndoc-dissect-buffer ()
516 "Go through the document and partition it into heads/bodies/articles."
519 head-begin head-end body-begin body-end)
520 (setq nndoc-dissection-alist nil)
522 (set-buffer nndoc-current-buffer)
523 (goto-char (point-min))
524 ;; Find the beginning of the file.
525 (when nndoc-file-begin
526 (nndoc-search nndoc-file-begin))
527 ;; Go through the file.
528 (while (if (and first nndoc-first-article)
529 (nndoc-search nndoc-first-article)
531 (nndoc-search nndoc-article-begin)
534 (cond (nndoc-head-begin-function
535 (funcall nndoc-head-begin-function))
537 (nndoc-search nndoc-head-begin)))
538 (if (or (>= (point) (point-max))
540 (looking-at nndoc-file-end)))
541 (goto-char (point-max))
542 (setq head-begin (point))
543 (nndoc-search (or nndoc-head-end "^$"))
544 (setq head-end (point))
545 (if nndoc-body-begin-function
546 (funcall nndoc-body-begin-function)
547 (nndoc-search (or nndoc-body-begin "^\n")))
548 (setq body-begin (point))
549 (or (and nndoc-body-end-function
550 (funcall nndoc-body-end-function))
552 (nndoc-search nndoc-body-end))
554 (nndoc-search nndoc-article-begin)
557 (goto-char (point-max))
559 (and (re-search-backward nndoc-file-end nil t)
560 (beginning-of-line)))))
561 (setq body-end (point))
562 (push (list (incf i) head-begin head-end body-begin body-end
563 (count-lines body-begin body-end))
564 nndoc-dissection-alist))))))
566 (defun nndoc-unquote-dashes ()
567 "Unquote quoted non-separators in digests."
568 (while (re-search-forward "^- -"nil t)
569 (replace-match "-" t t)))
572 (defun nndoc-add-type (definition &optional position)
573 "Add document DEFINITION to the list of nndoc document definitions.
574 If POSITION is nil or `last', the definition will be added
575 as the last checked definition, if t or `first', add as the
576 first definition, and if any other symbol, add after that
577 symbol in the alist."
578 ;; First remove any old instances.
579 (setq nndoc-type-alist
580 (delq (assq (car definition) nndoc-type-alist)
582 ;; Then enter the new definition in the proper place.
584 ((or (null position) (eq position 'last))
585 (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
586 ((or (eq position t) (eq position 'first))
587 (push definition nndoc-type-alist))
589 (let ((list (memq (assq position nndoc-type-alist)
592 (error "No such position: %s" position))
593 (setcdr list (cons definition (cdr list)))))))
597 ;;; nndoc.el ends here