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"))
55 c type attribute encoded number prev-attribute vals
56 prev-encoded parameters value)
57 (ietf-drums-init (mail-header-remove-whitespace
58 (mail-header-remove-comments string)))
59 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
60 (modify-syntax-entry ?\' "w" table)
61 (modify-syntax-entry ?* " " table)
62 (modify-syntax-entry ?\; " " table)
63 (modify-syntax-entry ?= " " table)
64 ;; The following isn't valid, but one should be liberal
65 ;; in what one receives.
66 (modify-syntax-entry ?\: "w" table)
67 (set-syntax-table table))
69 (when (and (memq c ttoken)
70 (not (memq c stoken)))
71 (setq type (downcase (buffer-substring
72 (point) (progn (forward-sexp 1) (point)))))
79 (error "Invalid header: %s" string))
81 ;; If c in nil, then this is an invalid header, but
82 ;; since elm generates invalid headers on this form,
84 (when (setq c (char-after))
85 (if (and (memq c ttoken)
86 (not (memq c stoken)))
91 (point) (progn (forward-sexp 1) (point))))))
92 (error "Invalid header: %s" string))
98 (if (not (memq c ntoken))
104 (point) (progn (forward-sexp 1) (point)))))
105 (setq c (char-after))
109 (setq c (char-after)))))
112 ;; See if we have any previous continuations.
113 (when (and prev-attribute
114 (not (eq prev-attribute attribute)))
116 (mapconcat 'cdr (sort vals 'car-less-than-car) ""))
117 (push (cons prev-attribute
119 (rfc2231-decode-encoded-string vals)
122 (setq prev-attribute nil
126 (error "Invalid header: %s" string))
128 (setq c (char-after))
131 (setq value (buffer-substring (1+ (point))
136 (setq value (mapconcat (lambda (c) (format "%%%02x" c))
138 ((and (or (memq c ttoken)
139 ;; EXTENSION: Support non-ascii chars.
141 (not (memq c stoken)))
147 ;; We might not have reached at the end of
148 ;; the value because of non-ascii chars,
149 ;; so we should jump over them if any.
150 (while (and (not (eobp))
151 (> (char-after) ?\177))
156 (error "Invalid header: %s" string)))
159 (push (cons number value) vals)
160 (setq prev-attribute attribute
161 prev-encoded encoded))
162 (push (cons attribute
164 (rfc2231-decode-encoded-string value)
168 ;; Take care of any final continuations.
170 (setq vals (mapconcat 'cdr (sort vals 'car-less-than-car) ""))
171 (push (cons prev-attribute
173 (rfc2231-decode-encoded-string vals)
177 (setq parameters nil)
179 (signal (car err) (cdr err))
180 ;;(message "%s" (error-message-string err))
184 `(,type ,@(nreverse parameters)))))))
186 (defun rfc2231-decode-encoded-string (string)
187 "Decode an RFC2231-encoded string.
189 \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\",
190 \"us-ascii''This%20is%20%2A%2A%2Afun%2A%2A%2A\",
191 \"'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\",
192 \"''This%20is%20%2A%2A%2Afun%2A%2A%2A\", or
193 \"This is ***fun***\"."
194 (string-match "\\`\\(?:\\([^']+\\)?'\\([^']+\\)?'\\)?\\(.+\\)" string)
195 (let ((coding-system (mm-charset-to-coding-system (match-string 1 string)))
196 ;;(language (match-string 2 string))
197 (value (match-string 3 string)))
198 (mm-with-multibyte-buffer
200 (goto-char (point-min))
201 (while (search-forward "%" nil t)
204 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
205 (delete-region (1- (point)) (+ (point) 2)))))
206 ;; Decode using the charset, if any.
207 (unless (memq coding-system '(nil ascii))
208 (mm-decode-coding-region (point-min) (point-max) coding-system))
211 (defun rfc2231-encode-string (param value)
212 "Return and PARAM=VALUE string encoded according to RFC2231.
213 Use `mml-insert-parameter' or `mml-insert-parameter-string' to insert
214 the result of this function."
215 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
216 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
217 (special (ietf-drums-token-to-list "*'%\n\t"))
218 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
220 ;; Don't make lines exceeding 76 column.
221 (limit (- 74 (length param)))
222 spacep encodep charsetp charset broken)
225 (goto-char (point-min))
228 ((or (memq (following-char) control)
229 (memq (following-char) tspecial)
230 (memq (following-char) special))
232 ((eq (following-char) ? )
234 ((not (memq (following-char) ascii))
238 (setq charset (mm-encode-body)))
240 ((or encodep charsetp
243 (> (current-column) (if spacep (- limit 2) limit))))
244 (setq limit (- limit 6))
245 (goto-char (point-min))
246 (insert (symbol-name (or charset 'us-ascii)) "''")
248 (if (or (not (memq (following-char) ascii))
249 (memq (following-char) control)
250 (memq (following-char) tspecial)
251 (memq (following-char) special)
252 (eq (following-char) ? ))
254 (when (>= (current-column) (1- limit))
257 (insert "%" (format "%02x" (following-char)))
259 (when (> (current-column) limit)
263 (goto-char (point-min))
267 (insert (if (>= num 0) " " "")
268 param "*" (format "%d" (incf num)) "*=")
271 (goto-char (point-min))
273 (goto-char (point-max))
276 (goto-char (point-min))
282 ;;; arch-tag: c3ab751d-d108-406a-b301-68882ad8cd63
283 ;;; rfc2231.el ends here