*** empty log message ***
[gnus] / lisp / nndoc.el
1 ;;; nndoc.el --- single file access for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'nnheader)
30 (require 'rmail)
31 (require 'nnmail)
32 (require 'nnoo)
33 (eval-when-compile (require 'cl))
34
35 (nnoo-declare nndoc)
36
37 (defvoo nndoc-article-type 'guess
38   "*Type of the file.
39 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
40 `mime-digest', `standard-digest', `slack-digest', `clari-briefs' or
41 `guess'.")
42
43 (defvoo nndoc-post-type 'mail
44   "*Whether the nndoc group is `mail' or `post'.")
45
46 (defvar nndoc-type-alist 
47   `((mmdf 
48      (article-begin .  "^\^A\^A\^A\^A\n")
49      (body-end .  "^\^A\^A\^A\^A\n"))
50     (news
51      (article-begin . "^Path:"))
52     (rnews
53      (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
54      (body-end-function . nndoc-rnews-body-end))
55     (mbox 
56      (article-begin . 
57                     ,(let ((delim (concat "^" rmail-unix-mail-delimiter)))
58                        (if (string-match "\n\\'" delim)
59                            (substring delim 0 (match-beginning 0))
60                          delim)))
61      (body-end-function . nndoc-mbox-body-end))
62     (babyl 
63      (article-begin . "\^_\^L *\n")
64      (body-end . "\^_")
65      (body-begin-function . nndoc-babyl-body-begin)
66      (head-begin . "^[0-9].*\n"))
67     (forward
68      (article-begin . "^-+ Start of forwarded message -+\n+")
69      (body-end . "^-+ End of forwarded message -+$")
70      (prepare-body . nndoc-unquote-dashes))
71     (clari-briefs
72      (article-begin . "^ \\*")
73      (body-end . "^\t------*[ \t]^*\n^ \\*")
74      (body-begin . "^\t")
75      (head-end . "^\t")
76      (generate-head . nndoc-generate-clari-briefs-head)
77      (article-transform . nndoc-transform-clari-briefs))
78     (slack-digest
79      (article-begin . "^------------------------------*[\n \t]+")
80      (head-end . "^ ?$")
81      (body-end-function . nndoc-digest-body-end)
82      (body-begin . "^ ?$")
83      (file-end . "^End of")
84      (prepare-body . nndoc-unquote-dashes))
85     (mime-digest
86      (article-begin . "")
87      (head-end . "^ ?$")
88      (body-end . "")
89      (file-end . ""))
90     (standard-digest
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)
95      (head-end . "^ ?$")
96      (body-begin . "^ ?\n")
97      (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$"))
98     (guess 
99      (guess . nndoc-guess-type))
100     (digest
101      (guess . nndoc-guess-digest-type))
102     ))
103
104 \f
105
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)
121
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)
127
128 (defconst nndoc-version "nndoc 1.0"
129   "nndoc version.")
130
131 \f
132
133 ;;; Interface functions
134
135 (nnoo-define-basics nndoc)
136
137 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
138   (when (nndoc-possibly-change-buffer newsgroup server)
139     (save-excursion
140       (set-buffer nntp-server-buffer)
141       (erase-buffer)
142       (let (article entry)
143         (if (stringp (car articles))
144             'headers
145           (while 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)))
156               (insert ".\n")))
157
158           (nnheader-fold-continuation-lines)
159           'headers)))))
160
161 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
162   (nndoc-possibly-change-buffer newsgroup server)
163   (save-excursion
164     (let ((buffer (or buffer nntp-server-buffer))
165           (entry (cdr (assq article nndoc-dissection-alist)))
166           beg)
167       (set-buffer buffer)
168       (erase-buffer)
169       (if (stringp article)
170           nil
171         (insert-buffer-substring 
172          nndoc-current-buffer (car entry) (nth 1 entry))
173         (insert "\n")
174         (setq beg (point))
175         (insert-buffer-substring 
176          nndoc-current-buffer (nth 2 entry) (nth 3 entry))
177         (goto-char beg)
178         (when nndoc-prepare-body
179           (funcall nndoc-prepare-body))
180         (when nndoc-article-transform
181           (funcall nndoc-article-transform article))
182         t))))
183
184 (deffoo nndoc-request-group (group &optional server dont-check)
185   "Select news GROUP."
186   (let (number)
187     (cond 
188      ((not (nndoc-possibly-change-buffer group server))
189       (nnheader-report 'nndoc "No such file or buffer: %s"
190                        nndoc-address))
191      (dont-check
192       (nnheader-report 'nndoc "Selected group %s" group)
193       t)
194      ((zerop (setq number (length nndoc-dissection-alist)))
195       (nndoc-close-group group)
196       (nnheader-report 'nndoc "No articles in group %s" group))
197      (t
198       (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
199
200 (deffoo nndoc-request-type (group &optional article)
201   (cond ((not article) 'unknown)
202         (nndoc-post-type nndoc-post-type)
203         (t 'unknown)))
204
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)
211                                 nndoc-group-alist))
212   (setq nndoc-current-buffer nil)
213   (nnoo-close-server 'nndoc server)
214   (setq nndoc-dissection-alist nil)
215   t)
216
217 (deffoo nndoc-request-list (&optional server)
218   nil)
219
220 (deffoo nndoc-request-newgroups (date &optional server)
221   nil)
222
223 (deffoo nndoc-request-list-newsgroups (&optional server)
224   nil)
225
226 \f
227 ;;; Internal functions.
228
229 (defun nndoc-possibly-change-buffer (group source)
230   (let (buf)
231     (cond 
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. 
239      (buf
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 
248                               (get-buffer-create 
249                                (concat " *nndoc " group "*"))))
250             nndoc-group-alist)
251       (setq nndoc-dissection-alist nil)
252       (save-excursion
253         (set-buffer nndoc-current-buffer)
254         (buffer-disable-undo (current-buffer))
255         (erase-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))
262       (save-excursion
263         (set-buffer nndoc-current-buffer)
264         (nndoc-set-delims)
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))
270
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))
277     (cond 
278      ;; MIME digest.
279      ((and
280        (re-search-forward
281         (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
282                 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
283         nil t)
284        (match-beginning 1))
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))
288       (setcdr entry
289               (list
290                (cons 'head-end "^ ?$")
291                (cons 'body-begin "^ ?\n")
292                (cons 'article-begin b-delimiter)
293                (cons 'body-end-function 'nndoc-digest-body-end)
294 ;              (cons 'body-end 
295 ;                    (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
296                (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
297       'mime-digest)
298      ;; Standard digest.
299      ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
300            (re-search-forward 
301             (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
302       'standard-digest)
303      ;; Stupid digest.
304      (t
305       'slack-digest))))
306
307 (defun nndoc-guess-type ()
308   "Guess what document type is in the current buffer."
309   (goto-char (point-min))
310   (cond 
311    ((looking-at rmail-unix-mail-delimiter)
312     'mbox)
313    ((looking-at "\^A\^A\^A\^A$")
314     'mmdf)
315    ((looking-at "^Path:.*\n")
316     'news)
317    ((looking-at "#! *rnews")
318     'rnews)
319    ((re-search-forward "\^_\^L *\n" nil t)
320     'babyl)
321    ((save-excursion
322       (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
323            (not (re-search-forward "^Subject:.*digest" nil t))))
324     'forward)
325    ((let ((case-fold-search nil))
326       (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
327     'clari-briefs)
328    (t 
329     'digest)))
330
331 (defun nndoc-set-delims ()
332   "Set the nndoc delimiter variables according to the type of the document."
333   (let ((vars '(nndoc-file-begin 
334                 nndoc-first-article 
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)))
340     (while vars
341       (set (pop vars) nil)))
342   (let* (defs guess)
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.
348     (while defs
349       (set (intern (format "nndoc-%s" (caar defs)))
350            (cdr (pop defs))))))
351
352 (defun nndoc-search (regexp)
353   (prog1
354       (re-search-forward regexp nil t)
355     (beginning-of-line)))
356
357 (defun nndoc-dissect-buffer ()
358   "Go through the document and partition it into heads/bodies/articles."
359   (let ((i 0)
360         (first t)
361         head-begin head-end body-begin body-end)
362     (setq nndoc-dissection-alist nil)
363     (save-excursion
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))
373         (setq first nil)
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))
388               (and nndoc-body-end
389                    (nndoc-search nndoc-body-end))
390               (nndoc-search nndoc-article-begin)
391               (progn
392                 (goto-char (point-max))
393                 (when nndoc-file-end
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))))))
400
401 (defun nndoc-unquote-dashes ()
402   "Unquote quoted non-separators in digests."
403   (while (re-search-forward "^- -"nil t)
404     (replace-match "-" t t)))
405
406 (defun nndoc-digest-body-end ()
407   (and (re-search-forward nndoc-article-begin nil t)
408        (goto-char (match-beginning 0))))
409
410 (defun nndoc-mbox-body-end ()
411   (let ((beg (point))
412         len end)
413     (when
414         (save-excursion
415           (and (re-search-backward nndoc-article-begin nil t)
416                (setq end (point))
417                (search-forward "\n\n" beg t)
418                (re-search-backward
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))
424                         (goto-char len)
425                         (looking-at nndoc-article-begin)))))
426       (goto-char len))))
427
428 (defun nndoc-rnews-body-end ()
429   (and (re-search-backward nndoc-article-begin nil t)
430        (forward-line 1)
431        (goto-char (+ (point) (string-to-int (match-string 1))))))
432
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))
438
439 (defun nndoc-generate-clari-briefs-head (article)
440   (let ((entry (cdr (assq article nndoc-dissection-alist)))
441         subject from)
442     (save-excursion
443       (set-buffer nndoc-current-buffer)
444       (save-restriction
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)))))
451         (when
452             (let ((case-fold-search nil))
453               (re-search-forward
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")))
458
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)))
463
464 (provide 'nndoc)
465
466 ;;; nndoc.el ends here