Merge `deisui-1_14_0-1'.
[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 <tomo@m17n.org>
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 'poem)
30 (require 'alist)
31 (require 'path-util)
32
33 (defcustom mime-encoding-list
34   '("7bit" "8bit" "binary" "base64" "quoted-printable")
35   "List of Content-Transfer-Encoding.  Each encoding must be string."
36   :group 'mime
37   :type '(repeat string))
38
39 (defun mime-encoding-list (&optional service)
40   "Return list of Content-Transfer-Encoding.
41 If SERVICE is specified, it returns available list of
42 Content-Transfer-Encoding for it."
43   (if service
44       (let (dest)
45         (mapatoms (lambda (sym)
46                     (or (eq sym nil)
47                         (setq dest (cons (symbol-name sym) dest)))
48                     )
49                   (symbol-value (intern (format "%s-obarray" service))))
50         (let ((rest mel-encoding-module-alist)
51               pair)
52           (while (setq pair (car rest))
53             (let ((key (car pair)))
54               (or (member key dest)
55                   (<= (length key) 1)
56                   (setq dest (cons key dest))))
57             (setq rest (cdr rest)))
58           )
59         dest)
60     mime-encoding-list))
61
62 (defun mime-encoding-alist (&optional service)
63   "Return table of Content-Transfer-Encoding for completion."
64   (mapcar #'list (mime-encoding-list service)))
65
66 (defsubst mel-use-module (name encodings)
67   (while encodings
68     (set-alist 'mel-encoding-module-alist
69                (car encodings)
70                (cons name (cdr (assoc (car encodings)
71                                       mel-encoding-module-alist))))
72     (setq encodings (cdr encodings))))
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 (mel-define-backend "7bit")
83 (mel-define-method-function (mime-encode-string string (nil "7bit"))
84                             'identity)
85 (mel-define-method-function (mime-decode-string string (nil "7bit"))
86                             'identity)
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)
94
95 (mel-define-backend "8bit" ("7bit"))
96
97 (mel-define-backend "binary" ("8bit"))
98
99 (defvar mel-b-builtin
100    (and (fboundp 'base64-encode-string)
101         (subrp (symbol-function 'base64-encode-string))))
102
103 (when mel-b-builtin
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 "*fInsert encoded file: ")
119     (insert (base64-encode-string
120              (with-temp-buffer
121                (set-buffer-multibyte nil)
122                (insert-file-contents-as-binary filename)
123                (buffer-string))))
124     (or (bolp) (insert ?\n)))
125     
126   (mel-define-method-function (encoded-text-encode-string string (nil "B"))
127                               'base64-encode-string)
128   (mel-define-method encoded-text-decode-string (string (nil "B"))
129     (if (string-match (eval-when-compile
130                         (concat "\\`" B-encoded-text-regexp "\\'"))
131                       string)
132         (base64-decode-string string)
133       (error "Invalid encoded-text %s" string)))
134   )
135
136 (mel-use-module 'mel-b-el '("base64" "B"))
137 (mel-use-module 'mel-q '("quoted-printable" "Q"))
138 (mel-use-module 'mel-g '("x-gzip64"))
139 (mel-use-module 'mel-u '("x-uue" "x-uuencode"))
140
141 (defvar mel-b-ccl-module
142   (and (featurep 'mule)
143        (progn
144          (require 'path-util)
145          (module-installed-p 'mel-b-ccl))))
146
147 (defvar mel-q-ccl-module
148   (and (featurep 'mule)
149        (progn
150          (require 'path-util)
151          (module-installed-p 'mel-q-ccl))))
152
153 (when mel-b-ccl-module
154   (mel-use-module 'mel-b-ccl '("base64" "B")))
155
156 (when mel-q-ccl-module
157   (mel-use-module 'mel-q-ccl '("quoted-printable" "Q")))
158
159 (when base64-dl-module
160   (mel-use-module 'mel-b-dl '("base64" "B")))
161
162
163 ;;; @ region
164 ;;;
165
166 ;;;###autoload
167 (defun mime-encode-region (start end encoding)
168   "Encode region START to END of current buffer using ENCODING.
169 ENCODING must be string."
170   (interactive
171    (list (region-beginning)(region-end)
172          (completing-read "Encoding: "
173                           (mime-encoding-alist)
174                           nil t "base64")))
175   (funcall (mel-find-function 'mime-encode-region encoding) start end))
176
177
178 ;;;###autoload
179 (defun mime-decode-region (start end encoding)
180   "Decode region START to END of current buffer using ENCODING.
181 ENCODING must be string."
182   (interactive
183    (list (region-beginning)(region-end)
184          (completing-read "Encoding: "
185                           (mime-encoding-alist 'mime-decode-region)
186                           nil t "base64")))
187   (funcall (mel-find-function 'mime-decode-region encoding)
188            start end))
189
190
191 ;;; @ string
192 ;;;
193
194 ;;;###autoload
195 (defun mime-decode-string (string encoding)
196   "Decode STRING using ENCODING.
197 ENCODING must be string.  If ENCODING is found in
198 `mime-string-decoding-method-alist' as its key, this function decodes
199 the STRING by its value."
200   (let ((f (mel-find-function 'mime-decode-string encoding)))
201     (if f
202         (funcall f string)
203       string)))
204
205
206 (mel-define-service encoded-text-encode-string (string encoding)
207   "Encode STRING as encoded-text using ENCODING.  ENCODING must be string.")
208
209 (mel-define-service encoded-text-decode-string (string encoding)
210   "Decode STRING as encoded-text using ENCODING.  ENCODING must be string.")
211
212 (defun base64-encoded-length (string)
213   (* (/ (+ (length string) 2) 3) 4))
214
215 (defsubst Q-encoding-printable-char-p (chr mode)
216   (and (not (memq chr '(?= ?? ?_)))
217        (<= ?\  chr)(<= chr ?~)
218        (cond ((eq mode 'text) t)
219              ((eq mode 'comment)
220               (not (memq chr '(?\( ?\) ?\\))))
221              (t
222               (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))))))
223
224 (defun Q-encoded-text-length (string &optional mode)
225   (let ((l 0)(i 0)(len (length string)) chr)
226     (while (< i len)
227       (setq chr (aref string i))
228       (if (or (Q-encoding-printable-char-p chr mode)
229               (eq chr ? ))
230           (setq l (+ l 1))
231         (setq l (+ l 3)))
232       (setq i (+ i 1)))
233     l))
234
235
236 ;;; @ file
237 ;;;
238
239 ;;;###autoload
240 (defun mime-insert-encoded-file (filename encoding)
241   "Insert file FILENAME encoded by ENCODING format."
242   (interactive
243    (list (read-file-name "Insert encoded file: ")
244          (completing-read "Encoding: "
245                           (mime-encoding-alist)
246                           nil t "base64")))
247   (funcall (mel-find-function 'mime-insert-encoded-file encoding)
248            filename))
249
250
251 ;;;###autoload
252 (defun mime-write-decoded-region (start end filename encoding)
253   "Decode and write current region encoded by ENCODING into FILENAME.
254 START and END are buffer positions."
255   (interactive
256    (list (region-beginning)(region-end)
257          (read-file-name "Write decoded region to file: ")
258          (completing-read "Encoding: "
259                           (mime-encoding-alist 'mime-write-decoded-region)
260                           nil t "base64")))
261   (funcall (mel-find-function 'mime-write-decoded-region encoding)
262            start end filename))
263
264
265 ;;; @ end
266 ;;;
267
268 (provide 'mel)
269
270 ;;; mel.el ends here.