bd4ea111c663286eb46424e9572bc3ed0bf4c969
[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
30 (defcustom mime-encoding-list
31   '("7bit" "8bit" "binary" "base64" "quoted-printable")
32   "List of Content-Transfer-Encoding.  Each encoding must be string."
33   :group 'mime
34   :type '(repeat string))
35
36 (defun mime-encoding-list (&optional service)
37   "Return list of Content-Transfer-Encoding.
38 If SERVICE is specified, it returns available list of
39 Content-Transfer-Encoding for it."
40   (if service
41       (let (dest)
42         (mapatoms (lambda (sym)
43                     (or (eq sym nil)
44                         (setq dest (cons (symbol-name sym) dest)))
45                     )
46                   (symbol-value (intern (format "%s-obarray" service))))
47         (let ((rest mel-encoding-module-alist)
48               pair)
49           (while (setq pair (car rest))
50             (let ((key (car pair)))
51               (or (member key dest)
52                   (<= (length key) 1)
53                   (setq dest (cons key dest))))
54             (setq rest (cdr rest)))
55           )
56         dest)
57     mime-encoding-list))
58
59 (defun mime-encoding-alist (&optional service)
60   "Return table of Content-Transfer-Encoding for completion."
61   (mapcar #'list (mime-encoding-list service))
62   )
63
64 (defsubst mel-use-module (name encodings)
65   (let (encoding)
66     (while (setq encoding (car encodings))
67       (set-alist 'mel-encoding-module-alist
68                  encoding
69                  (cons name (cdr (assoc encoding mel-encoding-module-alist))))
70       (setq encodings (cdr encodings))
71       )))
72
73 (defsubst mel-find-function (service encoding)
74   (mel-find-function-from-obarray
75    (symbol-value (intern (format "%s-obarray" service))) encoding))
76
77
78 ;;; @ setting for modules
79 ;;;
80
81 (defvar mel-ccl-module
82   (and (featurep 'mule)
83        (progn
84          (require 'path-util)
85          (module-installed-p 'mel-ccl)
86          )))
87
88 (mel-use-module 'mel-b '("base64" "B"))
89 (mel-use-module 'mel-q '("quoted-printable" "Q"))
90 (mel-use-module 'mel-g '("x-gzip64"))
91 (mel-use-module 'mel-u '("x-uue" "x-uuencode"))
92
93 (if mel-ccl-module
94     (mel-use-module 'mel-ccl '("base64" "quoted-printable" "B" "Q"))
95   )
96
97 (if base64-dl-module
98     (mel-use-module 'mel-b-dl '("base64" "B"))
99   )
100
101 (mel-define-backend "7bit")
102 (mel-define-method-function (mime-encode-string string (nil "7bit"))
103                             'identity)
104 (mel-define-method-function (mime-decode-string string (nil "7bit"))
105                             'identity)
106 (mel-define-method mime-encode-region (start end (nil "7bit")))
107 (mel-define-method mime-decode-region (start end (nil "7bit")))
108 (mel-define-method-function (mime-insert-encoded-file filename (nil "7bit"))
109                             'insert-file-contents-as-binary)
110 (mel-define-method-function (mime-write-decoded-region
111                              start end filename (nil "7bit"))
112                             'write-region-as-binary)
113
114 (mel-define-backend "8bit" ("7bit"))
115
116 (mel-define-backend "binary" ("8bit"))
117
118
119 ;;; @ region
120 ;;;
121
122 ;;;###autoload
123 (defun mime-encode-region (start end encoding)
124   "Encode region START to END of current buffer using ENCODING.
125 ENCODING must be string."
126   (interactive
127    (list (region-beginning) (region-end)
128          (completing-read "encoding: "
129                           (mime-encoding-alist)
130                           nil t "base64")))
131   (funcall (mel-find-function 'mime-encode-region encoding) start end)
132   )
133
134
135 ;;;###autoload
136 (defun mime-decode-region (start end encoding)
137   "Decode region START to END of current buffer using ENCODING.
138 ENCODING must be string."
139   (interactive
140    (list (region-beginning) (region-end)
141          (completing-read "encoding: "
142                           (mime-encoding-alist 'mime-decode-region)
143                           nil t "base64")))
144   (funcall (mel-find-function 'mime-decode-region encoding)
145            start end))
146
147
148 ;;; @ string
149 ;;;
150
151 ;;;###autoload
152 (defun mime-decode-string (string encoding)
153   "Decode STRING using ENCODING.
154 ENCODING must be string.  If ENCODING is found in
155 `mime-string-decoding-method-alist' as its key, this function decodes
156 the STRING by its value."
157   (funcall (mel-find-function 'mime-decode-string encoding)
158            string))
159
160
161 (mel-define-service encoded-text-encode-string (string encoding)
162   "Encode STRING as encoded-text using ENCODING.
163 ENCODING must be string.")
164
165 (mel-define-service encoded-text-decode-string (string encoding)
166   "Decode STRING as encoded-text using ENCODING.
167 ENCODING must be string.")
168
169 (defun base64-encoded-length (string)
170   (let ((len (length string)))
171     (* (+ (/ len 3)
172           (if (= (mod len 3) 0) 0 1)
173           ) 4)
174     ))
175
176 (defsubst Q-encoding-printable-char-p (chr mode)
177   (and (not (memq chr '(?= ?? ?_)))
178        (<= ?\   chr)(<= chr ?~)
179        (cond ((eq mode 'text) t)
180              ((eq mode 'comment)
181               (not (memq chr '(?\( ?\) ?\\)))
182               )
183              (t
184               (string-match "[A-Za-z0-9!*+/=_---]" (char-to-string chr))
185               ))))
186
187 (defun Q-encoded-text-length (string &optional mode)
188   (let ((l 0)(i 0)(len (length string)) chr)
189     (while (< i len)
190       (setq chr (elt string i))
191       (if (Q-encoding-printable-char-p chr mode)
192           (setq l (+ l 1))
193         (setq l (+ l 3))
194         )
195       (setq i (+ i 1)) )
196     l))
197
198
199 ;;; @ file
200 ;;;
201
202 ;;;###autoload
203 (defun mime-insert-encoded-file (filename encoding)
204   "Insert file FILENAME encoded by ENCODING format."
205   (interactive
206    (list (read-file-name "Insert encoded file: ")
207          (completing-read "encoding: "
208                           (mime-encoding-alist)
209                           nil t "base64")))
210   (funcall (mel-find-function 'mime-insert-encoded-file encoding)
211            filename))
212
213
214 ;;;###autoload
215 (defun mime-write-decoded-region (start end filename encoding)
216   "Decode and write current region encoded by ENCODING into FILENAME.
217 START and END are buffer positions."
218   (interactive
219    (list (region-beginning) (region-end)
220          (read-file-name "Write decoded region to file: ")
221          (completing-read "encoding: "
222                           (mime-encoding-alist 'mime-write-decoded-region)
223                           nil t "base64")))
224   (funcall (mel-find-function 'mime-write-decoded-region encoding)
225            start end filename))
226
227
228 ;;; @ end
229 ;;;
230
231 (provide 'mel)
232
233 ;;; mel.el ends here.