1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
3 ;; Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
28 (eval-when-compile (require 'cl))
31 (autoload 'mm-encode-body "mm-bodies")
32 (autoload 'mail-header-remove-whitespace "mail-parse")
33 (autoload 'mail-header-remove-comments "mail-parse")
35 (defun rfc2231-get-value (ct attribute)
36 "Return the value of ATTRIBUTE from CT."
37 (cdr (assq attribute (cdr ct))))
39 (defun rfc2231-parse-qp-string (string)
40 "Parse QP-encoded string using `rfc2231-parse-string'.
41 N.B. This is in violation with RFC2047, but it seem to be in common use."
42 (rfc2231-parse-string (rfc2047-decode-string string)))
44 (defun rfc2231-parse-string (string &optional signal-error)
45 "Parse STRING and return a list.
46 The list will be on the form
47 `(name (attribute . value) (attribute . value)...)'.
49 If the optional SIGNAL-ERROR is non-nil, signal an error when this
50 function fails in parsing of parameters."
52 (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
53 (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
54 (ntoken (ietf-drums-token-to-list "0-9"))
56 display-name mailbox c display-string parameters
57 attribute value type subtype number encoded
58 prev-attribute prev-encoded)
59 ;; Some mailer (e.g. Thunderbird 1.5) doesn't terminate each
60 ;; line with semicolon when folding a long parameter value.
61 (while (string-match "\\([^\t\n\r ;]\\)[\t ]*\r?\n[\t ]+" string)
62 (setq string (replace-match "\\1;\n " nil nil string)))
63 (ietf-drums-init (mail-header-remove-whitespace
64 (mail-header-remove-comments string)))
65 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
66 (modify-syntax-entry ?\' "w" table)
67 (modify-syntax-entry ?* " " table)
68 (modify-syntax-entry ?\; " " table)
69 (modify-syntax-entry ?= " " table)
70 ;; The following isn't valid, but one should be liberal
71 ;; in what one receives.
72 (modify-syntax-entry ?\: "w" table)
73 (set-syntax-table table))
75 (when (and (memq c ttoken)
76 (not (memq c stoken)))
77 (setq type (downcase (buffer-substring
78 (point) (progn (forward-sexp 1) (point)))))
85 (error "Invalid header: %s" string))
87 ;; If c in nil, then this is an invalid header, but
88 ;; since elm generates invalid headers on this form,
90 (when (setq c (char-after))
91 (if (and (memq c ttoken)
92 (not (memq c stoken)))
97 (point) (progn (forward-sexp 1) (point))))))
98 (error "Invalid header: %s" string))
102 (setq c (char-after))
103 (if (not (memq c ntoken))
109 (point) (progn (forward-sexp 1) (point)))))
110 (setq c (char-after))
114 (setq c (char-after)))))
115 ;; See if we have any previous continuations.
116 (when (and prev-attribute
117 (not (eq prev-attribute attribute)))
118 (push (cons prev-attribute
120 (rfc2231-decode-encoded-string prev-value)
123 (setq prev-attribute nil
127 (error "Invalid header: %s" string))
129 (setq c (char-after))
132 (setq value (buffer-substring (1+ (point))
136 ((and (or (memq c ttoken)
137 ;; EXTENSION: Support non-ascii chars.
139 (not (memq c stoken)))
145 ;; We might not have reached at the end of
146 ;; the value because of non-ascii chars,
147 ;; so we should jump over them if any.
148 (while (and (not (eobp))
149 (> (char-after) ?\177))
154 (error "Invalid header: %s" string)))
156 (setq prev-attribute attribute
157 prev-value (concat prev-value value)
158 prev-encoded encoded)
159 (push (cons attribute
161 (rfc2231-decode-encoded-string value)
165 ;; Take care of any final continuations.
167 (push (cons prev-attribute
169 (rfc2231-decode-encoded-string prev-value)
173 (setq parameters nil)
175 (signal (car err) (cdr err))
176 ;;(message "%s" (error-message-string err))
180 `(,type ,@(nreverse parameters)))))))
182 (defun rfc2231-decode-encoded-string (string)
183 "Decode an RFC2231-encoded string.
184 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
186 (let ((elems (split-string string "'")))
187 ;; The encoded string may contain zero to two single-quote
188 ;; marks. This should give us the encoded word stripped
189 ;; of any preceding values.
190 (insert (car (last elems)))
191 (goto-char (point-min))
192 (while (search-forward "%" nil t)
195 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
196 (delete-region (1- (point)) (+ (point) 2)))))
197 ;; Encode using the charset, if any.
198 (when (and (mm-multibyte-p)
200 (not (equal (intern (downcase (car elems))) 'us-ascii)))
201 (mm-decode-coding-region (point-min) (point-max)
202 (intern (downcase (car elems)))))
205 (defun rfc2231-encode-string (param value)
206 "Return and PARAM=VALUE string encoded according to RFC2231.
207 Use `mml-insert-parameter' or `mml-insert-parameter-string' to insert
208 the result of this function."
209 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
210 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
211 (special (ietf-drums-token-to-list "*'%\n\t"))
212 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
214 ;; Don't make lines exceeding 76 column.
215 (limit (- 74 (length param)))
216 spacep encodep charsetp charset broken)
219 (goto-char (point-min))
222 ((or (memq (following-char) control)
223 (memq (following-char) tspecial)
224 (memq (following-char) special))
226 ((eq (following-char) ? )
228 ((not (memq (following-char) ascii))
232 (setq charset (mm-encode-body)))
234 ((or encodep charsetp
237 (> (current-column) (if spacep (- limit 2) limit))))
238 (setq limit (- limit 6))
239 (goto-char (point-min))
240 (insert (symbol-name (or charset 'us-ascii)) "''")
242 (if (or (not (memq (following-char) ascii))
243 (memq (following-char) control)
244 (memq (following-char) tspecial)
245 (memq (following-char) special)
246 (eq (following-char) ? ))
248 (when (>= (current-column) (1- limit))
251 (insert "%" (format "%02x" (following-char)))
253 (when (> (current-column) limit)
257 (goto-char (point-min))
261 (insert (if (>= num 0) " " "")
262 param "*" (format "%d" (incf num)) "*=")
265 (goto-char (point-min))
266 (insert "\n " param "=\"")
267 (goto-char (point-max))
270 (goto-char (point-min))
271 (insert "\n " param "=")))
276 ;;; arch-tag: c3ab751d-d108-406a-b301-68882ad8cd63
277 ;;; rfc2231.el ends here