Importing pgnus-0.39
[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.1.1 $
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 ;;;### autoload
72
73 (defun mm-uu-dissect ()
74   "Dissect the current buffer and return a list of uu handles."
75   (save-excursion
76     (save-restriction
77       (mail-narrow-to-head)
78       (goto-char (point-max)))
79     (forward-line)
80     (let ((text-start (point)) start-char end-char 
81           type file-name end-line result)
82       (while (re-search-forward mm-uu-begin-line nil t)
83         (beginning-of-line)
84         (setq start-char (point))
85         (forward-line) ;; in case of failure
86         (setq type (cdr (assq (aref (match-string 0) 0) 
87                               mm-uu-identifier-alist)))
88         (setq file-name 
89               (if (eq type 'uu)
90                   (and (match-string 1)
91                        (let ((nnheader-file-name-translation-alist
92                               '((?/ . ?,) (? . ?_) (?* . ?_) (?$ . ?_))))
93                          (nnheader-translate-file-chars (match-string 1))))))
94         (setq end-line (symbol-value 
95                         (intern (concat "mm-uu-" (symbol-name type) 
96                                        "-end-line"))))
97         (when (re-search-forward end-line nil t)
98           (forward-line)
99           (setq end-char (point))
100           (when (or (not (eq type 'binhex))
101                     (setq file-name 
102                           (condition-case nil
103                               (binhex-decode-region start-char end-char t)
104                             (error nil))))
105             (if (> start-char text-start)
106                 (push
107                  (list (mm-uu-copy-to-buffer text-start start-char) 
108                        '("text/plain") nil nil nil nil) 
109                  result))
110             (push 
111              (cond
112               ((eq type 'postscript)
113                (list (mm-uu-copy-to-buffer start-char end-char) 
114                      '("application/postscript") nil nil nil nil))
115               ((eq type 'uu)
116                (list (mm-uu-copy-to-buffer start-char end-char) 
117                      (list (or (and file-name
118                                     (string-match "\\.[^\\.]+$" file-name) 
119                                     (mailcap-extension-to-mime 
120                                      (match-string 0 file-name)))
121                                "application/octet-stream"))
122                      mm-uu-decode-function nil 
123                      (if (and file-name (not (equal file-name "")))
124                          (list "attachment" (cons 'filename file-name)))
125                    file-name))
126               ((eq type 'binhex)
127                (list (mm-uu-copy-to-buffer start-char end-char) 
128                      (list (or (and file-name
129                                     (string-match "\\.[^\\.]+$" file-name) 
130                                     (mailcap-extension-to-mime 
131                                      (match-string 0 file-name)))
132                                "application/octet-stream"))
133                      mm-uu-binhex-decode-function nil 
134                      (if (and file-name (not (equal file-name "")))
135                          (list "attachment" (cons 'filename file-name)))
136                      file-name))
137               ((eq type 'shar)
138                (list (mm-uu-copy-to-buffer start-char end-char) 
139                      '("application/x-shar") nil nil nil nil))) 
140              result)
141             (setq text-start end-char))))
142       (when result
143         (if (> (point-max) (1+ text-start))
144             (push
145              (list (mm-uu-copy-to-buffer text-start (point-max)) 
146                    '("text/plain") nil nil nil nil) 
147              result))
148         (setq result (cons "multipart/mixed" (nreverse result))))
149       result)))
150
151 (provide 'mm-uu)
152
153 ;;; mm-uu.el ends here