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 (head-begin . "^[0-9].*\n"))
64 (article-begin . "^-+ Start of forwarded message -+\n+")
65 (body-end . "^-+ End of forwarded message -+\n"))
67 (article-begin . "^ \\*")
68 (body-end . "^\t------*[ \t]^*\n^ \\*")
71 (generate-head . nndoc-generate-clari-briefs-head)
72 (article-transform . nndoc-transform-clari-briefs))
74 (article-begin . "^------------------------------*[\n \t]+")
76 (body-end-function . nndoc-digest-body-end)
78 (file-end . "^End of")
79 (prepare-body . nndoc-prepare-digest-body))
86 (first-article . ,(concat "^" (make-string 70 ?-) "\n\n"))
87 (article-begin . ,(concat "\n\n" (make-string 30 ?-) "\n\n"))
88 (prepare-body . nndoc-prepare-digest-body)
89 (body-end-function . nndoc-digest-body-end)
90 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*"))
92 (guess . nndoc-guess-type))
94 (guess . nndoc-guess-digest-type))
99 (defvar nndoc-file-begin nil)
100 (defvar nndoc-first-article nil)
101 (defvar nndoc-article-end nil)
102 (defvar nndoc-article-begin nil)
103 (defvar nndoc-head-begin nil)
104 (defvar nndoc-head-end nil)
105 (defvar nndoc-file-end nil)
106 (defvar nndoc-body-begin nil)
107 (defvar nndoc-body-end-function nil)
108 (defvar nndoc-body-end nil)
109 (defvar nndoc-dissection-alist nil)
110 (defvar nndoc-prepare-body nil)
111 (defvar nndoc-generate-head nil)
112 (defvar nndoc-article-transform nil)
114 (defvar nndoc-current-server nil)
115 (defvar nndoc-server-alist nil)
116 (defvar nndoc-server-variables
118 (list 'nndoc-article-type nndoc-article-type)
119 '(nndoc-article-begin nil)
120 '(nndoc-article-end nil)
121 '(nndoc-head-begin nil)
122 '(nndoc-head-end nil)
123 '(nndoc-first-article nil)
124 '(nndoc-current-buffer nil)
125 '(nndoc-group-alist nil)
126 '(nndoc-end-of-file nil)
127 '(nndoc-body-begin nil)
128 '(nndoc-address nil)))
130 (defconst nndoc-version "nndoc 1.0"
133 (defvar nndoc-current-buffer nil
134 "Current nndoc news buffer.")
136 (defvar nndoc-address nil)
140 (defvar nndoc-status-string "")
142 (defvar nndoc-group-alist nil)
144 ;;; Interface functions
146 (defun nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
147 (when (nndoc-possibly-change-buffer newsgroup server)
149 (set-buffer nntp-server-buffer)
152 (if (stringp (car articles))
155 (when (setq entry (cdr (assq (setq article (pop articles))
156 nndoc-dissection-alist)))
157 (insert (format "221 %d Article retrieved.\n" article))
158 (if nndoc-generate-head
159 (funcall nndoc-generate-head article)
160 (insert-buffer-substring
161 nndoc-current-buffer (car entry) (nth 1 entry)))
162 (goto-char (point-max))
163 (or (= (char-after (1- (point))) ?\n) (insert "\n"))
164 (insert (format "Lines: %d\n" (nth 4 entry)))
167 (nnheader-fold-continuation-lines)
170 (defun nndoc-open-server (server &optional defs)
171 (nnheader-init-server-buffer)
172 (if (equal server nndoc-current-server)
174 (if nndoc-current-server
175 (setq nndoc-server-alist
176 (cons (list nndoc-current-server
177 (nnheader-save-variables nndoc-server-variables))
178 nndoc-server-alist)))
179 (let ((state (assoc server nndoc-server-alist)))
182 (nnheader-restore-variables (nth 1 state))
183 (setq nndoc-server-alist (delq state nndoc-server-alist)))
184 (nnheader-set-init-variables nndoc-server-variables defs)))
185 (setq nndoc-current-server server)
188 (defun nndoc-close-server (&optional server)
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 (if (not (and nndoc-current-buffer
301 (not nndoc-dissection-alist)))
304 (set-buffer nndoc-current-buffer)
306 (nndoc-dissect-buffer)))
307 ;; Return whether we managed to select a file.
308 nndoc-current-buffer))
310 ;; MIME (RFC 1341) digest hack by Ulrik Dickow <dickow@nbi.dk>.
311 (defun nndoc-guess-digest-type ()
312 "Guess what digest type the current document is."
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]*$"))))
338 ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
340 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
346 (defun nndoc-guess-type ()
347 "Guess what document type is in the current buffer."
348 (goto-char (point-min))
350 ((looking-at rmail-unix-mail-delimiter)
352 ((looking-at "\^A\^A\^A\^A$")
354 ((looking-at "^Path:.*\n")
356 ((looking-at "#! *rnews")
358 ((re-search-forward "\^_\^L *\n" nil t)
361 (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
362 (not (re-search-forward "^Subject:.*digest" nil t))))
364 ((let ((case-fold-search nil))
365 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
370 (defun nndoc-set-delims ()
371 "Set the nndoc delimiter variables according to the type of the document."
372 (let ((vars '(nndoc-file-begin
374 nndoc-article-end nndoc-head-begin nndoc-head-end
375 nndoc-file-end nndoc-article-begin
376 nndoc-body-begin nndoc-body-end-function nndoc-body-end
377 nndoc-prepare-body nndoc-article-transform
378 nndoc-generate-head)))
380 (set (pop vars) nil)))
382 ;; Guess away until we find the real file type.
383 (while (setq defs (cdr (assq nndoc-article-type nndoc-type-alist))
384 guess (assq 'guess defs))
385 (setq nndoc-article-type (funcall (cdr guess))))
386 ;; Set the nndoc variables.
388 (set (intern (format "nndoc-%s" (car (car defs))))
391 (defun nndoc-search (regexp)
393 (re-search-forward regexp nil t)
394 (beginning-of-line)))
396 (defun nndoc-dissect-buffer ()
397 "Go through the document and partition it into heads/bodies/articles."
400 head-begin head-end body-begin body-end)
401 (setq nndoc-dissection-alist nil)
403 (set-buffer nndoc-current-buffer)
404 (goto-char (point-min))
405 ;; Find the beginning of the file.
406 (when nndoc-file-begin
407 (nndoc-search nndoc-file-begin))
408 ;; Go through the file.
409 (while (if (and first nndoc-first-article)
410 (nndoc-search nndoc-first-article)
411 (nndoc-search nndoc-article-begin))
413 (when nndoc-head-begin
414 (nndoc-search nndoc-head-begin))
415 (if (and nndoc-file-end
416 (looking-at nndoc-file-end))
417 (goto-char (point-max))
418 (setq head-begin (point))
419 (nndoc-search (or nndoc-head-end "^$"))
420 (setq head-end (point))
421 (nndoc-search (or nndoc-body-begin "^\n"))
422 (setq body-begin (point))
423 (or (and nndoc-body-end-function
424 (funcall nndoc-body-end-function))
426 (nndoc-search nndoc-body-end))
427 (nndoc-search nndoc-article-begin)
429 (goto-char (point-max))
431 (and (re-search-backward nndoc-file-end nil t)
432 (beginning-of-line)))))
433 (setq body-end (point))
434 (push (list (incf i) head-begin head-end body-begin body-end
435 (count-lines body-begin body-end))
436 nndoc-dissection-alist))))))
438 (defun nndoc-prepare-digest-body ()
439 "Unquote quoted non-separators in digests."
440 (while (re-search-forward "^- -"nil t)
441 (replace-match "-" t t)))
443 (defun nndoc-digest-body-end ()
444 (and (re-search-forward nndoc-article-begin nil t)
445 (goto-char (match-beginning 0))))
447 (defun nndoc-mbox-body-end ()
452 (and (re-search-backward nndoc-article-begin nil t)
454 (search-forward "\n\n" beg t)
456 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
457 (setq len (string-to-int (match-string 1)))
458 (search-forward "\n\n" beg t)
459 (or (= (setq len (+ (point) len)) (point-max))
460 (and (< len (point-max))
462 (looking-at nndoc-article-begin)))))
465 (defun nndoc-rnews-body-end ()
466 (and (re-search-backward nndoc-article-begin nil t)
468 (goto-char (+ (point) (string-to-int (match-string 1))))))
470 (defun nndoc-transform-clari-briefs (article)
471 (goto-char (point-min))
472 (when (looking-at " *\\*\\(.*\\)\n")
473 (replace-match "" t t))
474 (nndoc-generate-clari-briefs-head article))
476 (defun nndoc-generate-clari-briefs-head (article)
477 (let ((entry (cdr (assq article nndoc-dissection-alist)))
480 (set-buffer nndoc-current-buffer)
482 (narrow-to-region (car entry) (nth 3 entry))
483 (goto-char (point-min))
484 (when (looking-at " *\\*\\(.*\\)$")
485 (setq subject (match-string 1)))
487 (let ((case-fold-search nil))
489 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
490 (setq from (match-string 1)))))
491 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
492 "\nSubject: " (or subject "(no subject)") "\n")))
496 ;;; nndoc.el ends here