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.6 $
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)))
75 (defun mm-uu-dissect ()
76 "Dissect the current buffer and return a list of uu handles."
77 (let (ct ctl cte charset text-start start-char end-char
78 type file-name end-line result text-plain-type)
82 (when (and (mail-fetch-field "mime-version")
83 (setq ct (mail-fetch-field "content-type")))
84 (setq cte (message-fetch-field "content-transfer-encoding" t)
85 ctl (condition-case () (mail-header-parse-content-type ct)
87 charset (and ctl (mail-content-type-get ctl 'charset)))
89 (setq cte (intern (downcase (mail-header-remove-whitespace
90 (mail-header-remove-comments
92 (goto-char (point-max)))
94 (setq text-start (point)
95 text-plain-type (cons "text/plain"
97 (list (cons 'charset charset)))))
98 (while (re-search-forward mm-uu-begin-line nil t)
100 (setq start-char (point))
101 (forward-line) ;; in case of failure
102 (setq type (cdr (assq (aref (match-string 0) 0)
103 mm-uu-identifier-alist)))
106 (and (match-string 1)
107 (let ((nnheader-file-name-translation-alist
108 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
109 (nnheader-translate-file-chars (match-string 1))))))
110 (setq end-line (symbol-value
111 (intern (concat "mm-uu-" (symbol-name type)
113 (when (re-search-forward end-line nil t)
115 (setq end-char (point))
116 (when (or (not (eq type 'binhex))
119 (binhex-decode-region start-char end-char t)
121 (if (> start-char text-start)
123 (mm-make-handle (mm-uu-copy-to-buffer text-start start-char)
128 ((eq type 'postscript)
129 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
130 '("application/postscript")))
132 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
133 (list (or (and file-name
134 (string-match "\\.[^\\.]+$" file-name)
135 (mailcap-extension-to-mime
136 (match-string 0 file-name)))
137 "application/octet-stream"))
138 mm-uu-decode-function nil
139 (if (and file-name (not (equal file-name "")))
140 (list "attachment" (cons 'filename file-name)))))
142 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
143 (list (or (and file-name
144 (string-match "\\.[^\\.]+$" file-name)
145 (mailcap-extension-to-mime
146 (match-string 0 file-name)))
147 "application/octet-stream"))
148 mm-uu-binhex-decode-function nil
149 (if (and file-name (not (equal file-name "")))
150 (list "attachment" (cons 'filename file-name)))))
152 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
153 '("application/x-shar"))))
155 (setq text-start end-char))))
157 (if (> (point-max) (1+ text-start))
159 (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
162 (setq result (cons "multipart/mixed" (nreverse result))))
167 "Check whether the current buffer contains uu stuffs."
170 (mail-narrow-to-head)
171 (goto-char (point-max)))
173 (let (type end-line result)
174 (while (and (not result) (re-search-forward mm-uu-begin-line nil t))
176 (setq type (cdr (assq (aref (match-string 0) 0)
177 mm-uu-identifier-alist)))
178 (setq end-line (symbol-value
179 (intern (concat "mm-uu-" (symbol-name type)
181 (if (re-search-forward end-line nil t)
187 ;;; mm-uu.el ends here