77f8fa5850fc0058bca79ac476f07e05796fd8c4
[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 (eval-when-compile (require 'cl))
33
34 (defvar nndoc-article-type 'guess
35   "*Type of the file.
36 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
37 `mime-digest', `standard-digest', `slack-digest', `clari-briefs' or
38 `guess'.")
39
40 (defvar nndoc-post-type 'mail
41   "*Whether the nndoc group is `mail' or `post'.")
42
43 (defvar nndoc-type-alist 
44   `((mmdf 
45      (article-begin .  "^\^A\^A\^A\^A\n")
46      (body-end .  "^\^A\^A\^A\^A\n"))
47     (news
48      (article-begin . "^Path:"))
49     (rnews
50      (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
51      (body-end-function . nndoc-rnews-body-end))
52     (mbox 
53      (article-begin . 
54                     ,(let ((delim (concat "^" rmail-unix-mail-delimiter)))
55                        (if (string-match "\n\\'" delim)
56                            (substring delim 0 (match-beginning 0))
57                          delim)))
58      (body-end-function . nndoc-mbox-body-end))
59     (babyl 
60      (article-begin . "\^_\^L *\n")
61      (body-end . "\^_")
62      (body-begin-function . nndoc-babyl-body-begin)
63      (head-begin . "^[0-9].*\n"))
64     (forward
65      (article-begin . "^-+ Start of forwarded message -+\n+")
66      (body-end . "^-+ End of forwarded message -+\n"))
67     (clari-briefs
68      (article-begin . "^ \\*")
69      (body-end . "^\t------*[ \t]^*\n^ \\*")
70      (body-begin . "^\t")
71      (head-end . "^\t")
72      (generate-head . nndoc-generate-clari-briefs-head)
73      (article-transform . nndoc-transform-clari-briefs))
74     (slack-digest
75      (article-begin . "^------------------------------*[\n \t]+")
76      (head-end . "^ ?$")
77      (body-end-function . nndoc-digest-body-end)
78      (body-begin . "^ ?$")
79      (file-end . "^End of")
80      (prepare-body . nndoc-prepare-digest-body))
81     (mime-digest
82      (article-begin . "")
83      (head-end . "^ ?$")
84      (body-end . "")
85      (file-end . ""))
86     (standard-digest
87      (first-article . ,(concat "^" (make-string 70 ?-) "\n\n+"))
88      (article-begin . ,(concat "\n\n" (make-string 30 ?-) "\n\n+"))
89      (prepare-body . nndoc-prepare-digest-body)
90      (body-end-function . nndoc-digest-body-end)
91      (head-end . "^ ?$")
92      (body-begin . "^ ?\n")
93      (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$"))
94     (guess 
95      (guess . nndoc-guess-type))
96     (digest
97      (guess . nndoc-guess-digest-type))
98     ))
99
100 \f
101
102 (defvar nndoc-file-begin nil)
103 (defvar nndoc-first-article nil)
104 (defvar nndoc-article-end nil)
105 (defvar nndoc-article-begin nil)
106 (defvar nndoc-head-begin nil)
107 (defvar nndoc-head-end nil)
108 (defvar nndoc-file-end nil)
109 (defvar nndoc-body-begin nil)
110 (defvar nndoc-body-end-function nil)
111 (defvar nndoc-body-begin-function nil)
112 (defvar nndoc-body-end nil)
113 (defvar nndoc-dissection-alist nil)
114 (defvar nndoc-prepare-body nil)
115 (defvar nndoc-generate-head nil)
116 (defvar nndoc-article-transform nil)
117
118 (defvar nndoc-status-string "")
119 (defvar nndoc-group-alist nil)
120 (defvar nndoc-current-buffer nil
121   "Current nndoc news buffer.")
122 (defvar nndoc-address nil)
123
124 \f
125
126 (defvar nndoc-current-server nil)
127 (defvar nndoc-server-alist nil)
128 (defvar nndoc-server-variables
129   `((nndoc-article-type ,nndoc-article-type)
130     (nndoc-article-begin nil)
131     (nndoc-article-end nil)
132     (nndoc-head-begin nil)
133     (nndoc-head-end nil)
134     (nndoc-first-article nil)
135     (nndoc-current-buffer nil)
136     (nndoc-group-alist nil)
137     (nndoc-end-of-file nil)
138     (nndoc-body-begin nil)
139     (nndoc-dissection-alist nil)
140     (nndoc-generate-head nil)
141     (nndoc-article-transform nil)
142     (nndoc-body-end-function nil)
143     (nndoc-prepare-body nil)
144     (nndoc-body-begin-function nil)
145     (nndoc-status-string ,nndoc-status-string)
146     (nndoc-body-end ,nndoc-body-end)
147     (nndoc-file-end ,nndoc-file-end)
148     (nndoc-file-begin ,nndoc-file-begin)
149     (nndoc-type-alist ,nndoc-type-alist)
150     (nndoc-post-type ,nndoc-post-type)
151     (nndoc-address nil)))
152
153 (defconst nndoc-version "nndoc 1.0"
154   "nndoc version.")
155
156 \f
157
158 ;;; Interface functions
159
160 (defun nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
161   (when (nndoc-possibly-change-buffer newsgroup server)
162     (save-excursion
163       (set-buffer nntp-server-buffer)
164       (erase-buffer)
165       (let (article entry)
166         (if (stringp (car articles))
167             'headers
168           (while articles
169             (when (setq entry (cdr (assq (setq article (pop articles))
170                                          nndoc-dissection-alist)))
171               (insert (format "221 %d Article retrieved.\n" article))
172               (if nndoc-generate-head
173                   (funcall nndoc-generate-head article)
174                 (insert-buffer-substring
175                  nndoc-current-buffer (car entry) (nth 1 entry)))
176               (goto-char (point-max))
177               (or (= (char-after (1- (point))) ?\n) (insert "\n"))
178               (insert (format "Lines: %d\n" (nth 4 entry)))
179               (insert ".\n")))
180
181           (nnheader-fold-continuation-lines)
182           'headers)))))
183
184 (defun nndoc-open-server (server &optional defs)
185   (nnheader-change-server 'nndoc server defs))
186
187 (defun nndoc-close-server (&optional server)
188   (setq nndoc-current-server nil)
189   t)
190
191 (defun nndoc-server-opened (&optional server)
192   (and (equal server nndoc-current-server)
193        nntp-server-buffer
194        (buffer-name nntp-server-buffer)))
195
196 (defun nndoc-status-message (&optional server)
197   nndoc-status-string)
198
199 (defun nndoc-request-article (article &optional newsgroup server buffer)
200   (nndoc-possibly-change-buffer newsgroup server)
201   (save-excursion
202     (let ((buffer (or buffer nntp-server-buffer))
203           (entry (cdr (assq article nndoc-dissection-alist)))
204           beg)
205       (set-buffer buffer)
206       (erase-buffer)
207       (if (stringp article)
208           nil
209         (insert-buffer-substring 
210          nndoc-current-buffer (car entry) (nth 1 entry))
211         (insert "\n")
212         (setq beg (point))
213         (insert-buffer-substring 
214          nndoc-current-buffer (nth 2 entry) (nth 3 entry))
215         (goto-char beg)
216         (when nndoc-prepare-body
217           (funcall nndoc-prepare-body))
218         (when nndoc-article-transform
219           (funcall nndoc-article-transform article))
220         t))))
221
222 (defun nndoc-request-group (group &optional server dont-check)
223   "Select news GROUP."
224   (let (number)
225     (cond 
226      ((not (nndoc-possibly-change-buffer group server))
227       (nnheader-report 'nndoc "No such file or buffer: %s"
228                        nndoc-address))
229      (dont-check
230       (nnheader-report 'nndoc "Selected group %s" group)
231       t)
232      ((zerop (setq number (length nndoc-dissection-alist)))
233       (nndoc-close-group group)
234       (nnheader-report 'nndoc "No articles in group %s" group))
235      (t
236       (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
237
238 (defun nndoc-request-type (group &optional article)
239   (cond ((not article) 'unknown)
240         (nndoc-post-type nndoc-post-type)
241         (t 'unknown)))
242
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)
249                                 nndoc-group-alist))
250   (setq nndoc-current-buffer nil)
251   (setq nndoc-current-server nil)
252   (setq nndoc-dissection-alist nil)
253   t)
254
255 (defun nndoc-request-list (&optional server)
256   nil)
257
258 (defun nndoc-request-newgroups (date &optional server)
259   nil)
260
261 (defun nndoc-request-list-newsgroups (&optional server)
262   nil)
263
264 (defalias 'nndoc-request-post 'nnmail-request-post)
265
266 \f
267 ;;; Internal functions.
268
269 (defun nndoc-possibly-change-buffer (group source)
270   (let (buf)
271     (cond 
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. 
279      (buf
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 
288                               (get-buffer-create 
289                                (concat " *nndoc " group "*"))))
290             nndoc-group-alist)
291       (setq nndoc-dissection-alist nil)
292       (save-excursion
293         (set-buffer nndoc-current-buffer)
294         (buffer-disable-undo (current-buffer))
295         (erase-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     (when (and nndoc-current-buffer
301                (not nndoc-dissection-alist))
302       (save-excursion
303         (set-buffer nndoc-current-buffer)
304         (nndoc-set-delims)
305         (nndoc-dissect-buffer)))
306     (unless nndoc-current-buffer
307       (nndoc-close-server))
308     ;; Return whether we managed to select a file.
309     nndoc-current-buffer))
310
311 ;; MIME (RFC 1341) digest hack by Ulrik Dickow <dickow@nbi.dk>.
312 (defun nndoc-guess-digest-type ()
313   "Guess what digest type the current document is."
314   (let ((case-fold-search t)            ; We match a bit too much, keep it simple.
315         boundary-id b-delimiter entry)
316     (goto-char (point-min))
317     (cond 
318      ;; MIME digest.
319      ((and
320        (re-search-forward
321         (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
322                 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
323         nil t)
324        (match-beginning 1))
325       (setq boundary-id (match-string 1)
326             b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
327       (setq entry (assq 'mime-digest nndoc-type-alist))
328       (setcdr entry
329               (list
330                (cons 'head-end "^ ?$")
331                (cons 'body-begin "^ ?\n")
332                (cons 'article-begin b-delimiter)
333                (cons 'body-end-function 'nndoc-digest-body-end)
334 ;              (cons 'body-end 
335 ;                    (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
336                (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
337       'mime-digest)
338      ;; Standard digest.
339      ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
340            (re-search-forward 
341             (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
342       'standard-digest)
343      ;; Stupid digest.
344      (t
345       'slack-digest))))
346
347 (defun nndoc-guess-type ()
348   "Guess what document type is in the current buffer."
349   (goto-char (point-min))
350   (cond 
351    ((looking-at rmail-unix-mail-delimiter)
352     'mbox)
353    ((looking-at "\^A\^A\^A\^A$")
354     'mmdf)
355    ((looking-at "^Path:.*\n")
356     'news)
357    ((looking-at "#! *rnews")
358     'rnews)
359    ((re-search-forward "\^_\^L *\n" nil t)
360     'babyl)
361    ((save-excursion
362       (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
363            (not (re-search-forward "^Subject:.*digest" nil t))))
364     'forward)
365    ((let ((case-fold-search nil))
366       (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
367     'clari-briefs)
368    (t 
369     'digest)))
370
371 (defun nndoc-set-delims ()
372   "Set the nndoc delimiter variables according to the type of the document."
373   (let ((vars '(nndoc-file-begin 
374                 nndoc-first-article 
375                 nndoc-article-end nndoc-head-begin nndoc-head-end
376                 nndoc-file-end nndoc-article-begin
377                 nndoc-body-begin nndoc-body-end-function nndoc-body-end
378                 nndoc-prepare-body nndoc-article-transform
379                 nndoc-generate-head nndoc-body-begin-function)))
380     (while vars
381       (set (pop vars) nil)))
382   (let* (defs guess)
383     ;; Guess away until we find the real file type.
384     (while (setq defs (cdr (assq nndoc-article-type nndoc-type-alist))
385                  guess (assq 'guess defs))
386       (setq nndoc-article-type (funcall (cdr guess))))
387     ;; Set the nndoc variables.
388     (while defs
389       (set (intern (format "nndoc-%s" (caar defs)))
390            (cdr (pop defs))))))
391
392 (defun nndoc-search (regexp)
393   (prog1
394       (re-search-forward regexp nil t)
395     (beginning-of-line)))
396
397 (defun nndoc-dissect-buffer ()
398   "Go through the document and partition it into heads/bodies/articles."
399   (let ((i 0)
400         (first t)
401         head-begin head-end body-begin body-end)
402     (setq nndoc-dissection-alist nil)
403     (save-excursion
404       (set-buffer nndoc-current-buffer)
405       (goto-char (point-min))
406       ;; Find the beginning of the file.
407       (when nndoc-file-begin
408         (nndoc-search nndoc-file-begin))
409       ;; Go through the file.
410       (while (if (and first nndoc-first-article)
411                  (nndoc-search nndoc-first-article)
412                (nndoc-search nndoc-article-begin))
413         (setq first nil)
414         (when nndoc-head-begin 
415           (nndoc-search nndoc-head-begin))
416         (if (and nndoc-file-end
417                  (looking-at nndoc-file-end))
418             (goto-char (point-max))
419           (setq head-begin (point))
420           (nndoc-search (or nndoc-head-end "^$"))
421           (setq head-end (point))
422           (if nndoc-body-begin-function
423               (funcall nndoc-body-begin-function)
424             (nndoc-search (or nndoc-body-begin "^\n")))
425           (setq body-begin (point))
426           (or (and nndoc-body-end-function
427                    (funcall nndoc-body-end-function))
428               (and nndoc-body-end
429                    (nndoc-search nndoc-body-end))
430               (nndoc-search nndoc-article-begin)
431               (progn
432                 (goto-char (point-max))
433                 (when nndoc-file-end
434                   (and (re-search-backward nndoc-file-end nil t)
435                        (beginning-of-line)))))
436           (setq body-end (point))
437           (push (list (incf i) head-begin head-end body-begin body-end
438                       (count-lines body-begin body-end))
439                 nndoc-dissection-alist))))))
440
441 (defun nndoc-prepare-digest-body ()
442   "Unquote quoted non-separators in digests."
443   (while (re-search-forward "^- -"nil t)
444     (replace-match "-" t t)))
445
446 (defun nndoc-digest-body-end ()
447   (and (re-search-forward nndoc-article-begin nil t)
448        (goto-char (match-beginning 0))))
449
450 (defun nndoc-mbox-body-end ()
451   (let ((beg (point))
452         len end)
453     (when
454         (save-excursion
455           (and (re-search-backward nndoc-article-begin nil t)
456                (setq end (point))
457                (search-forward "\n\n" beg t)
458                (re-search-backward
459                 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
460                (setq len (string-to-int (match-string 1)))
461                (search-forward "\n\n" beg t)
462                (or (= (setq len (+ (point) len)) (point-max))
463                    (and (< len (point-max))
464                         (goto-char len)
465                         (looking-at nndoc-article-begin)))))
466       (goto-char len))))
467
468 (defun nndoc-rnews-body-end ()
469   (and (re-search-backward nndoc-article-begin nil t)
470        (forward-line 1)
471        (goto-char (+ (point) (string-to-int (match-string 1))))))
472
473 (defun nndoc-transform-clari-briefs (article)
474   (goto-char (point-min))
475   (when (looking-at " *\\*\\(.*\\)\n")
476     (replace-match "" t t))
477   (nndoc-generate-clari-briefs-head article))
478
479 (defun nndoc-generate-clari-briefs-head (article)
480   (let ((entry (cdr (assq article nndoc-dissection-alist)))
481         subject from)
482     (save-excursion
483       (set-buffer nndoc-current-buffer)
484       (save-restriction
485         (narrow-to-region (car entry) (nth 3 entry))
486         (goto-char (point-min))
487         (when (looking-at " *\\*\\(.*\\)$")
488           (setq subject (match-string 1))
489           (when (string-match "[ \t]+$" subject)
490             (setq subject (substring subject 0 (match-beginning 0)))))
491         (when
492             (let ((case-fold-search nil))
493               (re-search-forward
494                "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
495           (setq from (match-string 1)))))
496     (insert "From: " "clari@clari.net (" (or from "unknown") ")"
497             "\nSubject: " (or subject "(no subject)") "\n")))
498
499 (defun nndoc-babyl-body-begin ()
500   (re-search-forward "^\n" nil t)
501   (when (looking-at "\*\*\* EOOH \*\*\*")
502     (re-search-forward "^\n" nil t)))
503
504 (provide 'nndoc)
505
506 ;;; nndoc.el ends here