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 (ietf-drums-init (mail-header-remove-whitespace
60 (mail-header-remove-comments string)))
61 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
62 (modify-syntax-entry ?\' "w" table)
63 (modify-syntax-entry ?* " " table)
64 (modify-syntax-entry ?\; " " table)
65 (modify-syntax-entry ?= " " table)
66 ;; The following isn't valid, but one should be liberal
67 ;; in what one receives.
68 (modify-syntax-entry ?\: "w" table)
69 (set-syntax-table table))
71 (when (and (memq c ttoken)
72 (not (memq c stoken)))
73 (setq type (downcase (buffer-substring
74 (point) (progn (forward-sexp 1) (point)))))
81 (error "Invalid header: %s" string))
83 ;; If c in nil, then this is an invalid header, but
84 ;; since elm generates invalid headers on this form,
86 (when (setq c (char-after))
87 (if (and (memq c ttoken)
88 (not (memq c stoken)))
93 (point) (progn (forward-sexp 1) (point))))))
94 (error "Invalid header: %s" string))
99 (if (not (memq c ntoken))
105 (point) (progn (forward-sexp 1) (point)))))
106 (setq c (char-after))
110 (setq c (char-after)))))
111 ;; See if we have any previous continuations.
112 (when (and prev-attribute
113 (not (eq prev-attribute attribute)))
114 (push (cons prev-attribute
116 (rfc2231-decode-encoded-string prev-value)
119 (setq prev-attribute nil
123 (error "Invalid header: %s" string))
125 (setq c (char-after))
128 (setq value (buffer-substring (1+ (point))
132 ((and (or (memq c ttoken)
133 ;; EXTENSION: Support non-ascii chars.
135 (not (memq c stoken)))
141 ;; We might not have reached at the end of
142 ;; the value because of non-ascii chars,
143 ;; so we should jump over them if any.
144 (while (and (not (eobp))
145 (> (char-after) ?\177))
150 (error "Invalid header: %s" string)))
152 (setq prev-attribute attribute
153 prev-value (concat prev-value value)
154 prev-encoded encoded)
155 (push (cons attribute
157 (rfc2231-decode-encoded-string value)
161 ;; Take care of any final continuations.
163 (push (cons prev-attribute
165 (rfc2231-decode-encoded-string prev-value)
169 (setq parameters nil)
171 (signal (car err) (cdr err))
172 ;;(message "%s" (error-message-string err))
176 `(,type ,@(nreverse parameters)))))))
178 (defun rfc2231-decode-encoded-string (string)
179 "Decode an RFC2231-encoded string.
180 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
182 (let ((elems (split-string string "'")))
183 ;; The encoded string may contain zero to two single-quote
184 ;; marks. This should give us the encoded word stripped
185 ;; of any preceding values.
186 (insert (car (last elems)))
187 (goto-char (point-min))
188 (while (search-forward "%" nil t)
191 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
192 (delete-region (1- (point)) (+ (point) 2)))))
193 ;; Encode using the charset, if any.
194 (when (and (mm-multibyte-p)
196 (not (equal (intern (downcase (car elems))) 'us-ascii)))
197 (mm-decode-coding-region (point-min) (point-max)
198 (intern (downcase (car elems)))))
201 (defun rfc2231-encode-string (param value)
202 "Return and PARAM=VALUE string encoded according to RFC2231.
203 Use `mml-insert-parameter' or `mml-insert-parameter-string' to insert
204 the result of this function."
205 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
206 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
207 (special (ietf-drums-token-to-list "*'%\n\t"))
208 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
210 ;; Don't make lines exceeding 76 column.
211 (limit (- 74 (length param)))
212 spacep encodep charsetp charset broken)
215 (goto-char (point-min))
218 ((or (memq (following-char) control)
219 (memq (following-char) tspecial)
220 (memq (following-char) special))
222 ((eq (following-char) ? )
224 ((not (memq (following-char) ascii))
228 (setq charset (mm-encode-body)))
230 ((or encodep charsetp
233 (> (current-column) (if spacep (- limit 2) limit))))
234 (setq limit (- limit 6))
235 (goto-char (point-min))
236 (insert (symbol-name (or charset 'us-ascii)) "''")
238 (if (or (not (memq (following-char) ascii))
239 (memq (following-char) control)
240 (memq (following-char) tspecial)
241 (memq (following-char) special)
242 (eq (following-char) ? ))
244 (when (>= (current-column) (1- limit))
247 (insert "%" (format "%02x" (following-char)))
249 (when (> (current-column) limit)
253 (goto-char (point-min))
257 (insert (if (>= num 0) " " "")
258 param "*" (format "%d" (incf num)) "*=")
261 (goto-char (point-min))
263 (goto-char (point-max))
266 (goto-char (point-min))
272 ;;; arch-tag: c3ab751d-d108-406a-b301-68882ad8cd63
273 ;;; rfc2231.el ends here