010d3f762688cfbeab77c14ef964dc01d7368f6d
[elisp/gnus.git-] / lisp / mm-uu.el
1 ;;; mm-uu.el -- Return uu stuffs as mm handles
2 ;; Copyright (c) 1998,99 by Shenghuo Zhu
3
4 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
5 ;; Keywords: postscript uudecode binhex shar forward
6
7 ;; This file is part of pgnus.
8
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)
12 ;; any later version.
13 ;;
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.
18
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.
23
24 ;;; Commentary:
25
26
27 ;;; Code:
28
29 (require 'mail-parse)
30 (require 'message)
31 (require 'nnheader)
32 (require 'mm-decode)
33 (require 'mailcap)
34
35 (eval-and-compile
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"))
40
41 (defun mm-uu-copy-to-buffer (from to)
42   "Copy the contents of the current buffer to a fresh buffer."
43   (save-excursion
44     (let ((obuf (current-buffer)))
45       (set-buffer (generate-new-buffer " *mm-uu*"))
46       (insert-buffer-substring obuf from to)
47       (current-buffer))))
48
49 ;;; postscript
50
51 (defconst mm-uu-postscript-begin-line "^%!PS-")
52 (defconst mm-uu-postscript-end-line "^%%EOF$")
53
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)
57
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)
62
63 (defconst mm-uu-shar-begin-line "^#! */bin/sh")
64 (defconst mm-uu-shar-end-line "^exit 0")
65
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")
70
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))
77
78 (defconst mm-uu-identifier-alist
79   '((?% . postscript) (?b . uu) (?: . binhex) (?# . shar)
80     (?- . forward)))
81
82 (defvar mm-dissect-disposition "inline"
83   "The default disposition of uu parts.
84 This can be either \"inline\" or \"attachment\".")
85
86 ;;;### autoload
87
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
93            (case-fold-search t))
94     (save-excursion
95       (save-restriction
96         (mail-narrow-to-head)
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)))
102           (if (stringp cte)
103               (setq cte (intern (downcase (mail-header-remove-whitespace
104                                            (mail-header-remove-comments
105                                             cte)))))))
106         (goto-char (point-max)))
107       (forward-line)
108       (setq text-start (point)
109             text-plain-type (cons "text/plain"
110                                   (if charset
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)))
118         (setq file-name
119               (if (eq type 'uu)
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)
126                                         "-end-line"))))
127         (when (re-search-forward end-line nil t)
128           (setq end-char-1 (match-beginning 0))
129           (forward-line)
130           (setq end-char (point))
131           (when (or (not (eq type 'binhex))
132                     (setq file-name
133                           (ignore-errors
134                             (binhex-decode-region start-char end-char t))))
135             (if (> start-char text-start)
136                 (push
137                  (mm-make-handle (mm-uu-copy-to-buffer text-start start-char)
138                        text-plain-type cte)
139                  result))
140             (push
141              (cond
142               ((eq type 'postscript)
143                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
144                      '("application/postscript")))
145               ((eq type 'forward)
146                (mm-make-handle (mm-uu-copy-to-buffer start-char-1 end-char-1)
147                      '("message/rfc822")))
148               ((eq type 'uu)
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"))
155                      'x-uuencode nil
156                      (if (and file-name (not (equal file-name "")))
157                          (list mm-dissect-disposition
158                                (cons 'filename file-name)))))
159               ((eq type 'binhex)
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"))
166                      'x-binhex nil
167                      (if (and file-name (not (equal file-name "")))
168                          (list mm-dissect-disposition
169                                (cons 'filename file-name)))))
170               ((eq type 'shar)
171                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
172                      '("application/x-shar"))))
173              result)
174             (setq text-start end-char))))
175       (when result
176         (if (> (point-max) (1+ text-start))
177             (push
178              (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
179                    text-plain-type cte)
180              result))
181         (setq result (cons "multipart/mixed" (nreverse result))))
182       result)))
183
184 ;;;### autoload
185 (defun mm-uu-test ()
186   "Check whether the current buffer contains uu stuffs."
187   (save-excursion
188     (save-restriction
189       (mail-narrow-to-head)
190       (goto-char (point-max)))
191     (forward-line)
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))
195         (forward-line)
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)
200                                         "-end-line"))))
201         (if (re-search-forward end-line nil t)
202             (setq result t)))
203       result)))
204
205 (provide 'mm-uu)
206
207 ;;; mm-uu.el ends here