See ChangeLog for comments
[gnus] / 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 (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   ;;  (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