1 ;;; nndoc.el --- single file access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; For Outlook mail boxes format, see http://mbx2mbox.sourceforge.net/
38 (eval-when-compile (require 'cl))
42 (defvoo nndoc-article-type 'guess
44 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
45 `rfc934', `rfc822-forward', `mime-parts', `standard-digest',
46 `slack-digest', `clari-briefs', `nsmail', `outlook', `oe-dbx',
47 `mailman', `exim-bounce', or `guess'.")
49 (defvoo nndoc-post-type 'mail
50 "*Whether the nndoc group is `mail' or `post'.")
52 (defvoo nndoc-open-document-hook 'nnheader-ms-strip-cr
53 "Hook run after opening a document.
54 The default function removes all trailing carriage returns
57 (defvar nndoc-type-alist
59 (article-begin . "^\^A\^A\^A\^A\n")
60 (body-end . "^\^A\^A\^A\^A\n"))
63 (head-begin . "^ ?\n")
67 (subtype digest guess))
69 (generate-head-function . nndoc-generate-mime-parts-head)
70 (article-transform-function . nndoc-transform-mime-parts))
72 (article-begin . "^From - "))
74 (article-begin . "^Path:"))
76 (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
77 (body-end-function . nndoc-rnews-body-end))
79 (article-begin-function . nndoc-mbox-article-begin)
80 (body-end-function . nndoc-mbox-body-end))
82 (article-begin . "\^_\^L *\n")
84 (body-begin-function . nndoc-babyl-body-begin)
85 (head-begin-function . nndoc-babyl-head-begin))
87 (article-begin . "^------ This is a copy of the message, including all the headers. ------\n\n")
88 (body-end-function . nndoc-exim-bounce-body-end-function))
90 (article-begin . "^--.*\n+")
92 (prepare-body-function . nndoc-unquote-dashes))
94 (article-begin . "^--__--__--\n\nMessage:")
95 (body-end . "^--__--__--$")
96 (prepare-body-function . nndoc-unquote-dashes))
98 (article-begin . "^ \\*")
99 (body-end . "^\t------*[ \t]^*\n^ \\*")
102 (generate-head-function . nndoc-generate-clari-briefs-head)
103 (article-transform-function . nndoc-transform-clari-briefs))
106 (first-article . ,(concat "^" (make-string 70 ?-) "\n *\n+"))
107 (article-begin . ,(concat "^\n" (make-string 30 ?-) "\n *\n+"))
108 (prepare-body-function . nndoc-unquote-dashes)
109 (body-end-function . nndoc-digest-body-end)
111 (body-begin . "^ *\n")
112 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$")
113 (subtype digest guess))
115 (article-begin . "^------------------------------*[\n \t]+")
117 (body-end-function . nndoc-digest-body-end)
118 (body-begin . "^ ?$")
119 (file-end . "^End of")
120 (prepare-body-function . nndoc-unquote-dashes)
121 (subtype digest guess))
123 (article-begin . "^\\\\\\\\\n")
124 (head-begin . "^Paper.*:")
125 (head-end . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
127 (body-end . "\\(-------------------------------------------------\\|%-%-%-%-%-%-%-%-%-%-%-%-%-%-\\|%%--%%--%%--%%--%%--%%--%%--%%--\\|%%%---%%%---%%%---%%%---\\)")
128 (file-end . "\\(^Title: Recent Seminal\\|%%%---%%%---%%%---%%%---\\)")
129 (generate-head-function . nndoc-generate-lanl-gov-head)
130 (article-transform-function . nndoc-transform-lanl-gov-announce)
131 (subtype preprints guess))
133 (article-begin . "^\n+")
134 (body-end-function . nndoc-rfc822-forward-body-end-function)
135 (generate-head-function . nndoc-rfc822-forward-generate-head)
136 (generate-article-function . nndoc-rfc822-forward-generate-article))
138 (article-begin-function . nndoc-outlook-article-begin)
140 (oe-dbx ;; Outlook Express DBX format
141 (dissection-function . nndoc-oe-dbx-dissection)
142 (generate-head-function . nndoc-oe-dbx-generate-head)
143 (generate-article-function . nndoc-oe-dbx-generate-article))
145 (article-begin . "^-+ \\(Start of \\)?forwarded message.*\n+")
146 (body-end . "^-+ End \\(of \\)?forwarded message.*$")
147 (prepare-body-function . nndoc-unquote-dashes))
148 (mail-in-mail ;; Wild guess on mailer daemon's messages or others
149 (article-begin-function . nndoc-mail-in-mail-article-begin))
160 (defvar nndoc-binary-file-names ".[Dd][Bb][Xx]$"
161 "Regexp for binary nndoc file names.")
164 (defvoo nndoc-file-begin nil)
165 (defvoo nndoc-first-article nil)
166 (defvoo nndoc-article-begin nil)
167 (defvoo nndoc-head-begin nil)
168 (defvoo nndoc-head-end nil)
169 (defvoo nndoc-file-end nil)
170 (defvoo nndoc-body-begin nil)
171 (defvoo nndoc-body-end-function nil)
172 (defvoo nndoc-body-begin-function nil)
173 (defvoo nndoc-head-begin-function nil)
174 (defvoo nndoc-body-end nil)
175 ;; nndoc-dissection-alist is a list of sublists. Each sublist holds the
176 ;; following items. ARTICLE acts as the association key and is an ordinal
177 ;; starting at 1. HEAD-BEGIN [0], HEAD-END [1], BODY-BEGIN [2] and BODY-END
178 ;; [3] are positions in the `nndoc' buffer. LINE-COUNT [4] is a count of
179 ;; lines in the body. For MIME dissections only, ARTICLE-INSERT [5] and
180 ;; SUMMARY-INSERT [6] give headers to insert for full article or summary line
181 ;; generation, respectively. Other headers usually follow directly from the
182 ;; buffer. Value `nil' means no insert.
183 (defvoo nndoc-dissection-alist nil)
184 (defvoo nndoc-prepare-body-function nil)
185 (defvoo nndoc-generate-head-function nil)
186 (defvoo nndoc-article-transform-function nil)
187 (defvoo nndoc-article-begin-function nil)
188 (defvoo nndoc-generate-article-function nil)
189 (defvoo nndoc-dissection-function nil)
191 (defvoo nndoc-status-string "")
192 (defvoo nndoc-group-alist nil)
193 (defvoo nndoc-current-buffer nil
194 "Current nndoc news buffer.")
195 (defvoo nndoc-address nil)
197 (defconst nndoc-version "nndoc 1.0"
202 ;;; Interface functions
204 (nnoo-define-basics nndoc)
206 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
207 (when (nndoc-possibly-change-buffer newsgroup server)
209 (set-buffer nntp-server-buffer)
212 (if (stringp (car articles))
215 (when (setq entry (cdr (assq (setq article (pop articles))
216 nndoc-dissection-alist)))
217 (insert (format "221 %d Article retrieved.\n" article))
218 (if nndoc-generate-head-function
219 (funcall nndoc-generate-head-function article)
220 (insert-buffer-substring
221 nndoc-current-buffer (car entry) (nth 1 entry)))
222 (goto-char (point-max))
223 (unless (eq (char-after (1- (point))) ?\n)
225 (insert (format "Lines: %d\n" (nth 4 entry)))
228 (nnheader-fold-continuation-lines)
231 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
232 (nndoc-possibly-change-buffer newsgroup server)
234 (let ((buffer (or buffer nntp-server-buffer))
235 (entry (cdr (assq article nndoc-dissection-alist)))
241 ((stringp article) nil)
242 (nndoc-generate-article-function
243 (funcall nndoc-generate-article-function article))
245 (insert-buffer-substring
246 nndoc-current-buffer (car entry) (nth 1 entry))
249 (insert-buffer-substring
250 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
252 (when nndoc-prepare-body-function
253 (funcall nndoc-prepare-body-function))
254 (when nndoc-article-transform-function
255 (funcall nndoc-article-transform-function article))
258 (deffoo nndoc-request-group (group &optional server dont-check)
262 ((not (nndoc-possibly-change-buffer group server))
263 (nnheader-report 'nndoc "No such file or buffer: %s"
266 (nnheader-report 'nndoc "Selected group %s" group)
268 ((zerop (setq number (length nndoc-dissection-alist)))
269 (nndoc-close-group group)
270 (nnheader-report 'nndoc "No articles in group %s" group))
272 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
274 (deffoo nndoc-request-type (group &optional article)
275 (cond ((not article) 'unknown)
276 (nndoc-post-type nndoc-post-type)
279 (deffoo nndoc-close-group (group &optional server)
280 (nndoc-possibly-change-buffer group server)
281 (and nndoc-current-buffer
282 (buffer-name nndoc-current-buffer)
283 (kill-buffer nndoc-current-buffer))
284 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
286 (setq nndoc-current-buffer nil)
287 (nnoo-close-server 'nndoc server)
288 (setq nndoc-dissection-alist nil)
291 (deffoo nndoc-request-list (&optional server)
294 (deffoo nndoc-request-newgroups (date &optional server)
297 (deffoo nndoc-request-list-newsgroups (&optional server)
301 ;;; Internal functions.
303 (defun nndoc-possibly-change-buffer (group source)
306 ;; The current buffer is this group's buffer.
307 ((and nndoc-current-buffer
308 (buffer-name nndoc-current-buffer)
309 (eq nndoc-current-buffer
310 (setq buf (cdr (assoc group nndoc-group-alist))))))
311 ;; We change buffers by taking an old from the group alist.
312 ;; `source' is either a string (a file name) or a buffer object.
314 (setq nndoc-current-buffer buf))
315 ;; It's a totally new group.
316 ((or (and (bufferp nndoc-address)
317 (buffer-name nndoc-address))
318 (and (stringp nndoc-address)
319 (file-exists-p nndoc-address)
320 (not (file-directory-p nndoc-address))))
321 (push (cons group (setq nndoc-current-buffer
323 (concat " *nndoc " group "*"))))
325 (setq nndoc-dissection-alist nil)
327 (set-buffer nndoc-current-buffer)
329 (if (and (stringp nndoc-address)
330 (string-match nndoc-binary-file-names nndoc-address))
331 (let ((coding-system-for-read 'binary))
332 (mm-insert-file-contents nndoc-address))
333 (if (stringp nndoc-address)
334 (nnheader-insert-file-contents nndoc-address)