1 ;;; mel.el --- A MIME encoding/decoding library.
3 ;; Copyright (C) 1995,1996,1997,1998,1999 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.
32 (defcustom mime-encoding-list
33 '("7bit" "8bit" "binary" "base64" "quoted-printable")
34 "List of Content-Transfer-Encoding. Each encoding must be string."
36 :type '(repeat string))
38 (defun mime-encoding-list (&optional service)
39 "Return list of Content-Transfer-Encoding.
40 If SERVICE is specified, it returns available list of
41 Content-Transfer-Encoding for it."
44 (mapatoms (lambda (sym)
46 (setq dest (cons (symbol-name sym) dest)))
48 (symbol-value (intern (format "%s-obarray" service))))
49 (let ((rest mel-encoding-module-alist)
51 (while (setq pair (car rest))
52 (let ((key (car pair)))
55 (setq dest (cons key dest))))
56 (setq rest (cdr rest)))
61 (defun mime-encoding-alist (&optional service)
62 "Return table of Content-Transfer-Encoding for completion."
63 (mapcar #'list (mime-encoding-list service)))
65 (defsubst mel-use-module (name encodings)
67 (set-alist 'mel-encoding-module-alist
69 (cons name (cdr (assoc (car encodings)
70 mel-encoding-module-alist))))
71 (setq encodings (cdr encodings))))
73 (defsubst mel-find-function (service encoding)
74 (mel-find-function-from-obarray
75 (symbol-value (intern (format "%s-obarray" service))) encoding))
78 ;;; @ setting for modules
81 (mel-define-backend "7bit")
82 (mel-define-method-function (mime-encode-string string (nil "7bit"))
84 (mel-define-method-function (mime-decode-string string (nil "7bit"))
86 (mel-define-method mime-encode-region (start end (nil "7bit")))
87 (mel-define-method mime-decode-region (start end (nil "7bit")))
88 (mel-define-method-function (mime-insert-encoded-file filename (nil "7bit"))
89 'binary-insert-file-contents)
90 (mel-define-method-function (mime-write-decoded-region
91 start end filename (nil "7bit"))
94 (mel-define-backend "8bit" ("7bit"))
96 (mel-define-backend "binary" ("8bit"))
99 (and (fboundp 'base64-encode-string)
100 (subrp (symbol-function 'base64-encode-string))))
103 (mel-define-backend "base64")
104 (mel-define-method-function (mime-encode-string string (nil "base64"))
105 'base64-encode-string)
106 (mel-define-method-function (mime-decode-string string (nil "base64"))
107 'base64-decode-string)
108 (mel-define-method-function (mime-encode-region start end (nil "base64"))
109 'base64-encode-region)
110 (mel-define-method-function (mime-decode-region start end (nil "base64"))
111 'base64-decode-region)
112 (mel-define-method mime-insert-encoded-file (filename (nil "base64"))
113 "Encode contents of file FILENAME to base64, and insert the result.
114 It calls external base64 encoder specified by
115 `base64-external-encoder'. So you must install the program (maybe
116 mmencode included in metamail or XEmacs package)."
117 (interactive "*fInsert encoded file: ")
118 (insert (base64-encode-string
120 (set-buffer-multibyte nil)
121 (binary-insert-file-contents filename)
123 (or (bolp) (insert ?\n)))
125 ;; (mel-define-method-function (encoded-text-encode-string string (nil "B"))
126 ;; 'base64-encode-string)
127 (mel-define-method encoded-text-decode-string (string (nil "B"))
128 (if (string-match (eval-when-compile
129 (concat "\\`" B-encoded-text-regexp "\\'"))
131 (base64-decode-string string)
132 (error "Invalid encoded-text %s" string)))
135 (mel-use-module 'mel-b-el '("base64" "B"))
136 (mel-use-module 'mel-q '("quoted-printable" "Q"))
137 (mel-use-module 'mel-g '("x-gzip64"))
138 (mel-use-module 'mel-u '("x-uue" "x-uuencode"))
140 (defvar mel-b-ccl-module
141 (and (featurep 'mule)
144 (module-installed-p 'mel-b-ccl))))
146 (defvar mel-q-ccl-module
147 (and (featurep 'mule)
150 (module-installed-p 'mel-q-ccl))))
152 (when mel-b-ccl-module
153 (mel-use-module 'mel-b-ccl '("base64" "B")))
155 (when mel-q-ccl-module
156 (mel-use-module 'mel-q-ccl '("quoted-printable" "Q")))
158 (when base64-dl-module
159 (mel-use-module 'mel-b-dl '("base64" "B")))
166 (defun mime-encode-region (start end encoding)
167 "Encode region START to END of current buffer using ENCODING.
168 ENCODING must be string."
170 (list (region-beginning)(region-end)
171 (completing-read "Encoding: "
172 (mime-encoding-alist)
174 (funcall (mel-find-function 'mime-encode-region encoding) start end))
178 (defun mime-decode-region (start end encoding)
179 "Decode region START to END of current buffer using ENCODING.
180 ENCODING must be string."
182 (list (region-beginning)(region-end)
183 (completing-read "Encoding: "
184 (mime-encoding-alist 'mime-decode-region)
186 (funcall (mel-find-function 'mime-decode-region encoding)
194 (defun mime-decode-string (string encoding)
195 "Decode STRING using ENCODING.
196 ENCODING must be string. If ENCODING is found in
197 `mime-string-decoding-method-alist' as its key, this function decodes
198 the STRING by its value."
199 (let ((f (mel-find-function 'mime-decode-string encoding)))
205 (mel-define-service encoded-text-encode-string)
206 (defun encoded-text-encode-string (string encoding &optional mode)
207 "Encode STRING as encoded-text using ENCODING.
208 ENCODING must be string.
209 Optional argument MODE allows `text', `comment', `phrase' or nil.
210 Default value is `phrase'."
211 (if (string= encoding "B")
212 (base64-encode-string string 'no-line-break)
213 (let ((f (mel-find-function 'encoded-text-encode-string encoding)))
215 (funcall f string mode)
218 (mel-define-service encoded-text-decode-string (string encoding)
219 "Decode STRING as encoded-text using ENCODING. 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 '(?\( ?\) ?\\))))
231 (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))))))
233 (defun Q-encoded-text-length (string &optional mode)
234 (let ((l 0)(i 0)(len (length string)) chr)
236 (setq chr (aref string i))
237 (if (or (Q-encoding-printable-char-p chr mode)
249 (defun mime-insert-encoded-file (filename encoding)
250 "Insert file FILENAME encoded by ENCODING format."
252 (list (read-file-name "Insert encoded file: ")
253 (completing-read "Encoding: "
254 (mime-encoding-alist)
256 (funcall (mel-find-function 'mime-insert-encoded-file encoding)
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."
265 (list (region-beginning)(region-end)
266 (read-file-name "Write decoded region to file: ")
267 (completing-read "Encoding: "
268 (mime-encoding-alist 'mime-write-decoded-region)
270 (funcall (mel-find-function 'mime-write-decoded-region encoding)
279 ;;; mel.el ends here.