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