]> code.delx.au - gnu-emacs/blob - lisp/gnus/uudecode.el
(message-tokenize-header, message-send-mail-with-qmail):
[gnu-emacs] / lisp / gnus / uudecode.el
1 ;;; uudecode.el -- elisp native uudecode
2
3 ;; Copyright (c) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
4
5 ;; Author: Shenghuo Zhu <zsh@cs.rochester.edu>
6 ;; Keywords: uudecode news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (autoload 'executable-find "executable")
30
31 (eval-when-compile (require 'cl))
32
33 (eval-and-compile
34 (defalias 'uudecode-char-int
35 (if (fboundp 'char-int)
36 'char-int
37 'identity)))
38
39 (defcustom uudecode-decoder-program "uudecode"
40 "*Non-nil value should be a string that names a uu decoder.
41 The program should expect to read uu data on its standard
42 input and write the converted data to its standard output."
43 :type 'string
44 :group 'gnus-extract)
45
46 (defcustom uudecode-decoder-switches nil
47 "*List of command line flags passed to `uudecode-decoder-program'."
48 :group 'gnus-extract
49 :type '(repeat string))
50
51 (defcustom uudecode-use-external
52 (executable-find uudecode-decoder-program)
53 "*Use external uudecode program."
54 :group 'gnus-extract
55 :type 'boolean)
56
57 (defconst uudecode-alphabet "\040-\140")
58
59 (defconst uudecode-begin-line "^begin[ \t]+[0-7][0-7][0-7][ \t]+\\(.*\\)$")
60 (defconst uudecode-end-line "^end[ \t]*$")
61
62 (defconst uudecode-body-line
63 (let ((i 61) (str "^M"))
64 (while (> (setq i (1- i)) 0)
65 (setq str (concat str "[^a-z]")))
66 (concat str ".?$")))
67
68 (defvar uudecode-temporary-file-directory
69 (cond ((fboundp 'temp-directory) (temp-directory))
70 ((boundp 'temporary-file-directory) temporary-file-directory)
71 ("/tmp")))
72
73 ;;;###autoload
74 (defun uudecode-decode-region-external (start end &optional file-name)
75 "Uudecode region between START and END using external program.
76 If FILE-NAME is non-nil, save the result to FILE-NAME. The program
77 used is specified by `uudecode-decoder-program'."
78 (interactive "r\nP")
79 (let ((cbuf (current-buffer)) tempfile firstline status)
80 (save-excursion
81 (goto-char start)
82 (when (re-search-forward uudecode-begin-line nil t)
83 (forward-line 1)
84 (setq firstline (point))
85 (cond ((null file-name))
86 ((stringp file-name))
87 (t
88 (setq file-name (read-file-name "File to Name:"
89 nil nil nil
90 (match-string 1)))))
91 (setq tempfile (if file-name
92 (expand-file-name file-name)
93 (if (fboundp 'make-temp-file)
94 (let ((temporary-file-directory
95 uudecode-temporary-file-directory))
96 (make-temp-file "uu"))
97 (expand-file-name
98 (make-temp-name "uu")
99 uudecode-temporary-file-directory))))
100 (let ((cdir default-directory)
101 default-process-coding-system)
102 (unwind-protect
103 (with-temp-buffer
104 (insert "begin 600 " (file-name-nondirectory tempfile) "\n")
105 (insert-buffer-substring cbuf firstline end)
106 (cd (file-name-directory tempfile))
107 (apply 'call-process-region
108 (point-min)
109 (point-max)
110 uudecode-decoder-program
111 nil
112 nil
113 nil
114 uudecode-decoder-switches))
115 (cd cdir) (set-buffer cbuf)))
116 (if (file-exists-p tempfile)
117 (unless file-name
118 (goto-char start)
119 (delete-region start end)
120 (let (format-alist)
121 (insert-file-contents-literally tempfile)))
122 (message "Can not uudecode")))
123 (ignore-errors (or file-name (delete-file tempfile))))))
124
125 ;;;###autoload
126 (defun uudecode-decode-region-internal (start end &optional file-name)
127 "Uudecode region between START and END without using an external program.
128 If FILE-NAME is non-nil, save the result to FILE-NAME."
129 (interactive "r\nP")
130 (let ((done nil)
131 (counter 0)
132 (remain 0)
133 (bits 0)
134 (lim 0) inputpos result
135 (non-data-chars (concat "^" uudecode-alphabet)))
136 (save-excursion
137 (goto-char start)
138 (when (re-search-forward uudecode-begin-line nil t)
139 (cond ((null file-name))
140 ((stringp file-name))
141 (t
142 (setq file-name (expand-file-name
143 (read-file-name "File to Name:"
144 nil nil nil
145 (match-string 1))))))
146 (forward-line 1)
147 (skip-chars-forward non-data-chars end)
148 (while (not done)
149 (setq inputpos (point))
150 (setq remain 0 bits 0 counter 0)
151 (cond
152 ((> (skip-chars-forward uudecode-alphabet end) 0)
153 (setq lim (point))
154 (setq remain
155 (logand (- (uudecode-char-int (char-after inputpos)) 32)
156 63))
157 (setq inputpos (1+ inputpos))
158 (if (= remain 0) (setq done t))
159 (while (and (< inputpos lim) (> remain 0))
160 (setq bits (+ bits
161 (logand
162 (-
163 (uudecode-char-int (char-after inputpos)) 32)
164 63)))
165 (if (/= counter 0) (setq remain (1- remain)))
166 (setq counter (1+ counter)
167 inputpos (1+ inputpos))
168 (cond ((= counter 4)
169 (setq result (cons
170 (concat
171 (char-to-string (lsh bits -16))
172 (char-to-string (logand (lsh bits -8) 255))
173 (char-to-string (logand bits 255)))
174 result))
175 (setq bits 0 counter 0))
176 (t (setq bits (lsh bits 6)))))))
177 (cond
178 (done)
179 ((> 0 remain)
180 (error "uucode line ends unexpectly")
181 (setq done t))
182 ((and (= (point) end) (not done))
183 ;;(error "uucode ends unexpectly")
184 (setq done t))
185 ((= counter 3)
186 (setq result (cons
187 (concat
188 (char-to-string (logand (lsh bits -16) 255))
189 (char-to-string (logand (lsh bits -8) 255)))
190 result)))
191 ((= counter 2)
192 (setq result (cons
193 (char-to-string (logand (lsh bits -10) 255))
194 result))))
195 (skip-chars-forward non-data-chars end))
196 (if file-name
197 (let (default-enable-multibyte-characters)
198 (with-temp-file file-name
199 (insert (apply 'concat (nreverse result)))))
200 (or (markerp end) (setq end (set-marker (make-marker) end)))
201 (goto-char start)
202 (insert (apply 'concat (nreverse result)))
203 (delete-region (point) end))))))
204
205 ;;;###autoload
206 (defun uudecode-decode-region (start end &optional file-name)
207 "Uudecode region between START and END.
208 If FILE-NAME is non-nil, save the result to FILE-NAME."
209 (if uudecode-use-external
210 (uudecode-decode-region-external start end file-name)
211 (uudecode-decode-region-internal start end file-name)))
212
213 (provide 'uudecode)
214
215 ;;; arch-tag: e1f09ed5-62b4-4677-9f13-4e81c4fe8ce3
216 ;;; uudecode.el ends here