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