1 ;;; mm-uu.el --- Return uu stuff as mm handles
3 ;; Copyright (C) 1998-2016 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é <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 (defcustom mm-uu-hide-markers
(< 16 (length (defined-colors)))
253 "If non-nil, hide verbatim markers.
254 The value should be nil on displays where the face
255 `mm-uu-extract' isn't distinguishable to the face `default'."
256 :type
'(choice (const :tag
"Hide" t
)
257 (const :tag
"Don't hide" nil
))
258 :version
"23.1" ;; No Gnus
259 :group
'gnus-article-mime
)
261 (defface mm-uu-extract
'(;; Inspired by `gnus-cite-3'
265 (:background
"dark blue"))
268 (:foreground
"light yellow"
269 :background
"dark green"))
273 (:foreground
"dark blue"))
276 (:foreground
"dark green"
277 :background
"light yellow"))
280 "Face for extracted buffers."
281 ;; See `mm-uu-verbatim-marks-extract'.
282 :version
"23.1" ;; No Gnus
283 :group
'gnus-article-mime
)
285 (defun mm-uu-copy-to-buffer (&optional from to properties
)
286 "Copy the contents of the current buffer to a fresh buffer.
289 If PROPERTIES is non-nil, PROPERTIES are applied to the buffer,
290 see `set-text-properties'. If PROPERTIES equals t, this means to
291 apply the face `mm-uu-extract'."
292 (let ((obuf (current-buffer))
293 (multi enable-multibyte-characters
)
294 (coding-system buffer-file-coding-system
))
295 (with-current-buffer (generate-new-buffer " *mm-uu*")
296 (if multi
(mm-enable-multibyte) (mm-disable-multibyte))
297 (setq buffer-file-coding-system coding-system
)
298 (insert-buffer-substring obuf from to
)
299 (cond ((eq properties t
)
300 (set-text-properties (point-min) (point-max)
301 '(face mm-uu-extract
)))
303 (set-text-properties (point-min) (point-max) properties
)))
306 (defun mm-uu-configure-p (key val
)
307 (member (cons key val
) mm-uu-configure-list
))
309 (defun mm-uu-configure (&optional symbol value
)
310 "Configure detection of non-MIME attachments."
312 (if symbol
(set-default symbol value
))
313 (setq mm-uu-beginning-regexp nil
)
314 (mapcar (lambda (mm-uu-entry)
315 (if (mm-uu-configure-p (mm-uu-type mm-uu-entry
) 'disabled
)
317 (setq mm-uu-beginning-regexp
318 (concat mm-uu-beginning-regexp
319 (if mm-uu-beginning-regexp
"\\|")
320 (mm-uu-beginning-regexp mm-uu-entry
)))))
330 (defun mm-uu-uu-filename ()
331 (if (looking-at ".+")
333 (let ((nnheader-file-name-translation-alist
334 '((?
/ . ?
,) (?\ . ?_
) (?
* . ?_
) (?$ . ?_
))))
335 (nnheader-translate-file-chars (match-string 0))))))
337 (defun mm-uu-binhex-filename ()
340 (binhex-decode-region start-point end-point t
))))
342 (defun mm-uu-yenc-filename ()
343 (goto-char start-point
)
346 (yenc-extract-filename))))
348 (defun mm-uu-forward-test ()
350 (goto-char start-point
)
352 (looking-at "[\r\n]*[a-zA-Z][a-zA-Z0-9-]*:")))
354 (defun mm-uu-postscript-extract ()
355 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
356 '("application/postscript")))
358 (defun mm-uu-verbatim-marks-extract (start-offset end-offset
362 (let ((start (or (and mm-uu-hide-markers
366 (end (or (and mm-uu-hide-markers
371 (mm-uu-copy-to-buffer
372 (progn (goto-char start-point
)
375 (progn (goto-char end-point
)
379 '("text/x-verbatim" (charset . gnus-decoded
)))))
381 (defun mm-uu-latex-extract ()
383 (mm-uu-copy-to-buffer start-point end-point t
)
384 ;; application/x-tex?
385 '("text/x-verbatim" (charset . gnus-decoded
))))
387 (defun mm-uu-emacs-sources-extract ()
388 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
389 '("application/emacs-lisp" (charset . gnus-decoded
))
391 (list mm-dissect-disposition
392 (cons 'filename file-name
))))
394 (defun mm-uu-org-src-code-block-extract ()
395 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
398 (defvar gnus-newsgroup-name
)
400 (defun mm-uu-emacs-sources-test ()
401 (setq file-name
(match-string 1))
402 (and gnus-newsgroup-name
403 mm-uu-emacs-sources-regexp
404 (string-match mm-uu-emacs-sources-regexp gnus-newsgroup-name
)))
406 (defun mm-uu-diff-extract ()
407 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
408 '("text/x-patch" (charset . gnus-decoded
))))
410 (defun mm-uu-diff-test ()
411 (and gnus-newsgroup-name
412 mm-uu-diff-groups-regexp
413 (string-match mm-uu-diff-groups-regexp gnus-newsgroup-name
)))
415 (defun mm-uu-latex-test ()
416 (and gnus-newsgroup-name
417 mm-uu-tex-groups-regexp
418 (string-match mm-uu-tex-groups-regexp gnus-newsgroup-name
)))
420 (defun mm-uu-forward-extract ()
421 (mm-make-handle (mm-uu-copy-to-buffer
423 (goto-char start-point
)
425 (skip-chars-forward "\n")
427 (progn (goto-char end-point
) (forward-line -
1) (point)))
428 '("message/rfc822" (charset . gnus-decoded
))))
430 (defun mm-uu-uu-extract ()
431 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
432 (list (or (and file-name
433 (string-match "\\.[^\\.]+$"
435 (mailcap-extension-to-mime
436 (match-string 0 file-name
)))
437 "application/octet-stream"))
439 (if (and file-name
(not (equal file-name
"")))
440 (list mm-dissect-disposition
441 (cons 'filename file-name
)))))
443 (defun mm-uu-binhex-extract ()
444 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
445 (list (or (and file-name
446 (string-match "\\.[^\\.]+$" file-name
)
447 (mailcap-extension-to-mime
448 (match-string 0 file-name
)))
449 "application/octet-stream"))
451 (if (and file-name
(not (equal file-name
"")))
452 (list mm-dissect-disposition
453 (cons 'filename file-name
)))))
455 (defvar gnus-original-article-buffer
) ; gnus.el
457 (defun mm-uu-yenc-extract ()
458 ;; This might not be exactly correct, but we sure can't get the
459 ;; binary data from the article buffer, since that's already in a
460 ;; non-binary charset. So get it from the original article buffer.
461 (mm-make-handle (with-current-buffer gnus-original-article-buffer
462 (mm-uu-copy-to-buffer start-point end-point
))
463 (list (or (and file-name
464 (string-match "\\.[^\\.]+$" file-name
)
465 (mailcap-extension-to-mime
466 (match-string 0 file-name
)))
467 "application/octet-stream"))
469 (if (and file-name
(not (equal file-name
"")))
470 (list mm-dissect-disposition
471 (cons 'filename file-name
)))))
474 (defun mm-uu-shar-extract ()
475 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point
)
476 '("application/x-shar")))
478 (defun mm-uu-gnatsweb-extract ()
480 (goto-char start-point
)
482 (narrow-to-region (point) end-point
)
483 (mm-dissect-buffer t
)))
485 (defun mm-uu-pgp-signed-test (&rest rest
)
488 (mml2015-clear-verify-function)
490 ((eq mm-verify-option
'never
) nil
)
491 ((eq mm-verify-option
'always
) t
)
492 ((eq mm-verify-option
'known
) t
)
494 (y-or-n-p "Verify pgp signed part? ")
497 (defvar gnus-newsgroup-charset
)
499 (defun mm-uu-pgp-signed-extract-1 (handles ctl
)
500 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max))))
501 (with-current-buffer buf
502 (if (mm-uu-pgp-signed-test)
504 (mml2015-clean-buffer)
505 (let ((coding-system-for-write (or buffer-file-coding-system
506 gnus-newsgroup-charset
508 (coding-system-for-read (or buffer-file-coding-system
509 gnus-newsgroup-charset
511 (funcall (mml2015-clear-verify-function))))
512 (when (and mml2015-use
(null (mml2015-clear-verify-function)))
513 (mm-set-handle-multipart-parameter
514 mm-security-handle
'gnus-details
516 "Clear verification not supported by `%s'.\n" mml2015-use
)))
517 (mml2015-extract-cleartext-signature))
518 (list (mm-make-handle buf mm-uu-text-plain-type
)))))
520 (defun mm-uu-pgp-signed-extract ()
521 (let ((mm-security-handle (list (format "multipart/signed"))))
522 (mm-set-handle-multipart-parameter
523 mm-security-handle
'protocol
"application/x-gnus-pgp-signature")
525 (narrow-to-region start-point end-point
)
526 (add-text-properties 0 (length (car mm-security-handle
))
527 (list 'buffer
(mm-uu-copy-to-buffer))
528 (car mm-security-handle
))
529 (setcdr mm-security-handle
530 (mm-uu-pgp-signed-extract-1 nil
531 mm-security-handle
)))
534 (defun mm-uu-pgp-encrypted-test (&rest rest
)
537 (mml2015-clear-decrypt-function)
539 ((eq mm-decrypt-option
'never
) nil
)
540 ((eq mm-decrypt-option
'always
) t
)
541 ((eq mm-decrypt-option
'known
) t
)
543 (y-or-n-p "Decrypt pgp encrypted part? ")
546 (defun mm-uu-pgp-encrypted-extract-1 (handles ctl
)
547 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max)))
550 ;; Make sure there's a blank line between header and body.
551 (with-current-buffer buf
552 (goto-char (point-min))
556 (looking-at "[^\t\n ]+:")
557 (looking-at "[^\t\n ]+:\\|[\t ]"))
559 (unless (memq (char-after) '(?
\n nil
))
562 (narrow-to-region (point-min) (point))
563 (setq charset
(mail-fetch-field "charset")))
564 (if (and (mm-uu-pgp-encrypted-test)
566 (mml2015-clean-buffer)
567 (funcall (mml2015-clear-decrypt-function))
568 (equal (mm-handle-multipart-ctl-parameter mm-security-handle
574 (setq charset gnus-newsgroup-charset
))
575 (setq charset
(mm-charset-to-coding-system charset
))
576 (not (eq charset
'ascii
)))
577 ;; Assume that buffer's multibyteness is turned off.
578 ;; See `mml2015-pgg-clear-decrypt'.
579 (insert (decode-coding-string (prog1
582 (mm-enable-multibyte))
584 (mm-enable-multibyte))
585 (list (mm-make-handle buf mm-uu-text-plain-type
)))
586 (list (mm-make-handle buf
'("application/pgp-encrypted")))))))
588 (defun mm-uu-pgp-encrypted-extract ()
589 (let ((mm-security-handle (list (format "multipart/encrypted"))))
590 (mm-set-handle-multipart-parameter
591 mm-security-handle
'protocol
"application/x-gnus-pgp-encrypted")
593 (narrow-to-region start-point end-point
)
594 (add-text-properties 0 (length (car mm-security-handle
))
595 (list 'buffer
(mm-uu-copy-to-buffer))
596 (car mm-security-handle
))
597 (setcdr mm-security-handle
598 (mm-uu-pgp-encrypted-extract-1 nil
599 mm-security-handle
)))
602 (defun mm-uu-gpg-key-skip-to-last ()
603 (let ((point (point))
604 (end-regexp (mm-uu-end-regexp mm-uu-entry
))
605 (beginning-regexp (mm-uu-beginning-regexp mm-uu-entry
)))
606 (when (and end-regexp
607 (not (mm-uu-configure-p (mm-uu-type mm-uu-entry
) 'disabled
)))
608 (while (re-search-forward end-regexp nil t
)
609 (skip-chars-forward " \t\n\r")
610 (if (looking-at beginning-regexp
)
611 (setq point
(match-end 0)))))
614 (defun mm-uu-pgp-key-extract ()
615 (let ((buf (mm-uu-copy-to-buffer start-point end-point
)))
617 '("application/pgp-keys"))))
620 (defun mm-uu-dissect (&optional noheader mime-type
)
621 "Dissect the current buffer and return a list of uu handles.
622 The optional NOHEADER means there's no header in the buffer.
623 MIME-TYPE specifies a MIME type and parameters, which defaults to the
624 value of `mm-uu-text-plain-type'."
625 (let ((case-fold-search t
)
626 (mm-uu-text-plain-type (or mime-type mm-uu-text-plain-type
))
627 text-start start-point end-point file-name result entry func
)
629 (goto-char (point-min))
634 ((search-forward "\n\n" nil t
)
636 (t (goto-char (point-max))))
637 (setq text-start
(point))
638 (while (re-search-forward mm-uu-beginning-regexp nil t
)
639 (setq start-point
(match-beginning 0)
641 (let ((alist mm-uu-type-alist
)
642 (beginning-regexp (match-string 0)))
644 (if (string-match (mm-uu-beginning-regexp (car alist
))
646 (setq entry
(car alist
))
648 (if (setq func
(mm-uu-function-1 entry
))
650 (forward-line);; in case of failure
651 (when (and (not (mm-uu-configure-p (mm-uu-type entry
) 'disabled
))
652 (let ((end-regexp (mm-uu-end-regexp entry
)))
654 (or (setq end-point
(point-max)) t
)
656 (re-search-forward end-regexp nil t
)
658 (setq end-point
(point)))))
659 (or (not (setq func
(mm-uu-function-2 entry
)))
661 (if (and (> start-point text-start
)
663 (goto-char text-start
)
664 (re-search-forward "." start-point t
)))
667 (mm-uu-copy-to-buffer
669 ;; A start-separator is likely accompanied by
670 ;; a leading newline.
671 (if (and (eq (char-before start-point
) ?
\n)
672 (eq (char-before (1- start-point
)) ?
\n))
675 mm-uu-text-plain-type
)
678 (funcall (mm-uu-function-extract entry
))
680 (goto-char (setq text-start end-point
))))
682 (goto-char text-start
)
683 (when (re-search-forward "." nil t
)
684 (push (mm-make-handle
685 (mm-uu-copy-to-buffer
686 ;; An end-separator is likely accompanied by
687 ;; a trailing newline.
688 (if (eq (char-after text-start
) ?
\n)
692 mm-uu-text-plain-type
)
694 (setq result
(cons "multipart/mixed" (nreverse result
))))
698 (defun mm-uu-dissect-text-parts (handle &optional decoded
)
699 "Dissect text parts and put uu handles into HANDLE.
700 Assume text has been decoded if DECODED is non-nil."
701 (let ((buffer (mm-handle-buffer handle
)))
702 (cond ((stringp buffer
)
703 (dolist (elem (cdr handle
))
704 (mm-uu-dissect-text-parts elem decoded
)))
706 (let ((type (mm-handle-media-type handle
))
707 (case-fold-search t
) ;; string-match
708 children charset encoding
)
711 ;; Mutt still uses application/pgp even though
712 ;; it has already been withdrawn.
713 (string-match "\\`text/\\|\\`application/pgp\\'" type
)
714 (equal (car (mm-handle-disposition handle
))
718 (with-current-buffer buffer
721 (eq (setq charset
(mail-content-type-get
722 (mm-handle-type handle
)
727 t
(cons type
'((charset . gnus-decoded
)))))
730 (mm-with-multibyte-buffer
731 (insert (mm-decode-string (mm-get-part handle
)
734 t
(cons type
'((charset . gnus-decoded
))))))
735 ((setq encoding
(mm-handle-encoding handle
))
737 ;; Inherit the multibyteness of the `buffer'.
739 (insert-buffer-substring buffer
)
740 (mm-decode-content-transfer-encoding
742 (mm-uu-dissect t
(list type
))))
745 (mm-uu-dissect t
(list type
)))))))
746 ;; Ignore it if a given part is dissected into a single
747 ;; part of which the type is the same as the given one.
748 (if (and (<= (length children
) 2)
749 (string-equal (mm-handle-media-type (cadr children
))
751 (kill-buffer (mm-handle-buffer (cadr children
)))
753 (setcdr handle
(cdr children
))
754 (setcar handle
(car children
)) ;; "multipart/mixed"
755 (dolist (elem (cdr children
))
756 (mm-uu-dissect-text-parts elem decoded
))))))
758 (dolist (elem handle
)
759 (mm-uu-dissect-text-parts elem decoded
))))))
767 ;;; mm-uu.el ends here