* lisp/{rfc2047.el,nnweb.el,nnultimate.el,nntp.el,nnslashdot.el,
[elisp/gnus.git-] / lisp / nndoc.el
1 ;;; nndoc.el --- single file access for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 (eval-when-compile (require 'cl))
30 (require 'nnheader)
31 (require 'message)
32 (require 'nnmail)
33 (require 'nnoo)
34 (require 'gnus-util)
35
36 (nnoo-declare nndoc)
37
38 (defvoo nndoc-article-type 'guess
39   "*Type of the file.
40 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
41 `rfc934', `rfc822-forward', `mime-parts', `standard-digest',
42 `slack-digest', `clari-briefs', `nsmail' or `guess'.")
43
44 (defvoo nndoc-post-type 'mail
45   "*Whether the nndoc group is `mail' or `post'.")
46
47 (defvoo nndoc-open-document-hook 'nnheader-ms-strip-cr
48   "Hook run after opening a document.
49 The default function removes all trailing carriage returns
50 from the document.")
51
52 (defvar nndoc-type-alist
53   `((mmdf
54      (article-begin .  "^\^A\^A\^A\^A\n")
55      (body-end .  "^\^A\^A\^A\^A\n"))
56     (nsmail
57      (article-begin .  "^From - "))
58     (news
59      (article-begin . "^Path:"))
60     (rnews
61      (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
62      (body-end-function . nndoc-rnews-body-end))
63     (mbox
64      (article-begin-function . nndoc-mbox-article-begin)
65      (body-end-function . nndoc-mbox-body-end))
66     (babyl
67      (article-begin . "\^_\^L *\n")
68      (body-end . "\^_")
69      (body-begin-function . nndoc-babyl-body-begin)
70      (head-begin-function . nndoc-babyl-head-begin))
71     (forward
72      (article-begin . "^-+ Start of forwarded message -+\n+")
73      (body-end . "^-+ End of forwarded message -+$")
74      (prepare-body-function . nndoc-unquote-dashes))
75     (rfc934
76      (article-begin . "^--.*\n+")
77      (body-end . "^--.*$")
78      (prepare-body-function . nndoc-unquote-dashes))
79     (clari-briefs
80      (article-begin . "^ \\*")
81      (body-end . "^\t------*[ \t]^*\n^ \\*")
82      (body-begin . "^\t")
83      (head-end . "^\t")
84      (generate-head-function . nndoc-generate-clari-briefs-head)
85      (article-transform-function . nndoc-transform-clari-briefs))
86     (mime-digest
87      (article-begin . "")
88      (head-end . "^ ?$")
89      (body-end . "")
90      (file-end . "")
91      (subtype digest guess))
92     (mime-parts
93      (generate-head-function . nndoc-generate-mime-parts-head)
94      (article-transform-function . nndoc-transform-mime-parts))
95     (standard-digest
96      (first-article . ,(concat "^" (make-string 70 ?-) "\n *\n+"))
97      (article-begin . ,(concat "^\n" (make-string 30 ?-) "\n *\n+"))
98      (prepare-body-function . nndoc-unquote-dashes)
99      (body-end-function . nndoc-digest-body-end)
100      (head-end . "^ *$")
101      (body-begin . "^ *\n")
102      (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$")
103      (subtype digest guess))
104     (slack-digest
105      (article-begin . "^------------------------------*[\n \t]+")
106      (head-end . "^ ?$")
107      (body-end-function . nndoc-digest-body-end)
108      (body-begin . "^ ?$")
109      (file-end . "^End of")
110      (prepare-body-function . nndoc-unquote-dashes)
111      (subtype digest guess))
112     (lanl-gov-announce
113      (article-begin . "^\\\\\\\\\n")
114      (head-begin . "^Paper.*:")
115      (head-end   . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
116      (body-begin . "")
117      (body-end   . "-------------------------------------------------")
118      (file-end   . "^Title: Recent Seminal")
119      (generate-head-function . nndoc-generate-lanl-gov-head)
120      (article-transform-function . nndoc-transform-lanl-gov-announce)
121      (subtype preprints guess))
122     (rfc822-forward
123      (article-begin . "^\n")
124      (body-end-function . nndoc-rfc822-forward-body-end-function))
125     (guess
126      (guess . t)
127      (subtype nil))
128     (digest
129      (guess . t)
130      (subtype nil))
131     (preprints
132      (guess . t)
133      (subtype nil))))
134
135 \f
136 (defvoo nndoc-file-begin nil)
137 (defvoo nndoc-first-article nil)
138 (defvoo nndoc-article-begin nil)
139 (defvoo nndoc-head-begin nil)
140 (defvoo nndoc-head-end nil)
141 (defvoo nndoc-file-end nil)
142 (defvoo nndoc-body-begin nil)
143 (defvoo nndoc-body-end-function nil)
144 (defvoo nndoc-body-begin-function nil)
145 (defvoo nndoc-head-begin-function nil)
146 (defvoo nndoc-body-end nil)
147 ;; nndoc-dissection-alist is a list of sublists.  Each sublist holds the
148 ;; following items.  ARTICLE acts as the association key and is an ordinal
149 ;; starting at 1.  HEAD-BEGIN [0], HEAD-END [1], BODY-BEGIN [2] and BODY-END
150 ;; [3] are positions in the `nndoc' buffer.  LINE-COUNT [4] is a count of
151 ;; lines in the body.  For MIME dissections only, ARTICLE-INSERT [5] and
152 ;; SUMMARY-INSERT [6] give headers to insert for full article or summary line
153 ;; generation, respectively.  Other headers usually follow directly from the
154 ;; buffer.  Value `nil' means no insert.
155 (defvoo nndoc-dissection-alist nil)
156 (defvoo nndoc-prepare-body-function nil)
157 (defvoo nndoc-generate-head-function nil)
158 (defvoo nndoc-article-transform-function nil)
159 (defvoo nndoc-article-begin-function nil)
160
161 (defvoo nndoc-status-string "")
162 (defvoo nndoc-group-alist nil)
163 (defvoo nndoc-current-buffer nil
164   "Current nndoc news buffer.")
165 (defvoo nndoc-address nil)
166
167 (defconst nndoc-version "nndoc 1.0"
168   "nndoc version.")
169
170 \f
171
172 ;;; Interface functions
173
174 (nnoo-define-basics nndoc)
175
176 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
177   (when (nndoc-possibly-change-buffer newsgroup server)
178     (save-excursion
179       (set-buffer nntp-server-buffer)
180       (erase-buffer)
181       (let (article entry)
182         (if (stringp (car articles))
183             'headers
184           (while articles
185             (when (setq entry (cdr (assq (setq article (pop articles))
186                                          nndoc-dissection-alist)))
187               (insert (format "221 %d Article retrieved.\n" article))
188               (if nndoc-generate-head-function
189                   (funcall nndoc-generate-head-function article)
190                 (insert-buffer-substring
191                  nndoc-current-buffer (car entry) (nth 1 entry)))
192               (goto-char (point-max))
193               (unless (eq (char-after (1- (point))) ?\n)
194                 (insert "\n"))
195               (insert (format "Lines: %d\n" (nth 4 entry)))
196               (insert ".\n")))
197
198           (nnheader-fold-continuation-lines)
199           'headers)))))
200
201 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
202   (nndoc-possibly-change-buffer newsgroup server)
203   (save-excursion
204     (let ((buffer (or buffer nntp-server-buffer))
205           (entry (cdr (assq article nndoc-dissection-alist)))
206           beg)
207       (set-buffer buffer)
208       (erase-buffer)
209       (when entry
210         (if (stringp article)
211             nil
212           (insert-buffer-substring
213            nndoc-current-buffer (car entry) (nth 1 entry))
214           (insert "\n")
215           (setq beg (point))
216           (insert-buffer-substring
217            nndoc-current-buffer (nth 2 entry) (nth 3 entry))
218           (goto-char beg)
219           (when nndoc-prepare-body-function
220             (funcall nndoc-prepare-body-function))
221           (when nndoc-article-transform-function
222             (funcall nndoc-article-transform-function article))
223           t)))))
224
225 (deffoo nndoc-request-group (group &optional server dont-check)
226   "Select news GROUP."
227   (let (number)
228     (cond
229      ((not (nndoc-possibly-change-buffer group server))
230       (nnheader-report 'nndoc "No such file or buffer: %s"
231                        nndoc-address))
232      (dont-check
233       (nnheader-report 'nndoc "Selected group %s" group)
234       t)
235      ((zerop (setq number (length nndoc-dissection-alist)))
236       (nndoc-close-group group)
237       (nnheader-report 'nndoc "No articles in group %s" group))
238      (t
239       (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
240
241 (deffoo nndoc-request-type (group &optional article)
242   (cond ((not article) 'unknown)
243         (nndoc-post-type nndoc-post-type)
244         (t 'unknown)))
245
246 (deffoo nndoc-close-group (group &optional server)
247   (nndoc-possibly-change-buffer group server)
248   (and nndoc-current-buffer
249        (buffer-name nndoc-current-buffer)
250        (kill-buffer nndoc-current-buffer))
251   (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
252                                 nndoc-group-alist))
253   (setq nndoc-current-buffer nil)
254   (nnoo-close-server 'nndoc server)
255   (setq nndoc-dissection-alist nil)
256   t)
257
258 (deffoo nndoc-request-list (&optional server)
259   nil)
260
261 (deffoo nndoc-request-newgroups (date &optional server)
262   nil)
263
264 (deffoo nndoc-request-list-newsgroups (&optional server)
265   nil)
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         (mm-enable-multibyte)
296         (erase-buffer)
297         (if (stringp nndoc-address)
298             (nnheader-insert-file-contents nndoc-address)
299           (insert-buffer-substring nndoc-address))
300         (run-hooks 'nndoc-open-document-hook))))
301     ;; Initialize the nndoc structures according to this new document.
302     (when (and nndoc-current-buffer
303                (not nndoc-dissection-alist))
304       (save-excursion
305         (set-buffer nndoc-current-buffer)
306         (nndoc-set-delims)
307         (if (eq nndoc-article-type 'mime-parts)
308             (nndoc-dissect-mime-parts)
309           (nndoc-dissect-buffer))))
310     (unless nndoc-current-buffer
311       (nndoc-close-server))
312     ;; Return whether we managed to select a file.
313     nndoc-current-buffer))
314
315 ;;;
316 ;;; Deciding what document type we have
317 ;;;
318
319 (defun nndoc-set-delims ()
320   "Set the nndoc delimiter variables according to the type of the document."
321   (let ((vars '(nndoc-file-begin
322                 nndoc-first-article
323                 nndoc-article-begin-function
324                 nndoc-head-begin nndoc-head-end
325                 nndoc-file-end nndoc-article-begin
326                 nndoc-body-begin nndoc-body-end-function nndoc-body-end
327                 nndoc-prepare-body-function nndoc-article-transform-function
328                 nndoc-generate-head-function nndoc-body-begin-function
329                 nndoc-head-begin-function)))
330     (while vars
331       (set (pop vars) nil)))
332   (let (defs)
333     ;; Guess away until we find the real file type.
334     (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
335                                               nndoc-type-alist))))
336       (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
337     ;; Set the nndoc variables.
338     (while defs
339       (set (intern (format "nndoc-%s" (caar defs)))
340            (cdr (pop defs))))))
341
342 (defun nndoc-guess-type (subtype)
343   (let ((alist nndoc-type-alist)
344         results result entry)
345     (while (and (not result)
346                 (setq entry (pop alist)))
347       (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
348         (goto-char (point-min))
349         ;; Remove blank lines.
350         (while (eq (following-char) ?\n)
351           (delete-char 1))
352         (when (numberp (setq result (funcall (intern
353                                               (format "nndoc-%s-type-p"
354                                                       (car entry))))))
355           (push (cons result entry) results)
356           (setq result nil))))
357     (unless (or result results)
358       (error "Document is not of any recognized type"))
359     (if result
360         (car entry)
361       (cadar (sort results 'car-less-than-car)))))
362
363 ;;;
364 ;;; Built-in type predicates and functions
365 ;;;
366
367 (defun nndoc-mbox-type-p ()
368   (when (looking-at message-unix-mail-delimiter)
369     t))
370
371 (defun nndoc-mbox-article-begin ()
372   (when (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
373     (goto-char (match-beginning 0))))
374
375 (defun nndoc-mbox-body-end ()
376   (let ((beg (point))
377         len end)
378     (when
379         (save-excursion
380           (and (re-search-backward
381                 (concat "^" message-unix-mail-delimiter) nil t)
382                (setq end (point))
383                (search-forward "\n\n" beg t)
384                (re-search-backward
385                 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
386                (setq len (string-to-int (match-string 1)))
387                (search-forward "\n\n" beg t)
388                (unless (= (setq len (+ (point) len)) (point-max))
389                  (and (< len (point-max))
390                       (goto-char len)
391                       (looking-at message-unix-mail-delimiter)))))
392       (goto-char len))))
393
394 (defun nndoc-mmdf-type-p ()
395   (when (looking-at "\^A\^A\^A\^A$")
396     t))
397
398 (defun nndoc-news-type-p ()
399   (when (looking-at "^Path:.*\n")
400     t))
401
402 (defun nndoc-rnews-type-p ()
403   (when (looking-at "#! *rnews")
404     t))
405
406 (defun nndoc-rnews-body-end ()
407   (and (re-search-backward nndoc-article-begin nil t)
408        (forward-line 1)
409        (goto-char (+ (point) (string-to-int (match-string 1))))))
410
411 (defun nndoc-babyl-type-p ()
412   (when (re-search-forward "\^_\^L *\n" nil t)
413     t))
414
415 (defun nndoc-babyl-body-begin ()
416   (re-search-forward "^\n" nil t)
417   (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
418     (let ((next (or (save-excursion
419                       (re-search-forward nndoc-article-begin nil t))
420                     (point-max))))
421       (unless (re-search-forward "^\n" next t)
422         (goto-char next)
423         (forward-line -1)
424         (insert "\n")
425         (forward-line -1)))))
426
427 (defun nndoc-babyl-head-begin ()
428   (when (re-search-forward "^[0-9].*\n" nil t)
429     (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
430       (forward-line 1))
431     t))
432
433 (defun nndoc-forward-type-p ()
434   (when (and (re-search-forward "^-+ Start of forwarded message -+\n+" nil t)
435              (not (re-search-forward "^Subject:.*digest" nil t))
436              (not (re-search-backward "^From:" nil t 2))
437              (not (re-search-forward "^From:" nil t 2)))
438     t))
439
440 (defun nndoc-rfc934-type-p ()
441   (when (and (re-search-forward "^-+ Start of forwarded.*\n+" nil t)
442              (not (re-search-forward "^Subject:.*digest" nil t))
443              (not (re-search-backward "^From:" nil t 2))
444              (not (re-search-forward "^From:" nil t 2)))
445     t))
446
447 (defun nndoc-rfc822-forward-type-p ()
448   (save-restriction
449     (message-narrow-to-head)
450     (when (re-search-forward "^Content-Type: *message/rfc822" nil t)
451       t)))
452
453 (defun nndoc-rfc822-forward-body-end-function ()
454   (goto-char (point-max)))
455
456 (defun nndoc-mime-parts-type-p ()
457   (let ((case-fold-search t)
458         (limit (search-forward "\n\n" nil t)))
459     (goto-char (point-min))
460     (when (and limit
461                (re-search-forward
462                 (concat "\
463 ^Content-Type:[ \t]*multipart/[a-z]+ *; *\\(\\(\n[ \t]\\)?.*;\\)*"
464                         "\\(\n[ \t]\\)?[ \t]*boundary=\"?[^\"\n]*[^\" \t\n]")
465                 limit t))
466       t)))
467
468 (defun nndoc-transform-mime-parts (article)
469   (let* ((entry (cdr (assq article nndoc-dissection-alist)))
470          (headers (nth 5 entry)))
471     (when headers
472       (goto-char (point-min))
473       (insert headers))))
474
475 (defun nndoc-generate-mime-parts-head (article)
476   (let* ((entry (cdr (assq article nndoc-dissection-alist)))
477          (headers (nth 6 entry)))
478     (save-restriction
479       (narrow-to-region (point) (point))
480       (insert-buffer-substring
481        nndoc-current-buffer (car entry) (nth 1 entry))
482       (goto-char (point-max)))
483     (when headers
484       (insert headers))))
485
486 (defun nndoc-clari-briefs-type-p ()
487   (when (let ((case-fold-search nil))
488           (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
489     t))
490
491 (defun nndoc-transform-clari-briefs (article)
492   (goto-char (point-min))
493   (when (looking-at " *\\*\\(.*\\)\n")
494     (replace-match "" t t))
495   (nndoc-generate-clari-briefs-head article))
496
497 (defun nndoc-generate-clari-briefs-head (article)
498   (let ((entry (cdr (assq article nndoc-dissection-alist)))
499         subject from)
500     (save-excursion
501       (set-buffer nndoc-current-buffer)
502       (save-restriction
503         (narrow-to-region (car entry) (nth 3 entry))
504         (goto-char (point-min))
505         (when (looking-at " *\\*\\(.*\\)$")
506           (setq subject (match-string 1))
507           (when (string-match "[ \t]+$" subject)
508             (setq subject (substring subject 0 (match-beginning 0)))))
509         (when
510             (let ((case-fold-search nil))
511               (re-search-forward
512                "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
513           (setq from (match-string 1)))))
514     (insert "From: " "clari@clari.net (" (or from "unknown") ")"
515             "\nSubject: " (or subject "(no subject)") "\n")))
516
517
518 (defun nndoc-mime-digest-type-p ()
519   (let ((case-fold-search t)
520         boundary-id b-delimiter entry)
521     (when (and
522            (re-search-forward
523             (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
524                     "boundary=\"?\\([^\"\n]*[^\" \t\n]\\)")
525             nil t)
526            (match-beginning 1))
527       (setq boundary-id (match-string 1)
528             b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
529       (setq entry (assq 'mime-digest nndoc-type-alist))
530       (setcdr entry
531               (list
532                (cons 'head-end "^ ?$")
533                (cons 'body-begin "^ ?\n")
534                (cons 'article-begin b-delimiter)
535                (cons 'body-end-function 'nndoc-digest-body-end)
536                (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
537       t)))
538
539 (defun nndoc-standard-digest-type-p ()
540   (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
541              (re-search-forward
542               (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
543     t))
544
545 (defun nndoc-digest-body-end ()
546   (and (re-search-forward nndoc-article-begin nil t)
547        (goto-char (match-beginning 0))))
548
549 (defun nndoc-slack-digest-type-p ()
550   0)
551
552 (defun nndoc-lanl-gov-announce-type-p ()
553   (when (let ((case-fold-search nil))
554           (re-search-forward "^\\\\\\\\\nPaper: [a-z-]+/[0-9]+" nil t))
555     t))
556
557 (defun nndoc-transform-lanl-gov-announce (article)
558   (goto-char (point-max))
559   (when (re-search-backward "^\\\\\\\\ +(\\([^ ]*\\) , *\\([^ ]*\\))" nil t)
560     (replace-match "\n\nGet it at \\1 (\\2)" t nil))
561   ;;  (when (re-search-backward "^\\\\\\\\$" nil t)
562   ;;    (replace-match "" t t))
563   )
564
565 (defun nndoc-generate-lanl-gov-head (article)
566   (let ((entry (cdr (assq article nndoc-dissection-alist)))
567         (e-mail "no address given")
568         subject from)
569     (save-excursion
570       (set-buffer nndoc-current-buffer)
571       (save-restriction
572         (narrow-to-region (car entry) (nth 1 entry))
573         (goto-char (point-min))
574         (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
575           (setq subject (concat " (" (match-string 1) ")"))
576           (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
577             (setq e-mail (match-string 1)))
578           (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
579                                    nil t)
580             (setq subject (concat (match-string 1) subject))
581             (setq from (concat (match-string 2) " <" e-mail ">"))))
582         ))
583     (while (and from (string-match "(\[^)\]*)" from))
584       (setq from (replace-match "" t t from)))
585     (insert "From: "  (or from "unknown")
586             "\nSubject: " (or subject "(no subject)") "\n")))
587
588 (defun nndoc-nsmail-type-p ()
589   (when (looking-at "From - ")
590     t))
591
592 (deffoo nndoc-request-accept-article (group &optional server last)
593   nil)
594
595
596 ;;;
597 ;;; Functions for dissecting the documents
598 ;;;
599
600 (defun nndoc-search (regexp)
601   (prog1
602       (re-search-forward regexp nil t)
603     (beginning-of-line)))
604
605 (defun nndoc-dissect-buffer ()
606   "Go through the document and partition it into heads/bodies/articles."
607   (let ((i 0)
608         (first t)
609         head-begin head-end body-begin body-end)
610     (setq nndoc-dissection-alist nil)
611     (save-excursion
612       (set-buffer nndoc-current-buffer)
613       (goto-char (point-min))
614       ;; Remove blank lines.
615       (while (eq (following-char) ?\n)
616         (delete-char 1))
617       ;; Find the beginning of the file.
618       (when nndoc-file-begin
619         (nndoc-search nndoc-file-begin))
620       ;; Go through the file.
621       (while (if (and first nndoc-first-article)
622                  (nndoc-search nndoc-first-article)
623                (nndoc-article-begin))
624         (setq first nil)
625         (cond (nndoc-head-begin-function
626                (funcall nndoc-head-begin-function))
627               (nndoc-head-begin
628                (nndoc-search nndoc-head-begin)))
629         (if (or (eobp)
630                 (and nndoc-file-end
631                      (looking-at nndoc-file-end)))
632             (goto-char (point-max))
633           (setq head-begin (point))
634           (nndoc-search (or nndoc-head-end "^$"))
635           (setq head-end (point))
636           (if nndoc-body-begin-function
637               (funcall nndoc-body-begin-function)
638             (nndoc-search (or nndoc-body-begin "^\n")))
639           (setq body-begin (point))
640           (or (and nndoc-body-end-function
641                    (funcall nndoc-body-end-function))
642               (and nndoc-body-end
643                    (nndoc-search nndoc-body-end))
644               (nndoc-article-begin)
645               (progn
646                 (goto-char (point-max))
647                 (when nndoc-file-end
648                   (and (re-search-backward nndoc-file-end nil t)
649                        (beginning-of-line)))))
650           (setq body-end (point))
651           (push (list (incf i) head-begin head-end body-begin body-end
652                       (count-lines body-begin body-end))
653                 nndoc-dissection-alist))))))
654
655 (defun nndoc-article-begin ()
656   (if nndoc-article-begin-function
657       (funcall nndoc-article-begin-function)
658     (ignore-errors
659       (nndoc-search nndoc-article-begin))))
660
661 (defun nndoc-unquote-dashes ()
662   "Unquote quoted non-separators in digests."
663   (while (re-search-forward "^- -"nil t)
664     (replace-match "-" t t)))
665
666 ;; Against compiler warnings.
667 (defvar nndoc-mime-split-ordinal)
668
669 (defun nndoc-dissect-mime-parts ()
670   "Go through a MIME composite article and partition it into sub-articles.
671 When a MIME entity contains sub-entities, dissection produces one article for
672 the header of this entity, and one article per sub-entity."
673   (setq nndoc-dissection-alist nil
674         nndoc-mime-split-ordinal 0)
675   (save-excursion
676     (set-buffer nndoc-current-buffer)
677     (nndoc-dissect-mime-parts-sub (point-min) (point-max) nil nil nil)))
678
679 (defun nndoc-dissect-mime-parts-sub (head-begin body-end article-insert
680                                                 position parent)
681   "Dissect an entity, within a composite MIME message.
682 The complete message or MIME entity extends from HEAD-BEGIN to BODY-END.
683 ARTICLE-INSERT should be added at beginning for generating a full article.
684 The string POSITION holds a dotted decimal representation of the article
685 position in the hierarchical structure, it is nil for the outer entity.
686 PARENT is the message-ID of the parent summary line, or nil for none."
687   (let ((case-fold-search t)
688         (message-id (nnmail-message-id))
689         head-end body-begin summary-insert message-rfc822 multipart-any
690         subject content-type type subtype boundary-regexp)
691     ;; Gracefully handle a missing body.
692     (goto-char head-begin)
693     (if (search-forward "\n\n" body-end t)
694         (setq head-end (1- (point))
695               body-begin (point))
696       (setq head-end body-end
697             body-begin body-end))
698     (narrow-to-region head-begin head-end)
699     ;; Save MIME attributes.
700     (goto-char head-begin)
701     (setq content-type (message-fetch-field "Content-Type"))
702     (when content-type
703       (when (string-match
704              "^ *\\([^ \t\n/;]+\\)/\\([^ \t\n/;]+\\)" content-type)
705         (setq type (downcase (match-string 1 content-type))
706               subtype (downcase (match-string 2 content-type))
707               message-rfc822 (and (string= type "message")
708                                   (string= subtype "rfc822"))
709               multipart-any (string= type "multipart")))
710       (when (string-match ";[ \t\n]*name=\\([^ \t\n;]+\\)" content-type)
711         (setq subject (match-string 1 content-type)))
712       (when (string-match "boundary=\"?\\([^\"\n]*[^\" \t\n]\\)" content-type)
713         (setq boundary-regexp (concat "^--"
714                                       (regexp-quote
715                                        (match-string 1 content-type))
716                                       "\\(--\\)?[ \t]*\n"))))
717     (unless subject
718       (when (or multipart-any (not article-insert))
719         (setq subject (message-fetch-field "Subject"))))
720     (unless type
721       (setq type "text"
722             subtype "plain"))
723     ;; Prepare the article and summary inserts.
724     (unless article-insert
725       (setq article-insert (buffer-substring (point-min) (point-max))
726             head-end head-begin))
727     (setq summary-insert article-insert)
728     ;; - summary Subject.
729     (setq summary-insert
730           (let ((line (concat "Subject: <" position
731                               (and position multipart-any ".")
732                               (and multipart-any "*")
733                               (and (or position multipart-any) " ")
734                               (cond ((string= subtype "plain") type)
735                                     ((string= subtype "basic") type)
736                                     (t subtype))
737                               ">"
738                               (and subject " ")
739                               subject
740                               "\n")))
741             (if (string-match "Subject:.*\n\\([ \t].*\n\\)*" summary-insert)
742                 (replace-match line t t summary-insert)
743               (concat summary-insert line))))
744     ;; - summary Message-ID.
745     (setq summary-insert
746           (let ((line (concat "Message-ID: " message-id "\n")))
747             (if (string-match "Message-ID:.*\n\\([ \t].*\n\\)*" summary-insert)
748                 (replace-match line t t summary-insert)
749               (concat summary-insert line))))
750     ;; - summary References.
751     (when parent
752       (setq summary-insert
753             (let ((line (concat "References: " parent "\n")))
754               (if (string-match "References:.*\n\\([ \t].*\n\\)*"
755                                 summary-insert)
756                   (replace-match line t t summary-insert)
757                 (concat summary-insert line)))))
758     ;; Generate dissection information for this entity.
759     (push (list (incf nndoc-mime-split-ordinal)
760                 head-begin head-end body-begin body-end
761                 (count-lines body-begin body-end)
762                 article-insert summary-insert)
763           nndoc-dissection-alist)
764     ;; Recurse for all sub-entities, if any.
765     (widen)
766     (cond
767      (message-rfc822
768       (save-excursion
769         (nndoc-dissect-mime-parts-sub body-begin body-end nil
770                                       position message-id)))
771      ((and multipart-any boundary-regexp)
772       (let ((part-counter 0)
773             part-begin part-end eof-flag)
774         (while (string-match "\
775 ^\\(Lines\\|Content-\\(Type\\|Transfer-Encoding\\)\\):.*\n\\([ \t].*\n\\)*"
776                              article-insert)
777           (setq article-insert (replace-match "" t t article-insert)))
778         (let ((case-fold-search nil))
779           (goto-char body-begin)
780           (setq eof-flag (not (re-search-forward boundary-regexp body-end t)))
781           (while (not eof-flag)
782             (setq part-begin (point))
783             (cond ((re-search-forward boundary-regexp body-end t)
784                    (or (not (match-string 1))
785                        (string= (match-string 1) "")
786                        (setq eof-flag t))
787                    (forward-line -1)
788                    (setq part-end (point))
789                    (forward-line 1))
790                   (t (setq part-end body-end
791                            eof-flag t)))
792             (save-excursion
793               (nndoc-dissect-mime-parts-sub
794                part-begin part-end article-insert
795                (concat position
796                        (and position ".")
797                        (format "%d" (incf part-counter)))
798                message-id)))))))))
799
800 ;;;###autoload
801 (defun nndoc-add-type (definition &optional position)
802   "Add document DEFINITION to the list of nndoc document definitions.
803 If POSITION is nil or `last', the definition will be added
804 as the last checked definition, if t or `first', add as the
805 first definition, and if any other symbol, add after that
806 symbol in the alist."
807   ;; First remove any old instances.
808   (gnus-pull (car definition) nndoc-type-alist)
809   ;; Then enter the new definition in the proper place.
810   (cond
811    ((or (null position) (eq position 'last))
812     (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
813    ((or (eq position t) (eq position 'first))
814     (push definition nndoc-type-alist))
815    (t
816     (let ((list (memq (assq position nndoc-type-alist)
817                       nndoc-type-alist)))
818       (unless list
819         (error "No such position: %s" position))
820       (setcdr list (cons definition (cdr list)))))))
821
822 (provide 'nndoc)
823
824 ;;; nndoc.el ends here