(mime-content-disposition-parameter): Don't strip quoted-string.
[elisp/flim.git] / mel.el
1 ;;; mel.el : a MIME encoding/decoding library
2
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; modified by Shuhei KOBAYASHI <shuhei-k@jaist.ac.jp>
7 ;; Created: 1995/6/25
8 ;; Keywords: MIME, Base64, Quoted-Printable, uuencode, gzip64
9
10 ;; This file is part of MEL (MIME Encoding Library).
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Code:
28
29 (require 'emu)
30
31
32 ;;; @ variable
33 ;;;
34
35 (defvar base64-dl-module
36   (and (fboundp 'dynamic-link)
37        (let ((path (expand-file-name "base64.so" exec-directory)))
38          (and (file-exists-p path)
39               path))))
40
41
42 ;;; @ autoload
43 ;;;
44
45 (cond (base64-dl-module
46        (autoload 'base64-encode-string "mel-dl"
47          "Encode STRING to base64, and return the result.")
48        (autoload 'base64-decode-string "mel-dl"
49          "Decode STRING which is encoded in base64, and return the result.")
50        (autoload 'base64-encode-region "mel-dl"
51          "Encode current region by base64." t)
52        (autoload 'base64-decode-region "mel-dl"
53          "Decode current region by base64." t)
54        (autoload 'base64-insert-encoded-file "mel-dl"
55          "Encode contents of file to base64, and insert the result." t)
56        (autoload 'base64-write-decoded-region "mel-dl"
57          "Decode and write current region encoded by base64 into FILENAME." t)
58        ;; for encoded-word
59        (autoload 'base64-encoded-length "mel-dl")
60        )
61       (t
62        (autoload 'base64-encode-string "mel-b"
63          "Encode STRING to base64, and return the result.")
64        (autoload 'base64-decode-string "mel-b"
65          "Decode STRING which is encoded in base64, and return the result.")
66        (autoload 'base64-encode-region "mel-b"
67          "Encode current region by base64." t)
68        (autoload 'base64-decode-region "mel-b"
69          "Decode current region by base64." t)
70        (autoload 'base64-insert-encoded-file "mel-b"
71          "Encode contents of file to base64, and insert the result." t)
72        (autoload 'base64-write-decoded-region "mel-b"
73          "Decode and write current region encoded by base64 into FILENAME." t)
74        ;; for encoded-word
75        (autoload 'base64-encoded-length "mel-b")
76        ))
77
78 (autoload 'quoted-printable-encode-string "mel-q"
79   "Encode STRING to quoted-printable, and return the result.")
80 (autoload 'quoted-printable-decode-string "mel-q"
81   "Decode STRING which is encoded in quoted-printable, and return the result.")
82 (autoload 'quoted-printable-encode-region "mel-q"
83   "Encode current region by Quoted-Printable." t)
84 (autoload 'quoted-printable-decode-region "mel-q"
85   "Decode current region by Quoted-Printable." t)
86 (autoload 'quoted-printable-insert-encoded-file "mel-q"
87   "Encode contents of file to quoted-printable, and insert the result." t)
88 (autoload 'quoted-printable-write-decoded-region "mel-q"
89   "Decode and write current region encoded by quoted-printable into FILENAME."
90   t)
91 ;; for encoded-word
92 (autoload 'q-encoding-encode-string "mel-q"
93   "Encode STRING to Q-encoding of encoded-word, and return the result.")
94 (autoload 'q-encoding-decode-string "mel-q"
95   "Decode STRING which is encoded in Q-encoding and return the result.")
96 (autoload 'q-encoding-encoded-length "mel-q")
97
98 (autoload 'uuencode-encode-region "mel-u"
99   "Encode current region by unofficial uuencode format." t)
100 (autoload 'uuencode-decode-region "mel-u"
101   "Decode current region by unofficial uuencode format." t)
102 (autoload 'uuencode-insert-encoded-file "mel-u"
103   "Insert file encoded by unofficial uuencode format." t)
104 (autoload 'uuencode-write-decoded-region "mel-u"
105   "Decode and write current region encoded by uuencode into FILENAME." t)
106
107 (autoload 'gzip64-encode-region "mel-g"
108   "Encode current region by unofficial x-gzip64 format." t)
109 (autoload 'gzip64-decode-region "mel-g"
110   "Decode current region by unofficial x-gzip64 format." t)
111 (autoload 'gzip64-insert-encoded-file "mel-g"
112   "Insert file encoded by unofficial gzip64 format." t)
113 (autoload 'gzip64-write-decoded-region "mel-g"
114   "Decode and write current region encoded by gzip64 into FILENAME." t)
115
116
117 ;;; @ region
118 ;;;
119
120 ;;;###autoload
121 (defvar mime-encoding-method-alist
122   '(("base64"           . base64-encode-region)
123     ("quoted-printable" . quoted-printable-encode-region)
124     ;; Not standard, their use is DISCOURAGED.
125     ;; ("x-uue"            . uuencode-encode-region)
126     ;; ("x-gzip64"         . gzip64-encode-region)
127     ("7bit")
128     ("8bit")
129     ("binary")
130     )
131   "Alist of encoding vs. corresponding method to encode region.
132 Each element looks like (STRING . FUNCTION) or (STRING . nil).
133 STRING is content-transfer-encoding.
134 FUNCTION is region encoder and nil means not to encode.")
135
136 ;;;###autoload
137 (defvar mime-decoding-method-alist
138   '(("base64"           . base64-decode-region)
139     ("quoted-printable" . quoted-printable-decode-region)
140     ("x-uue"            . uuencode-decode-region)
141     ("x-uuencode"       . uuencode-decode-region)
142     ("x-gzip64"         . gzip64-decode-region)
143     )
144   "Alist of encoding vs. corresponding method to decode region.
145 Each element looks like (STRING . FUNCTION).
146 STRING is content-transfer-encoding.
147 FUNCTION is region decoder.")
148
149 ;;;###autoload
150 (defvar mime-string-decoding-method-alist
151   '(("base64"           . base64-decode-string)
152     ("quoted-printable" . quoted-printable-decode-string)
153     ("7bit"             . identity)
154     ("8bit"             . identity)
155     ("binary"           . identity)
156     )
157   "Alist of encoding vs. corresponding method to decode string.
158 Each element looks like (STRING . FUNCTION).
159 STRING is content-transfer-encoding.
160 FUNCTION is string decoder.")
161
162 ;;;###autoload
163 (defun mime-encode-region (start end encoding)
164   "Encode region START to END of current buffer using ENCODING.
165 ENCODING must be string.  If ENCODING is found in
166 `mime-encoding-method-alist' as its key, this function encodes the
167 region by its value."
168   (interactive
169    (list (region-beginning) (region-end)
170          (completing-read "encoding: "
171                           mime-encoding-method-alist
172                           nil t "base64"))
173    )
174   (let ((f (cdr (assoc encoding mime-encoding-method-alist))))
175     (if f
176         (funcall f start end)
177       )))
178
179 ;;;###autoload
180 (defun mime-decode-region (start end encoding)
181   "Decode region START to END of current buffer using ENCODING.
182 ENCODING must be string.  If ENCODING is found in
183 `mime-decoding-method-alist' as its key, this function decodes the
184 region by its value."
185   (interactive
186    (list (region-beginning) (region-end)
187          (completing-read "encoding: "
188                           mime-decoding-method-alist
189                           nil t "base64"))
190    )
191   (let ((f (cdr (assoc encoding mime-decoding-method-alist))))
192     (if f
193         (funcall f start end)
194       )))
195
196 ;;;###autoload
197 (defun mime-decode-string (string encoding)
198   "Decode STRING using ENCODING.
199 ENCODING must be string.  If ENCODING is found in
200 `mime-string-decoding-method-alist' as its key, this function decodes
201 the STRING by its value."
202   (let ((f (cdr (assoc encoding mime-string-decoding-method-alist))))
203     (if f
204         (funcall f string)
205       (with-temp-buffer
206         (insert string)
207         (mime-decode-region (point-min)(point-max) encoding)
208         (buffer-string)
209         ))))
210
211
212 ;;; @ file
213 ;;;
214
215 ;;;###autoload
216 (defvar mime-file-encoding-method-alist
217   '(("base64"           . base64-insert-encoded-file)
218     ("quoted-printable" . quoted-printable-insert-encoded-file)
219     ;; Not standard, their use is DISCOURAGED.
220     ;; ("x-uue"            . uuencode-insert-encoded-file)
221     ;; ("x-gzip64"         . gzip64-insert-encoded-file)
222     ("7bit"             . insert-file-contents-as-binary)
223     ("8bit"             . insert-file-contents-as-binary)
224     ("binary"           . insert-file-contents-as-binary)
225     )
226   "Alist of encoding vs. corresponding method to insert encoded file.
227 Each element looks like (STRING . FUNCTION).
228 STRING is content-transfer-encoding.
229 FUNCTION is function to insert encoded file.")
230
231 ;;;###autoload
232 (defvar mime-file-decoding-method-alist
233   '(("base64"           . base64-write-decoded-region)
234     ("quoted-printable" . quoted-printable-write-decoded-region)
235     ("x-uue"            . uuencode-write-decoded-region)
236     ("x-gzip64"         . gzip64-write-decoded-region)
237     ("7bit"             . write-region-as-binary)
238     ("8bit"             . write-region-as-binary)
239     ("binary"           . write-region-as-binary)
240     )
241   "Alist of encoding vs. corresponding method to write decoded region to file.
242 Each element looks like (STRING . FUNCTION).
243 STRING is content-transfer-encoding.
244 FUNCTION is function to write decoded region to file.")
245
246 ;;;###autoload
247 (defun mime-insert-encoded-file (filename encoding)
248   "Insert file FILENAME encoded by ENCODING format."
249   (interactive
250    (list (read-file-name "Insert encoded file: ")
251          (completing-read "encoding: "
252                           mime-encoding-method-alist
253                           nil t "base64"))
254    )
255   (let ((f (cdr (assoc encoding mime-file-encoding-method-alist))))
256     (if f
257         (funcall f filename)
258       )))
259
260 ;;;###autoload
261 (defun mime-write-decoded-region (start end filename encoding)
262   "Decode and write current region encoded by ENCODING into FILENAME.
263 START and END are buffer positions."
264   (interactive
265    (list (region-beginning) (region-end)
266          (read-file-name "Write decoded region to file: ")
267          (completing-read "encoding: "
268                           mime-file-decoding-method-alist
269                           nil t "base64")))
270   (let ((f (cdr (assoc encoding mime-file-decoding-method-alist))))
271     (if f
272         (funcall f start end filename)
273       )))
274
275
276 ;;; @ end
277 ;;;
278
279 (provide 'mel)
280
281 ;;; mel.el ends here.