ccfc07279288ae926967cdbd87d39ae7e476d70a
[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 (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 (when (and (fboundp 'base64-encode-string)
100            (subrp (symbol-function 'base64-encode-string)))
101   (mel-define-backend "base64")
102   (mel-define-method-function (mime-encode-string string (nil "base64"))
103                               'base64-encode-string)
104   (mel-define-method-function (mime-decode-string string (nil "base64"))
105                               'base64-decode-string)
106   (mel-define-method-function (mime-encode-region start end (nil "base64"))
107                               'base64-encode-region)
108   (mel-define-method-function (mime-decode-region start end (nil "base64"))
109                               'base64-decode-region)  
110   (mel-define-method mime-insert-encoded-file (filename (nil "base64"))
111     "Encode contents of file FILENAME to base64, and insert the result.
112 It calls external base64 encoder specified by
113 `base64-external-encoder'.  So you must install the program (maybe
114 mmencode included in metamail or XEmacs package)."
115     (interactive (list (read-file-name "Insert encoded file: ")))
116     (insert (base64-encode-string
117              (with-temp-buffer
118                (set-buffer-multibyte nil)
119                (insert-file-contents-as-binary filename)
120                (buffer-string))))
121     (or (bolp)
122         (insert "\n"))
123     )
124     
125   (mel-define-method-function (encoded-text-encode-string string (nil "B"))
126                               'base64-encode-string)
127   (mel-define-method encoded-text-decode-string (string (nil "B"))
128     (if (and (string-match B-encoded-text-regexp string)
129              (string= string (match-string 0 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
146 (defvar mel-q-ccl-module
147   (and (featurep 'mule)
148        (progn
149          (require 'path-util)
150          (module-installed-p 'mel-q-ccl)
151          )))
152
153 (if mel-b-ccl-module
154     (mel-use-module 'mel-b-ccl '("base64" "B"))
155   )
156
157 (if mel-q-ccl-module
158     (mel-use-module 'mel-q-ccl '("quoted-printable" "Q"))
159   )
160
161 (if base64-dl-module
162     (mel-use-module 'mel-b-dl '("base64" "B"))
163   )
164
165
166 ;;; @ region
167 ;;;
168
169 ;;;###autoload
170 (defun mime-encode-region (start end encoding)
171   "Encode region START to END of current buffer using ENCODING.
172 ENCODING must be string."
173   (interactive
174    (list (region-beginning) (region-end)
175          (completing-read "encoding: "
176                           (mime-encoding-alist)
177                           nil t "base64")))
178   (funcall (mel-find-function 'mime-encode-region encoding) start end)
179   )
180
181
182 ;;;###autoload
183 (defun mime-decode-region (start end encoding)
184   "Decode region START to END of current buffer using ENCODING.
185 ENCODING must be string."
186   (interactive
187    (list (region-beginning) (region-end)
188          (completing-read "encoding: "
189                           (mime-encoding-alist 'mime-decode-region)
190                           nil t "base64")))
191   (funcall (mel-find-function 'mime-decode-region encoding)
192            start end))
193
194
195 ;;; @ string
196 ;;;
197
198 ;;;###autoload
199 (defun mime-decode-string (string encoding)
200   "Decode STRING using ENCODING.
201 ENCODING must be string.  If ENCODING is found in
202 `mime-string-decoding-method-alist' as its key, this function decodes
203 the STRING by its value."
204   (funcall (mel-find-function 'mime-decode-string encoding)
205            string))
206
207
208 (mel-define-service encoded-text-encode-string (string encoding)
209   "Encode STRING as encoded-text using ENCODING.
210 ENCODING must be string.")
211
212 (mel-define-service encoded-text-decode-string (string encoding)
213   "Decode STRING as encoded-text using ENCODING.
214 ENCODING must be string.")
215
216 (defun base64-encoded-length (string)
217   (* (/ (+ (length string) 2) 3) 4))
218
219 (defsubst Q-encoding-printable-char-p (chr mode)
220   (and (not (memq chr '(?= ?? ?_)))
221        (<= ?\   chr)(<= chr ?~)
222        (cond ((eq mode 'text) t)
223              ((eq mode 'comment)
224               (not (memq chr '(?\( ?\) ?\\)))
225               )
226              (t
227               (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
228               ))))
229
230 (defun Q-encoded-text-length (string &optional mode)
231   (let ((l 0)(i 0)(len (length string)) chr)
232     (while (< i len)
233       (setq chr (elt string i))
234       (if (Q-encoding-printable-char-p chr mode)
235           (setq l (+ l 1))
236         (setq l (+ l 3))
237         )
238       (setq i (+ i 1)) )
239     l))
240
241
242 ;;; @ file
243 ;;;
244
245 ;;;###autoload
246 (defun mime-insert-encoded-file (filename encoding)
247   "Insert file FILENAME encoded by ENCODING format."
248   (interactive
249    (list (read-file-name "Insert encoded file: ")
250          (completing-read "encoding: "
251                           (mime-encoding-alist)
252                           nil t "base64")))
253   (funcall (mel-find-function 'mime-insert-encoded-file encoding)
254            filename))
255
256
257 ;;;###autoload
258 (defun mime-write-decoded-region (start end filename encoding)
259   "Decode and write current region encoded by ENCODING into FILENAME.
260 START and END are buffer positions."
261   (interactive
262    (list (region-beginning) (region-end)
263          (read-file-name "Write decoded region to file: ")
264          (completing-read "encoding: "
265                           (mime-encoding-alist 'mime-write-decoded-region)
266                           nil t "base64")))
267   (funcall (mel-find-function 'mime-write-decoded-region encoding)
268            start end filename))
269
270
271 ;;; @ end
272 ;;;
273
274 (provide 'mel)
275
276 ;;; mel.el ends here.