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