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]*$")
56 (defvar mm-uu-decode-function 'uudecode-decode-region)
58 (defconst mm-uu-binhex-begin-line
59 "^:...............................................................$")
60 (defconst mm-uu-binhex-end-line ":$")
61 (defvar mm-uu-binhex-decode-function 'binhex-decode-region)
63 (defconst mm-uu-shar-begin-line "^#! */bin/sh")
64 (defconst mm-uu-shar-end-line "^exit 0")
66 ;;; Thanks to Edward J. Sabol <sabol@alderaan.gsfc.nasa.gov> and
67 ;;; Peter von der Ah\'e <pahe@daimi.au.dk>
68 (defconst mm-uu-forward-begin-line "^-+ \\(Start of \\)?Forwarded message")
69 (defconst mm-uu-forward-end-line "^-+ End of forwarded message")
71 (defconst mm-uu-begin-line
72 (concat mm-uu-postscript-begin-line "\\|"
73 mm-uu-uu-begin-line "\\|"
74 mm-uu-binhex-begin-line "\\|"
75 mm-uu-shar-begin-line "\\|"
76 mm-uu-forward-begin-line))
78 (defconst mm-uu-identifier-alist
79 '((?% . postscript) (?b . uu) (?: . binhex) (?# . shar)
82 (defvar mm-dissect-disposition "inline"
83 "The default disposition of uu parts.
84 This can be either \"inline\" or \"attachment\".")
88 (defun mm-uu-dissect ()
89 "Dissect the current buffer and return a list of uu handles."
90 (let (ct ctl cte charset text-start start-char end-char
91 type file-name end-line result text-plain-type
92 start-char-1 end-char-1
97 (when (and (mail-fetch-field "mime-version")
98 (setq ct (mail-fetch-field "content-type")))
99 (setq cte (message-fetch-field "content-transfer-encoding" t)
100 ctl (ignore-errors (mail-header-parse-content-type ct))
101 charset (and ctl (mail-content-type-get ctl 'charset)))
103 (setq cte (intern (downcase (mail-header-remove-whitespace
104 (mail-header-remove-comments
106 (goto-char (point-max)))
108 (setq text-start (point)
109 text-plain-type (cons "text/plain"
111 (list (cons 'charset charset)))))
112 (while (re-search-forward mm-uu-begin-line nil t)
113 (setq start-char (match-beginning 0))
114 (forward-line) ;; in case of failure
115 (setq start-char-1 (point))
116 (setq type (cdr (assq (aref (match-string 0) 0)
117 mm-uu-identifier-alist)))
120 (and (match-string 1)
121 (let ((nnheader-file-name-translation-alist
122 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
123 (nnheader-translate-file-chars (match-string 1))))))
124 (setq end-line (symbol-value
125 (intern (concat "mm-uu-" (symbol-name type)
127 (when (re-search-forward end-line nil t)
128 (setq end-char-1 (match-beginning 0))
130 (setq end-char (point))
131 (when (or (not (eq type 'binhex))
134 (binhex-decode-region start-char end-char t))))
135 (if (> start-char text-start)
137 (mm-make-handle (mm-uu-copy-to-buffer text-start start-char)
142 ((eq type 'postscript)
143 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
144 '("application/postscript")))
146 (mm-make-handle (mm-uu-copy-to-buffer start-char-1 end-char-1)
147 '("message/rfc822")))
149 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
150 (list (or (and file-name
151 (string-match "\\.[^\\.]+$" file-name)
152 (mailcap-extension-to-mime
153 (match-string 0 file-name)))
154 "application/octet-stream"))
156 (if (and file-name (not (equal file-name "")))
157 (list mm-dissect-disposition
158 (cons 'filename file-name)))))
160 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
161 (list (or (and file-name
162 (string-match "\\.[^\\.]+$" file-name)
163 (mailcap-extension-to-mime
164 (match-string 0 file-name)))
165 "application/octet-stream"))
167 (if (and file-name (not (equal file-name "")))
168 (list mm-dissect-disposition
169 (cons 'filename file-name)))))
171 (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
172 '("application/x-shar"))))
174 (setq text-start end-char))))
176 (if (> (point-max) (1+ text-start))
178 (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
181 (setq result (cons "multipart/mixed" (nreverse result))))
186 "Check whether the current buffer contains uu stuffs."
189 (mail-narrow-to-head)
190 (goto-char (point-max)))
192 (let (type end-line result
193 (case-fold-search t))
194 (while (and (not result) (re-search-forward mm-uu-begin-line nil t))
196 (setq type (cdr (assq (aref (match-string 0) 0)
197 mm-uu-identifier-alist)))
198 (setq end-line (symbol-value
199 (intern (concat "mm-uu-" (symbol-name type)
201 (if (re-search-forward end-line nil t)
207 ;;; mm-uu.el ends here