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))
56 (article-begin . "^From \\([^ \n]*\\(\\|\".*\"[^ \n]*\\)\\) ?\\([^ \n]*\\) *\\([^ ]*\\) *\\([0-9]*\\) *\\([0-9:]*\\) *\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?\\|[-+]?[0-9][0-9][0-9][0-9]\\|\\) * [0-9][0-9]\\([0-9]*\\) *\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?\\|[-+]?[0-9][0-9][0-9][0-9]\\|\\) *\\(remote from .*\\)?\n")
57 (article-begin-function . nndoc-mbox-article-begin)
58 (body-end-function . nndoc-mbox-body-end))
60 (article-begin . "\^_\^L *\n")
62 (body-begin-function . nndoc-babyl-body-begin)
63 (head-begin-function . nndoc-babyl-head-begin))
65 (article-begin . "^-+ Start of forwarded message -+\n+")
66 (body-end . "^-+ End of forwarded message -+$")
67 (prepare-body . nndoc-unquote-dashes))
69 (article-begin . "^ \\*")
70 (body-end . "^\t------*[ \t]^*\n^ \\*")
73 (generate-head . nndoc-generate-clari-briefs-head)
74 (article-transform . nndoc-transform-clari-briefs))
76 (article-begin . "^------------------------------*[\n \t]+")
78 (body-end-function . nndoc-digest-body-end)
80 (file-end . "^End of")
81 (prepare-body . nndoc-unquote-dashes))
88 (first-article . ,(concat "^" (make-string 70 ?-) "\n\n+"))
89 (article-begin . ,(concat "\n\n" (make-string 30 ?-) "\n\n+"))
90 (prepare-body . nndoc-unquote-dashes)
91 (body-end-function . nndoc-digest-body-end)
93 (body-begin . "^ ?\n")
94 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$"))
96 (guess . nndoc-guess-type))
98 (guess . nndoc-guess-digest-type))
103 (defvoo nndoc-file-begin nil)
104 (defvoo nndoc-first-article nil)
105 (defvoo nndoc-article-end nil)
106 (defvoo nndoc-article-begin nil)
107 (defvoo nndoc-article-begin-function nil)
108 (defvoo nndoc-head-begin nil)
109 (defvoo nndoc-head-end nil)
110 (defvoo nndoc-file-end nil)
111 (defvoo nndoc-body-begin nil)
112 (defvoo nndoc-body-end-function nil)
113 (defvoo nndoc-body-begin-function nil)
114 (defvoo nndoc-head-begin-function nil)
115 (defvoo nndoc-body-end nil)
116 (defvoo nndoc-dissection-alist nil)
117 (defvoo nndoc-prepare-body nil)
118 (defvoo nndoc-generate-head nil)
119 (defvoo nndoc-article-transform nil)
121 (defvoo nndoc-status-string "")
122 (defvoo nndoc-group-alist nil)
123 (defvoo nndoc-current-buffer nil
124 "Current nndoc news buffer.")
125 (defvoo nndoc-address nil)
127 (defconst nndoc-version "nndoc 1.0"
132 ;;; Interface functions
134 (nnoo-define-basics nndoc)
136 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
137 (when (nndoc-possibly-change-buffer newsgroup server)
139 (set-buffer nntp-server-buffer)
142 (if (stringp (car articles))
145 (when (setq entry (cdr (assq (setq article (pop articles))
146 nndoc-dissection-alist)))
147 (insert (format "221 %d Article retrieved.\n" article))
148 (if nndoc-generate-head
149 (funcall nndoc-generate-head article)
150 (insert-buffer-substring
151 nndoc-current-buffer (car entry) (nth 1 entry)))
152 (goto-char (point-max))
153 (or (= (char-after (1- (point))) ?\n) (insert "\n"))
154 (insert (format "Lines: %d\n" (nth 4 entry)))
157 (nnheader-fold-continuation-lines)
160 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
161 (nndoc-possibly-change-buffer newsgroup server)
163 (let ((buffer (or buffer nntp-server-buffer))
164 (entry (cdr (assq article nndoc-dissection-alist)))
168 (if (stringp article)
170 (insert-buffer-substring
171 nndoc-current-buffer (car entry) (nth 1 entry))
174 (insert-buffer-substring
175 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
177 (when nndoc-prepare-body
178 (funcall nndoc-prepare-body))
179 (when nndoc-article-transform
180 (funcall nndoc-article-transform article))
183 (deffoo nndoc-request-group (group &optional server dont-check)
187 ((not (nndoc-possibly-change-buffer group server))
188 (nnheader-report 'nndoc "No such file or buffer: %s"
191 (nnheader-report 'nndoc "Selected group %s" group)
193 ((zerop (setq number (length nndoc-dissection-alist)))
194 (nndoc-close-group group)
195 (nnheader-report 'nndoc "No articles in group %s" group))
197 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
199 (deffoo nndoc-request-type (group &optional article)
200 (cond ((not article) 'unknown)
201 (nndoc-post-type nndoc-post-type)
204 (deffoo nndoc-close-group (group &optional server)
205 (nndoc-possibly-change-buffer group server)
206 (and nndoc-current-buffer
207 (buffer-name nndoc-current-buffer)
208 (kill-buffer nndoc-current-buffer))
209 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
211 (setq nndoc-current-buffer nil)
212 (nnoo-close-server 'nndoc server)
213 (setq nndoc-dissection-alist nil)
216 (deffoo nndoc-request-list (&optional server)
219 (deffoo nndoc-request-newgroups (date &optional server)
222 (deffoo nndoc-request-list-newsgroups (&optional server)
226 ;;; Internal functions.
228 (defun nndoc-possibly-change-buffer (group source)
231 ;; The current buffer is this group's buffer.
232 ((and nndoc-current-buffer
233 (buffer-name nndoc-current-buffer)
234 (eq nndoc-current-buffer
235 (setq buf (cdr (assoc group nndoc-group-alist))))))
236 ;; We change buffers by taking an old from the group alist.
237 ;; `source' is either a string (a file name) or a buffer object.
239 (setq nndoc-current-buffer buf))
240 ;; It's a totally new group.
241 ((or (and (bufferp nndoc-address)
242 (buffer-name nndoc-address))
243 (and (stringp nndoc-address)
244 (file-exists-p nndoc-address)
245 (not (file-directory-p nndoc-address))))
246 (push (cons group (setq nndoc-current-buffer
248 (concat " *nndoc " group "*"))))
250 (setq nndoc-dissection-alist nil)
252 (set-buffer nndoc-current-buffer)
253 (buffer-disable-undo (current-buffer))
255 (if (stringp nndoc-address)
256 (insert-file-contents nndoc-address)
257 (insert-buffer-substring nndoc-address)))))
258 ;; Initialize the nndoc structures according to this new document.
259 (when (and nndoc-current-buffer
260 (not nndoc-dissection-alist))
262 (set-buffer nndoc-current-buffer)
264 (nndoc-dissect-buffer)))
265 (unless nndoc-current-buffer
266 (nndoc-close-server))
267 ;; Return whether we managed to select a file.
268 nndoc-current-buffer))
270 ;; MIME (RFC 1341) digest hack by Ulrik Dickow <dickow@nbi.dk>.
271 (defun nndoc-guess-digest-type ()
272 "Guess what digest type the current document is."
273 (let ((case-fold-search t) ; We match a bit too much, keep it simple.
274 boundary-id b-delimiter entry)
275 (goto-char (point-min))
280 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
281 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
284 (setq boundary-id (match-string 1)
285 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
286 (setq entry (assq 'mime-digest nndoc-type-alist))
289 (cons 'head-end "^ ?$")
290 (cons 'body-begin "^ ?\n")
291 (cons 'article-begin b-delimiter)
292 (cons 'body-end-function 'nndoc-digest-body-end)
294 ; (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
295 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
298 ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
300 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
306 (defun nndoc-guess-type ()
307 "Guess what document type is in the current buffer."
308 (goto-char (point-min))
310 ((looking-at message-unix-mail-delimiter)
312 ((looking-at "\^A\^A\^A\^A$")
314 ((looking-at "^Path:.*\n")
316 ((looking-at "#! *rnews")
318 ((re-search-forward "\^_\^L *\n" nil t)
321 (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
322 (not (re-search-forward "^Subject:.*digest" nil t))))
324 ((let ((case-fold-search nil))
325 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
330 (defun nndoc-set-delims ()
331 "Set the nndoc delimiter variables according to the type of the document."
332 (let ((vars '(nndoc-file-begin
334 nndoc-article-end nndoc-head-begin nndoc-head-end
335 nndoc-file-end nndoc-article-begin
336 nndoc-body-begin nndoc-body-end-function nndoc-body-end
337 nndoc-prepare-body nndoc-article-transform
338 nndoc-generate-head nndoc-body-begin-function
339 nndoc-head-begin-function nndoc-article-begin-function)))
341 (set (pop vars) nil)))
343 ;; Guess away until we find the real file type.
344 (while (setq defs (cdr (assq nndoc-article-type nndoc-type-alist))
345 guess (assq 'guess defs))
346 (setq nndoc-article-type (funcall (cdr guess))))
347 ;; Set the nndoc variables.
349 (set (intern (format "nndoc-%s" (caar defs)))
352 (defun nndoc-search (regexp)
354 (re-search-forward regexp nil t)
355 (beginning-of-line)))
357 (defun nndoc-dissect-buffer ()
358 "Go through the document and partition it into heads/bodies/articles."
361 head-begin head-end body-begin body-end)
362 (setq nndoc-dissection-alist nil)
364 (set-buffer nndoc-current-buffer)
365 (goto-char (point-min))
366 ;; Find the beginning of the file.
367 (when nndoc-file-begin
368 (nndoc-search nndoc-file-begin))
369 ;; Go through the file.
370 (while (if (and first nndoc-first-article)
371 (nndoc-search nndoc-first-article)
372 (if nndoc-article-begin-function
373 (funcall nndoc-article-begin-function)
374 (nndoc-search nndoc-article-begin)))
376 (cond (nndoc-head-begin-function
377 (funcall nndoc-head-begin-function))
379 (nndoc-search nndoc-head-begin)))
380 (if (and nndoc-file-end
381 (looking-at nndoc-file-end))
382 (goto-char (point-max))
383 (setq head-begin (point))
384 (nndoc-search (or nndoc-head-end "^$"))
385 (setq head-end (point))
386 (if nndoc-body-begin-function
387 (funcall nndoc-body-begin-function)
388 (nndoc-search (or nndoc-body-begin "^\n")))
389 (setq body-begin (point))
390 (or (and nndoc-body-end-function
391 (funcall nndoc-body-end-function))
393 (nndoc-search nndoc-body-end))
394 (if nndoc-article-begin-function
395 (funcall nndoc-article-begin-function)
396 (nndoc-search nndoc-article-begin))
398 (goto-char (point-max))
400 (and (re-search-backward nndoc-file-end nil t)
401 (beginning-of-line)))))
402 (setq body-end (point))
403 (push (list (incf i) head-begin head-end body-begin body-end
404 (count-lines body-begin body-end))
405 nndoc-dissection-alist))))))
407 (defun nndoc-unquote-dashes ()
408 "Unquote quoted non-separators in digests."
409 (while (re-search-forward "^- -"nil t)
410 (replace-match "-" t t)))
412 (defun nndoc-digest-body-end ()
413 (and (re-search-forward nndoc-article-begin nil t)
414 (goto-char (match-beginning 0))))
416 (defun nndoc-mbox-article-begin ()
417 (when (re-search-forward nndoc-article-begin nil t)
418 (goto-char (match-beginning 0))))
420 (defun nndoc-mbox-body-end ()
425 (and (re-search-backward nndoc-article-begin nil t)
427 (search-forward "\n\n" beg t)
429 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
430 (setq len (string-to-int (match-string 1)))
431 (search-forward "\n\n" beg t)
432 (or (= (setq len (+ (point) len)) (point-max))
433 (and (< len (point-max))
435 (looking-at nndoc-article-begin)))))
438 (defun nndoc-rnews-body-end ()
439 (and (re-search-backward nndoc-article-begin nil t)
441 (goto-char (+ (point) (string-to-int (match-string 1))))))
443 (defun nndoc-transform-clari-briefs (article)
444 (goto-char (point-min))
445 (when (looking-at " *\\*\\(.*\\)\n")
446 (replace-match "" t t))
447 (nndoc-generate-clari-briefs-head article))
449 (defun nndoc-generate-clari-briefs-head (article)
450 (let ((entry (cdr (assq article nndoc-dissection-alist)))
453 (set-buffer nndoc-current-buffer)
455 (narrow-to-region (car entry) (nth 3 entry))
456 (goto-char (point-min))
457 (when (looking-at " *\\*\\(.*\\)$")
458 (setq subject (match-string 1))
459 (when (string-match "[ \t]+$" subject)
460 (setq subject (substring subject 0 (match-beginning 0)))))
462 (let ((case-fold-search nil))
464 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
465 (setq from (match-string 1)))))
466 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
467 "\nSubject: " (or subject "(no subject)") "\n")))
469 (defun nndoc-babyl-body-begin ()
470 (re-search-forward "^\n" nil t)
471 (when (looking-at "\*\*\* EOOH \*\*\*")
472 (re-search-forward "^\n" nil t)))
474 (defun nndoc-babyl-head-begin ()
475 (when (re-search-forward "^[0-9].*\n" nil t)
476 (when (looking-at "\*\*\* EOOH \*\*\*")
482 ;;; nndoc.el ends here