*** 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 (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      (prepare-body . nndoc-unquote-dashes))
68     (clari-briefs
69      (article-begin . "^ \\*")
70      (body-end . "^\t------*[ \t]^*\n^ \\*")
71      (body-begin . "^\t")
72      (head-end . "^\t")
73      (generate-head . nndoc-generate-clari-briefs-head)
74      (article-transform . nndoc-transform-clari-briefs))
75     (slack-digest
76      (article-begin . "^------------------------------*[\n \t]+")
77      (head-end . "^ ?$")
78      (body-end-function . nndoc-digest-body-end)
79      (body-begin . "^ ?$")
80      (file-end . "^End of")
81      (prepare-body . nndoc-unquote-dashes))
82     (mime-digest
83      (article-begin . "")
84      (head-end . "^ ?$")
85      (body-end . "")
86      (file-end . ""))
87     (standard-digest
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)
92      (head-end . "^ ?$")
93      (body-begin . "^ ?\n")
94      (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$"))
95     (guess 
96      (guess . nndoc-guess-type))
97     (digest
98      (guess . nndoc-guess-digest-type))
99     ))
100
101 \f
102
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)
118
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)
124
125 \f
126
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)
134     (nndoc-head-end 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)))
153
154 (defconst nndoc-version "nndoc 1.0"
155   "nndoc version.")
156
157 \f
158
159 ;;; Interface functions
160
161 (defun nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
162   (when (nndoc-possibly-change-buffer newsgroup server)
163     (save-excursion
164       (set-buffer nntp-server-buffer)
165       (erase-buffer)
166       (let (article entry)
167         (if (stringp (car articles))
168             'headers
169           (while 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)))
180               (insert ".\n")))
181
182           (nnheader-fold-continuation-lines)
183           'headers)))))
184
185 (defun nndoc-open-server (server &optional defs)
186   (nnheader-change-server 'nndoc server defs))
187
188 (defun nndoc-close-server (&optional server)
189   (setq nndoc-current-server nil)
190   t)
191
192 (defun nndoc-server-opened (&optional server)
193   (and (equal server nndoc-current-server)
194        nntp-server-buffer
195        (buffer-name nntp-server-buffer)))
196
197 (defun nndoc-status-message (&optional server)
198   nndoc-status-string)
199
200 (defun nndoc-request-article (article &optional newsgroup server buffer)
201   (nndoc-possibly-change-buffer newsgroup server)
202   (save-excursion
203     (let ((buffer (or buffer nntp-server-buffer))
204           (entry (cdr (assq article nndoc-dissection-alist)))
205           beg)
206       (set-buffer buffer)
207       (erase-buffer)
208       (if (stringp article)
209           nil
210         (insert-buffer-substring 
211          nndoc-current-buffer (car entry) (nth 1 entry))
212         (insert "\n")
213         (setq beg (point))
214         (insert-buffer-substring 
215          nndoc-current-buffer (nth 2 entry) (nth 3 entry))
216         (goto-char beg)
217         (when nndoc-prepare-body
218           (funcall nndoc-prepare-body))
219         (when nndoc-article-transform
220           (funcall nndoc-article-transform article))
221         t))))
222
223 (defun nndoc-request-group (group &optional server dont-check)
224   "Select news GROUP."
225   (let (number)
226     (cond 
227      ((not (nndoc-possibly-change-buffer group server))
228       (nnheader-report 'nndoc "No such file or buffer: %s"
229                        nndoc-address))
230      (dont-check
231       (nnheader-report 'nndoc "Selected group %s" group)
232       t)
233      ((zerop (setq number (length nndoc-dissection-alist)))
234       (nndoc-close-group group)
235       (nnheader-report 'nndoc "No articles in group %s" group))
236      (t
237       (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
238
239 (defun nndoc-request-type (group &optional article)
240   (cond ((not article) 'unknown)
241         (nndoc-post-type nndoc-post-type)
242         (t 'unknown)))
243
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)
250                                 nndoc-group-alist))
251   (setq nndoc-current-buffer nil)
252   (setq nndoc-current-server nil)
253   (setq nndoc-dissection-alist nil)
254   t)
255
256 (defun nndoc-request-list (&optional server)
257   nil)
258
259 (defun nndoc-request-newgroups (date &optional server)
260   nil)
261
262 (defun nndoc-request-list-newsgroups (&optional server)
263   nil)
264
265 (defalias 'nndoc-request-post 'nnmail-request-post)
266
267 \f
268 ;;; Internal functions.
269
270 (defun nndoc-possibly-change-buffer (group source)
271   (let (buf)
272     (cond 
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. 
280      (buf
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 
289                               (get-buffer-create 
290                                (concat " *nndoc " group "*"))))
291             nndoc-group-alist)
292       (setq nndoc-dissection-alist nil)
293       (save-excursion
294         (set-buffer nndoc-current-buffer)
295         (buffer-disable-undo (current-buffer))
296         (erase-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))
303       (save-excursion
304         (set-buffer nndoc-current-buffer)
305         (nndoc-set-delims)
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))
311
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))
318     (cond 
319      ;; MIME digest.
320      ((and
321        (re-search-forward
322         (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
323                 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
324         nil t)
325        (match-beginning 1))
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))
329       (setcdr entry
330               (list
331                (cons 'head-end "^ ?$")
332                (cons 'body-begin "^ ?\n")
333                (cons 'article-begin b-delimiter)
334                (cons 'body-end-function 'nndoc-digest-body-end)
335 ;              (cons 'body-end 
336 ;                    (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
337                (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
338       'mime-digest)
339      ;; Standard digest.
340      ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
341            (re-search-forward 
342             (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
343       'standard-digest)
344      ;; Stupid digest.
345      (t
346       'slack-digest))))
347
348 (defun nndoc-guess-type ()
349   "Guess what document type is in the current buffer."
350   (goto-char (point-min))
351   (cond 
352    ((looking-at rmail-unix-mail-delimiter)
353     'mbox)
354    ((looking-at "\^A\^A\^A\^A$")
355     'mmdf)
356    ((looking-at "^Path:.*\n")
357     'news)
358    ((looking-at "#! *rnews")
359     'rnews)
360    ((re-search-forward "\^_\^L *\n" nil t)
361     'babyl)
362    ((save-excursion
363       (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
364            (not (re-search-forward "^Subject:.*digest" nil t))))
365     'forward)
366    ((let ((case-fold-search nil))
367       (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
368     'clari-briefs)
369    (t 
370     'digest)))
371
372 (defun nndoc-set-delims ()
373   "Set the nndoc delimiter variables according to the type of the document."
374   (let ((vars '(nndoc-file-begin 
375                 nndoc-first-article 
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)))
381     (while vars
382       (set (pop vars) nil)))
383   (let* (defs guess)
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.
389     (while defs
390       (set (intern (format "nndoc-%s" (caar defs)))
391            (cdr (pop defs))))))
392
393 (defun nndoc-search (regexp)
394   (prog1
395       (re-search-forward regexp nil t)
396     (beginning-of-line)))
397
398 (defun nndoc-dissect-buffer ()
399   "Go through the document and partition it into heads/bodies/articles."
400   (let ((i 0)
401         (first t)
402         head-begin head-end body-begin body-end)
403     (setq nndoc-dissection-alist nil)
404     (save-excursion
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))
414         (setq first nil)
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))
429               (and nndoc-body-end
430                    (nndoc-search nndoc-body-end))
431               (nndoc-search nndoc-article-begin)
432               (progn
433                 (goto-char (point-max))
434                 (when nndoc-file-end
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))))))
441
442 (defun nndoc-unquote-dashes ()
443   "Unquote quoted non-separators in digests."
444   (while (re-search-forward "^- -"nil t)
445     (replace-match "-" t t)))
446
447 (defun nndoc-digest-body-end ()
448   (and (re-search-forward nndoc-article-begin nil t)
449        (goto-char (match-beginning 0))))
450
451 (defun nndoc-mbox-body-end ()
452   (let ((beg (point))
453         len end)
454     (when
455         (save-excursion
456           (and (re-search-backward nndoc-article-begin nil t)
457                (setq end (point))
458                (search-forward "\n\n" beg t)
459                (re-search-backward
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))
465                         (goto-char len)
466                         (looking-at nndoc-article-begin)))))
467       (goto-char len))))
468
469 (defun nndoc-rnews-body-end ()
470   (and (re-search-backward nndoc-article-begin nil t)
471        (forward-line 1)
472        (goto-char (+ (point) (string-to-int (match-string 1))))))
473
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))
479
480 (defun nndoc-generate-clari-briefs-head (article)
481   (let ((entry (cdr (assq article nndoc-dissection-alist)))
482         subject from)
483     (save-excursion
484       (set-buffer nndoc-current-buffer)
485       (save-restriction
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)))))
492         (when
493             (let ((case-fold-search nil))
494               (re-search-forward
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")))
499
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)))
504
505 (provide 'nndoc)
506
507 ;;; nndoc.el ends here