* lisp/gnus.el (gnus-version-number): Update to 6.10.057.
[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.11 $
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 (defvar mm-dissect-disposition "inline"
74   "The default disposition of uu parts.
75 This can be either \"inline\" or \"attachment\".")
76
77 ;;;### autoload
78
79 (defun mm-uu-dissect ()
80   "Dissect the current buffer and return a list of uu handles."
81   (let (ct ctl cte charset text-start start-char end-char
82            type file-name end-line result text-plain-type)
83     (save-excursion
84       (save-restriction
85         (mail-narrow-to-head)
86         (when (and (mail-fetch-field "mime-version")
87                    (setq ct (mail-fetch-field "content-type")))
88           (setq cte (message-fetch-field "content-transfer-encoding" t)
89                 ctl (ignore-errors (mail-header-parse-content-type ct))
90                 charset (and ctl (mail-content-type-get ctl 'charset)))
91           (if (stringp cte)
92               (setq cte (intern (downcase (mail-header-remove-whitespace
93                                            (mail-header-remove-comments
94                                             cte)))))))
95         (goto-char (point-max)))
96       (forward-line)
97       (setq text-start (point)
98             text-plain-type (cons "text/plain"
99                                   (if charset
100                                       (list (cons 'charset charset)))))
101       (while (re-search-forward mm-uu-begin-line nil t)
102         (beginning-of-line)
103         (setq start-char (point))
104         (forward-line) ;; in case of failure
105         (setq type (cdr (assq (aref (match-string 0) 0)
106                               mm-uu-identifier-alist)))
107         (setq file-name
108               (if (eq type 'uu)
109                   (and (match-string 1)
110                        (let ((nnheader-file-name-translation-alist
111                               '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
112                          (nnheader-translate-file-chars (match-string 1))))))
113         (setq end-line (symbol-value
114                         (intern (concat "mm-uu-" (symbol-name type)
115                                         "-end-line"))))
116         (when (re-search-forward end-line nil t)
117           (forward-line)
118           (setq end-char (point))
119           (when (or (not (eq type 'binhex))
120                     (setq file-name
121                           (ignore-errors
122                             (binhex-decode-region start-char end-char t))))
123             (if (> start-char text-start)
124                 (push
125                  (mm-make-handle (mm-uu-copy-to-buffer text-start start-char)
126                        text-plain-type cte)
127                  result))
128             (push
129              (cond
130               ((eq type 'postscript)
131                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
132                      '("application/postscript")))
133               ((eq type 'uu)
134                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
135                      (list (or (and file-name
136                                     (string-match "\\.[^\\.]+$" file-name)
137                                     (mailcap-extension-to-mime
138                                      (match-string 0 file-name)))
139                                "application/octet-stream"))
140                      'x-uuencode nil
141                      (if (and file-name (not (equal file-name "")))
142                          (list mm-dissect-disposition
143                                (cons 'filename file-name)))))
144               ((eq type 'binhex)
145                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
146                      (list (or (and file-name
147                                     (string-match "\\.[^\\.]+$" file-name)
148                                     (mailcap-extension-to-mime
149                                      (match-string 0 file-name)))
150                                "application/octet-stream"))
151                      'x-binhex nil
152                      (if (and file-name (not (equal file-name "")))
153                          (list mm-dissect-disposition
154                                (cons 'filename file-name)))))
155               ((eq type 'shar)
156                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
157                      '("application/x-shar"))))
158              result)
159             (setq text-start end-char))))
160       (when result
161         (if (> (point-max) (1+ text-start))
162             (push
163              (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
164                    text-plain-type cte)
165              result))
166         (setq result (cons "multipart/mixed" (nreverse result))))
167       result)))
168
169 ;;;### autoload
170 (defun mm-uu-test ()
171   "Check whether the current buffer contains uu stuffs."
172   (save-excursion
173     (save-restriction
174       (mail-narrow-to-head)
175       (goto-char (point-max)))
176     (forward-line)
177     (let (type end-line result)
178       (while (and (not result) (re-search-forward mm-uu-begin-line nil t))
179         (forward-line)
180         (setq type (cdr (assq (aref (match-string 0) 0)
181                               mm-uu-identifier-alist)))
182         (setq end-line (symbol-value
183                         (intern (concat "mm-uu-" (symbol-name type)
184                                         "-end-line"))))
185         (if (re-search-forward end-line nil t)
186             (setq result t)))
187       result)))
188
189 (provide 'mm-uu)
190
191 ;;; mm-uu.el ends here