(gnus-cache-file-name): Do not replace file name character in base path.
[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 (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         (mm-enable-multibyte)
297         (erase-buffer)
298         (if (stringp nndoc-address)
299             (nnheader-insert-file-contents nndoc-address)
300           (insert-buffer-substring nndoc-address))
301         (run-hooks 'nndoc-open-document-hook))))
302     ;; Initialize the nndoc structures according to this new document.
303     (when (and nndoc-current-buffer
304                (not nndoc-dissection-alist))
305       (save-excursion
306         (set-buffer nndoc-current-buffer)
307         (nndoc-set-delims)
308         (if (eq nndoc-article-type 'mime-parts)
309             (nndoc-dissect-mime-parts)
310           (nndoc-dissect-buffer))))
311     (unless nndoc-current-buffer
312       (nndoc-close-server))
313     ;; Return whether we managed to select a file.
314     nndoc-current-buffer))
315
316 ;;;
317 ;;; Deciding what document type we have
318 ;;;
319
320 (defun nndoc-set-delims ()
321   "Set the nndoc delimiter variables according to the type of the document."
322   (let ((vars '(nndoc-file-begin
323                 nndoc-first-article
324                 nndoc-article-begin-function
325                 nndoc-head-begin nndoc-head-end
326                 nndoc-file-end nndoc-article-begin
327                 nndoc-body-begin nndoc-body-end-function nndoc-body-end
328                 nndoc-prepare-body-function nndoc-article-transform-function
329                 nndoc-generate-head-function nndoc-body-begin-function
330                 nndoc-head-begin-function)))
331     (while vars
332       (set (pop vars) nil)))
333   (let (defs)
334     ;; Guess away until we find the real file type.
335     (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
336                                               nndoc-type-alist))))
337       (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
338     ;; Set the nndoc variables.
339     (while defs
340       (set (intern (format "nndoc-%s" (caar defs)))
341            (cdr (pop defs))))))
342
343 (defun nndoc-guess-type (subtype)
344   (let ((alist nndoc-type-alist)
345         results result entry)
346     (while (and (not result)
347                 (setq entry (pop alist)))
348       (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
349         (goto-char (point-min))
350         ;; Remove blank lines.
351         (while (eq (following-char) ?\n)
352           (delete-char 1))
353         (when (numberp (setq result (funcall (intern
354                                               (format "nndoc-%s-type-p"
355                                                       (car entry))))))
356           (push (cons result entry) results)
357           (setq result nil))))
358     (unless (or result results)
359       (error "Document is not of any recognized type"))
360     (if result
361         (car entry)
362       (cadar (sort results 'car-less-than-car)))))
363
364 ;;;
365 ;;; Built-in type predicates and functions
366 ;;;
367
368 (defun nndoc-mbox-type-p ()
369   (when (looking-at message-unix-mail-delimiter)
370     t))
371
372 (defun nndoc-mbox-article-begin ()
373   (when (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
374     (goto-char (match-beginning 0))))
375
376 (defun nndoc-mbox-body-end ()
377   (let ((beg (point))
378         len end)
379     (when
380         (save-excursion
381           (and (re-search-backward
382                 (concat "^" message-unix-mail-delimiter) nil t)
383                (setq end (point))
384                (search-forward "\n\n" beg t)
385                (re-search-backward
386                 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
387                (setq len (string-to-int (match-string 1)))
388                (search-forward "\n\n" beg t)
389                (unless (= (setq len (+ (point) len)) (point-max))
390                  (and (< len (point-max))
391                       (goto-char len)
392                       (looking-at message-unix-mail-delimiter)))))
393       (goto-char len))))
394
395 (defun nndoc-mmdf-type-p ()
396   (when (looking-at "\^A\^A\^A\^A$")
397     t))
398
399 (defun nndoc-news-type-p ()
400   (when (looking-at "^Path:.*\n")
401     t))
402
403 (defun nndoc-rnews-type-p ()
404   (when (looking-at "#! *rnews")
405     t))
406
407 (defun nndoc-rnews-body-end ()
408   (and (re-search-backward nndoc-article-begin nil t)
409        (forward-line 1)
410        (goto-char (+ (point) (string-to-int (match-string 1))))))
411
412 (defun nndoc-babyl-type-p ()
413   (when (re-search-forward "\^_\^L *\n" nil t)
414     t))
415
416 (defun nndoc-babyl-body-begin ()
417   (re-search-forward "^\n" nil t)
418   (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
419     (let ((next (or (save-excursion
420                       (re-search-forward nndoc-article-begin nil t))
421                     (point-max))))
422       (unless (re-search-forward "^\n" next t)
423         (goto-char next)
424         (forward-line -1)
425         (insert "\n")
426         (forward-line -1)))))
427
428 (defun nndoc-babyl-head-begin ()
429   (when (re-search-forward "^[0-9].*\n" nil t)
430     (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
431       (forward-line 1))
432     t))
433
434 (defun nndoc-forward-type-p ()
435   (when (and (re-search-forward "^-+ Start of forwarded message -+\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-rfc934-type-p ()
442   (when (and (re-search-forward "^-+ Start of forwarded.*\n+" nil t)
443              (not (re-search-forward "^Subject:.*digest" nil t))
444              (not (re-search-backward "^From:" nil t 2))
445              (not (re-search-forward "^From:" nil t 2)))
446     t))
447
448 (defun nndoc-rfc822-forward-type-p ()
449   (save-restriction
450     (message-narrow-to-head)
451     (when (re-search-forward "^Content-Type: *message/rfc822" nil t)
452       t)))
453
454 (defun nndoc-rfc822-forward-body-end-function ()
455   (goto-char (point-max)))
456
457 (defun nndoc-mime-parts-type-p ()
458   (let ((case-fold-search t)
459         (limit (search-forward "\n\n" nil t)))
460     (goto-char (point-min))
461     (when (and limit
462                (re-search-forward
463                 (concat "\
464 ^Content-Type:[ \t]*multipart/[a-z]+ *; *\\(\\(\n[ \t]\\)?.*;\\)*"
465                         "\\(\n[ \t]\\)?[ \t]*boundary=\"?[^\"\n]*[^\" \t\n]")
466                 limit t))
467       t)))
468
469 (defun nndoc-transform-mime-parts (article)
470   (let* ((entry (cdr (assq article nndoc-dissection-alist)))
471          (headers (nth 5 entry)))
472     (when headers
473       (goto-char (point-min))
474       (insert headers))))
475
476 (defun nndoc-generate-mime-parts-head (article)
477   (let* ((entry (cdr (assq article nndoc-dissection-alist)))
478          (headers (nth 6 entry)))
479     (save-restriction
480       (narrow-to-region (point) (point))
481       (insert-buffer-substring
482        nndoc-current-buffer (car entry) (nth 1 entry))
483       (goto-char (point-max)))
484     (when headers
485       (insert headers))))
486
487 (defun nndoc-clari-briefs-type-p ()
488   (when (let ((case-fold-search nil))
489           (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
490     t))
491
492 (defun nndoc-transform-clari-briefs (article)
493   (goto-char (point-min))
494   (when (looking-at " *\\*\\(.*\\)\n")
495     (replace-match "" t t))
496   (nndoc-generate-clari-briefs-head article))
497
498 (defun nndoc-generate-clari-briefs-head (article)
499   (let ((entry (cdr (assq article nndoc-dissection-alist)))
500         subject from)
501     (save-excursion
502       (set-buffer nndoc-current-buffer)
503       (save-restriction
504         (narrow-to-region (car entry) (nth 3 entry))
505         (goto-char (point-min))
506         (when (looking-at " *\\*\\(.*\\)$")
507           (setq subject (match-string 1))
508           (when (string-match "[ \t]+$" subject)
509             (setq subject (substring subject 0 (match-beginning 0)))))
510         (when
511             (let ((case-fold-search nil))
512               (re-search-forward
513                "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
514           (setq from (match-string 1)))))
515     (insert "From: " "clari@clari.net (" (or from "unknown") ")"
516             "\nSubject: " (or subject "(no subject)") "\n")))
517
518
519 (defun nndoc-mime-digest-type-p ()
520   (let ((case-fold-search t)
521         boundary-id b-delimiter entry)
522     (when (and
523            (re-search-forward
524             (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
525                     "boundary=\"?\\([^\"\n]*[^\" \t\n]\\)")
526             nil t)
527            (match-beginning 1))
528       (setq boundary-id (match-string 1)
529             b-delimiter (concat "\n--" boundary-id "[\n \t]+"))
530       (setq entry (assq 'mime-digest nndoc-type-alist))
531       (setcdr entry
532               (list
533                (cons 'head-end "^ ?$")
534                (cons 'body-begin "^ ?\n")
535                (cons 'article-begin b-delimiter)
536                (cons 'body-end-function 'nndoc-digest-body-end)
537                (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
538       t)))
539
540 (defun nndoc-standard-digest-type-p ()
541   (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
542              (re-search-forward
543               (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
544     t))
545
546 (defun nndoc-digest-body-end ()
547   (and (re-search-forward nndoc-article-begin nil t)
548        (goto-char (match-beginning 0))))
549
550 (defun nndoc-slack-digest-type-p ()
551   0)
552
553 (defun nndoc-lanl-gov-announce-type-p ()
554   (when (let ((case-fold-search nil))
555           (re-search-forward "^\\\\\\\\\nPaper: [a-z-]+/[0-9]+" nil t))
556     t))
557
558 (defun nndoc-transform-lanl-gov-announce (article)
559   (goto-char (point-max))
560   (when (re-search-backward "^\\\\\\\\ +(\\([^ ]*\\) , *\\([^ ]*\\))" nil t)
561     (replace-match "\n\nGet it at \\1 (\\2)" t nil))
562   ;;  (when (re-search-backward "^\\\\\\\\$" nil t)
563   ;;    (replace-match "" t t))
564   )
565
566 (defun nndoc-generate-lanl-gov-head (article)
567   (let ((entry (cdr (assq article nndoc-dissection-alist)))
568         (e-mail "no address given")
569         subject from)
570     (save-excursion
571       (set-buffer nndoc-current-buffer)
572       (save-restriction
573         (narrow-to-region (car entry) (nth 1 entry))
574         (goto-char (point-min))
575         (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
576           (setq subject (concat " (" (match-string 1) ")"))
577           (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
578             (setq e-mail (match-string 1)))
579           (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
580                                    nil t)
581             (setq subject (concat (match-string 1) subject))
582             (setq from (concat (match-string 2) " <" e-mail ">"))))
583         ))
584     (while (and from (string-match "(\[^)\]*)" from))
585       (setq from (replace-match "" t t from)))
586     (insert "From: "  (or from "unknown")
587             "\nSubject: " (or subject "(no subject)") "\n")))
588
589 (defun nndoc-nsmail-type-p ()
590   (when (looking-at "From - ")
591     t))
592
593 (deffoo nndoc-request-accept-article (group &optional server last)
594   nil)
595
596
597 ;;;
598 ;;; Functions for dissecting the documents
599 ;;;
600
601 (defun nndoc-search (regexp)
602   (prog1
603       (re-search-forward regexp nil t)
604     (beginning-of-line)))
605
606 (defun nndoc-dissect-buffer ()
607   "Go through the document and partition it into heads/bodies/articles."
608   (let ((i 0)
609         (first t)
610         head-begin head-end body-begin body-end)
611     (setq nndoc-dissection-alist nil)
612     (save-excursion
613       (set-buffer nndoc-current-buffer)
614       (goto-char (point-min))
615       ;; Remove blank lines.
616       (while (eq (following-char) ?\n)
617         (delete-char 1))
618       ;; Find the beginning of the file.
619       (when nndoc-file-begin
620         (nndoc-search nndoc-file-begin))
621       ;; Go through the file.
622       (while (if (and first nndoc-first-article)
623                  (nndoc-search nndoc-first-article)
624                (nndoc-article-begin))
625         (setq first nil)
626         (cond (nndoc-head-begin-function
627                (funcall nndoc-head-begin-function))
628               (nndoc-head-begin
629                (nndoc-search nndoc-head-begin)))
630         (if (or (eobp)
631                 (and nndoc-file-end
632                      (looking-at nndoc-file-end)))
633             (goto-char (point-max))
634           (setq head-begin (point))
635           (nndoc-search (or nndoc-head-end "^$"))
636           (setq head-end (point))
637           (if nndoc-body-begin-function
638               (funcall nndoc-body-begin-function)
639             (nndoc-search (or nndoc-body-begin "^\n")))
640           (setq body-begin (point))
641           (or (and nndoc-body-end-function
642                    (funcall nndoc-body-end-function))
643               (and nndoc-body-end
644                    (nndoc-search nndoc-body-end))
645               (nndoc-article-begin)
646               (progn
647                 (goto-char (point-max))
648                 (when nndoc-file-end
649                   (and (re-search-backward nndoc-file-end nil t)
650                        (beginning-of-line)))))
651           (setq body-end (point))
652           (push (list (incf i) head-begin head-end body-begin body-end
653                       (count-lines body-begin body-end))
654                 nndoc-dissection-alist))))))
655
656 (defun nndoc-article-begin ()
657   (if nndoc-article-begin-function
658       (funcall nndoc-article-begin-function)
659     (ignore-errors
660       (nndoc-search nndoc-article-begin))))
661
662 (defun nndoc-unquote-dashes ()
663   "Unquote quoted non-separators in digests."
664   (while (re-search-forward "^- -"nil t)
665     (replace-match "-" t t)))
666
667 ;; Against compiler warnings.
668 (defvar nndoc-mime-split-ordinal)
669
670 (defun nndoc-dissect-mime-parts ()
671   "Go through a MIME composite article and partition it into sub-articles.
672 When a MIME entity contains sub-entities, dissection produces one article for
673 the header of this entity, and one article per sub-entity."
674   (setq nndoc-dissection-alist nil
675         nndoc-mime-split-ordinal 0)
676   (save-excursion
677     (set-buffer nndoc-current-buffer)
678     (nndoc-dissect-mime-parts-sub (point-min) (point-max) nil nil nil)))
679
680 (defun nndoc-dissect-mime-parts-sub (head-begin body-end article-insert
681                                                 position parent)
682   "Dissect an entity, within a composite MIME message.
683 The complete message or MIME entity extends from HEAD-BEGIN to BODY-END.
684 ARTICLE-INSERT should be added at beginning for generating a full article.
685 The string POSITION holds a dotted decimal representation of the article
686 position in the hierarchical structure, it is nil for the outer entity.
687 PARENT is the message-ID of the parent summary line, or nil for none."
688   (let ((case-fold-search t)
689         (message-id (nnmail-message-id))
690         head-end body-begin summary-insert message-rfc822 multipart-any
691         subject content-type type subtype boundary-regexp)
692     ;; Gracefully handle a missing body.
693     (goto-char head-begin)
694     (if (search-forward "\n\n" body-end t)
695         (setq head-end (1- (point))
696               body-begin (point))
697       (setq head-end body-end
698             body-begin body-end))
699     (narrow-to-region head-begin head-end)
700     ;; Save MIME attributes.
701     (goto-char head-begin)
702     (setq content-type (message-fetch-field "Content-Type"))
703     (when content-type
704       (when (string-match
705              "^ *\\([^ \t\n/;]+\\)/\\([^ \t\n/;]+\\)" content-type)
706         (setq type (downcase (match-string 1 content-type))
707               subtype (downcase (match-string 2 content-type))
708               message-rfc822 (and (string= type "message")
709                                   (string= subtype "rfc822"))
710               multipart-any (string= type "multipart")))
711       (when (string-match ";[ \t\n]*name=\\([^ \t\n;]+\\)" content-type)
712         (setq subject (match-string 1 content-type)))
713       (when (string-match "boundary=\"?\\([^\"\n]*[^\" \t\n]\\)" content-type)
714         (setq boundary-regexp (concat "^--"
715                                       (regexp-quote
716                                        (match-string 1 content-type))
717                                       "\\(--\\)?[ \t]*\n"))))
718     (unless subject
719       (when (or multipart-any (not article-insert))
720         (setq subject (message-fetch-field "Subject"))))
721     (unless type
722       (setq type "text"
723             subtype "plain"))
724     ;; Prepare the article and summary inserts.
725     (unless article-insert
726       (setq article-insert (buffer-substring (point-min) (point-max))
727             head-end head-begin))
728     (setq summary-insert article-insert)
729     ;; - summary Subject.
730     (setq summary-insert
731           (let ((line (concat "Subject: <" position
732                               (and position multipart-any ".")
733                               (and multipart-any "*")
734                               (and (or position multipart-any) " ")
735                               (cond ((string= subtype "plain") type)
736                                     ((string= subtype "basic") type)
737                                     (t subtype))
738                               ">"
739                               (and subject " ")
740                               subject
741                               "\n")))
742             (if (string-match "Subject:.*\n\\([ \t].*\n\\)*" summary-insert)
743                 (replace-match line t t summary-insert)
744               (concat summary-insert line))))
745     ;; - summary Message-ID.
746     (setq summary-insert
747           (let ((line (concat "Message-ID: " message-id "\n")))
748             (if (string-match "Message-ID:.*\n\\([ \t].*\n\\)*" summary-insert)
749                 (replace-match line t t summary-insert)
750               (concat summary-insert line))))
751     ;; - summary References.
752     (when parent
753       (setq summary-insert
754             (let ((line (concat "References: " parent "\n")))
755               (if (string-match "References:.*\n\\([ \t].*\n\\)*"
756                                 summary-insert)
757                   (replace-match line t t summary-insert)
758                 (concat summary-insert line)))))
759     ;; Generate dissection information for this entity.
760     (push (list (incf nndoc-mime-split-ordinal)
761                 head-begin head-end body-begin body-end
762                 (count-lines body-begin body-end)
763                 article-insert summary-insert)
764           nndoc-dissection-alist)
765     ;; Recurse for all sub-entities, if any.
766     (widen)
767     (cond
768      (message-rfc822
769       (save-excursion
770         (nndoc-dissect-mime-parts-sub body-begin body-end nil
771                                       position message-id)))
772      ((and multipart-any boundary-regexp)
773       (let ((part-counter 0)
774             part-begin part-end eof-flag)
775         (while (string-match "\
776 ^\\(Lines\\|Content-\\(Type\\|Transfer-Encoding\\)\\):.*\n\\([ \t].*\n\\)*"
777                              article-insert)
778           (setq article-insert (replace-match "" t t article-insert)))
779         (let ((case-fold-search nil))
780           (goto-char body-begin)
781           (setq eof-flag (not (re-search-forward boundary-regexp body-end t)))
782           (while (not eof-flag)
783             (setq part-begin (point))
784             (cond ((re-search-forward boundary-regexp body-end t)
785                    (or (not (match-string 1))
786                        (string= (match-string 1) "")
787                        (setq eof-flag t))
788                    (forward-line -1)
789                    (setq part-end (point))
790                    (forward-line 1))
791                   (t (setq part-end body-end
792                            eof-flag t)))
793             (save-excursion
794               (nndoc-dissect-mime-parts-sub
795                part-begin part-end article-insert
796                (concat position
797                        (and position ".")
798                        (format "%d" (incf part-counter)))
799                message-id)))))))))
800
801 ;;;###autoload
802 (defun nndoc-add-type (definition &optional position)
803   "Add document DEFINITION to the list of nndoc document definitions.
804 If POSITION is nil or `last', the definition will be added
805 as the last checked definition, if t or `first', add as the
806 first definition, and if any other symbol, add after that
807 symbol in the alist."
808   ;; First remove any old instances.
809   (gnus-pull (car definition) nndoc-type-alist)
810   ;; Then enter the new definition in the proper place.
811   (cond
812    ((or (null position) (eq position 'last))
813     (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
814    ((or (eq position t) (eq position 'first))
815     (push definition nndoc-type-alist))
816    (t
817     (let ((list (memq (assq position nndoc-type-alist)
818                       nndoc-type-alist)))
819       (unless list
820         (error "No such position: %s" position))
821       (setcdr list (cons definition (cdr list)))))))
822
823 (provide 'nndoc)
824
825 ;;; nndoc.el ends here