Importing Pterodactyl Gnus v0.96.
[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
57 (defcustom mm-uu-decode-function 'uudecode-decode-region
58   "*Function to uudecode.
59 Internal function is done in elisp by default, therefore decoding may
60 appear to be horribly slow . You can make Gnus use the external Unix
61 decoder, such as uudecode."
62   :type '(choice (item :tag "internal" uudecode-decode-region)
63                  (item :tag "external" uudecode-decode-region-external))
64   :group 'gnus-article-mime) 
65
66 (defconst mm-uu-binhex-begin-line
67   "^:...............................................................$")
68 (defconst mm-uu-binhex-end-line ":$")
69
70 (defcustom mm-uu-binhex-decode-function 'binhex-decode-region
71   "*Function to binhex decode.
72 Internal function is done in elisp by default, therefore decoding may
73 appear to be horribly slow . You can make Gnus use the external Unix
74 decoder, such as hexbin."
75   :type '(choice (item :tag "internal" binhex-decode-region)
76                  (item :tag "external" binhex-decode-region-external))
77   :group 'gnus-article-mime) 
78
79 (defconst mm-uu-shar-begin-line "^#! */bin/sh")
80 (defconst mm-uu-shar-end-line "^exit 0\\|^$")
81
82 ;;; Thanks to Edward J. Sabol <sabol@alderaan.gsfc.nasa.gov> and 
83 ;;; Peter von der Ah\'e <pahe@daimi.au.dk>
84 (defconst mm-uu-forward-begin-line "^-+ \\(?:Start of \\)?Forwarded message")
85 (defconst mm-uu-forward-end-line "^-+ End\\(?: of\\)? forwarded message")
86
87 (defvar mm-uu-begin-line nil)
88
89 (defconst mm-uu-identifier-alist
90   '((?% . postscript) (?b . uu) (?: . binhex) (?# . shar)
91     (?- . forward)))
92
93 (defvar mm-dissect-disposition "inline"
94   "The default disposition of uu parts.
95 This can be either \"inline\" or \"attachment\".")
96
97 (defun mm-uu-configure-p  (key val)
98   (member (cons key val) mm-uu-configure-list))
99
100 (defun mm-uu-configure (&optional symbol value)
101   (if symbol (set-default symbol value))
102   (setq mm-uu-begin-line nil)
103   (mapcar '(lambda (type)
104              (if (mm-uu-configure-p type 'disabled) 
105                  nil
106                (setq mm-uu-begin-line
107                      (concat mm-uu-begin-line
108                              (if mm-uu-begin-line "\\|")
109                              (symbol-value
110                               (intern (concat "mm-uu-" (symbol-name type)
111                                               "-begin-line")))))))
112           '(uu postscript binhex shar forward)))
113
114 (defcustom mm-uu-configure-list nil
115   "A list of mm-uu configuration.
116 To disable dissecting shar codes, for instance, add
117 `(shar . disabled)' to this list."
118   :type '(repeat (cons 
119                   (choice (item postscript)
120                           (item uu) 
121                           (item binhex)
122                           (item shar)
123                           (item forward))
124                   (choice (item disabled))))
125   :group 'gnus-article-mime
126   :set 'mm-uu-configure) 
127
128 (mm-uu-configure)
129
130 ;;;### autoload
131
132 (defun mm-uu-dissect ()
133   "Dissect the current buffer and return a list of uu handles."
134   (let (ct ctl cte charset text-start start-char end-char
135            type file-name end-line result text-plain-type 
136            start-char-1 end-char-1
137            (case-fold-search t))
138     (save-excursion
139       (save-restriction
140         (mail-narrow-to-head)
141         (when (and (mail-fetch-field "mime-version")
142                    (setq ct (mail-fetch-field "content-type")))
143           (setq cte (message-fetch-field "content-transfer-encoding" t)
144                 ctl (ignore-errors (mail-header-parse-content-type ct))
145                 charset (and ctl (mail-content-type-get ctl 'charset)))
146           (if (stringp cte)
147               (setq cte (intern (downcase (mail-header-remove-whitespace
148                                            (mail-header-remove-comments
149                                             cte))))))
150           (if (eq cte 'base64)
151               (setq charset 'gnus-encoded ;; a fake charset
152                     cte nil)))
153         (goto-char (point-max)))
154       (forward-line)
155       (setq text-start (point)
156             text-plain-type (cons "text/plain"
157                                   (if charset
158                                       (list (cons 'charset charset)))))
159       (while (re-search-forward mm-uu-begin-line nil t)
160         (setq start-char (match-beginning 0))
161         (setq type (cdr (assq (aref (match-string 0) 0)
162                               mm-uu-identifier-alist)))
163         (setq file-name
164               (if (and (eq type 'uu)
165                        (looking-at "\\(.+\\)$"))
166                   (and (match-string 1)
167                        (let ((nnheader-file-name-translation-alist
168                               '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
169                          (nnheader-translate-file-chars (match-string 1))))))
170         (forward-line) ;; in case of failure
171         (setq start-char-1 (point))
172         (setq end-line (symbol-value
173                         (intern (concat "mm-uu-" (symbol-name type)
174                                         "-end-line"))))
175         (when (and (re-search-forward end-line nil t)
176                    (not (eq (match-beginning 0) (match-end 0)))
177                    ;; Do not dissect base64 forward. 
178                    (not (and (eq charset 'gnus-encoded) (eq type 'forward))))
179           (setq end-char-1 (match-beginning 0))
180           (forward-line)
181           (setq end-char (point))
182           (when (or (not (eq type 'binhex))
183                     (setq file-name
184                           (ignore-errors
185                             (binhex-decode-region start-char end-char t))))
186             (if (> start-char text-start)
187                 (push
188                  (mm-make-handle (mm-uu-copy-to-buffer text-start start-char)
189                        text-plain-type cte)
190                  result))
191             (push
192              (cond
193               ((eq type 'postscript)
194                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
195                      '("application/postscript")))
196               ((eq type 'forward)
197                (mm-make-handle (mm-uu-copy-to-buffer start-char-1 end-char-1)
198                      '("message/rfc822")))
199               ((eq type 'uu)
200                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
201                      (list (or (and file-name
202                                     (string-match "\\.[^\\.]+$" file-name)
203                                     (mailcap-extension-to-mime
204                                      (match-string 0 file-name)))
205                                "application/octet-stream"))
206                      'x-uuencode nil
207                      (if (and file-name (not (equal file-name "")))
208                          (list mm-dissect-disposition
209                                (cons 'filename file-name)))))
210               ((eq type 'binhex)
211                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
212                      (list (or (and file-name
213                                     (string-match "\\.[^\\.]+$" file-name)
214                                     (mailcap-extension-to-mime
215                                      (match-string 0 file-name)))
216                                "application/octet-stream"))
217                      'x-binhex nil
218                      (if (and file-name (not (equal file-name "")))
219                          (list mm-dissect-disposition
220                                (cons 'filename file-name)))))
221               ((eq type 'shar)
222                (mm-make-handle (mm-uu-copy-to-buffer start-char end-char)
223                      '("application/x-shar"))))
224              result)
225             (setq text-start end-char))))
226       (when result
227         (if (> (point-max) (1+ text-start))
228             (push
229              (mm-make-handle (mm-uu-copy-to-buffer text-start (point-max))
230                    text-plain-type cte)
231              result))
232         (setq result (cons "multipart/mixed" (nreverse result))))
233       result)))
234
235 ;;;### autoload
236 (defun mm-uu-test ()
237   "Check whether the current buffer contains uu stuffs."
238   (save-excursion
239     (save-restriction
240       (mail-narrow-to-head)
241       (goto-char (point-max)))
242     (forward-line)
243     (let (type end-line result
244                (case-fold-search t))
245       (while (and mm-uu-begin-line
246                   (not result) (re-search-forward mm-uu-begin-line nil t))
247         (forward-line)
248         (setq type (cdr (assq (aref (match-string 0) 0)
249                               mm-uu-identifier-alist)))
250         (setq end-line (symbol-value
251                         (intern (concat "mm-uu-" (symbol-name type)
252                                         "-end-line"))))
253         (if (and (re-search-forward end-line nil t)
254                  (not (eq (match-beginning 0) (match-end 0))))
255             (setq result t)))
256       result)))
257
258 (provide 'mm-uu)
259
260 ;;; mm-uu.el ends here