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
106 "^begin[ \t]+0?[0-7][0-7][0-7][ \t]+"
111 "^:...............................................................$"
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 ,(concat "^" (regexp-quote message-mark-insert-begin))
169 ,(concat "^" (regexp-quote message-mark-insert-end))
170 (lambda () (mm-uu-verbatim-marks-extract 0 -1 1 -2))
172 ;; Omitting [a-z8<] leads to false positives (bogus signature separators
173 ;; and mailing list banners).
175 "^ *\\(-\\|_\\)\\{30,\\}.*[a-z8<].*\\(-\\|_\\)\\{30,\\} *$"
176 "^ *\\(-\\|_\\)\\{30,\\}.*[a-z8<].*\\(-\\|_\\)\\{30,\\} *$"
177 (lambda () (mm-uu-verbatim-marks-extract 0 0 1 -1))
180 ;; slrn-style verbatim marks, see
181 ;; http://www.slrn.org/manual/slrn-manual-6.html#ss6.81
184 (lambda () (mm-uu-verbatim-marks-extract 0 0 1 -1))
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.
196 After modifying this list you must run \\[mm-uu-configure].")
198 (defcustom mm-uu-configure-list '((shar . disabled))
199 "A list of mm-uu configuration.
200 To disable dissecting shar codes, for instance, add
201 `(shar . disabled)' to this list."
203 :options (mapcar (lambda (entry)
204 (list (car entry) '(const disabled)))
206 :group 'gnus-article-mime)
208 (defvar mm-uu-text-plain-type '("text/plain" (charset . gnus-decoded))
209 "MIME type and parameters for text/plain parts.
210 `gnus-decoded' is a fake charset, which means no further decoding.")
214 (defsubst mm-uu-type (entry)
217 (defsubst mm-uu-beginning-regexp (entry)
220 (defsubst mm-uu-end-regexp (entry)
223 (defsubst mm-uu-function-extract (entry)
226 (defsubst mm-uu-function-1 (entry)
229 (defsubst mm-uu-function-2 (entry)
232 ;; In Emacs 22, we could use `min-colors' in the face definition. But Emacs
233 ;; 21 and XEmacs don't support it.
234 (defcustom mm-uu-hide-markers
235 (< 16 (or (and (fboundp 'defined-colors)
236 (length (defined-colors)))
237 (and (fboundp (device-color-cells))
238 (length (device-color-cells)))
240 "If non-nil, hide verbatim markers.
241 The value should be nil on displays where the face
242 `mm-uu-extract' isn't distinguishable to the face `default'."
243 :type '(choice (const :tag "Hide" t)
244 (const :tag "Don't hide" nil))
245 :version "23.0" ;; No Gnus
246 :group 'gnus-article-mime)
248 (defface mm-uu-extract '(;; Colors from `gnus-cite-3' plus background:
251 (:foreground "light yellow"
252 :background "dark green"))
255 (:foreground "dark green"
256 :background "light yellow"))
259 "Face for extracted buffers."
260 ;; See `mm-uu-verbatim-marks-extract'.
261 :version "23.0" ;; No Gnus
262 :group 'gnus-article-mime)
264 (defun mm-uu-copy-to-buffer (&optional from to properties)
265 "Copy the contents of the current buffer to a fresh buffer.
268 If PROPERTIES is non-nil, PROPERTIES are applied to the buffer,
269 see `set-text-properties'. If PROPERTIES equals t, this means to
270 apply the face `mm-uu-extract'."
271 (let ((obuf (current-buffer))
273 ;; Might not exist in non-MULE XEmacs
274 (when (boundp 'buffer-file-coding-system)
275 buffer-file-coding-system)))
276 (with-current-buffer (generate-new-buffer " *mm-uu*")
277 (setq buffer-file-coding-system coding-system)
278 (insert-buffer-substring obuf from to)
279 (cond ((eq properties t)
280 (set-text-properties (point-min) (point-max)
281 '(face mm-uu-extract)))
283 (set-text-properties (point-min) (point-max) properties)))
286 (defun mm-uu-configure-p (key val)
287 (member (cons key val) mm-uu-configure-list))
289 (defun mm-uu-configure (&optional symbol value)
290 "Configure detection of non-MIME attachments."
292 (if symbol (set-default symbol value))
293 (setq mm-uu-beginning-regexp nil)
294 (mapcar (lambda (entry)
295 (if (mm-uu-configure-p (mm-uu-type entry) 'disabled)
297 (setq mm-uu-beginning-regexp
298 (concat mm-uu-beginning-regexp
299 (if mm-uu-beginning-regexp "\\|")
300 (mm-uu-beginning-regexp entry)))))
311 (defun mm-uu-uu-filename ()
312 (if (looking-at ".+")
314 (let ((nnheader-file-name-translation-alist
315 '((?/ . ?,) (?\ . ?_) (?* . ?_) (?$ . ?_))))
316 (nnheader-translate-file-chars (match-string 0))))))
318 (defun mm-uu-binhex-filename ()
321 (binhex-decode-region start-point end-point t))))
323 (defun mm-uu-yenc-filename ()
324 (goto-char start-point)
327 (yenc-extract-filename))))
329 (defun mm-uu-forward-test ()
331 (goto-char start-point)
333 (looking-at "[\r\n]*[a-zA-Z][a-zA-Z0-9-]*:")))
335 (defun mm-uu-postscript-extract ()
336 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
337 '("application/postscript")))
339 (defun mm-uu-verbatim-marks-extract (start-offset end-offset
343 (let ((start (or (and mm-uu-hide-markers
347 (end (or (and mm-uu-hide-markers
352 (mm-uu-copy-to-buffer
353 (progn (goto-char start-point)
356 (progn (goto-char end-point)
360 '("text/x-verbatim" (charset . gnus-decoded)))))
362 (defun mm-uu-latex-extract ()
364 (mm-uu-copy-to-buffer start-point end-point t)
365 ;; application/x-tex?
366 '("text/x-verbatim" (charset . gnus-decoded))))
368 (defun mm-uu-emacs-sources-extract ()
369 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
370 '("application/emacs-lisp")
372 (list mm-dissect-disposition
373 (cons 'filename file-name))))
376 (defvar gnus-newsgroup-name))
378 (defun mm-uu-emacs-sources-test ()
379 (setq file-name (match-string 1))
380 (and gnus-newsgroup-name
381 mm-uu-emacs-sources-regexp
382 (string-match mm-uu-emacs-sources-regexp gnus-newsgroup-name)))
384 (defun mm-uu-diff-extract ()
385 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
388 (defun mm-uu-diff-test ()
389 (and gnus-newsgroup-name
390 mm-uu-diff-groups-regexp
391 (string-match mm-uu-diff-groups-regexp gnus-newsgroup-name)))
393 (defun mm-uu-latex-test ()
394 (and gnus-newsgroup-name
395 mm-uu-tex-groups-regexp
396 (string-match mm-uu-tex-groups-regexp gnus-newsgroup-name)))
398 (defun mm-uu-forward-extract ()
399 (mm-make-handle (mm-uu-copy-to-buffer
400 (progn (goto-char start-point) (forward-line) (point))
401 (progn (goto-char end-point) (forward-line -1) (point)))
402 '("message/rfc822" (charset . gnus-decoded))))
404 (defun mm-uu-uu-extract ()
405 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
406 (list (or (and file-name
407 (string-match "\\.[^\\.]+$"
409 (mailcap-extension-to-mime
410 (match-string 0 file-name)))
411 "application/octet-stream"))
413 (if (and file-name (not (equal file-name "")))
414 (list mm-dissect-disposition
415 (cons 'filename file-name)))))
417 (defun mm-uu-binhex-extract ()
418 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
419 (list (or (and file-name
420 (string-match "\\.[^\\.]+$" file-name)
421 (mailcap-extension-to-mime
422 (match-string 0 file-name)))
423 "application/octet-stream"))
425 (if (and file-name (not (equal file-name "")))
426 (list mm-dissect-disposition
427 (cons 'filename file-name)))))
429 (defun mm-uu-yenc-extract ()
430 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
431 (list (or (and file-name
432 (string-match "\\.[^\\.]+$" file-name)
433 (mailcap-extension-to-mime
434 (match-string 0 file-name)))
435 "application/octet-stream"))
437 (if (and file-name (not (equal file-name "")))
438 (list mm-dissect-disposition
439 (cons 'filename file-name)))))
442 (defun mm-uu-shar-extract ()
443 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
444 '("application/x-shar")))
446 (defun mm-uu-gnatsweb-extract ()
448 (goto-char start-point)
450 (narrow-to-region (point) end-point)
451 (mm-dissect-buffer t)))
453 (defun mm-uu-pgp-signed-test (&rest rest)
456 (mml2015-clear-verify-function)
458 ((eq mm-verify-option 'never) nil)
459 ((eq mm-verify-option 'always) t)
460 ((eq mm-verify-option 'known) t)
461 (t (y-or-n-p "Verify pgp signed part? ")))))
464 (defvar gnus-newsgroup-charset))
466 (defun mm-uu-pgp-signed-extract-1 (handles ctl)
467 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max))))
468 (with-current-buffer buf
469 (if (mm-uu-pgp-signed-test)
471 (mml2015-clean-buffer)
472 (let ((coding-system-for-write (or gnus-newsgroup-charset
474 (funcall (mml2015-clear-verify-function))))
475 (when (and mml2015-use (null (mml2015-clear-verify-function)))
476 (mm-set-handle-multipart-parameter
477 mm-security-handle 'gnus-details
478 (format "Clear verification not supported by `%s'.\n" mml2015-use))))
479 (goto-char (point-min))
480 (if (search-forward "\n\n" nil t)
481 (delete-region (point-min) (point)))
482 (if (re-search-forward mm-uu-pgp-beginning-signature nil t)
483 (delete-region (match-beginning 0) (point-max)))
484 (goto-char (point-min))
485 (while (re-search-forward "^- " nil t)
486 (replace-match "" t t)
488 (list (mm-make-handle buf mm-uu-text-plain-type))))
490 (defun mm-uu-pgp-signed-extract ()
491 (let ((mm-security-handle (list (format "multipart/signed"))))
492 (mm-set-handle-multipart-parameter
493 mm-security-handle 'protocol "application/x-gnus-pgp-signature")
495 (narrow-to-region start-point end-point)
496 (add-text-properties 0 (length (car mm-security-handle))
497 (list 'buffer (mm-uu-copy-to-buffer))
498 (car mm-security-handle))
499 (setcdr mm-security-handle
500 (mm-uu-pgp-signed-extract-1 nil
501 mm-security-handle)))
504 (defun mm-uu-pgp-encrypted-test (&rest rest)
507 (mml2015-clear-decrypt-function)
509 ((eq mm-decrypt-option 'never) nil)
510 ((eq mm-decrypt-option 'always) t)
511 ((eq mm-decrypt-option 'known) t)
512 (t (y-or-n-p "Decrypt pgp encrypted part? ")))))
514 (defun mm-uu-pgp-encrypted-extract-1 (handles ctl)
515 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max))))
516 (if (mm-uu-pgp-encrypted-test)
517 (with-current-buffer buf
518 (mml2015-clean-buffer)
519 (funcall (mml2015-clear-decrypt-function))))
520 (list (mm-make-handle buf mm-uu-text-plain-type))))
522 (defun mm-uu-pgp-encrypted-extract ()
523 (let ((mm-security-handle (list (format "multipart/encrypted"))))
524 (mm-set-handle-multipart-parameter
525 mm-security-handle 'protocol "application/x-gnus-pgp-encrypted")
527 (narrow-to-region start-point end-point)
528 (add-text-properties 0 (length (car mm-security-handle))
529 (list 'buffer (mm-uu-copy-to-buffer))
530 (car mm-security-handle))
531 (setcdr mm-security-handle
532 (mm-uu-pgp-encrypted-extract-1 nil
533 mm-security-handle)))
536 (defun mm-uu-gpg-key-skip-to-last ()
537 (let ((point (point))
538 (end-regexp (mm-uu-end-regexp entry))
539 (beginning-regexp (mm-uu-beginning-regexp entry)))
540 (when (and end-regexp
541 (not (mm-uu-configure-p (mm-uu-type entry) 'disabled)))
542 (while (re-search-forward end-regexp nil t)
543 (skip-chars-forward " \t\n\r")
544 (if (looking-at beginning-regexp)
545 (setq point (match-end 0)))))
548 (defun mm-uu-pgp-key-extract ()
549 (let ((buf (mm-uu-copy-to-buffer start-point end-point)))
551 '("application/pgp-keys"))))
554 (defun mm-uu-dissect (&optional noheader mime-type)
555 "Dissect the current buffer and return a list of uu handles.
556 The optional NOHEADER means there's no header in the buffer.
557 MIME-TYPE specifies a MIME type and parameters, which defaults to the
558 value of `mm-uu-text-plain-type'."
559 (let ((case-fold-search t)
560 (mm-uu-text-plain-type (or mime-type mm-uu-text-plain-type))
561 text-start start-point end-point file-name result entry func)
563 (goto-char (point-min))
568 ((search-forward "\n\n" nil t)
570 (t (goto-char (point-max))))
571 (setq text-start (point))
572 (while (re-search-forward mm-uu-beginning-regexp nil t)
573 (setq start-point (match-beginning 0))
574 (let ((alist mm-uu-type-alist)
575 (beginning-regexp (match-string 0)))
577 (if (string-match (mm-uu-beginning-regexp (car alist))
579 (setq entry (car alist))
581 (if (setq func (mm-uu-function-1 entry))
583 (forward-line);; in case of failure
584 (when (and (not (mm-uu-configure-p (mm-uu-type entry) 'disabled))
585 (let ((end-regexp (mm-uu-end-regexp entry)))
587 (or (setq end-point (point-max)) t)
589 (re-search-forward end-regexp nil t)
591 (setq end-point (point)))))
592 (or (not (setq func (mm-uu-function-2 entry)))
594 (if (and (> start-point text-start)
596 (goto-char text-start)
597 (re-search-forward "." start-point t)))
599 (mm-make-handle (mm-uu-copy-to-buffer text-start start-point)
600 mm-uu-text-plain-type)
603 (funcall (mm-uu-function-extract entry))
605 (goto-char (setq text-start end-point))))
607 (if (and (> (point-max) (1+ text-start))
609 (goto-char text-start)
610 (re-search-forward "." nil t)))
612 (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
613 mm-uu-text-plain-type)
615 (setq result (cons "multipart/mixed" (nreverse result))))
618 (defun mm-uu-dissect-text-parts (handle)
619 "Dissect text parts and put uu handles into HANDLE."
620 (let ((buffer (mm-handle-buffer handle))
622 (cond ((stringp buffer)
623 (mapc 'mm-uu-dissect-text-parts (cdr handle)))
625 (when (and (setq type (mm-handle-media-type handle))
627 (string-match "\\`text/" type)
628 (with-current-buffer buffer
630 (mm-uu-dissect t (mm-handle-type handle)))))
632 (setcar handle (car children))
633 (setcdr handle (cdr children))))
635 (mapc 'mm-uu-dissect-text-parts handle)))))
639 ;;; mm-uu.el ends here