* mel-b-ccl.el: New file.
[elisp/flim.git] / mel.el
1 ;;; mel.el : a MIME encoding/decoding library
2
3 ;; Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
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 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.
25
26 ;;; Code:
27
28 (require 'mime-def)
29 (require 'poem)
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
65 (defsubst mel-use-module (name encodings)
66   (let (encoding)
67     (while (setq encoding (car encodings))
68       (set-alist 'mel-encoding-module-alist
69                  encoding
70                  (cons name (cdr (assoc encoding mel-encoding-module-alist))))
71       (setq encodings (cdr encodings))
72       )))
73
74 (defsubst mel-find-function (service encoding)
75   (mel-find-function-from-obarray
76    (symbol-value (intern (format "%s-obarray" service))) encoding))
77
78
79 ;;; @ setting for modules
80 ;;;
81
82 (defvar mel-ccl-module (featurep 'mule))
83
84 (defvar mel-b-ccl-module
85   (and mel-ccl-module
86        (progn
87          (require 'path-util)
88          (module-installed-p 'mel-b-ccl)
89          )))
90
91 (defvar mel-q-ccl-module
92   (and mel-ccl-module
93        (progn
94          (require 'path-util)
95          (module-installed-p 'mel-q-ccl)
96          )))
97
98 (mel-use-module 'mel-b '("base64" "B"))
99 (mel-use-module 'mel-q '("quoted-printable" "Q"))
100 (mel-use-module 'mel-g '("x-gzip64"))
101 (mel-use-module 'mel-u '("x-uue" "x-uuencode"))
102
103 (if mel-b-ccl-module
104     (mel-use-module 'mel-b-ccl '("base64" "B"))
105   )
106
107 (if mel-q-ccl-module
108     (mel-use-module 'mel-q-ccl '("quoted-printable" "Q"))
109   )
110
111 (if base64-dl-module
112     (mel-use-module 'mel-b-dl '("base64" "B"))
113   )
114
115 (mel-define-backend "7bit")
116 (mel-define-method-function (mime-encode-string string (nil "7bit"))
117                             'identity)
118 (mel-define-method-function (mime-decode-string string (nil "7bit"))
119                             'identity)
120 (mel-define-method mime-encode-region (start end (nil "7bit")))
121 (mel-define-method mime-decode-region (start end (nil "7bit")))
122 (mel-define-method-function (mime-insert-encoded-file filename (nil "7bit"))
123                             'insert-file-contents-as-binary)
124 (mel-define-method-function (mime-write-decoded-region
125                              start end filename (nil "7bit"))
126                             'write-region-as-binary)
127
128 (mel-define-backend "8bit" ("7bit"))
129
130 (mel-define-backend "binary" ("8bit"))
131
132
133 ;;; @ region
134 ;;;
135
136 ;;;###autoload
137 (defun mime-encode-region (start end encoding)
138   "Encode region START to END of current buffer using ENCODING.
139 ENCODING must be string."
140   (interactive
141    (list (region-beginning) (region-end)
142          (completing-read "encoding: "
143                           (mime-encoding-alist)
144                           nil t "base64")))
145   (funcall (mel-find-function 'mime-encode-region encoding) start end)
146   )
147
148
149 ;;;###autoload
150 (defun mime-decode-region (start end encoding)
151   "Decode region START to END of current buffer using ENCODING.
152 ENCODING must be string."
153   (interactive
154    (list (region-beginning) (region-end)
155          (completing-read "encoding: "
156                           (mime-encoding-alist 'mime-decode-region)
157                           nil t "base64")))
158   (funcall (mel-find-function 'mime-decode-region encoding)
159            start end))
160
161
162 ;;; @ string
163 ;;;
164
165 ;;;###autoload
166 (defun mime-decode-string (string encoding)
167   "Decode STRING using ENCODING.
168 ENCODING must be string.  If ENCODING is found in
169 `mime-string-decoding-method-alist' as its key, this function decodes
170 the STRING by its value."
171   (funcall (mel-find-function 'mime-decode-string encoding)
172            string))
173
174
175 (mel-define-service encoded-text-encode-string (string encoding)
176   "Encode STRING as encoded-text using ENCODING.
177 ENCODING must be string.")
178
179 (mel-define-service encoded-text-decode-string (string encoding)
180   "Decode STRING as encoded-text using ENCODING.
181 ENCODING must be string.")
182
183 (defun base64-encoded-length (string)
184   (* (/ (+ (length string) 2) 3) 4))
185
186 (defsubst Q-encoding-printable-char-p (chr mode)
187   (and (not (memq chr '(?= ?? ?_)))
188        (<= ?\   chr)(<= chr ?~)
189        (cond ((eq mode 'text) t)
190              ((eq mode 'comment)
191               (not (memq chr '(?\( ?\) ?\\)))
192               )
193              (t
194               (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
195               ))))
196
197 (defun Q-encoded-text-length (string &optional mode)
198   (let ((l 0)(i 0)(len (length string)) chr)
199     (while (< i len)
200       (setq chr (elt string i))
201       (if (Q-encoding-printable-char-p chr mode)
202           (setq l (+ l 1))
203         (setq l (+ l 3))
204         )
205       (setq i (+ i 1)) )
206     l))
207
208
209 ;;; @ file
210 ;;;
211
212 ;;;###autoload
213 (defun mime-insert-encoded-file (filename encoding)
214   "Insert file FILENAME encoded by ENCODING format."
215   (interactive
216    (list (read-file-name "Insert encoded file: ")
217          (completing-read "encoding: "
218                           (mime-encoding-alist)
219                           nil t "base64")))
220   (funcall (mel-find-function 'mime-insert-encoded-file encoding)
221            filename))
222
223
224 ;;;###autoload
225 (defun mime-write-decoded-region (start end filename encoding)
226   "Decode and write current region encoded by ENCODING into FILENAME.
227 START and END are buffer positions."
228   (interactive
229    (list (region-beginning) (region-end)
230          (read-file-name "Write decoded region to file: ")
231          (completing-read "encoding: "
232                           (mime-encoding-alist 'mime-write-decoded-region)
233                           nil t "base64")))
234   (funcall (mel-find-function 'mime-write-decoded-region encoding)
235            start end filename))
236
237
238 ;;; @ end
239 ;;;
240
241 (provide 'mel)
242
243 ;;; mel.el ends here.