1 ;;; rfc1522.el --- Functions for encoding and decoding rfc1522 messages
2 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; This file is part of GNU Emacs.
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)
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.
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.
31 (defvar rfc1522-header-encoding-alist
32 '(("Newsgroups" . nil)
35 "*Header/encoding method alist.
36 The list is traversed sequentially. The keys can either be
37 header regexps or `t'.
41 1) nil, in which case no encoding is done;
42 2) `mime', in which case the header will be encoded according to RFC1522;
43 3) a charset, in which case it will be encoded as that charse;
44 4) `default', in which case the field will be encoded as the rest
47 (defvar rfc1522-charset-encoding-alist
66 "Alist of MIME charsets to RFC1522 encodings.
67 Valid encodings are nil, `Q' and `B'.")
69 (defvar rfc1522-encoding-function-alist
70 '((Q . rfc1522-q-encode-region)
71 (B . base64-encode-region)
73 "Alist of RFC1522 encodings to encoding functions.")
75 (defvar rfc1522-q-encoding-alist
76 '(("\\(From\\|Cc\\|To\\|Bcc\||Reply-To\\):" . "[^-A-Za-z0-9!*+/=_]")
77 ("." . "[\000-\007\013\015-\037\200-\377=_?]"))
78 "Alist of header regexps and valid Q characters.")
81 ;;; Functions for encoding RFC1522 messages
84 (defun rfc1522-narrow-to-field ()
85 "Narrow the buffer to the header on the current line."
91 (if (re-search-forward "^[^ \n\t]" nil t)
96 (goto-char (point-min)))
99 (defun rfc1522-encode-message-header ()
100 "Encode the message header according to `rfc1522-header-encoding-alist'.
101 Should be called narrowed to the head of the message."
103 (when (featurep 'mule)
105 (let ((alist rfc1522-header-encoding-alist)
109 (rfc1522-narrow-to-field)
110 (when (find-non-ascii-charset-region (point-min) (point-max))
111 ;; We found something that may perhaps be encoded.
112 (while (setq elem (pop alist))
113 (when (or (and (stringp (car elem))
114 (looking-at (car elem)))
121 (rfc1522-encode-region (point-min) (point-max)))
124 (goto-char (point-max))))))))
126 (defun rfc1522-encode-region (b e)
127 "Encode all encodable words in REGION."
128 (let (prev c start qstart qprev qend)
131 (while (re-search-forward "[^ \t\n]+" nil t)
133 (narrow-to-region (match-beginning 0) (match-end 0))
134 (goto-char (setq start (point-min)))
137 (unless (eq (setq c (char-charset (following-char))) 'ascii)
142 (setq qstart (or qstart start)
147 ;(rfc1522-encode start (setq start (point)) prev)
150 (when (and (not prev) qstart)
151 (rfc1522-encode qstart qend qprev)
154 (rfc1522-encode qstart qend qprev)
155 (setq qstart nil)))))
157 (defun rfc1522-encode-string (string)
158 "Encode words in STRING."
161 (rfc1522-encode-region (point-min) (point-max))
164 (defun rfc1522-encode (b e charset)
165 "Encode the word in the region with CHARSET."
166 (let* ((mime-charset (mm-mule-charset-to-mime-charset charset))
167 (encoding (cdr (assq mime-charset
168 rfc1522-charset-encoding-alist)))
170 "=?" (downcase (symbol-name mime-charset)) "?"
171 (downcase (symbol-name encoding)) "?")))
173 (narrow-to-region b e)
176 (mm-encode-coding-string (buffer-string) mime-charset)
177 (delete-region (point-min) (point-max))))
178 (funcall (cdr (assq encoding rfc1522-encoding-function-alist))
179 (point-min) (point-max))
180 (goto-char (point-min))
182 (goto-char (point-max))
184 ;; Encoded words can't be more than 75 chars long, so we have to
185 ;; split the long ones up.
187 (while (> (current-column) 74)
190 (insert "?=\n " start)
193 (defun rfc1522-q-encode-region (b e)
194 "Encode the header contained in REGION with the Q encoding."
197 (narrow-to-region (goto-char b) e)
198 (let ((alist rfc1522-q-encoding-alist))
200 (when (looking-at (caar alist))
201 (quoted-printable-encode-region b e nil (cdar alist))
202 (subst-char-in-region (point-min) (point-max) ? ?_))
206 ;;; Functions for decoding RFC1522 messages
209 (defvar rfc1522-encoded-word-regexp
210 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~]+\\)\\?=")
213 (defun rfc1522-decode-region (start end)
214 "Decode MIME-encoded words in region between START and END."
218 (narrow-to-region start end)
219 (goto-char (point-min))
220 ;; Remove whitespace between encoded words.
221 (while (re-search-forward
222 (concat "\\(" rfc1522-encoded-word-regexp "\\)"
224 "\\(" rfc1522-encoded-word-regexp "\\)")
226 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
227 ;; Decode the encoded words.
228 (goto-char (point-min))
229 (while (re-search-forward rfc1522-encoded-word-regexp nil t)
230 (insert (rfc1522-parse-and-decode
233 (delete-region (match-beginning 0) (match-end 0)))))))))
236 (defun rfc1522-decode-string (string)
237 "Decode the quoted-printable-encoded STRING and return the results."
241 (rfc1522-decode-region (point-min) (point-max)))
244 (defun rfc1522-parse-and-decode (word)
245 "Decode WORD and return it if it is an encoded word.
247 (if (not (string-match rfc1522-encoded-word-regexp word))
252 (match-string 1 word)
253 (upcase (match-string 2 word))
254 (match-string 3 word))
258 (defun rfc1522-decode (charset encoding string)
259 "Decode STRING as an encoded text.
260 Valid ENCODINGs are \"B\" and \"Q\".
261 If your Emacs implementation can't decode CHARSET, it returns nil."
262 (let ((cs (mm-charset-to-coding-system charset)))
264 (mm-decode-coding-string
266 ((equal "B" encoding)
267 (base64-decode string))
268 ((equal "Q" encoding)
269 (quoted-printable-decode-string
270 (mm-replace-chars-in-string string ?_ ? )))
271 (t (error "Invalid encoding: %s" encoding)))
276 ;;; rfc1522.el ends here