51d611c955bc8c0fb01a11a68000fb30dbb8c008
[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
83   (and (featurep 'mule)
84        (progn
85          (require 'path-util)
86          (module-installed-p 'mel-ccl)
87          )))
88
89 (mel-use-module 'mel-b '("base64" "B"))
90 (mel-use-module 'mel-q '("quoted-printable" "Q"))
91 (mel-use-module 'mel-g '("x-gzip64"))
92 (mel-use-module 'mel-u '("x-uue" "x-uuencode"))
93
94 (if mel-ccl-module
95     (mel-use-module 'mel-ccl '("base64" "quoted-printable" "B" "Q"))
96   )
97
98 (if base64-dl-module
99     (mel-use-module 'mel-b-dl '("base64" "B"))
100   )
101
102 (mel-define-backend "7bit")
103 (mel-define-method-function (mime-encode-string string (nil "7bit"))
104                             'identity)
105 (mel-define-method-function (mime-decode-string string (nil "7bit"))
106                             'identity)
107 (mel-define-method mime-encode-region (start end (nil "7bit")))
108 (mel-define-method mime-decode-region (start end (nil "7bit")))
109 (mel-define-method-function (mime-insert-encoded-file filename (nil "7bit"))
110                             'insert-file-contents-as-binary)
111 (mel-define-method-function (mime-write-decoded-region
112                              start end filename (nil "7bit"))
113                             'write-region-as-binary)
114
115 (mel-define-backend "8bit" ("7bit"))
116
117 (mel-define-backend "binary" ("8bit"))
118
119
120 ;;; @ region
121 ;;;
122
123 ;;;###autoload
124 (defun mime-encode-region (start end encoding)
125   "Encode region START to END of current buffer using ENCODING.
126 ENCODING must be string."
127   (interactive
128    (list (region-beginning) (region-end)
129          (completing-read "encoding: "
130                           (mime-encoding-alist)
131                           nil t "base64")))
132   (funcall (mel-find-function 'mime-encode-region encoding) start end)
133   )
134
135
136 ;;;###autoload
137 (defun mime-decode-region (start end encoding)
138   "Decode 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 'mime-decode-region)
144                           nil t "base64")))
145   (funcall (mel-find-function 'mime-decode-region encoding)
146            start end))
147
148
149 ;;; @ string
150 ;;;
151
152 ;;;###autoload
153 (defun mime-decode-string (string encoding)
154   "Decode STRING using ENCODING.
155 ENCODING must be string.  If ENCODING is found in
156 `mime-string-decoding-method-alist' as its key, this function decodes
157 the STRING by its value."
158   (funcall (mel-find-function 'mime-decode-string encoding)
159            string))
160
161
162 (mel-define-service encoded-text-encode-string (string encoding)
163   "Encode STRING as encoded-text using ENCODING.
164 ENCODING must be string.")
165
166 (mel-define-service encoded-text-decode-string (string encoding)
167   "Decode STRING as encoded-text using ENCODING.
168 ENCODING must be string.")
169
170 (defun base64-encoded-length (string)
171   (let ((len (length string)))
172     (* (+ (/ len 3)
173           (if (= (mod len 3) 0) 0 1)
174           ) 4)
175     ))
176
177 (defsubst Q-encoding-printable-char-p (chr mode)
178   (and (not (memq chr '(?= ?? ?_)))
179        (<= ?\   chr)(<= chr ?~)
180        (cond ((eq mode 'text) t)
181              ((eq mode 'comment)
182               (not (memq chr '(?\( ?\) ?\\)))
183               )
184              (t
185               (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
186               ))))
187
188 (defun Q-encoded-text-length (string &optional mode)
189   (let ((l 0)(i 0)(len (length string)) chr)
190     (while (< i len)
191       (setq chr (elt string i))
192       (if (Q-encoding-printable-char-p chr mode)
193           (setq l (+ l 1))
194         (setq l (+ l 3))
195         )
196       (setq i (+ i 1)) )
197     l))
198
199
200 ;;; @ file
201 ;;;
202
203 ;;;###autoload
204 (defun mime-insert-encoded-file (filename encoding)
205   "Insert file FILENAME encoded by ENCODING format."
206   (interactive
207    (list (read-file-name "Insert encoded file: ")
208          (completing-read "encoding: "
209                           (mime-encoding-alist)
210                           nil t "base64")))
211   (funcall (mel-find-function 'mime-insert-encoded-file encoding)
212            filename))
213
214
215 ;;;###autoload
216 (defun mime-write-decoded-region (start end filename encoding)
217   "Decode and write current region encoded by ENCODING into FILENAME.
218 START and END are buffer positions."
219   (interactive
220    (list (region-beginning) (region-end)
221          (read-file-name "Write decoded region to file: ")
222          (completing-read "encoding: "
223                           (mime-encoding-alist 'mime-write-decoded-region)
224                           nil t "base64")))
225   (funcall (mel-find-function 'mime-write-decoded-region encoding)
226            start end filename))
227
228
229 ;;; @ end
230 ;;;
231
232 (provide 'mel)
233
234 ;;; mel.el ends here.