1 ;;; mm-uu.el --- Return uu stuff as mm handles
3 ;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; Keywords: postscript uudecode binhex shar forward gnatsweb pgp
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'cl
))
34 (autoload 'uudecode-decode-region
"uudecode")
35 (autoload 'uudecode-decode-region-external
"uudecode")
36 (autoload 'uudecode-decode-region-internal
"uudecode")
38 (autoload 'binhex-decode-region
"binhex")
39 (autoload 'binhex-decode-region-external
"binhex")
40 (autoload 'binhex-decode-region-internal
"binhex")
42 (autoload 'yenc-decode-region
"yenc")
43 (autoload 'yenc-extract-filename
"yenc")
45 (defcustom mm-uu-decode-function
'uudecode-decode-region
46 "*Function to uudecode.
47 Internal function is done in Lisp by default, therefore decoding may
48 appear to be horribly slow. You can make Gnus use an external
49 decoder, such as uudecode."
51 (function-item :tag
"Auto detect" uudecode-decode-region
)
52 (function-item :tag
"Internal" uudecode-decode-region-internal
)
53 (function-item :tag
"External" uudecode-decode-region-external
))
54 :group
'gnus-article-mime
)
56 (defcustom mm-uu-binhex-decode-function
'binhex-decode-region
57 "*Function to binhex decode.
58 Internal function is done in elisp by default, therefore decoding may
59 appear to be horribly slow . You can make Gnus use the external Unix
60 decoder, such as hexbin."
61 :type
'(choice (function-item :tag
"Auto detect" binhex-decode-region
)
62 (function-item :tag
"Internal" binhex-decode-region-internal
)
63 (function-item :tag
"External" binhex-decode-region-external
))
64 :group
'gnus-article-mime
)
66 (defvar mm-uu-yenc-decode-function
'yenc-decode-region
)
68 (defvar mm-uu-beginning-regexp nil
)
70 (defvar mm-dissect-disposition
"inline"
71 "The default disposition of uu parts.
72 This can be either \"inline\" or \"attachment\".")
74 (defcustom mm-uu-emacs-sources-regexp
"\\.emacs\\.sources"
75 "The regexp of Emacs sources groups."
78 :group
'gnus-article-mime
)
80 (defcustom mm-uu-diff-groups-regexp
81 "\\(gmane\\|gnu\\)\\..*\\(diff\\|commit\\|cvs\\|bug\\|devel\\)"
82 "Regexp matching diff groups."
85 :group
'gnus-article-mime
)
87 (defcustom mm-uu-tex-groups-regexp
"\\.tex\\>"
88 "*Regexp matching TeX groups."
91 :group
'gnus-article-mime
)
93 (defvar mm-uu-type-alist
97 mm-uu-postscript-extract
99 (uu ;; Maybe we should have a more strict test here.
100 "^begin[ \t]+0?[0-7][0-7][0-7][ \t]+"
109 mm-uu-binhex-filename
)
111 "^=ybegin.*size=[0-9]+.*name=.*$"
112 "^=yend.*size=[0-9]+"
120 ;; Thanks to Edward J. Sabol <sabol@alderaan.gsfc.nasa.gov> and
121 ;; Peter von der Ah\'e <pahe@daimi.au.dk>
122 "^-+ \\(Start of \\)?Forwarded message"
123 "^-+ End \\(of \\)?forwarded message"
124 mm-uu-forward-extract
128 "^----gnatsweb-attachment----"
130 mm-uu-gnatsweb-extract
)
132 "^-----BEGIN PGP SIGNED MESSAGE-----"
133 "^-----END PGP SIGNATURE-----"
134 mm-uu-pgp-signed-extract
138 "^-----BEGIN PGP MESSAGE-----"
139 "^-----END PGP MESSAGE-----"
140 mm-uu-pgp-encrypted-extract
144 "^-----BEGIN PGP PUBLIC KEY BLOCK-----"
145 "^-----END PGP PUBLIC KEY BLOCK-----"
146 mm-uu-pgp-key-extract
147 mm-uu-gpg-key-skip-to-last
150 "^;;;?[ \t]*[^ \t]+\\.el[ \t]*--"
151 "^;;;?[ \t]*\\([^ \t]+\\.el\\)[ \t]+ends here"
152 mm-uu-emacs-sources-extract
154 mm-uu-emacs-sources-test
)
162 "^=== modified file "
174 ;; Text enclosed with tags similar to `message-mark-insert-begin' and
175 ;; `message-mark-insert-end'. Don't use those variables to avoid
176 ;; dependency on `message.el'.
177 "^-+[8<>]*-\\{9,\\}[a-z ]+-\\{9,\\}[a-z ]+-\\{9,\\}[8<>]*-+$"
178 "^-+[8<>]*-\\{9,\\}[a-z ]+-\\{9,\\}[a-z ]+-\\{9,\\}[8<>]*-+$"
179 (lambda () (mm-uu-verbatim-marks-extract 0 0 1 -
1))
181 ;; Omitting [a-z8<] leads to false positives (bogus signature separators
182 ;; and mailing list banners).
184 "^ *\\(-\\|_\\)\\{30,\\}.*[a-z8<].*\\(-\\|_\\)\\{30,\\} *$"
185 "^ *\\(-\\|_\\)\\{30,\\}.*[a-z8<].*\\(-\\|_\\)\\{30,\\} *$"
186 (lambda () (mm-uu-verbatim-marks-extract 0 0 1 -
1))
189 ;; slrn-style verbatim marks, see
190 ;; http://slrn.sourceforge.net/docs/slrn-manual-6.html#process_verbatim_marks
193 (lambda () (mm-uu-verbatim-marks-extract 0 0))
196 "^\\([\\\\%][^\n]+\n\\)*\\\\documentclass.*[[{%]"
204 mm-uu-org-src-code-block-extract
)
206 "^[ \t]*#\\+[[:alpha:]]+: "
208 mm-uu-org-src-code-block-extract
))
209 "A list of specifications for non-MIME attachments.
210 Each element consist of the following entries: label,
211 start-regexp, end-regexp, extract-function, test-function.
213 After modifying this list you must run \\[mm-uu-configure].
215 You can disable elements from this list by customizing
216 `mm-uu-configure-list'.")
218 (defcustom mm-uu-configure-list
'((shar . disabled
))
219 "A list of mm-uu configuration.
220 To disable dissecting shar codes, for instance, add
221 `(shar . disabled)' to this list."
223 :options
(mapcar (lambda (entry)
224 (list (car entry
) '(const disabled
)))
226 :group
'gnus-article-mime
)
228 (defvar mm-uu-text-plain-type
'("text/plain" (charset . gnus-decoded
))
229 "MIME type and parameters for text/plain parts.
230 `gnus-decoded' is a fake charset, which means no further decoding.")
234 (defsubst mm-uu-type
(entry)
237 (defsubst mm-uu-beginning-regexp
(entry)
240 (defsubst mm-uu-end-regexp
(entry)
243 (defsubst mm-uu-function-extract
(entry)
246 (defsubst mm-uu-function-1
(entry)
249 (defsubst mm-uu-function-2
(entry)
252 ;; In Emacs 22, we could use `min-colors' in the face definition. But Emacs
253 ;; 21 and XEmacs don't support it.
254 (defcustom mm-uu-hide-markers
255 (< 16 (or (and (fboundp 'defined-colors
)
256 (length (defined-colors)))
257 (and (fboundp 'device-color-cells
)
258 (device-color-cells))
260 "If non-nil, hide verbatim markers.
261 The value should be nil on displays where the face
262 `mm-uu-extract' isn't distinguishable to the face `default'."
263 :type
'(choice (const :tag
"Hide" t
)
264 (const :tag
"Don't hide" nil
))
265 :version
"23.1" ;; No Gnus
266 :group
'gnus-article-mime
)
268 (defface mm-uu-extract
'(;; Inspired by `gnus-cite-3'
272 (:background
"dark blue"))
275 (:foreground
"light yellow"
276 :background
"dark green"))
280 (:foreground
"dark blue"))
283 (:foreground
"dark green"
284 :background
"light yellow"))
287 "Face for extracted buffers."
288 ;; See `mm-uu-verbatim-marks-extract'.
289 :version
"23.1" ;; No Gnus
290 :group
'gnus-article-mime
)
292 (defun mm-uu-copy-to-buffer (&optional from to properties
)
293 "Copy the contents of the current buffer to a fresh buffer.
296 If PROPERTIES is non-nil, PROPERTIES are applied to the buffer,
297 see `set-text-properties'. If PROPERTIES equals t, this means to
298 apply the face `mm-uu-extract'."
299 (let ((obuf (current-buffer))
300 (multi (and (boundp 'enable-multibyte-characters
)
301 enable-multibyte-characters
))
303 ;; Might not exist in non-MULE XEmacs
304 (when (boundp 'buffer-file-coding-system
)
305 buffer-file-coding-system
)))
306 (with-current-buffer (generate-new-buffer " *mm-uu*")
307 (if multi
(mm-enable-multibyte) (mm-disable-multibyte))
308 (setq buffer-file-coding-system coding-system
)
309 (insert-buffer-substring obuf from to
)
310 (cond ((eq properties t
)
311 (set-text-properties (point-min) (point-max)
312 '(face mm-uu-extract
)))
314 (set-text-properties (point-min) (point-max) properties
)))
317 (defun mm-uu-configure-p (key val
)
318 (member (cons key val
) mm-uu-configure-list
))
320 (defun mm-uu-configure (&optional symbol value
)
321 "Configure detection of non-MIME attachments."
323 (if symbol
(set-default symbol value
))
324 (setq mm-uu-beginning-regexp nil
)
325 (mapcar (lambda (entry)
326 (if (mm-uu-configure-p (mm-uu-type entry
) 'disabled
)
328 (setq mm-uu-beginning-regexp
329 (concat mm-uu-beginning-regexp
330 (if mm-uu-beginning-regexp
"\\|")
331 (mm-uu-beginning-regexp entry
)))))
341 (defun mm-uu-uu-filename ()
342 (if (looking-at ".+")
344 (let ((nnheader-file-name-translation-alist
345 '((?
/ . ?
,) (?\ . ?_
) (?
* . ?_
) (?$ . ?_
))))
346 (nnheader-translate-file-chars (match-string 0))))))
348 (defun mm-uu-binhex-filename ()
351 (binhex-decode-region start-point end-point t
))))
353 (defun mm-uu-yenc-filename ()
354 (goto-char start-point
)
357 (yenc-extract-filename))))
359 (defun mm-uu-forward-test ()
361 (goto-char start-point
)
363 (looking-at "[\r\n]*[a-zA-Z][a-zA-Z0-9-]*:")))
365 (defun mm-uu-postscript-extract ()
366 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
367 '("application/postscript")))
369 (defun mm-uu-verbatim-marks-extract (start-offset end-offset
373 (let ((start (or (and mm-uu-hide-markers
377 (end (or (and mm-uu-hide-markers
382 (mm-uu-copy-to-buffer
383 (progn (goto-char start-point
)
386 (progn (goto-char end-point
)
390 '("text/x-verbatim" (charset . gnus-decoded
)))))
392 (defun mm-uu-latex-extract ()
394 (mm-uu-copy-to-buffer start-point end-point t
)
395 ;; application/x-tex?
396 '("text/x-verbatim" (charset . gnus-decoded
))))
398 (defun mm-uu-emacs-sources-extract ()
399 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
400 '("application/emacs-lisp" (charset . gnus-decoded
))
402 (list mm-dissect-disposition
403 (cons 'filename file-name
))))
405 (defun mm-uu-org-src-code-block-extract ()
406 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
409 (defvar gnus-newsgroup-name
)
411 (defun mm-uu-emacs-sources-test ()
412 (setq file-name
(match-string 1))
413 (and gnus-newsgroup-name
414 mm-uu-emacs-sources-regexp
415 (string-match mm-uu-emacs-sources-regexp gnus-newsgroup-name
)))
417 (defun mm-uu-diff-extract ()
418 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
419 '("text/x-patch" (charset . gnus-decoded
))))
421 (defun mm-uu-diff-test ()
422 (and gnus-newsgroup-name
423 mm-uu-diff-groups-regexp
424 (string-match mm-uu-diff-groups-regexp gnus-newsgroup-name
)))
426 (defun mm-uu-latex-test ()
427 (and gnus-newsgroup-name
428 mm-uu-tex-groups-regexp
429 (string-match mm-uu-tex-groups-regexp gnus-newsgroup-name
)))
431 (defun mm-uu-forward-extract ()
432 (mm-make-handle (mm-uu-copy-to-buffer
434 (goto-char start-point
)
436 (skip-chars-forward "\n")
438 (progn (goto-char end-point
) (forward-line -
1) (point)))
439 '("message/rfc822" (charset . gnus-decoded
))))
441 (defun mm-uu-uu-extract ()
442 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
443 (list (or (and file-name
444 (string-match "\\.[^\\.]+$"
446 (mailcap-extension-to-mime
447 (match-string 0 file-name
)))
448 "application/octet-stream"))
450 (if (and file-name
(not (equal file-name
"")))
451 (list mm-dissect-disposition
452 (cons 'filename file-name
)))))
454 (defun mm-uu-binhex-extract ()
455 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
456 (list (or (and file-name
457 (string-match "\\.[^\\.]+$" file-name
)
458 (mailcap-extension-to-mime
459 (match-string 0 file-name
)))
460 "application/octet-stream"))
462 (if (and file-name
(not (equal file-name
"")))
463 (list mm-dissect-disposition
464 (cons 'filename file-name
)))))
466 (defvar gnus-original-article-buffer
) ; gnus.el
468 (defun mm-uu-yenc-extract ()
469 ;; This might not be exactly correct, but we sure can't get the
470 ;; binary data from the article buffer, since that's already in a
471 ;; non-binary charset. So get it from the original article buffer.
472 (mm-make-handle (with-current-buffer gnus-original-article-buffer
473 (mm-uu-copy-to-buffer start-point end-point
))
474 (list (or (and file-name
475 (string-match "\\.[^\\.]+$" file-name
)
476 (mailcap-extension-to-mime
477 (match-string 0 file-name
)))
478 "application/octet-stream"))
480 (if (and file-name
(not (equal file-name
"")))
481 (list mm-dissect-disposition
482 (cons 'filename file-name
)))))
485 (defun mm-uu-shar-extract ()
486 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
487 '("application/x-shar")))
489 (defun mm-uu-gnatsweb-extract ()
491 (goto-char start-point
)
493 (narrow-to-region (point) end-point
)
494 (mm-dissect-buffer t
)))
496 (defun mm-uu-pgp-signed-test (&rest rest
)
499 (mml2015-clear-verify-function)
501 ((eq mm-verify-option
'never
) nil
)
502 ((eq mm-verify-option
'always
) t
)
503 ((eq mm-verify-option
'known
) t
)
505 (y-or-n-p "Verify pgp signed part? ")
508 (defvar gnus-newsgroup-charset
)
510 (defun mm-uu-pgp-signed-extract-1 (handles ctl
)
511 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max))))
512 (with-current-buffer buf
513 (if (mm-uu-pgp-signed-test)
515 (mml2015-clean-buffer)
516 (let ((coding-system-for-write (or buffer-file-coding-system
517 gnus-newsgroup-charset
519 (coding-system-for-read (or buffer-file-coding-system
520 gnus-newsgroup-charset
522 (funcall (mml2015-clear-verify-function))))
523 (when (and mml2015-use
(null (mml2015-clear-verify-function)))
524 (mm-set-handle-multipart-parameter
525 mm-security-handle
'gnus-details
526 (format "Clear verification not supported by `%s'.\n" mml2015-use
)))
527 (mml2015-extract-cleartext-signature))
528 (list (mm-make-handle buf mm-uu-text-plain-type
)))))
530 (defun mm-uu-pgp-signed-extract ()
531 (let ((mm-security-handle (list (format "multipart/signed"))))
532 (mm-set-handle-multipart-parameter
533 mm-security-handle
'protocol
"application/x-gnus-pgp-signature")
535 (narrow-to-region start-point end-point
)
536 (add-text-properties 0 (length (car mm-security-handle
))
537 (list 'buffer
(mm-uu-copy-to-buffer))
538 (car mm-security-handle
))
539 (setcdr mm-security-handle
540 (mm-uu-pgp-signed-extract-1 nil
541 mm-security-handle
)))
544 (defun mm-uu-pgp-encrypted-test (&rest rest
)
547 (mml2015-clear-decrypt-function)
549 ((eq mm-decrypt-option
'never
) nil
)
550 ((eq mm-decrypt-option
'always
) t
)
551 ((eq mm-decrypt-option
'known
) t
)
553 (y-or-n-p "Decrypt pgp encrypted part? ")
556 (defun mm-uu-pgp-encrypted-extract-1 (handles ctl
)
557 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max)))
560 ;; Make sure there's a blank line between header and body.
561 (with-current-buffer buf
562 (goto-char (point-min))
566 (looking-at "[^\t\n ]+:")
567 (looking-at "[^\t\n ]+:\\|[\t ]"))
569 (unless (memq (char-after) '(?
\n nil
))
572 (narrow-to-region (point-min) (point))
573 (setq charset
(mail-fetch-field "charset")))
574 (if (and (mm-uu-pgp-encrypted-test)
576 (mml2015-clean-buffer)
577 (funcall (mml2015-clear-decrypt-function))
578 (equal (mm-handle-multipart-ctl-parameter mm-security-handle
584 (setq charset gnus-newsgroup-charset
))
585 (setq charset
(mm-charset-to-coding-system charset
))
586 (not (eq charset
'ascii
)))
587 ;; Assume that buffer's multibyteness is turned off.
588 ;; See `mml2015-pgg-clear-decrypt'.
589 (insert (mm-decode-coding-string (prog1
592 (mm-enable-multibyte))
594 (mm-enable-multibyte))
595 (list (mm-make-handle buf mm-uu-text-plain-type
)))
596 (list (mm-make-handle buf
'("application/pgp-encrypted")))))))
598 (defun mm-uu-pgp-encrypted-extract ()
599 (let ((mm-security-handle (list (format "multipart/encrypted"))))
600 (mm-set-handle-multipart-parameter
601 mm-security-handle
'protocol
"application/x-gnus-pgp-encrypted")
603 (narrow-to-region start-point end-point
)
604 (add-text-properties 0 (length (car mm-security-handle
))
605 (list 'buffer
(mm-uu-copy-to-buffer))
606 (car mm-security-handle
))
607 (setcdr mm-security-handle
608 (mm-uu-pgp-encrypted-extract-1 nil
609 mm-security-handle
)))
612 (defun mm-uu-gpg-key-skip-to-last ()
613 (let ((point (point))
614 (end-regexp (mm-uu-end-regexp entry
))
615 (beginning-regexp (mm-uu-beginning-regexp entry
)))
616 (when (and end-regexp
617 (not (mm-uu-configure-p (mm-uu-type entry
) 'disabled
)))
618 (while (re-search-forward end-regexp nil t
)
619 (skip-chars-forward " \t\n\r")
620 (if (looking-at beginning-regexp
)
621 (setq point
(match-end 0)))))
624 (defun mm-uu-pgp-key-extract ()
625 (let ((buf (mm-uu-copy-to-buffer start-point end-point
)))
627 '("application/pgp-keys"))))
630 (defun mm-uu-dissect (&optional noheader mime-type
)
631 "Dissect the current buffer and return a list of uu handles.
632 The optional NOHEADER means there's no header in the buffer.
633 MIME-TYPE specifies a MIME type and parameters, which defaults to the
634 value of `mm-uu-text-plain-type'."
635 (let ((case-fold-search t
)
636 (mm-uu-text-plain-type (or mime-type mm-uu-text-plain-type
))
637 text-start start-point end-point file-name result entry func
)
639 (goto-char (point-min))
644 ((search-forward "\n\n" nil t
)
646 (t (goto-char (point-max))))
647 (setq text-start
(point))
648 (while (re-search-forward mm-uu-beginning-regexp nil t
)
649 (setq start-point
(match-beginning 0)
651 (let ((alist mm-uu-type-alist
)
652 (beginning-regexp (match-string 0)))
654 (if (string-match (mm-uu-beginning-regexp (car alist
))
656 (setq entry
(car alist
))
658 (if (setq func
(mm-uu-function-1 entry
))
660 (forward-line);; in case of failure
661 (when (and (not (mm-uu-configure-p (mm-uu-type entry
) 'disabled
))
662 (let ((end-regexp (mm-uu-end-regexp entry
)))
664 (or (setq end-point
(point-max)) t
)
666 (re-search-forward end-regexp nil t
)
668 (setq end-point
(point)))))
669 (or (not (setq func
(mm-uu-function-2 entry
)))
671 (if (and (> start-point text-start
)
673 (goto-char text-start
)
674 (re-search-forward "." start-point t
)))
676 (mm-make-handle (mm-uu-copy-to-buffer text-start start-point
)
677 mm-uu-text-plain-type
)
680 (funcall (mm-uu-function-extract entry
))
682 (goto-char (setq text-start end-point
))))
684 (if (and (> (point-max) (1+ text-start
))
686 (goto-char text-start
)
687 (re-search-forward "." nil t
)))
689 (mm-make-handle (mm-uu-copy-to-buffer text-start
(point-max))
690 mm-uu-text-plain-type
)
692 (setq result
(cons "multipart/mixed" (nreverse result
))))
696 (defun mm-uu-dissect-text-parts (handle &optional decoded
)
697 "Dissect text parts and put uu handles into HANDLE.
698 Assume text has been decoded if DECODED is non-nil."
699 (let ((buffer (mm-handle-buffer handle
)))
700 (cond ((stringp buffer
)
701 (dolist (elem (cdr handle
))
702 (mm-uu-dissect-text-parts elem decoded
)))
704 (let ((type (mm-handle-media-type handle
))
705 (case-fold-search t
) ;; string-match
706 children charset encoding
)
709 ;; Mutt still uses application/pgp even though
710 ;; it has already been withdrawn.
711 (string-match "\\`text/\\|\\`application/pgp\\'" type
)
712 (equal (car (mm-handle-disposition handle
))
716 (with-current-buffer buffer
719 (eq (setq charset
(mail-content-type-get
720 (mm-handle-type handle
)
725 t
(cons type
'((charset . gnus-decoded
)))))
728 (mm-with-multibyte-buffer
729 (insert (mm-decode-string (mm-get-part handle
)
732 t
(cons type
'((charset . gnus-decoded
))))))
733 ((setq encoding
(mm-handle-encoding handle
))
735 ;; Inherit the multibyteness of the `buffer'.
737 (insert-buffer-substring buffer
)
738 (mm-decode-content-transfer-encoding
740 (mm-uu-dissect t
(list type
))))
743 (mm-uu-dissect t
(list type
)))))))
744 ;; Ignore it if a given part is dissected into a single
745 ;; part of which the type is the same as the given one.
746 (if (and (<= (length children
) 2)
747 (string-equal (mm-handle-media-type (cadr children
))
749 (kill-buffer (mm-handle-buffer (cadr children
)))
751 (setcdr handle
(cdr children
))
752 (setcar handle
(car children
)) ;; "multipart/mixed"
753 (dolist (elem (cdr children
))
754 (mm-uu-dissect-text-parts elem decoded
))))))
756 (dolist (elem handle
)
757 (mm-uu-dissect-text-parts elem decoded
))))))
761 ;;; mm-uu.el ends here