1 ;;; nndoc.el --- single file access for Gnus
2 ;; Copyright (C) 1995,96 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 `mime-digest', `standard-digest', `slack-digest', `clari-briefs' or
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))
57 ,(let ((delim (concat "^" message-unix-mail-delimiter)))
58 (if (string-match "\n\\'" delim)
59 (substring delim 0 (match-beginning 0))
61 (body-end-function . nndoc-mbox-body-end))
63 (article-begin . "\^_\^L *\n")
65 (body-begin-function . nndoc-babyl-body-begin)
66 (head-begin-function . nndoc-babyl-head-begin))
68 (article-begin . "^-+ Start of forwarded message -+\n+")
69 (body-end . "^-+ End of forwarded message -+$")
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\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 (guess . nndoc-guess-type))
104 (guess . nndoc-guess-digest-type))
109 (defvoo nndoc-file-begin nil)
110 (defvoo nndoc-first-article nil)
111 (defvoo nndoc-article-end nil)
112 (defvoo nndoc-article-begin nil)
113 (defvoo nndoc-head-begin nil)
114 (defvoo nndoc-head-end nil)
115 (defvoo nndoc-file-end nil)
116 (defvoo nndoc-body-begin nil)
117 (defvoo nndoc-body-end-function nil)
118 (defvoo nndoc-body-begin-function nil)
119 (defvoo nndoc-head-begin-function nil)
120 (defvoo nndoc-body-end nil)
121 (defvoo nndoc-dissection-alist nil)
122 (defvoo nndoc-prepare-body-function nil)
123 (defvoo nndoc-generate-head-function nil)
124 (defvoo nndoc-article-transform-function nil)
126 (defvoo nndoc-status-string "")
127 (defvoo nndoc-group-alist nil)
128 (defvoo nndoc-current-buffer nil
129 "Current nndoc news buffer.")
130 (defvoo nndoc-address nil)
132 (defconst nndoc-version "nndoc 1.0"
137 ;;; Interface functions
139 (nnoo-define-basics nndoc)
141 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
142 (when (nndoc-possibly-change-buffer newsgroup server)
144 (set-buffer nntp-server-buffer)
147 (if (stringp (car articles))
150 (when (setq entry (cdr (assq (setq article (pop articles))
151 nndoc-dissection-alist)))
152 (insert (format "221 %d Article retrieved.\n" article))
153 (if nndoc-generate-head-function
154 (funcall nndoc-generate-head-function article)
155 (insert-buffer-substring
156 nndoc-current-buffer (car entry) (nth 1 entry)))
157 (goto-char (point-max))
158 (or (= (char-after (1- (point))) ?\n) (insert "\n"))
159 (insert (format "Lines: %d\n" (nth 4 entry)))
162 (nnheader-fold-continuation-lines)
165 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
166 (nndoc-possibly-change-buffer newsgroup server)
168 (let ((buffer (or buffer nntp-server-buffer))
169 (entry (cdr (assq article nndoc-dissection-alist)))
173 (if (stringp article)
175 (insert-buffer-substring
176 nndoc-current-buffer (car entry) (nth 1 entry))
179 (insert-buffer-substring
180 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
182 (when nndoc-prepare-body-function
183 (funcall nndoc-prepare-body-function))
184 (when nndoc-article-transform-function
185 (funcall nndoc-article-transform-function article))
188 (deffoo nndoc-request-group (group &optional server dont-check)
192 ((not (nndoc-possibly-change-buffer group server))
193 (nnheader-report 'nndoc "No such file or buffer: %s"
196 (nnheader-report 'nndoc "Selected group %s" group)
198 ((zerop (setq number (length nndoc-dissection-alist)))
199 (nndoc-close-group group)
200 (nnheader-report 'nndoc "No articles in group %s" group))
202 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
204 (deffoo nndoc-request-type (group &optional article)
205 (cond ((not article) 'unknown)
206 (nndoc-post-type nndoc-post-type)
209 (deffoo nndoc-close-group (group &optional server)
210 (nndoc-possibly-change-buffer group server)
211 (and nndoc-current-buffer
212 (buffer-name nndoc-current-buffer)
213 (kill-buffer nndoc-current-buffer))
214 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
216 (setq nndoc-current-buffer nil)
217 (nnoo-close-server 'nndoc server)
218 (setq nndoc-dissection-alist nil)
221 (deffoo nndoc-request-list (&optional server)
224 (deffoo nndoc-request-newgroups (date &optional server)
227 (deffoo nndoc-request-list-newsgroups (&optional server)
231 ;;; Internal functions.
233 (defun nndoc-possibly-change-buffer (group source)
236 ;; The current buffer is this group's buffer.
237 ((and nndoc-current-buffer
238 (buffer-name nndoc-current-buffer)
239 (eq nndoc-current-buffer
240 (setq buf (cdr (assoc group nndoc-group-alist))))))
241 ;; We change buffers by taking an old from the group alist.
242 ;; `source' is either a string (a file name) or a buffer object.
244 (setq nndoc-current-buffer buf))
245 ;; It's a totally new group.
246 ((or (and (bufferp nndoc-address)
247 (buffer-name nndoc-address))
248 (and (stringp nndoc-address)
249 (file-exists-p nndoc-address)
250 (not (file-directory-p nndoc-address))))
251 (push (cons group (setq nndoc-current-buffer
253 (concat " *nndoc " group "*"))))
255 (setq nndoc-dissection-alist nil)
257 (set-buffer nndoc-current-buffer)
258 (buffer-disable-undo (current-buffer))
260 (if (stringp nndoc-address)
261 (insert-file-contents nndoc-address)
262 (insert-buffer-substring nndoc-address)))))
263 ;; Initialize the nndoc structures according to this new document.
264 (when (and nndoc-current-buffer
265 (not nndoc-dissection-alist))
267 (set-buffer nndoc-current-buffer)
269 (nndoc-dissect-buffer)))
270 (unless nndoc-current-buffer
271 (nndoc-close-server))
272 ;; Return whether we managed to select a file.
273 nndoc-current-buffer))
276 ;;; Deciding what document type we have
279 (defun nndoc-set-delims ()
280 "Set the nndoc delimiter variables according to the type of the document."
281 (let ((vars '(nndoc-file-begin
283 nndoc-article-end nndoc-head-begin nndoc-head-end
284 nndoc-file-end nndoc-article-begin
285 nndoc-body-begin nndoc-body-end-function nndoc-body-end
286 nndoc-prepare-body-function nndoc-article-transform-function
287 nndoc-generate-head-function nndoc-body-begin-function
288 nndoc-head-begin-function)))
290 (set (pop vars) nil)))
292 ;; Guess away until we find the real file type.
293 (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
295 (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
296 ;; Set the nndoc variables.
298 (set (intern (format "nndoc-%s" (caar defs)))
301 (defun nndoc-guess-type (subtype)
302 (let ((alist nndoc-type-alist)
303 results result entry)
304 (while (and (not result)
305 (setq entry (pop alist)))
306 (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
307 (goto-char (point-min))
308 (when (numberp (setq result (funcall (intern
309 (format "nndoc-%s-type-p"
311 (push (cons result entry) results)
313 (unless (or result results)
314 (error "Document is not of any recognized type"))
317 (cadar (sort results (lambda (r1 r2) (< (car r1) (car r2))))))))
320 ;;; Built-in type predicates and functions
323 (defun nndoc-mbox-type-p ()
324 (when (looking-at message-unix-mail-delimiter)
327 (defun nndoc-mbox-body-end ()
332 (and (re-search-backward nndoc-article-begin nil t)
334 (search-forward "\n\n" beg t)
336 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
337 (setq len (string-to-int (match-string 1)))
338 (search-forward "\n\n" beg t)
339 (or (= (setq len (+ (point) len)) (point-max))
340 (and (< len (point-max))
342 (looking-at nndoc-article-begin)))))
345 (defun nndoc-mmdf-type-p ()
346 (when (looking-at "\^A\^A\^A\^A$")
349 (defun nndoc-news-type-p ()
350 (when (looking-at "^Path:.*\n")
353 (defun nndoc-rnews-type-p ()
354 (when (looking-at "#! *rnews")
357 (defun nndoc-rnews-body-end ()
358 (and (re-search-backward nndoc-article-begin nil t)
360 (goto-char (+ (point) (string-to-int (match-string 1))))))
362 (defun nndoc-babyl-type-p ()
363 (when (re-search-forward "\^_\^L *\n" nil t)
366 (defun nndoc-babyl-body-begin ()
367 (re-search-forward "^\n" nil t)
368 (when (looking-at "\*\*\* EOOH \*\*\*")
369 (re-search-forward "^\n" nil t)))
371 (defun nndoc-babyl-head-begin ()
372 (when (re-search-forward "^[0-9].*\n" nil t)
373 (when (looking-at "\*\*\* EOOH \*\*\*")
377 (defun nndoc-forward-type-p ()
378 (when (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
379 (not (re-search-forward "^Subject:.*digest" nil t)))
382 (defun nndoc-clari-briefs-type-p ()
383 (when (let ((case-fold-search nil))
384 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
387 (defun nndoc-transform-clari-briefs (article)
388 (goto-char (point-min))
389 (when (looking-at " *\\*\\(.*\\)\n")
390 (replace-match "" t t))
391 (nndoc-generate-clari-briefs-head article))
393 (defun nndoc-generate-clari-briefs-head (article)
394 (let ((entry (cdr (assq article nndoc-dissection-alist)))
397 (set-buffer nndoc-current-buffer)
399 (narrow-to-region (car entry) (nth 3 entry))
400 (goto-char (point-min))
401 (when (looking-at " *\\*\\(.*\\)$")
402 (setq subject (match-string 1))
403 (when (string-match "[ \t]+$" subject)
404 (setq subject (substring subject 0 (match-beginning 0)))))
406 (let ((case-fold-search nil))
408 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
409 (setq from (match-string 1)))))
410 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
411 "\nSubject: " (or subject "(no subject)") "\n")))
413 (defun nndoc-mime-digest-type-p ()
414 (let ((case-fold-search t)
415 boundary-id b-delimiter entry)
418 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
419 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
422 (setq boundary-id (match-string 1)
423 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
424 (setq entry (assq 'mime-digest nndoc-type-alist))
427 (cons 'head-end "^ ?$")
428 (cons 'body-begin "^ ?\n")
429 (cons 'article-begin b-delimiter)
430 (cons 'body-end-function 'nndoc-digest-body-end)
431 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
434 (defun nndoc-standard-digest-type-p ()
435 (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
437 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
440 (defun nndoc-digest-body-end ()
441 (and (re-search-forward nndoc-article-begin nil t)
442 (goto-char (match-beginning 0))))
444 (defun nndoc-slack-digest-type-p ()
448 ;;; Functions for dissecting the documents
451 (defun nndoc-search (regexp)
453 (re-search-forward regexp nil t)
454 (beginning-of-line)))
456 (defun nndoc-dissect-buffer ()
457 "Go through the document and partition it into heads/bodies/articles."
460 head-begin head-end body-begin body-end)
461 (setq nndoc-dissection-alist nil)
463 (set-buffer nndoc-current-buffer)
464 (goto-char (point-min))
465 ;; Find the beginning of the file.
466 (when nndoc-file-begin
467 (nndoc-search nndoc-file-begin))
468 ;; Go through the file.
469 (while (if (and first nndoc-first-article)
470 (nndoc-search nndoc-first-article)
471 (nndoc-search nndoc-article-begin))
473 (cond (nndoc-head-begin-function
474 (funcall nndoc-head-begin-function))
476 (nndoc-search nndoc-head-begin)))
477 (if (and nndoc-file-end
478 (looking-at nndoc-file-end))
479 (goto-char (point-max))
480 (setq head-begin (point))
481 (nndoc-search (or nndoc-head-end "^$"))
482 (setq head-end (point))
483 (if nndoc-body-begin-function
484 (funcall nndoc-body-begin-function)
485 (nndoc-search (or nndoc-body-begin "^\n")))
486 (setq body-begin (point))
487 (or (and nndoc-body-end-function
488 (funcall nndoc-body-end-function))
490 (nndoc-search nndoc-body-end))
491 (nndoc-search nndoc-article-begin)
493 (goto-char (point-max))
495 (and (re-search-backward nndoc-file-end nil t)
496 (beginning-of-line)))))
497 (setq body-end (point))
498 (push (list (incf i) head-begin head-end body-begin body-end
499 (count-lines body-begin body-end))
500 nndoc-dissection-alist))))))
502 (defun nndoc-unquote-dashes ()
503 "Unquote quoted non-separators in digests."
504 (while (re-search-forward "^- -"nil t)
505 (replace-match "-" t t)))
508 (defun nndoc-add-type (definition &optional position)
509 "Add document DEFINITION to the list of nndoc document definitions.
510 If POSITION is nil or `last', the definition will be added
511 as the last checked definition, if t or `first', add as the
512 first definition, and if any other symbol, add after that
513 symbol in the alist."
514 ;; First remove any old instances.
515 (setq nndoc-type-alist
516 (delq (assq (car definition) nndoc-type-alist)
518 ;; Then enter the new definition in the proper place.
520 ((or (null position) (eq position 'last))
521 (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
522 ((or (eq position t) (eq position 'first))
523 (push definition nndoc-type-alist))
525 (let ((list (memq (assq position nndoc-type-alist)
528 (error "No such position: %s" position))
529 (setcdr list (cons definition (cdr list)))))))
533 ;;; nndoc.el ends here