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)
135 (defvoo nndoc-article-begin-function nil)
137 (defvoo nndoc-status-string "")
138 (defvoo nndoc-group-alist nil)
139 (defvoo nndoc-current-buffer nil
140 "Current nndoc news buffer.")
141 (defvoo nndoc-address nil)
143 (defconst nndoc-version "nndoc 1.0"
148 ;;; Interface functions
150 (nnoo-define-basics nndoc)
152 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
153 (when (nndoc-possibly-change-buffer newsgroup server)
155 (set-buffer nntp-server-buffer)
158 (if (stringp (car articles))
161 (when (setq entry (cdr (assq (setq article (pop articles))
162 nndoc-dissection-alist)))
163 (insert (format "221 %d Article retrieved.\n" article))
164 (if nndoc-generate-head-function
165 (funcall nndoc-generate-head-function article)
166 (insert-buffer-substring
167 nndoc-current-buffer (car entry) (nth 1 entry)))
168 (goto-char (point-max))
169 (or (= (char-after (1- (point))) ?\n) (insert "\n"))
170 (insert (format "Lines: %d\n" (nth 4 entry)))
173 (nnheader-fold-continuation-lines)
176 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
177 (nndoc-possibly-change-buffer newsgroup server)
179 (let ((buffer (or buffer nntp-server-buffer))
180 (entry (cdr (assq article nndoc-dissection-alist)))
184 (if (stringp article)
186 (insert-buffer-substring
187 nndoc-current-buffer (car entry) (nth 1 entry))
190 (insert-buffer-substring
191 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
193 (when nndoc-prepare-body-function
194 (funcall nndoc-prepare-body-function))
195 (when nndoc-article-transform-function
196 (funcall nndoc-article-transform-function article))
199 (deffoo nndoc-request-group (group &optional server dont-check)
203 ((not (nndoc-possibly-change-buffer group server))
204 (nnheader-report 'nndoc "No such file or buffer: %s"
207 (nnheader-report 'nndoc "Selected group %s" group)
209 ((zerop (setq number (length nndoc-dissection-alist)))
210 (nndoc-close-group group)
211 (nnheader-report 'nndoc "No articles in group %s" group))
213 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
215 (deffoo nndoc-request-type (group &optional article)
216 (cond ((not article) 'unknown)
217 (nndoc-post-type nndoc-post-type)
220 (deffoo nndoc-close-group (group &optional server)
221 (nndoc-possibly-change-buffer group server)
222 (and nndoc-current-buffer
223 (buffer-name nndoc-current-buffer)
224 (kill-buffer nndoc-current-buffer))
225 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
227 (setq nndoc-current-buffer nil)
228 (nnoo-close-server 'nndoc server)
229 (setq nndoc-dissection-alist nil)
232 (deffoo nndoc-request-list (&optional server)
235 (deffoo nndoc-request-newgroups (date &optional server)
238 (deffoo nndoc-request-list-newsgroups (&optional server)
242 ;;; Internal functions.
244 (defun nndoc-possibly-change-buffer (group source)
247 ;; The current buffer is this group's buffer.
248 ((and nndoc-current-buffer
249 (buffer-name nndoc-current-buffer)
250 (eq nndoc-current-buffer
251 (setq buf (cdr (assoc group nndoc-group-alist))))))
252 ;; We change buffers by taking an old from the group alist.
253 ;; `source' is either a string (a file name) or a buffer object.
255 (setq nndoc-current-buffer buf))
256 ;; It's a totally new group.
257 ((or (and (bufferp nndoc-address)
258 (buffer-name nndoc-address))
259 (and (stringp nndoc-address)
260 (file-exists-p nndoc-address)
261 (not (file-directory-p nndoc-address))))
262 (push (cons group (setq nndoc-current-buffer
264 (concat " *nndoc " group "*"))))
266 (setq nndoc-dissection-alist nil)
268 (set-buffer nndoc-current-buffer)
269 (buffer-disable-undo (current-buffer))
271 (if (stringp nndoc-address)
272 (insert-file-contents nndoc-address)
273 (insert-buffer-substring nndoc-address)))))
274 ;; Initialize the nndoc structures according to this new document.
275 (when (and nndoc-current-buffer
276 (not nndoc-dissection-alist))
278 (set-buffer nndoc-current-buffer)
280 (nndoc-dissect-buffer)))
281 (unless nndoc-current-buffer
282 (nndoc-close-server))
283 ;; Return whether we managed to select a file.
284 nndoc-current-buffer))
287 ;;; Deciding what document type we have
290 (defun nndoc-set-delims ()
291 "Set the nndoc delimiter variables according to the type of the document."
292 (let ((vars '(nndoc-file-begin
294 nndoc-article-end nndoc-head-begin nndoc-head-end
295 nndoc-file-end nndoc-article-begin
296 nndoc-body-begin nndoc-body-end-function nndoc-body-end
297 nndoc-prepare-body-function nndoc-article-transform-function
298 nndoc-generate-head-function nndoc-body-begin-function
299 nndoc-head-begin-function)))
301 (set (pop vars) nil)))
303 ;; Guess away until we find the real file type.
304 (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
306 (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
307 ;; Set the nndoc variables.
309 (set (intern (format "nndoc-%s" (caar defs)))
312 (defun nndoc-guess-type (subtype)
313 (let ((alist nndoc-type-alist)
314 results result entry)
315 (while (and (not result)
316 (setq entry (pop alist)))
317 (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
318 (goto-char (point-min))
319 (when (numberp (setq result (funcall (intern
320 (format "nndoc-%s-type-p"
322 (push (cons result entry) results)
324 (unless (or result results)
325 (error "Document is not of any recognized type"))
328 (cadar (sort results (lambda (r1 r2) (< (car r1) (car r2))))))))
331 ;;; Built-in type predicates and functions
334 (defun nndoc-mbox-type-p ()
335 (when (looking-at message-unix-mail-delimiter)
338 (defun nndoc-mbox-article-begin ()
339 (when (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
340 (goto-char (match-beginning 0))))
342 (defun nndoc-mbox-body-end ()
347 (and (re-search-backward
348 (concat "^" message-unix-mail-delimiter) nil t)
350 (search-forward "\n\n" beg t)
352 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
353 (setq len (string-to-int (match-string 1)))
354 (search-forward "\n\n" beg t)
355 (or (= (setq len (+ (point) len)) (point-max))
356 (and (< len (point-max))
358 (looking-at message-unix-mail-delimiter)))))
361 (defun nndoc-mmdf-type-p ()
362 (when (looking-at "\^A\^A\^A\^A$")
365 (defun nndoc-news-type-p ()
366 (when (looking-at "^Path:.*\n")
369 (defun nndoc-rnews-type-p ()
370 (when (looking-at "#! *rnews")
373 (defun nndoc-rnews-body-end ()
374 (and (re-search-backward nndoc-article-begin nil t)
376 (goto-char (+ (point) (string-to-int (match-string 1))))))
378 (defun nndoc-babyl-type-p ()
379 (when (re-search-forward "\^_\^L *\n" nil t)
382 (defun nndoc-babyl-body-begin ()
383 (re-search-forward "^\n" nil t)
384 (when (looking-at "\*\*\* EOOH \*\*\*")
385 (let ((next (or (save-excursion
386 (re-search-forward nndoc-article-begin nil t))
388 (unless (re-search-forward "^\n" next t)
392 (forward-line -1)))))
394 (defun nndoc-babyl-head-begin ()
395 (when (re-search-forward "^[0-9].*\n" nil t)
396 (when (looking-at "\*\*\* EOOH \*\*\*")
400 (defun nndoc-forward-type-p ()
401 (when (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
402 (not (re-search-forward "^Subject:.*digest" nil t)))
405 (defun nndoc-clari-briefs-type-p ()
406 (when (let ((case-fold-search nil))
407 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
410 (defun nndoc-transform-clari-briefs (article)
411 (goto-char (point-min))
412 (when (looking-at " *\\*\\(.*\\)\n")
413 (replace-match "" t t))
414 (nndoc-generate-clari-briefs-head article))
416 (defun nndoc-generate-clari-briefs-head (article)
417 (let ((entry (cdr (assq article nndoc-dissection-alist)))
420 (set-buffer nndoc-current-buffer)
422 (narrow-to-region (car entry) (nth 3 entry))
423 (goto-char (point-min))
424 (when (looking-at " *\\*\\(.*\\)$")
425 (setq subject (match-string 1))
426 (when (string-match "[ \t]+$" subject)
427 (setq subject (substring subject 0 (match-beginning 0)))))
429 (let ((case-fold-search nil))
431 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
432 (setq from (match-string 1)))))
433 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
434 "\nSubject: " (or subject "(no subject)") "\n")))
436 (defun nndoc-mime-digest-type-p ()
437 (let ((case-fold-search t)
438 boundary-id b-delimiter entry)
441 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
442 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
445 (setq boundary-id (match-string 1)
446 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
447 (setq entry (assq 'mime-digest nndoc-type-alist))
450 (cons 'head-end "^ ?$")
451 (cons 'body-begin "^ ?\n")
452 (cons 'article-begin b-delimiter)
453 (cons 'body-end-function 'nndoc-digest-body-end)
454 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
457 (defun nndoc-standard-digest-type-p ()
458 (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
460 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
463 (defun nndoc-digest-body-end ()
464 (and (re-search-forward nndoc-article-begin nil t)
465 (goto-char (match-beginning 0))))
467 (defun nndoc-slack-digest-type-p ()
470 (defun nndoc-lanl-gov-announce-type-p ()
471 (when (let ((case-fold-search nil))
472 (re-search-forward "^\\\\\\\\\nPaper: [a-z-]+/[0-9]+" nil t))
475 (defun nndoc-transform-lanl-gov-announce (article)
476 (goto-char (point-max))
477 (when (re-search-backward "^\\\\\\\\ +(\\([^ ]*\\) , *\\([^ ]*\\))" nil t)
478 (replace-match "\n\nGet it at \\1 (\\2)" t nil))
479 ;; (when (re-search-backward "^\\\\\\\\$" nil t)
480 ;; (replace-match "" t t))
483 (defun nndoc-generate-lanl-gov-head (article)
484 (let ((entry (cdr (assq article nndoc-dissection-alist)))
485 (e-mail "no address given")
488 (set-buffer nndoc-current-buffer)
490 (narrow-to-region (car entry) (nth 1 entry))
491 (goto-char (point-min))
492 (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
493 (setq subject (concat " (" (match-string 1) ")"))
494 (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
495 (setq e-mail (match-string 1)))
496 (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
498 (setq subject (concat (match-string 1) subject))
499 (setq from (concat (match-string 2) " <" e-mail ">"))))
501 (while (and from (string-match "(\[^)\]*)" from))
502 (setq from (replace-match "" t t from)))
503 (insert "From: " (or from "unknown")
504 "\nSubject: " (or subject "(no subject)") "\n")))
509 ;;; Functions for dissecting the documents
512 (defun nndoc-search (regexp)
514 (re-search-forward regexp nil t)
515 (beginning-of-line)))
517 (defun nndoc-dissect-buffer ()
518 "Go through the document and partition it into heads/bodies/articles."
521 head-begin head-end body-begin body-end)
522 (setq nndoc-dissection-alist nil)
524 (set-buffer nndoc-current-buffer)
525 (goto-char (point-min))
526 ;; Find the beginning of the file.
527 (when nndoc-file-begin
528 (nndoc-search nndoc-file-begin))
529 ;; Go through the file.
530 (while (if (and first nndoc-first-article)
531 (nndoc-search nndoc-first-article)
532 (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))
553 (nndoc-article-begin)
555 (goto-char (point-max))
557 (and (re-search-backward nndoc-file-end nil t)
558 (beginning-of-line)))))
559 (setq body-end (point))
560 (push (list (incf i) head-begin head-end body-begin body-end
561 (count-lines body-begin body-end))
562 nndoc-dissection-alist))))))
564 (defun nndoc-article-begin ()
565 (if nndoc-article-begin-function
566 (funcall nndoc-article-begin-function)
568 (nndoc-search nndoc-article-begin)
571 (defun nndoc-unquote-dashes ()
572 "Unquote quoted non-separators in digests."
573 (while (re-search-forward "^- -"nil t)
574 (replace-match "-" t t)))
577 (defun nndoc-add-type (definition &optional position)
578 "Add document DEFINITION to the list of nndoc document definitions.
579 If POSITION is nil or `last', the definition will be added
580 as the last checked definition, if t or `first', add as the
581 first definition, and if any other symbol, add after that
582 symbol in the alist."
583 ;; First remove any old instances.
584 (setq nndoc-type-alist
585 (delq (assq (car definition) nndoc-type-alist)
587 ;; Then enter the new definition in the proper place.
589 ((or (null position) (eq position 'last))
590 (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
591 ((or (eq position t) (eq position 'first))
592 (push definition nndoc-type-alist))
594 (let ((list (memq (assq position nndoc-type-alist)
597 (error "No such position: %s" position))
598 (setcdr list (cons definition (cdr list)))))))
602 ;;; nndoc.el ends here