1 ;;; nndoc.el --- single file access for Gnus
2 ;; Copyright (C) 1995 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
32 (defvar nndoc-article-type 'guess
34 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf',
35 `forward', `mime-digest', `standard-digest', `slack-digest', or
38 (defvar nndoc-type-alist
40 (article-begin . "^\^A\^A\^A\^A\n")
41 (body-end . "^\^A\^A\^A\^A\n"))
43 (article-begin . "^Path:"))
45 (article-begin . "^#! *rnews +\\([0-9]\\)+ *\n")
46 (body-end-function . nndoc-rnews-body-end))
49 ,(let ((delim (concat "^" rmail-unix-mail-delimiter)))
50 (if (string-match "\n\\'" delim)
51 (substring delim 0 (match-beginning 0))
53 (body-end-function . nndoc-mbox-body-end))
55 (article-begin . "\^_\^L *\n")
57 (head-begin . "^[0-9].*\n"))
59 (article-begin . "^-+ Start of forwarded message -+\n+")
60 (body-end . "^-+ End of forwarded message -+\n"))
62 (article-begin . "^------------------------------*[\n \t]+")
65 (file-end . "^End of")
66 (prepare-body . nndoc-prepare-digest-body))
73 (first-article . ,(concat "^" (make-string 70 ?-) "\n\n"))
74 (article-begin . ,(concat "\n\n" (make-string 30 ?-) "\n\n"))
75 (prepare-body . nndoc-prepare-digest-body)
76 (body-end-function . nndoc-digest-body-end)
77 (file-end . "^End of .* Digest"))
79 (guess . nndoc-guess-type))
81 (guess . nndoc-guess-digest-type))
86 (defvar nndoc-file-begin nil)
87 (defvar nndoc-first-article nil)
88 (defvar nndoc-article-end nil)
89 (defvar nndoc-article-begin nil)
90 (defvar nndoc-head-begin nil)
91 (defvar nndoc-head-end nil)
92 (defvar nndoc-file-end nil)
93 (defvar nndoc-body-begin nil)
94 (defvar nndoc-body-end-function nil)
95 (defvar nndoc-body-end nil)
96 (defvar nndoc-dissection-alist nil)
97 (defvar nndoc-prepare-body nil)
99 (defvar nndoc-current-server nil)
100 (defvar nndoc-server-alist nil)
101 (defvar nndoc-server-variables
103 (list 'nndoc-article-type nndoc-article-type)
104 '(nndoc-article-begin nil)
105 '(nndoc-article-end nil)
106 '(nndoc-head-begin nil)
107 '(nndoc-head-end nil)
108 '(nndoc-first-article nil)
109 '(nndoc-current-buffer nil)
110 '(nndoc-group-alist nil)
111 '(nndoc-end-of-file nil)
112 '(nndoc-body-begin nil)
113 '(nndoc-address nil)))
115 (defconst nndoc-version "nndoc 1.0"
118 (defvar nndoc-current-buffer nil
119 "Current nndoc news buffer.")
121 (defvar nndoc-address nil)
125 (defvar nndoc-status-string "")
127 (defvar nndoc-group-alist nil)
129 ;;; Interface functions
131 (defun nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
132 (when (nndoc-possibly-change-buffer newsgroup server)
134 (set-buffer nntp-server-buffer)
137 (if (stringp (car articles))
140 (setq entry (cdr (assq (setq article (pop articles))
141 nndoc-dissection-alist)))
142 (insert (format "221 %d Article retrieved.\n" article))
143 (insert-buffer-substring
144 nndoc-current-buffer (car entry) (nth 1 entry))
145 (goto-char (point-max))
146 (or (= (char-after (1- (point))) ?\n) (insert "\n"))
147 (insert (format "Lines: %d\n" (nth 4 entry)))
150 ;; Fold continuation lines.
151 (goto-char (point-min))
152 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
153 (replace-match " " t t))
156 (defun nndoc-open-server (server &optional defs)
157 (nnheader-init-server-buffer)
158 (if (equal server nndoc-current-server)
160 (if nndoc-current-server
161 (setq nndoc-server-alist
162 (cons (list nndoc-current-server
163 (nnheader-save-variables nndoc-server-variables))
164 nndoc-server-alist)))
165 (let ((state (assoc server nndoc-server-alist)))
168 (nnheader-restore-variables (nth 1 state))
169 (setq nndoc-server-alist (delq state nndoc-server-alist)))
170 (nnheader-set-init-variables nndoc-server-variables defs)))
171 (setq nndoc-current-server server)
174 (defun nndoc-close-server (&optional server)
177 (defun nndoc-server-opened (&optional server)
178 (and (equal server nndoc-current-server)
180 (buffer-name nntp-server-buffer)))
182 (defun nndoc-status-message (&optional server)
185 (defun nndoc-request-article (article &optional newsgroup server buffer)
186 (nndoc-possibly-change-buffer newsgroup server)
188 (let ((buffer (or buffer nntp-server-buffer))
189 (entry (cdr (assq article nndoc-dissection-alist)))
193 (if (stringp article)
195 (insert-buffer-substring
196 nndoc-current-buffer (car entry) (nth 1 entry))
199 (insert-buffer-substring
200 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
202 (when nndoc-prepare-body
203 (funcall nndoc-prepare-body))
206 (defun nndoc-request-group (group &optional server dont-check)
209 (if (not (nndoc-possibly-change-buffer group server))
211 (setq nndoc-status-string "No such file or buffer")
216 (set-buffer nntp-server-buffer)
218 (let ((number (length nndoc-dissection-alist)))
221 (nndoc-close-group group)
223 (insert (format "211 %d %d %d %s\n" number 1 number group))
226 (defun nndoc-close-group (group &optional server)
227 (nndoc-possibly-change-buffer group server)
228 (and nndoc-current-buffer
229 (buffer-name nndoc-current-buffer)
230 (kill-buffer nndoc-current-buffer))
231 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
233 (setq nndoc-current-buffer nil)
234 (setq nndoc-current-server nil)
235 (setq nndoc-dissection-alist nil)
238 (defun nndoc-request-list (&optional server)
241 (defun nndoc-request-newgroups (date &optional server)
244 (defun nndoc-request-list-newsgroups (&optional server)
247 (defalias 'nndoc-request-post 'nnmail-request-post)
250 ;;; Internal functions.
252 (defun nndoc-possibly-change-buffer (group source)
255 ;; The current buffer is this group's buffer.
256 ((and nndoc-current-buffer
257 (buffer-name nndoc-current-buffer)
258 (eq nndoc-current-buffer
259 (setq buf (cdr (assoc group nndoc-group-alist))))))
260 ;; We change buffers by taking an old from the group alist.
261 ;; `source' is either a string (a file name) or a buffer object.
263 (setq nndoc-current-buffer buf))
264 ;; It's a totally new group.
265 ((or (and (bufferp nndoc-address)
266 (buffer-name nndoc-address))
267 (and (stringp nndoc-address)
268 (file-exists-p nndoc-address)
269 (not (file-directory-p nndoc-address))))
270 (setq nndoc-group-alist
271 (cons (cons group (setq nndoc-current-buffer
273 (concat " *nndoc " group "*"))))
275 (setq nndoc-dissection-alist nil)
277 (set-buffer nndoc-current-buffer)
278 (buffer-disable-undo (current-buffer))
280 (if (stringp nndoc-address)
281 (insert-file-contents nndoc-address)
282 (insert-buffer-substring nndoc-address)))))
283 (when (and nndoc-current-buffer
284 (not nndoc-dissection-alist))
286 (set-buffer nndoc-current-buffer)
288 (nndoc-dissect-buffer))
291 ;; MIME (RFC 1341) digest hack by Ulrik Dickow <dickow@nbi.dk>.
292 (defun nndoc-guess-digest-type ()
293 (let ((case-fold-search t) ; We match a bit too much, keep it simple.
294 boundary-id b-delimiter entry)
295 (goto-char (point-min))
300 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
301 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
304 (setq boundary-id (match-string 1)
305 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
306 (setq entry (assq 'mime-digest nndoc-type-alist))
309 (cons 'head-end "^ ?$")
310 (cons 'body-begin "^ \n")
311 (cons 'article-begin b-delimiter)
312 (cons 'body-end-function 'nndoc-digest-body-end)
314 ; (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
315 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
317 ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
319 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
325 (defun nndoc-guess-type ()
326 "Guess what document type is in the current buffer."
327 (goto-char (point-min))
329 ((looking-at rmail-unix-mail-delimiter)
331 ((looking-at "\^A\^A\^A\^A$")
333 ((looking-at "^Path:.*\n")
336 (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
337 (not (re-search-forward "^Subject:.*digest" nil t))))
339 ((re-search-forward "\^_\^L *\n" nil t)
341 ((re-search-forward "^Path: .*!" nil t)
346 (defun nndoc-set-delims ()
347 (let ((vars '(nndoc-file-begin
349 nndoc-article-end nndoc-head-begin nndoc-head-end
350 nndoc-file-end nndoc-article-begin
351 nndoc-body-begin nndoc-body-end-function nndoc-body-end
352 nndoc-prepare-body)))
354 (set (pop vars) nil)))
356 ;; Guess away until we find the real file type.
357 (while (setq defs (cdr (assq nndoc-article-type nndoc-type-alist))
358 guess (assq 'guess defs))
359 (setq nndoc-article-type (funcall (cdr guess))))
361 (set (intern (format "nndoc-%s" (car (car defs))))
364 (defun nndoc-search (regexp)
366 (re-search-forward regexp nil t)
367 (beginning-of-line)))
369 (defun nndoc-dissect-buffer ()
372 head-begin head-end body-begin body-end)
373 (setq nndoc-dissection-alist nil)
375 (set-buffer nndoc-current-buffer)
376 (goto-char (point-min))
377 ;; Find the beginning of the file.
378 (when nndoc-file-begin
379 (nndoc-search nndoc-file-begin))
380 ;; Go through the file.
381 (while (if (and first nndoc-first-article)
382 (nndoc-search nndoc-first-article)
383 (nndoc-search nndoc-article-begin))
385 (when nndoc-head-begin
386 (nndoc-search nndoc-head-begin))
387 (setq head-begin (point))
388 (nndoc-search (or nndoc-head-end "^$"))
389 (setq head-end (point))
390 (nndoc-search (or nndoc-body-begin "^\n"))
391 (setq body-begin (point))
392 (or (and nndoc-body-end-function
393 (funcall nndoc-body-end-function))
395 (nndoc-search nndoc-body-end))
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-prepare-digest-body ()
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-body-end ()
421 (and (re-search-backward nndoc-article-begin nil t)
423 (search-forward "\n\n" beg t)
424 (re-search-backward "^Content-Length: \\([0-9]+\\) *$" end t)
425 (setq len (string-to-int (match-string 1)))
426 (search-forward "\n\n" beg t)
427 (or (= (setq len (+ (point) len)) (point-max))
428 (and (< len (point-max))
430 (looking-at nndoc-article-begin)))))
433 (defun nndoc-rnews-body-end ()
435 (and (re-search-backward nndoc-article-begin nil t)
436 (goto-char (+ (point) (string-to-int (match-string 1)))))))
440 ;;; nndoc.el ends here