Sync up with Pteroductyl Gnus 0.59
[elisp/gnus.git-] / 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: 1.1.2.6 $
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 (require 'gnus-mailcap)
32
33 (eval-and-compile
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"))
38
39 (defun mm-uu-copy-to-buffer (from to)
40   "Copy the contents of the current buffer to a fresh buffer."
41   (save-excursion
42     (let ((obuf (current-buffer)))
43       (set-buffer (generate-new-buffer " *mm-uu*"))
44       (insert-buffer-substring obuf from to)
45       (current-buffer))))
46
47 ;;; postscript
48
49 (defconst mm-uu-postscript-begin-line "^%!PS-")
50 (defconst mm-uu-postscript-end-line "^%%EOF$")
51
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)
55
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)
60
61 (defconst mm-uu-shar-begin-line "^#! */bin/sh")
62 (defconst mm-uu-shar-end-line "^exit 0")
63
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))
69
70 (defvar mm-uu-identifier-alist
71   '((?% . postscript) (?b . uu) (?: . binhex) (?# . shar)))
72
73 ;;;### autoload
74
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)
79     (save-excursion
80       (save-restriction
81         (mail-narrow-to-head)
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)
86                       (error nil))
87                 charset (and ctl (mail-content-type-get ctl 'charset)))
88           (if (stringp cte) 
89               (setq cte (intern (downcase (mail-header-remove-whitespace
90                                            (mail-header-remove-comments
91                                             cte)))))))
92         (goto-char (point-max)))
93       (forward-line)
94       (setq text-start (point)
95             text-plain-type (cons "text/plain" 
96                                   (if charset 
97                                       (list (cons 'charset charset)))))
98       (while (re-search-forward mm-uu-begin-line nil t)
99         (beginning-of-line)
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)))
104         (setq file-name 
105               (if (eq type 'uu)
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) 
112                                         "-end-line"))))
113         (when (re-search-forward end-line nil t)
114           (forward-line)
115           (setq end-char (point))
116           (when (or (not (eq type 'binhex))
117                     (setq file-name 
118                           (condition-case nil
119                               (binhex-decode-region start-char end-char t)
120                             (error nil))))
121             (if (> start-char text-start)
122                 (push
123                  (mm-make-handle (mm-uu-copy-to-buffer text-start start-char) 
124                        text-plain-type cte) 
125                  result))
126             (push 
127              (cond
128               ((eq type 'postscript)
129                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char) 
130                      '("application/postscript")))
131               ((eq type 'uu)
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)))))
141               ((eq type 'binhex)
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)))))
151               ((eq type 'shar)
152                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char) 
153                      '("application/x-shar")))) 
154              result)
155             (setq text-start end-char))))
156       (when result
157         (if (> (point-max) (1+ text-start))
158             (push
159              (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max)) 
160                    text-plain-type cte) 
161              result))
162         (setq result (cons "multipart/mixed" (nreverse result))))
163       result)))
164
165 ;;;### autoload
166 (defun mm-uu-test ()
167   "Check whether the current buffer contains uu stuffs."
168   (save-excursion
169     (save-restriction
170       (mail-narrow-to-head)
171       (goto-char (point-max)))
172     (forward-line)
173     (let (type end-line result)
174       (while (and (not result) (re-search-forward mm-uu-begin-line nil t))
175         (forward-line) 
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) 
180                                         "-end-line"))))
181         (if (re-search-forward end-line nil t)
182             (setq result t)))
183       result)))
184
185 (provide 'mm-uu)
186
187 ;;; mm-uu.el ends here