1 ;;; mel.el --- A MIME encoding/decoding library.
3 ;; Copyright (C) 1995,1996,1997,1998,1999,2000 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
7 ;; Keywords: MIME, Base64, Quoted-Printable, uuencode, gzip64
9 ;; This file is part of FLIM (Faithful Library about Internet Message).
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.
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.
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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
31 (defcustom mime-encoding-list
32 '("7bit" "8bit" "binary" "base64" "quoted-printable")
33 "List of Content-Transfer-Encoding. Each encoding must be string."
35 :type '(repeat string))
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."
43 (mapatoms (lambda (sym)
45 (setq dest (cons (symbol-name sym) dest)))
47 (symbol-value (intern (format "%s-obarray" service))))
48 (let ((rest mel-encoding-module-alist)
50 (while (setq pair (car rest))
51 (let ((key (car pair)))
54 (setq dest (cons key dest))))
55 (setq rest (cdr rest)))
60 (defun mime-encoding-alist (&optional service)
61 "Return table of Content-Transfer-Encoding for completion."
62 (mapcar #'list (mime-encoding-list service)))
64 (defsubst mel-use-module (name encodings)
66 (set-alist 'mel-encoding-module-alist
68 (cons name (cdr (assoc (car encodings)
69 mel-encoding-module-alist))))
70 (setq encodings (cdr encodings))))
72 (defsubst mel-find-function (service encoding)
73 (mel-find-function-from-obarray
74 (symbol-value (intern (format "%s-obarray" service))) encoding))
77 ;;; @ setting for modules
80 (defun 8bit-insert-encoded-file (filename)
81 "Insert file FILENAME encoded by \"7bit\" format."
82 (let ((coding-system-for-read 'raw-text)
84 ;; Returns list of absolute file name and length of data inserted.
85 (insert-file-contents filename)))
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)
91 (write-region start end filename)))
93 (mel-define-backend "8bit")
94 (mel-define-method-function (mime-encode-string string (nil "8bit"))
96 (mel-define-method-function (mime-decode-string string (nil "8bit"))
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)
107 (defalias '7bit-insert-encoded-file '8bit-insert-encoded-file)
108 (defalias '7bit-write-decoded-region '8bit-write-decoded-region)
110 (mel-define-backend "7bit" ("8bit"))
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)))
119 (defalias 'binary-insert-encoded-file 'insert-file-contents-literally)
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)
125 (find-file-noselect filename nowarn rawfile)))
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))
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))
140 (mel-define-backend "binary")
141 (mel-define-method-function (mime-encode-string string (nil "binary"))
143 (mel-define-method-function (mime-decode-string string (nil "binary"))
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)
153 (defvar mel-b-builtin
154 (and (fboundp 'base64-encode-string)
155 (subrp (symbol-function 'base64-encode-string))))
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
175 (set-buffer-multibyte nil)
176 (binary-insert-encoded-file filename)
178 (or (bolp) (insert ?\n)))
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 "\\'"))
186 (base64-decode-string string)
187 (error "Invalid encoded-text %s" string)))
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"))
195 (defvar mel-b-ccl-module
196 (and (featurep 'mule)
199 (module-installed-p 'mel-b-ccl))))
201 (defvar mel-q-ccl-module
202 (and (featurep 'mule)
205 (module-installed-p 'mel-q-ccl))))
207 (when mel-b-ccl-module
208 (mel-use-module 'mel-b-ccl '("base64" "B")))
210 (when mel-q-ccl-module
211 (mel-use-module 'mel-q-ccl '("quoted-printable" "Q")))
213 (when base64-dl-module
214 (mel-use-module 'mel-b-dl '("base64" "B")))
221 (defun mime-encode-region (start end encoding)
222 "Encode region START to END of current buffer using ENCODING.
223 ENCODING must be string."
225 (list (region-beginning)(region-end)
226 (completing-read "Encoding: "
227 (mime-encoding-alist)
229 (funcall (mel-find-function 'mime-encode-region encoding) start end))
233 (defun mime-decode-region (start end encoding)
234 "Decode region START to END of current buffer using ENCODING.
235 ENCODING must be string."
237 (list (region-beginning)(region-end)
238 (completing-read "Encoding: "
239 (mime-encoding-alist 'mime-decode-region)
241 (funcall (mel-find-function 'mime-decode-region encoding)
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)))
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)))
270 (funcall f string mode)
273 (mel-define-service encoded-text-decode-string (string encoding)
274 "Decode STRING as encoded-text using ENCODING. ENCODING must be string.")
276 (defun base64-encoded-length (string)
277 (* (/ (+ (length string) 2) 3) 4))
279 (defsubst Q-encoding-printable-char-p (chr mode)
280 (and (not (memq chr '(?= ?? ?_)))
281 (<= ?\ chr)(<= chr ?~)
282 (cond ((eq mode 'text) t)
284 (not (memq chr '(?\( ?\) ?\\))))
286 (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))))))
288 (defun Q-encoded-text-length (string &optional mode)
289 (let ((l 0)(i 0)(len (length string)) chr)
291 (setq chr (aref string i))
292 (if (or (Q-encoding-printable-char-p chr mode)
304 (defun mime-insert-encoded-file (filename encoding)
305 "Insert file FILENAME encoded by ENCODING format."
307 (list (read-file-name "Insert encoded file: ")
308 (completing-read "Encoding: "
309 (mime-encoding-alist)
311 (funcall (mel-find-function 'mime-insert-encoded-file encoding)
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."
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)
325 (funcall (mel-find-function 'mime-write-decoded-region encoding)
334 ;;; mel.el ends here.