* gnus.el: Update all the copyright notices.
[gnus] / lisp / nndoc.el
1 ;;; nndoc.el --- single file access for Gnus
2 ;; Copyright (C) 1995-2000 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 (require 'nnheader)
30 (require 'message)
31 (require 'nnmail)
32 (require 'nnoo)
33 (require 'gnus-util)
34 (require 'mm-util)
35 (eval-when-compile (require 'cl))
36
37 (nnoo-declare nndoc)
38
39 (defvoo nndoc-article-type 'guess
40   "*Type of the file.
41 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
42 `rfc934', `rfc822-forward', `mime-parts', `standard-digest',
43 `slack-digest', `clari-briefs', `nsmail' or `guess'.")
44
45 (defvoo nndoc-post-type 'mail
46   "*Whether the nndoc group is `mail' or `post'.")
47
48 (defvoo nndoc-open-document-hook 'nnheader-ms-strip-cr
49   "Hook run after opening a document.
50 The default function removes all trailing carriage returns
51 from the document.")
52
53 (defvar nndoc-type-alist
54   `((mmdf
55      (article-begin .  "^\^A\^A\^A\^A\n")
56      (body-end .  "^\^A\^A\^A\^A\n"))
57     (nsmail
58      (article-begin .  "^From - "))
59     (news
60      (article-begin . "^Path:"))
61     (rnews
62      (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
63      (body-end-function . nndoc-rnews-body-end))
64     (mbox
65      (article-begin-function . nndoc-mbox-article-begin)
66      (body-end-function . nndoc-mbox-body-end))
67     (babyl
68      (article-begin . "\^_\^L *\n")
69      (body-end . "\^_")
70      (body-begin-function . nndoc-babyl-body-begin)
71      (head-begin-function . nndoc-babyl-head-begin))
72     (forward
73      (article-begin . "^-+ Start of forwarded message -+\n+")
74      (body-end . "^-+ End of forwarded message -+$")
75      (prepare-body-function . nndoc-unquote-dashes))
76     (rfc934
77      (article-begin . "^--.*\n+")
78      (body-end . "^--.*$")
79      (prepare-body-function . nndoc-unquote-dashes))
80     (clari-briefs
81      (article-begin . "^ \\*")
82      (body-end . "^\t------*[ \t]^*\n^ \\*")
83      (body-begin . "^\t")
84      (head-end . "^\t")
85      (generate-head-function . nndoc-generate-clari-briefs-head)
86      (article-transform-function . nndoc-transform-clari-briefs))
87     (mime-digest
88      (article-begin . "")
89      (head-end . "^ ?$")
90      (body-end . "")
91      (file-end . "")
92      (subtype digest guess))
93     (mime-parts
94      (generate-head-function . nndoc-generate-mime-parts-head)
95      (article-transform-function . nndoc-transform-mime-parts))
96     (standard-digest
97      (first-article . ,(concat "^" (make-string 70 ?-) "\n *\n+"))
98      (article-begin . ,(concat "^\n" (make-string 30 ?-) "\n *\n+"))
99      (prepare-body-function . nndoc-unquote-dashes)
100      (body-end-function . nndoc-digest-body-end)
101      (head-end . "^ *$")
102      (body-begin . "^ *\n")
103      (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$")
104      (subtype digest guess))
105     (slack-digest
106      (article-begin . "^------------------------------*[\n \t]+")
107      (head-end . "^ ?$")
108      (body-end-function . nndoc-digest-body-end)
109      (body-begin . "^ ?$")
110      (file-end . "^End of")
111      (prepare-body-function . nndoc-unquote-dashes)
112      (subtype digest guess))
113     (lanl-gov-announce
114      (article-begin . "^\\\\\\\\\n")
115      (head-begin . "^Paper.*:")
116      (head-end   . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
117      (body-begin . "")
118      (body-end   . "-------------------------------------------------")
119      (file-end   . "^Title: Recent Seminal")
120      (generate-head-function . nndoc-generate-lanl-gov-head)
121      (article-transform-function . nndoc-transform-lanl-gov-announce)
122      (subtype preprints guess))
123     (rfc822-forward
124      (article-begin . "^\n")
125      (body-end-function . nndoc-rfc822-forward-body-end-function))
126     (guess
127      (guess . t)
128      (subtype nil))
129     (digest
130      (guess . t)
131      (subtype nil))
132     (preprints
133      (guess . t)
134      (subtype nil))))
135
136 \f
137 (defvoo nndoc-file-begin nil)
138 (defvoo nndoc-first-article nil)
139 (defvoo nndoc-article-begin nil)
140 (defvoo nndoc-head-begin nil)
141 (defvoo nndoc-head-end nil)
142 (defvoo nndoc-file-end nil)
143 (defvoo nndoc-body-begin nil)
144 (defvoo nndoc-body-end-function nil)
145 (defvoo nndoc-body-begin-function nil)
146 (defvoo nndoc-head-begin-function nil)
147 (defvoo nndoc-body-end nil)
148 ;; nndoc-dissection-alist is a list of sublists.  Each sublist holds the
149 ;; following items.  ARTICLE acts as the association key and is an ordinal
150 ;; starting at 1.  HEAD-BEGIN [0], HEAD-END [1], BODY-BEGIN [2] and BODY-END
151 ;; [3] are positions in the `nndoc' buffer.  LINE-COUNT [4] is a count of
152 ;; lines in the body.  For MIME dissections only, ARTICLE-INSERT [5] and
153 ;; SUMMARY-INSERT [6] give headers to insert for full article or summary line
154 ;; generation, respectively.  Other headers usually follow directly from the
155 ;; buffer.  Value `nil' means no insert.
156 (defvoo nndoc-dissection-alist nil)
157 (defvoo nndoc-prepare-body-function nil)
158 (defvoo nndoc-generate-head-function nil)
159 (defvoo nndoc-article-transform-function nil)
160 (defvoo nndoc-article-begin-function nil)
161
162 (defvoo nndoc-status-string "")
163 (defvoo nndoc-group-alist nil)
164 (defvoo nndoc-current-buffer nil
165   "Current nndoc news buffer.")
166 (defvoo nndoc-address nil)
167
168 (defconst nndoc-version "nndoc 1.0"
169   "nndoc version.")
170
171 \f
172
173 ;;; Interface functions
174
175 (nnoo-define-basics nndoc)
176
177 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
178   (when (nndoc-possibly-change-buffer newsgroup server)
179     (save-excursion
180       (set-buffer nntp-server-buffer)
181       (erase-buffer)
182       (let (article entry)
183         (if (stringp (car articles))
184             'headers
185           (while articles
186             (when (setq entry (cdr (assq (setq article (pop articles))
187                                          nndoc-dissection-alist)))
188               (insert (format "221 %d Article retrieved.\n" article))
189               (if nndoc-generate-head-function
190                   (funcall nndoc-generate-head-function article)
191                 (insert-buffer-substring
192                  nndoc-current-buffer (car entry) (nth 1 entry)))
193               (goto-char (point-max))
194               (unless (eq (char-after (1- (point))) ?\n)
195                 (insert "\n"))
196               (insert (format "Lines: %d\n" (nth 4 entry)))
197               (insert ".\n")))
198
199           (nnheader-fold-continuation-lines)
200           'headers)))))
201
202 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
203   (nndoc-possibly-change-buffer newsgroup server)
204   (save-excursion
205     (let ((buffer (or buffer nntp-server-buffer))
206           (entry (cdr (assq article nndoc-dissection-alist)))
207           beg)
208       (set-buffer buffer)
209       (erase-buffer)
210       (when entry
211         (if (stringp article)
212             nil
213           (insert-buffer-substring
214            nndoc-current-buffer (car entry) (nth 1 entry))
215           (insert "\n")
216           (setq beg (point))
217           (insert-buffer-substring
218            nndoc-current-buffer (nth 2 entry) (nth 3 entry))
219           (goto-char beg)
220           (when nndoc-prepare-body-function
221             (funcall nndoc-prepare-body-function))
222           (when nndoc-article-transform-function
223             (funcall nndoc-article-transform-function article))
224           t)))))
225
226 (deffoo nndoc-request-group (group &optional server dont-check)
227   "Select news GROUP."
228   (let (number)
229     (cond
230      ((not (nndoc-possibly-change-buffer group server))
231       (nnheader-report 'nndoc "No such file or buffer: %s"
232                        nndoc-address))
233      (dont-check
234       (nnheader-report 'nndoc "Selected group %s" group)
235       t)
236      ((zerop (setq number (length nndoc-dissection-alist)))
237       (nndoc-close-group group)
238       (nnheader-report 'nndoc "No articles in group %s" group))
239      (t
240       (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
241
242 (deffoo nndoc-request-type (group &optional article)
243   (cond ((not article) 'unknown)
244         (nndoc-post-type nndoc-post-type)
245         (t 'unknown)))
246
247 (deffoo nndoc-close-group (group &optional server)
248   (nndoc-possibly-change-buffer group server)
249   (and nndoc-current-buffer
250        (buffer-name nndoc-current-buffer)
251        (kill-buffer nndoc-current-buffer))
252   (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
253                                 nndoc-group-alist))
254   (setq nndoc-current-buffer nil)
255   (nnoo-close-server 'nndoc server)
256   (setq nndoc-dissection-alist nil)
257   t)
258
259 (deffoo nndoc-request-list (&optional server)
260   nil)
261
262 (deffoo nndoc-request-newgroups (date &optional server)
263   nil)
264
265 (deffoo nndoc-request-list-newsgroups (&optional server)
266   nil)
267
268 \f
269 ;;; Internal functions.
270
271 (defun nndoc-possibly-change-buffer (group source)
272   (let (buf)
273     (cond
274      ;; The current buffer is this group's buffer.
275      ((and nndoc-current-buffer
276            (buffer-name nndoc-current-buffer)
277            (eq nndoc-current-buffer
278                (setq buf (cdr (assoc group nndoc-group-alist))))))
279      ;; We change buffers by taking an old from the group alist.
280      ;; `source' is either a string (a file name) or a buffer object.
281      (buf
282       (setq nndoc-current-buffer buf))
283      ;; It's a totally new group.
284      ((or (and (bufferp nndoc-address)
285                (buffer-name nndoc-address))
286           (and (stringp nndoc-address)
287                (file-exists-p nndoc-address)
288                (not (file-directory-p nndoc-address))))
289       (push (cons group (setq nndoc-current-buffer
290                               (get-buffer-create
291                                (concat " *nndoc " group "*"))))
292             nndoc-group-alist)
293       (setq nndoc-dissection-alist nil)
294       (save-excursion
295         (set-buffer nndoc-current-buffer)
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
562 (defun nndoc-generate-lanl-gov-head (article)
563   (let ((entry (cdr (assq article nndoc-dissection-alist)))
564         (e-mail "no address given")
565         subject from)
566     (save-excursion
567       (set-buffer nndoc-current-buffer)
568       (save-restriction
569         (narrow-to-region (car entry) (nth 1 entry))
570         (goto-char (point-min))
571         (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
572           (setq subject (concat " (" (match-string 1) ")"))
573           (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
574             (setq e-mail (match-string 1)))
575           (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
576                                    nil t)
577             (setq subject (concat (match-string 1) subject))
578             (setq from (concat (match-string 2) " <" e-mail ">"))))))
579     (while (and from (string-match "(\[^)\]*)" from))
580       (setq from (replace-match "" t t from)))
581     (insert "From: "  (or from "unknown")
582             "\nSubject: " (or subject "(no subject)") "\n")))
583
584 (defun nndoc-nsmail-type-p ()
585   (when (looking-at "From - ")
586     t))
587
588 (deffoo nndoc-request-accept-article (group &optional server last)
589   nil)
590
591
592 ;;;
593 ;;; Functions for dissecting the documents
594 ;;;
595
596 (defun nndoc-search (regexp)
597   (prog1
598       (re-search-forward regexp nil t)
599     (beginning-of-line)))
600
601 (defun nndoc-dissect-buffer ()
602   "Go through the document and partition it into heads/bodies/articles."
603   (let ((i 0)
604         (first t)
605         head-begin head-end body-begin body-end)
606     (setq nndoc-dissection-alist nil)
607     (save-excursion
608       (set-buffer nndoc-current-buffer)
609       (goto-char (point-min))
610       ;; Remove blank lines.
611       (while (eq (following-char) ?\n)
612         (delete-char 1))
613       ;; Find the beginning of the file.
614       (when nndoc-file-begin
615         (nndoc-search nndoc-file-begin))
616       ;; Go through the file.
617       (while (if (and first nndoc-first-article)
618                  (nndoc-search nndoc-first-article)
619                (nndoc-article-begin))
620         (setq first nil)
621         (cond (nndoc-head-begin-function
622                (funcall nndoc-head-begin-function))
623               (nndoc-head-begin
624                (nndoc-search nndoc-head-begin)))
625         (if (or (eobp)
626                 (and nndoc-file-end
627                      (looking-at nndoc-file-end)))
628             (goto-char (point-max))
629           (setq head-begin (point))
630           (nndoc-search (or nndoc-head-end "^$"))
631           (setq head-end (point))
632           (if nndoc-body-begin-function
633               (funcall nndoc-body-begin-function)
634             (nndoc-search (or nndoc-body-begin "^\n")))
635           (setq body-begin (point))
636           (or (and nndoc-body-end-function
637                    (funcall nndoc-body-end-function))
638               (and nndoc-body-end
639                    (nndoc-search nndoc-body-end))
640               (nndoc-article-begin)
641               (progn
642                 (goto-char (point-max))
643                 (when nndoc-file-end
644                   (and (re-search-backward nndoc-file-end nil t)
645                        (beginning-of-line)))))
646           (setq body-end (point))
647           (push (list (incf i) head-begin head-end body-begin body-end
648                       (count-lines body-begin body-end))
649                 nndoc-dissection-alist))))))
650
651 (defun nndoc-article-begin ()
652   (if nndoc-article-begin-function
653       (funcall nndoc-article-begin-function)
654     (ignore-errors
655       (nndoc-search nndoc-article-begin))))
656
657 (defun nndoc-unquote-dashes ()
658   "Unquote quoted non-separators in digests."
659   (while (re-search-forward "^- -"nil t)
660     (replace-match "-" t t)))
661
662 ;; Against compiler warnings.
663 (defvar nndoc-mime-split-ordinal)
664
665 (defun nndoc-dissect-mime-parts ()
666   "Go through a MIME composite article and partition it into sub-articles.
667 When a MIME entity contains sub-entities, dissection produces one article for
668 the header of this entity, and one article per sub-entity."
669   (setq nndoc-dissection-alist nil
670         nndoc-mime-split-ordinal 0)
671   (save-excursion
672     (set-buffer nndoc-current-buffer)
673     (nndoc-dissect-mime-parts-sub (point-min) (point-max) nil nil nil)))
674
675 (defun nndoc-dissect-mime-parts-sub (head-begin body-end article-insert
676                                                 position parent)
677   "Dissect an entity, within a composite MIME message.
678 The complete message or MIME entity extends from HEAD-BEGIN to BODY-END.
679 ARTICLE-INSERT should be added at beginning for generating a full article.
680 The string POSITION holds a dotted decimal representation of the article
681 position in the hierarchical structure, it is nil for the outer entity.
682 PARENT is the message-ID of the parent summary line, or nil for none."
683   (let ((case-fold-search t)
684         (message-id (nnmail-message-id))
685         head-end body-begin summary-insert message-rfc822 multipart-any
686         subject content-type type subtype boundary-regexp)
687     ;; Gracefully handle a missing body.
688     (goto-char head-begin)
689     (if (search-forward "\n\n" body-end t)
690         (setq head-end (1- (point))
691               body-begin (point))
692       (setq head-end body-end
693             body-begin body-end))
694     (narrow-to-region head-begin head-end)
695     ;; Save MIME attributes.
696     (goto-char head-begin)
697     (setq content-type (message-fetch-field "Content-Type"))
698     (when content-type
699       (when (string-match
700              "^ *\\([^ \t\n/;]+\\)/\\([^ \t\n/;]+\\)" content-type)
701         (setq type (downcase (match-string 1 content-type))
702               subtype (downcase (match-string 2 content-type))
703               message-rfc822 (and (string= type "message")
704                                   (string= subtype "rfc822"))
705               multipart-any (string= type "multipart")))
706       (when (string-match ";[ \t\n]*name=\\([^ \t\n;]+\\)" content-type)
707         (setq subject (match-string 1 content-type)))
708       (when (string-match "boundary=\"?\\([^\"\n]*[^\" \t\n]\\)" content-type)
709         (setq boundary-regexp (concat "^--"
710                                       (regexp-quote
711                                        (match-string 1 content-type))
712                                       "\\(--\\)?[ \t]*\n"))))
713     (unless subject
714       (when (or multipart-any (not article-insert))
715         (setq subject (message-fetch-field "Subject"))))
716     (unless type
717       (setq type "text"
718             subtype "plain"))
719     ;; Prepare the article and summary inserts.
720     (unless article-insert
721       (setq article-insert (buffer-substring (point-min) (point-max))
722             head-end head-begin))
723     (setq summary-insert article-insert)
724     ;; - summary Subject.
725     (setq summary-insert
726           (let ((line (concat "Subject: <" position
727                               (and position multipart-any ".")
728                               (and multipart-any "*")
729                               (and (or position multipart-any) " ")
730                               (cond ((string= subtype "plain") type)
731                                     ((string= subtype "basic") type)
732                                     (t subtype))
733                               ">"
734                               (and subject " ")
735                               subject
736                               "\n")))
737             (if (string-match "Subject:.*\n\\([ \t].*\n\\)*" summary-insert)
738                 (replace-match line t t summary-insert)
739               (concat summary-insert line))))
740     ;; - summary Message-ID.
741     (setq summary-insert
742           (let ((line (concat "Message-ID: " message-id "\n")))
743             (if (string-match "Message-ID:.*\n\\([ \t].*\n\\)*" summary-insert)
744                 (replace-match line t t summary-insert)
745               (concat summary-insert line))))
746     ;; - summary References.
747     (when parent
748       (setq summary-insert
749             (let ((line (concat "References: " parent "\n")))
750               (if (string-match "References:.*\n\\([ \t].*\n\\)*"
751                                 summary-insert)
752                   (replace-match line t t summary-insert)
753                 (concat summary-insert line)))))
754     ;; Generate dissection information for this entity.
755     (push (list (incf nndoc-mime-split-ordinal)
756                 head-begin head-end body-begin body-end
757                 (count-lines body-begin body-end)
758                 article-insert summary-insert)
759           nndoc-dissection-alist)
760     ;; Recurse for all sub-entities, if any.
761     (widen)
762     (cond
763      (message-rfc822
764       (save-excursion
765         (nndoc-dissect-mime-parts-sub body-begin body-end nil
766                                       position message-id)))
767      ((and multipart-any boundary-regexp)
768       (let ((part-counter 0)
769             part-begin part-end eof-flag)
770         (while (string-match "\
771 ^\\(Lines\\|Content-\\(Type\\|Transfer-Encoding\\)\\):.*\n\\([ \t].*\n\\)*"
772                              article-insert)
773           (setq article-insert (replace-match "" t t article-insert)))
774         (let ((case-fold-search nil))
775           (goto-char body-begin)
776           (setq eof-flag (not (re-search-forward boundary-regexp body-end t)))
777           (while (not eof-flag)
778             (setq part-begin (point))
779             (cond ((re-search-forward boundary-regexp body-end t)
780                    (or (not (match-string 1))
781                        (string= (match-string 1) "")
782                        (setq eof-flag t))
783                    (forward-line -1)
784                    (setq part-end (point))
785                    (forward-line 1))
786                   (t (setq part-end body-end
787                            eof-flag t)))
788             (save-excursion
789               (nndoc-dissect-mime-parts-sub
790                part-begin part-end article-insert
791                (concat position
792                        (and position ".")
793                        (format "%d" (incf part-counter)))
794                message-id)))))))))
795
796 ;;;###autoload
797 (defun nndoc-add-type (definition &optional position)
798   "Add document DEFINITION to the list of nndoc document definitions.
799 If POSITION is nil or `last', the definition will be added
800 as the last checked definition, if t or `first', add as the
801 first definition, and if any other symbol, add after that
802 symbol in the alist."
803   ;; First remove any old instances.
804   (gnus-pull (car definition) nndoc-type-alist)
805   ;; Then enter the new definition in the proper place.
806   (cond
807    ((or (null position) (eq position 'last))
808     (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
809    ((or (eq position t) (eq position 'first))
810     (push definition nndoc-type-alist))
811    (t
812     (let ((list (memq (assq position nndoc-type-alist)
813                       nndoc-type-alist)))
814       (unless list
815         (error "No such position: %s" position))
816       (setcdr list (cons definition (cdr list)))))))
817
818 (provide 'nndoc)
819
820 ;;; nndoc.el ends here