*** empty log message ***
[gnus] / lisp / rfc2047.el
1 ;;; rfc2047.el --- Functions for encoding and decoding rfc2047 messages
2 ;; Copyright (C) 1998,99 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   (eval
29    '(unless (fboundp 'base64-decode-string)
30       (require 'base64))))
31
32 (require 'qp)
33 (require 'mm-util)
34 (require 'ietf-drums)
35 (require 'mail-prsvr)
36
37 (defvar rfc2047-header-encoding-alist
38   '(("Newsgroups" . nil)
39     ("Message-ID" . nil)
40     (t . mime))
41   "*Header/encoding method alist.
42 The list is traversed sequentially.  The keys can either be
43 header regexps or `t'.
44
45 The values can be:
46
47 1) nil, in which case no encoding is done;
48 2) `mime', in which case the header will be encoded according to RFC2047;
49 3) a charset, in which case it will be encoded as that charse;
50 4) `default', in which case the field will be encoded as the rest
51    of the article.")
52
53 (defvar rfc2047-charset-encoding-alist
54   '((us-ascii . nil)
55     (iso-8859-1 . Q)
56     (iso-8859-2 . Q)
57     (iso-8859-3 . Q)
58     (iso-8859-4 . Q)
59     (iso-8859-5 . B)
60     (koi8-r . B)
61     (iso-8859-7 . Q)
62     (iso-8859-8 . Q)
63     (iso-8859-9 . Q)
64     (iso-2022-jp . B)
65     (iso-2022-kr . B)
66     (gb2312 . B)
67     (cn-gb . B)
68     (cn-gb-2312 . B)
69     (euc-kr . B)
70     (iso-2022-jp-2 . B)
71     (iso-2022-int-1 . B))
72   "Alist of MIME charsets to RFC2047 encodings.
73 Valid encodings are nil, `Q' and `B'.")
74
75 (defvar rfc2047-encoding-function-alist
76   '((Q . rfc2047-q-encode-region)
77     (B . rfc2047-b-encode-region)
78     (nil . ignore))
79   "Alist of RFC2047 encodings to encoding functions.")
80
81 (defvar rfc2047-q-encoding-alist
82   '(("\\(From\\|Cc\\|To\\|Bcc\||Reply-To\\):" . "-A-Za-z0-9!*+/=_")
83     ("." . "^\000-\007\013\015-\037\200-\377=_?"))
84   "Alist of header regexps and valid Q characters.")
85
86 ;;;
87 ;;; Functions for encoding RFC2047 messages
88 ;;;
89
90 (defun rfc2047-narrow-to-field ()
91   "Narrow the buffer to the header on the current line."
92   (beginning-of-line)
93   (narrow-to-region
94    (point)
95    (progn
96      (forward-line 1)
97      (if (re-search-forward "^[^ \n\t]" nil t)
98          (progn
99            (beginning-of-line)
100            (point))
101        (point-max))))
102   (goto-char (point-min)))
103
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       (goto-char (point-min))
111       (let ((alist rfc2047-header-encoding-alist)
112             elem method)
113         (while (not (eobp))
114           (save-restriction
115             (rfc2047-narrow-to-field)
116             (when (rfc2047-encodable-p)
117               ;; We found something that may perhaps be encoded.
118               (while (setq elem (pop alist))
119                 (when (or (and (stringp (car elem))
120                                (looking-at (car elem)))
121                           (eq (car elem) t))
122                   (setq alist nil
123                         method (cdr elem))))
124               (when method
125                 (cond
126                  ((eq method 'mime)
127                   (rfc2047-encode-region (point-min) (point-max)))
128                  ;; Hm.
129                  (t))))
130             (goto-char (point-max)))))
131       (when mail-parse-charset
132         (encode-coding-region (point-min) (point-max)
133                               mail-parse-charset)))))
134
135 (defun rfc2047-encodable-p ()
136   "Say whether the current (narrowed) buffer contains characters that need encoding."
137   (let ((charsets
138          (mapcar
139           'mm-mime-charset
140           (mm-find-charset-region (point-min) (point-max))))
141         (cs (list 'us-ascii mail-parse-charset))
142         found)
143     (while charsets
144       (unless (memq (pop charsets) cs)
145         (setq found t)))
146     found))
147
148 (defun rfc2047-dissect-region (b e)
149   "Dissect the region between B and E into words."
150   (let (words)
151     (save-restriction
152       (narrow-to-region b e)
153       (goto-char (point-min))
154       (while (re-search-forward
155               (concat "[^" ietf-drums-tspecials " \t\n]+") nil t)
156         (push
157          (list (match-beginning 0) (match-end 0)
158                (car (delq 'ascii (mm-find-charset-region
159                                   (match-beginning 0) (match-end 0)))))
160          words))
161       words)))
162
163 (defun rfc2047-encode-region (b e)
164   "Encode all encodable words in REGION."
165   (let ((words (rfc2047-dissect-region b e))
166         beg end current word)
167     (while (setq word (pop words))
168       (if (equal (nth 2 word) current)
169           (setq beg (nth 0 word))
170         (when current
171           (rfc2047-encode beg end current))
172         (setq current (nth 2 word)
173               beg (nth 0 word)
174               end (nth 1 word))))
175     (when current
176       (rfc2047-encode beg end current))))
177
178 (defun rfc2047-encode-string (string)
179   "Encode words in STRING."
180   (with-temp-buffer
181     (insert string)
182     (rfc2047-encode-region (point-min) (point-max))
183     (buffer-string)))
184
185 (defun rfc2047-encode (b e charset)
186   "Encode the word in the region with CHARSET."
187   (let* ((mime-charset (mm-mime-charset charset))
188          (encoding (or (cdr (assq mime-charset
189                                   rfc2047-charset-encoding-alist))
190                        'B))
191          (start (concat
192                  "=?" (downcase (symbol-name mime-charset)) "?"
193                  (downcase (symbol-name encoding)) "?"))
194          (first t))
195     (save-restriction
196       (narrow-to-region b e)
197       (mm-encode-coding-region b e mime-charset)
198       (funcall (cdr (assq encoding rfc2047-encoding-function-alist))
199                (point-min) (point-max))
200       (goto-char (point-min))
201       (while (not (eobp))
202         (unless first
203           (insert " "))
204         (setq first nil)
205         (insert start)
206         (end-of-line)
207         (insert "?=")
208         (forward-line 1)))))
209
210 (defun rfc2047-b-encode-region (b e)
211   "Encode the header contained in REGION with the B encoding."
212   (base64-encode-region b e t)
213   (goto-char (point-min))
214   (while (not (eobp))
215     (goto-char (min (point-max) (+ 64 (point))))
216     (unless (eobp)
217       (insert "\n"))))
218
219 (defun rfc2047-q-encode-region (b e)
220   "Encode the header contained in REGION with the Q encoding."
221   (save-excursion
222     (save-restriction
223       (narrow-to-region (goto-char b) e)
224       (let ((alist rfc2047-q-encoding-alist))
225         (while alist
226           (when (looking-at (caar alist))
227             (quoted-printable-encode-region b e nil (cdar alist))
228             (subst-char-in-region (point-min) (point-max) ?  ?_)
229             (setq alist nil))
230           (pop alist))
231         (goto-char (point-min))
232         (while (not (eobp))
233           (goto-char (min (point-max) (+ 64 (point))))
234           (search-backward "=" (- (point) 2) t)
235           (unless (eobp)
236             (insert "\n")))))))
237
238 ;;;
239 ;;; Functions for decoding RFC2047 messages
240 ;;;
241
242 (defvar rfc2047-encoded-word-regexp
243   "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ +]+\\)\\?=")
244
245 (defun rfc2047-decode-region (start end)
246   "Decode MIME-encoded words in region between START and END."
247   (interactive "r")
248   (let ((case-fold-search t)
249         b e)
250     (save-excursion
251       (save-restriction
252         (narrow-to-region start end)
253         (goto-char (point-min))
254         ;; Remove whitespace between encoded words.
255         (while (re-search-forward
256                 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
257                         "\\(\n?[ \t]\\)+"
258                         "\\(" rfc2047-encoded-word-regexp "\\)")
259                 nil t)
260           (delete-region (goto-char (match-end 1)) (match-beginning 6)))
261         ;; Decode the encoded words.
262         (setq b (goto-char (point-min)))
263         (while (re-search-forward rfc2047-encoded-word-regexp nil t)
264           (setq e (match-beginning 0))
265           (insert (rfc2047-parse-and-decode
266                    (prog1
267                        (match-string 0)
268                      (delete-region (match-beginning 0) (match-end 0)))))
269           (when (and (mm-multibyte-p)
270                      mail-parse-charset)
271             (mm-decode-coding-region b e mail-parse-charset))
272           (setq b (point)))
273         (when (and (mm-multibyte-p)
274                    mail-parse-charset
275                    (not (eq mail-parse-charset 'us-ascii)))
276           (mm-decode-coding-region b (point-max) mail-parse-charset))))))
277
278 (defun rfc2047-decode-string (string)
279   "Decode the quoted-printable-encoded STRING and return the results."
280   (let ((m (mm-multibyte-p)))
281     (with-temp-buffer
282       (when m
283         (mm-enable-multibyte))
284       (insert string)
285       (inline
286         (rfc2047-decode-region (point-min) (point-max)))
287       (buffer-string))))
288
289 (defun rfc2047-parse-and-decode (word)
290   "Decode WORD and return it if it is an encoded word.
291 Return WORD if not."
292   (if (not (string-match rfc2047-encoded-word-regexp word))
293       word
294     (or
295      (condition-case nil
296          (rfc2047-decode
297           (match-string 1 word)
298           (upcase (match-string 2 word))
299           (match-string 3 word))
300        (error word))
301      word)))
302
303 (defun rfc2047-decode (charset encoding string)
304   "Decode STRING that uses CHARSET with ENCODING.
305 Valid ENCODINGs are \"B\" and \"Q\".
306 If your Emacs implementation can't decode CHARSET, it returns nil."
307   (let ((cs (mm-charset-to-coding-system charset)))
308     (when cs
309       (when (and (eq cs 'ascii)
310                  mail-parse-charset)
311         (setq cs mail-parse-charset))
312       (mm-decode-coding-string
313        (cond
314         ((equal "B" encoding)
315          (base64-decode-string string))
316         ((equal "Q" encoding)
317          (quoted-printable-decode-string
318           (mm-replace-chars-in-string string ?_ ? )))
319         (t (error "Invalid encoding: %s" encoding)))
320        cs))))
321
322 (provide 'rfc2047)
323
324 ;;; rfc2047.el ends here