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>
5 ;; $Revision: 1.1.2.11 $
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.
31 (require 'gnus-mailcap)
34 (autoload 'binhex-decode-region "binhex")
35 (autoload 'binhex-decode-region-external "binhex")
36 (autoload 'uudecode-decode-region "uudecode")
37 (autoload 'uudecode-decode-region-external "uudecode"))
39 (defun mm-uu-copy-to-buffer (from to)
40 "Copy the contents of the current buffer to a fresh buffer."
42 (let ((obuf (current-buffer)))
43 (set-buffer (generate-new-buffer " *mm-uu*"))
44 (insert-buffer-substring obuf from to)
49 (defconst mm-uu-postscript-begin-line "^%!PS-")
50 (defconst mm-uu-postscript-end-line "^%%EOF$")
52 (defconst mm-uu-uu-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
53 (defconst mm-uu-uu-end-line "^end[ \t]*$")
54 (defvar mm-uu-decode-function 'uudecode-decode-region)
56 (defconst mm-uu-binhex-begin-line
57 "^:...............................................................$")
58 (defconst mm-uu-binhex-end-line ":$")
59 (defvar mm-uu-binhex-decode-function 'binhex-decode-region)
61 (defconst mm-uu-shar-begin-line "^#! */bin/sh")
62 (defconst mm-uu-shar-end-line "^exit 0")
64 (defvar mm-uu-begin-line
65 (concat mm-uu-postscript-begin-line "\\|"
66 mm-uu-uu-begin-line "\\|"
67 mm-uu-binhex-begin-line "\\|"
68 mm-uu-shar-begin-line))
70 (defvar mm-uu-identifier-alist
71 '((?% . postscript) (?b . uu) (?: . binhex) (?# . shar)))
73 (defvar mm-dissect-disposition "inline"
74 "The default disposition of uu parts.
75 This can be either \"inline\" or \"attachment\".")
79 (defun mm-uu-dissect ()
80 "Dissect the current buffer and return a list of uu handles."
81 (let (ct ctl cte charset text-start start-char end-char
82 type file-name end-line result text-plain-type)
86 (when (and (mail-fetch-field "mime-version")
87 (setq ct (mail-fetch-field "content-type")))
88 (setq cte (message-fetch-field "content-transfer-encoding" t)
89 ctl (ignore-errors (mail-header-parse-content-type ct))
90 charset (and ctl (mail-content-type-get ctl 'charset)))
92 (setq cte (intern (downcase (mail-header-remove-whitespace
93 (mail-header-remove-comments
95 (goto-char (point-max)))
97 (setq text-start (point)
98 text-plain-type (cons "text/plain"
100 (list (cons 'charset charset)))))
101 (while (re-search-forward mm-uu-begin-line nil t)
103 (setq start-char (point))
104 (forward-line) ;; in case of failure
105 (setq type (cdr (assq (aref (match-string 0) 0)
106 mm-uu-identifier-alist)))
109 (and (match-string 1)
110 (let ((nnheader-file-name-translation-alist
111 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
112 (nnheader-translate-file-chars (match-string 1))))))
113 (setq end-line (symbol-value
114 (intern (concat "mm-uu-" (symbol-name type)
116 (when (re-search-forward end-line nil t)
118 (setq end-char (point))
119 (when (or (not (eq type 'binhex))
122 (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
143 (cons 'filename file-name)))))
145 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
146 (list (or (and file-name
147 (string-match "\\.[^\\.]+$" file-name)
148 (mailcap-extension-to-mime
149 (match-string 0 file-name)))
150 "application/octet-stream"))
152 (if (and file-name (not (equal file-name "")))
153 (list mm-dissect-disposition
154 (cons 'filename file-name)))))
156 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
157 '("application/x-shar"))))
159 (setq text-start end-char))))
161 (if (> (point-max) (1+ text-start))
163 (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
166 (setq result (cons "multipart/mixed" (nreverse result))))
171 "Check whether the current buffer contains uu stuffs."
174 (mail-narrow-to-head)
175 (goto-char (point-max)))
177 (let (type end-line result)
178 (while (and (not result) (re-search-forward mm-uu-begin-line nil t))
180 (setq type (cdr (assq (aref (match-string 0) 0)
181 mm-uu-identifier-alist)))
182 (setq end-line (symbol-value
183 (intern (concat "mm-uu-" (symbol-name type)
185 (if (re-search-forward end-line nil t)
191 ;;; mm-uu.el ends here