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/
38 (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))
145 (mail-in-mail ;; Wild guess on mailer daemon's messages or others
146 (article-begin-function . nndoc-mail-in-mail-article-begin))
157 (defvar nndoc-binary-file-names ".[Dd][Bb][Xx]$"
158 "Regexp for binary nndoc file names.")
161 (defvoo nndoc-file-begin nil)
162 (defvoo nndoc-first-article nil)
163 (defvoo nndoc-article-begin nil)
164 (defvoo nndoc-head-begin nil)
165 (defvoo nndoc-head-end nil)
166 (defvoo nndoc-file-end nil)
167 (defvoo nndoc-body-begin nil)
168 (defvoo nndoc-body-end-function nil)
169 (defvoo nndoc-body-begin-function nil)
170 (defvoo nndoc-head-begin-function nil)
171 (defvoo nndoc-body-end nil)
172 ;; nndoc-dissection-alist is a list of sublists. Each sublist holds the
173 ;; following items. ARTICLE acts as the association key and is an ordinal
174 ;; starting at 1. HEAD-BEGIN [0], HEAD-END [1], BODY-BEGIN [2] and BODY-END
175 ;; [3] are positions in the `nndoc' buffer. LINE-COUNT [4] is a count of
176 ;; lines in the body. For MIME dissections only, ARTICLE-INSERT [5] and
177 ;; SUMMARY-INSERT [6] give headers to insert for full article or summary line
178 ;; generation, respectively. Other headers usually follow directly from the
179 ;; buffer. Value `nil' means no insert.
180 (defvoo nndoc-dissection-alist nil)
181 (defvoo nndoc-prepare-body-function nil)
182 (defvoo nndoc-generate-head-function nil)
183 (defvoo nndoc-article-transform-function nil)
184 (defvoo nndoc-article-begin-function nil)
185 (defvoo nndoc-generate-article-function nil)
186 (defvoo nndoc-dissection-function nil)
188 (defvoo nndoc-status-string "")
189 (defvoo nndoc-group-alist nil)
190 (defvoo nndoc-current-buffer nil
191 "Current nndoc news buffer.")
192 (defvoo nndoc-address nil)
194 (defconst nndoc-version "nndoc 1.0"
199 ;;; Interface functions
201 (nnoo-define-basics nndoc)
203 (deffoo nndoc-retrieve-headers (articles &optional newsgroup server fetch-old)
204 (when (nndoc-possibly-change-buffer newsgroup server)
206 (set-buffer nntp-server-buffer)
209 (if (stringp (car articles))
212 (when (setq entry (cdr (assq (setq article (pop articles))
213 nndoc-dissection-alist)))
214 (insert (format "221 %d Article retrieved.\n" article))
215 (if nndoc-generate-head-function
216 (funcall nndoc-generate-head-function article)
217 (insert-buffer-substring
218 nndoc-current-buffer (car entry) (nth 1 entry)))
219 (goto-char (point-max))
220 (unless (eq (char-after (1- (point))) ?\n)
222 (insert (format "Lines: %d\n" (nth 4 entry)))
225 (nnheader-fold-continuation-lines)
228 (deffoo nndoc-request-article (article &optional newsgroup server buffer)
229 (nndoc-possibly-change-buffer newsgroup server)
231 (let ((buffer (or buffer nntp-server-buffer))
232 (entry (cdr (assq article nndoc-dissection-alist)))
238 ((stringp article) nil)
239 (nndoc-generate-article-function
240 (funcall nndoc-generate-article-function article))
242 (insert-buffer-substring
243 nndoc-current-buffer (car entry) (nth 1 entry))
246 (insert-buffer-substring
247 nndoc-current-buffer (nth 2 entry) (nth 3 entry))
249 (when nndoc-prepare-body-function
250 (funcall nndoc-prepare-body-function))
251 (when nndoc-article-transform-function
252 (funcall nndoc-article-transform-function article))
255 (deffoo nndoc-request-group (group &optional server dont-check)
259 ((not (nndoc-possibly-change-buffer group server))
260 (nnheader-report 'nndoc "No such file or buffer: %s"
263 (nnheader-report 'nndoc "Selected group %s" group)
265 ((zerop (setq number (length nndoc-dissection-alist)))
266 (nndoc-close-group group)
267 (nnheader-report 'nndoc "No articles in group %s" group))
269 (nnheader-insert "211 %d %d %d %s\n" number 1 number group)))))
271 (deffoo nndoc-request-type (group &optional article)
272 (cond ((not article) 'unknown)
273 (nndoc-post-type nndoc-post-type)
276 (deffoo nndoc-close-group (group &optional server)
277 (nndoc-possibly-change-buffer group server)
278 (and nndoc-current-buffer
279 (buffer-name nndoc-current-buffer)
280 (kill-buffer nndoc-current-buffer))
281 (setq nndoc-group-alist (delq (assoc group nndoc-group-alist)
283 (setq nndoc-current-buffer nil)
284 (nnoo-close-server 'nndoc server)
285 (setq nndoc-dissection-alist nil)
288 (deffoo nndoc-request-list (&optional server)
291 (deffoo nndoc-request-newgroups (date &optional server)
294 (deffoo nndoc-request-list-newsgroups (&optional server)
298 ;;; Internal functions.
300 (defun nndoc-possibly-change-buffer (group source)
303 ;; The current buffer is this group's buffer.
304 ((and nndoc-current-buffer
305 (buffer-name nndoc-current-buffer)
306 (eq nndoc-current-buffer
307 (setq buf (cdr (assoc group nndoc-group-alist))))))
308 ;; We change buffers by taking an old from the group alist.
309 ;; `source' is either a string (a file name) or a buffer object.
311 (setq nndoc-current-buffer buf))
312 ;; It's a totally new group.
313 ((or (and (bufferp nndoc-address)
314 (buffer-name nndoc-address))
315 (and (stringp nndoc-address)
316 (file-exists-p nndoc-address)
317 (not (file-directory-p nndoc-address))))
318 (push (cons group (setq nndoc-current-buffer
320 (concat " *nndoc " group "*"))))
322 (setq nndoc-dissection-alist nil)
324 (set-buffer nndoc-current-buffer)
326 (if (and (stringp nndoc-address)
327 (string-match nndoc-binary-file-names nndoc-address))
328 (let ((coding-system-for-read 'binary))
329 (mm-insert-file-contents nndoc-address))
330 (if (stringp nndoc-address)
331 (nnheader-insert-file-contents nndoc-address)
332 (insert-buffer-substring nndoc-address))
333 (run-hooks 'nndoc-open-document-hook)))))
334 ;; Initialize the nndoc structures according to this new document.
335 (when (and nndoc-current-buffer
336 (not nndoc-dissection-alist))
338 (set-buffer nndoc-current-buffer)
340 (if (eq nndoc-article-type 'mime-parts)
341 (nndoc-dissect-mime-parts)
342 (nndoc-dissect-buffer))))
343 (unless nndoc-current-buffer
344 (nndoc-close-server))
345 ;; Return whether we managed to select a file.
346 nndoc-current-buffer))
349 ;;; Deciding what document type we have
352 (defun nndoc-set-delims ()
353 "Set the nndoc delimiter variables according to the type of the document."
354 (let ((vars '(nndoc-file-begin
356 nndoc-article-begin-function
357 nndoc-head-begin nndoc-head-end
358 nndoc-file-end nndoc-article-begin
359 nndoc-body-begin nndoc-body-end-function nndoc-body-end
360 nndoc-prepare-body-function nndoc-article-transform-function
361 nndoc-generate-head-function nndoc-body-begin-function
362 nndoc-head-begin-function
363 nndoc-generate-article-function
364 nndoc-dissection-function)))
366 (set (pop vars) nil)))
368 ;; Guess away until we find the real file type.
369 (while (assq 'guess (setq defs (cdr (assq nndoc-article-type
371 (setq nndoc-article-type (nndoc-guess-type nndoc-article-type)))
372 ;; Set the nndoc variables.
374 (set (intern (format "nndoc-%s" (caar defs)))
377 (defun nndoc-guess-type (subtype)
378 (let ((alist nndoc-type-alist)
379 results result entry)
380 (while (and (not result)
381 (setq entry (pop alist)))
382 (when (memq subtype (or (cdr (assq 'subtype entry)) '(guess)))
383 (goto-char (point-min))
384 ;; Remove blank lines.
385 (while (eq (following-char) ?\n)
387 (when (numberp (setq result (funcall (intern
388 (format "nndoc-%s-type-p"
390 (push (cons result entry) results)
392 (unless (or result results)
393 (error "Document is not of any recognized type"))
396 (cadar (sort results 'car-less-than-car)))))
399 ;;; Built-in type predicates and functions
402 (defun nndoc-mbox-type-p ()
403 (when (looking-at message-unix-mail-delimiter)
406 (defun nndoc-mbox-article-begin ()
407 (when (re-search-forward (concat "^" message-unix-mail-delimiter) nil t)
408 (goto-char (match-beginning 0))))
410 (defun nndoc-mbox-body-end ()
415 (and (re-search-backward
416 (concat "^" message-unix-mail-delimiter) nil t)
418 (search-forward "\n\n" beg t)
420 "^Content-Length:[ \t]*\\([0-9]+\\) *$" end t)
421 (setq len (string-to-int (match-string 1)))
422 (search-forward "\n\n" beg t)
423 (unless (= (setq len (+ (point) len)) (point-max))
424 (and (< len (point-max))
426 (looking-at message-unix-mail-delimiter)))))
429 (defun nndoc-mmdf-type-p ()
430 (when (looking-at "\^A\^A\^A\^A$")
433 (defun nndoc-news-type-p ()
434 (when (looking-at "^Path:.*\n")
437 (defun nndoc-rnews-type-p ()
438 (when (looking-at "#! *rnews")
441 (defun nndoc-rnews-body-end ()
442 (and (re-search-backward nndoc-article-begin nil t)
444 (goto-char (+ (point) (string-to-int (match-string 1))))))
446 (defun nndoc-babyl-type-p ()
447 (when (re-search-forward "\^_\^L *\n" nil t)
450 (defun nndoc-babyl-body-begin ()
451 (re-search-forward "^\n" nil t)
452 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
453 (let ((next (or (save-excursion
454 (re-search-forward nndoc-article-begin nil t))
456 (unless (re-search-forward "^\n" next t)
460 (forward-line -1)))))
462 (defun nndoc-babyl-head-begin ()
463 (when (re-search-forward "^[0-9].*\n" nil t)
464 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
468 (defun nndoc-forward-type-p ()
469 (when (and (re-search-forward "^-+ \\(Start of \\)?forwarded message.*\n+"
471 (looking-at "[\r\n]*[a-zA-Z][a-zA-Z0-9-]*:"))
474 (defun nndoc-rfc934-type-p ()
475 (when (and (re-search-forward "^-+ Start of forwarded.*\n+" nil t)
476 (not (re-search-forward "^Subject:.*digest" nil t))
477 (not (re-search-backward "^From:" nil t 2))
478 (not (re-search-forward "^From:" nil t 2)))
481 (defun nndoc-mailman-type-p ()
482 (when (re-search-forward "^--__--__--\n+" nil t)
485 (defun nndoc-rfc822-forward-type-p ()
487 (message-narrow-to-head)
488 (when (re-search-forward "^Content-Type: *message/rfc822" nil t)
491 (defun nndoc-rfc822-forward-body-end-function ()
492 (goto-char (point-max)))
494 (defun nndoc-mime-parts-type-p ()
495 (let ((case-fold-search t)
496 (limit (search-forward "\n\n" nil t)))
497 (goto-char (point-min))
501 ^Content-Type:[ \t]*multipart/[a-z]+ *; *\\(\\(\n[ \t]\\)?.*;\\)*"
502 "\\(\n[ \t]\\)?[ \t]*boundary=\"?[^\"\n]*[^\" \t\n]")
506 (defun nndoc-transform-mime-parts (article)
507 (let* ((entry (cdr (assq article nndoc-dissection-alist)))
508 (headers (nth 5 entry)))
510 (goto-char (point-min))
513 (defun nndoc-generate-mime-parts-head (article)
514 (let* ((entry (cdr (assq article nndoc-dissection-alist)))
515 (headers (nth 6 entry)))
517 (narrow-to-region (point) (point))
518 (insert-buffer-substring
519 nndoc-current-buffer (car entry) (nth 1 entry))
520 (goto-char (point-max)))
524 (defun nndoc-clari-briefs-type-p ()
525 (when (let ((case-fold-search nil))
526 (re-search-forward "^\t[^a-z]+ ([^a-z]+) --" nil t))
529 (defun nndoc-transform-clari-briefs (article)
530 (goto-char (point-min))
531 (when (looking-at " *\\*\\(.*\\)\n")
532 (replace-match "" t t))
533 (nndoc-generate-clari-briefs-head article))
535 (defun nndoc-generate-clari-briefs-head (article)
536 (let ((entry (cdr (assq article nndoc-dissection-alist)))
539 (set-buffer nndoc-current-buffer)
541 (narrow-to-region (car entry) (nth 3 entry))
542 (goto-char (point-min))
543 (when (looking-at " *\\*\\(.*\\)$")
544 (setq subject (match-string 1))
545 (when (string-match "[ \t]+$" subject)
546 (setq subject (substring subject 0 (match-beginning 0)))))
548 (let ((case-fold-search nil))
550 "^\t\\([^a-z]+\\(,[^(]+\\)? ([^a-z]+)\\) --" nil t))
551 (setq from (match-string 1)))))
552 (insert "From: " "clari@clari.net (" (or from "unknown") ")"
553 "\nSubject: " (or subject "(no subject)") "\n")))
555 (defun nndoc-exim-bounce-type-p ()
556 (and (re-search-forward "^------ This is a copy of the message, including all the headers. ------" nil t)
559 (defun nndoc-exim-bounce-body-end-function ()
560 (goto-char (point-max)))
563 (defun nndoc-mime-digest-type-p ()
564 (let ((case-fold-search t)
565 boundary-id b-delimiter entry)
568 (concat "^Content-Type: *multipart/digest;[ \t\n]*[ \t]"
569 "boundary=\"?\\([^\"\n]*[^\" \t\n]\\)")
572 (setq boundary-id (match-string 1)
573 b-delimiter (concat "\n--" boundary-id "[ \t]*$"))
574 (setq entry (assq 'mime-digest nndoc-type-alist))
577 (cons 'head-begin "^ ?\n")
578 (cons 'head-end "^ ?$")
579 (cons 'body-begin "^ ?\n")
580 (cons 'article-begin b-delimiter)
581 (cons 'body-end-function 'nndoc-digest-body-end)
582 (cons 'file-end (concat "\n--" boundary-id "--[ \t]*$"))))
585 (defun nndoc-standard-digest-type-p ()
586 (when (and (re-search-forward (concat "^" (make-string 70 ?-) "\n\n") nil t)
588 (concat "\n\n" (make-string 30 ?-) "\n\n") nil t))
591 (defun nndoc-digest-body-end ()
592 (and (re-search-forward nndoc-article-begin nil t)
593 (goto-char (match-beginning 0))))
595 (defun nndoc-slack-digest-type-p ()
598 (defun nndoc-lanl-gov-announce-type-p ()
599 (when (let ((case-fold-search nil))
600 (re-search-forward "^\\\\\\\\\nPaper: [a-z-]+/[0-9]+" nil t))
603 (defun nndoc-transform-lanl-gov-announce (article)
604 (goto-char (point-max))
605 (when (re-search-backward "^\\\\\\\\ +(\\([^ ]*\\) , *\\([^ ]*\\))" nil t)
606 (replace-match "\n\nGet it at \\1 (\\2)" t nil)))
608 (defun nndoc-generate-lanl-gov-head (article)
609 (let ((entry (cdr (assq article nndoc-dissection-alist)))
610 (e-mail "no address given")
613 (set-buffer nndoc-current-buffer)
615 (narrow-to-region (car entry) (nth 1 entry))
616 (goto-char (point-min))
617 (when (looking-at "^Paper.*: \\([a-z-]+/[0-9]+\\)")
618 (setq subject (concat " (" (match-string 1) ")"))
619 (when (re-search-forward "^From: \\([^ ]+\\)" nil t)
620 (setq e-mail (match-string 1)))
621 (when (re-search-forward "^Title: \\([^\f]*\\)\nAuthors?: \\(.*\\)"
623 (setq subject (concat (match-string 1) subject))
624 (setq from (concat (match-string 2) " <" e-mail ">"))))))
625 (while (and from (string-match "(\[^)\]*)" from))
626 (setq from (replace-match "" t t from)))
627 (insert "From: " (or from "unknown")
628 "\nSubject: " (or subject "(no subject)") "\n")))
630 (defun nndoc-nsmail-type-p ()
631 (when (looking-at "From - ")
634 (defun nndoc-outlook-article-begin ()
635 (prog1 (re-search-forward "From:\\|Received:" nil t)
636 (goto-char (match-beginning 0))))
638 (defun nndoc-outlook-type-p ()
639 ;; FIXME: Is JMF the magic of outlook mailbox? -- ShengHuo.
642 (defun nndoc-oe-dbx-type-p ()
643 (looking-at (mm-string-as-multibyte "\317\255\022\376")))
645 (defun nndoc-read-little-endian ()
646 (+ (prog1 (char-after) (forward-char 1))
647 (lsh (prog1 (char-after) (forward-char 1)) 8)
648 (lsh (prog1 (char-after) (forward-char 1)) 16)
649 (lsh (prog1 (char-after) (forward-char 1)) 24)))
651 (defun nndoc-oe-dbx-decode-block ()
653 (nndoc-read-little-endian) ;; this address
654 (nndoc-read-little-endian) ;; next address offset
655 (nndoc-read-little-endian) ;; blocksize
656 (nndoc-read-little-endian))) ;; next address
658 (defun nndoc-oe-dbx-dissection ()
659 (let ((i 0) blk p tp)
660 (goto-char 60117) ;; 0x0000EAD4+1
663 (setq blk (nndoc-oe-dbx-decode-block)))
664 (while (and blk (> (car blk) 0) (or (zerop (nth 3 blk))
666 (push (list (incf i) p nil nil nil 0) nndoc-dissection-alist)
667 (while (and (> (car blk) 0) (> (nth 3 blk) p))
668 (goto-char (1+ (nth 3 blk)))
669 (setq blk (nndoc-oe-dbx-decode-block)))
670 (if (or (<= (car blk) p)
672 (not (zerop (nth 3 blk))))
674 (setq tp (+ (car blk) (nth 1 blk) 17))
675 (if (or (<= tp p) (>= tp (point-max)))
679 blk (nndoc-oe-dbx-decode-block)))))))
681 (defun nndoc-oe-dbx-generate-article (article &optional head)
682 (let ((entry (cdr (assq article nndoc-dissection-alist)))
683 (cur (current-buffer))
686 (with-current-buffer nndoc-current-buffer
688 (while (> p (point-min))
690 (setq blk (nndoc-oe-dbx-decode-block))
692 (with-current-buffer cur
693 (insert-buffer-substring nndoc-current-buffer p (+ p (nth 2 blk))))
694 (setq p (1+ (nth 3 blk)))))
696 (while (re-search-forward "\r$" nil t)
697 (delete-backward-char 1))
700 (when (search-forward "\n\n" nil t)
701 (setcar (cddddr entry) (count-lines (point) (point-max)))
702 (delete-region (1- (point)) (point-max))))
705 (defun nndoc-oe-dbx-generate-head (article)
706 (nndoc-oe-dbx-generate-article article 'head))
708 (defun nndoc-mail-in-mail-type-p ()
712 (while (re-search-forward "\n\n[-A-Za-z0-9]+:" nil t)
715 (while (looking-at "[ \t]\\|[-A-Za-z0-9]+:")
716 (if (looking-at "[-A-Za-z0-9]+:")
717 (setq found (1+ found)))
719 (if (and (> found 0) (looking-at "\n"))
723 (defun nndoc-mail-in-mail-article-begin ()
726 (while (re-search-forward "\n\n\\([-A-Za-z0-9]+:\\)" nil t)
728 (setq point (match-beginning 1))
730 (while (looking-at "[ \t]\\|[-A-Za-z0-9]+:")
731 (if (looking-at "[-A-Za-z0-9]+:")
732 (setq found (1+ found)))
734 (if (and (> found 0) (looking-at "\n"))
739 (deffoo nndoc-request-accept-article (group &optional server last)
743 ;;; Functions for dissecting the documents
746 (defun nndoc-search (regexp)
748 (re-search-forward regexp nil t)
749 (beginning-of-line)))
751 (defun nndoc-dissect-buffer ()
752 "Go through the document and partition it into heads/bodies/articles."
755 head-begin head-end body-begin body-end)
756 (setq nndoc-dissection-alist nil)
758 (set-buffer nndoc-current-buffer)
759 (goto-char (point-min))
760 ;; Remove blank lines.
761 (while (eq (following-char) ?\n)
763 (if nndoc-dissection-function
764 (funcall nndoc-dissection-function)
765 ;; Find the beginning of the file.
766 (when nndoc-file-begin
767 (nndoc-search nndoc-file-begin))
768 ;; Go through the file.
769 (while (if (and first nndoc-first-article)
770 (nndoc-search nndoc-first-article)
771 (nndoc-article-begin))
773 (cond (nndoc-head-begin-function
774 (funcall nndoc-head-begin-function))
776 (nndoc-search nndoc-head-begin)))
779 (looking-at nndoc-file-end)))
780 (goto-char (point-max))
781 (setq head-begin (point))
782 (nndoc-search (or nndoc-head-end "^$"))
783 (setq head-end (point))
784 (if nndoc-body-begin-function
785 (funcall nndoc-body-begin-function)
786 (nndoc-search (or nndoc-body-begin "^\n")))
787 (setq body-begin (point))
788 (or (and nndoc-body-end-function
789 (funcall nndoc-body-end-function))
791 (nndoc-search nndoc-body-end))
792 (nndoc-article-begin)
794 (goto-char (point-max))
796 (and (re-search-backward nndoc-file-end nil t)
797 (beginning-of-line)))))
798 (setq body-end (point))
799 (push (list (incf i) head-begin head-end body-begin body-end
800 (count-lines body-begin body-end))
801 nndoc-dissection-alist)))))))
803 (defun nndoc-article-begin ()
804 (if nndoc-article-begin-function
805 (funcall nndoc-article-begin-function)
807 (nndoc-search nndoc-article-begin))))
809 (defun nndoc-unquote-dashes ()
810 "Unquote quoted non-separators in digests."
811 (while (re-search-forward "^- -"nil t)
812 (replace-match "-" t t)))
814 ;; Against compiler warnings.
815 (defvar nndoc-mime-split-ordinal)
817 (defun nndoc-dissect-mime-parts ()
818 "Go through a MIME composite article and partition it into sub-articles.
819 When a MIME entity contains sub-entities, dissection produces one article for
820 the header of this entity, and one article per sub-entity."
821 (setq nndoc-dissection-alist nil
822 nndoc-mime-split-ordinal 0)
824 (set-buffer nndoc-current-buffer)
825 (nndoc-dissect-mime-parts-sub (point-min) (point-max) nil nil nil)))
827 (defun nndoc-dissect-mime-parts-sub (head-begin body-end article-insert
829 "Dissect an entity, within a composite MIME message.
830 The complete message or MIME entity extends from HEAD-BEGIN to BODY-END.
831 ARTICLE-INSERT should be added at beginning for generating a full article.
832 The string POSITION holds a dotted decimal representation of the article
833 position in the hierarchical structure, it is nil for the outer entity.
834 PARENT is the message-ID of the parent summary line, or nil for none."
835 (let ((case-fold-search t)
836 (message-id (nnmail-message-id))
837 head-end body-begin summary-insert message-rfc822 multipart-any
838 subject content-type type subtype boundary-regexp)
839 ;; Gracefully handle a missing body.
840 (goto-char head-begin)
841 (if (or (and (eq (char-after) ?\n) (or (forward-char 1) t))
842 (search-forward "\n\n" body-end t))
843 (setq head-end (1- (point))
845 (setq head-end body-end
846 body-begin body-end))
847 (narrow-to-region head-begin head-end)
848 ;; Save MIME attributes.
849 (goto-char head-begin)
850 (setq content-type (message-fetch-field "Content-Type"))
853 "^ *\\([^ \t\n/;]+\\)/\\([^ \t\n/;]+\\)" content-type)
854 (setq type (downcase (match-string 1 content-type))
855 subtype (downcase (match-string 2 content-type))
856 message-rfc822 (and (string= type "message")
857 (string= subtype "rfc822"))
858 multipart-any (string= type "multipart")))
859 (when (string-match ";[ \t\n]*name=\\([^ \t\n;]+\\)" content-type)
860 (setq subject (match-string 1 content-type)))
861 (when (string-match "boundary=\"?\\([^\"\n]*[^\" \t\n]\\)" content-type)
862 (setq boundary-regexp (concat "^--"
864 (match-string 1 content-type))
865 "\\(--\\)?[ \t]*\n"))))
867 (when (or multipart-any (not article-insert))
868 (setq subject (message-fetch-field "Subject"))))
872 ;; Prepare the article and summary inserts.
873 (unless article-insert
874 (setq article-insert (buffer-substring (point-min) (point-max))
875 head-end head-begin))
877 (unless (string-match "MIME-Version:" article-insert)
879 (concat article-insert "MIME-Version: 1.0\n")))
880 (setq summary-insert article-insert)
881 ;; - summary Subject.
883 (let ((line (concat "Subject: <" position
884 (and position multipart-any ".")
885 (and multipart-any "*")
886 (and (or position multipart-any) " ")
887 (cond ((string= subtype "plain") type)
888 ((string= subtype "basic") type)
894 (if (string-match "Subject:.*\n\\([ \t].*\n\\)*" summary-insert)
895 (replace-match line t t summary-insert)
896 (concat summary-insert line))))
897 ;; - summary Message-ID.
899 (let ((line (concat "Message-ID: " message-id "\n")))
900 (if (string-match "Message-ID:.*\n\\([ \t].*\n\\)*" summary-insert)
901 (replace-match line t t summary-insert)
902 (concat summary-insert line))))
903 ;; - summary References.
906 (let ((line (concat "References: " parent "\n")))
907 (if (string-match "References:.*\n\\([ \t].*\n\\)*"
909 (replace-match line t t summary-insert)
910 (concat summary-insert line)))))
911 ;; Generate dissection information for this entity.
912 (push (list (incf nndoc-mime-split-ordinal)
913 head-begin head-end body-begin body-end
914 (count-lines body-begin body-end)
915 article-insert summary-insert)
916 nndoc-dissection-alist)
917 ;; Recurse for all sub-entities, if any.
922 (nndoc-dissect-mime-parts-sub body-begin body-end nil
923 position message-id)))
924 ((and multipart-any boundary-regexp)
925 (let ((part-counter 0)
926 part-begin part-end eof-flag)
927 (while (string-match "\
928 ^\\(Lines\\|Content-\\(Type\\|Transfer-Encoding\\|Disposition\\)\\):.*\n\\([ \t].*\n\\)*"
930 (setq article-insert (replace-match "" t t article-insert)))
931 (let ((case-fold-search nil))
932 (goto-char body-begin)
933 (setq eof-flag (not (re-search-forward boundary-regexp body-end t)))
934 (while (not eof-flag)
935 (setq part-begin (point))
936 (cond ((re-search-forward boundary-regexp body-end t)
937 (or (not (match-string 1))
938 (string= (match-string 1) "")
941 (setq part-end (point))
943 (t (setq part-end body-end
946 (nndoc-dissect-mime-parts-sub
947 part-begin part-end article-insert
950 (format "%d" (incf part-counter)))
954 (defun nndoc-add-type (definition &optional position)
955 "Add document DEFINITION to the list of nndoc document definitions.
956 If POSITION is nil or `last', the definition will be added
957 as the last checked definition, if t or `first', add as the
958 first definition, and if any other symbol, add after that
959 symbol in the alist."
960 ;; First remove any old instances.
961 (gnus-pull (car definition) nndoc-type-alist)
962 ;; Then enter the new definition in the proper place.
964 ((or (null position) (eq position 'last))
965 (setq nndoc-type-alist (nconc nndoc-type-alist (list definition))))
966 ((or (eq position t) (eq position 'first))
967 (push definition nndoc-type-alist))
969 (let ((list (memq (assq position nndoc-type-alist)
972 (error "No such position: %s" position))
973 (setcdr list (cons definition (cdr list)))))))
977 ;;; nndoc.el ends here