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.
32 (eval-when-compile (require 'cl))
34 (defvar nndoc-article-type 'guess
36 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
37 `mime-digest', `standard-digest', `slack-digest', `clari-briefs' or
40 (defvar nndoc-post-type 'mail
41 "*Whether the nndoc group is `mail' or `post'.")
43 (defvar nndoc-type-alist
45 (article-begin . "^\^A\^A\^A\^A\n")
46 (body-end . "^\^A\^A\^A\^A\n"))
48 (article-begin . "^Path:"))
50 (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
51 (body-end-function . nndoc-rnews-body-end))
54 ,(let ((delim (concat "^" rmail-unix-mail-delimiter)))
55 (if (string-match "\n\\'" delim)
56 (substring delim 0 (match-beginning 0))
58 (body-end-function . nndoc-mbox-body-end))
60 (article-begin . "\^_\^L *\n")
62 (body-begin-function . nndoc-babyl-body-begin)
63 (head-begin . "^[0-9].*\n"))
65 (article-begin . "^-+ Start of forwarded message -+\n+")
66 (body-end . "^-+ End of forwarded message -+\n"))
68 (article-begin . "^ \\*")
69 (body-end . "^\t------*[ \t]^*\n^ \\*")
72 (generate-head . nndoc-generate-clari-briefs-head)
73 (article-transform . nndoc-transform-clari-briefs))
75 (article-begin . "^------------------------------*[\n \t]+")
77 (body-end-function . nndoc-digest-body-end)
79 (file-end . "^End of")
80 (prepare-body . nndoc-prepare-digest-body))
87 (first-article . ,(concat "^" (make-string 70 ?-) "\n\n+"))
88 (article-begin . ,(concat "\n\n" (make-string 30 ?-) "\n\n+"))
89 (prepare-body . nndoc-prepare-digest-body)
90 (body-end-function . nndoc-digest-body-end)
91 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$"))
93 (guess . nndoc-guess-type))
95 (guess . nndoc-guess-digest-type))
100 (defvar nndoc-file-begin nil)
101 (defvar nndoc-first-article nil)
102 (defvar nndoc-article-end nil)
103 (defvar nndoc-article-begin nil)
104 (defvar nndoc-head-begin nil)
105 (defvar nndoc-head-end nil)
106 (defvar nndoc-file-end nil)
107 (defvar nndoc-body-begin nil)
108 (defvar nndoc-body-end-function nil)
109 (defvar nndoc-body-begin-function nil)
110 (defvar nndoc-body-end nil)
111 (defvar nndoc-dissection-alist nil)
112 (defvar nndoc-prepare-body nil)
113 (defvar nndoc-generate-head nil)
114 (defvar nndoc-article-transform nil)
116 (defvar nndoc-current-server nil)
117 (defvar nndoc-server-alist nil)
118 (defvar nndoc-server-variables
119 `((nndoc-article-type ,nndoc-article-type)
120 (nndoc-article-begin nil)
121 (nndoc-article-end nil)
122 (nndoc-head-begin nil)
124 (nndoc-first-article nil)
125 (nndoc-current-buffer nil)
126 (nndoc-group-alist nil)
127 (nndoc-end-of-file nil)
128 (nndoc-body-begin nil)
129 (nndoc-dissection-alist nil)
130 (nndoc-generate-head nil)
131 (nndoc-article-transform nil)
132 (nndoc-body-end-function nil)
133 (nndoc-prepare-body nil)
134 (nndoc-body-begin-function nil)
135 (nndoc-address nil)))
137 (defconst nndoc-version "nndoc 1.0"
140 (defvar nndoc-current-buffer nil
141 "Current nndoc news buffer.")
143 (defvar nndoc-address nil)
147 (defvar nndoc-status-string "")
149 (defvar nndoc-group-alist nil)
151 ;;; Interface functions
153 (defun nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
154 (when (nndoc-possibly-change-buffer newsgroup server)
156 (set-buffer nntp-server-buffer)
159 (if (stringp (car articles))
162 (when (setq entry (cdr (assq (setq article (pop articles))
163 nndoc-dissection-alist)))
164 (insert (format "221 %d Article retrieved.\n" article))
165 (if nndoc-generate-head
166 (funcall nndoc-generate-head article)
167 (insert-buffer-substring
168 nndoc-current-buffer (car entry) (nth 1 entry)))
169 (goto-char (point-max))
170 (or (= (char-after (1- (point))) ?\n) (insert "\n"))
171 (insert (format "Lines: %d\n" (nth 4 entry)))
174 (nnheader-fold-continuation-lines)
177 (defun nndoc-open-server (server &optional defs)
178 (nnheader-change-server 'nndoc server defs))
180 (defun nndoc-close-server (&optional server)
181 (setq nndoc-current-server nil)
184 (defun nndoc-server-opened (&optional server)
185 (and (equal server nndoc-current-server)
187 (buffer-name nntp-server-buffer)))
189 (defun nndoc-status-message (&optional server)
192 (defun nndoc-request-article (article &optional newsgroup server buffer)
193 (nndoc-possibly-change-buffer newsgroup server)
195 (let ((buffer (or buffer nntp-server-buffer))
196 (entry (cdr (assq article nndoc-dissection-alist)))
200 (if (stringp article)
202 (insert-buffer-substring
203 nndoc-current-buffer (car entry) (nth 1 entry))
206 (insert-buffer-substring
207 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
209 (when nndoc-prepare-body
210 (funcall nndoc-prepare-body))
211 (when nndoc-article-transform
212 (funcall nndoc-article-transform article))
215 (defun nndoc-request-group (group &optional server dont-check)
219 ((not (nndoc-possibly-change-buffer group server))
220 (nnheader-report 'nndoc "No such file or buffer: %s"
223 (nnheader-report 'nndoc "Selected group %s" group)
225 ((zerop (setq number (length nndoc-dissection-alist)))
226 (nndoc-close-group group)
227 (nnheader-report 'nndoc "No articles in group %s" group))
229 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
231 (defun nndoc-request-type (group &optional article)
232 (cond ((not article) 'unknown)
233 (nndoc-post-type nndoc-post-type)
236 (defun nndoc-close-group (group &optional server)
237 (nndoc-possibly-change-buffer group server)
238 (and nndoc-current-buffer
239 (buffer-name nndoc-current-buffer)
240 (kill-buffer nndoc-current-buffer))
241 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
243 (setq nndoc-current-buffer nil)
244 (setq nndoc-current-server nil)
245 (setq nndoc-dissection-alist nil)
248 (defun nndoc-request-list (&optional server)
251 (defun nndoc-request-newgroups (date &optional server)
254 (defun nndoc-request-list-newsgroups (&optional server)
257 (defalias 'nndoc-request-post 'nnmail-request-post)
260 ;;; Internal functions.
262 (defun nndoc-possibly-change-buffer (group source)
265 ;; The current buffer is this group's buffer.
266 ((and nndoc-current-buffer
267 (buffer-name nndoc-current-buffer)
268 (eq nndoc-current-buffer
269 (setq buf (cdr (assoc group nndoc-group-alist))))))
270 ;; We change buffers by taking an old from the group alist.
271 ;; `source' is either a string (a file name) or a buffer object.
273 (setq nndoc-current-buffer buf))
274 ;; It's a totally new group.
275 ((or (and (bufferp nndoc-address)
276 (buffer-name nndoc-address))
277 (and (stringp nndoc-address)
278 (file-exists-p nndoc-address)
279 (not (file-directory-p nndoc-address))))
280 (push (cons group (setq nndoc-current-buffer
282 (concat " *nndoc " group "*"))))
284 (setq nndoc-dissection-alist nil)
286 (set-buffer nndoc-current-buffer)
287 (buffer-disable-undo (current-buffer))
289 (if (stringp nndoc-address)
290 (insert-file-contents nndoc-address)
291 (insert-buffer-substring nndoc-address)))))
292 ;; Initialize the nndoc structures according to this new document.
293 (when (and nndoc-current-buffer
294 (not nndoc-dissection-alist))
296 (set-buffer nndoc-current-buffer)
298 (nndoc-dissect-buffer)))
299 (unless nndoc-current-buffer
300 (nndoc-close-server))
301 ;; Return whether we managed to select a file.
302 nndoc-current-buffer))
304 ;; MIME (RFC 1341) digest hack by Ulrik Dickow <dickow@nbi.dk>.
305 (defun nndoc-guess-digest-type ()
306 "Guess what digest type the current document is."
307 (let ((case-fold-search t) ; We match a bit too much, keep it simple.
308 boundary-id b-delimiter entry)
309 (goto-char (point-min))
314 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
315 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
318 (setq boundary-id (match-string 1)
319 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
320 (setq entry (assq 'mime-digest nndoc-type-alist))
323 (cons 'head-end "^ ?$")
324 (cons 'body-begin "^ ?\n")
325 (cons 'article-begin b-delimiter)
326 (cons 'body-end-function 'nndoc-digest-body-end)
328 ; (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
329 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
332 ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
334 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
340 (defun nndoc-guess-type ()
341 "Guess what document type is in the current buffer."
342 (goto-char (point-min))
344 ((looking-at rmail-unix-mail-delimiter)
346 ((looking-at "\^A\^A\^A\^A$")
348 ((looking-at "^Path:.*\n")
350 ((looking-at "#! *rnews")
352 ((re-search-forward "\^_\^L *\n" nil t)
355 (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
356 (not (re-search-forward "^Subject:.*digest" nil t))))
358 ((let ((case-fold-search nil))
359 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
364 (defun nndoc-set-delims ()
365 "Set the nndoc delimiter variables according to the type of the document."
366 (let ((vars '(nndoc-file-begin
368 nndoc-article-end nndoc-head-begin nndoc-head-end
369 nndoc-file-end nndoc-article-begin
370 nndoc-body-begin nndoc-body-end-function nndoc-body-end
371 nndoc-prepare-body nndoc-article-transform
372 nndoc-generate-head nndoc-body-begin-function)))
374 (set (pop vars) nil)))
376 ;; Guess away until we find the real file type.
377 (while (setq defs (cdr (assq nndoc-article-type nndoc-type-alist))
378 guess (assq 'guess defs))
379 (setq nndoc-article-type (funcall (cdr guess))))
380 ;; Set the nndoc variables.
382 (set (intern (format "nndoc-%s" (caar defs)))
385 (defun nndoc-search (regexp)
387 (re-search-forward regexp nil t)
388 (beginning-of-line)))
390 (defun nndoc-dissect-buffer ()
391 "Go through the document and partition it into heads/bodies/articles."
394 head-begin head-end body-begin body-end)
395 (setq nndoc-dissection-alist nil)
397 (set-buffer nndoc-current-buffer)
398 (goto-char (point-min))
399 ;; Find the beginning of the file.
400 (when nndoc-file-begin
401 (nndoc-search nndoc-file-begin))
402 ;; Go through the file.
403 (while (if (and first nndoc-first-article)
404 (nndoc-search nndoc-first-article)
405 (nndoc-search nndoc-article-begin))
407 (when nndoc-head-begin
408 (nndoc-search nndoc-head-begin))
409 (if (and nndoc-file-end
410 (looking-at nndoc-file-end))
411 (goto-char (point-max))
412 (setq head-begin (point))
413 (nndoc-search (or nndoc-head-end "^$"))
414 (setq head-end (point))
415 (if nndoc-body-begin-function
416 (funcall nndoc-body-begin-function)
417 (nndoc-search (or nndoc-body-begin "^\n")))
418 (setq body-begin (point))
419 (or (and nndoc-body-end-function
420 (funcall nndoc-body-end-function))
422 (nndoc-search nndoc-body-end))
423 (nndoc-search nndoc-article-begin)
425 (goto-char (point-max))
427 (and (re-search-backward nndoc-file-end nil t)
428 (beginning-of-line)))))
429 (setq body-end (point))
430 (push (list (incf i) head-begin head-end body-begin body-end
431 (count-lines body-begin body-end))
432 nndoc-dissection-alist))))))
434 (defun nndoc-prepare-digest-body ()
435 "Unquote quoted non-separators in digests."
436 (while (re-search-forward "^- -"nil t)
437 (replace-match "-" t t)))
439 (defun nndoc-digest-body-end ()
440 (and (re-search-forward nndoc-article-begin nil t)
441 (goto-char (match-beginning 0))))
443 (defun nndoc-mbox-body-end ()
448 (and (re-search-backward nndoc-article-begin nil t)
450 (search-forward "\n\n" beg t)
452 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
453 (setq len (string-to-int (match-string 1)))
454 (search-forward "\n\n" beg t)
455 (or (= (setq len (+ (point) len)) (point-max))
456 (and (< len (point-max))
458 (looking-at nndoc-article-begin)))))
461 (defun nndoc-rnews-body-end ()
462 (and (re-search-backward nndoc-article-begin nil t)
464 (goto-char (+ (point) (string-to-int (match-string 1))))))
466 (defun nndoc-transform-clari-briefs (article)
467 (goto-char (point-min))
468 (when (looking-at " *\\*\\(.*\\)\n")
469 (replace-match "" t t))
470 (nndoc-generate-clari-briefs-head article))
472 (defun nndoc-generate-clari-briefs-head (article)
473 (let ((entry (cdr (assq article nndoc-dissection-alist)))
476 (set-buffer nndoc-current-buffer)
478 (narrow-to-region (car entry) (nth 3 entry))
479 (goto-char (point-min))
480 (when (looking-at " *\\*\\(.*\\)$")
481 (setq subject (match-string 1))
482 (when (string-match "[ \t]+$" subject)
483 (setq subject (substring subject 0 (match-beginning 0)))))
485 (let ((case-fold-search nil))
487 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
488 (setq from (match-string 1)))))
489 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
490 "\nSubject: " (or subject "(no subject)") "\n")))
492 (defun nndoc-babyl-body-begin ()
493 (re-search-forward "^\n" nil t)
494 (when (looking-at "\*\*\* EOOH \*\*\*")
495 (re-search-forward "^\n" nil t)))
499 ;;; nndoc.el ends here