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