*** empty log message ***
[gnus] / lisp / rfc2047.el
1 ;;; rfc2047.el --- Functions for encoding and decoding rfc2047 messages
2 ;; Copyright (C) 1998 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-and-compile
28   (if (not (fboundp 'base64-encode-string))
29       (require 'base64)))
30 (require 'qp)
31 (require 'mm-util)
32
33 (defvar rfc2047-unencoded-charsets '(ascii latin-iso8859-1)
34   "List of MULE charsets not to encode.")
35
36 (defvar rfc2047-header-encoding-alist
37   '(("Newsgroups" . nil)
38     ("Message-ID" . nil)
39     (t . mime))
40   "*Header/encoding method alist.
41 The list is traversed sequentially.  The keys can either be
42 header regexps or `t'.
43
44 The values can be:
45
46 1) nil, in which case no encoding is done;
47 2) `mime', in which case the header will be encoded according to RFC2047;
48 3) a charset, in which case it will be encoded as that charse;
49 4) `default', in which case the field will be encoded as the rest
50    of the article.")
51
52 (defvar rfc2047-charset-encoding-alist
53   '((us-ascii . nil)
54     (iso-8859-1 . Q)
55     (iso-8859-2 . Q)
56     (iso-8859-3 . Q)
57     (iso-8859-4 . Q)
58     (iso-8859-5 . Q)
59     (koi8-r . Q)
60     (iso-8859-7 . Q)
61     (iso-8859-8 . Q)
62     (iso-8859-9 . Q)
63     (iso-2022-jp . B)
64     (iso-2022-kr . B)
65     (gb2312 . B)
66     (cn-gb . B)
67     (cn-gb-2312 . B)
68     (euc-kr . B)
69     (iso-2022-jp-2 . B)
70     (iso-2022-int-1 . B))
71   "Alist of MIME charsets to RFC2047 encodings.
72 Valid encodings are nil, `Q' and `B'.")
73
74 (defvar rfc2047-encoding-function-alist
75   '((Q . rfc2047-q-encode-region)
76     (B . base64-encode-region)
77     (nil . ignore))
78   "Alist of RFC2047 encodings to encoding functions.")
79
80 (defvar rfc2047-q-encoding-alist
81   '(("\\(From\\|Cc\\|To\\|Bcc\||Reply-To\\):" . "[^-A-Za-z0-9!*+/=_]")
82     ("." . "[\000-\007\013\015-\037\200-\377=_?]"))
83   "Alist of header regexps and valid Q characters.")
84
85 ;;;
86 ;;; Functions for encoding RFC2047 messages
87 ;;;
88
89 (defun rfc2047-narrow-to-field ()
90   "Narrow the buffer to the header on the current line."
91   (beginning-of-line)
92   (narrow-to-region
93    (point)
94    (progn
95      (forward-line 1)
96      (if (re-search-forward "^[^ \n\t]" nil t)
97          (progn
98            (beginning-of-line)
99            (point))
100        (point-max))))
101   (goto-char (point-min)))
102
103 ;;;###autoload
104 (defun rfc2047-encode-message-header ()
105   "Encode the message header according to `rfc2047-header-encoding-alist'.
106 Should be called narrowed to the head of the message."
107   (interactive "*")
108   (when (featurep 'mule)
109     (save-excursion
110       (let ((alist rfc2047-header-encoding-alist)
111             elem method)
112         (while (not (eobp))
113           (save-restriction
114             (rfc2047-narrow-to-field)
115             (when (rfc2047-encodable-p)
116               ;; We found something that may perhaps be encoded.
117               (while (setq elem (pop alist))
118                 (when (or (and (stringp (car elem))
119                                (looking-at (car elem)))
120                           (eq (car elem) t))
121                   (setq alist nil
122                         method (cdr elem))))
123               (when method
124                 (cond
125                  ((eq method 'mime)
126                   (rfc2047-encode-region (point-min) (point-max)))
127                  ;; Hm.
128                  (t))))
129             (goto-char (point-max))))))))
130
131 (defun rfc2047-encodable-p ()
132   "Say whether the current (narrowed) buffer contains characters that need encoding."
133   (let ((charsets (find-charset-region (point-min) (point-max)))
134         (cs rfc2047-unencoded-charsets)
135         found)
136     (while charsets
137       (unless (memq (pop charsets) cs)
138         (setq found t)))
139     found))
140
141 (defun rfc2047-encode-region (b e)
142   "Encode all encodable words in REGION."
143   (let (prev c start qstart qprev qend)
144     (save-excursion
145       (goto-char b)
146       (while (re-search-forward "[^ \t\n]+" nil t)
147         (save-restriction
148           (narrow-to-region (match-beginning 0) (match-end 0))
149           (goto-char (setq start (point-min)))
150           (setq prev nil)
151           (while (not (eobp))
152             (unless (eq (setq c (char-charset (following-char))) 'ascii)
153               (cond
154                ((eq c prev)
155                 )
156                ((null prev)
157                 (setq qstart (or qstart start)
158                       qend (point-max)
159                       qprev c)
160                 (setq prev c))
161                (t
162                 ;(rfc2047-encode start (setq start (point)) prev)
163                 (setq prev c))))
164             (forward-char 1)))
165         (when (and (not prev) qstart)
166           (rfc2047-encode qstart qend qprev)
167           (setq qstart nil)))
168       (when qstart
169         (rfc2047-encode qstart qend qprev)
170         (setq qstart nil)))))
171
172 (defun rfc2047-encode-string (string)
173   "Encode words in STRING."
174   (with-temp-buffer
175     (insert string)
176     (rfc2047-encode-region (point-min) (point-max))
177     (buffer-string)))
178
179 (defun rfc2047-encode (b e charset)
180   "Encode the word in the region with CHARSET."
181   (let* ((mime-charset (mm-mule-charset-to-mime-charset charset))
182          (encoding (cdr (assq mime-charset
183                               rfc2047-charset-encoding-alist)))
184          (start (concat
185                  "=?" (downcase (symbol-name mime-charset)) "?"
186                  (downcase (symbol-name encoding)) "?")))
187     (save-restriction
188       (narrow-to-region b e)
189       (mm-encode-coding-region b e mime-charset)
190       (funcall (cdr (assq encoding rfc2047-encoding-function-alist))
191                (point-min) (point-max))
192       (goto-char (point-min))
193       (insert start)
194       (goto-char (point-max))
195       (insert "?=")
196       ;; Encoded words can't be more than 75 chars long, so we have to
197       ;; split the long ones up.
198       (end-of-line)
199       (while (> (current-column) 74)
200         (beginning-of-line)
201         (forward-char 73)
202         (insert "?=\n " start)
203         (end-of-line)))))
204
205 (defun rfc2047-q-encode-region (b e)
206   "Encode the header contained in REGION with the Q encoding."
207   (save-excursion
208     (save-restriction
209       (narrow-to-region (goto-char b) e)
210       (let ((alist rfc2047-q-encoding-alist))
211         (while alist
212           (when (looking-at (caar alist))
213             (quoted-printable-encode-region b e nil (cdar alist))
214             (subst-char-in-region (point-min) (point-max) ?  ?_))
215           (pop alist))))))
216
217 ;;;
218 ;;; Functions for decoding RFC2047 messages
219 ;;;
220
221 (defvar rfc2047-encoded-word-regexp
222   "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~]+\\)\\?=")
223
224 ;;;###autoload
225 (defun rfc2047-decode-region (start end)
226   "Decode MIME-encoded words in region between START and END."
227   (interactive "r")
228   (save-excursion
229     (save-restriction
230       (narrow-to-region start end)
231       (goto-char (point-min))
232       ;; Remove whitespace between encoded words.
233       (while (re-search-forward
234               (concat "\\(" rfc2047-encoded-word-regexp "\\)"
235                       "\\(\n?[ \t]\\)+"
236                       "\\(" rfc2047-encoded-word-regexp "\\)")
237               nil t)
238         (delete-region (goto-char (match-end 1)) (match-beginning 6)))
239       ;; Decode the encoded words.
240       (goto-char (point-min))
241       (while (re-search-forward rfc2047-encoded-word-regexp nil t)
242         (insert (rfc2047-parse-and-decode
243                  (prog1
244                      (match-string 0)
245                    (delete-region (match-beginning 0) (match-end 0)))))))))
246
247 ;;;###autoload
248 (defun rfc2047-decode-string (string)
249  "Decode the quoted-printable-encoded STRING and return the results."
250  (with-temp-buffer
251    (mm-enable-multibyte)
252    (insert string)
253    (inline
254      (rfc2047-decode-region (point-min) (point-max)))
255    (buffer-string)))
256
257 (defun rfc2047-parse-and-decode (word)
258   "Decode WORD and return it if it is an encoded word.
259 Return WORD if not."
260   (if (not (string-match rfc2047-encoded-word-regexp word))
261       word
262     (or
263      (condition-case nil
264          (rfc2047-decode
265           (match-string 1 word)
266           (upcase (match-string 2 word))
267           (match-string 3 word))
268        (error word))
269      word)))
270
271 (defun rfc2047-decode (charset encoding string)
272   "Decode STRING that uses CHARSET with ENCODING.
273 Valid ENCODINGs are \"B\" and \"Q\".
274 If your Emacs implementation can't decode CHARSET, it returns nil."
275   (let ((cs (mm-charset-to-coding-system charset)))
276     (when cs
277       (mm-decode-coding-string
278        (cond
279         ((equal "B" encoding)
280          (base64-decode string))
281         ((equal "Q" encoding)
282          (quoted-printable-decode-string
283           (mm-replace-chars-in-string string ?_ ? )))
284         (t (error "Invalid encoding: %s" encoding)))
285        cs))))
286
287 (provide 'rfc2047)
288
289 ;;; rfc2047.el ends here