1 ;;; mm-uu.el --- Return uu stuff as mm handles
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005 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))
34 (require 'gnus-mailcap)
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 -2))
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 1 -1))
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 (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."
206 :options (mapcar (lambda (entry)
207 (list (car entry) '(const disabled)))
209 :group 'gnus-article-mime)
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.")
217 (defsubst mm-uu-type (entry)
220 (defsubst mm-uu-beginning-regexp (entry)
223 (defsubst mm-uu-end-regexp (entry)
226 (defsubst mm-uu-function-extract (entry)
229 (defsubst mm-uu-function-1 (entry)
232 (defsubst mm-uu-function-2 (entry)
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))
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.0" ;; No Gnus
249 :group 'gnus-article-mime)
251 (defface mm-uu-extract '(;; Colors from `gnus-cite-3' plus background:
254 (:foreground "light yellow"
255 :background "dark green"))
258 (:foreground "dark green"
259 :background "light yellow"))
262 "Face for extracted buffers."
263 ;; See `mm-uu-verbatim-marks-extract'.
264 :version "23.0" ;; No Gnus
265 :group 'gnus-article-mime)
267 (defun mm-uu-copy-to-buffer (&optional from to properties)
268 "Copy the contents of the current buffer to a fresh buffer.
271 If PROPERTIES is non-nil, PROPERTIES are applied to the buffer,
272 see `set-text-properties'. If PROPERTIES equals t, this means to
273 apply the face `mm-uu-extract'."
274 (let ((obuf (current-buffer))
276 ;; Might not exist in non-MULE XEmacs
277 (when (boundp 'buffer-file-coding-system)
278 buffer-file-coding-system)))
279 (with-current-buffer (generate-new-buffer " *mm-uu*")
280 (setq buffer-file-coding-system coding-system)
281 (insert-buffer-substring obuf from to)
282 (cond ((eq properties t)
283 (set-text-properties (point-min) (point-max)
284 '(face mm-uu-extract)))
286 (set-text-properties (point-min) (point-max) properties)))
289 (defun mm-uu-configure-p (key val)
290 (member (cons key val) mm-uu-configure-list))
292 (defun mm-uu-configure (&optional symbol value)
293 "Configure detection of non-MIME attachments."
295 (if symbol (set-default symbol value))
296 (setq mm-uu-beginning-regexp nil)
297 (mapcar (lambda (entry)
298 (if (mm-uu-configure-p (mm-uu-type entry) 'disabled)
300 (setq mm-uu-beginning-regexp
301 (concat mm-uu-beginning-regexp
302 (if mm-uu-beginning-regexp "\\|")
303 (mm-uu-beginning-regexp entry)))))
314 (defun mm-uu-uu-filename ()
315 (if (looking-at ".+")
317 (let ((nnheader-file-name-translation-alist
318 '((?/ . ?,) (?\ . ?_) (?* . ?_) (?$ . ?_))))
319 (nnheader-translate-file-chars (match-string 0))))))
321 (defun mm-uu-binhex-filename ()
324 (binhex-decode-region start-point end-point t))))
326 (defun mm-uu-yenc-filename ()
327 (goto-char start-point)
330 (yenc-extract-filename))))
332 (defun mm-uu-forward-test ()
334 (goto-char start-point)
336 (looking-at "[\r\n]*[a-zA-Z][a-zA-Z0-9-]*:")))
338 (defun mm-uu-postscript-extract ()
339 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
340 '("application/postscript")))
342 (defun mm-uu-verbatim-marks-extract (start-offset end-offset
346 (let ((start (or (and mm-uu-hide-markers
350 (end (or (and mm-uu-hide-markers
355 (mm-uu-copy-to-buffer
356 (progn (goto-char start-point)
359 (progn (goto-char end-point)
363 '("text/x-verbatim" (charset . gnus-decoded)))))
365 (defun mm-uu-latex-extract ()
367 (mm-uu-copy-to-buffer start-point end-point t)
368 ;; application/x-tex?
369 '("text/x-verbatim" (charset . gnus-decoded))))
371 (defun mm-uu-emacs-sources-extract ()
372 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
373 '("application/emacs-lisp")
375 (list mm-dissect-disposition
376 (cons 'filename file-name))))
379 (defvar gnus-newsgroup-name))
381 (defun mm-uu-emacs-sources-test ()
382 (setq file-name (match-string 1))
383 (and gnus-newsgroup-name
384 mm-uu-emacs-sources-regexp
385 (string-match mm-uu-emacs-sources-regexp gnus-newsgroup-name)))
387 (defun mm-uu-diff-extract ()
388 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
391 (defun mm-uu-diff-test ()
392 (and gnus-newsgroup-name
393 mm-uu-diff-groups-regexp
394 (string-match mm-uu-diff-groups-regexp gnus-newsgroup-name)))
396 (defun mm-uu-latex-test ()
397 (and gnus-newsgroup-name
398 mm-uu-tex-groups-regexp
399 (string-match mm-uu-tex-groups-regexp gnus-newsgroup-name)))
401 (defun mm-uu-forward-extract ()
402 (mm-make-handle (mm-uu-copy-to-buffer
403 (progn (goto-char start-point) (forward-line) (point))
404 (progn (goto-char end-point) (forward-line -1) (point)))
405 '("message/rfc822" (charset . gnus-decoded))))
407 (defun mm-uu-uu-extract ()
408 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
409 (list (or (and file-name
410 (string-match "\\.[^\\.]+$"
412 (mailcap-extension-to-mime
413 (match-string 0 file-name)))
414 "application/octet-stream"))
416 (if (and file-name (not (equal file-name "")))
417 (list mm-dissect-disposition
418 (cons 'filename file-name)))))
420 (defun mm-uu-binhex-extract ()
421 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
422 (list (or (and file-name
423 (string-match "\\.[^\\.]+$" file-name)
424 (mailcap-extension-to-mime
425 (match-string 0 file-name)))
426 "application/octet-stream"))
428 (if (and file-name (not (equal file-name "")))
429 (list mm-dissect-disposition
430 (cons 'filename file-name)))))
432 (defun mm-uu-yenc-extract ()
433 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
434 (list (or (and file-name
435 (string-match "\\.[^\\.]+$" file-name)
436 (mailcap-extension-to-mime
437 (match-string 0 file-name)))
438 "application/octet-stream"))
440 (if (and file-name (not (equal file-name "")))
441 (list mm-dissect-disposition
442 (cons 'filename file-name)))))
445 (defun mm-uu-shar-extract ()
446 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
447 '("application/x-shar")))
449 (defun mm-uu-gnatsweb-extract ()
451 (goto-char start-point)
453 (narrow-to-region (point) end-point)
454 (mm-dissect-buffer t)))
456 (defun mm-uu-pgp-signed-test (&rest rest)
459 (mml2015-clear-verify-function)
461 ((eq mm-verify-option 'never) nil)
462 ((eq mm-verify-option 'always) t)
463 ((eq mm-verify-option 'known) t)
464 (t (y-or-n-p "Verify pgp signed part? ")))))
467 (defvar gnus-newsgroup-charset))
469 (defun mm-uu-pgp-signed-extract-1 (handles ctl)
470 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max))))
471 (with-current-buffer buf
472 (if (mm-uu-pgp-signed-test)
474 (mml2015-clean-buffer)
475 (let ((coding-system-for-write (or gnus-newsgroup-charset
477 (funcall (mml2015-clear-verify-function))))
478 (when (and mml2015-use (null (mml2015-clear-verify-function)))
479 (mm-set-handle-multipart-parameter
480 mm-security-handle 'gnus-details
481 (format "Clear verification not supported by `%s'.\n" mml2015-use))))
482 (goto-char (point-min))
483 (if (search-forward "\n\n" nil t)
484 (delete-region (point-min) (point)))
485 (if (re-search-forward mm-uu-pgp-beginning-signature nil t)
486 (delete-region (match-beginning 0) (point-max)))
487 (goto-char (point-min))
488 (while (re-search-forward "^- " nil t)
489 (replace-match "" t t)
491 (list (mm-make-handle buf mm-uu-text-plain-type))))
493 (defun mm-uu-pgp-signed-extract ()
494 (let ((mm-security-handle (list (format "multipart/signed"))))
495 (mm-set-handle-multipart-parameter
496 mm-security-handle 'protocol "application/x-gnus-pgp-signature")
498 (narrow-to-region start-point end-point)
499 (add-text-properties 0 (length (car mm-security-handle))
500 (list 'buffer (mm-uu-copy-to-buffer))
501 (car mm-security-handle))
502 (setcdr mm-security-handle
503 (mm-uu-pgp-signed-extract-1 nil
504 mm-security-handle)))
507 (defun mm-uu-pgp-encrypted-test (&rest rest)
510 (mml2015-clear-decrypt-function)
512 ((eq mm-decrypt-option 'never) nil)
513 ((eq mm-decrypt-option 'always) t)
514 ((eq mm-decrypt-option 'known) t)
515 (t (y-or-n-p "Decrypt pgp encrypted part? ")))))
517 (defun mm-uu-pgp-encrypted-extract-1 (handles ctl)
518 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max))))
519 (if (mm-uu-pgp-encrypted-test)
520 (with-current-buffer buf
521 (mml2015-clean-buffer)
522 (funcall (mml2015-clear-decrypt-function))))
523 (list (mm-make-handle buf mm-uu-text-plain-type))))
525 (defun mm-uu-pgp-encrypted-extract ()
526 (let ((mm-security-handle (list (format "multipart/encrypted"))))
527 (mm-set-handle-multipart-parameter
528 mm-security-handle 'protocol "application/x-gnus-pgp-encrypted")
530 (narrow-to-region start-point end-point)
531 (add-text-properties 0 (length (car mm-security-handle))
532 (list 'buffer (mm-uu-copy-to-buffer))
533 (car mm-security-handle))
534 (setcdr mm-security-handle
535 (mm-uu-pgp-encrypted-extract-1 nil
536 mm-security-handle)))
539 (defun mm-uu-gpg-key-skip-to-last ()
540 (let ((point (point))
541 (end-regexp (mm-uu-end-regexp entry))
542 (beginning-regexp (mm-uu-beginning-regexp entry)))
543 (when (and end-regexp
544 (not (mm-uu-configure-p (mm-uu-type entry) 'disabled)))
545 (while (re-search-forward end-regexp nil t)
546 (skip-chars-forward " \t\n\r")
547 (if (looking-at beginning-regexp)
548 (setq point (match-end 0)))))
551 (defun mm-uu-pgp-key-extract ()
552 (let ((buf (mm-uu-copy-to-buffer start-point end-point)))
554 '("application/pgp-keys"))))
557 (defun mm-uu-dissect (&optional noheader mime-type)
558 "Dissect the current buffer and return a list of uu handles.
559 The optional NOHEADER means there's no header in the buffer.
560 MIME-TYPE specifies a MIME type and parameters, which defaults to the
561 value of `mm-uu-text-plain-type'."
562 (let ((case-fold-search t)
563 (mm-uu-text-plain-type (or mime-type mm-uu-text-plain-type))
564 text-start start-point end-point file-name result entry func)
566 (goto-char (point-min))
571 ((search-forward "\n\n" nil t)
573 (t (goto-char (point-max))))
574 (setq text-start (point))
575 (while (re-search-forward mm-uu-beginning-regexp nil t)
576 (setq start-point (match-beginning 0))
577 (let ((alist mm-uu-type-alist)
578 (beginning-regexp (match-string 0)))
580 (if (string-match (mm-uu-beginning-regexp (car alist))
582 (setq entry (car alist))
584 (if (setq func (mm-uu-function-1 entry))
586 (forward-line);; in case of failure
587 (when (and (not (mm-uu-configure-p (mm-uu-type entry) 'disabled))
588 (let ((end-regexp (mm-uu-end-regexp entry)))
590 (or (setq end-point (point-max)) t)
592 (re-search-forward end-regexp nil t)
594 (setq end-point (point)))))
595 (or (not (setq func (mm-uu-function-2 entry)))
597 (if (and (> start-point text-start)
599 (goto-char text-start)
600 (re-search-forward "." start-point t)))
602 (mm-make-handle (mm-uu-copy-to-buffer text-start start-point)
603 mm-uu-text-plain-type)
606 (funcall (mm-uu-function-extract entry))
608 (goto-char (setq text-start end-point))))
610 (if (and (> (point-max) (1+ text-start))
612 (goto-char text-start)
613 (re-search-forward "." nil t)))
615 (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
616 mm-uu-text-plain-type)
618 (setq result (cons "multipart/mixed" (nreverse result))))
621 (defun mm-uu-dissect-text-parts (handle)
622 "Dissect text parts and put uu handles into HANDLE."
623 (let ((buffer (mm-handle-buffer handle))
625 (cond ((stringp buffer)
626 (mapc 'mm-uu-dissect-text-parts (cdr handle)))
628 (when (and (setq type (mm-handle-media-type handle))
630 (string-match "\\`text/" type)
631 (with-current-buffer buffer
633 (mm-uu-dissect t (mm-handle-type handle)))))
635 (setcar handle (car children))
636 (setcdr handle (cdr children))))
638 (mapc 'mm-uu-dissect-text-parts handle)))))
642 ;;; mm-uu.el ends here