1 ;;; mm-uu.el -- Return uu stuffs as mm handles
2 ;; Copyright (c) 1998 by Shenghuo Zhu <zsh@cs.rochester.edu>
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; Keywords: news postscript uudecode binhex shar
8 ;; This file is not part of GNU Emacs, but the same permissions
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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
32 (autoload 'binhex-decode-region "binhex")
33 (autoload 'binhex-decode-region-external "binhex")
34 (autoload 'uudecode-decode-region "uudecode")
35 (autoload 'uudecode-decode-region-external "uudecode"))
37 (defun mm-uu-copy-to-buffer (from to)
38 "Copy the contents of the current buffer to a fresh buffer."
40 (let ((obuf (current-buffer)))
41 (set-buffer (generate-new-buffer " *mm-uu*"))
42 (insert-buffer-substring obuf from to)
47 (defconst mm-uu-postscript-begin-line "^%!PS-")
48 (defconst mm-uu-postscript-end-line "^%%EOF$")
50 (defconst mm-uu-uu-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
51 (defconst mm-uu-uu-end-line "^end[ \t]*$")
52 (defvar mm-uu-decode-function 'uudecode-decode-region)
54 (defconst mm-uu-binhex-begin-line
55 "^:...............................................................$")
56 (defconst mm-uu-binhex-end-line ":$")
57 (defvar mm-uu-binhex-decode-function 'binhex-decode-region)
59 (defconst mm-uu-shar-begin-line "^#! */bin/sh")
60 (defconst mm-uu-shar-end-line "^exit 0")
62 (defvar mm-uu-begin-line
63 (concat mm-uu-postscript-begin-line "\\|"
64 mm-uu-uu-begin-line "\\|"
65 mm-uu-binhex-begin-line "\\|"
66 mm-uu-shar-begin-line))
68 (defvar mm-uu-identifier-alist
69 '((?% . postscript) (?b . uu) (?: . binhex) (?# . shar)))
71 (defvar mm-dissect-disposition "inline"
72 "The default disposition of uu parts.
73 This can be either \"inline\" or \"attachment\".")
77 (defun mm-uu-dissect ()
78 "Dissect the current buffer and return a list of uu handles."
79 (let (ct ctl cte charset text-start start-char end-char
80 type file-name end-line result text-plain-type)
84 (when (and (mail-fetch-field "mime-version")
85 (setq ct (mail-fetch-field "content-type")))
86 (setq cte (message-fetch-field "content-transfer-encoding" t)
87 ctl (condition-case () (mail-header-parse-content-type ct)
89 charset (and ctl (mail-content-type-get ctl 'charset)))
91 (setq cte (intern (downcase (mail-header-remove-whitespace
92 (mail-header-remove-comments
94 (goto-char (point-max)))
96 (setq text-start (point)
97 text-plain-type (cons "text/plain"
99 (list (cons 'charset charset)))))
100 (while (re-search-forward mm-uu-begin-line nil t)
102 (setq start-char (point))
103 (forward-line) ;; in case of failure
104 (setq type (cdr (assq (aref (match-string 0) 0)
105 mm-uu-identifier-alist)))
108 (and (match-string 1)
109 (let ((nnheader-file-name-translation-alist
110 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
111 (nnheader-translate-file-chars (match-string 1))))))
112 (setq end-line (symbol-value
113 (intern (concat "mm-uu-" (symbol-name type)
115 (when (re-search-forward end-line nil t)
117 (setq end-char (point))
118 (when (or (not (eq type 'binhex))
121 (binhex-decode-region start-char end-char t)
123 (if (> start-char text-start)
125 (mm-make-handle (mm-uu-copy-to-buffer text-start start-char)
130 ((eq type 'postscript)
131 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
132 '("application/postscript")))
134 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
135 (list (or (and file-name
136 (string-match "\\.[^\\.]+$" file-name)
137 (mailcap-extension-to-mime
138 (match-string 0 file-name)))
139 "application/octet-stream"))
141 (if (and file-name (not (equal file-name "")))
142 (list mm-dissect-disposition (cons 'filename file-name)))))
144 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
145 (list (or (and file-name
146 (string-match "\\.[^\\.]+$" file-name)
147 (mailcap-extension-to-mime
148 (match-string 0 file-name)))
149 "application/octet-stream"))
151 (if (and file-name (not (equal file-name "")))
152 (list mm-dissect-disposition (cons 'filename file-name)))))
154 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
155 '("application/x-shar"))))
157 (setq text-start end-char))))
159 (if (> (point-max) (1+ text-start))
161 (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
164 (setq result (cons "multipart/mixed" (nreverse result))))
169 "Check whether the current buffer contains uu stuffs."
172 (mail-narrow-to-head)
173 (goto-char (point-max)))
175 (let (type end-line result)
176 (while (and (not result) (re-search-forward mm-uu-begin-line nil t))
178 (setq type (cdr (assq (aref (match-string 0) 0)
179 mm-uu-identifier-alist)))
180 (setq end-line (symbol-value
181 (intern (concat "mm-uu-" (symbol-name type)
183 (if (re-search-forward end-line nil t)
189 ;;; mm-uu.el ends here