1 ;;; mm-uu.el -- Return uu stuffs as mm handles
2 ;; Copyright (c) 1998,99 by Shenghuo Zhu
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: postscript uudecode binhex shar forward
7 ;; This file is part of pgnus.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
33 (require 'gnus-mailcap)
36 (autoload 'binhex-decode-region "binhex")
37 (autoload 'binhex-decode-region-external "binhex")
38 (autoload 'uudecode-decode-region "uudecode")
39 (autoload 'uudecode-decode-region-external "uudecode"))
41 (defun mm-uu-copy-to-buffer (from to)
42 "Copy the contents of the current buffer to a fresh buffer."
44 (let ((obuf (current-buffer)))
45 (set-buffer (generate-new-buffer " *mm-uu*"))
46 (insert-buffer-substring obuf from to)
51 (defconst mm-uu-postscript-begin-line "^%!PS-")
52 (defconst mm-uu-postscript-end-line "^%%EOF$")
54 (defconst mm-uu-uu-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+")
55 (defconst mm-uu-uu-end-line "^end[ \t]*$")
57 (defcustom mm-uu-decode-function 'uudecode-decode-region
58 "*Function to uudecode.
59 Internal function is done in elisp by default, therefore decoding may
60 appear to be horribly slow . You can make Gnus use the external Unix
61 decoder, such as uudecode."
62 :type '(choice (item :tag "internal" uudecode-decode-region)
63 (item :tag "external" uudecode-decode-region-external))
64 :group 'gnus-article-mime)
66 (defconst mm-uu-binhex-begin-line
67 "^:...............................................................$")
68 (defconst mm-uu-binhex-end-line ":$")
70 (defcustom mm-uu-binhex-decode-function 'binhex-decode-region
71 "*Function to binhex decode.
72 Internal function is done in elisp by default, therefore decoding may
73 appear to be horribly slow . You can make Gnus use the external Unix
74 decoder, such as hexbin."
75 :type '(choice (item :tag "internal" binhex-decode-region)
76 (item :tag "external" binhex-decode-region-external))
77 :group 'gnus-article-mime)
79 (defconst mm-uu-shar-begin-line "^#! */bin/sh")
80 (defconst mm-uu-shar-end-line "^exit 0\\|^$")
82 ;;; Thanks to Edward J. Sabol <sabol@alderaan.gsfc.nasa.gov> and
83 ;;; Peter von der Ah\'e <pahe@daimi.au.dk>
84 (defconst mm-uu-forward-begin-line "^-+ \\(?:Start of \\)?Forwarded message")
85 (defconst mm-uu-forward-end-line "^-+ End of forwarded message")
87 (defvar mm-uu-begin-line nil)
89 (defconst mm-uu-identifier-alist
90 '((?% . postscript) (?b . uu) (?: . binhex) (?# . shar)
93 (defvar mm-dissect-disposition "inline"
94 "The default disposition of uu parts.
95 This can be either \"inline\" or \"attachment\".")
97 (defun mm-uu-configure-p (key val)
98 (member (cons key val) mm-uu-configure-list))
100 (defun mm-uu-configure (&optional symbol value)
101 (if symbol (set-default symbol value))
102 (setq mm-uu-begin-line nil)
103 (mapcar '(lambda (type)
104 (if (mm-uu-configure-p type 'disabled)
106 (setq mm-uu-begin-line
107 (concat mm-uu-begin-line
108 (if mm-uu-begin-line "\\|")
110 (intern (concat "mm-uu-" (symbol-name type)
112 '(uu postscript binhex shar forward)))
114 (defcustom mm-uu-configure-list nil
115 "A list of mm-uu configuration.
116 To disable dissecting shar codes, for instance, add
117 `(shar . disabled)' to this list."
119 (choice (item postscript)
124 (choice (item disabled))))
125 :group 'gnus-article-mime
126 :set 'mm-uu-configure)
132 (defun mm-uu-dissect ()
133 "Dissect the current buffer and return a list of uu handles."
134 (let (ct ctl cte charset text-start start-char end-char
135 type file-name end-line result text-plain-type
136 start-char-1 end-char-1
137 (case-fold-search t))
140 (mail-narrow-to-head)
141 (when (and (mail-fetch-field "mime-version")
142 (setq ct (mail-fetch-field "content-type")))
143 (setq cte (message-fetch-field "content-transfer-encoding" t)
144 ctl (ignore-errors (mail-header-parse-content-type ct))
145 charset (and ctl (mail-content-type-get ctl 'charset)))
147 (setq cte (intern (downcase (mail-header-remove-whitespace
148 (mail-header-remove-comments
150 (goto-char (point-max)))
152 (setq text-start (point)
153 text-plain-type (cons "text/plain"
155 (list (cons 'charset charset)))))
156 (while (re-search-forward mm-uu-begin-line nil t)
157 (setq start-char (match-beginning 0))
158 (setq type (cdr (assq (aref (match-string 0) 0)
159 mm-uu-identifier-alist)))
161 (if (and (eq type 'uu)
162 (looking-at "\\(.+\\)$"))
163 (and (match-string 1)
164 (let ((nnheader-file-name-translation-alist
165 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
166 (nnheader-translate-file-chars (match-string 1))))))
167 (forward-line) ;; in case of failure
168 (setq start-char-1 (point))
169 (setq end-line (symbol-value
170 (intern (concat "mm-uu-" (symbol-name type)
172 (when (and (re-search-forward end-line nil t)
173 (not (eq (match-beginning 0) (match-end 0))))
174 (setq end-char-1 (match-beginning 0))
176 (setq end-char (point))
177 (when (or (not (eq type 'binhex))
180 (binhex-decode-region start-char end-char t))))
181 (if (> start-char text-start)
183 (mm-make-handle (mm-uu-copy-to-buffer text-start start-char)
188 ((eq type 'postscript)
189 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
190 '("application/postscript")))
192 (mm-make-handle (mm-uu-copy-to-buffer start-char-1 end-char-1)
193 '("message/rfc822")))
195 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
196 (list (or (and file-name
197 (string-match "\\.[^\\.]+$" file-name)
198 (mailcap-extension-to-mime
199 (match-string 0 file-name)))
200 "application/octet-stream"))
202 (if (and file-name (not (equal file-name "")))
203 (list mm-dissect-disposition
204 (cons 'filename file-name)))))
206 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
207 (list (or (and file-name
208 (string-match "\\.[^\\.]+$" file-name)
209 (mailcap-extension-to-mime
210 (match-string 0 file-name)))
211 "application/octet-stream"))
213 (if (and file-name (not (equal file-name "")))
214 (list mm-dissect-disposition
215 (cons 'filename file-name)))))
217 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
218 '("application/x-shar"))))
220 (setq text-start end-char))))
222 (if (> (point-max) (1+ text-start))
224 (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
227 (setq result (cons "multipart/mixed" (nreverse result))))
232 "Check whether the current buffer contains uu stuffs."
235 (mail-narrow-to-head)
236 (goto-char (point-max)))
238 (let (type end-line result
239 (case-fold-search t))
240 (while (and mm-uu-begin-line
241 (not result) (re-search-forward mm-uu-begin-line nil t))
243 (setq type (cdr (assq (aref (match-string 0) 0)
244 mm-uu-identifier-alist)))
245 (setq end-line (symbol-value
246 (intern (concat "mm-uu-" (symbol-name type)
248 (if (and (re-search-forward end-line nil t)
249 (not (eq (match-beginning 0) (match-end 0))))
255 ;;; mm-uu.el ends here