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)
92 (body-begin . "^ ?\n")
93 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$"))
95 (guess . nndoc-guess-type))
97 (guess . nndoc-guess-digest-type))
102 (defvar nndoc-file-begin nil)
103 (defvar nndoc-first-article nil)
104 (defvar nndoc-article-end nil)
105 (defvar nndoc-article-begin nil)
106 (defvar nndoc-head-begin nil)
107 (defvar nndoc-head-end nil)
108 (defvar nndoc-file-end nil)
109 (defvar nndoc-body-begin nil)
110 (defvar nndoc-body-end-function nil)
111 (defvar nndoc-body-begin-function nil)
112 (defvar nndoc-body-end nil)
113 (defvar nndoc-dissection-alist nil)
114 (defvar nndoc-prepare-body nil)
115 (defvar nndoc-generate-head nil)
116 (defvar nndoc-article-transform nil)
118 (defvar nndoc-status-string "")
119 (defvar nndoc-group-alist nil)
120 (defvar nndoc-current-buffer nil
121 "Current nndoc news buffer.")
122 (defvar nndoc-address nil)
126 (defvar nndoc-current-server nil)
127 (defvar nndoc-server-alist nil)
128 (defvar nndoc-server-variables
129 `((nndoc-article-type ,nndoc-article-type)
130 (nndoc-article-begin nil)
131 (nndoc-article-end nil)
132 (nndoc-head-begin nil)
134 (nndoc-first-article nil)
135 (nndoc-current-buffer nil)
136 (nndoc-group-alist nil)
137 (nndoc-end-of-file nil)
138 (nndoc-body-begin nil)
139 (nndoc-dissection-alist nil)
140 (nndoc-generate-head nil)
141 (nndoc-article-transform nil)
142 (nndoc-body-end-function nil)
143 (nndoc-prepare-body nil)
144 (nndoc-body-begin-function nil)
145 (nndoc-status-string ,nndoc-status-string)
146 (nndoc-body-end ,nndoc-body-end)
147 (nndoc-file-end ,nndoc-file-end)
148 (nndoc-file-begin ,nndoc-file-begin)
149 (nndoc-type-alist ,nndoc-type-alist)
150 (nndoc-post-type ,nndoc-post-type)
151 (nndoc-address nil)))
153 (defconst nndoc-version "nndoc 1.0"
158 ;;; Interface functions
160 (defun nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
161 (when (nndoc-possibly-change-buffer newsgroup server)
163 (set-buffer nntp-server-buffer)
166 (if (stringp (car articles))
169 (when (setq entry (cdr (assq (setq article (pop articles))
170 nndoc-dissection-alist)))
171 (insert (format "221 %d Article retrieved.\n" article))
172 (if nndoc-generate-head
173 (funcall nndoc-generate-head article)
174 (insert-buffer-substring
175 nndoc-current-buffer (car entry) (nth 1 entry)))
176 (goto-char (point-max))
177 (or (= (char-after (1- (point))) ?\n) (insert "\n"))
178 (insert (format "Lines: %d\n" (nth 4 entry)))
181 (nnheader-fold-continuation-lines)
184 (defun nndoc-open-server (server &optional defs)
185 (nnheader-change-server 'nndoc server defs))
187 (defun nndoc-close-server (&optional server)
188 (setq nndoc-current-server nil)
191 (defun nndoc-server-opened (&optional server)
192 (and (equal server nndoc-current-server)
194 (buffer-name nntp-server-buffer)))
196 (defun nndoc-status-message (&optional server)
199 (defun nndoc-request-article (article &optional newsgroup server buffer)
200 (nndoc-possibly-change-buffer newsgroup server)
202 (let ((buffer (or buffer nntp-server-buffer))
203 (entry (cdr (assq article nndoc-dissection-alist)))
207 (if (stringp article)
209 (insert-buffer-substring
210 nndoc-current-buffer (car entry) (nth 1 entry))
213 (insert-buffer-substring
214 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
216 (when nndoc-prepare-body
217 (funcall nndoc-prepare-body))
218 (when nndoc-article-transform
219 (funcall nndoc-article-transform article))
222 (defun nndoc-request-group (group &optional server dont-check)
226 ((not (nndoc-possibly-change-buffer group server))
227 (nnheader-report 'nndoc "No such file or buffer: %s"
230 (nnheader-report 'nndoc "Selected group %s" group)
232 ((zerop (setq number (length nndoc-dissection-alist)))
233 (nndoc-close-group group)
234 (nnheader-report 'nndoc "No articles in group %s" group))
236 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
238 (defun nndoc-request-type (group &optional article)
239 (cond ((not article) 'unknown)
240 (nndoc-post-type nndoc-post-type)
243 (defun nndoc-close-group (group &optional server)
244 (nndoc-possibly-change-buffer group server)
245 (and nndoc-current-buffer
246 (buffer-name nndoc-current-buffer)
247 (kill-buffer nndoc-current-buffer))
248 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
250 (setq nndoc-current-buffer nil)
251 (setq nndoc-current-server nil)
252 (setq nndoc-dissection-alist nil)
255 (defun nndoc-request-list (&optional server)
258 (defun nndoc-request-newgroups (date &optional server)
261 (defun nndoc-request-list-newsgroups (&optional server)
264 (defalias 'nndoc-request-post 'nnmail-request-post)
267 ;;; Internal functions.
269 (defun nndoc-possibly-change-buffer (group source)
272 ;; The current buffer is this group's buffer.
273 ((and nndoc-current-buffer
274 (buffer-name nndoc-current-buffer)
275 (eq nndoc-current-buffer
276 (setq buf (cdr (assoc group nndoc-group-alist))))))
277 ;; We change buffers by taking an old from the group alist.
278 ;; `source' is either a string (a file name) or a buffer object.
280 (setq nndoc-current-buffer buf))
281 ;; It's a totally new group.
282 ((or (and (bufferp nndoc-address)
283 (buffer-name nndoc-address))
284 (and (stringp nndoc-address)
285 (file-exists-p nndoc-address)
286 (not (file-directory-p nndoc-address))))
287 (push (cons group (setq nndoc-current-buffer
289 (concat " *nndoc " group "*"))))
291 (setq nndoc-dissection-alist nil)
293 (set-buffer nndoc-current-buffer)
294 (buffer-disable-undo (current-buffer))
296 (if (stringp nndoc-address)
297 (insert-file-contents nndoc-address)
298 (insert-buffer-substring nndoc-address)))))
299 ;; Initialize the nndoc structures according to this new document.
300 (when (and nndoc-current-buffer
301 (not nndoc-dissection-alist))
303 (set-buffer nndoc-current-buffer)
305 (nndoc-dissect-buffer)))
306 (unless nndoc-current-buffer
307 (nndoc-close-server))
308 ;; Return whether we managed to select a file.
309 nndoc-current-buffer))
311 ;; MIME (RFC 1341) digest hack by Ulrik Dickow <dickow@nbi.dk>.
312 (defun nndoc-guess-digest-type ()
313 "Guess what digest type the current document is."
314 (let ((case-fold-search t) ; We match a bit too much, keep it simple.
315 boundary-id b-delimiter entry)
316 (goto-char (point-min))
321 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
322 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
325 (setq boundary-id (match-string 1)
326 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
327 (setq entry (assq 'mime-digest nndoc-type-alist))
330 (cons 'head-end "^ ?$")
331 (cons 'body-begin "^ ?\n")
332 (cons 'article-begin b-delimiter)
333 (cons 'body-end-function 'nndoc-digest-body-end)
335 ; (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
336 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
339 ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
341 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
347 (defun nndoc-guess-type ()
348 "Guess what document type is in the current buffer."
349 (goto-char (point-min))
351 ((looking-at rmail-unix-mail-delimiter)
353 ((looking-at "\^A\^A\^A\^A$")
355 ((looking-at "^Path:.*\n")
357 ((looking-at "#! *rnews")
359 ((re-search-forward "\^_\^L *\n" nil t)
362 (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
363 (not (re-search-forward "^Subject:.*digest" nil t))))
365 ((let ((case-fold-search nil))
366 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
371 (defun nndoc-set-delims ()
372 "Set the nndoc delimiter variables according to the type of the document."
373 (let ((vars '(nndoc-file-begin
375 nndoc-article-end nndoc-head-begin nndoc-head-end
376 nndoc-file-end nndoc-article-begin
377 nndoc-body-begin nndoc-body-end-function nndoc-body-end
378 nndoc-prepare-body nndoc-article-transform
379 nndoc-generate-head nndoc-body-begin-function)))
381 (set (pop vars) nil)))
383 ;; Guess away until we find the real file type.
384 (while (setq defs (cdr (assq nndoc-article-type nndoc-type-alist))
385 guess (assq 'guess defs))
386 (setq nndoc-article-type (funcall (cdr guess))))
387 ;; Set the nndoc variables.
389 (set (intern (format "nndoc-%s" (caar defs)))
392 (defun nndoc-search (regexp)
394 (re-search-forward regexp nil t)
395 (beginning-of-line)))
397 (defun nndoc-dissect-buffer ()
398 "Go through the document and partition it into heads/bodies/articles."
401 head-begin head-end body-begin body-end)
402 (setq nndoc-dissection-alist nil)
404 (set-buffer nndoc-current-buffer)
405 (goto-char (point-min))
406 ;; Find the beginning of the file.
407 (when nndoc-file-begin
408 (nndoc-search nndoc-file-begin))
409 ;; Go through the file.
410 (while (if (and first nndoc-first-article)
411 (nndoc-search nndoc-first-article)
412 (nndoc-search nndoc-article-begin))
414 (when nndoc-head-begin
415 (nndoc-search nndoc-head-begin))
416 (if (and nndoc-file-end
417 (looking-at nndoc-file-end))
418 (goto-char (point-max))
419 (setq head-begin (point))
420 (nndoc-search (or nndoc-head-end "^$"))
421 (setq head-end (point))
422 (if nndoc-body-begin-function
423 (funcall nndoc-body-begin-function)
424 (nndoc-search (or nndoc-body-begin "^\n")))
425 (setq body-begin (point))
426 (or (and nndoc-body-end-function
427 (funcall nndoc-body-end-function))
429 (nndoc-search nndoc-body-end))
430 (nndoc-search nndoc-article-begin)
432 (goto-char (point-max))
434 (and (re-search-backward nndoc-file-end nil t)
435 (beginning-of-line)))))
436 (setq body-end (point))
437 (push (list (incf i) head-begin head-end body-begin body-end
438 (count-lines body-begin body-end))
439 nndoc-dissection-alist))))))
441 (defun nndoc-prepare-digest-body ()
442 "Unquote quoted non-separators in digests."
443 (while (re-search-forward "^- -"nil t)
444 (replace-match "-" t t)))
446 (defun nndoc-digest-body-end ()
447 (and (re-search-forward nndoc-article-begin nil t)
448 (goto-char (match-beginning 0))))
450 (defun nndoc-mbox-body-end ()
455 (and (re-search-backward nndoc-article-begin nil t)
457 (search-forward "\n\n" beg t)
459 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
460 (setq len (string-to-int (match-string 1)))
461 (search-forward "\n\n" beg t)
462 (or (= (setq len (+ (point) len)) (point-max))
463 (and (< len (point-max))
465 (looking-at nndoc-article-begin)))))
468 (defun nndoc-rnews-body-end ()
469 (and (re-search-backward nndoc-article-begin nil t)
471 (goto-char (+ (point) (string-to-int (match-string 1))))))
473 (defun nndoc-transform-clari-briefs (article)
474 (goto-char (point-min))
475 (when (looking-at " *\\*\\(.*\\)\n")
476 (replace-match "" t t))
477 (nndoc-generate-clari-briefs-head article))
479 (defun nndoc-generate-clari-briefs-head (article)
480 (let ((entry (cdr (assq article nndoc-dissection-alist)))
483 (set-buffer nndoc-current-buffer)
485 (narrow-to-region (car entry) (nth 3 entry))
486 (goto-char (point-min))
487 (when (looking-at " *\\*\\(.*\\)$")
488 (setq subject (match-string 1))
489 (when (string-match "[ \t]+$" subject)
490 (setq subject (substring subject 0 (match-beginning 0)))))
492 (let ((case-fold-search nil))
494 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
495 (setq from (match-string 1)))))
496 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
497 "\nSubject: " (or subject "(no subject)") "\n")))
499 (defun nndoc-babyl-body-begin ()
500 (re-search-forward "^\n" nil t)
501 (when (looking-at "\*\*\* EOOH \*\*\*")
502 (re-search-forward "^\n" nil t)))
506 ;;; nndoc.el ends here