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 (defvar mm-uu-emacs-sources-regexp "gnu\\.emacs\\.sources"
81 "The regexp of Emacs sources groups.")
83 (defcustom mm-uu-diff-groups-regexp "gnus\\.commits"
84 "*Regexp matching diff groups."
87 :group 'gnus-article-mime)
89 (defvar mm-uu-type-alist
93 mm-uu-postscript-extract
96 "^begin[ \t]+0?[0-7][0-7][0-7][ \t]+"
101 "^:...............................................................$"
105 mm-uu-binhex-filename)
107 "^=ybegin.*size=[0-9]+.*name=.*$"
108 "^=yend.*size=[0-9]+"
116 ;; Thanks to Edward J. Sabol <sabol@alderaan.gsfc.nasa.gov> and
117 ;; Peter von der Ah\'e <pahe@daimi.au.dk>
118 "^-+ \\(Start of \\)?Forwarded message"
119 "^-+ End \\(of \\)?forwarded message"
120 mm-uu-forward-extract
124 "^----gnatsweb-attachment----"
126 mm-uu-gnatsweb-extract)
128 "^-----BEGIN PGP SIGNED MESSAGE-----"
129 "^-----END PGP SIGNATURE-----"
130 mm-uu-pgp-signed-extract
134 "^-----BEGIN PGP MESSAGE-----"
135 "^-----END PGP MESSAGE-----"
136 mm-uu-pgp-encrypted-extract
140 "^-----BEGIN PGP PUBLIC KEY BLOCK-----"
141 "^-----END PGP PUBLIC KEY BLOCK-----"
142 mm-uu-pgp-key-extract
143 mm-uu-gpg-key-skip-to-last
146 "^;;;?[ \t]*[^ \t]+\\.el[ \t]*--"
147 "^;;;?[ \t]*\\([^ \t]+\\.el\\)[ \t]+ends here"
148 mm-uu-emacs-sources-extract
150 mm-uu-emacs-sources-test)
158 (defcustom mm-uu-configure-list '((shar . disabled))
159 "A list of mm-uu configuration.
160 To disable dissecting shar codes, for instance, add
161 `(shar . disabled)' to this list."
163 :options (mapcar (lambda (entry)
164 (list (car entry) '(const disabled)))
166 :group 'gnus-article-mime)
168 (defvar mm-uu-text-plain-type '("text/plain" (charset . gnus-decoded))
169 "MIME type and parameters for text/plain parts.
170 `gnus-decoded' is a fake charset, which means no further decoding.")
174 (defsubst mm-uu-type (entry)
177 (defsubst mm-uu-beginning-regexp (entry)
180 (defsubst mm-uu-end-regexp (entry)
183 (defsubst mm-uu-function-extract (entry)
186 (defsubst mm-uu-function-1 (entry)
189 (defsubst mm-uu-function-2 (entry)
192 (defun mm-uu-copy-to-buffer (&optional from to)
193 "Copy the contents of the current buffer to a fresh buffer.
195 (let ((obuf (current-buffer))
197 ;; Might not exist in non-MULE XEmacs
198 (when (boundp 'buffer-file-coding-system)
199 buffer-file-coding-system)))
200 (with-current-buffer (generate-new-buffer " *mm-uu*")
201 (setq buffer-file-coding-system coding-system)
202 (insert-buffer-substring obuf from to)
205 (defun mm-uu-configure-p (key val)
206 (member (cons key val) mm-uu-configure-list))
208 (defun mm-uu-configure (&optional symbol value)
209 (if symbol (set-default symbol value))
210 (setq mm-uu-beginning-regexp nil)
211 (mapcar (lambda (entry)
212 (if (mm-uu-configure-p (mm-uu-type entry) 'disabled)
214 (setq mm-uu-beginning-regexp
215 (concat mm-uu-beginning-regexp
216 (if mm-uu-beginning-regexp "\\|")
217 (mm-uu-beginning-regexp entry)))))
228 (defun mm-uu-uu-filename ()
229 (if (looking-at ".+")
231 (let ((nnheader-file-name-translation-alist
232 '((?/ . ?,) (?\ . ?_) (?* . ?_) (?$ . ?_))))
233 (nnheader-translate-file-chars (match-string 0))))))
235 (defun mm-uu-binhex-filename ()
238 (binhex-decode-region start-point end-point t))))
240 (defun mm-uu-yenc-filename ()
241 (goto-char start-point)
244 (yenc-extract-filename))))
246 (defun mm-uu-forward-test ()
248 (goto-char start-point)
250 (looking-at "[\r\n]*[a-zA-Z][a-zA-Z0-9-]*:")))
252 (defun mm-uu-postscript-extract ()
253 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
254 '("application/postscript")))
256 (defun mm-uu-emacs-sources-extract ()
257 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
258 '("application/emacs-lisp")
260 (list mm-dissect-disposition
261 (cons 'filename file-name))))
264 (defvar gnus-newsgroup-name))
266 (defun mm-uu-emacs-sources-test ()
267 (setq file-name (match-string 1))
268 (and gnus-newsgroup-name
269 mm-uu-emacs-sources-regexp
270 (string-match mm-uu-emacs-sources-regexp gnus-newsgroup-name)))
272 (defun mm-uu-diff-extract ()
273 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
276 (defun mm-uu-diff-test ()
277 (and gnus-newsgroup-name
278 mm-uu-diff-groups-regexp
279 (string-match mm-uu-diff-groups-regexp gnus-newsgroup-name)))
281 (defun mm-uu-forward-extract ()
282 (mm-make-handle (mm-uu-copy-to-buffer
283 (progn (goto-char start-point) (forward-line) (point))
284 (progn (goto-char end-point) (forward-line -1) (point)))
285 '("message/rfc822" (charset . gnus-decoded))))
287 (defun mm-uu-uu-extract ()
288 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
289 (list (or (and file-name
290 (string-match "\\.[^\\.]+$"
292 (mailcap-extension-to-mime
293 (match-string 0 file-name)))
294 "application/octet-stream"))
296 (if (and file-name (not (equal file-name "")))
297 (list mm-dissect-disposition
298 (cons 'filename file-name)))))
300 (defun mm-uu-binhex-extract ()
301 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
302 (list (or (and file-name
303 (string-match "\\.[^\\.]+$" file-name)
304 (mailcap-extension-to-mime
305 (match-string 0 file-name)))
306 "application/octet-stream"))
308 (if (and file-name (not (equal file-name "")))
309 (list mm-dissect-disposition
310 (cons 'filename file-name)))))
312 (defun mm-uu-yenc-extract ()
313 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
314 (list (or (and file-name
315 (string-match "\\.[^\\.]+$" file-name)
316 (mailcap-extension-to-mime
317 (match-string 0 file-name)))
318 "application/octet-stream"))
320 (if (and file-name (not (equal file-name "")))
321 (list mm-dissect-disposition
322 (cons 'filename file-name)))))
325 (defun mm-uu-shar-extract ()
326 (mm-make-handle (mm-uu-copy-to-buffer start-point end-point)
327 '("application/x-shar")))
329 (defun mm-uu-gnatsweb-extract ()
331 (goto-char start-point)
333 (narrow-to-region (point) end-point)
334 (mm-dissect-buffer t)))
336 (defun mm-uu-pgp-signed-test (&rest rest)
339 (mml2015-clear-verify-function)
341 ((eq mm-verify-option 'never) nil)
342 ((eq mm-verify-option 'always) t)
343 ((eq mm-verify-option 'known) t)
344 (t (y-or-n-p "Verify pgp signed part? ")))))
347 (defvar gnus-newsgroup-charset))
349 (defun mm-uu-pgp-signed-extract-1 (handles ctl)
350 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max))))
351 (with-current-buffer buf
352 (if (mm-uu-pgp-signed-test)
354 (mml2015-clean-buffer)
355 (let ((coding-system-for-write (or gnus-newsgroup-charset
357 (funcall (mml2015-clear-verify-function))))
358 (when (and mml2015-use (null (mml2015-clear-verify-function)))
359 (mm-set-handle-multipart-parameter
360 mm-security-handle 'gnus-details
361 (format "Clear verification not supported by `%s'.\n" mml2015-use))))
362 (goto-char (point-min))
363 (if (search-forward "\n\n" nil t)
364 (delete-region (point-min) (point)))
365 (if (re-search-forward mm-uu-pgp-beginning-signature nil t)
366 (delete-region (match-beginning 0) (point-max)))
367 (goto-char (point-min))
368 (while (re-search-forward "^- " nil t)
369 (replace-match "" t t)
371 (list (mm-make-handle buf mm-uu-text-plain-type))))
373 (defun mm-uu-pgp-signed-extract ()
374 (let ((mm-security-handle (list (format "multipart/signed"))))
375 (mm-set-handle-multipart-parameter
376 mm-security-handle 'protocol "application/x-gnus-pgp-signature")
378 (narrow-to-region start-point end-point)
379 (add-text-properties 0 (length (car mm-security-handle))
380 (list 'buffer (mm-uu-copy-to-buffer))
381 (car mm-security-handle))
382 (setcdr mm-security-handle
383 (mm-uu-pgp-signed-extract-1 nil
384 mm-security-handle)))
387 (defun mm-uu-pgp-encrypted-test (&rest rest)
390 (mml2015-clear-decrypt-function)
392 ((eq mm-decrypt-option 'never) nil)
393 ((eq mm-decrypt-option 'always) t)
394 ((eq mm-decrypt-option 'known) t)
395 (t (y-or-n-p "Decrypt pgp encrypted part? ")))))
397 (defun mm-uu-pgp-encrypted-extract-1 (handles ctl)
398 (let ((buf (mm-uu-copy-to-buffer (point-min) (point-max))))
399 (if (mm-uu-pgp-encrypted-test)
400 (with-current-buffer buf
401 (mml2015-clean-buffer)
402 (funcall (mml2015-clear-decrypt-function))))
403 (list (mm-make-handle buf mm-uu-text-plain-type))))
405 (defun mm-uu-pgp-encrypted-extract ()
406 (let ((mm-security-handle (list (format "multipart/encrypted"))))
407 (mm-set-handle-multipart-parameter
408 mm-security-handle 'protocol "application/x-gnus-pgp-encrypted")
410 (narrow-to-region start-point end-point)
411 (add-text-properties 0 (length (car mm-security-handle))
412 (list 'buffer (mm-uu-copy-to-buffer))
413 (car mm-security-handle))
414 (setcdr mm-security-handle
415 (mm-uu-pgp-encrypted-extract-1 nil
416 mm-security-handle)))
419 (defun mm-uu-gpg-key-skip-to-last ()
420 (let ((point (point))
421 (end-regexp (mm-uu-end-regexp entry))
422 (beginning-regexp (mm-uu-beginning-regexp entry)))
423 (when (and end-regexp
424 (not (mm-uu-configure-p (mm-uu-type entry) 'disabled)))
425 (while (re-search-forward end-regexp nil t)
426 (skip-chars-forward " \t\n\r")
427 (if (looking-at beginning-regexp)
428 (setq point (match-end 0)))))
431 (defun mm-uu-pgp-key-extract ()
432 (let ((buf (mm-uu-copy-to-buffer start-point end-point)))
434 '("application/pgp-keys"))))
437 (defun mm-uu-dissect (&optional noheader mime-type)
438 "Dissect the current buffer and return a list of uu handles.
439 The optional NOHEADER means there's no header in the buffer.
440 MIME-TYPE specifies a MIME type and parameters, which defaults to the
441 value of `mm-uu-text-plain-type'."
442 (let ((case-fold-search t)
443 (mm-uu-text-plain-type (or mime-type mm-uu-text-plain-type))
444 text-start start-point end-point file-name result entry func)
446 (goto-char (point-min))
451 ((search-forward "\n\n" nil t)
453 (t (goto-char (point-max))))
454 (setq text-start (point))
455 (while (re-search-forward mm-uu-beginning-regexp nil t)
456 (setq start-point (match-beginning 0))
457 (let ((alist mm-uu-type-alist)
458 (beginning-regexp (match-string 0)))
460 (if (string-match (mm-uu-beginning-regexp (car alist))
462 (setq entry (car alist))
464 (if (setq func (mm-uu-function-1 entry))
466 (forward-line);; in case of failure
467 (when (and (not (mm-uu-configure-p (mm-uu-type entry) 'disabled))
468 (let ((end-regexp (mm-uu-end-regexp entry)))
470 (or (setq end-point (point-max)) t)
472 (re-search-forward end-regexp nil t)
474 (setq end-point (point)))))
475 (or (not (setq func (mm-uu-function-2 entry)))
477 (if (and (> start-point text-start)
479 (goto-char text-start)
480 (re-search-forward "." start-point t)))
482 (mm-make-handle (mm-uu-copy-to-buffer text-start start-point)
483 mm-uu-text-plain-type)
486 (funcall (mm-uu-function-extract entry))
488 (goto-char (setq text-start end-point))))
490 (if (and (> (point-max) (1+ text-start))
492 (goto-char text-start)
493 (re-search-forward "." nil t)))
495 (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
496 mm-uu-text-plain-type)
498 (setq result (cons "multipart/mixed" (nreverse result))))
501 (defun mm-uu-dissect-text-parts (handle)
502 "Dissect text parts and put uu handles into HANDLE."
503 (let ((buffer (mm-handle-buffer handle))
505 (cond ((stringp buffer)
506 (mapc 'mm-uu-dissect-text-parts (cdr handle)))
508 (when (and (setq type (mm-handle-media-type handle))
510 (string-match "\\`text/" type)
511 (with-current-buffer buffer
513 (mm-uu-dissect t (mm-handle-type handle)))))
515 (setcar handle (car children))
516 (setcdr handle (cdr children))))
518 (mapc 'mm-uu-dissect-text-parts handle)))))
522 ;;; mm-uu.el ends here