Update FSF's address in GPL notices.
[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     
180   ;; (mel-define-method-function (encoded-text-encode-string string (nil "B"))
181   ;;                             'base64-encode-string)
182   (mel-define-method encoded-text-decode-string (string (nil "B"))
183     (if (string-match (eval-when-compile
184                         (concat "\\`" B-encoded-text-regexp "\\'"))
185                       string)
186         (base64-decode-string string)
187       (error "Invalid encoded-text %s" string)))
188   )
189
190 (mel-use-module 'mel-b-el '("base64" "B"))
191 (mel-use-module 'mel-q '("quoted-printable" "Q"))
192 (mel-use-module 'mel-g '("x-gzip64"))
193 (mel-use-module 'mel-u '("x-uue" "x-uuencode"))
194
195 (defvar mel-b-ccl-module
196   (and (featurep 'mule)
197        (progn
198          (require 'path-util)
199          (module-installed-p 'mel-b-ccl))))
200
201 (defvar mel-q-ccl-module
202   (and (featurep 'mule)
203        (progn
204          (require 'path-util)
205          (module-installed-p 'mel-q-ccl))))
206
207 (when mel-b-ccl-module
208   (mel-use-module 'mel-b-ccl '("base64" "B")))
209
210 (when mel-q-ccl-module
211   (mel-use-module 'mel-q-ccl '("quoted-printable" "Q")))
212
213 (when base64-dl-module
214   (mel-use-module 'mel-b-dl '("base64" "B")))
215
216
217 ;;; @ region
218 ;;;
219
220 ;;;###autoload
221 (defun mime-encode-region (start end encoding)
222   "Encode region START to END of current buffer using ENCODING.
223 ENCODING must be string."
224   (interactive
225    (list (region-beginning)(region-end)
226          (completing-read "Encoding: "
227                           (mime-encoding-alist)
228                           nil t "base64")))
229   (funcall (mel-find-function 'mime-encode-region encoding) start end))
230
231
232 ;;;###autoload
233 (defun mime-decode-region (start end encoding)
234   "Decode region START to END of current buffer using ENCODING.
235 ENCODING must be string."
236   (interactive
237    (list (region-beginning)(region-end)
238          (completing-read "Encoding: "
239                           (mime-encoding-alist 'mime-decode-region)
240                           nil t "base64")))
241   (funcall (mel-find-function 'mime-decode-region encoding)
242            start end))
243
244
245 ;;; @ string
246 ;;;
247
248 ;;;###autoload
249 (defun mime-decode-string (string encoding)
250   "Decode STRING using ENCODING.
251 ENCODING must be string.  If ENCODING is found in
252 `mime-string-decoding-method-alist' as its key, this function decodes
253 the STRING by its value."
254   (let ((f (mel-find-function 'mime-decode-string encoding)))
255     (if f
256         (funcall f string)
257       string)))
258
259
260 (mel-define-service encoded-text-encode-string)
261 (defun encoded-text-encode-string (string encoding &optional mode)
262   "Encode STRING as encoded-text using ENCODING.
263 ENCODING must be string.
264 Optional argument MODE allows `text', `comment', `phrase' or nil.
265 Default value is `phrase'."
266   (if (string= encoding "B")
267       (base64-encode-string string 'no-line-break)
268     (let ((f (mel-find-function 'encoded-text-encode-string encoding)))
269       (if f
270           (funcall f string mode)
271         string))))
272
273 (mel-define-service encoded-text-decode-string (string encoding)
274   "Decode STRING as encoded-text using ENCODING.  ENCODING must be string.")
275
276 (defun base64-encoded-length (string)
277   (* (/ (+ (length string) 2) 3) 4))
278
279 (defsubst Q-encoding-printable-char-p (chr mode)
280   (and (not (memq chr '(?= ?? ?_)))
281        (<= ?\  chr)(<= chr ?~)
282        (cond ((eq mode 'text) t)
283              ((eq mode 'comment)
284               (not (memq chr '(?\( ?\) ?\\))))
285              (t
286               (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))))))
287
288 (defun Q-encoded-text-length (string &optional mode)
289   (let ((l 0)(i 0)(len (length string)) chr)
290     (while (< i len)
291       (setq chr (aref string i))
292       (if (or (Q-encoding-printable-char-p chr mode)
293               (eq chr ? ))
294           (setq l (+ l 1))
295         (setq l (+ l 3)))
296       (setq i (+ i 1)))
297     l))
298
299
300 ;;; @ file
301 ;;;
302
303 ;;;###autoload
304 (defun mime-insert-encoded-file (filename encoding)
305   "Insert file FILENAME encoded by ENCODING format."
306   (interactive
307    (list (read-file-name "Insert encoded file: ")
308          (completing-read "Encoding: "
309                           (mime-encoding-alist)
310                           nil t "base64")))
311   (funcall (mel-find-function 'mime-insert-encoded-file encoding)
312            filename))
313
314
315 ;;;###autoload
316 (defun mime-write-decoded-region (start end filename encoding)
317   "Decode and write current region encoded by ENCODING into FILENAME.
318 START and END are buffer positions."
319   (interactive
320    (list (region-beginning)(region-end)
321          (read-file-name "Write decoded region to file: ")
322          (completing-read "Encoding: "
323                           (mime-encoding-alist 'mime-write-decoded-region)
324                           nil t "base64")))
325   (funcall (mel-find-function 'mime-write-decoded-region encoding)
326            start end filename))
327
328
329 ;;; @ end
330 ;;;
331
332 (provide 'mel)
333
334 ;;; mel.el ends here.