Merge from emacs--devo--0, emacs--rel--22
[gnus] / lisp / mm-uu.el
1 ;;; mm-uu.el --- Return uu stuff as mm handles
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: postscript uudecode binhex shar forward gnatsweb pgp
8
9 ;; This file is part of GNU Emacs.
10
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 3, or (at your option)
14 ;; any later version.
15 ;;
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.
20
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31 (require 'mail-parse)
32 (require 'nnheader)
33 (require 'mm-decode)
34 (require 'mailcap)
35 (require 'mml2015)
36
37 (autoload 'uudecode-decode-region "uudecode")
38 (autoload 'uudecode-decode-region-external "uudecode")
39 (autoload 'uudecode-decode-region-internal "uudecode")
40
41 (autoload 'binhex-decode-region "binhex")
42 (autoload 'binhex-decode-region-external "binhex")
43 (autoload 'binhex-decode-region-internal "binhex")
44
45 (autoload 'yenc-decode-region "yenc")
46 (autoload 'yenc-extract-filename "yenc")
47
48 (defcustom mm-uu-decode-function 'uudecode-decode-region
49   "*Function to uudecode.
50 Internal function is done in Lisp by default, therefore decoding may
51 appear to be horribly slow.  You can make Gnus use an external
52 decoder, such as uudecode."
53   :type '(choice
54           (function-item :tag "Auto detect" uudecode-decode-region)
55           (function-item :tag "Internal" uudecode-decode-region-internal)
56           (function-item :tag "External" uudecode-decode-region-external))
57   :group 'gnus-article-mime)
58
59 (defcustom mm-uu-binhex-decode-function 'binhex-decode-region
60   "*Function to binhex decode.
61 Internal function is done in elisp by default, therefore decoding may
62 appear to be horribly slow . You can make Gnus use the external Unix
63 decoder, such as hexbin."
64   :type '(choice (function-item :tag "Auto detect" binhex-decode-region)
65                  (function-item :tag "Internal" binhex-decode-region-internal)
66                  (function-item :tag "External" binhex-decode-region-external))
67   :group 'gnus-article-mime)
68
69 (defvar mm-uu-yenc-decode-function 'yenc-decode-region)
70
71 (defvar mm-uu-beginning-regexp nil)
72
73 (defvar mm-dissect-disposition "inline"
74   "The default disposition of uu parts.
75 This can be either \"inline\" or \"attachment\".")
76
77 (defcustom mm-uu-emacs-sources-regexp "\\.emacs\\.sources"
78   "The regexp of Emacs sources groups."
79   :version "22.1"
80   :type 'regexp
81   :group 'gnus-article-mime)
82
83 (defcustom mm-uu-diff-groups-regexp
84   "\\(gmane\\|gnu\\)\\..*\\(diff\\|commit\\|cvs\\|bug\\|devel\\)"
85   "Regexp matching diff groups."
86   :version "22.1"
87   :type 'regexp
88   :group 'gnus-article-mime)
89
90 (defcustom mm-uu-tex-groups-regexp "\\.tex\\>"
91   "*Regexp matching TeX groups."
92   :version "23.1"
93   :type 'regexp
94   :group 'gnus-article-mime)
95
96 (defvar mm-uu-type-alist
97   '((postscript
98      "^%!PS-"
99      "^%%EOF$"
100      mm-uu-postscript-extract
101      nil)
102     (uu ;; Maybe we should have a more strict test here.
103      "^begin[ \t]+0?[0-7][0-7][0-7][ \t]+"
104      "^end[ \t]*$"
105      mm-uu-uu-extract
106      mm-uu-uu-filename)
107     (binhex
108      "^:.\\{63,63\\}$"
109      ":$"
110      mm-uu-binhex-extract
111      nil
112      mm-uu-binhex-filename)
113     (yenc
114      "^=ybegin.*size=[0-9]+.*name=.*$"
115      "^=yend.*size=[0-9]+"
116      mm-uu-yenc-extract
117      mm-uu-yenc-filename)
118     (shar
119      "^#! */bin/sh"
120      "^exit 0$"
121      mm-uu-shar-extract)
122     (forward
123      ;; Thanks to Edward J. Sabol <sabol@alderaan.gsfc.nasa.gov> and
124      ;; Peter von der Ah\'e <pahe@daimi.au.dk>
125      "^-+ \\(Start of \\)?Forwarded message"
126      "^-+ End \\(of \\)?forwarded message"
127      mm-uu-forward-extract
128      nil
129      mm-uu-forward-test)
130     (gnatsweb
131      "^----gnatsweb-attachment----"
132      nil
133      mm-uu-gnatsweb-extract)
134     (pgp-signed
135      "^-----BEGIN PGP SIGNED MESSAGE-----"
136      "^-----END PGP SIGNATURE-----"
137      mm-uu-pgp-signed-extract
138      nil
139      nil)
140     (pgp-encrypted
141      "^-----BEGIN PGP MESSAGE-----"
142      "^-----END PGP MESSAGE-----"
143      mm-uu-pgp-encrypted-extract
144      nil
145      nil)
146     (pgp-key
147      "^-----BEGIN PGP PUBLIC KEY BLOCK-----"
148      "^-----END PGP PUBLIC KEY BLOCK-----"
149      mm-uu-pgp-key-extract
150      mm-uu-gpg-key-skip-to-last
151      nil)
152     (emacs-sources
153      "^;;;?[ \t]*[^ \t]+\\.el[ \t]*--"
154      "^;;;?[ \t]*\\([^ \t]+\\.el\\)[ \t]+ends here"
155      mm-uu-emacs-sources-extract
156      nil
157      mm-uu-emacs-sources-test)
158     (diff
159      "^Index: "
160      nil
161      mm-uu-diff-extract
162      nil
163      mm-uu-diff-test)
164     (message-marks
165      ;; Text enclosed with tags similar to `message-mark-insert-begin' and
166      ;; `message-mark-insert-end'.  Don't use those variables to avoid
167      ;; dependency on `message.el'.
168      "^-+[8<>]*-\\{9,\\}[a-z ]+-\\{9,\\}[a-z ]+-\\{9,\\}[8<>]*-+$"
169      "^-+[8<>]*-\\{9,\\}[a-z ]+-\\{9,\\}[a-z ]+-\\{9,\\}[8<>]*-+$"
170      (lambda () (mm-uu-verbatim-marks-extract -1 0 1 -1))
171      nil)
172     ;; Omitting [a-z8<] leads to false positives (bogus signature separators
173     ;; and mailing list banners).
174     (insert-marks
175      "^ *\\(-\\|_\\)\\{30,\\}.*[a-z8<].*\\(-\\|_\\)\\{30,\\} *$"
176      "^ *\\(-\\|_\\)\\{30,\\}.*[a-z8<].*\\(-\\|_\\)\\{30,\\} *$"
177      (lambda () (mm-uu-verbatim-marks-extract 0 0 1 -1))
178      nil)
179     (verbatim-marks
180      ;; slrn-style verbatim marks, see
181      ;; http://www.slrn.org/manual/slrn-manual-6.html#ss6.81
182      "^#v\\+"
183      "^#v\\-$"
184      (lambda () (mm-uu-verbatim-marks-extract 0 0))
185      nil)
186     (LaTeX
187      "^\\([\\\\%][^\n]+\n\\)*\\\\documentclass.*[[{%]"
188      "^\\\\end{document}"
189      mm-uu-latex-extract
190      nil
191      mm-uu-latex-test))
192   "A list of specifications for non-MIME attachments.
193 Each element consist of the following entries: label,
194 start-regexp, end-regexp, extract-function, test-function.
195
196 After modifying this list you must run \\[mm-uu-configure].
197
198 You can disable elements from this list by customizing
199 `mm-uu-configure-list'.")
200
201 (defcustom mm-uu-configure-list '((shar . disabled))
202   "A list of mm-uu configuration.
203 To disable dissecting shar codes, for instance, add
204 `(shar . disabled)' to this list."
205   :type 'alist
206   :options (mapcar (lambda (entry)
207                      (list (car entry) '(const disabled)))
208                    mm-uu-type-alist)
209   :group 'gnus-article-mime)
210
211 (defvar mm-uu-text-plain-type '("text/plain" (charset . gnus-decoded))
212   "MIME type and parameters for text/plain parts.
213 `gnus-decoded' is a fake charset, which means no further decoding.")
214
215 ;; functions
216
217 (defsubst mm-uu-type (entry)
218   (car entry))
219
220 (defsubst mm-uu-beginning-regexp (entry)
221   (nth 1 entry))
222
223 (defsubst mm-uu-end-regexp (entry)
224   (nth 2 entry))
225
226 (defsubst mm-uu-function-extract (entry)
227   (nth 3 entry))
228
229 (defsubst mm-uu-function-1 (entry)
230   (nth 4 entry))
231
232 (defsubst mm-uu-function-2 (entry)
233   (nth 5 entry))
234
235 ;; In Emacs 22, we could use `min-colors' in the face definition.  But Emacs
236 ;; 21 and XEmacs don't support it.
237 (defcustom mm-uu-hide-markers
238   (< 16 (or (and (fboundp 'defined-colors)
239                  (length (defined-colors)))
240             (and (fboundp 'device-color-cells)
241                  (device-color-cells))
242             0))
243   "If non-nil, hide verbatim markers.
244 The value should be nil on displays where the face
245 `mm-uu-extract' isn't distinguishable to the face `default'."
246   :type '(choice (const :tag "Hide" t)
247                  (const :tag "Don't hide" nil))
248   :version "23.1" ;; No Gnus
249   :group 'gnus-article-mime)
250
251 (defface mm-uu-extract '(;; Inspired by `gnus-cite-3'
252                          (((type tty)
253                            (class color)
254                            (background dark))
255                           (:background "dark blue"))
256                          (((class color)
257                            (background dark))
258                           (:foreground "light yellow"
259                            :background "dark green"))
260                          (((type tty)
261                            (class color)
262                            (background light))
263                           (:foreground "dark blue"))
264                          (((class color)
265                            (background light))
266                           (:foreground "dark green"
267                            :background "light yellow"))
268                          (t
269                           ()))
270   "Face for extracted buffers."
271   ;; See `mm-uu-verbatim-marks-extract'.
272   :version "23.1" ;; No Gnus
273   :group 'gnus-article-mime)
274
275 (defun mm-uu-copy-to-buffer (&optional from to properties)
276   "Copy the contents of the current buffer to a fresh buffer.
277 Return that buffer.
278
279 If PROPERTIES is non-nil, PROPERTIES are applied to the buffer,
280 see `set-text-properties'.  If PROPERTIES equals t, this means to
281 apply the face `mm-uu-extract'."
282   (let ((obuf (current-buffer))
283         (coding-system
284          ;; Might not exist in non-MULE XEmacs
285          (when (boundp 'buffer-file-coding-system)
286            buffer-file-coding-system)))
287     (with-current-buffer (generate-new-buffer " *mm-uu*")
288       (setq buffer-file-coding-system coding-system)
289       (insert-buffer-substring obuf from to)
290       (cond ((eq properties  t)
291              (set-text-properties (point-min) (point-max)
292                                   '(face mm-uu-extract)))
293             (properties
294              (set-text-properties (point-min) (point-max) properties)))
295       (current-buffer))))
296
297 (defun mm-uu-configure-p  (key val)
298   (member (cons key val) mm-uu-configure-list))
299
300 (defun mm-uu-configure (&optional symbol value)
301   "Configure detection of non-MIME attachments."
302   (interactive)
303   (if symbol (set-default symbol value))
304   (setq mm-uu-beginning-regexp nil)
305   (mapcar (lambda (entry)
306              (if (mm-uu-configure-p (mm-uu-type entry) 'disabled)
307                  nil
308                (setq mm-uu-beginning-regexp
309                      (concat mm-uu-beginning-regexp
310                              (if mm-uu-beginning-regexp "\\|")
311                              (mm-uu-beginning-regexp entry)))))
312           mm-uu-type-alist))
313
314 (mm-uu-configure)
315
316 (defvar file-name)
317 (defvar start-point)
318 (defvar end-point)
319 (defvar entry)
320
321 (defun mm-uu-uu-filename ()
322   (if (looking-at ".+")
323       (setq file-name
324             (let ((nnheader-file-name-translation-alist
325                    '((?/ . ?,) (?\  . ?_) (?* . ?_) (?$ . ?_))))
326               (nnheader-translate-file-chars (match-string 0))))))
327
328 (defun mm-uu-binhex-filename ()
329   (setq file-name
330         (ignore-errors
331           (binhex-decode-region start-point end-point t))))
332
333 (defun mm-uu-yenc-filename ()
334   (goto-char start-point)
335   (setq file-name
336         (ignore-errors
337           (yenc-extract-filename))))
338
339 (defun mm-uu-forward-test ()
340   (save-excursion
341     (goto-char start-point)
342     (forward-line)
343     (looking-at "[\r\n]*[a-zA-Z][a-zA-Z0-9-]*:")))
344
345 (defun mm-uu-postscript-extract ()
346   (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
347                   '("application/postscript")))
348
349 (defun mm-uu-verbatim-marks-extract (start-offset end-offset
350                                                   &optional
351                                                   start-hide
352                                                   end-hide)
353   (let ((start (or (and mm-uu-hide-markers
354                         start-hide)
355                    start-offset
356                    1))
357         (end   (or (and mm-uu-hide-markers
358                         end-hide)
359                    end-offset
360                    -1)))
361     (mm-make-handle
362      (mm-uu-copy-to-buffer
363       (progn (goto-char start-point)
364              (forward-line start)
365              (point))
366       (progn (goto-char end-point)
367            (forward-line end)
368            (point))
369       t)
370      '("text/x-verbatim" (charset . gnus-decoded)))))
371
372 (defun mm-uu-latex-extract ()
373   (mm-make-handle
374    (mm-uu-copy-to-buffer start-point end-point t)
375    ;; application/x-tex?
376    '("text/x-verbatim" (charset . gnus-decoded))))
377
378 (defun mm-uu-emacs-sources-extract ()
379   (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
380                   '("application/emacs-lisp" (charset . gnus-decoded))
381                   nil nil
382                   (list mm-dissect-disposition
383                         (cons 'filename file-name))))
384
385 (defvar gnus-newsgroup-name)
386
387 (defun mm-uu-emacs-sources-test ()
388   (setq file-name (match-string 1))
389   (and gnus-newsgroup-name
390        mm-uu-emacs-sources-regexp
391        (string-match mm-uu-emacs-sources-regexp gnus-newsgroup-name)))
392
393 (defun mm-uu-diff-extract ()
394   (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
395                   '("text/x-patch" (charset . gnus-decoded))))
396
397 (defun mm-uu-diff-test ()
398   (and gnus-newsgroup-name
399        mm-uu-diff-groups-regexp
400        (string-match mm-uu-diff-groups-regexp gnus-newsgroup-name)))
401
402 (defun mm-uu-latex-test ()
403   (and gnus-newsgroup-name
404        mm-uu-tex-groups-regexp
405        (string-match mm-uu-tex-groups-regexp gnus-newsgroup-name)))
406
407 (defun mm-uu-forward-extract ()
408   (mm-make-handle (mm-uu-copy-to-buffer
409                    (progn (goto-char start-point) (forward-line) (point))
410                    (progn (goto-char end-point) (forward-line -1) (point)))
411                   '("message/rfc822" (charset . gnus-decoded))))
412
413 (defun mm-uu-uu-extract ()
414   (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
415                   (list (or (and file-name
416                                  (string-match "\\.[^\\.]+$"
417                                                file-name)
418                                  (mailcap-extension-to-mime
419                                   (match-string 0 file-name)))
420                             "application/octet-stream"))
421                   'x-uuencode nil
422                   (if (and file-name (not (equal file-name "")))
423                       (list mm-dissect-disposition
424                             (cons 'filename file-name)))))
425
426 (defun mm-uu-binhex-extract ()
427   (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
428                   (list (or (and file-name
429                                  (string-match "\\.[^\\.]+$" file-name)
430                                  (mailcap-extension-to-mime
431                                   (match-string 0 file-name)))
432                             "application/octet-stream"))
433                   'x-binhex nil
434                   (if (and file-name (not (equal file-name "")))
435                       (list mm-dissect-disposition
436                             (cons 'filename file-name)))))
437
438 (defvar gnus-original-article-buffer)   ; gnus.el
439
440 (defun mm-uu-yenc-extract ()
441   ;; This might not be exactly correct, but we sure can't get the
442   ;; binary data from the article buffer, since that's already in a
443   ;; non-binary charset.  So get it from the original article buffer. 
444   (mm-make-handle (save-excursion
445                     (set-buffer gnus-original-article-buffer)
446                     (mm-uu-copy-to-buffer start-point end-point))
447                   (list (or (and file-name
448                                  (string-match "\\.[^\\.]+$" file-name)
449                                  (mailcap-extension-to-mime
450                                   (match-string 0 file-name)))
451                             "application/octet-stream"))
452                   'x-yenc nil
453                   (if (and file-name (not (equal file-name "")))
454                       (list mm-dissect-disposition
455                             (cons 'filename file-name)))))
456
457
458 (defun mm-uu-shar-extract ()
459   (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
460                   '("application/x-shar")))
461
462 (defun mm-uu-gnatsweb-extract ()
463   (save-restriction
464     (goto-char start-point)
465     (forward-line)
466     (narrow-to-region (point) end-point)
467     (mm-dissect-buffer t)))
468
469 (defun mm-uu-pgp-signed-test (&rest rest)
470   (and
471    mml2015-use
472    (mml2015-clear-verify-function)
473    (cond
474     ((eq mm-verify-option 'never) nil)
475     ((eq mm-verify-option 'always) t)
476     ((eq mm-verify-option 'known) t)
477     (t (prog1
478            (y-or-n-p "Verify pgp signed part? ")
479          (message ""))))))
480
481 (defvar gnus-newsgroup-charset)
482
483 (defun mm-uu-pgp-signed-extract-1 (handles ctl)
484   (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max))))
485     (with-current-buffer buf
486       (if (mm-uu-pgp-signed-test)
487           (progn
488             (mml2015-clean-buffer)
489             (let ((coding-system-for-write (or gnus-newsgroup-charset
490                                                'iso-8859-1))
491                   (coding-system-for-read (or gnus-newsgroup-charset
492                                               'iso-8859-1)))
493               (funcall (mml2015-clear-verify-function))))
494         (when (and mml2015-use (null (mml2015-clear-verify-function)))
495           (mm-set-handle-multipart-parameter
496            mm-security-handle 'gnus-details
497            (format "Clear verification not supported by `%s'.\n" mml2015-use)))
498         (mml2015-extract-cleartext-signature))
499       (list (mm-make-handle buf mm-uu-text-plain-type)))))
500
501 (defun mm-uu-pgp-signed-extract ()
502   (let ((mm-security-handle (list (format "multipart/signed"))))
503     (mm-set-handle-multipart-parameter
504      mm-security-handle 'protocol "application/x-gnus-pgp-signature")
505     (save-restriction
506       (narrow-to-region start-point end-point)
507       (add-text-properties 0 (length (car mm-security-handle))
508                            (list 'buffer (mm-uu-copy-to-buffer))
509                            (car mm-security-handle))
510       (setcdr mm-security-handle
511               (mm-uu-pgp-signed-extract-1 nil
512                                           mm-security-handle)))
513     mm-security-handle))
514
515 (defun mm-uu-pgp-encrypted-test (&rest rest)
516   (and
517    mml2015-use
518    (mml2015-clear-decrypt-function)
519    (cond
520     ((eq mm-decrypt-option 'never) nil)
521     ((eq mm-decrypt-option 'always) t)
522     ((eq mm-decrypt-option 'known) t)
523     (t (prog1
524            (y-or-n-p "Decrypt pgp encrypted part? ")
525          (message ""))))))
526
527 (defun mm-uu-pgp-encrypted-extract-1 (handles ctl)
528   (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max)))
529         (first t)
530         charset)
531     ;; Make sure there's a blank line between header and body.
532     (with-current-buffer buf
533       (goto-char (point-min))
534       (while (prog2
535                  (forward-line 1)
536                  (if first
537                      (looking-at "[^\t\n ]+:")
538                    (looking-at "[^\t\n ]+:\\|[\t ]"))
539                (setq first nil)))
540       (unless (memq (char-after) '(?\n nil))
541         (insert "\n"))
542       (save-restriction
543         (narrow-to-region (point-min) (point))
544         (setq charset (mail-fetch-field "charset")))
545       (if (and (mm-uu-pgp-encrypted-test)
546                (progn
547                  (mml2015-clean-buffer)
548                  (funcall (mml2015-clear-decrypt-function))
549                  (equal (mm-handle-multipart-ctl-parameter mm-security-handle
550                                                            'gnus-info)
551                         "OK")))
552           (progn
553             ;; Decode charset.
554             (if (and (or charset
555                          (setq charset gnus-newsgroup-charset))
556                      (setq charset (mm-charset-to-coding-system charset))
557                      (not (eq charset 'ascii)))
558                 ;; Assume that buffer's multibyteness is turned off.
559                 ;; See `mml2015-pgg-clear-decrypt'.
560                 (insert (mm-decode-coding-string (prog1
561                                                      (buffer-string)
562                                                    (erase-buffer)
563                                                    (mm-enable-multibyte))
564                                                  charset))
565               (mm-enable-multibyte))
566             (list (mm-make-handle buf mm-uu-text-plain-type)))
567         (list (mm-make-handle buf '("application/pgp-encrypted")))))))
568
569 (defun mm-uu-pgp-encrypted-extract ()
570   (let ((mm-security-handle (list (format "multipart/encrypted"))))
571     (mm-set-handle-multipart-parameter
572      mm-security-handle 'protocol "application/x-gnus-pgp-encrypted")
573     (save-restriction
574       (narrow-to-region start-point end-point)
575       (add-text-properties 0 (length (car mm-security-handle))
576                            (list 'buffer (mm-uu-copy-to-buffer))
577                            (car mm-security-handle))
578       (setcdr mm-security-handle
579               (mm-uu-pgp-encrypted-extract-1 nil
580                                              mm-security-handle)))
581     mm-security-handle))
582
583 (defun mm-uu-gpg-key-skip-to-last ()
584   (let ((point (point))
585         (end-regexp (mm-uu-end-regexp entry))
586         (beginning-regexp (mm-uu-beginning-regexp entry)))
587     (when (and end-regexp
588                (not (mm-uu-configure-p (mm-uu-type entry) 'disabled)))
589       (while (re-search-forward end-regexp nil t)
590         (skip-chars-forward " \t\n\r")
591         (if (looking-at beginning-regexp)
592             (setq point (match-end 0)))))
593     (goto-char point)))
594
595 (defun mm-uu-pgp-key-extract ()
596   (let ((buf (mm-uu-copy-to-buffer start-point end-point)))
597     (mm-make-handle buf
598                     '("application/pgp-keys"))))
599
600 ;;;###autoload
601 (defun mm-uu-dissect (&optional noheader mime-type)
602   "Dissect the current buffer and return a list of uu handles.
603 The optional NOHEADER means there's no header in the buffer.
604 MIME-TYPE specifies a MIME type and parameters, which defaults to the
605 value of `mm-uu-text-plain-type'."
606   (let ((case-fold-search t)
607         (mm-uu-text-plain-type (or mime-type mm-uu-text-plain-type))
608         text-start start-point end-point file-name result entry func)
609     (save-excursion
610       (goto-char (point-min))
611       (cond
612        (noheader)
613        ((looking-at "\n")
614         (forward-line))
615        ((search-forward "\n\n" nil t)
616         t)
617        (t (goto-char (point-max))))
618       (setq text-start (point))
619       (while (re-search-forward mm-uu-beginning-regexp nil t)
620         (setq start-point (match-beginning 0)
621               entry nil)
622         (let ((alist mm-uu-type-alist)
623               (beginning-regexp (match-string 0)))
624           (while (not entry)
625             (if (string-match (mm-uu-beginning-regexp (car alist))
626                               beginning-regexp)
627                 (setq entry (car alist))
628               (pop alist))))
629         (if (setq func (mm-uu-function-1 entry))
630             (funcall func))
631         (forward-line);; in case of failure
632         (when (and (not (mm-uu-configure-p (mm-uu-type entry) 'disabled))
633                    (let ((end-regexp (mm-uu-end-regexp entry)))
634                      (if (not end-regexp)
635                          (or (setq end-point (point-max)) t)
636                        (prog1
637                            (re-search-forward end-regexp nil t)
638                          (forward-line)
639                          (setq end-point (point)))))
640                    (or (not (setq func (mm-uu-function-2 entry)))
641                        (funcall func)))
642           (if (and (> start-point text-start)
643                    (progn
644                      (goto-char text-start)
645                      (re-search-forward "." start-point t)))
646               (push
647                (mm-make-handle (mm-uu-copy-to-buffer text-start start-point)
648                                mm-uu-text-plain-type)
649                result))
650           (push
651            (funcall (mm-uu-function-extract entry))
652            result)
653           (goto-char (setq text-start end-point))))
654       (when result
655         (if (and (> (point-max) (1+ text-start))
656                  (save-excursion
657                    (goto-char text-start)
658                    (re-search-forward "." nil t)))
659             (push
660              (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
661                              mm-uu-text-plain-type)
662              result))
663         (setq result (cons "multipart/mixed" (nreverse result))))
664       result)))
665
666 ;;;###autoload
667 (defun mm-uu-dissect-text-parts (handle &optional decoded)
668   "Dissect text parts and put uu handles into HANDLE.
669 Assume text has been decoded if DECODED is non-nil."
670   (let ((buffer (mm-handle-buffer handle)))
671     (cond ((stringp buffer)
672            (dolist (elem (cdr handle))
673              (mm-uu-dissect-text-parts elem decoded)))
674           ((bufferp buffer)
675            (let ((type (mm-handle-media-type handle))
676                  (case-fold-search t) ;; string-match
677                  children charset encoding)
678              (when (and
679                     (stringp type)
680                     ;; Mutt still uses application/pgp even though
681                     ;; it has already been withdrawn.
682                     (string-match "\\`text/\\|\\`application/pgp\\'" type)
683                     (setq
684                      children
685                      (with-current-buffer buffer
686                        (cond
687                         ((or decoded
688                              (eq (setq charset (mail-content-type-get
689                                                 (mm-handle-type handle)
690                                                 'charset))
691                                  'gnus-decoded))
692                          (setq decoded t)
693                          (mm-uu-dissect
694                           t (cons type '((charset . gnus-decoded)))))
695                         (charset
696                          (setq decoded t)
697                          (mm-with-multibyte-buffer
698                            (insert (mm-decode-string (mm-get-part handle)
699                                                      charset))
700                            (mm-uu-dissect
701                             t (cons type '((charset . gnus-decoded))))))
702                         ((setq encoding (mm-handle-encoding handle))
703                          (setq decoded nil)
704                          ;; Inherit the multibyteness of the `buffer'.
705                          (with-temp-buffer
706                            (insert-buffer-substring buffer)
707                            (mm-decode-content-transfer-encoding
708                             encoding type)
709                            (mm-uu-dissect t (list type))))
710                         (t
711                          (setq decoded nil)
712                          (mm-uu-dissect t (list type)))))))
713                ;; Ignore it if a given part is dissected into a single
714                ;; part of which the type is the same as the given one.
715                (if (and (<= (length children) 2)
716                         (string-equal (mm-handle-media-type (cadr children))
717                                       type))
718                    (kill-buffer (mm-handle-buffer (cadr children)))
719                  (kill-buffer buffer)
720                  (setcdr handle (cdr children))
721                  (setcar handle (car children)) ;; "multipart/mixed"
722                  (dolist (elem (cdr children))
723                    (mm-uu-dissect-text-parts elem decoded))))))
724           (t
725            (dolist (elem handle)
726              (mm-uu-dissect-text-parts elem decoded))))))
727
728 (provide 'mm-uu)
729
730 ;; arch-tag: 7db076bf-53db-4320-aa19-ca76a1d2ab2c
731 ;;; mm-uu.el ends here