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