1 ;;; mm-uu.el --- Return uu stuff as mm handles
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: postscript uudecode binhex shar forward gnatsweb pgp
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., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl))
37 (autoload 'uudecode-decode-region "uudecode")
38 (autoload 'uudecode-decode-region-external "uudecode")
39 (autoload 'uudecode-decode-region-internal "uudecode")
41 (autoload 'binhex-decode-region "binhex")
42 (autoload 'binhex-decode-region-external "binhex")
43 (autoload 'binhex-decode-region-internal "binhex")
45 (autoload 'yenc-decode-region "yenc")
46 (autoload 'yenc-extract-filename "yenc")
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."
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)
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)
69 (defvar mm-uu-yenc-decode-function 'yenc-decode-region)
71 (defvar mm-uu-pgp-beginning-signature
72 "^-----BEGIN PGP SIGNATURE-----")
74 (defvar mm-uu-beginning-regexp nil)
76 (defvar mm-dissect-disposition "inline"
77 "The default disposition of uu parts.
78 This can be either \"inline\" or \"attachment\".")
80 (defcustom mm-uu-emacs-sources-regexp "\\.emacs\\.sources"
81 "The regexp of Emacs sources groups."
84 :group 'gnus-article-mime)
86 (defcustom mm-uu-diff-groups-regexp
87 "\\(gmane\\|gnu\\)\\..*\\(diff\\|commit\\|cvs\\|bug\\|devel\\)"
88 "Regexp matching diff groups."
91 :group 'gnus-article-mime)
93 (defcustom mm-uu-tex-groups-regexp "\\.tex\\>"
94 "*Regexp matching TeX groups."
97 :group 'gnus-article-mime)
99 (defvar mm-uu-type-alist
103 mm-uu-postscript-extract
105 (uu ;; Maybe we should have a more strict test here.
106 "^begin[ \t]+0?[0-7][0-7][0-7][ \t]+"
115 mm-uu-binhex-filename)
117 "^=ybegin.*size=[0-9]+.*name=.*$"
118 "^=yend.*size=[0-9]+"
126 ;; Thanks to Edward J. Sabol <sabol@alderaan.gsfc.nasa.gov> and
127 ;; Peter von der Ah\'e <pahe@daimi.au.dk>
128 "^-+ \\(Start of \\)?Forwarded message"
129 "^-+ End \\(of \\)?forwarded message"
130 mm-uu-forward-extract
134 "^----gnatsweb-attachment----"
136 mm-uu-gnatsweb-extract)
138 "^-----BEGIN PGP SIGNED MESSAGE-----"
139 "^-----END PGP SIGNATURE-----"
140 mm-uu-pgp-signed-extract
144 "^-----BEGIN PGP MESSAGE-----"
145 "^-----END PGP MESSAGE-----"
146 mm-uu-pgp-encrypted-extract
150 "^-----BEGIN PGP PUBLIC KEY BLOCK-----"
151 "^-----END PGP PUBLIC KEY BLOCK-----"
152 mm-uu-pgp-key-extract
153 mm-uu-gpg-key-skip-to-last
156 "^;;;?[ \t]*[^ \t]+\\.el[ \t]*--"
157 "^;;;?[ \t]*\\([^ \t]+\\.el\\)[ \t]+ends here"
158 mm-uu-emacs-sources-extract
160 mm-uu-emacs-sources-test)
168 ;; Text enclosed with tags similar to `message-mark-insert-begin' and
169 ;; `message-mark-insert-end'. Don't use those variables to avoid
170 ;; dependency on `message.el'.
171 "^-+[8<>]*-\\{9,\\}[a-z ]+-\\{9,\\}[a-z ]+-\\{9,\\}[8<>]*-+$"
172 "^-+[8<>]*-\\{9,\\}[a-z ]+-\\{9,\\}[a-z ]+-\\{9,\\}[8<>]*-+$"
173 (lambda () (mm-uu-verbatim-marks-extract 0 -1 1 -1))
175 ;; Omitting [a-z8<] leads to false positives (bogus signature separators
176 ;; and mailing list banners).
178 "^ *\\(-\\|_\\)\\{30,\\}.*[a-z8<].*\\(-\\|_\\)\\{30,\\} *$"
179 "^ *\\(-\\|_\\)\\{30,\\}.*[a-z8<].*\\(-\\|_\\)\\{30,\\} *$"
180 (lambda () (mm-uu-verbatim-marks-extract 0 0 1 -1))
183 ;; slrn-style verbatim marks, see
184 ;; http://www.slrn.org/manual/slrn-manual-6.html#ss6.81
187 (lambda () (mm-uu-verbatim-marks-extract 0 0))
190 "^\\([\\\\%][^\n]+\n\\)*\\\\documentclass.*[[{%]"
195 "A list of specifications for non-MIME attachments.
196 Each element consist of the following entries: label,
197 start-regexp, end-regexp, extract-function, test-function.
199 After modifying this list you must run \\[mm-uu-configure].
201 You can disable elements from this list by customizing
202 `mm-uu-configure-list'.")
204 (defcustom mm-uu-configure-list '((shar . disabled))
205 "A list of mm-uu configuration.
206 To disable dissecting shar codes, for instance, add
207 `(shar . disabled)' to this list."
209 :options (mapcar (lambda (entry)
210 (list (car entry) '(const disabled)))
212 :group 'gnus-article-mime)
214 (defvar mm-uu-text-plain-type '("text/plain" (charset . gnus-decoded))
215 "MIME type and parameters for text/plain parts.
216 `gnus-decoded' is a fake charset, which means no further decoding.")
220 (defsubst mm-uu-type (entry)
223 (defsubst mm-uu-beginning-regexp (entry)
226 (defsubst mm-uu-end-regexp (entry)
229 (defsubst mm-uu-function-extract (entry)
232 (defsubst mm-uu-function-1 (entry)
235 (defsubst mm-uu-function-2 (entry)
238 ;; In Emacs 22, we could use `min-colors' in the face definition. But Emacs
239 ;; 21 and XEmacs don't support it.
240 (defcustom mm-uu-hide-markers
241 (< 16 (or (and (fboundp 'defined-colors)
242 (length (defined-colors)))
243 (and (fboundp 'device-color-cells)
244 (device-color-cells))
246 "If non-nil, hide verbatim markers.
247 The value should be nil on displays where the face
248 `mm-uu-extract' isn't distinguishable to the face `default'."
249 :type '(choice (const :tag "Hide" t)
250 (const :tag "Don't hide" nil))
251 :version "23.0" ;; No Gnus
252 :group 'gnus-article-mime)
254 (defface mm-uu-extract '(;; Colors from `gnus-cite-3' plus background:
257 (:foreground "light yellow"
258 :background "dark green"))
261 (:foreground "dark green"
262 :background "light yellow"))
265 "Face for extracted buffers."
266 ;; See `mm-uu-verbatim-marks-extract'.
267 :version "23.0" ;; No Gnus
268 :group 'gnus-article-mime)
270 (defun mm-uu-copy-to-buffer (&optional from to properties)
271 "Copy the contents of the current buffer to a fresh buffer.
274 If PROPERTIES is non-nil, PROPERTIES are applied to the buffer,
275 see `set-text-properties'. If PROPERTIES equals t, this means to
276 apply the face `mm-uu-extract'."
277 (let ((obuf (current-buffer))
279 ;; Might not exist in non-MULE XEmacs
280 (when (boundp 'buffer-file-coding-system)
281 buffer-file-coding-system)))
282 (with-current-buffer (generate-new-buffer " *mm-uu*")
283 (setq buffer-file-coding-system coding-system)
284 (insert-buffer-substring obuf from to)
285 (cond ((eq properties t)
286 (set-text-properties (point-min) (point-max)
287 '(face mm-uu-extract)))
289 (set-text-properties (point-min) (point-max) properties)))
292 (defun mm-uu-configure-p (key val)
293 (member (cons key val) mm-uu-configure-list))
295 (defun mm-uu-configure (&optional symbol value)
296 "Configure detection of non-MIME attachments."
298 (if symbol (set-default symbol value))
299 (setq mm-uu-beginning-regexp nil)
300 (mapcar (lambda (entry)
301 (if (mm-uu-configure-p (mm-uu-type entry) 'disabled)
303 (setq mm-uu-beginning-regexp
304 (concat mm-uu-beginning-regexp
305 (if mm-uu-beginning-regexp "\\|")
306 (mm-uu-beginning-regexp entry)))))
317 (defun mm-uu-uu-filename ()
318 (if (looking-at ".+")
320 (let ((nnheader-file-name-translation-alist
321 '((?/ . ?,) (?\ . ?_) (?* . ?_) (?$ . ?_))))
322 (nnheader-translate-file-chars (match-string 0))))))
324 (defun mm-uu-binhex-filename ()
327 (binhex-decode-region start-point end-point t))))
329 (defun mm-uu-yenc-filename ()
330 (goto-char start-point)
333 (yenc-extract-filename))))
335 (defun mm-uu-forward-test ()
337 (goto-char start-point)
339 (looking-at "[\r\n]*[a-zA-Z][a-zA-Z0-9-]*:")))
341 (defun mm-uu-postscript-extract ()
342 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
343 '("application/postscript")))
345 (defun mm-uu-verbatim-marks-extract (start-offset end-offset
349 (let ((start (or (and mm-uu-hide-markers
353 (end (or (and mm-uu-hide-markers
358 (mm-uu-copy-to-buffer
359 (progn (goto-char start-point)
362 (progn (goto-char end-point)
366 '("text/x-verbatim" (charset . gnus-decoded)))))
368 (defun mm-uu-latex-extract ()
370 (mm-uu-copy-to-buffer start-point end-point t)
371 ;; application/x-tex?
372 '("text/x-verbatim" (charset . gnus-decoded))))
374 (defun mm-uu-emacs-sources-extract ()
375 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
376 '("application/emacs-lisp" (charset . gnus-decoded))
378 (list mm-dissect-disposition
379 (cons 'filename file-name))))
382 (defvar gnus-newsgroup-name))
384 (defun mm-uu-emacs-sources-test ()
385 (setq file-name (match-string 1))
386 (and gnus-newsgroup-name
387 mm-uu-emacs-sources-regexp
388 (string-match mm-uu-emacs-sources-regexp gnus-newsgroup-name)))
390 (defun mm-uu-diff-extract ()
391 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
392 '("text/x-patch" (charset . gnus-decoded))))
394 (defun mm-uu-diff-test ()
395 (and gnus-newsgroup-name
396 mm-uu-diff-groups-regexp
397 (string-match mm-uu-diff-groups-regexp gnus-newsgroup-name)))
399 (defun mm-uu-latex-test ()
400 (and gnus-newsgroup-name
401 mm-uu-tex-groups-regexp
402 (string-match mm-uu-tex-groups-regexp gnus-newsgroup-name)))
404 (defun mm-uu-forward-extract ()
405 (mm-make-handle (mm-uu-copy-to-buffer
406 (progn (goto-char start-point) (forward-line) (point))
407 (progn (goto-char end-point) (forward-line -1) (point)))
408 '("message/rfc822" (charset . gnus-decoded))))
410 (defun mm-uu-uu-extract ()
411 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
412 (list (or (and file-name
413 (string-match "\\.[^\\.]+$"
415 (mailcap-extension-to-mime
416 (match-string 0 file-name)))
417 "application/octet-stream"))
419 (if (and file-name (not (equal file-name "")))
420 (list mm-dissect-disposition
421 (cons 'filename file-name)))))
423 (defun mm-uu-binhex-extract ()
424 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
425 (list (or (and file-name
426 (string-match "\\.[^\\.]+$" file-name)
427 (mailcap-extension-to-mime
428 (match-string 0 file-name)))
429 "application/octet-stream"))
431 (if (and file-name (not (equal file-name "")))
432 (list mm-dissect-disposition
433 (cons 'filename file-name)))))
435 (defun mm-uu-yenc-extract ()
436 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
437 (list (or (and file-name
438 (string-match "\\.[^\\.]+$" file-name)
439 (mailcap-extension-to-mime
440 (match-string 0 file-name)))
441 "application/octet-stream"))
443 (if (and file-name (not (equal file-name "")))
444 (list mm-dissect-disposition
445 (cons 'filename file-name)))))
448 (defun mm-uu-shar-extract ()
449 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
450 '("application/x-shar")))
452 (defun mm-uu-gnatsweb-extract ()
454 (goto-char start-point)
456 (narrow-to-region (point) end-point)
457 (mm-dissect-buffer t)))
459 (defun mm-uu-pgp-signed-test (&rest rest)
462 (mml2015-clear-verify-function)
464 ((eq mm-verify-option 'never) nil)
465 ((eq mm-verify-option 'always) t)
466 ((eq mm-verify-option 'known) t)
468 (y-or-n-p "Verify pgp signed part? ")
472 (defvar gnus-newsgroup-charset))
474 (defun mm-uu-pgp-signed-extract-1 (handles ctl)
475 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max))))
476 (with-current-buffer buf
477 (if (mm-uu-pgp-signed-test)
479 (mml2015-clean-buffer)
480 (let ((coding-system-for-write (or gnus-newsgroup-charset
482 (funcall (mml2015-clear-verify-function))))
483 (when (and mml2015-use (null (mml2015-clear-verify-function)))
484 (mm-set-handle-multipart-parameter
485 mm-security-handle 'gnus-details
486 (format "Clear verification not supported by `%s'.\n" mml2015-use))))
487 (goto-char (point-min))
489 ;; We need to be careful not to strip beyond the armor headers.
490 ;; Previously, an attacker could replace the text inside our
491 ;; markup with trailing garbage by injecting whitespace into the
493 (while (looking-at "Hash:") ; The only header allowed in cleartext
494 (forward-line)) ; signatures according to RFC2440.
495 (when (looking-at "[\t ]*$")
497 (delete-region (point-min) (point))
498 (if (re-search-forward mm-uu-pgp-beginning-signature nil t)
499 (delete-region (match-beginning 0) (point-max)))
500 (goto-char (point-min))
501 (while (re-search-forward "^- " nil t)
502 (replace-match "" t t)
504 (list (mm-make-handle buf mm-uu-text-plain-type))))
506 (defun mm-uu-pgp-signed-extract ()
507 (let ((mm-security-handle (list (format "multipart/signed"))))
508 (mm-set-handle-multipart-parameter
509 mm-security-handle 'protocol "application/x-gnus-pgp-signature")
511 (narrow-to-region start-point end-point)
512 (add-text-properties 0 (length (car mm-security-handle))
513 (list 'buffer (mm-uu-copy-to-buffer))
514 (car mm-security-handle))
515 (setcdr mm-security-handle
516 (mm-uu-pgp-signed-extract-1 nil
517 mm-security-handle)))
520 (defun mm-uu-pgp-encrypted-test (&rest rest)
523 (mml2015-clear-decrypt-function)
525 ((eq mm-decrypt-option 'never) nil)
526 ((eq mm-decrypt-option 'always) t)
527 ((eq mm-decrypt-option 'known) t)
529 (y-or-n-p "Decrypt pgp encrypted part? ")
532 (defun mm-uu-pgp-encrypted-extract-1 (handles ctl)
533 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max)))
536 ;; Make sure there's a blank line between header and body.
537 (with-current-buffer buf
538 (goto-char (point-min))
542 (looking-at "[^\t\n ]+:")
543 (looking-at "[^\t\n ]+:\\|[\t ]"))
545 (unless (memq (char-after) '(?\n nil))
548 (narrow-to-region (point-min) (point))
549 (setq charset (mail-fetch-field "charset")))
550 (if (and (mm-uu-pgp-encrypted-test)
552 (mml2015-clean-buffer)
553 (funcall (mml2015-clear-decrypt-function))
554 (equal (mm-handle-multipart-ctl-parameter mm-security-handle
560 (setq charset gnus-newsgroup-charset))
561 (setq charset (mm-charset-to-coding-system charset))
562 (not (eq charset 'ascii)))
563 ;; Assume that buffer's multibyteness is turned off.
564 ;; See `mml2015-pgg-clear-decrypt'.
565 (insert (mm-decode-coding-string (prog1
568 (mm-enable-multibyte))
570 (mm-enable-multibyte))
571 (list (mm-make-handle buf mm-uu-text-plain-type)))
572 (list (mm-make-handle buf '("application/pgp-encrypted")))))))
574 (defun mm-uu-pgp-encrypted-extract ()
575 (let ((mm-security-handle (list (format "multipart/encrypted"))))
576 (mm-set-handle-multipart-parameter
577 mm-security-handle 'protocol "application/x-gnus-pgp-encrypted")
579 (narrow-to-region start-point end-point)
580 (add-text-properties 0 (length (car mm-security-handle))
581 (list 'buffer (mm-uu-copy-to-buffer))
582 (car mm-security-handle))
583 (setcdr mm-security-handle
584 (mm-uu-pgp-encrypted-extract-1 nil
585 mm-security-handle)))
588 (defun mm-uu-gpg-key-skip-to-last ()
589 (let ((point (point))
590 (end-regexp (mm-uu-end-regexp entry))
591 (beginning-regexp (mm-uu-beginning-regexp entry)))
592 (when (and end-regexp
593 (not (mm-uu-configure-p (mm-uu-type entry) 'disabled)))
594 (while (re-search-forward end-regexp nil t)
595 (skip-chars-forward " \t\n\r")
596 (if (looking-at beginning-regexp)
597 (setq point (match-end 0)))))
600 (defun mm-uu-pgp-key-extract ()
601 (let ((buf (mm-uu-copy-to-buffer start-point end-point)))
603 '("application/pgp-keys"))))
606 (defun mm-uu-dissect (&optional noheader mime-type)
607 "Dissect the current buffer and return a list of uu handles.
608 The optional NOHEADER means there's no header in the buffer.
609 MIME-TYPE specifies a MIME type and parameters, which defaults to the
610 value of `mm-uu-text-plain-type'."
611 (let ((case-fold-search t)
612 (mm-uu-text-plain-type (or mime-type mm-uu-text-plain-type))
613 text-start start-point end-point file-name result entry func)
615 (goto-char (point-min))
620 ((search-forward "\n\n" nil t)
622 (t (goto-char (point-max))))
623 (setq text-start (point))
624 (while (re-search-forward mm-uu-beginning-regexp nil t)
625 (setq start-point (match-beginning 0)
627 (let ((alist mm-uu-type-alist)
628 (beginning-regexp (match-string 0)))
630 (if (string-match (mm-uu-beginning-regexp (car alist))
632 (setq entry (car alist))
634 (if (setq func (mm-uu-function-1 entry))
636 (forward-line);; in case of failure
637 (when (and (not (mm-uu-configure-p (mm-uu-type entry) 'disabled))
638 (let ((end-regexp (mm-uu-end-regexp entry)))
640 (or (setq end-point (point-max)) t)
642 (re-search-forward end-regexp nil t)
644 (setq end-point (point)))))
645 (or (not (setq func (mm-uu-function-2 entry)))
647 (if (and (> start-point text-start)
649 (goto-char text-start)
650 (re-search-forward "." start-point t)))
652 (mm-make-handle (mm-uu-copy-to-buffer text-start start-point)
653 mm-uu-text-plain-type)
656 (funcall (mm-uu-function-extract entry))
658 (goto-char (setq text-start end-point))))
660 (if (and (> (point-max) (1+ text-start))
662 (goto-char text-start)
663 (re-search-forward "." nil t)))
665 (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
666 mm-uu-text-plain-type)
668 (setq result (cons "multipart/mixed" (nreverse result))))
672 (defun mm-uu-dissect-text-parts (handle &optional decoded)
673 "Dissect text parts and put uu handles into HANDLE.
674 Assume text has been decoded if DECODED is non-nil."
675 (let ((buffer (mm-handle-buffer handle)))
676 (cond ((stringp buffer)
677 (dolist (elem (cdr handle))
678 (mm-uu-dissect-text-parts elem decoded)))
680 (let ((type (mm-handle-media-type handle))
681 (case-fold-search t) ;; string-match
682 children charset encoding)
685 ;; Mutt still uses application/pgp even though
686 ;; it has already been withdrawn.
687 (string-match "\\`text/\\|\\`application/pgp\\'" type)
690 (with-current-buffer buffer
693 (eq (setq charset (mail-content-type-get
694 (mm-handle-type handle)
699 t (cons type '((charset . gnus-decoded)))))
702 (mm-with-multibyte-buffer
703 (insert (mm-decode-string (mm-get-part handle)
706 t (cons type '((charset . gnus-decoded))))))
707 ((setq encoding (mm-handle-encoding handle))
709 ;; Inherit the multibyteness of the `buffer'.
711 (insert-buffer-substring buffer)
712 (mm-decode-content-transfer-encoding
714 (mm-uu-dissect t (list type))))
717 (mm-uu-dissect t (list type)))))))
718 ;; Ignore it if a given part is dissected into a single
719 ;; part of which the type is the same as the given one.
720 (if (and (<= (length children) 2)
721 (string-equal (mm-handle-media-type (cadr children))
723 (kill-buffer (mm-handle-buffer (cadr children)))
725 (setcdr handle (cdr children))
726 (setcar handle (car children)) ;; "multipart/mixed"
727 (dolist (elem (cdr children))
728 (mm-uu-dissect-text-parts elem decoded))))))
730 (dolist (elem handle)
731 (mm-uu-dissect-text-parts elem decoded))))))
735 ;; arch-tag: 7db076bf-53db-4320-aa19-ca76a1d2ab2c
736 ;;; mm-uu.el ends here