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")
67 (prepare-body . nndoc-unquote-dashes))
69 (article-begin . "^ \\*")
70 (body-end . "^\t------*[ \t]^*\n^ \\*")
73 (generate-head . nndoc-generate-clari-briefs-head)
74 (article-transform . nndoc-transform-clari-briefs))
76 (article-begin . "^------------------------------*[\n \t]+")
78 (body-end-function . nndoc-digest-body-end)
80 (file-end . "^End of")
81 (prepare-body . nndoc-unquote-dashes))
88 (first-article . ,(concat "^" (make-string 70 ?-) "\n\n+"))
89 (article-begin . ,(concat "\n\n" (make-string 30 ?-) "\n\n+"))
90 (prepare-body . nndoc-unquote-dashes)
91 (body-end-function . nndoc-digest-body-end)
93 (body-begin . "^ ?\n")
94 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$"))
96 (guess . nndoc-guess-type))
98 (guess . nndoc-guess-digest-type))
103 (defvar nndoc-file-begin nil)
104 (defvar nndoc-first-article nil)
105 (defvar nndoc-article-end nil)
106 (defvar nndoc-article-begin nil)
107 (defvar nndoc-head-begin nil)
108 (defvar nndoc-head-end nil)
109 (defvar nndoc-file-end nil)
110 (defvar nndoc-body-begin nil)
111 (defvar nndoc-body-end-function nil)
112 (defvar nndoc-body-begin-function nil)
113 (defvar nndoc-body-end nil)
114 (defvar nndoc-dissection-alist nil)
115 (defvar nndoc-prepare-body nil)
116 (defvar nndoc-generate-head nil)
117 (defvar nndoc-article-transform nil)
119 (defvar nndoc-status-string "")
120 (defvar nndoc-group-alist nil)
121 (defvar nndoc-current-buffer nil
122 "Current nndoc news buffer.")
123 (defvar nndoc-address nil)
127 (defvar nndoc-current-server nil)
128 (defvar nndoc-server-alist nil)
129 (defvar nndoc-server-variables
130 `((nndoc-article-type ,nndoc-article-type)
131 (nndoc-article-begin nil)
132 (nndoc-article-end nil)
133 (nndoc-head-begin nil)
135 (nndoc-first-article nil)
136 (nndoc-current-buffer nil)
137 (nndoc-group-alist nil)
138 (nndoc-end-of-file nil)
139 (nndoc-body-begin nil)
140 (nndoc-dissection-alist nil)
141 (nndoc-generate-head nil)
142 (nndoc-article-transform nil)
143 (nndoc-body-end-function nil)
144 (nndoc-prepare-body nil)
145 (nndoc-body-begin-function nil)
146 (nndoc-status-string ,nndoc-status-string)
147 (nndoc-body-end ,nndoc-body-end)
148 (nndoc-file-end ,nndoc-file-end)
149 (nndoc-file-begin ,nndoc-file-begin)
150 (nndoc-type-alist ,nndoc-type-alist)
151 (nndoc-post-type ,nndoc-post-type)
152 (nndoc-address nil)))
154 (defconst nndoc-version "nndoc 1.0"
159 ;;; Interface functions
161 (defun nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
162 (when (nndoc-possibly-change-buffer newsgroup server)
164 (set-buffer nntp-server-buffer)
167 (if (stringp (car articles))
170 (when (setq entry (cdr (assq (setq article (pop articles))
171 nndoc-dissection-alist)))
172 (insert (format "221 %d Article retrieved.\n" article))
173 (if nndoc-generate-head
174 (funcall nndoc-generate-head article)
175 (insert-buffer-substring
176 nndoc-current-buffer (car entry) (nth 1 entry)))
177 (goto-char (point-max))
178 (or (= (char-after (1- (point))) ?\n) (insert "\n"))
179 (insert (format "Lines: %d\n" (nth 4 entry)))
182 (nnheader-fold-continuation-lines)
185 (defun nndoc-open-server (server &optional defs)
186 (nnheader-change-server 'nndoc server defs))
188 (defun nndoc-close-server (&optional server)
189 (setq nndoc-current-server nil)
192 (defun nndoc-server-opened (&optional server)
193 (and (equal server nndoc-current-server)
195 (buffer-name nntp-server-buffer)))
197 (defun nndoc-status-message (&optional server)
200 (defun nndoc-request-article (article &optional newsgroup server buffer)
201 (nndoc-possibly-change-buffer newsgroup server)
203 (let ((buffer (or buffer nntp-server-buffer))
204 (entry (cdr (assq article nndoc-dissection-alist)))
208 (if (stringp article)
210 (insert-buffer-substring
211 nndoc-current-buffer (car entry) (nth 1 entry))
214 (insert-buffer-substring
215 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
217 (when nndoc-prepare-body
218 (funcall nndoc-prepare-body))
219 (when nndoc-article-transform
220 (funcall nndoc-article-transform article))
223 (defun nndoc-request-group (group &optional server dont-check)
227 ((not (nndoc-possibly-change-buffer group server))
228 (nnheader-report 'nndoc "No such file or buffer: %s"
231 (nnheader-report 'nndoc "Selected group %s" group)
233 ((zerop (setq number (length nndoc-dissection-alist)))
234 (nndoc-close-group group)
235 (nnheader-report 'nndoc "No articles in group %s" group))
237 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
239 (defun nndoc-request-type (group &optional article)
240 (cond ((not article) 'unknown)
241 (nndoc-post-type nndoc-post-type)
244 (defun nndoc-close-group (group &optional server)
245 (nndoc-possibly-change-buffer group server)
246 (and nndoc-current-buffer
247 (buffer-name nndoc-current-buffer)
248 (kill-buffer nndoc-current-buffer))
249 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
251 (setq nndoc-current-buffer nil)
252 (setq nndoc-current-server nil)
253 (setq nndoc-dissection-alist nil)
256 (defun nndoc-request-list (&optional server)
259 (defun nndoc-request-newgroups (date &optional server)
262 (defun nndoc-request-list-newsgroups (&optional server)
265 (defalias 'nndoc-request-post 'nnmail-request-post)
268 ;;; Internal functions.
270 (defun nndoc-possibly-change-buffer (group source)
273 ;; The current buffer is this group's buffer.
274 ((and nndoc-current-buffer
275 (buffer-name nndoc-current-buffer)
276 (eq nndoc-current-buffer
277 (setq buf (cdr (assoc group nndoc-group-alist))))))
278 ;; We change buffers by taking an old from the group alist.
279 ;; `source' is either a string (a file name) or a buffer object.
281 (setq nndoc-current-buffer buf))
282 ;; It's a totally new group.
283 ((or (and (bufferp nndoc-address)
284 (buffer-name nndoc-address))
285 (and (stringp nndoc-address)
286 (file-exists-p nndoc-address)
287 (not (file-directory-p nndoc-address))))
288 (push (cons group (setq nndoc-current-buffer
290 (concat " *nndoc " group "*"))))
292 (setq nndoc-dissection-alist nil)
294 (set-buffer nndoc-current-buffer)
295 (buffer-disable-undo (current-buffer))
297 (if (stringp nndoc-address)
298 (insert-file-contents nndoc-address)
299 (insert-buffer-substring nndoc-address)))))
300 ;; Initialize the nndoc structures according to this new document.
301 (when (and nndoc-current-buffer
302 (not nndoc-dissection-alist))
304 (set-buffer nndoc-current-buffer)
306 (nndoc-dissect-buffer)))
307 (unless nndoc-current-buffer
308 (nndoc-close-server))
309 ;; Return whether we managed to select a file.
310 nndoc-current-buffer))
312 ;; MIME (RFC 1341) digest hack by Ulrik Dickow <dickow@nbi.dk>.
313 (defun nndoc-guess-digest-type ()
314 "Guess what digest type the current document is."
315 (let ((case-fold-search t) ; We match a bit too much, keep it simple.
316 boundary-id b-delimiter entry)
317 (goto-char (point-min))
322 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
323 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
326 (setq boundary-id (match-string 1)
327 b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
328 (setq entry (assq 'mime-digest nndoc-type-alist))
331 (cons 'head-end "^ ?$")
332 (cons 'body-begin "^ ?\n")
333 (cons 'article-begin b-delimiter)
334 (cons 'body-end-function 'nndoc-digest-body-end)
336 ; (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
337 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
340 ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
342 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
348 (defun nndoc-guess-type ()
349 "Guess what document type is in the current buffer."
350 (goto-char (point-min))
352 ((looking-at rmail-unix-mail-delimiter)
354 ((looking-at "\^A\^A\^A\^A$")
356 ((looking-at "^Path:.*\n")
358 ((looking-at "#! *rnews")
360 ((re-search-forward "\^_\^L *\n" nil t)
363 (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
364 (not (re-search-forward "^Subject:.*digest" nil t))))
366 ((let ((case-fold-search nil))
367 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
372 (defun nndoc-set-delims ()
373 "Set the nndoc delimiter variables according to the type of the document."
374 (let ((vars '(nndoc-file-begin
376 nndoc-article-end nndoc-head-begin nndoc-head-end
377 nndoc-file-end nndoc-article-begin
378 nndoc-body-begin nndoc-body-end-function nndoc-body-end
379 nndoc-prepare-body nndoc-article-transform
380 nndoc-generate-head nndoc-body-begin-function)))
382 (set (pop vars) nil)))
384 ;; Guess away until we find the real file type.
385 (while (setq defs (cdr (assq nndoc-article-type nndoc-type-alist))
386 guess (assq 'guess defs))
387 (setq nndoc-article-type (funcall (cdr guess))))
388 ;; Set the nndoc variables.
390 (set (intern (format "nndoc-%s" (caar defs)))
393 (defun nndoc-search (regexp)
395 (re-search-forward regexp nil t)
396 (beginning-of-line)))
398 (defun nndoc-dissect-buffer ()
399 "Go through the document and partition it into heads/bodies/articles."
402 head-begin head-end body-begin body-end)
403 (setq nndoc-dissection-alist nil)
405 (set-buffer nndoc-current-buffer)
406 (goto-char (point-min))
407 ;; Find the beginning of the file.
408 (when nndoc-file-begin
409 (nndoc-search nndoc-file-begin))
410 ;; Go through the file.
411 (while (if (and first nndoc-first-article)
412 (nndoc-search nndoc-first-article)
413 (nndoc-search nndoc-article-begin))
415 (when nndoc-head-begin
416 (nndoc-search nndoc-head-begin))
417 (if (and nndoc-file-end
418 (looking-at nndoc-file-end))
419 (goto-char (point-max))
420 (setq head-begin (point))
421 (nndoc-search (or nndoc-head-end "^$"))
422 (setq head-end (point))
423 (if nndoc-body-begin-function
424 (funcall nndoc-body-begin-function)
425 (nndoc-search (or nndoc-body-begin "^\n")))
426 (setq body-begin (point))
427 (or (and nndoc-body-end-function
428 (funcall nndoc-body-end-function))
430 (nndoc-search nndoc-body-end))
431 (nndoc-search nndoc-article-begin)
433 (goto-char (point-max))
435 (and (re-search-backward nndoc-file-end nil t)
436 (beginning-of-line)))))
437 (setq body-end (point))
438 (push (list (incf i) head-begin head-end body-begin body-end
439 (count-lines body-begin body-end))
440 nndoc-dissection-alist))))))
442 (defun nndoc-unquote-dashes ()
443 "Unquote quoted non-separators in digests."
444 (while (re-search-forward "^- -"nil t)
445 (replace-match "-" t t)))
447 (defun nndoc-digest-body-end ()
448 (and (re-search-forward nndoc-article-begin nil t)
449 (goto-char (match-beginning 0))))
451 (defun nndoc-mbox-body-end ()
456 (and (re-search-backward nndoc-article-begin nil t)
458 (search-forward "\n\n" beg t)
460 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
461 (setq len (string-to-int (match-string 1)))
462 (search-forward "\n\n" beg t)
463 (or (= (setq len (+ (point) len)) (point-max))
464 (and (< len (point-max))
466 (looking-at nndoc-article-begin)))))
469 (defun nndoc-rnews-body-end ()
470 (and (re-search-backward nndoc-article-begin nil t)
472 (goto-char (+ (point) (string-to-int (match-string 1))))))
474 (defun nndoc-transform-clari-briefs (article)
475 (goto-char (point-min))
476 (when (looking-at " *\\*\\(.*\\)\n")
477 (replace-match "" t t))
478 (nndoc-generate-clari-briefs-head article))
480 (defun nndoc-generate-clari-briefs-head (article)
481 (let ((entry (cdr (assq article nndoc-dissection-alist)))
484 (set-buffer nndoc-current-buffer)
486 (narrow-to-region (car entry) (nth 3 entry))
487 (goto-char (point-min))
488 (when (looking-at " *\\*\\(.*\\)$")
489 (setq subject (match-string 1))
490 (when (string-match "[ \t]+$" subject)
491 (setq subject (substring subject 0 (match-beginning 0)))))
493 (let ((case-fold-search nil))
495 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
496 (setq from (match-string 1)))))
497 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
498 "\nSubject: " (or subject "(no subject)") "\n")))
500 (defun nndoc-babyl-body-begin ()
501 (re-search-forward "^\n" nil t)
502 (when (looking-at "\*\*\* EOOH \*\*\*")
503 (re-search-forward "^\n" nil t)))
507 ;;; nndoc.el ends here