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 (eval-when-compile (require 'cl))
37 (defvoo nndoc-article-type 'guess
39 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
40 `mime-digest', `standard-digest', `slack-digest', `clari-briefs' or
43 (defvoo nndoc-post-type 'mail
44 "*Whether the nndoc group is `mail' or `post'.")
46 (defvar nndoc-type-alist
48 (article-begin . "^\^A\^A\^A\^A\n")
49 (body-end . "^\^A\^A\^A\^A\n"))
51 (article-begin . "^Path:"))
53 (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
54 (body-end-function . nndoc-rnews-body-end))
57 ,(let ((delim (concat "^" rmail-unix-mail-delimiter)))
58 (if (string-match "\n\\'" delim)
59 (substring delim 0 (match-beginning 0))
61 (body-end-function . nndoc-mbox-body-end))
63 (article-begin . "\^_\^L *\n")
65 (body-begin-function . nndoc-babyl-body-begin)
66 (head-begin . "^[0-9].*\n"))
68 (article-begin . "^-+ Start of forwarded message -+\n+")
69 (body-end . "^-+ End of forwarded message -+$")
70 (prepare-body . nndoc-unquote-dashes))
72 (article-begin . "^ \\*")
73 (body-end . "^\t------*[ \t]^*\n^ \\*")
76 (generate-head . nndoc-generate-clari-briefs-head)
77 (article-transform . nndoc-transform-clari-briefs))
79 (article-begin . "^------------------------------*[\n \t]+")
81 (body-end-function . nndoc-digest-body-end)
83 (file-end . "^End of")
84 (prepare-body . nndoc-unquote-dashes))
91 (first-article . ,(concat "^" (make-string 70 ?-) "\n\n+"))
92 (article-begin . ,(concat "\n\n" (make-string 30 ?-) "\n\n+"))
93 (prepare-body . nndoc-unquote-dashes)
94 (body-end-function . nndoc-digest-body-end)
96 (body-begin . "^ ?\n")
97 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$"))
99 (guess . nndoc-guess-type))
101 (guess . nndoc-guess-digest-type))
106 (defvoo nndoc-file-begin nil)
107 (defvoo nndoc-first-article nil)
108 (defvoo nndoc-article-end nil)
109 (defvoo nndoc-article-begin nil)
110 (defvoo nndoc-head-begin nil)
111 (defvoo nndoc-head-end nil)
112 (defvoo nndoc-file-end nil)
113 (defvoo nndoc-body-begin nil)
114 (defvoo nndoc-body-end-function nil)
115 (defvoo nndoc-body-begin-function nil)
116 (defvoo nndoc-body-end nil)
117 (defvoo nndoc-dissection-alist nil)
118 (defvoo nndoc-prepare-body nil)
119 (defvoo nndoc-generate-head nil)
120 (defvoo nndoc-article-transform nil)
122 (defvoo nndoc-status-string "")
123 (defvoo nndoc-group-alist nil)
124 (defvoo nndoc-current-buffer nil
125 "Current nndoc news buffer.")
126 (defvoo nndoc-address nil)
128 (defconst nndoc-version "nndoc 1.0"
133 ;;; Interface functions
135 (nnoo-define-basics nndoc)
137 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
138 (when (nndoc-possibly-change-buffer newsgroup server)
140 (set-buffer nntp-server-buffer)
143 (if (stringp (car articles))
146 (when (setq entry (cdr (assq (setq article (pop articles))
147 nndoc-dissection-alist)))
148 (insert (format "221 %d Article retrieved.\n" article))
149 (if nndoc-generate-head
150 (funcall nndoc-generate-head article)
151 (insert-buffer-substring
152 nndoc-current-buffer (car entry) (nth 1 entry)))
153 (goto-char (point-max))
154 (or (= (char-after (1- (point))) ?\n) (insert "\n"))
155 (insert (format "Lines: %d\n" (nth 4 entry)))
158 (nnheader-fold-continuation-lines)
161 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
162 (nndoc-possibly-change-buffer newsgroup server)
164 (let ((buffer (or buffer nntp-server-buffer))
165 (entry (cdr (assq article nndoc-dissection-alist)))
169 (if (stringp article)
171 (insert-buffer-substring
172 nndoc-current-buffer (car entry) (nth 1 entry))
175 (insert-buffer-substring
176 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
178 (when nndoc-prepare-body
179 (funcall nndoc-prepare-body))
180 (when nndoc-article-transform
181 (funcall nndoc-article-transform article))
184 (deffoo nndoc-request-group (group &optional server dont-check)
188 ((not (nndoc-possibly-change-buffer group server))
189 (nnheader-report 'nndoc "No such file or buffer: %s"
192 (nnheader-report 'nndoc "Selected group %s" group)
194 ((zerop (setq number (length nndoc-dissection-alist)))
195 (nndoc-close-group group)
196 (nnheader-report 'nndoc "No articles in group %s" group))
198 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
200 (deffoo nndoc-request-type (group &optional article)
201 (cond ((not article) 'unknown)
202 (nndoc-post-type nndoc-post-type)
205 (deffoo nndoc-close-group (group &optional server)
206 (nndoc-possibly-change-buffer group server)
207 (and nndoc-current-buffer
208 (buffer-name nndoc-current-buffer)
209 (kill-buffer nndoc-current-buffer))
210 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
212 (setq nndoc-current-buffer nil)
213 (nnoo-close-server 'nndoc server)
214 (setq nndoc-dissection-alist nil)
217 (deffoo nndoc-request-list (&optional server)
220 (deffoo nndoc-request-newgroups (date &optional server)
223 (deffoo nndoc-request-list-newsgroups (&optional server)
227 ;;; Internal functions.
229 (defun nndoc-possibly-change-buffer (group source)
232 ;; The current buffer is this group's buffer.
233 ((and nndoc-current-buffer
234 (buffer-name nndoc-current-buffer)
235 (eq nndoc-current-buffer
236 (setq buf (cdr (assoc group nndoc-group-alist))))))
237 ;; We change buffers by taking an old from the group alist.
238 ;; `source' is either a string (a file name) or a buffer object.
240 (setq nndoc-current-buffer buf))
241 ;; It's a totally new group.
242 ((or (and (bufferp nndoc-address)
243 (buffer-name nndoc-address))
244 (and (stringp nndoc-address)
245 (file-exists-p nndoc-address)
246 (not (file-directory-p nndoc-address))))
247 (push (cons group (setq nndoc-current-buffer
249 (concat " *nndoc " group "*"))))
251 (setq nndoc-dissection-alist nil)
253 (set-buffer nndoc-current-buffer)
254 (buffer-disable-undo (current-buffer))
256 (if (stringp nndoc-address)
257 (insert-file-contents nndoc-address)
258 (insert-buffer-substring nndoc-address)))))
259 ;; Initialize the nndoc structures according to this new document.
260 (when (and nndoc-current-buffer
261 (not nndoc-dissection-alist))
263 (set-buffer nndoc-current-buffer)
265 (nndoc-dissect-buffer)))
266 (unless nndoc-current-buffer
267 (nndoc-close-server))
268 ;; Return whether we managed to select a file.
269 nndoc-current-buffer))
271 ;; MIME (RFC 1341) digest hack by Ulrik Dickow <dickow@nbi.dk>.
272 (defun nndoc-guess-digest-type ()
273 "Guess what digest type the current document is."
274 (let ((case-fold-search t) ; We match a bit too much, keep it simple.
275 boundary-id b-delimiter entry)
276 (goto-char (point-min))
281 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
282 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
285 (setq boundary-id (match-string 1)
286 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
287 (setq entry (assq 'mime-digest nndoc-type-alist))
290 (cons 'head-end "^ ?$")
291 (cons 'body-begin "^ ?\n")
292 (cons 'article-begin b-delimiter)
293 (cons 'body-end-function 'nndoc-digest-body-end)
295 ; (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
296 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
299 ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
301 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
307 (defun nndoc-guess-type ()
308 "Guess what document type is in the current buffer."
309 (goto-char (point-min))
311 ((looking-at rmail-unix-mail-delimiter)
313 ((looking-at "\^A\^A\^A\^A$")
315 ((looking-at "^Path:.*\n")
317 ((looking-at "#! *rnews")
319 ((re-search-forward "\^_\^L *\n" nil t)
322 (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
323 (not (re-search-forward "^Subject:.*digest" nil t))))
325 ((let ((case-fold-search nil))
326 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
331 (defun nndoc-set-delims ()
332 "Set the nndoc delimiter variables according to the type of the document."
333 (let ((vars '(nndoc-file-begin
335 nndoc-article-end nndoc-head-begin nndoc-head-end
336 nndoc-file-end nndoc-article-begin
337 nndoc-body-begin nndoc-body-end-function nndoc-body-end
338 nndoc-prepare-body nndoc-article-transform
339 nndoc-generate-head nndoc-body-begin-function)))
341 (set (pop vars) nil)))
343 ;; Guess away until we find the real file type.
344 (while (setq defs (cdr (assq nndoc-article-type nndoc-type-alist))
345 guess (assq 'guess defs))
346 (setq nndoc-article-type (funcall (cdr guess))))
347 ;; Set the nndoc variables.
349 (set (intern (format "nndoc-%s" (caar defs)))
352 (defun nndoc-search (regexp)
354 (re-search-forward regexp nil t)
355 (beginning-of-line)))
357 (defun nndoc-dissect-buffer ()
358 "Go through the document and partition it into heads/bodies/articles."
361 head-begin head-end body-begin body-end)
362 (setq nndoc-dissection-alist nil)
364 (set-buffer nndoc-current-buffer)
365 (goto-char (point-min))
366 ;; Find the beginning of the file.
367 (when nndoc-file-begin
368 (nndoc-search nndoc-file-begin))
369 ;; Go through the file.
370 (while (if (and first nndoc-first-article)
371 (nndoc-search nndoc-first-article)
372 (nndoc-search nndoc-article-begin))
374 (when nndoc-head-begin
375 (nndoc-search nndoc-head-begin))
376 (if (and nndoc-file-end
377 (looking-at nndoc-file-end))
378 (goto-char (point-max))
379 (setq head-begin (point))
380 (nndoc-search (or nndoc-head-end "^$"))
381 (setq head-end (point))
382 (if nndoc-body-begin-function
383 (funcall nndoc-body-begin-function)
384 (nndoc-search (or nndoc-body-begin "^\n")))
385 (setq body-begin (point))
386 (or (and nndoc-body-end-function
387 (funcall nndoc-body-end-function))
389 (nndoc-search nndoc-body-end))
390 (nndoc-search nndoc-article-begin)
392 (goto-char (point-max))
394 (and (re-search-backward nndoc-file-end nil t)
395 (beginning-of-line)))))
396 (setq body-end (point))
397 (push (list (incf i) head-begin head-end body-begin body-end
398 (count-lines body-begin body-end))
399 nndoc-dissection-alist))))))
401 (defun nndoc-unquote-dashes ()
402 "Unquote quoted non-separators in digests."
403 (while (re-search-forward "^- -"nil t)
404 (replace-match "-" t t)))
406 (defun nndoc-digest-body-end ()
407 (and (re-search-forward nndoc-article-begin nil t)
408 (goto-char (match-beginning 0))))
410 (defun nndoc-mbox-body-end ()
415 (and (re-search-backward nndoc-article-begin nil t)
417 (search-forward "\n\n" beg t)
419 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
420 (setq len (string-to-int (match-string 1)))
421 (search-forward "\n\n" beg t)
422 (or (= (setq len (+ (point) len)) (point-max))
423 (and (< len (point-max))
425 (looking-at nndoc-article-begin)))))
428 (defun nndoc-rnews-body-end ()
429 (and (re-search-backward nndoc-article-begin nil t)
431 (goto-char (+ (point) (string-to-int (match-string 1))))))
433 (defun nndoc-transform-clari-briefs (article)
434 (goto-char (point-min))
435 (when (looking-at " *\\*\\(.*\\)\n")
436 (replace-match "" t t))
437 (nndoc-generate-clari-briefs-head article))
439 (defun nndoc-generate-clari-briefs-head (article)
440 (let ((entry (cdr (assq article nndoc-dissection-alist)))
443 (set-buffer nndoc-current-buffer)
445 (narrow-to-region (car entry) (nth 3 entry))
446 (goto-char (point-min))
447 (when (looking-at " *\\*\\(.*\\)$")
448 (setq subject (match-string 1))
449 (when (string-match "[ \t]+$" subject)
450 (setq subject (substring subject 0 (match-beginning 0)))))
452 (let ((case-fold-search nil))
454 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
455 (setq from (match-string 1)))))
456 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
457 "\nSubject: " (or subject "(no subject)") "\n")))
459 (defun nndoc-babyl-body-begin ()
460 (re-search-forward "^\n" nil t)
461 (when (looking-at "\*\*\* EOOH \*\*\*")
462 (re-search-forward "^\n" nil t)))
466 ;;; nndoc.el ends here