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 (unless (= (char-after (1- (point))) ?\n)
171 (insert (format "Lines: %d\n" (nth 4 entry)))
174 (nnheader-fold-continuation-lines)
177 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
178 (nndoc-possibly-change-buffer newsgroup server)
180 (let ((buffer (or buffer nntp-server-buffer))
181 (entry (cdr (assq article nndoc-dissection-alist)))
186 (if (stringp article)
188 (insert-buffer-substring
189 nndoc-current-buffer (car entry) (nth 1 entry))
192 (insert-buffer-substring
193 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
195 (when nndoc-prepare-body-function
196 (funcall nndoc-prepare-body-function))
197 (when nndoc-article-transform-function
198 (funcall nndoc-article-transform-function article))
201 (deffoo nndoc-request-group (group &optional server dont-check)
205 ((not (nndoc-possibly-change-buffer group server))
206 (nnheader-report 'nndoc "No such file or buffer: %s"
209 (nnheader-report 'nndoc "Selected group %s" group)
211 ((zerop (setq number (length nndoc-dissection-alist)))
212 (nndoc-close-group group)
213 (nnheader-report 'nndoc "No articles in group %s" group))
215 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
217 (deffoo nndoc-request-type (group &optional article)
218 (cond ((not article) 'unknown)
219 (nndoc-post-type nndoc-post-type)
222 (deffoo nndoc-close-group (group &optional server)
223 (nndoc-possibly-change-buffer group server)
224 (and nndoc-current-buffer
225 (buffer-name nndoc-current-buffer)
226 (kill-buffer nndoc-current-buffer))
227 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
229 (setq nndoc-current-buffer nil)
230 (nnoo-close-server 'nndoc server)
231 (setq nndoc-dissection-alist nil)
234 (deffoo nndoc-request-list (&optional server)
237 (deffoo nndoc-request-newgroups (date &optional server)
240 (deffoo nndoc-request-list-newsgroups (&optional server)
244 ;;; Internal functions.
246 (defun nndoc-possibly-change-buffer (group source)
249 ;; The current buffer is this group's buffer.
250 ((and nndoc-current-buffer
251 (buffer-name nndoc-current-buffer)
252 (eq nndoc-current-buffer
253 (setq buf (cdr (assoc group nndoc-group-alist))))))
254 ;; We change buffers by taking an old from the group alist.
255 ;; `source' is either a string (a file name) or a buffer object.
257 (setq nndoc-current-buffer buf))
258 ;; It's a totally new group.
259 ((or (and (bufferp nndoc-address)
260 (buffer-name nndoc-address))
261 (and (stringp nndoc-address)
262 (file-exists-p nndoc-address)
263 (not (file-directory-p nndoc-address))))
264 (push (cons group (setq nndoc-current-buffer
266 (concat " *nndoc " group "*"))))
268 (setq nndoc-dissection-alist nil)
270 (set-buffer nndoc-current-buffer)
271 (buffer-disable-undo (current-buffer))
273 (if (stringp nndoc-address)
274 (nnheader-insert-file-contents nndoc-address)
275 (insert-buffer-substring nndoc-address)))))
276 ;; Initialize the nndoc structures according to this new document.
277 (when (and nndoc-current-buffer
278 (not nndoc-dissection-alist))
280 (set-buffer nndoc-current-buffer)
282 (nndoc-dissect-buffer)))
283 (unless nndoc-current-buffer
284 (nndoc-close-server))
285 ;; Return whether we managed to select a file.
286 nndoc-current-buffer))
289 ;;; Deciding what document type we have
292 (defun nndoc-set-delims ()
293 "Set the nndoc delimiter variables according to the type of the document."
294 (let ((vars '(nndoc-file-begin
296 nndoc-article-end nndoc-head-begin nndoc-head-end
297 nndoc-file-end nndoc-article-begin
298 nndoc-body-begin nndoc-body-end-function nndoc-body-end
299 nndoc-prepare-body-function nndoc-article-transform-function
300 nndoc-generate-head-function nndoc-body-begin-function
301 nndoc-head-begin-function)))
303 (set (pop vars) nil)))
305 ;; Guess away until we find the real file type.
306 (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
308 (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
309 ;; Set the nndoc variables.
311 (set (intern (format "nndoc-%s" (caar defs)))
314 (defun nndoc-guess-type (subtype)
315 (let ((alist nndoc-type-alist)
316 results result entry)
317 (while (and (not result)
318 (setq entry (pop alist)))
319 (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
320 (goto-char (point-min))
321 (when (numberp (setq result (funcall (intern
322 (format "nndoc-%s-type-p"
324 (push (cons result entry) results)
326 (unless (or result results)
327 (error "Document is not of any recognized type"))
330 (cadar (sort results (lambda (r1 r2) (< (car r1) (car r2))))))))
333 ;;; Built-in type predicates and functions
336 (defun nndoc-mbox-type-p ()
337 (when (looking-at message-unix-mail-delimiter)
340 (defun nndoc-mbox-article-begin ()
341 (when (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
342 (goto-char (match-beginning 0))))
344 (defun nndoc-mbox-body-end ()
349 (and (re-search-backward
350 (concat "^" message-unix-mail-delimiter) nil t)
352 (search-forward "\n\n" beg t)
354 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
355 (setq len (string-to-int (match-string 1)))
356 (search-forward "\n\n" beg t)
357 (unless (= (setq len (+ (point) len)) (point-max))
358 (and (< len (point-max))
360 (looking-at message-unix-mail-delimiter)))))
363 (defun nndoc-mmdf-type-p ()
364 (when (looking-at "\^A\^A\^A\^A$")
367 (defun nndoc-news-type-p ()
368 (when (looking-at "^Path:.*\n")
371 (defun nndoc-rnews-type-p ()
372 (when (looking-at "#! *rnews")
375 (defun nndoc-rnews-body-end ()
376 (and (re-search-backward nndoc-article-begin nil t)
378 (goto-char (+ (point) (string-to-int (match-string 1))))))
380 (defun nndoc-babyl-type-p ()
381 (when (re-search-forward "\^_\^L *\n" nil t)
384 (defun nndoc-babyl-body-begin ()
385 (re-search-forward "^\n" nil t)
386 (when (looking-at "\*\*\* EOOH \*\*\*")
387 (let ((next (or (save-excursion
388 (re-search-forward nndoc-article-begin nil t))
390 (unless (re-search-forward "^\n" next t)
394 (forward-line -1)))))
396 (defun nndoc-babyl-head-begin ()
397 (when (re-search-forward "^[0-9].*\n" nil t)
398 (when (looking-at "\*\*\* EOOH \*\*\*")
402 (defun nndoc-forward-type-p ()
403 (when (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
404 (not (re-search-forward "^Subject:.*digest" nil t))
405 (not (re-search-backward "^From:" nil t 2))
406 (not (re-search-forward "^From:" nil t 2)))
409 (defun nndoc-clari-briefs-type-p ()
410 (when (let ((case-fold-search nil))
411 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
414 (defun nndoc-transform-clari-briefs (article)
415 (goto-char (point-min))
416 (when (looking-at " *\\*\\(.*\\)\n")
417 (replace-match "" t t))
418 (nndoc-generate-clari-briefs-head article))
420 (defun nndoc-generate-clari-briefs-head (article)
421 (let ((entry (cdr (assq article nndoc-dissection-alist)))
424 (set-buffer nndoc-current-buffer)
426 (narrow-to-region (car entry) (nth 3 entry))
427 (goto-char (point-min))
428 (when (looking-at " *\\*\\(.*\\)$")
429 (setq subject (match-string 1))
430 (when (string-match "[ \t]+$" subject)
431 (setq subject (substring subject 0 (match-beginning 0)))))
433 (let ((case-fold-search nil))
435 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
436 (setq from (match-string 1)))))
437 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
438 "\nSubject: " (or subject "(no subject)") "\n")))
440 (defun nndoc-mime-digest-type-p ()
441 (let ((case-fold-search t)
442 boundary-id b-delimiter entry)
445 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
446 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
449 (setq boundary-id (match-string 1)
450 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
451 (setq entry (assq 'mime-digest nndoc-type-alist))
454 (cons 'head-end "^ ?$")
455 (cons 'body-begin "^ ?\n")
456 (cons 'article-begin b-delimiter)
457 (cons 'body-end-function 'nndoc-digest-body-end)
458 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
461 (defun nndoc-standard-digest-type-p ()
462 (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
464 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
467 (defun nndoc-digest-body-end ()
468 (and (re-search-forward nndoc-article-begin nil t)
469 (goto-char (match-beginning 0))))
471 (defun nndoc-slack-digest-type-p ()
474 (defun nndoc-lanl-gov-announce-type-p ()
475 (when (let ((case-fold-search nil))
476 (re-search-forward "^\\\\\\\\\nPaper: [a-z-]+/[0-9]+" nil t))
479 (defun nndoc-transform-lanl-gov-announce (article)
480 (goto-char (point-max))
481 (when (re-search-backward "^\\\\\\\\ +(\\([^ ]*\\) , *\\([^ ]*\\))" nil t)
482 (replace-match "\n\nGet it at \\1 (\\2)" t nil))
483 ;; (when (re-search-backward "^\\\\\\\\$" nil t)
484 ;; (replace-match "" t t))
487 (defun nndoc-generate-lanl-gov-head (article)
488 (let ((entry (cdr (assq article nndoc-dissection-alist)))
489 (e-mail "no address given")
492 (set-buffer nndoc-current-buffer)
494 (narrow-to-region (car entry) (nth 1 entry))
495 (goto-char (point-min))
496 (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
497 (setq subject (concat " (" (match-string 1) ")"))
498 (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
499 (setq e-mail (match-string 1)))
500 (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
502 (setq subject (concat (match-string 1) subject))
503 (setq from (concat (match-string 2) " <" e-mail ">"))))
505 (while (and from (string-match "(\[^)\]*)" from))
506 (setq from (replace-match "" t t from)))
507 (insert "From: " (or from "unknown")
508 "\nSubject: " (or subject "(no subject)") "\n")))
513 ;;; Functions for dissecting the documents
516 (defun nndoc-search (regexp)
518 (re-search-forward regexp nil t)
519 (beginning-of-line)))
521 (defun nndoc-dissect-buffer ()
522 "Go through the document and partition it into heads/bodies/articles."
525 head-begin head-end body-begin body-end)
526 (setq nndoc-dissection-alist nil)
528 (set-buffer nndoc-current-buffer)
529 (goto-char (point-min))
530 ;; Find the beginning of the file.
531 (when nndoc-file-begin
532 (nndoc-search nndoc-file-begin))
533 ;; Go through the file.
534 (while (if (and first nndoc-first-article)
535 (nndoc-search nndoc-first-article)
536 (nndoc-article-begin))
538 (cond (nndoc-head-begin-function
539 (funcall nndoc-head-begin-function))
541 (nndoc-search nndoc-head-begin)))
542 (if (or (>= (point) (point-max))
544 (looking-at nndoc-file-end)))
545 (goto-char (point-max))
546 (setq head-begin (point))
547 (nndoc-search (or nndoc-head-end "^$"))
548 (setq head-end (point))
549 (if nndoc-body-begin-function
550 (funcall nndoc-body-begin-function)
551 (nndoc-search (or nndoc-body-begin "^\n")))
552 (setq body-begin (point))
553 (or (and nndoc-body-end-function
554 (funcall nndoc-body-end-function))
556 (nndoc-search nndoc-body-end))
557 (nndoc-article-begin)
559 (goto-char (point-max))
561 (and (re-search-backward nndoc-file-end nil t)
562 (beginning-of-line)))))
563 (setq body-end (point))
564 (push (list (incf i) head-begin head-end body-begin body-end
565 (count-lines body-begin body-end))
566 nndoc-dissection-alist))))))
568 (defun nndoc-article-begin ()
569 (if nndoc-article-begin-function
570 (funcall nndoc-article-begin-function)
572 (nndoc-search nndoc-article-begin))))
574 (defun nndoc-unquote-dashes ()
575 "Unquote quoted non-separators in digests."
576 (while (re-search-forward "^- -"nil t)
577 (replace-match "-" t t)))
580 (defun nndoc-add-type (definition &optional position)
581 "Add document DEFINITION to the list of nndoc document definitions.
582 If POSITION is nil or `last', the definition will be added
583 as the last checked definition, if t or `first', add as the
584 first definition, and if any other symbol, add after that
585 symbol in the alist."
586 ;; First remove any old instances.
587 (setq nndoc-type-alist
588 (delq (assq (car definition) nndoc-type-alist)
590 ;; Then enter the new definition in the proper place.
592 ((or (null position) (eq position 'last))
593 (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
594 ((or (eq position t) (eq position 'first))
595 (push definition nndoc-type-alist))
597 (let ((list (memq (assq position nndoc-type-alist)
600 (error "No such position: %s" position))
601 (setcdr list (cons definition (cdr list)))))))
605 ;;; nndoc.el ends here