*** 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      (head-begin . "^[0-9].*\n"))
63     (forward
64      (article-begin . "^-+ Start of forwarded message -+\n+")
65      (body-end . "^-+ End of forwarded message -+\n"))
66     (clari-briefs
67      (article-begin . "^ \\*")
68      (body-end . "^\t------*[ \t]^*\n^ \\*")
69      (body-begin . "^\t")
70      (head-end . "^\t")
71      (generate-head . nndoc-generate-clari-briefs-head)
72      (article-transform . nndoc-transform-clari-briefs))
73     (slack-digest
74      (article-begin . "^------------------------------*[\n \t]+")
75      (head-end . "^ ?$")
76      (body-end-function . nndoc-digest-body-end)
77      (body-begin . "^ ?$")
78      (file-end . "^End of")
79      (prepare-body . nndoc-prepare-digest-body))
80     (mime-digest
81      (article-begin . "")
82      (head-end . "^ ?$")
83      (body-end . "")
84      (file-end . ""))
85     (standard-digest
86      (first-article . ,(concat "^" (make-string 70 ?-) "\n\n"))
87      (article-begin . ,(concat "\n\n" (make-string 30 ?-) "\n\n"))
88      (prepare-body . nndoc-prepare-digest-body)
89      (body-end-function . nndoc-digest-body-end)
90      (file-end . "^End of .*digest.*[0-9].*\n\\*\\*"))
91     (guess 
92      (guess . nndoc-guess-type))
93     (digest
94      (guess . nndoc-guess-digest-type))
95     ))
96
97 \f
98
99 (defvar nndoc-file-begin nil)
100 (defvar nndoc-first-article nil)
101 (defvar nndoc-article-end nil)
102 (defvar nndoc-article-begin nil)
103 (defvar nndoc-head-begin nil)
104 (defvar nndoc-head-end nil)
105 (defvar nndoc-file-end nil)
106 (defvar nndoc-body-begin nil)
107 (defvar nndoc-body-end-function nil)
108 (defvar nndoc-body-end nil)
109 (defvar nndoc-dissection-alist nil)
110 (defvar nndoc-prepare-body nil)
111 (defvar nndoc-generate-head nil)
112 (defvar nndoc-article-transform nil)
113
114 (defvar nndoc-current-server nil)
115 (defvar nndoc-server-alist nil)
116 (defvar nndoc-server-variables
117   (list
118    (list 'nndoc-article-type nndoc-article-type)
119    '(nndoc-article-begin nil)
120    '(nndoc-article-end nil)
121    '(nndoc-head-begin nil)
122    '(nndoc-head-end nil)
123    '(nndoc-first-article nil)
124    '(nndoc-current-buffer nil)
125    '(nndoc-group-alist nil)
126    '(nndoc-end-of-file nil)
127    '(nndoc-body-begin nil)
128    '(nndoc-address nil)))
129
130 (defconst nndoc-version "nndoc 1.0"
131   "nndoc version.")
132
133 (defvar nndoc-current-buffer nil
134   "Current nndoc news buffer.")
135
136 (defvar nndoc-address nil)
137
138 \f
139
140 (defvar nndoc-status-string "")
141
142 (defvar nndoc-group-alist nil)
143
144 ;;; Interface functions
145
146 (defun nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
147   (when (nndoc-possibly-change-buffer newsgroup server)
148     (save-excursion
149       (set-buffer nntp-server-buffer)
150       (erase-buffer)
151       (let (article entry)
152         (if (stringp (car articles))
153             'headers
154           (while articles
155             (when (setq entry (cdr (assq (setq article (pop articles))
156                                          nndoc-dissection-alist)))
157               (insert (format "221 %d Article retrieved.\n" article))
158               (if nndoc-generate-head
159                   (funcall nndoc-generate-head article)
160                 (insert-buffer-substring
161                  nndoc-current-buffer (car entry) (nth 1 entry)))
162               (goto-char (point-max))
163               (or (= (char-after (1- (point))) ?\n) (insert "\n"))
164               (insert (format "Lines: %d\n" (nth 4 entry)))
165               (insert ".\n")))
166
167           (nnheader-fold-continuation-lines)
168           'headers)))))
169
170 (defun nndoc-open-server (server &optional defs)
171   (nnheader-init-server-buffer)
172   (if (equal server nndoc-current-server)
173       t
174     (if nndoc-current-server
175         (setq nndoc-server-alist 
176               (cons (list nndoc-current-server
177                           (nnheader-save-variables nndoc-server-variables))
178                     nndoc-server-alist)))
179     (let ((state (assoc server nndoc-server-alist)))
180       (if state 
181           (progn
182             (nnheader-restore-variables (nth 1 state))
183             (setq nndoc-server-alist (delq state nndoc-server-alist)))
184         (nnheader-set-init-variables nndoc-server-variables defs)))
185     (setq nndoc-current-server server)
186     t))
187
188 (defun nndoc-close-server (&optional server)
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     (if (not (and nndoc-current-buffer
301                   (not nndoc-dissection-alist)))
302         (nndoc-close-server)
303       (save-excursion
304         (set-buffer nndoc-current-buffer)
305         (nndoc-set-delims)
306         (nndoc-dissect-buffer)))
307     ;; Return whether we managed to select a file.
308     nndoc-current-buffer))
309
310 ;; MIME (RFC 1341) digest hack by Ulrik Dickow <dickow@nbi.dk>.
311 (defun nndoc-guess-digest-type ()
312   "Guess what digest type the current document is."
313   (let ((case-fold-search t)            ; We match a bit too much, keep it simple.
314         boundary-id b-delimiter entry)
315     (goto-char (point-min))
316     (cond 
317      ;; MIME digest.
318      ((and
319        (re-search-forward
320         (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
321                 "boundary=\"\\([^\"\n]*[^\" \t\n]\\)\"")
322         nil t)
323        (match-beginning 1))
324       (setq boundary-id (match-string 1)
325             b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
326       (setq entry (assq 'mime-digest nndoc-type-alist))
327       (setcdr entry
328               (list
329                (cons 'head-end "^ ?$")
330                (cons 'body-begin "^ ?\n")
331                (cons 'article-begin b-delimiter)
332                (cons 'body-end-function 'nndoc-digest-body-end)
333 ;              (cons 'body-end 
334 ;                    (concat "\n--" boundary-id "\\(--\\)?[\n \t]+"))
335                (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
336       'mime-digest)
337      ;; Standard digest.
338      ((and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
339            (re-search-forward 
340             (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
341       'standard-digest)
342      ;; Stupid digest.
343      (t
344       'slack-digest))))
345
346 (defun nndoc-guess-type ()
347   "Guess what document type is in the current buffer."
348   (goto-char (point-min))
349   (cond 
350    ((looking-at rmail-unix-mail-delimiter)
351     'mbox)
352    ((looking-at "\^A\^A\^A\^A$")
353     'mmdf)
354    ((looking-at "^Path:.*\n")
355     'news)
356    ((looking-at "#! *rnews")
357     'rnews)
358    ((re-search-forward "\^_\^L *\n" nil t)
359     'babyl)
360    ((save-excursion
361       (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
362            (not (re-search-forward "^Subject:.*digest" nil t))))
363     'forward)
364    ((let ((case-fold-search nil))
365       (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
366     'clari-briefs)
367    (t 
368     'digest)))
369
370 (defun nndoc-set-delims ()
371   "Set the nndoc delimiter variables according to the type of the document."
372   (let ((vars '(nndoc-file-begin 
373                 nndoc-first-article 
374                 nndoc-article-end nndoc-head-begin nndoc-head-end
375                 nndoc-file-end nndoc-article-begin
376                 nndoc-body-begin nndoc-body-end-function nndoc-body-end
377                 nndoc-prepare-body nndoc-article-transform
378                 nndoc-generate-head)))
379     (while vars
380       (set (pop vars) nil)))
381   (let* (defs guess)
382     ;; Guess away until we find the real file type.
383     (while (setq defs (cdr (assq nndoc-article-type nndoc-type-alist))
384                  guess (assq 'guess defs))
385       (setq nndoc-article-type (funcall (cdr guess))))
386     ;; Set the nndoc variables.
387     (while defs
388       (set (intern (format "nndoc-%s" (car (car defs))))
389            (cdr (pop defs))))))
390
391 (defun nndoc-search (regexp)
392   (prog1
393       (re-search-forward regexp nil t)
394     (beginning-of-line)))
395
396 (defun nndoc-dissect-buffer ()
397   "Go through the document and partition it into heads/bodies/articles."
398   (let ((i 0)
399         (first t)
400         head-begin head-end body-begin body-end)
401     (setq nndoc-dissection-alist nil)
402     (save-excursion
403       (set-buffer nndoc-current-buffer)
404       (goto-char (point-min))
405       ;; Find the beginning of the file.
406       (when nndoc-file-begin
407         (nndoc-search nndoc-file-begin))
408       ;; Go through the file.
409       (while (if (and first nndoc-first-article)
410                  (nndoc-search nndoc-first-article)
411                (nndoc-search nndoc-article-begin))
412         (setq first nil)
413         (when nndoc-head-begin 
414           (nndoc-search nndoc-head-begin))
415         (if (and nndoc-file-end
416                  (looking-at nndoc-file-end))
417             (goto-char (point-max))
418           (setq head-begin (point))
419           (nndoc-search (or nndoc-head-end "^$"))
420           (setq head-end (point))
421           (nndoc-search (or nndoc-body-begin "^\n"))
422           (setq body-begin (point))
423           (or (and nndoc-body-end-function
424                    (funcall nndoc-body-end-function))
425               (and nndoc-body-end
426                    (nndoc-search nndoc-body-end))
427               (nndoc-search nndoc-article-begin)
428               (progn
429                 (goto-char (point-max))
430                 (when nndoc-file-end
431                   (and (re-search-backward nndoc-file-end nil t)
432                        (beginning-of-line)))))
433           (setq body-end (point))
434           (push (list (incf i) head-begin head-end body-begin body-end
435                       (count-lines body-begin body-end))
436                 nndoc-dissection-alist))))))
437
438 (defun nndoc-prepare-digest-body ()
439   "Unquote quoted non-separators in digests."
440   (while (re-search-forward "^- -"nil t)
441     (replace-match "-" t t)))
442
443 (defun nndoc-digest-body-end ()
444   (and (re-search-forward nndoc-article-begin nil t)
445        (goto-char (match-beginning 0))))
446
447 (defun nndoc-mbox-body-end ()
448   (let ((beg (point))
449         len end)
450     (when
451         (save-excursion
452           (and (re-search-backward nndoc-article-begin nil t)
453                (setq end (point))
454                (search-forward "\n\n" beg t)
455                (re-search-backward
456                 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
457                (setq len (string-to-int (match-string 1)))
458                (search-forward "\n\n" beg t)
459                (or (= (setq len (+ (point) len)) (point-max))
460                    (and (< len (point-max))
461                         (goto-char len)
462                         (looking-at nndoc-article-begin)))))
463       (goto-char len))))
464
465 (defun nndoc-rnews-body-end ()
466   (and (re-search-backward nndoc-article-begin nil t)
467        (forward-line 1)
468        (goto-char (+ (point) (string-to-int (match-string 1))))))
469
470 (defun nndoc-transform-clari-briefs (article)
471   (goto-char (point-min))
472   (when (looking-at " *\\*\\(.*\\)\n")
473     (replace-match "" t t))
474   (nndoc-generate-clari-briefs-head article))
475
476 (defun nndoc-generate-clari-briefs-head (article)
477   (let ((entry (cdr (assq article nndoc-dissection-alist)))
478         subject from)
479     (save-excursion
480       (set-buffer nndoc-current-buffer)
481       (save-restriction
482         (narrow-to-region (car entry) (nth 3 entry))
483         (goto-char (point-min))
484         (when (looking-at " *\\*\\(.*\\)$")
485           (setq subject (match-string 1)))
486         (when
487             (let ((case-fold-search nil))
488               (re-search-forward
489                "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
490           (setq from (match-string 1)))))
491     (insert "From: " "clari@clari.net (" (or from "unknown") ")"
492             "\nSubject: " (or subject "(no subject)") "\n")))
493
494 (provide 'nndoc)
495
496 ;;; nndoc.el ends here