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