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 (defvar nndoc-article-type 'guess
35 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
36 `mime-digest', `standard-digest', `slack-digest', `clari-briefs' or
39 (defvar nndoc-post-type 'mail
40 "*Whether the nndoc group is `mail' or `post'.")
42 (defvar nndoc-type-alist
44 (article-begin . "^\^A\^A\^A\^A\n")
45 (body-end . "^\^A\^A\^A\^A\n"))
47 (article-begin . "^Path:"))
49 (article-begin . "^#! *rnews +\\([0-9]\\)+ *\n")
50 (body-end-function . nndoc-rnews-body-end))
53 ,(let ((delim (concat "^" rmail-unix-mail-delimiter)))
54 (if (string-match "\n\\'" delim)
55 (substring delim 0 (match-beginning 0))
57 (body-end-function . nndoc-mbox-body-end))
59 (article-begin . "\^_\^L *\n")
61 (head-begin . "^[0-9].*\n"))
63 (article-begin . "^-+ Start of forwarded message -+\n+")
64 (body-end . "^-+ End of forwarded message -+\n"))
66 (article-begin . "^ \\*")
67 (body-end . "^\t------*[ \t]^*\n^ \\*")
70 (generate-head . nndoc-generate-clari-briefs-head)
71 (article-transform . nndoc-transform-clari-briefs))
73 (article-begin . "^------------------------------*[\n \t]+")
76 (file-end . "^End of")
77 (prepare-body . nndoc-prepare-digest-body))
84 (first-article . ,(concat "^" (make-string 70 ?-) "\n\n"))
85 (article-begin . ,(concat "\n\n" (make-string 30 ?-) "\n\n"))
86 (prepare-body . nndoc-prepare-digest-body)
87 (body-end-function . nndoc-digest-body-end)
88 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*"))
90 (guess . nndoc-guess-type))
92 (guess . nndoc-guess-digest-type))
97 (defvar nndoc-file-begin nil)
98 (defvar nndoc-first-article nil)
99 (defvar nndoc-article-end nil)
100 (defvar nndoc-article-begin nil)
101 (defvar nndoc-head-begin nil)
102 (defvar nndoc-head-end nil)
103 (defvar nndoc-file-end nil)
104 (defvar nndoc-body-begin nil)
105 (defvar nndoc-body-end-function nil)
106 (defvar nndoc-body-end nil)
107 (defvar nndoc-dissection-alist nil)
108 (defvar nndoc-prepare-body nil)
109 (defvar nndoc-generate-head nil)
110 (defvar nndoc-article-transform nil)
112 (defvar nndoc-current-server nil)
113 (defvar nndoc-server-alist nil)
114 (defvar nndoc-server-variables
116 (list 'nndoc-article-type nndoc-article-type)
117 '(nndoc-article-begin nil)
118 '(nndoc-article-end nil)
119 '(nndoc-head-begin nil)
120 '(nndoc-head-end nil)
121 '(nndoc-first-article nil)
122 '(nndoc-current-buffer nil)
123 '(nndoc-group-alist nil)
124 '(nndoc-end-of-file nil)
125 '(nndoc-body-begin nil)
126 '(nndoc-address nil)))
128 (defconst nndoc-version "nndoc 1.0"
131 (defvar nndoc-current-buffer nil
132 "Current nndoc news buffer.")
134 (defvar nndoc-address nil)
138 (defvar nndoc-status-string "")
140 (defvar nndoc-group-alist nil)
142 ;;; Interface functions
144 (defun nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
145 (when (nndoc-possibly-change-buffer newsgroup server)
147 (set-buffer nntp-server-buffer)
150 (if (stringp (car articles))
153 (when (setq entry (cdr (assq (setq article (pop articles))
154 nndoc-dissection-alist)))
155 (insert (format "221 %d Article retrieved.\n" article))
156 (if nndoc-generate-head
157 (funcall nndoc-generate-head article)
158 (insert-buffer-substring
159 nndoc-current-buffer (car entry) (nth 1 entry)))
160 (goto-char (point-max))
161 (or (= (char-after (1- (point))) ?\n) (insert "\n"))
162 (insert (format "Lines: %d\n" (nth 4 entry)))
165 (nnheader-fold-continuation-lines)
168 (defun nndoc-open-server (server &optional defs)
169 (nnheader-init-server-buffer)
170 (if (equal server nndoc-current-server)
172 (if nndoc-current-server
173 (setq nndoc-server-alist
174 (cons (list nndoc-current-server
175 (nnheader-save-variables nndoc-server-variables))
176 nndoc-server-alist)))
177 (let ((state (assoc server nndoc-server-alist)))
180 (nnheader-restore-variables (nth 1 state))
181 (setq nndoc-server-alist (delq state nndoc-server-alist)))
182 (nnheader-set-init-variables nndoc-server-variables defs)))
183 (setq nndoc-current-server server)
186 (defun nndoc-close-server (&optional server)
189 (defun nndoc-server-opened (&optional server)
190 (and (equal server nndoc-current-server)
192 (buffer-name nntp-server-buffer)))
194 (defun nndoc-status-message (&optional server)
197 (defun nndoc-request-article (article &optional newsgroup server buffer)
198 (nndoc-possibly-change-buffer newsgroup server)
200 (let ((buffer (or buffer nntp-server-buffer))
201 (entry (cdr (assq article nndoc-dissection-alist)))
205 (if (stringp article)
207 (insert-buffer-substring
208 nndoc-current-buffer (car entry) (nth 1 entry))
211 (insert-buffer-substring
212 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
214 (when nndoc-prepare-body
215 (funcall nndoc-prepare-body))
216 (when nndoc-article-transform
217 (funcall nndoc-article-transform article))
220 (defun nndoc-request-group (group &optional server dont-check)
225 ((not (nndoc-possibly-change-buffer group server))
226 (nnheader-report 'nndoc "No such file or buffer: %s"
229 (nnheader-report 'nndoc "Selected group %s" group)
231 ((zerop (setq number (length nndoc-dissection-alist)))
232 (nndoc-close-group group)
233 (nnheader-report 'nndoc "No articles in group %s" group))
236 (set-buffer nntp-server-buffer)
238 (insert (format "211 %d %d %d %s\n" number 1 number group))
241 (defun nndoc-request-type (group &optional article)
242 (cond ((not article) 'unknown)
243 (nndoc-post-type nndoc-post-type)
246 (defun nndoc-close-group (group &optional server)
247 (nndoc-possibly-change-buffer group server)
248 (and nndoc-current-buffer
249 (buffer-name nndoc-current-buffer)
250 (kill-buffer nndoc-current-buffer))
251 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
253 (setq nndoc-current-buffer nil)
254 (setq nndoc-current-server nil)
255 (setq nndoc-dissection-alist nil)
258 (defun nndoc-request-list (&optional server)
261 (defun nndoc-request-newgroups (date &optional server)
264 (defun nndoc-request-list-newsgroups (&optional server)
267 (defalias 'nndoc-request-post 'nnmail-request-post)
270 ;;; Internal functions.
272 (defun nndoc-possibly-change-buffer (group source)
275 ;; The current buffer is this group's buffer.
276 ((and nndoc-current-buffer
277 (buffer-name nndoc-current-buffer)
278 (eq nndoc-current-buffer
279 (setq buf (cdr (assoc group nndoc-group-alist))))))
280 ;; We change buffers by taking an old from the group alist.
281 ;; `source' is either a string (a file name) or a buffer object.
283 (setq nndoc-current-buffer buf))
284 ;; It's a totally new group.
285 ((or (and (bufferp nndoc-address)
286 (buffer-name nndoc-address))
287 (and (stringp nndoc-address)
288 (file-exists-p nndoc-address)
289 (not (file-directory-p nndoc-address))))
290 (setq nndoc-group-alist
291 (cons (cons group (setq nndoc-current-buffer
293 (concat " *nndoc " group "*"))))
295 (setq nndoc-dissection-alist nil)
297 (set-buffer nndoc-current-buffer)
298 (buffer-disable-undo (current-buffer))
300 (if (stringp nndoc-address)
301 (insert-file-contents nndoc-address)
302 (insert-buffer-substring nndoc-address)))))
303 (when (and nndoc-current-buffer
304 (not nndoc-dissection-alist))
306 (set-buffer nndoc-current-buffer)
308 (nndoc-dissect-buffer)))
311 ;; MIME (RFC 1341) digest hack by Ulrik Dickow <dickow@nbi.dk>.
312 (defun nndoc-guess-digest-type ()
313 (let ((case-fold-search t) ; We match a bit too much, keep it simple.
314 boundary-id b-delimiter entry)
315 (goto-char (point-min))
320 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
321 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
324 (setq boundary-id (match-string 1)
325 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
326 (setq entry (assq 'mime-digest nndoc-type-alist))
329 (cons 'head-end "^ ?$")
330 (cons 'body-begin "^ ?\n")
331 (cons 'article-begin b-delimiter)
332 (cons 'body-end-function 'nndoc-digest-body-end)
334 ; (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
335 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
337 ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
339 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
345 (defun nndoc-guess-type ()
346 "Guess what document type is in the current buffer."
347 (goto-char (point-min))
349 ((looking-at rmail-unix-mail-delimiter)
351 ((looking-at "\^A\^A\^A\^A$")
353 ((looking-at "^Path:.*\n")
355 ((looking-at "#! *rnews")
357 ((re-search-forward "\^_\^L *\n" nil t)
360 (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
361 (not (re-search-forward "^Subject:.*digest" nil t))))
363 ((let ((case-fold-search nil))
364 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
369 (defun nndoc-set-delims ()
370 (let ((vars '(nndoc-file-begin
372 nndoc-article-end nndoc-head-begin nndoc-head-end
373 nndoc-file-end nndoc-article-begin
374 nndoc-body-begin nndoc-body-end-function nndoc-body-end
375 nndoc-prepare-body nndoc-article-transform
376 nndoc-generate-head)))
378 (set (pop vars) nil)))
380 ;; Guess away until we find the real file type.
381 (while (setq defs (cdr (assq nndoc-article-type nndoc-type-alist))
382 guess (assq 'guess defs))
383 (setq nndoc-article-type (funcall (cdr guess))))
385 (set (intern (format "nndoc-%s" (car (car defs))))
388 (defun nndoc-search (regexp)
390 (re-search-forward regexp nil t)
391 (beginning-of-line)))
393 (defun nndoc-dissect-buffer ()
396 head-begin head-end body-begin body-end)
397 (setq nndoc-dissection-alist nil)
399 (set-buffer nndoc-current-buffer)
400 (goto-char (point-min))
401 ;; Find the beginning of the file.
402 (when nndoc-file-begin
403 (nndoc-search nndoc-file-begin))
404 ;; Go through the file.
405 (while (if (and first nndoc-first-article)
406 (nndoc-search nndoc-first-article)
407 (nndoc-search nndoc-article-begin))
409 (when nndoc-head-begin
410 (nndoc-search nndoc-head-begin))
411 (if (and nndoc-file-end
412 (looking-at nndoc-file-end))
413 (goto-char (point-max))
414 (setq head-begin (point))
415 (nndoc-search (or nndoc-head-end "^$"))
416 (setq head-end (point))
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)
451 (re-search-backward "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
452 (setq len (string-to-int (match-string 1)))
453 (search-forward "\n\n" beg t)
454 (or (= (setq len (+ (point) len)) (point-max))
455 (and (< len (point-max))
457 (looking-at nndoc-article-begin)))))
460 (defun nndoc-rnews-body-end ()
462 (and (re-search-backward nndoc-article-begin nil t)
463 (goto-char (+ (point) (string-to-int (match-string 1)))))))
465 (defun nndoc-transform-clari-briefs (article)
466 (goto-char (point-min))
467 (when (looking-at " *\\*\\(.*\\)\n")
468 (replace-match "" t t))
469 (nndoc-generate-clari-briefs-head article))
471 (defun nndoc-generate-clari-briefs-head (article)
472 (let ((entry (cdr (assq article nndoc-dissection-alist)))
475 (set-buffer nndoc-current-buffer)
477 (narrow-to-region (car entry) (nth 3 entry))
478 (goto-char (point-min))
479 (when (looking-at " *\\*\\(.*\\)$")
480 (setq subject (match-string 1)))
482 (let ((case-fold-search nil))
484 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
485 (setq from (match-string 1)))))
486 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
487 "\nSubject: " (or subject "(no subject)") "\n")))
491 ;;; nndoc.el ends here