16bc0e0276cedcc7397e5a40a59f4db9c52bb12c
[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 'emu)
29 (require 'mime-def)
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 (defvar mel-encoding-module-alist nil)
38
39 (defun mime-encoding-list (&optional service)
40   "Return list of Content-Transfer-Encoding."
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   (let* ((oba (symbol-value (intern (format "%s-obarray" service))))
76          (f (intern-soft encoding oba)))
77     (or f
78         (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
79           (while (and rest
80                       (progn
81                         (require (car rest))
82                         (null (setq f (intern-soft encoding oba)))
83                         ))
84             (setq rest (cdr rest))
85             )
86           f))))
87
88
89 ;;; @ setting for modules
90 ;;;
91
92 (defvar base64-dl-module
93   (and (fboundp 'dynamic-link)
94        (let ((path (expand-file-name "base64.so" exec-directory)))
95          (and (file-exists-p path)
96               path))))
97
98
99 (mel-use-module 'mel-b '("base64" "B"))
100 (mel-use-module 'mel-q '("quoted-printable" "Q"))
101 (mel-use-module 'mel-g '("x-gzip64"))
102 (mel-use-module 'mel-u '("x-uue" "x-uuencode"))
103
104 (if (featurep 'mule)
105     (mel-use-module 'mel-ccl '("base64" "quoted-printable" "B" "Q"))
106   )
107
108 (if base64-dl-module
109     (mel-use-module 'mel-b-dl '("base64" "B"))
110   )
111
112 (mel-define-method-function (mime-encode-string string (nil "7bit"))
113                             'identity)
114 (mel-define-method-function (mime-decode-string string (nil "7bit"))
115                             'identity)
116 (mel-define-method mime-encode-region (start end (nil "7bit")))
117 (mel-define-method mime-decode-region (start end (nil "7bit")))
118 (mel-define-method-function (mime-insert-encoded-file filename (nil "7bit"))
119                             'insert-file-contents-as-binary)
120 (mel-define-method-function (mime-write-decoded-region
121                              start end filename (nil "7bit"))
122                             'write-region-as-binary)
123
124 (mel-define-method-function (mime-encode-string string (nil "8bit"))
125                             'identity)
126 (mel-define-method-function (mime-decode-string string (nil "8bit"))
127                             'identity)
128 (mel-define-method mime-encode-region (start end (nil "8bit")))
129 (mel-define-method mime-decode-region (start end (nil "8bit")))
130 (mel-define-method-function (mime-insert-encoded-file filename (nil "8bit"))
131                             'insert-file-contents-as-binary)
132 (mel-define-method-function (mime-write-decoded-region
133                              start end filename (nil "8bit"))
134                             'write-region-as-binary)
135
136 (mel-define-method-function (mime-encode-string string (nil "binary"))
137                             'identity)
138 (mel-define-method-function (mime-decode-string string (nil "binary"))
139                             'identity)
140 (mel-define-method mime-encode-region (start end (nil "binary")))
141 (mel-define-method mime-decode-region (start end (nil "binary")))
142 (mel-define-method-function (mime-insert-encoded-file filename (nil "binary"))
143                             'insert-file-contents-as-binary)
144 (mel-define-method-function (mime-write-decoded-region
145                              start end filename (nil "binary"))
146                             'write-region-as-binary)
147
148
149 ;;; @ region
150 ;;;
151
152 ;;;###autoload
153 (defun mime-encode-region (start end encoding)
154   "Encode region START to END of current buffer using ENCODING.
155 ENCODING must be string."
156   (interactive
157    (list (region-beginning) (region-end)
158          (completing-read "encoding: "
159                           (mime-encoding-alist)
160                           nil t "base64")))
161   (funcall (mel-find-function 'mime-encode-region encoding) start end)
162   )
163
164
165 ;;;###autoload
166 (defun mime-decode-region (start end encoding)
167   "Decode region START to END of current buffer using ENCODING.
168 ENCODING must be string."
169   (interactive
170    (list (region-beginning) (region-end)
171          (completing-read "encoding: "
172                           (mime-encoding-alist 'mime-decode-region)
173                           nil t "base64")))
174   (funcall (mel-find-function 'mime-decode-region encoding)
175            start end))
176
177
178 ;;; @ string
179 ;;;
180
181 ;;;###autoload
182 (defun mime-decode-string (string encoding)
183   "Decode STRING using ENCODING.
184 ENCODING must be string.  If ENCODING is found in
185 `mime-string-decoding-method-alist' as its key, this function decodes
186 the STRING by its value."
187   (funcall (mel-find-function 'mime-decode-string encoding)
188            string))
189
190
191 (mel-define-service encoded-text-encode-string (string encoding)
192   "Encode STRING as encoded-text using ENCODING.
193 ENCODING must be string.")
194
195 (mel-define-service encoded-text-decode-string (string encoding)
196   "Decode STRING as encoded-text using ENCODING.
197 ENCODING must be string.")
198
199 (defun base64-encoded-length (string)
200   (* (/ (+ (length string) 2) 3) 4))
201
202 (defsubst Q-encoding-printable-char-p (chr mode)
203   (and (not (memq chr '(?= ?? ?_)))
204        (<= ?\   chr)(<= chr ?~)
205        (cond ((eq mode 'text) t)
206              ((eq mode 'comment)
207               (not (memq chr '(?\( ?\) ?\\)))
208               )
209              (t
210               (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
211               ))))
212
213 (defun Q-encoded-text-length (string &optional mode)
214   (let ((l 0)(i 0)(len (length string)) chr)
215     (while (< i len)
216       (setq chr (elt string i))
217       (if (Q-encoding-printable-char-p chr mode)
218           (setq l (+ l 1))
219         (setq l (+ l 3))
220         )
221       (setq i (+ i 1)) )
222     l))
223
224
225 ;;; @ file
226 ;;;
227
228 ;;;###autoload
229 (defun mime-insert-encoded-file (filename encoding)
230   "Insert file FILENAME encoded by ENCODING format."
231   (interactive
232    (list (read-file-name "Insert encoded file: ")
233          (completing-read "encoding: "
234                           (mime-encoding-alist)
235                           nil t "base64")))
236   (funcall (mel-find-function 'mime-insert-encoded-file encoding)
237            filename))
238
239
240 ;;;###autoload
241 (defun mime-write-decoded-region (start end filename encoding)
242   "Decode and write current region encoded by ENCODING into FILENAME.
243 START and END are buffer positions."
244   (interactive
245    (list (region-beginning) (region-end)
246          (read-file-name "Write decoded region to file: ")
247          (completing-read "encoding: "
248                           (mime-encoding-alist 'mime-write-decoded-region)
249                           nil t "base64")))
250   (funcall (mel-find-function 'mime-write-decoded-region encoding)
251            start end filename))
252
253
254 ;;; @ end
255 ;;;
256
257 (provide 'mel)
258
259 ;;; mel.el ends here.