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 ;; slrn-style verbatim marks, see
169 ;; http://www.slrn.org/manual/slrn-manual-6.html#ss6.81
172 mm-uu-verbatim-marks-extract
180 "A list of specifications for non-MIME attachments.
181 Each element consist of the following entries: label,
182 start-regexp, end-regexp, extract-function, test-function.
184 After modifying this list you must run \\[mm-uu-configure].")
186 (defcustom mm-uu-configure-list '((shar . disabled))
187 "A list of mm-uu configuration.
188 To disable dissecting shar codes, for instance, add
189 `(shar . disabled)' to this list."
191 :options (mapcar (lambda (entry)
192 (list (car entry) '(const disabled)))
194 :group 'gnus-article-mime)
196 (defvar mm-uu-text-plain-type '("text/plain" (charset . gnus-decoded))
197 "MIME type and parameters for text/plain parts.
198 `gnus-decoded' is a fake charset, which means no further decoding.")
202 (defsubst mm-uu-type (entry)
205 (defsubst mm-uu-beginning-regexp (entry)
208 (defsubst mm-uu-end-regexp (entry)
211 (defsubst mm-uu-function-extract (entry)
214 (defsubst mm-uu-function-1 (entry)
217 (defsubst mm-uu-function-2 (entry)
220 (defface mm-uu-extract
223 (:background "gray5"))
226 (:background "gray95"))
229 "Face for extracted buffers."
230 ;; See `mm-uu-verbatim-marks-extract'.
231 :version "23.0" ;; No Gnus
232 :group 'gnus-article-mime)
234 (defun mm-uu-copy-to-buffer (&optional from to properties)
235 "Copy the contents of the current buffer to a fresh buffer.
238 If PROPERTIES is non-nil, PROPERTIES are applied to the buffer,
239 see `set-text-properties'. If PROPERTIES equals t, this means to
240 apply the face `mm-uu-extract'."
241 (let ((obuf (current-buffer))
243 ;; Might not exist in non-MULE XEmacs
244 (when (boundp 'buffer-file-coding-system)
245 buffer-file-coding-system)))
246 (with-current-buffer (generate-new-buffer " *mm-uu*")
247 (setq buffer-file-coding-system coding-system)
248 (insert-buffer-substring obuf from to)
249 (cond ((eq properties t)
250 (set-text-properties (point-min) (point-max)
251 '(face mm-uu-extract)))
253 (set-text-properties (point-min) (point-max) properties)))
256 (defun mm-uu-configure-p (key val)
257 (member (cons key val) mm-uu-configure-list))
259 (defun mm-uu-configure (&optional symbol value)
260 "Configure detection of non-MIME attachments."
262 (if symbol (set-default symbol value))
263 (setq mm-uu-beginning-regexp nil)
264 (mapcar (lambda (entry)
265 (if (mm-uu-configure-p (mm-uu-type entry) 'disabled)
267 (setq mm-uu-beginning-regexp
268 (concat mm-uu-beginning-regexp
269 (if mm-uu-beginning-regexp "\\|")
270 (mm-uu-beginning-regexp entry)))))
281 (defun mm-uu-uu-filename ()
282 (if (looking-at ".+")
284 (let ((nnheader-file-name-translation-alist
285 '((?/ . ?,) (?\ . ?_) (?* . ?_) (?$ . ?_))))
286 (nnheader-translate-file-chars (match-string 0))))))
288 (defun mm-uu-binhex-filename ()
291 (binhex-decode-region start-point end-point t))))
293 (defun mm-uu-yenc-filename ()
294 (goto-char start-point)
297 (yenc-extract-filename))))
299 (defun mm-uu-forward-test ()
301 (goto-char start-point)
303 (looking-at "[\r\n]*[a-zA-Z][a-zA-Z0-9-]*:")))
305 (defun mm-uu-postscript-extract ()
306 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
307 '("application/postscript")))
309 (defun mm-uu-verbatim-marks-extract ()
311 (mm-uu-copy-to-buffer
312 (progn (goto-char start-point) (forward-line) (point))
313 (progn (goto-char end-point) (forward-line -1) (point))
315 '("text/x-verbatim" (charset . gnus-decoded))))
317 (defun mm-uu-latex-extract ()
319 (mm-uu-copy-to-buffer start-point end-point t)
320 ;; application/x-tex?
321 '("text/x-verbatim" (charset . gnus-decoded))))
323 (defun mm-uu-emacs-sources-extract ()
324 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
325 '("application/emacs-lisp")
327 (list mm-dissect-disposition
328 (cons 'filename file-name))))
331 (defvar gnus-newsgroup-name))
333 (defun mm-uu-emacs-sources-test ()
334 (setq file-name (match-string 1))
335 (and gnus-newsgroup-name
336 mm-uu-emacs-sources-regexp
337 (string-match mm-uu-emacs-sources-regexp gnus-newsgroup-name)))
339 (defun mm-uu-diff-extract ()
340 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
343 (defun mm-uu-diff-test ()
344 (and gnus-newsgroup-name
345 mm-uu-diff-groups-regexp
346 (string-match mm-uu-diff-groups-regexp gnus-newsgroup-name)))
348 (defun mm-uu-latex-test ()
349 (and gnus-newsgroup-name
350 mm-uu-tex-groups-regexp
351 (string-match mm-uu-tex-groups-regexp gnus-newsgroup-name)))
353 (defun mm-uu-forward-extract ()
354 (mm-make-handle (mm-uu-copy-to-buffer
355 (progn (goto-char start-point) (forward-line) (point))
356 (progn (goto-char end-point) (forward-line -1) (point)))
357 '("message/rfc822" (charset . gnus-decoded))))
359 (defun mm-uu-uu-extract ()
360 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
361 (list (or (and file-name
362 (string-match "\\.[^\\.]+$"
364 (mailcap-extension-to-mime
365 (match-string 0 file-name)))
366 "application/octet-stream"))
368 (if (and file-name (not (equal file-name "")))
369 (list mm-dissect-disposition
370 (cons 'filename file-name)))))
372 (defun mm-uu-binhex-extract ()
373 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
374 (list (or (and file-name
375 (string-match "\\.[^\\.]+$" file-name)
376 (mailcap-extension-to-mime
377 (match-string 0 file-name)))
378 "application/octet-stream"))
380 (if (and file-name (not (equal file-name "")))
381 (list mm-dissect-disposition
382 (cons 'filename file-name)))))
384 (defun mm-uu-yenc-extract ()
385 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
386 (list (or (and file-name
387 (string-match "\\.[^\\.]+$" file-name)
388 (mailcap-extension-to-mime
389 (match-string 0 file-name)))
390 "application/octet-stream"))
392 (if (and file-name (not (equal file-name "")))
393 (list mm-dissect-disposition
394 (cons 'filename file-name)))))
397 (defun mm-uu-shar-extract ()
398 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
399 '("application/x-shar")))
401 (defun mm-uu-gnatsweb-extract ()
403 (goto-char start-point)
405 (narrow-to-region (point) end-point)
406 (mm-dissect-buffer t)))
408 (defun mm-uu-pgp-signed-test (&rest rest)
411 (mml2015-clear-verify-function)
413 ((eq mm-verify-option 'never) nil)
414 ((eq mm-verify-option 'always) t)
415 ((eq mm-verify-option 'known) t)
416 (t (y-or-n-p "Verify pgp signed part? ")))))
419 (defvar gnus-newsgroup-charset))
421 (defun mm-uu-pgp-signed-extract-1 (handles ctl)
422 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max))))
423 (with-current-buffer buf
424 (if (mm-uu-pgp-signed-test)
426 (mml2015-clean-buffer)
427 (let ((coding-system-for-write (or gnus-newsgroup-charset
429 (funcall (mml2015-clear-verify-function))))
430 (when (and mml2015-use (null (mml2015-clear-verify-function)))
431 (mm-set-handle-multipart-parameter
432 mm-security-handle 'gnus-details
433 (format "Clear verification not supported by `%s'.\n" mml2015-use))))
434 (goto-char (point-min))
435 (if (search-forward "\n\n" nil t)
436 (delete-region (point-min) (point)))
437 (if (re-search-forward mm-uu-pgp-beginning-signature nil t)
438 (delete-region (match-beginning 0) (point-max)))
439 (goto-char (point-min))
440 (while (re-search-forward "^- " nil t)
441 (replace-match "" t t)
443 (list (mm-make-handle buf mm-uu-text-plain-type))))
445 (defun mm-uu-pgp-signed-extract ()
446 (let ((mm-security-handle (list (format "multipart/signed"))))
447 (mm-set-handle-multipart-parameter
448 mm-security-handle 'protocol "application/x-gnus-pgp-signature")
450 (narrow-to-region start-point end-point)
451 (add-text-properties 0 (length (car mm-security-handle))
452 (list 'buffer (mm-uu-copy-to-buffer))
453 (car mm-security-handle))
454 (setcdr mm-security-handle
455 (mm-uu-pgp-signed-extract-1 nil
456 mm-security-handle)))
459 (defun mm-uu-pgp-encrypted-test (&rest rest)
462 (mml2015-clear-decrypt-function)
464 ((eq mm-decrypt-option 'never) nil)
465 ((eq mm-decrypt-option 'always) t)
466 ((eq mm-decrypt-option 'known) t)
467 (t (y-or-n-p "Decrypt pgp encrypted part? ")))))
469 (defun mm-uu-pgp-encrypted-extract-1 (handles ctl)
470 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max))))
471 (if (mm-uu-pgp-encrypted-test)
472 (with-current-buffer buf
473 (mml2015-clean-buffer)
474 (funcall (mml2015-clear-decrypt-function))))
475 (list (mm-make-handle buf mm-uu-text-plain-type))))
477 (defun mm-uu-pgp-encrypted-extract ()
478 (let ((mm-security-handle (list (format "multipart/encrypted"))))
479 (mm-set-handle-multipart-parameter
480 mm-security-handle 'protocol "application/x-gnus-pgp-encrypted")
482 (narrow-to-region start-point end-point)
483 (add-text-properties 0 (length (car mm-security-handle))
484 (list 'buffer (mm-uu-copy-to-buffer))
485 (car mm-security-handle))
486 (setcdr mm-security-handle
487 (mm-uu-pgp-encrypted-extract-1 nil
488 mm-security-handle)))
491 (defun mm-uu-gpg-key-skip-to-last ()
492 (let ((point (point))
493 (end-regexp (mm-uu-end-regexp entry))
494 (beginning-regexp (mm-uu-beginning-regexp entry)))
495 (when (and end-regexp
496 (not (mm-uu-configure-p (mm-uu-type entry) 'disabled)))
497 (while (re-search-forward end-regexp nil t)
498 (skip-chars-forward " \t\n\r")
499 (if (looking-at beginning-regexp)
500 (setq point (match-end 0)))))
503 (defun mm-uu-pgp-key-extract ()
504 (let ((buf (mm-uu-copy-to-buffer start-point end-point)))
506 '("application/pgp-keys"))))
509 (defun mm-uu-dissect (&optional noheader mime-type)
510 "Dissect the current buffer and return a list of uu handles.
511 The optional NOHEADER means there's no header in the buffer.
512 MIME-TYPE specifies a MIME type and parameters, which defaults to the
513 value of `mm-uu-text-plain-type'."
514 (let ((case-fold-search t)
515 (mm-uu-text-plain-type (or mime-type mm-uu-text-plain-type))
516 text-start start-point end-point file-name result entry func)
518 (goto-char (point-min))
523 ((search-forward "\n\n" nil t)
525 (t (goto-char (point-max))))
526 (setq text-start (point))
527 (while (re-search-forward mm-uu-beginning-regexp nil t)
528 (setq start-point (match-beginning 0))
529 (let ((alist mm-uu-type-alist)
530 (beginning-regexp (match-string 0)))
532 (if (string-match (mm-uu-beginning-regexp (car alist))
534 (setq entry (car alist))
536 (if (setq func (mm-uu-function-1 entry))
538 (forward-line);; in case of failure
539 (when (and (not (mm-uu-configure-p (mm-uu-type entry) 'disabled))
540 (let ((end-regexp (mm-uu-end-regexp entry)))
542 (or (setq end-point (point-max)) t)
544 (re-search-forward end-regexp nil t)
546 (setq end-point (point)))))
547 (or (not (setq func (mm-uu-function-2 entry)))
549 (if (and (> start-point text-start)
551 (goto-char text-start)
552 (re-search-forward "." start-point t)))
554 (mm-make-handle (mm-uu-copy-to-buffer text-start start-point)
555 mm-uu-text-plain-type)
558 (funcall (mm-uu-function-extract entry))
560 (goto-char (setq text-start end-point))))
562 (if (and (> (point-max) (1+ text-start))
564 (goto-char text-start)
565 (re-search-forward "." nil t)))
567 (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
568 mm-uu-text-plain-type)
570 (setq result (cons "multipart/mixed" (nreverse result))))
573 (defun mm-uu-dissect-text-parts (handle)
574 "Dissect text parts and put uu handles into HANDLE."
575 (let ((buffer (mm-handle-buffer handle))
577 (cond ((stringp buffer)
578 (mapc 'mm-uu-dissect-text-parts (cdr handle)))
580 (when (and (setq type (mm-handle-media-type handle))
582 (string-match "\\`text/" type)
583 (with-current-buffer buffer
585 (mm-uu-dissect t (mm-handle-type handle)))))
587 (setcar handle (car children))
588 (setcdr handle (cdr children))))
590 (mapc 'mm-uu-dissect-text-parts handle)))))
594 ;;; mm-uu.el ends here