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