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