update.
[elisp/flim.git] / mel.el
1 ;;; mel.el --- A MIME encoding/decoding library.
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Created: 1995/6/25
7 ;; Keywords: MIME, Base64, Quoted-Printable, uuencode, gzip64
8
9 ;; This file is part of FLIM (Faithful Library about Internet Message).
10
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Code:
27
28 (require 'mime-def)
29 (require 'alist)
30
31 (defcustom mime-encoding-list
32   '("7bit" "8bit" "binary" "base64" "quoted-printable")
33   "List of Content-Transfer-Encoding.  Each encoding must be string."
34   :group 'mime
35   :type '(repeat string))
36
37 (defun mime-encoding-list (&optional service)
38   "Return list of Content-Transfer-Encoding.
39 If SERVICE is specified, it returns available list of
40 Content-Transfer-Encoding for it."
41   (if service
42       (let (dest)
43         (mapatoms (lambda (sym)
44                     (or (eq sym nil)
45                         (setq dest (cons (symbol-name sym) dest)))
46                     )
47                   (symbol-value (intern (format "%s-obarray" service))))
48         (let ((rest mel-encoding-module-alist)
49               pair)
50           (while (setq pair (car rest))
51             (let ((key (car pair)))
52               (or (member key dest)
53                   (<= (length key) 1)
54                   (setq dest (cons key dest))))
55             (setq rest (cdr rest)))
56           )
57         dest)
58     mime-encoding-list))
59
60 (defun mime-encoding-alist (&optional service)
61   "Return table of Content-Transfer-Encoding for completion."
62   (mapcar #'list (mime-encoding-list service)))
63
64 (defsubst mel-use-module (name encodings)
65   (while encodings
66     (set-alist 'mel-encoding-module-alist
67                (car encodings)
68                (cons name (cdr (assoc (car encodings)
69                                       mel-encoding-module-alist))))
70     (setq encodings (cdr encodings))))
71
72 (defsubst mel-find-function (service encoding)
73   (mel-find-function-from-obarray
74    (symbol-value (intern (format "%s-obarray" service))) encoding))
75
76
77 ;;; @ setting for modules
78 ;;;
79
80 (defun 8bit-insert-encoded-file (filename)
81   "Insert file FILENAME encoded by \"7bit\" format."
82   (let ((coding-system-for-read 'raw-text)
83         format-alist)
84     ;; Returns list of absolute file name and length of data inserted.
85     (insert-file-contents filename)))
86
87 (defun 8bit-write-decoded-region (start end filename)
88   "Decode and write current region encoded by \"8bit\" into FILENAME."
89   (let ((coding-system-for-write 'raw-text)
90         format-alist)
91     (write-region start end filename)))
92
93 (mel-define-backend "8bit")
94 (mel-define-method-function (mime-encode-string string (nil "8bit"))
95                             'identity)
96 (mel-define-method-function (mime-decode-string string (nil "8bit"))
97                             'identity)
98 (mel-define-method mime-encode-region (start end (nil "8bit")))
99 (mel-define-method mime-decode-region (start end (nil "8bit")))
100 (mel-define-method-function (mime-insert-encoded-file filename (nil "8bit"))
101                             '8bit-insert-encoded-file)
102 (mel-define-method-function (mime-write-decoded-region
103                              start end filename (nil "8bit"))
104                             '8bit-write-decoded-region)
105
106
107 (defalias '7bit-insert-encoded-file '8bit-insert-encoded-file)
108 (defalias '7bit-write-decoded-region '8bit-write-decoded-region)
109
110 (mel-define-backend "7bit" ("8bit"))
111
112
113 (defun binary-write-decoded-region (start end filename)
114   "Decode and write current region encoded by \"binary\" into FILENAME."
115   (let ((coding-system-for-write 'binary)
116         jka-compr-compression-info-list jam-zcat-filename-list)
117     (write-region start end filename)))
118
119 (defalias 'binary-insert-encoded-file 'insert-file-contents-literally)
120
121 (defun binary-find-file-noselect (filename &optional nowarn rawfile)
122   "Like `find-file-noselect', q.v., but don't code and format conversion."
123   (let ((coding-system-for-read 'binary)
124         format-alist)
125     (find-file-noselect filename nowarn rawfile)))
126
127 (defun binary-funcall (name &rest args)
128   "Like `funcall', q.v., but read and write as binary."
129   (let ((coding-system-for-read 'binary)
130         (coding-system-for-write 'binary))
131     (apply name args)))
132
133 (defun binary-to-text-funcall (coding-system name &rest args)
134   "Like `funcall', q.v., but write as binary and read as text.
135 Read text is decoded as CODING-SYSTEM."
136   (let ((coding-system-for-read coding-system)
137         (coding-system-for-write 'binary))
138     (apply name args)))
139
140 (mel-define-backend "binary")
141 (mel-define-method-function (mime-encode-string string (nil "binary"))
142                             'identity)
143 (mel-define-method-function (mime-decode-string string (nil "binary"))
144                             'identity)
145 (mel-define-method mime-encode-region (start end (nil "binary")))
146 (mel-define-method mime-decode-region (start end (nil "binary")))
147 (mel-define-method-function (mime-insert-encoded-file filename (nil "binary"))
148                             'binary-insert-encoded-file)
149 (mel-define-method-function (mime-write-decoded-region
150                              start end filename (nil "binary"))
151                             'binary-write-decoded-region)
152
153 (defvar mel-b-builtin
154    (and (fboundp 'base64-encode-string)
155         (subrp (symbol-function 'base64-encode-string))))
156
157 (when mel-b-builtin
158   (mel-define-backend "base64")
159   (mel-define-method-function (mime-encode-string string (nil "base64"))
160                               'base64-encode-string)
161   (mel-define-method-function (mime-decode-string string (nil "base64"))
162                               'base64-decode-string)
163   (mel-define-method-function (mime-encode-region start end (nil "base64"))
164                               'base64-encode-region)
165   (mel-define-method-function (mime-decode-region start end (nil "base64"))
166                               'base64-decode-region)  
167   (mel-define-method mime-insert-encoded-file (filename (nil "base64"))
168     "Encode contents of file FILENAME to base64, and insert the result.
169 It calls external base64 encoder specified by
170 `base64-external-encoder'.  So you must install the program (maybe
171 mmencode included in metamail or XEmacs package)."
172     (interactive "*fInsert encoded file: ")
173     (insert (base64-encode-string
174              (with-temp-buffer
175                (set-buffer-multibyte nil)
176                (binary-insert-encoded-file filename)
177                (buffer-string))))
178     (or (bolp) (insert ?\n)))
179   (mel-define-method mime-write-decoded-region (start end filename
180                                                       (nil "base64"))
181     "Decode the region from START to END and write out to FILENAME."
182     (interactive "*r\nFWrite decoded region to file: ")
183     (let ((str (buffer-substring start end)))
184       (with-temp-buffer
185         (insert str)
186         (base64-decode-region (point-min) (point-max))
187         (write-region-as-binary (point-min) (point-max) filename))))
188     
189   ;; (mel-define-method-function (encoded-text-encode-string string (nil "B"))
190   ;;                             'base64-encode-string)
191   (mel-define-method encoded-text-decode-string (string (nil "B"))
192     (if (string-match (eval-when-compile
193                         (concat "\\`" B-encoded-text-regexp "\\'"))
194                       string)
195         (base64-decode-string string)
196       (error "Invalid encoded-text %s" string)))
197   )
198
199 (mel-use-module 'mel-b-el '("base64" "B"))
200 (mel-use-module 'mel-q '("quoted-printable" "Q"))
201 (mel-use-module 'mel-g '("x-gzip64"))
202 (mel-use-module 'mel-u '("x-uue" "x-uuencode"))
203
204 (defvar mel-b-ccl-module
205   (and (featurep 'mule)
206        (progn
207          (require 'path-util)
208          (module-installed-p 'mel-b-ccl))))
209
210 (defvar mel-q-ccl-module
211   (and (featurep 'mule)
212        (progn
213          (require 'path-util)
214          (module-installed-p 'mel-q-ccl))))
215
216 (when mel-b-ccl-module
217   (mel-use-module 'mel-b-ccl '("base64" "B")))
218
219 (when mel-q-ccl-module
220   (mel-use-module 'mel-q-ccl '("quoted-printable" "Q")))
221
222 (when base64-dl-module
223   (mel-use-module 'mel-b-dl '("base64" "B")))
224
225
226 ;;; @ region
227 ;;;
228
229 ;;;###autoload
230 (defun mime-encode-region (start end encoding)
231   "Encode region START to END of current buffer using ENCODING.
232 ENCODING must be string."
233   (interactive
234    (list (region-beginning)(region-end)
235          (completing-read "Encoding: "
236                           (mime-encoding-alist)
237                           nil t "base64")))
238   (funcall (mel-find-function 'mime-encode-region encoding) start end))
239
240
241 ;;;###autoload
242 (defun mime-decode-region (start end encoding)
243   "Decode region START to END of current buffer using ENCODING.
244 ENCODING must be string."
245   (interactive
246    (list (region-beginning)(region-end)
247          (completing-read "Encoding: "
248                           (mime-encoding-alist 'mime-decode-region)
249                           nil t "base64")))
250   (funcall (mel-find-function 'mime-decode-region encoding)
251            start end))
252
253
254 ;;; @ string
255 ;;;
256
257 ;;;###autoload
258 (defun mime-decode-string (string encoding)
259   "Decode STRING using ENCODING.
260 ENCODING must be string.  If ENCODING is found in
261 `mime-string-decoding-method-alist' as its key, this function decodes
262 the STRING by its value."
263   (let ((f (mel-find-function 'mime-decode-string encoding)))
264     (if f
265         (funcall f string)
266       string)))
267
268
269 (mel-define-service encoded-text-encode-string)
270 (defun encoded-text-encode-string (string encoding &optional mode)
271   "Encode STRING as encoded-text using ENCODING.
272 ENCODING must be string.
273 Optional argument MODE allows `text', `comment', `phrase' or nil.
274 Default value is `phrase'."
275   (if (string= encoding "B")
276       (base64-encode-string string 'no-line-break)
277     (let ((f (mel-find-function 'encoded-text-encode-string encoding)))
278       (if f
279           (funcall f string mode)
280         string))))
281
282 (mel-define-service encoded-text-decode-string (string encoding)
283   "Decode STRING as encoded-text using ENCODING.  ENCODING must be string.")
284
285 (defun base64-encoded-length (string)
286   (* (/ (+ (length string) 2) 3) 4))
287
288 (defsubst Q-encoding-printable-char-p (chr mode)
289   (and (not (memq chr '(?= ?? ?_)))
290        (<= ?\  chr)(<= chr ?~)
291        (cond ((eq mode 'text) t)
292              ((eq mode 'comment)
293               (not (memq chr '(?\( ?\) ?\\))))
294              (t
295               (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))))))
296
297 (defun Q-encoded-text-length (string &optional mode)
298   (let ((l 0)(i 0)(len (length string)) chr)
299     (while (< i len)
300       (setq chr (aref string i))
301       (if (or (Q-encoding-printable-char-p chr mode)
302               (eq chr ? ))
303           (setq l (+ l 1))
304         (setq l (+ l 3)))
305       (setq i (+ i 1)))
306     l))
307
308
309 ;;; @ file
310 ;;;
311
312 ;;;###autoload
313 (defun mime-insert-encoded-file (filename encoding)
314   "Insert file FILENAME encoded by ENCODING format."
315   (interactive
316    (list (read-file-name "Insert encoded file: ")
317          (completing-read "Encoding: "
318                           (mime-encoding-alist)
319                           nil t "base64")))
320   (funcall (mel-find-function 'mime-insert-encoded-file encoding)
321            filename))
322
323
324 ;;;###autoload
325 (defun mime-write-decoded-region (start end filename encoding)
326   "Decode and write current region encoded by ENCODING into FILENAME.
327 START and END are buffer positions."
328   (interactive
329    (list (region-beginning)(region-end)
330          (read-file-name "Write decoded region to file: ")
331          (completing-read "Encoding: "
332                           (mime-encoding-alist 'mime-write-decoded-region)
333                           nil t "base64")))
334   (funcall (mel-find-function 'mime-write-decoded-region encoding)
335            start end filename))
336
337
338 ;;; @ end
339 ;;;
340
341 (provide 'mel)
342
343 ;;; mel.el ends here.