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