1 ;;; mel.el : a MIME encoding/decoding library
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
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 GNU Emacs; 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))
65 (defsubst mel-use-module (name encodings)
67 (while (setq encoding (car encodings))
68 (set-alist 'mel-encoding-module-alist
70 (cons name (cdr (assoc encoding mel-encoding-module-alist))))
71 (setq encodings (cdr encodings))
74 (defsubst mel-find-function (service encoding)
75 (mel-find-function-from-obarray
76 (symbol-value (intern (format "%s-obarray" service))) encoding))
79 ;;; @ setting for modules
82 (mel-define-backend "7bit")
83 (mel-define-method-function (mime-encode-string string (nil "7bit"))
85 (mel-define-method-function (mime-decode-string string (nil "7bit"))
87 (mel-define-method mime-encode-region (start end (nil "7bit")))
88 (mel-define-method mime-decode-region (start end (nil "7bit")))
89 (mel-define-method-function (mime-insert-encoded-file filename (nil "7bit"))
90 'insert-file-contents-as-binary)
91 (mel-define-method-function (mime-write-decoded-region
92 start end filename (nil "7bit"))
93 'write-region-as-binary)
95 (mel-define-backend "8bit" ("7bit"))
97 (mel-define-backend "binary" ("8bit"))
100 (and (fboundp 'base64-encode-string)
101 (subrp (symbol-function 'base64-encode-string))))
104 (mel-define-backend "base64")
105 (mel-define-method-function (mime-encode-string string (nil "base64"))
106 'base64-encode-string)
107 (mel-define-method-function (mime-decode-string string (nil "base64"))
108 'base64-decode-string)
109 (mel-define-method-function (mime-encode-region start end (nil "base64"))
110 'base64-encode-region)
111 (mel-define-method-function (mime-decode-region start end (nil "base64"))
112 'base64-decode-region)
113 (mel-define-method mime-insert-encoded-file (filename (nil "base64"))
114 "Encode contents of file FILENAME to base64, and insert the result.
115 It calls external base64 encoder specified by
116 `base64-external-encoder'. So you must install the program (maybe
117 mmencode included in metamail or XEmacs package)."
118 (interactive (list (read-file-name "Insert encoded file: ")))
119 (insert (base64-encode-string
121 (set-buffer-multibyte nil)
122 (insert-file-contents-as-binary filename)
128 (mel-define-method-function (encoded-text-encode-string string (nil "B"))
129 'base64-encode-string)
130 (mel-define-method encoded-text-decode-string (string (nil "B"))
131 (if (and (string-match B-encoded-text-regexp string)
132 (string= string (match-string 0 string)))
133 (base64-decode-string string)
134 (error "Invalid encoded-text %s" string)))
137 (mel-use-module 'mel-b-el '("base64" "B"))
138 (mel-use-module 'mel-q '("quoted-printable" "Q"))
139 (mel-use-module 'mel-g '("x-gzip64"))
140 (mel-use-module 'mel-u '("x-uue" "x-uuencode"))
142 (defvar mel-b-ccl-module
143 (and (featurep 'mule)
146 (module-installed-p 'mel-b-ccl)
149 (defvar mel-q-ccl-module
150 (and (featurep 'mule)
153 (module-installed-p 'mel-q-ccl)
157 (mel-use-module 'mel-b-ccl '("base64" "B"))
161 (mel-use-module 'mel-q-ccl '("quoted-printable" "Q"))
165 (mel-use-module 'mel-b-dl '("base64" "B"))
173 (defun mime-encode-region (start end encoding)
174 "Encode region START to END of current buffer using ENCODING.
175 ENCODING must be string."
177 (list (region-beginning) (region-end)
178 (completing-read "encoding: "
179 (mime-encoding-alist)
181 (funcall (mel-find-function 'mime-encode-region encoding) start end)
186 (defun mime-decode-region (start end encoding)
187 "Decode region START to END of current buffer using ENCODING.
188 ENCODING must be string."
190 (list (region-beginning) (region-end)
191 (completing-read "encoding: "
192 (mime-encoding-alist 'mime-decode-region)
194 (funcall (mel-find-function 'mime-decode-region encoding)
202 (defun mime-decode-string (string encoding)
203 "Decode STRING using ENCODING.
204 ENCODING must be string. If ENCODING is found in
205 `mime-string-decoding-method-alist' as its key, this function decodes
206 the STRING by its value."
207 (let ((fun (mel-find-function 'mime-decode-string encoding)))
209 (funcall (mel-find-function 'mime-decode-string encoding)
213 (mel-define-service encoded-text-encode-string (string encoding)
214 "Encode STRING as encoded-text using ENCODING.
215 ENCODING must be string.")
217 (mel-define-service encoded-text-decode-string (string encoding)
218 "Decode STRING as encoded-text using ENCODING.
219 ENCODING must be string.")
221 (defun base64-encoded-length (string)
222 (* (/ (+ (length string) 2) 3) 4))
224 (defsubst Q-encoding-printable-char-p (chr mode)
225 (and (not (memq chr '(?= ?? ?_)))
226 (<= ?\ chr)(<= chr ?~)
227 (cond ((eq mode 'text) t)
229 (not (memq chr '(?\( ?\) ?\\)))
232 (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
235 (defun Q-encoded-text-length (string &optional mode)
236 (let ((l 0)(i 0)(len (length string)) chr)
238 (setq chr (elt string i))
239 (if (Q-encoding-printable-char-p chr mode)
251 (defun mime-insert-encoded-file (filename encoding)
252 "Insert file FILENAME encoded by ENCODING format."
254 (list (read-file-name "Insert encoded file: ")
255 (completing-read "encoding: "
256 (mime-encoding-alist)
258 (funcall (mel-find-function 'mime-insert-encoded-file encoding)
263 (defun mime-write-decoded-region (start end filename encoding)
264 "Decode and write current region encoded by ENCODING into FILENAME.
265 START and END are buffer positions."
267 (list (region-beginning) (region-end)
268 (read-file-name "Write decoded region to file: ")
269 (completing-read "encoding: "
270 (mime-encoding-alist 'mime-write-decoded-region)
272 (funcall (mel-find-function 'mime-write-decoded-region encoding)
281 ;;; mel.el ends here.