Support multipart/digest.
[gnus] / lisp / nndoc.el
1 ;;; nndoc.el --- single file access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (require 'nnheader)
31 (require 'message)
32 (require 'nnmail)
33 (require 'nnoo)
34 (require 'gnus-util)
35 (require 'mm-util)
36 (eval-when-compile (require 'cl))
37
38 (nnoo-declare nndoc)
39
40 (defvoo nndoc-article-type 'guess
41   "*Type of the file.
42 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
43 `rfc934', `rfc822-forward', `mime-parts', `standard-digest',
44 `slack-digest', `clari-briefs', `nsmail' or `guess'.")
45
46 (defvoo nndoc-post-type 'mail
47   "*Whether the nndoc group is `mail' or `post'.")
48
49 (defvoo nndoc-open-document-hook 'nnheader-ms-strip-cr
50   "Hook run after opening a document.
51 The default function removes all trailing carriage returns
52 from the document.")
53
54 (defvar nndoc-type-alist
55   `((mmdf
56      (article-begin .  "^\^A\^A\^A\^A\n")
57      (body-end .  "^\^A\^A\^A\^A\n"))
58     (nsmail
59      (article-begin .  "^From - "))
60     (news
61      (article-begin . "^Path:"))
62     (rnews
63      (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
64      (body-end-function . nndoc-rnews-body-end))
65     (mbox
66      (article-begin-function . nndoc-mbox-article-begin)
67      (body-end-function . nndoc-mbox-body-end))
68     (babyl
69      (article-begin . "\^_\^L *\n")
70      (body-end . "\^_")
71      (body-begin-function . nndoc-babyl-body-begin)
72      (head-begin-function . nndoc-babyl-head-begin))
73     (forward
74      (article-begin . "^-+ \\(Start of \\)?forwarded message.*\n+")
75      (body-end . "^-+ End \\(of \\)?forwarded message.*$")
76      (prepare-body-function . nndoc-unquote-dashes))
77     (rfc934
78      (article-begin . "^--.*\n+")
79      (body-end . "^--.*$")
80      (prepare-body-function . nndoc-unquote-dashes))
81     (clari-briefs
82      (article-begin . "^ \\*")
83      (body-end . "^\t------*[ \t]^*\n^ \\*")
84      (body-begin . "^\t")
85      (head-end . "^\t")
86      (generate-head-function . nndoc-generate-clari-briefs-head)
87      (article-transform-function . nndoc-transform-clari-briefs))
88     (mime-digest
89      (article-begin . "")
90      (head-begin . "^ ?\n")
91      (head-end . "^ ?$")
92      (body-end . "")
93      (file-end . "")
94      (subtype digest guess))
95     (mime-parts
96      (generate-head-function . nndoc-generate-mime-parts-head)
97      (article-transform-function . nndoc-transform-mime-parts))
98     (standard-digest
99      (first-article . ,(concat "^" (make-string 70 ?-) "\n *\n+"))
100      (article-begin . ,(concat "^\n" (make-string 30 ?-) "\n *\n+"))
101      (prepare-body-function . nndoc-unquote-dashes)
102      (body-end-function . nndoc-digest-body-end)
103      (head-end . "^ *$")
104      (body-begin . "^ *\n")
105      (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$")
106      (subtype digest guess))
107     (slack-digest
108      (article-begin . "^------------------------------*[\n \t]+")
109      (head-end . "^ ?$")
110      (body-end-function . nndoc-digest-body-end)
111      (body-begin . "^ ?$")
112      (file-end . "^End of")
113      (prepare-body-function . nndoc-unquote-dashes)
114      (subtype digest guess))
115     (lanl-gov-announce
116      (article-begin . "^\\\\\\\\\n")
117      (head-begin . "^Paper.*:")
118      (head-end   . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
119      (body-begin . "")
120      (body-end   . "-------------------------------------------------")
121      (file-end   . "^Title: Recent Seminal")
122      (generate-head-function . nndoc-generate-lanl-gov-head)
123      (article-transform-function . nndoc-transform-lanl-gov-announce)
124      (subtype preprints guess))
125     (rfc822-forward
126      (article-begin . "^\n")
127      (body-end-function . nndoc-rfc822-forward-body-end-function))
128     (guess
129      (guess . t)
130      (subtype nil))
131     (digest
132      (guess . t)
133      (subtype nil))
134     (preprints
135      (guess . t)
136      (subtype nil))))
137
138 \f
139 (defvoo nndoc-file-begin nil)
140 (defvoo nndoc-first-article nil)
141 (defvoo nndoc-article-begin nil)
142 (defvoo nndoc-head-begin nil)
143 (defvoo nndoc-head-end nil)
144 (defvoo nndoc-file-end nil)
145 (defvoo nndoc-body-begin nil)
146 (defvoo nndoc-body-end-function nil)
147 (defvoo nndoc-body-begin-function nil)
148 (defvoo nndoc-head-begin-function nil)
149 (defvoo nndoc-body-end nil)
150 ;; nndoc-dissection-alist is a list of sublists.  Each sublist holds the
151 ;; following items.  ARTICLE acts as the association key and is an ordinal
152 ;; starting at 1.  HEAD-BEGIN [0], HEAD-END [1], BODY-BEGIN [2] and BODY-END
153 ;; [3] are positions in the `nndoc' buffer.  LINE-COUNT [4] is a count of
154 ;; lines in the body.  For MIME dissections only, ARTICLE-INSERT [5] and
155 ;; SUMMARY-INSERT [6] give headers to insert for full article or summary line
156 ;; generation, respectively.  Other headers usually follow directly from the
157 ;; buffer.  Value `nil' means no insert.
158 (defvoo nndoc-dissection-alist nil)
159 (defvoo nndoc-prepare-body-function nil)
160 (defvoo nndoc-generate-head-function nil)
161 (defvoo nndoc-article-transform-function nil)
162 (defvoo nndoc-article-begin-function nil)
163
164 (defvoo nndoc-status-string "")
165 (defvoo nndoc-group-alist nil)
166 (defvoo nndoc-current-buffer nil
167   "Current nndoc news buffer.")
168 (defvoo nndoc-address nil)
169
170 (defconst nndoc-version "nndoc 1.0"
171   "nndoc version.")
172
173 \f
174
175 ;;; Interface functions
176
177 (nnoo-define-basics nndoc)
178
179 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
180   (when (nndoc-possibly-change-buffer newsgroup server)
181     (save-excursion
182       (set-buffer nntp-server-buffer)
183       (erase-buffer)
184       (let (article entry)
185         (if (stringp (car articles))
186             'headers
187           (while articles
188             (when (setq entry (cdr (assq (setq article (pop articles))
189                                          nndoc-dissection-alist)))
190               (insert (format "221 %d Article retrieved.\n" article))
191               (if nndoc-generate-head-function
192                   (funcall nndoc-generate-head-function article)
193                 (insert-buffer-substring
194                  nndoc-current-buffer (car entry) (nth 1 entry)))
195               (goto-char (point-max))
196               (unless (eq (char-after (1- (point))) ?\n)
197                 (insert "\n"))
198               (insert (format "Lines: %d\n" (nth 4 entry)))
199               (insert ".\n")))
200
201           (nnheader-fold-continuation-lines)
202           'headers)))))
203
204 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
205   (nndoc-possibly-change-buffer newsgroup server)
206   (save-excursion
207     (let ((buffer (or buffer nntp-server-buffer))
208           (entry (cdr (assq article nndoc-dissection-alist)))
209           beg)
210       (set-buffer buffer)
211       (erase-buffer)
212       (when entry
213         (if (stringp article)
214             nil
215           (insert-buffer-substring
216            nndoc-current-buffer (car entry) (nth 1 entry))
217           (insert "\n")
218           (setq beg (point))
219           (insert-buffer-substring
220            nndoc-current-buffer (nth 2 entry) (nth 3 entry))
221           (goto-char beg)
222           (when nndoc-prepare-body-function
223             (funcall nndoc-prepare-body-function))
224           (when nndoc-article-transform-function
225             (funcall nndoc-article-transform-function article))
226           t)))))
227
228 (deffoo nndoc-request-group (group &optional server dont-check)
229   "Select news GROUP."
230   (let (number)
231     (cond
232      ((not (nndoc-possibly-change-buffer group server))
233       (nnheader-report 'nndoc "No such file or buffer: %s"
234                        nndoc-address))
235      (dont-check
236       (nnheader-report 'nndoc "Selected group %s" group)
237       t)
238      ((zerop (setq number (length nndoc-dissection-alist)))
239       (nndoc-close-group group)
240       (nnheader-report 'nndoc "No articles in group %s" group))
241      (t
242       (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
243
244 (deffoo nndoc-request-type (group &optional article)
245   (cond ((not article) 'unknown)
246         (nndoc-post-type nndoc-post-type)
247         (t 'unknown)))
248
249 (deffoo nndoc-close-group (group &optional server)
250   (nndoc-possibly-change-buffer group server)
251   (and nndoc-current-buffer
252        (buffer-name nndoc-current-buffer)
253        (kill-buffer nndoc-current-buffer))
254   (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
255                                 nndoc-group-alist))
256   (setq nndoc-current-buffer nil)
257   (nnoo-close-server 'nndoc server)
258   (setq nndoc-dissection-alist nil)
259   t)
260
261 (deffoo nndoc-request-list (&optional server)
262   nil)
263
264 (deffoo nndoc-request-newgroups (date &optional server)
265   nil)
266
267 (deffoo nndoc-request-list-newsgroups (&optional server)
268   nil)
269
270 \f
271 ;;; Internal functions.
272
273 (defun nndoc-possibly-change-buffer (group source)
274   (let (buf)
275     (cond
276      ;; The current buffer is this group's buffer.
277      ((and nndoc-current-buffer
278            (buffer-name nndoc-current-buffer)
279            (eq nndoc-current-buffer
280                (setq buf (cdr (assoc group nndoc-group-alist))))))
281      ;; We change buffers by taking an old from the group alist.
282      ;; `source' is either a string (a file name) or a buffer object.
283      (buf
284       (setq nndoc-current-buffer buf))
285      ;; It's a totally new group.
286      ((or (and (bufferp nndoc-address)
287                (buffer-name nndoc-address))
288           (and (stringp nndoc-address)
289                (file-exists-p nndoc-address)
290                (not (file-directory-p nndoc-address))))
291       (push (cons group (setq nndoc-current-buffer
292                               (get-buffer-create
293                                (concat " *nndoc " group "*"))))
294             nndoc-group-alist)
295       (setq nndoc-dissection-alist nil)
296       (save-excursion
297         (set-buffer nndoc-current-buffer)
298         (erase-buffer)
299         (if (stringp nndoc-address)
300             (nnheader-insert-file-contents nndoc-address)
301           (insert-buffer-substring nndoc-address))
302         (run-hooks 'nndoc-open-document-hook))))
303     ;; Initialize the nndoc structures according to this new document.
304     (when (and nndoc-current-buffer
305                (not nndoc-dissection-alist))
306       (save-excursion
307         (set-buffer nndoc-current-buffer)
308         (nndoc-set-delims)
309         (if (eq nndoc-article-type 'mime-parts)
310             (nndoc-dissect-mime-parts)
311           (nndoc-dissect-buffer))))
312     (unless nndoc-current-buffer
313       (nndoc-close-server))
314     ;; Return whether we managed to select a file.
315     nndoc-current-buffer))
316
317 ;;;
318 ;;; Deciding what document type we have
319 ;;;
320
321 (defun nndoc-set-delims ()
322   "Set the nndoc delimiter variables according to the type of the document."
323   (let ((vars '(nndoc-file-begin
324                 nndoc-first-article
325                 nndoc-article-begin-function
326                 nndoc-head-begin nndoc-head-end
327                 nndoc-file-end nndoc-article-begin
328                 nndoc-body-begin nndoc-body-end-function nndoc-body-end
329                 nndoc-prepare-body-function nndoc-article-transform-function
330                 nndoc-generate-head-function nndoc-body-begin-function
331                 nndoc-head-begin-function)))
332     (while vars
333       (set (pop vars) nil)))
334   (let (defs)
335     ;; Guess away until we find the real file type.
336     (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
337                                               nndoc-type-alist))))
338       (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
339     ;; Set the nndoc variables.
340     (while defs
341       (set (intern (format "nndoc-%s" (caar defs)))
342            (cdr (pop defs))))))
343
344 (defun nndoc-guess-type (subtype)
345   (let ((alist nndoc-type-alist)
346         results result entry)
347     (while (and (not result)
348                 (setq entry (pop alist)))
349       (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
350         (goto-char (point-min))
351         ;; Remove blank lines.
352         (while (eq (following-char) ?\n)
353           (delete-char 1))
354         (when (numberp (setq result (funcall (intern
355                                               (format "nndoc-%s-type-p"
356                                                       (car entry))))))
357           (push (cons result entry) results)
358           (setq result nil))))
359     (unless (or result results)
360       (error "Document is not of any recognized type"))
361     (if result
362         (car entry)
363       (cadar (sort results 'car-less-than-car)))))
364
365 ;;;
366 ;;; Built-in type predicates and functions
367 ;;;
368
369 (defun nndoc-mbox-type-p ()
370   (when (looking-at message-unix-mail-delimiter)
371     t))
372
373 (defun nndoc-mbox-article-begin ()
374   (when (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
375     (goto-char (match-beginning 0))))
376
377 (defun nndoc-mbox-body-end ()
378   (let ((beg (point))
379         len end)
380     (when
381         (save-excursion
382           (and (re-search-backward
383                 (concat "^" message-unix-mail-delimiter) nil t)
384                (setq end (point))
385                (search-forward "\n\n" beg t)
386                (re-search-backward
387                 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
388                (setq len (string-to-int (match-string 1)))
389                (search-forward "\n\n" beg t)
390                (unless (= (setq len (+ (point) len)) (point-max))
391                  (and (< len (point-max))
392                       (goto-char len)
393                       (looking-at message-unix-mail-delimiter)))))
394       (goto-char len))))
395
396 (defun nndoc-mmdf-type-p ()
397   (when (looking-at "\^A\^A\^A\^A$")
398     t))
399
400 (defun nndoc-news-type-p ()
401   (when (looking-at "^Path:.*\n")
402     t))
403
404 (defun nndoc-rnews-type-p ()
405   (when (looking-at "#! *rnews")
406     t))
407
408 (defun nndoc-rnews-body-end ()
409   (and (re-search-backward nndoc-article-begin nil t)
410        (forward-line 1)
411        (goto-char (+ (point) (string-to-int (match-string 1))))))
412
413 (defun nndoc-babyl-type-p ()
414   (when (re-search-forward "\^_\^L *\n" nil t)
415     t))
416
417 (defun nndoc-babyl-body-begin ()
418   (re-search-forward "^\n" nil t)
419   (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
420     (let ((next (or (save-excursion
421                       (re-search-forward nndoc-article-begin nil t))
422                     (point-max))))
423       (unless (re-search-forward "^\n" next t)
424         (goto-char next)
425         (forward-line -1)
426         (insert "\n")
427         (forward-line -1)))))
428
429 (defun nndoc-babyl-head-begin ()
430   (when (re-search-forward "^[0-9].*\n" nil t)
431     (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
432       (forward-line 1))
433     t))
434
435 (defun nndoc-forward-type-p ()
436   (when (and (re-search-forward "^-+ \\(Start of \\)?forwarded message.*\n+" 
437                                 nil t)
438              (not (re-search-forward "^Subject:.*digest" nil t))
439              (not (re-search-backward "^From:" nil t 2))
440              (not (re-search-forward "^From:" nil t 2)))
441     t))
442
443 (defun nndoc-rfc934-type-p ()
444   (when (and (re-search-forward "^-+ Start of forwarded.*\n+" nil t)
445              (not (re-search-forward "^Subject:.*digest" nil t))
446              (not (re-search-backward "^From:" nil t 2))
447              (not (re-search-forward "^From:" nil t 2)))
448     t))
449
450 (defun nndoc-rfc822-forward-type-p ()
451   (save-restriction
452     (message-narrow-to-head)
453     (when (re-search-forward "^Content-Type: *message/rfc822" nil t)
454       t)))
455
456 (defun nndoc-rfc822-forward-body-end-function ()
457   (goto-char (point-max)))
458
459 (defun nndoc-mime-parts-type-p ()
460   (let ((case-fold-search t)
461         (limit (search-forward "\n\n" nil t)))
462     (goto-char (point-min))
463     (when (and limit
464                (re-search-forward
465                 (concat "\
466 ^Content-Type:[ \t]*multipart/[a-z]+ *; *\\(\\(\n[ \t]\\)?.*;\\)*"
467                         "\\(\n[ \t]\\)?[ \t]*boundary=\"?[^\"\n]*[^\" \t\n]")
468                 limit t))
469       t)))
470
471 (defun nndoc-transform-mime-parts (article)
472   (let* ((entry (cdr (assq article nndoc-dissection-alist)))
473          (headers (nth 5 entry)))
474     (when headers
475       (goto-char (point-min))
476       (insert headers))))
477
478 (defun nndoc-generate-mime-parts-head (article)
479   (let* ((entry (cdr (assq article nndoc-dissection-alist)))
480          (headers (nth 6 entry)))
481     (save-restriction
482       (narrow-to-region (point) (point))
483       (insert-buffer-substring
484        nndoc-current-buffer (car entry) (nth 1 entry))
485       (goto-char (point-max)))
486     (when headers
487       (insert headers))))
488
489 (defun nndoc-clari-briefs-type-p ()
490   (when (let ((case-fold-search nil))
491           (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
492     t))
493
494 (defun nndoc-transform-clari-briefs (article)
495   (goto-char (point-min))
496   (when (looking-at " *\\*\\(.*\\)\n")
497     (replace-match "" t t))
498   (nndoc-generate-clari-briefs-head article))
499
500 (defun nndoc-generate-clari-briefs-head (article)
501   (let ((entry (cdr (assq article nndoc-dissection-alist)))
502         subject from)
503     (save-excursion
504       (set-buffer nndoc-current-buffer)
505       (save-restriction
506         (narrow-to-region (car entry) (nth 3 entry))
507         (goto-char (point-min))
508         (when (looking-at " *\\*\\(.*\\)$")
509           (setq subject (match-string 1))
510           (when (string-match "[ \t]+$" subject)
511             (setq subject (substring subject 0 (match-beginning 0)))))
512         (when
513             (let ((case-fold-search nil))
514               (re-search-forward
515                "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
516           (setq from (match-string 1)))))
517     (insert "From: " "clari@clari.net (" (or from "unknown") ")"
518             "\nSubject: " (or subject "(no subject)") "\n")))
519
520
521 (defun nndoc-mime-digest-type-p ()
522   (let ((case-fold-search t)
523         boundary-id b-delimiter entry)
524     (when (and
525            (re-search-forward
526             (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
527                     "boundary=\"?\\([^\"\n]*[^\" \t\n]\\)")
528             nil t)
529            (match-beginning 1))
530       (setq boundary-id (match-string 1)
531             b-delimiter (concat "\n--" boundary-id "[ \t]*$"))
532       (setq entry (assq 'mime-digest nndoc-type-alist))
533       (setcdr entry
534               (list
535                (cons 'head-begin "^ ?\n")
536                (cons 'head-end "^ ?$")
537                (cons 'body-begin "^ ?\n")
538                (cons 'article-begin b-delimiter)
539                (cons 'body-end-function 'nndoc-digest-body-end)
540                (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
541       t)))
542
543 (defun nndoc-standard-digest-type-p ()
544   (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
545              (re-search-forward
546               (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
547     t))
548
549 (defun nndoc-digest-body-end ()
550   (and (re-search-forward nndoc-article-begin nil t)
551        (goto-char (match-beginning 0))))
552
553 (defun nndoc-slack-digest-type-p ()
554   0)
555
556 (defun nndoc-lanl-gov-announce-type-p ()
557   (when (let ((case-fold-search nil))
558           (re-search-forward "^\\\\\\\\\nPaper: [a-z-]+/[0-9]+" nil t))
559     t))
560
561 (defun nndoc-transform-lanl-gov-announce (article)
562   (goto-char (point-max))
563   (when (re-search-backward "^\\\\\\\\ +(\\([^ ]*\\) , *\\([^ ]*\\))" nil t)
564     (replace-match "\n\nGet it at \\1 (\\2)" t nil)))
565
566 (defun nndoc-generate-lanl-gov-head (article)
567   (let ((entry (cdr (assq article nndoc-dissection-alist)))
568         (e-mail "no address given")
569         subject from)
570     (save-excursion
571       (set-buffer nndoc-current-buffer)
572       (save-restriction
573         (narrow-to-region (car entry) (nth 1 entry))
574         (goto-char (point-min))
575         (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
576           (setq subject (concat " (" (match-string 1) ")"))
577           (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
578             (setq e-mail (match-string 1)))
579           (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
580                                    nil t)
581             (setq subject (concat (match-string 1) subject))
582             (setq from (concat (match-string 2) " <" e-mail ">"))))))
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