*** empty log message ***
[gnus] / lisp / mm-uu.el
1 ;;; mm-uu.el -- Return uu stuffs as mm handles
2 ;; Copyright (c) 1998 by Shenghuo Zhu <zsh@cs.rochester.edu>
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; $Revision: 5.5 $
6 ;; Keywords: news postscript uudecode binhex shar
7   
8 ;; This file is not part of GNU Emacs, but the same permissions
9 ;; apply.
10 ;;
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)
14 ;; any later version.
15 ;;
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.
20 ;;
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.
25
26 ;;; Commentary:
27 ;;
28
29 ;;; Code:
30
31 (eval-and-compile
32   (autoload 'binhex-decode-region "binhex")
33   (autoload 'binhex-decode-region-external "binhex")
34   (autoload 'uudecode-decode-region "uudecode")
35   (autoload 'uudecode-decode-region-external "uudecode"))
36
37 (defun mm-uu-copy-to-buffer (from to)
38   "Copy the contents of the current buffer to a fresh buffer."
39   (save-excursion
40     (let ((obuf (current-buffer)))
41       (set-buffer (generate-new-buffer " *mm-uu*"))
42       (insert-buffer-substring obuf from to)
43       (current-buffer))))
44
45 ;;; postscript
46
47 (defconst mm-uu-postscript-begin-line "^%!PS-")
48 (defconst mm-uu-postscript-end-line "^%%EOF$")
49
50 (defconst mm-uu-uu-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
51 (defconst mm-uu-uu-end-line "^end[ \t]*$")
52 (defvar mm-uu-decode-function 'uudecode-decode-region)
53
54 (defconst mm-uu-binhex-begin-line
55   "^:...............................................................$")
56 (defconst mm-uu-binhex-end-line ":$")
57 (defvar mm-uu-binhex-decode-function 'binhex-decode-region)
58
59 (defconst mm-uu-shar-begin-line "^#! */bin/sh")
60 (defconst mm-uu-shar-end-line "^exit 0")
61
62 (defvar mm-uu-begin-line 
63   (concat mm-uu-postscript-begin-line "\\|"
64           mm-uu-uu-begin-line "\\|"
65           mm-uu-binhex-begin-line "\\|"
66           mm-uu-shar-begin-line))
67
68 (defvar mm-uu-identifier-alist
69   '((?% . postscript) (?b . uu) (?: . binhex) (?# . shar)))
70
71 ;;;### autoload
72
73 (defun mm-uu-dissect ()
74   "Dissect the current buffer and return a list of uu handles."
75   (let (ct ctl cte charset text-start start-char end-char 
76            type file-name end-line result text-plain-type)
77     (save-excursion
78       (save-restriction
79         (mail-narrow-to-head)
80         (when (and (mail-fetch-field "mime-version")
81                    (setq ct (mail-fetch-field "content-type")))
82           (setq cte (message-fetch-field "content-transfer-encoding" t)
83                 ctl (condition-case () (mail-header-parse-content-type ct)
84                       (error nil))
85                 charset (and ctl (mail-content-type-get ctl 'charset)))
86           (if (stringp cte) 
87               (setq cte (intern (downcase (mail-header-remove-whitespace
88                                            (mail-header-remove-comments
89                                             cte)))))))
90         (goto-char (point-max)))
91       (forward-line)
92       (setq text-start (point)
93             text-plain-type (cons "text/plain" 
94                                   (if charset 
95                                       (list (cons 'charset charset)))))
96       (while (re-search-forward mm-uu-begin-line nil t)
97         (beginning-of-line)
98         (setq start-char (point))
99         (forward-line) ;; in case of failure
100         (setq type (cdr (assq (aref (match-string 0) 0) 
101                               mm-uu-identifier-alist)))
102         (setq file-name 
103               (if (eq type 'uu)
104                   (and (match-string 1)
105                        (let ((nnheader-file-name-translation-alist
106                               '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
107                          (nnheader-translate-file-chars (match-string 1))))))
108         (setq end-line (symbol-value 
109                         (intern (concat "mm-uu-" (symbol-name type) 
110                                         "-end-line"))))
111         (when (re-search-forward end-line nil t)
112           (forward-line)
113           (setq end-char (point))
114           (when (or (not (eq type 'binhex))
115                     (setq file-name 
116                           (condition-case nil
117                               (binhex-decode-region start-char end-char t)
118                             (error nil))))
119             (if (> start-char text-start)
120                 (push
121                  (mm-make-handle (mm-uu-copy-to-buffer text-start start-char) 
122                        text-plain-type cte) 
123                  result))
124             (push 
125              (cond
126               ((eq type 'postscript)
127                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char) 
128                      '("application/postscript")))
129               ((eq type 'uu)
130                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char) 
131                      (list (or (and file-name
132                                     (string-match "\\.[^\\.]+$" file-name) 
133                                     (mailcap-extension-to-mime 
134                                      (match-string 0 file-name)))
135                                "application/octet-stream"))
136                      mm-uu-decode-function nil 
137                      (if (and file-name (not (equal file-name "")))
138                          (list "inline" (cons 'filename file-name)))))
139               ((eq type 'binhex)
140                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char) 
141                      (list (or (and file-name
142                                     (string-match "\\.[^\\.]+$" file-name) 
143                                     (mailcap-extension-to-mime 
144                                      (match-string 0 file-name)))
145                                "application/octet-stream"))
146                      mm-uu-binhex-decode-function nil 
147                      (if (and file-name (not (equal file-name "")))
148                          (list "inline" (cons 'filename file-name)))))
149               ((eq type 'shar)
150                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char) 
151                      '("application/x-shar")))) 
152              result)
153             (setq text-start end-char))))
154       (when result
155         (if (> (point-max) (1+ text-start))
156             (push
157              (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max)) 
158                    text-plain-type cte) 
159              result))
160         (setq result (cons "multipart/mixed" (nreverse result))))
161       result)))
162
163 ;;;### autoload
164 (defun mm-uu-test ()
165   "Check whether the current buffer contains uu stuffs."
166   (save-excursion
167     (save-restriction
168       (mail-narrow-to-head)
169       (goto-char (point-max)))
170     (forward-line)
171     (let (type end-line result)
172       (while (and (not result) (re-search-forward mm-uu-begin-line nil t))
173         (forward-line) 
174         (setq type (cdr (assq (aref (match-string 0) 0) 
175                               mm-uu-identifier-alist)))
176         (setq end-line (symbol-value 
177                         (intern (concat "mm-uu-" (symbol-name type) 
178                                         "-end-line"))))
179         (if (re-search-forward end-line nil t)
180             (setq result t)))
181       result)))
182
183 (provide 'mm-uu)
184
185 ;;; mm-uu.el ends here