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