*** 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     (when headers
467       (insert headers))
468     (insert-buffer-substring
469      nndoc-current-buffer (car entry) (nth 1 entry))))
470
471 (defun nndoc-clari-briefs-type-p ()
472   (when (let ((case-fold-search nil))
473           (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
474     t))
475
476 (defun nndoc-transform-clari-briefs (article)
477   (goto-char (point-min))
478   (when (looking-at " *\\*\\(.*\\)\n")
479     (replace-match "" t t))
480   (nndoc-generate-clari-briefs-head article))
481
482 (defun nndoc-generate-clari-briefs-head (article)
483   (let ((entry (cdr (assq article nndoc-dissection-alist)))
484         subject from)
485     (save-excursion
486       (set-buffer nndoc-current-buffer)
487       (save-restriction
488         (narrow-to-region (car entry) (nth 3 entry))
489         (goto-char (point-min))
490         (when (looking-at " *\\*\\(.*\\)$")
491           (setq subject (match-string 1))
492           (when (string-match "[ \t]+$" subject)
493             (setq subject (substring subject 0 (match-beginning 0)))))
494         (when
495             (let ((case-fold-search nil))
496               (re-search-forward
497                "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
498           (setq from (match-string 1)))))
499     (insert "From: " "clari@clari.net (" (or from "unknown") ")"
500             "\nSubject: " (or subject "(no subject)") "\n")))
501
502 (defun nndoc-standard-digest-type-p ()
503   (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
504              (re-search-forward
505               (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
506     t))
507
508 (defun nndoc-digest-body-end ()
509   (and (re-search-forward nndoc-article-begin nil t)
510        (goto-char (match-beginning 0))))
511
512 (defun nndoc-slack-digest-type-p ()
513   0)
514
515 (defun nndoc-lanl-gov-announce-type-p ()
516   (when (let ((case-fold-search nil))
517           (re-search-forward "^\\\\\\\\\nPaper: [a-z-]+/[0-9]+" nil t))
518     t))
519
520 (defun nndoc-transform-lanl-gov-announce (article)
521   (goto-char (point-max))
522   (when (re-search-backward "^\\\\\\\\ +(\\([^ ]*\\) , *\\([^ ]*\\))" nil t)
523     (replace-match "\n\nGet it at \\1 (\\2)" t nil))
524   ;;  (when (re-search-backward "^\\\\\\\\$" nil t)
525   ;;    (replace-match "" t t))
526   )
527
528 (defun nndoc-generate-lanl-gov-head (article)
529   (let ((entry (cdr (assq article nndoc-dissection-alist)))
530         (e-mail "no address given")
531         subject from)
532     (save-excursion
533       (set-buffer nndoc-current-buffer)
534       (save-restriction
535         (narrow-to-region (car entry) (nth 1 entry))
536         (goto-char (point-min))
537         (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
538           (setq subject (concat " (" (match-string 1) ")"))
539           (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
540             (setq e-mail (match-string 1)))
541           (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
542                                    nil t)
543             (setq subject (concat (match-string 1) subject))
544             (setq from (concat (match-string 2) " <" e-mail ">"))))
545         ))
546     (while (and from (string-match "(\[^)\]*)" from))
547       (setq from (replace-match "" t t from)))
548     (insert "From: "  (or from "unknown")
549             "\nSubject: " (or subject "(no subject)") "\n")))
550
551 (deffoo nndoc-request-accept-article (group &optional server last)
552   nil)
553
554
555
556 ;;;
557 ;;; Functions for dissecting the documents
558 ;;;
559
560 (defun nndoc-search (regexp)
561   (prog1
562       (re-search-forward regexp nil t)
563     (beginning-of-line)))
564
565 (defun nndoc-dissect-buffer ()
566   "Go through the document and partition it into heads/bodies/articles."
567   (let ((i 0)
568         (first t)
569         head-begin head-end body-begin body-end)
570     (setq nndoc-dissection-alist nil)
571     (save-excursion
572       (set-buffer nndoc-current-buffer)
573       (goto-char (point-min))
574       ;; Find the beginning of the file.
575       (when nndoc-file-begin
576         (nndoc-search nndoc-file-begin))
577       ;; Go through the file.
578       (while (if (and first nndoc-first-article)
579                  (nndoc-search nndoc-first-article)
580                (nndoc-article-begin))
581         (setq first nil)
582         (cond (nndoc-head-begin-function
583                (funcall nndoc-head-begin-function))
584               (nndoc-head-begin
585                (nndoc-search nndoc-head-begin)))
586         (if (or (eobp)
587                 (and nndoc-file-end
588                      (looking-at nndoc-file-end)))
589             (goto-char (point-max))
590           (setq head-begin (point))
591           (nndoc-search (or nndoc-head-end "^$"))
592           (setq head-end (point))
593           (if nndoc-body-begin-function
594               (funcall nndoc-body-begin-function)
595             (nndoc-search (or nndoc-body-begin "^\n")))
596           (setq body-begin (point))
597           (or (and nndoc-body-end-function
598                    (funcall nndoc-body-end-function))
599               (and nndoc-body-end
600                    (nndoc-search nndoc-body-end))
601               (nndoc-article-begin)
602               (progn
603                 (goto-char (point-max))
604                 (when nndoc-file-end
605                   (and (re-search-backward nndoc-file-end nil t)
606                        (beginning-of-line)))))
607           (setq body-end (point))
608           (push (list (incf i) head-begin head-end body-begin body-end
609                       (count-lines body-begin body-end))
610                 nndoc-dissection-alist))))))
611
612 (defun nndoc-article-begin ()
613   (if nndoc-article-begin-function
614       (funcall nndoc-article-begin-function)
615     (ignore-errors
616       (nndoc-search nndoc-article-begin))))
617
618 (defun nndoc-unquote-dashes ()
619   "Unquote quoted non-separators in digests."
620   (while (re-search-forward "^- -"nil t)
621     (replace-match "-" t t)))
622
623 ;; Against compiler warnings.
624 (defvar nndoc-mime-split-ordinal)
625
626 (defun nndoc-dissect-mime-parts ()
627   "Go through a MIME composite article and partition it into sub-articles.
628 When a MIME entity contains sub-entities, dissection produces one article for
629 the header of this entity, and one article per sub-entity."
630   (setq nndoc-dissection-alist nil
631         nndoc-mime-split-ordinal 0)
632   (save-excursion
633     (set-buffer nndoc-current-buffer)
634     (nndoc-dissect-mime-parts-sub (point-min) (point-max) nil nil nil)))
635
636 (defun nndoc-dissect-mime-parts-sub (head-begin body-end article-insert
637                                                 position parent)
638   "Dissect an entity, within a composite MIME message.
639 The complete message or MIME entity extends from HEAD-BEGIN to BODY-END.
640 ARTICLE-INSERT should be added at beginning for generating a full article.
641 The string POSITION holds a dotted decimal representation of the article
642 position in the hierarchical structure, it is nil for the outer entity.
643 PARENT is the message-ID of the parent summary line, or nil for none."
644   (let ((case-fold-search t)
645         (message-id (nnmail-message-id))
646         head-end body-begin summary-insert message-rfc822 multipart-any
647         subject content-type type subtype boundary-regexp)
648     ;; Gracefully handle a missing body.
649     (goto-char head-begin)
650     (if (search-forward "\n\n" body-end t)
651         (setq head-end (1- (point))
652               body-begin (point))
653       (setq head-end body-end
654             body-begin body-end))
655     (narrow-to-region head-begin head-end)
656     ;; Save MIME attributes.
657     (goto-char head-begin)
658     (setq content-type (message-fetch-field "Content-Type"))
659     (when content-type
660       (when (string-match
661              "^ *\\([^ \t\n/;]+\\)/\\([^ \t\n/;]+\\)" content-type)
662         (setq type (downcase (match-string 1 content-type))
663               subtype (downcase (match-string 2 content-type))
664               message-rfc822 (and (string= type "message")
665                                   (string= subtype "rfc822"))
666               multipart-any (string= type "multipart")))
667       (when (string-match ";[ \t\n]*name=\\([^ \t\n;]+\\)" content-type)
668         (setq subject (match-string 1 content-type)))
669       (when (string-match "boundary=\"?\\([^\"\n]*[^\" \t\n]\\)" content-type)
670         (setq boundary-regexp (concat "^--"
671                                       (regexp-quote
672                                        (match-string 1 content-type))
673                                       "\\(--\\)?[ \t]*\n"))))
674     (unless subject
675       (when (or multipart-any (not article-insert))
676         (setq subject (message-fetch-field "Subject"))))
677     (unless type
678       (setq type "text"
679             subtype "plain"))
680     ;; Prepare the article and summary inserts.
681     (unless article-insert
682       (setq article-insert (buffer-substring (point-min) (point-max))
683             head-end head-begin))
684     (setq summary-insert article-insert)
685     ;; - summary Subject.
686     (setq summary-insert
687           (let ((line (concat "Subject: <" position
688                               (and position multipart-any ".")
689                               (and multipart-any "*")
690                               (and (or position multipart-any) " ")
691                               (cond ((string= subtype "plain") type)
692                                     ((string= subtype "basic") type)
693                                     (t subtype))
694                               ">"
695                               (and subject " ")
696                               subject
697                               "\n")))
698             (if (string-match "Subject:.*\n\\([ \t].*\n\\)*" summary-insert)
699                 (replace-match line t t summary-insert)
700               (concat summary-insert line))))
701     ;; - summary Message-ID.
702     (setq summary-insert
703           (let ((line (concat "Message-ID: " message-id "\n")))
704             (if (string-match "Message-ID:.*\n\\([ \t].*\n\\)*" summary-insert)
705                 (replace-match line t t summary-insert)
706               (concat summary-insert line))))
707     ;; - summary References.
708     (when parent
709       (setq summary-insert
710             (let ((line (concat "References: " parent "\n")))
711               (if (string-match "References:.*\n\\([ \t].*\n\\)*"
712                                 summary-insert)
713                   (replace-match line t t summary-insert)
714                 (concat summary-insert line)))))
715     ;; Generate dissection information for this entity.
716     (push (list (incf nndoc-mime-split-ordinal)
717                 head-begin head-end body-begin body-end
718                 (count-lines body-begin body-end)
719                 article-insert summary-insert)
720           nndoc-dissection-alist)
721     ;; Recurse for all sub-entities, if any.
722     (widen)
723     (cond
724      (message-rfc822
725       (save-excursion
726         (nndoc-dissect-mime-parts-sub body-begin body-end nil
727                                       position message-id)))
728      ((and multipart-any boundary-regexp)
729       (let ((part-counter 0)
730             part-begin part-end eof-flag)
731         (while (string-match "\
732 ^\\(Lines\\|Content-\\(Type\\|Transfer-Encoding\\)\\):.*\n\\([ \t].*\n\\)*"
733                              article-insert)
734           (setq article-insert (replace-match "" t t article-insert)))
735         (let ((case-fold-search nil))
736           (goto-char body-begin)
737           (setq eof-flag (not (re-search-forward boundary-regexp body-end t)))
738           (while (not eof-flag)
739             (setq part-begin (point))
740             (cond ((re-search-forward boundary-regexp body-end t)
741                    (or (not (match-string 1))
742                        (string= (match-string 1) "")
743                        (setq eof-flag t))
744                    (forward-line -1)
745                    (setq part-end (point))
746                    (forward-line 1))
747                   (t (setq part-end body-end
748                            eof-flag t)))
749             (save-excursion
750               (nndoc-dissect-mime-parts-sub
751                part-begin part-end article-insert
752                (concat position
753                        (and position ".")
754                        (format "%d" (incf part-counter)))
755                message-id)))))))))
756
757 ;;;###autoload
758 (defun nndoc-add-type (definition &optional position)
759   "Add document DEFINITION to the list of nndoc document definitions.
760 If POSITION is nil or `last', the definition will be added
761 as the last checked definition, if t or `first', add as the
762 first definition, and if any other symbol, add after that
763 symbol in the alist."
764   ;; First remove any old instances.
765   (gnus-pull (car definition) nndoc-type-alist)
766   ;; Then enter the new definition in the proper place.
767   (cond
768    ((or (null position) (eq position 'last))
769     (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
770    ((or (eq position t) (eq position 'first))
771     (push definition nndoc-type-alist))
772    (t
773     (let ((list (memq (assq position nndoc-type-alist)
774                       nndoc-type-alist)))
775       (unless list
776         (error "No such position: %s" position))
777       (setcdr list (cons definition (cdr list)))))))
778
779 (provide 'nndoc)
780
781 ;;; nndoc.el ends here