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