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.4 $
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."
80 (goto-char (point-max)))
82 (let ((text-start (point)) start-char end-char
83 type file-name end-line result)
84 (while (re-search-forward mm-uu-begin-line nil t)
86 (setq start-char (point))
87 (forward-line) ;; in case of failure
88 (setq type (cdr (assq (aref (match-string 0) 0)
89 mm-uu-identifier-alist)))
93 (let ((nnheader-file-name-translation-alist
94 '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
95 (nnheader-translate-file-chars (match-string 1))))))
96 (setq end-line (symbol-value
97 (intern (concat "mm-uu-" (symbol-name type)
99 (when (re-search-forward end-line nil t)
101 (setq end-char (point))
102 (when (or (not (eq type 'binhex))
105 (binhex-decode-region start-char end-char t)
107 (if (> start-char text-start)
109 (list (mm-uu-copy-to-buffer text-start start-char)
110 '("text/plain") nil nil nil nil)
114 ((eq type 'postscript)
115 (list (mm-uu-copy-to-buffer start-char end-char)
116 '("application/postscript") nil nil nil nil))
118 (list (mm-uu-copy-to-buffer start-char end-char)
119 (list (or (and file-name
120 (string-match "\\.[^\\.]+$" file-name)
121 (mailcap-extension-to-mime
122 (match-string 0 file-name)))
123 "application/octet-stream"))
124 mm-uu-decode-function nil
125 (if (and file-name (not (equal file-name "")))
126 (list "attachment" (cons 'filename file-name)))
129 (list (mm-uu-copy-to-buffer start-char end-char)
130 (list (or (and file-name
131 (string-match "\\.[^\\.]+$" file-name)
132 (mailcap-extension-to-mime
133 (match-string 0 file-name)))
134 "application/octet-stream"))
135 mm-uu-binhex-decode-function nil
136 (if (and file-name (not (equal file-name "")))
137 (list "attachment" (cons 'filename file-name)))
140 (list (mm-uu-copy-to-buffer start-char end-char)
141 '("application/x-shar") nil nil nil nil)))
143 (setq text-start end-char))))
145 (if (> (point-max) (1+ text-start))
147 (list (mm-uu-copy-to-buffer text-start (point-max))
148 '("text/plain") nil nil nil nil)
150 (setq result (cons "multipart/mixed" (nreverse result))))
155 ;;; mm-uu.el ends here