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.
30 (defcustom mime-encoding-list
31 '("7bit" "8bit" "binary" "base64" "quoted-printable")
32 "List of Content-Transfer-Encoding. Each encoding must be string."
34 :type '(repeat string))
36 (defun mime-encoding-list (&optional service)
37 "Return list of Content-Transfer-Encoding.
38 If SERVICE is specified, it returns available list of
39 Content-Transfer-Encoding for it."
42 (mapatoms (lambda (sym)
44 (setq dest (cons (symbol-name sym) dest)))
46 (symbol-value (intern (format "%s-obarray" service))))
47 (let ((rest mel-encoding-module-alist)
49 (while (setq pair (car rest))
50 (let ((key (car pair)))
53 (setq dest (cons key dest))))
54 (setq rest (cdr rest)))
59 (defun mime-encoding-alist (&optional service)
60 "Return table of Content-Transfer-Encoding for completion."
61 (mapcar #'list (mime-encoding-list service))
64 (defsubst mel-use-module (name encodings)
66 (while (setq encoding (car encodings))
67 (set-alist 'mel-encoding-module-alist
69 (cons name (cdr (assoc encoding mel-encoding-module-alist))))
70 (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 (defvar mel-ccl-module
85 (module-installed-p 'mel-ccl)
88 (mel-use-module 'mel-b '("base64" "B"))
89 (mel-use-module 'mel-q '("quoted-printable" "Q"))
90 (mel-use-module 'mel-g '("x-gzip64"))
91 (mel-use-module 'mel-u '("x-uue" "x-uuencode"))
94 (mel-use-module 'mel-ccl '("base64" "quoted-printable" "B" "Q"))
98 (mel-use-module 'mel-b-dl '("base64" "B"))
101 (mel-define-backend "7bit")
102 (mel-define-method-function (mime-encode-string string (nil "7bit"))
104 (mel-define-method-function (mime-decode-string string (nil "7bit"))
106 (mel-define-method mime-encode-region (start end (nil "7bit")))
107 (mel-define-method mime-decode-region (start end (nil "7bit")))
108 (mel-define-method-function (mime-insert-encoded-file filename (nil "7bit"))
109 'insert-file-contents-as-binary)
110 (mel-define-method-function (mime-write-decoded-region
111 start end filename (nil "7bit"))
112 'write-region-as-binary)
114 (mel-define-backend "8bit" ("7bit"))
116 (mel-define-backend "binary" ("8bit"))
123 (defun mime-encode-region (start end encoding)
124 "Encode region START to END of current buffer using ENCODING.
125 ENCODING must be string."
127 (list (region-beginning) (region-end)
128 (completing-read "encoding: "
129 (mime-encoding-alist)
131 (funcall (mel-find-function 'mime-encode-region encoding) start end)
136 (defun mime-decode-region (start end encoding)
137 "Decode region START to END of current buffer using ENCODING.
138 ENCODING must be string."
140 (list (region-beginning) (region-end)
141 (completing-read "encoding: "
142 (mime-encoding-alist 'mime-decode-region)
144 (funcall (mel-find-function 'mime-decode-region encoding)
152 (defun mime-decode-string (string encoding)
153 "Decode STRING using ENCODING.
154 ENCODING must be string. If ENCODING is found in
155 `mime-string-decoding-method-alist' as its key, this function decodes
156 the STRING by its value."
157 (funcall (mel-find-function 'mime-decode-string encoding)
161 (mel-define-service encoded-text-encode-string (string encoding)
162 "Encode STRING as encoded-text using ENCODING.
163 ENCODING must be string.")
165 (mel-define-service encoded-text-decode-string (string encoding)
166 "Decode STRING as encoded-text using ENCODING.
167 ENCODING must be string.")
169 (defun base64-encoded-length (string)
170 (let ((len (length string)))
172 (if (= (mod len 3) 0) 0 1)
176 (defsubst Q-encoding-printable-char-p (chr mode)
177 (and (not (memq chr '(?= ?? ?_)))
178 (<= ?\ chr)(<= chr ?~)
179 (cond ((eq mode 'text) t)
181 (not (memq chr '(?\( ?\) ?\\)))
184 (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
187 (defun Q-encoded-text-length (string &optional mode)
188 (let ((l 0)(i 0)(len (length string)) chr)
190 (setq chr (elt string i))
191 (if (Q-encoding-printable-char-p chr mode)
203 (defun mime-insert-encoded-file (filename encoding)
204 "Insert file FILENAME encoded by ENCODING format."
206 (list (read-file-name "Insert encoded file: ")
207 (completing-read "encoding: "
208 (mime-encoding-alist)
210 (funcall (mel-find-function 'mime-insert-encoded-file encoding)
215 (defun mime-write-decoded-region (start end filename encoding)
216 "Decode and write current region encoded by ENCODING into FILENAME.
217 START and END are buffer positions."
219 (list (region-beginning) (region-end)
220 (read-file-name "Write decoded region to file: ")
221 (completing-read "encoding: "
222 (mime-encoding-alist 'mime-write-decoded-region)
224 (funcall (mel-find-function 'mime-write-decoded-region encoding)
233 ;;; mel.el ends here.