1 ;;; nndoc.el --- single file access for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; For Outlook mail boxes format, see http://mbx2mbox.sourceforge.net/
32 (eval-when-compile (require 'cl))
42 (defvoo nndoc-article-type 'guess
44 One of `mbox', `babyl', `digest', `news', `rnews', `mmdf', `forward',
45 `rfc934', `rfc822-forward', `mime-parts', `standard-digest',
46 `slack-digest', `clari-briefs', `nsmail', `outlook', `oe-dbx',
47 `mailman', `exim-bounce', or `guess'.")
49 (defvoo nndoc-post-type 'mail
50 "*Whether the nndoc group is `mail' or `post'.")
52 (defvoo nndoc-open-document-hook 'nnheader-ms-strip-cr
53 "Hook run after opening a document.
54 The default function removes all trailing carriage returns
57 (defvar nndoc-type-alist
59 (article-begin . "^\^A\^A\^A\^A\n")
60 (body-end . "^\^A\^A\^A\^A\n"))
62 (article-begin . "^------ This is a copy of the message, including all the headers. ------\n\n")
63 (body-end-function . nndoc-exim-bounce-body-end-function))
65 (article-begin . "^From - "))
67 (article-begin . "^Path:"))
69 (article-begin . "^#! *rnews +\\([0-9]+\\) *\n")
70 (body-end-function . nndoc-rnews-body-end))
72 (article-begin-function . nndoc-mbox-article-begin)
73 (body-end-function . nndoc-mbox-body-end))
75 (article-begin . "\^_\^L *\n")
77 (body-begin-function . nndoc-babyl-body-begin)
78 (head-begin-function . nndoc-babyl-head-begin))
80 (article-begin . "^--.*\n+")
82 (prepare-body-function . nndoc-unquote-dashes))
84 (article-begin . "^--__--__--\n\nMessage:")
85 (body-end . "^--__--__--$")
86 (prepare-body-function . nndoc-unquote-dashes))
88 (article-begin . "^ \\*")
89 (body-end . "^\t------*[ \t]^*\n^ \\*")
92 (generate-head-function . nndoc-generate-clari-briefs-head)
93 (article-transform-function . nndoc-transform-clari-briefs))
96 (head-begin . "^ ?\n")
100 (subtype digest guess))
102 (generate-head-function . nndoc-generate-mime-parts-head)
103 (article-transform-function . nndoc-transform-mime-parts))
105 (first-article . ,(concat "^" (make-string 70 ?-) "\n *\n+"))
106 (article-begin . ,(concat "^\n" (make-string 30 ?-) "\n *\n+"))
107 (prepare-body-function . nndoc-unquote-dashes)
108 (body-end-function . nndoc-digest-body-end)
110 (body-begin . "^ *\n")
111 (file-end . "^End of .*digest.*[0-9].*\n\\*\\*\\|^End of.*Digest *$")
112 (subtype digest guess))
114 (article-begin . "^------------------------------*[\n \t]+")
116 (body-end-function . nndoc-digest-body-end)
117 (body-begin . "^ ?$")
118 (file-end . "^End of")
119 (prepare-body-function . nndoc-unquote-dashes)
120 (subtype digest guess))
122 (article-begin . "^\\\\\\\\\n")
123 (head-begin . "^Paper.*:")
124 (head-end . "\\(^\\\\\\\\.*\n\\|-----------------\\)")
126 (body-end . "-------------------------------------------------")
127 (file-end . "^Title: Recent Seminal")
128 (generate-head-function . nndoc-generate-lanl-gov-head)
129 (article-transform-function . nndoc-transform-lanl-gov-announce)
130 (subtype preprints guess))
132 (article-begin . "^\n")
133 (body-end-function . nndoc-rfc822-forward-body-end-function))
135 (article-begin-function . nndoc-outlook-article-begin)
137 (oe-dbx ;; Outlook Express DBX format
138 (dissection-function . nndoc-oe-dbx-dissection)
139 (generate-head-function . nndoc-oe-dbx-generate-head)
140 (generate-article-function . nndoc-oe-dbx-generate-article))
142 (article-begin . "^-+ \\(Start of \\)?forwarded message.*\n+")
143 (body-end . "^-+ End \\(of \\)?forwarded message.*$")
144 (prepare-body-function . nndoc-unquote-dashes))
155 (defvar nndoc-binary-file-names ".[Dd][Bb][Xx]$"
156 "Regexp for binary nndoc file names.")
159 (defvoo nndoc-file-begin nil)
160 (defvoo nndoc-first-article nil)
161 (defvoo nndoc-article-begin nil)
162 (defvoo nndoc-head-begin nil)
163 (defvoo nndoc-head-end nil)
164 (defvoo nndoc-file-end nil)
165 (defvoo nndoc-body-begin nil)
166 (defvoo nndoc-body-end-function nil)
167 (defvoo nndoc-body-begin-function nil)
168 (defvoo nndoc-head-begin-function nil)
169 (defvoo nndoc-body-end nil)
170 ;; nndoc-dissection-alist is a list of sublists. Each sublist holds the
171 ;; following items. ARTICLE acts as the association key and is an ordinal
172 ;; starting at 1. HEAD-BEGIN [0], HEAD-END [1], BODY-BEGIN [2] and BODY-END
173 ;; [3] are positions in the `nndoc' buffer. LINE-COUNT [4] is a count of
174 ;; lines in the body. For MIME dissections only, ARTICLE-INSERT [5] and
175 ;; SUMMARY-INSERT [6] give headers to insert for full article or summary line
176 ;; generation, respectively. Other headers usually follow directly from the
177 ;; buffer. Value `nil' means no insert.
178 (defvoo nndoc-dissection-alist nil)
179 (defvoo nndoc-prepare-body-function nil)
180 (defvoo nndoc-generate-head-function nil)
181 (defvoo nndoc-article-transform-function nil)
182 (defvoo nndoc-article-begin-function nil)
183 (defvoo nndoc-generate-article-function nil)
184 (defvoo nndoc-dissection-function nil)
186 (defvoo nndoc-status-string "")
187 (defvoo nndoc-group-alist nil)
188 (defvoo nndoc-current-buffer nil
189 "Current nndoc news buffer.")
190 (defvoo nndoc-address nil)
192 (defconst nndoc-version "nndoc 1.0"
197 ;;; Interface functions
199 (nnoo-define-basics nndoc)
201 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
202 (when (nndoc-possibly-change-buffer newsgroup server)
204 (set-buffer nntp-server-buffer)
207 (if (stringp (car articles))
210 (when (setq entry (cdr (assq (setq article (pop articles))
211 nndoc-dissection-alist)))
212 (insert (format "221 %d Article retrieved.\n" article))
213 (if nndoc-generate-head-function
214 (funcall nndoc-generate-head-function article)
215 (insert-buffer-substring
216 nndoc-current-buffer (car entry) (nth 1 entry)))
217 (goto-char (point-max))
218 (unless (eq (char-after (1- (point))) ?\n)
220 (insert (format "Lines: %d\n" (nth 4 entry)))
223 (nnheader-fold-continuation-lines)
226 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
227 (nndoc-possibly-change-buffer newsgroup server)
229 (let ((buffer (or buffer nntp-server-buffer))
230 (entry (cdr (assq article nndoc-dissection-alist)))
236 ((stringp article) nil)
237 (nndoc-generate-article-function
238 (funcall nndoc-generate-article-function article))
240 (insert-buffer-substring
241 nndoc-current-buffer (car entry) (nth 1 entry))
244 (insert-buffer-substring
245 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
247 (when nndoc-prepare-body-function
248 (funcall nndoc-prepare-body-function))
249 (when nndoc-article-transform-function
250 (funcall nndoc-article-transform-function article))
253 (deffoo nndoc-request-group (group &optional server dont-check)
257 ((not (nndoc-possibly-change-buffer group server))
258 (nnheader-report 'nndoc "No such file or buffer: %s"
261 (nnheader-report 'nndoc "Selected group %s" group)
263 ((zerop (setq number (length nndoc-dissection-alist)))
264 (nndoc-close-group group)
265 (nnheader-report 'nndoc "No articles in group %s" group))
267 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
269 (deffoo nndoc-request-type (group &optional article)
270 (cond ((not article) 'unknown)
271 (nndoc-post-type nndoc-post-type)
274 (deffoo nndoc-close-group (group &optional server)
275 (nndoc-possibly-change-buffer group server)
276 (and nndoc-current-buffer
277 (buffer-name nndoc-current-buffer)
278 (kill-buffer nndoc-current-buffer))
279 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
281 (setq nndoc-current-buffer nil)
282 (nnoo-close-server 'nndoc server)
283 (setq nndoc-dissection-alist nil)
286 (deffoo nndoc-request-list (&optional server)
289 (deffoo nndoc-request-newgroups (date &optional server)
292 (deffoo nndoc-request-list-newsgroups (&optional server)
296 ;;; Internal functions.
298 (defun nndoc-possibly-change-buffer (group source)
301 ;; The current buffer is this group's buffer.
302 ((and nndoc-current-buffer
303 (buffer-name nndoc-current-buffer)
304 (eq nndoc-current-buffer
305 (setq buf (cdr (assoc group nndoc-group-alist))))))
306 ;; We change buffers by taking an old from the group alist.
307 ;; `source' is either a string (a file name) or a buffer object.
309 (setq nndoc-current-buffer buf))
310 ;; It's a totally new group.
311 ((or (and (bufferp nndoc-address)
312 (buffer-name nndoc-address))
313 (and (stringp nndoc-address)
314 (file-exists-p nndoc-address)
315 (not (file-directory-p nndoc-address))))
316 (push (cons group (setq nndoc-current-buffer
318 (concat " *nndoc " group "*"))))
320 (setq nndoc-dissection-alist nil)
322 (set-buffer nndoc-current-buffer)
324 (if (and (stringp nndoc-address)
325 (string-match nndoc-binary-file-names nndoc-address))
326 (let ((nnheader-file-coding-system 'binary))
327 (nnheader-insert-file-contents nndoc-address))
328 (if (stringp nndoc-address)
329 (nnheader-insert-file-contents nndoc-address)
330 (insert-buffer-substring nndoc-address))
331 (run-hooks 'nndoc-open-document-hook)))))
332 ;; Initialize the nndoc structures according to this new document.
333 (when (and nndoc-current-buffer
334 (not nndoc-dissection-alist))
336 (set-buffer nndoc-current-buffer)
338 (if (eq nndoc-article-type 'mime-parts)
339 (nndoc-dissect-mime-parts)
340 (nndoc-dissect-buffer))))
341 (unless nndoc-current-buffer
342 (nndoc-close-server))
343 ;; Return whether we managed to select a file.
344 nndoc-current-buffer))
347 ;;; Deciding what document type we have
350 (defun nndoc-set-delims ()
351 "Set the nndoc delimiter variables according to the type of the document."
352 (let ((vars '(nndoc-file-begin
354 nndoc-article-begin-function
355 nndoc-head-begin nndoc-head-end
356 nndoc-file-end nndoc-article-begin
357 nndoc-body-begin nndoc-body-end-function nndoc-body-end
358 nndoc-prepare-body-function nndoc-article-transform-function
359 nndoc-generate-head-function nndoc-body-begin-function
360 nndoc-head-begin-function
361 nndoc-generate-article-function
362 nndoc-dissection-function)))
364 (set (pop vars) nil)))
366 ;; Guess away until we find the real file type.
367 (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
369 (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
370 ;; Set the nndoc variables.
372 (set (intern (format "nndoc-%s" (caar defs)))
375 (defun nndoc-guess-type (subtype)
376 (let ((alist nndoc-type-alist)
377 results result entry)
378 (while (and (not result)
379 (setq entry (pop alist)))
380 (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
381 (goto-char (point-min))
382 ;; Remove blank lines.
383 (while (eq (following-char) ?\n)
385 (when (numberp (setq result (funcall (intern
386 (format "nndoc-%s-type-p"
388 (push (cons result entry) results)
390 (unless (or result results)
391 (error "Document is not of any recognized type"))
394 (cadar (sort results 'car-less-than-car)))))
397 ;;; Built-in type predicates and functions
400 (defun nndoc-mbox-type-p ()
401 (when (looking-at message-unix-mail-delimiter)
404 (defun nndoc-mbox-article-begin ()
405 (when (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
406 (goto-char (match-beginning 0))))
408 (defun nndoc-mbox-body-end ()
413 (and (re-search-backward
414 (concat "^" message-unix-mail-delimiter) nil t)
416 (search-forward "\n\n" beg t)
418 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
419 (setq len (string-to-int (match-string 1)))
420 (search-forward "\n\n" beg t)
421 (unless (= (setq len (+ (point) len)) (point-max))
422 (and (< len (point-max))
424 (looking-at message-unix-mail-delimiter)))))
427 (defun nndoc-mmdf-type-p ()
428 (when (looking-at "\^A\^A\^A\^A$")
431 (defun nndoc-news-type-p ()
432 (when (looking-at "^Path:.*\n")
435 (defun nndoc-rnews-type-p ()
436 (when (looking-at "#! *rnews")
439 (defun nndoc-rnews-body-end ()
440 (and (re-search-backward nndoc-article-begin nil t)
442 (goto-char (+ (point) (string-to-int (match-string 1))))))
444 (defun nndoc-babyl-type-p ()
445 (when (re-search-forward "\^_\^L *\n" nil t)
448 (defun nndoc-babyl-body-begin ()
449 (re-search-forward "^\n" nil t)
450 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
451 (let ((next (or (save-excursion
452 (re-search-forward nndoc-article-begin nil t))
454 (unless (re-search-forward "^\n" next t)
458 (forward-line -1)))))
460 (defun nndoc-babyl-head-begin ()
461 (when (re-search-forward "^[0-9].*\n" nil t)
462 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
466 (defun nndoc-forward-type-p ()
467 (when (and (re-search-forward "^-+ \\(Start of \\)?forwarded message.*\n+"
469 (looking-at "[\r\n]*[a-zA-Z][a-zA-Z0-9-]*:"))
472 (defun nndoc-rfc934-type-p ()
473 (when (and (re-search-forward "^-+ Start of forwarded.*\n+" nil t)
474 (not (re-search-forward "^Subject:.*digest" nil t))
475 (not (re-search-backward "^From:" nil t 2))
476 (not (re-search-forward "^From:" nil t 2)))
479 (defun nndoc-mailman-type-p ()
480 (when (re-search-forward "^--__--__--\n+" nil t)
483 (defun nndoc-rfc822-forward-type-p ()
485 (message-narrow-to-head)
486 (when (re-search-forward "^Content-Type: *message/rfc822" nil t)
489 (defun nndoc-rfc822-forward-body-end-function ()
490 (goto-char (point-max)))
492 (defun nndoc-mime-parts-type-p ()
493 (let ((case-fold-search t)
494 (limit (search-forward "\n\n" nil t)))
495 (goto-char (point-min))
499 ^Content-Type:[ \t]*multipart/[a-z]+ *; *\\(\\(\n[ \t]\\)?.*;\\)*"
500 "\\(\n[ \t]\\)?[ \t]*boundary=\"?[^\"\n]*[^\" \t\n]")
504 (defun nndoc-transform-mime-parts (article)
505 (let* ((entry (cdr (assq article nndoc-dissection-alist)))
506 (headers (nth 5 entry)))
508 (goto-char (point-min))
511 (defun nndoc-generate-mime-parts-head (article)
512 (let* ((entry (cdr (assq article nndoc-dissection-alist)))
513 (headers (nth 6 entry)))
515 (narrow-to-region (point) (point))
516 (insert-buffer-substring
517 nndoc-current-buffer (car entry) (nth 1 entry))
518 (goto-char (point-max)))
522 (defun nndoc-clari-briefs-type-p ()
523 (when (let ((case-fold-search nil))
524 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
527 (defun nndoc-transform-clari-briefs (article)
528 (goto-char (point-min))
529 (when (looking-at " *\\*\\(.*\\)\n")
530 (replace-match "" t t))
531 (nndoc-generate-clari-briefs-head article))
533 (defun nndoc-generate-clari-briefs-head (article)
534 (let ((entry (cdr (assq article nndoc-dissection-alist)))
537 (set-buffer nndoc-current-buffer)
539 (narrow-to-region (car entry) (nth 3 entry))
540 (goto-char (point-min))
541 (when (looking-at " *\\*\\(.*\\)$")
542 (setq subject (match-string 1))
543 (when (string-match "[ \t]+$" subject)
544 (setq subject (substring subject 0 (match-beginning 0)))))
546 (let ((case-fold-search nil))
548 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
549 (setq from (match-string 1)))))
550 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
551 "\nSubject: " (or subject "(no subject)") "\n")))
553 (defun nndoc-exim-bounce-type-p ()
554 (and (re-search-forward "^------ This is a copy of the message, including all the headers. ------" nil t)
557 (defun nndoc-exim-bounce-body-end-function ()
558 (goto-char (point-max)))
561 (defun nndoc-mime-digest-type-p ()
562 (let ((case-fold-search t)
563 boundary-id b-delimiter entry)
566 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
567 "boundary=\"?\\([^\"\n]*[^\" \t\n]\\)")
570 (setq boundary-id (match-string 1)
571 b-delimiter (concat "\n--" boundary-id "[ \t]*$"))
572 (setq entry (assq 'mime-digest nndoc-type-alist))
575 (cons 'head-begin "^ ?\n")
576 (cons 'head-end "^ ?$")
577 (cons 'body-begin "^ ?\n")
578 (cons 'article-begin b-delimiter)
579 (cons 'body-end-function 'nndoc-digest-body-end)
580 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
583 (defun nndoc-standard-digest-type-p ()
584 (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
586 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
589 (defun nndoc-digest-body-end ()
590 (and (re-search-forward nndoc-article-begin nil t)
591 (goto-char (match-beginning 0))))
593 (defun nndoc-slack-digest-type-p ()
596 (defun nndoc-lanl-gov-announce-type-p ()
597 (when (let ((case-fold-search nil))
598 (re-search-forward "^\\\\\\\\\nPaper: [a-z-]+/[0-9]+" nil t))
601 (defun nndoc-transform-lanl-gov-announce (article)
602 (goto-char (point-max))
603 (when (re-search-backward "^\\\\\\\\ +(\\([^ ]*\\) , *\\([^ ]*\\))" nil t)
604 (replace-match "\n\nGet it at \\1 (\\2)" t nil)))
606 (defun nndoc-generate-lanl-gov-head (article)
607 (let ((entry (cdr (assq article nndoc-dissection-alist)))
608 (e-mail "no address given")
611 (set-buffer nndoc-current-buffer)
613 (narrow-to-region (car entry) (nth 1 entry))
614 (goto-char (point-min))
615 (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
616 (setq subject (concat " (" (match-string 1) ")"))
617 (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
618 (setq e-mail (match-string 1)))
619 (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
621 (setq subject (concat (match-string 1) subject))
622 (setq from (concat (match-string 2) " <" e-mail ">"))))))
623 (while (and from (string-match "(\[^)\]*)" from))
624 (setq from (replace-match "" t t from)))
625 (insert "From: " (or from "unknown")
626 "\nSubject: " (or subject "(no subject)") "\n")))
628 (defun nndoc-nsmail-type-p ()
629 (when (looking-at "From - ")
632 (defun nndoc-outlook-article-begin ()
633 (prog1 (re-search-forward "From:\\|Received:" nil t)
634 (goto-char (match-beginning 0))))
636 (defun nndoc-outlook-type-p ()
637 ;; FIXME: Is JMF the magic of outlook mailbox? -- ShengHuo.
640 (defun nndoc-oe-dbx-type-p ()
641 (looking-at (string-as-multibyte "\317\255\022\376")))
643 (defun nndoc-read-little-endian ()
644 (+ (prog1 (char-after) (forward-char 1))
645 (lsh (prog1 (char-after) (forward-char 1)) 8)
646 (lsh (prog1 (char-after) (forward-char 1)) 16)
647 (lsh (prog1 (char-after) (forward-char 1)) 24)))
649 (defun nndoc-oe-dbx-decode-block ()
651 (nndoc-read-little-endian) ;; this address
652 (nndoc-read-little-endian) ;; next address offset
653 (nndoc-read-little-endian) ;; blocksize
654 (nndoc-read-little-endian))) ;; next address
656 (defun nndoc-oe-dbx-dissection ()
657 (let ((i 0) blk p tp)
658 (goto-char 60117) ;; 0x0000EAD4+1
661 (setq blk (nndoc-oe-dbx-decode-block)))
662 (while (and blk (> (car blk) 0) (or (zerop (nth 3 blk))
664 (push (list (incf i) p nil nil nil 0) nndoc-dissection-alist)
665 (while (and (> (car blk) 0) (> (nth 3 blk) p))
666 (goto-char (1+ (nth 3 blk)))
667 (setq blk (nndoc-oe-dbx-decode-block)))
668 (if (or (<= (car blk) p)
670 (not (zerop (nth 3 blk))))
672 (setq tp (+ (car blk) (nth 1 blk) 17))
673 (if (or (<= tp p) (>= tp (point-max)))
677 blk (nndoc-oe-dbx-decode-block)))))))
679 (defun nndoc-oe-dbx-generate-article (article &optional head)
680 (let ((entry (cdr (assq article nndoc-dissection-alist)))
681 (cur (current-buffer))
684 (with-current-buffer nndoc-current-buffer
686 (while (> p (point-min))
688 (setq blk (nndoc-oe-dbx-decode-block))
690 (with-current-buffer cur
691 (insert-buffer-substring nndoc-current-buffer p (+ p (nth 2 blk))))
692 (setq p (1+ (nth 3 blk)))))
694 (while (re-search-forward "\r$" nil t)
695 (delete-backward-char 1))
698 (when (search-forward "\n\n" nil t)
699 (setcar (cddddr entry) (count-lines (point) (point-max)))
700 (delete-region (1- (point)) (point-max))))
703 (defun nndoc-oe-dbx-generate-head (article)
704 (nndoc-oe-dbx-generate-article article 'head))
706 (deffoo nndoc-request-accept-article (group &optional server last)
710 ;;; Functions for dissecting the documents
713 (defun nndoc-search (regexp)
715 (re-search-forward regexp nil t)
716 (beginning-of-line)))
718 (defun nndoc-dissect-buffer ()
719 "Go through the document and partition it into heads/bodies/articles."
722 head-begin head-end body-begin body-end)
723 (setq nndoc-dissection-alist nil)
725 (set-buffer nndoc-current-buffer)
726 (goto-char (point-min))
727 ;; Remove blank lines.
728 (while (eq (following-char) ?\n)
730 (if nndoc-dissection-function
731 (funcall nndoc-dissection-function)
732 ;; Find the beginning of the file.
733 (when nndoc-file-begin
734 (nndoc-search nndoc-file-begin))
735 ;; Go through the file.
736 (while (if (and first nndoc-first-article)
737 (nndoc-search nndoc-first-article)
738 (nndoc-article-begin))
740 (cond (nndoc-head-begin-function
741 (funcall nndoc-head-begin-function))
743 (nndoc-search nndoc-head-begin)))
746 (looking-at nndoc-file-end)))
747 (goto-char (point-max))
748 (setq head-begin (point))
749 (nndoc-search (or nndoc-head-end "^$"))
750 (setq head-end (point))
751 (if nndoc-body-begin-function
752 (funcall nndoc-body-begin-function)
753 (nndoc-search (or nndoc-body-begin "^\n")))
754 (setq body-begin (point))
755 (or (and nndoc-body-end-function
756 (funcall nndoc-body-end-function))
758 (nndoc-search nndoc-body-end))
759 (nndoc-article-begin)
761 (goto-char (point-max))
763 (and (re-search-backward nndoc-file-end nil t)
764 (beginning-of-line)))))
765 (setq body-end (point))
766 (push (list (incf i) head-begin head-end body-begin body-end
767 (count-lines body-begin body-end))
768 nndoc-dissection-alist)))))))
770 (defun nndoc-article-begin ()
771 (if nndoc-article-begin-function
772 (funcall nndoc-article-begin-function)
774 (nndoc-search nndoc-article-begin))))
776 (defun nndoc-unquote-dashes ()
777 "Unquote quoted non-separators in digests."
778 (while (re-search-forward "^- -"nil t)
779 (replace-match "-" t t)))
781 ;; Against compiler warnings.
782 (defvar nndoc-mime-split-ordinal)
784 (defun nndoc-dissect-mime-parts ()
785 "Go through a MIME composite article and partition it into sub-articles.
786 When a MIME entity contains sub-entities, dissection produces one article for
787 the header of this entity, and one article per sub-entity."
788 (setq nndoc-dissection-alist nil
789 nndoc-mime-split-ordinal 0)
791 (set-buffer nndoc-current-buffer)
792 (nndoc-dissect-mime-parts-sub (point-min) (point-max) nil nil nil)))
794 (defun nndoc-dissect-mime-parts-sub (head-begin body-end article-insert
796 "Dissect an entity, within a composite MIME message.
797 The complete message or MIME entity extends from HEAD-BEGIN to BODY-END.
798 ARTICLE-INSERT should be added at beginning for generating a full article.
799 The string POSITION holds a dotted decimal representation of the article
800 position in the hierarchical structure, it is nil for the outer entity.
801 PARENT is the message-ID of the parent summary line, or nil for none."
802 (let ((case-fold-search t)
803 (message-id (nnmail-message-id))
804 head-end body-begin summary-insert message-rfc822 multipart-any
805 subject content-type type subtype boundary-regexp)
806 ;; Gracefully handle a missing body.
807 (goto-char head-begin)
808 (if (or (and (eq (char-after) ?\n) (or (forward-char 1) t))
809 (search-forward "\n\n" body-end t))
810 (setq head-end (1- (point))
812 (setq head-end body-end
813 body-begin body-end))
814 (narrow-to-region head-begin head-end)
815 ;; Save MIME attributes.
816 (goto-char head-begin)
817 (setq content-type (message-fetch-field "Content-Type"))
820 "^ *\\([^ \t\n/;]+\\)/\\([^ \t\n/;]+\\)" content-type)
821 (setq type (downcase (match-string 1 content-type))
822 subtype (downcase (match-string 2 content-type))
823 message-rfc822 (and (string= type "message")
824 (string= subtype "rfc822"))
825 multipart-any (string= type "multipart")))
826 (when (string-match ";[ \t\n]*name=\\([^ \t\n;]+\\)" content-type)
827 (setq subject (match-string 1 content-type)))
828 (when (string-match "boundary=\"?\\([^\"\n]*[^\" \t\n]\\)" content-type)
829 (setq boundary-regexp (concat "^--"
831 (match-string 1 content-type))
832 "\\(--\\)?[ \t]*\n"))))
834 (when (or multipart-any (not article-insert))
835 (setq subject (message-fetch-field "Subject"))))
839 ;; Prepare the article and summary inserts.
840 (unless article-insert
841 (setq article-insert (buffer-substring (point-min) (point-max))
842 head-end head-begin))
843 (setq summary-insert article-insert)
844 ;; - summary Subject.
846 (let ((line (concat "Subject: <" position
847 (and position multipart-any ".")
848 (and multipart-any "*")
849 (and (or position multipart-any) " ")
850 (cond ((string= subtype "plain") type)
851 ((string= subtype "basic") type)
857 (if (string-match "Subject:.*\n\\([ \t].*\n\\)*" summary-insert)
858 (replace-match line t t summary-insert)
859 (concat summary-insert line))))
860 ;; - summary Message-ID.
862 (let ((line (concat "Message-ID: " message-id "\n")))
863 (if (string-match "Message-ID:.*\n\\([ \t].*\n\\)*" summary-insert)
864 (replace-match line t t summary-insert)
865 (concat summary-insert line))))
866 ;; - summary References.
869 (let ((line (concat "References: " parent "\n")))
870 (if (string-match "References:.*\n\\([ \t].*\n\\)*"
872 (replace-match line t t summary-insert)
873 (concat summary-insert line)))))
874 ;; Generate dissection information for this entity.
875 (push (list (incf nndoc-mime-split-ordinal)
876 head-begin head-end body-begin body-end
877 (count-lines body-begin body-end)
878 article-insert summary-insert)
879 nndoc-dissection-alist)
880 ;; Recurse for all sub-entities, if any.
885 (nndoc-dissect-mime-parts-sub body-begin body-end nil
886 position message-id)))
887 ((and multipart-any boundary-regexp)
888 (let ((part-counter 0)
889 part-begin part-end eof-flag)
890 (while (string-match "\
891 ^\\(Lines\\|Content-\\(Type\\|Transfer-Encoding\\|Disposition\\)\\):.*\n\\([ \t].*\n\\)*"
893 (setq article-insert (replace-match "" t t article-insert)))
894 (let ((case-fold-search nil))
895 (goto-char body-begin)
896 (setq eof-flag (not (re-search-forward boundary-regexp body-end t)))
897 (while (not eof-flag)
898 (setq part-begin (point))
899 (cond ((re-search-forward boundary-regexp body-end t)
900 (or (not (match-string 1))
901 (string= (match-string 1) "")
904 (setq part-end (point))
906 (t (setq part-end body-end
909 (nndoc-dissect-mime-parts-sub
910 part-begin part-end article-insert
913 (format "%d" (incf part-counter)))
917 (defun nndoc-add-type (definition &optional position)
918 "Add document DEFINITION to the list of nndoc document definitions.
919 If POSITION is nil or `last', the definition will be added
920 as the last checked definition, if t or `first', add as the
921 first definition, and if any other symbol, add after that
922 symbol in the alist."
923 ;; First remove any old instances.
924 (gnus-pull (car definition) nndoc-type-alist)
925 ;; Then enter the new definition in the proper place.
927 ((or (null position) (eq position 'last))
928 (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
929 ((or (eq position t) (eq position 'first))
930 (push definition nndoc-type-alist))
932 (let ((list (memq (assq position nndoc-type-alist)
935 (error "No such position: %s" position))
936 (setcdr list (cons definition (cdr list)))))))
940 ;;; nndoc.el ends here