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)
45 "Parse STRING and return a list.
46 The list will be on the form
47 `(name (attribute . value) (attribute . value)...)"
49 (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token
))
50 (stoken (ietf-drums-token-to-list ietf-drums-tspecials
))
51 (ntoken (ietf-drums-token-to-list "0-9"))
53 display-name mailbox c display-string parameters
54 attribute value type subtype number encoded
55 prev-attribute prev-encoded
)
56 ;; Some mailer (e.g. Thunderbird 1.5) doesn't terminate each
57 ;; line with semicolon when folding a long parameter value.
58 (while (string-match "\\([^\t\n\r ;]\\)[\t ]*\r?\n[\t ]+" string
)
59 (setq string
(replace-match "\\1;\n " nil nil string
)))
60 (ietf-drums-init (mail-header-remove-whitespace
61 (mail-header-remove-comments string
)))
62 (let ((table (copy-syntax-table ietf-drums-syntax-table
)))
63 (modify-syntax-entry ?
\' "w" table
)
64 (modify-syntax-entry ?
* " " table
)
65 (modify-syntax-entry ?\
; " " table)
66 (modify-syntax-entry ?
= " " table
)
67 ;; The following isn't valid, but one should be liberal
68 ;; in what one receives.
69 (modify-syntax-entry ?\
: "w" table
)
70 (set-syntax-table table
))
72 (when (and (memq c ttoken
)
73 (not (memq c stoken
)))
74 (setq type
(downcase (buffer-substring
75 (point) (progn (forward-sexp 1) (point)))))
80 (error "Invalid header: %s" string
))
82 ;; If c in nil, then this is an invalid header, but
83 ;; since elm generates invalid headers on this form,
85 (when (setq c
(char-after))
86 (if (and (memq c ttoken
)
87 (not (memq c stoken
)))
92 (point) (progn (forward-sexp 1) (point))))))
93 (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)))))
110 ;; See if we have any previous continuations.
111 (when (and prev-attribute
112 (not (eq prev-attribute attribute
)))
113 (push (cons prev-attribute
115 (rfc2231-decode-encoded-string prev-value
)
118 (setq prev-attribute nil
122 (error "Invalid header: %s" string
))
124 (setq c
(char-after))
128 (buffer-substring (1+ (point))
129 (progn (forward-sexp 1) (1- (point))))))
130 ((and (or (memq c ttoken
)
131 (> c ?
\177)) ;; EXTENSION: Support non-ascii chars.
132 (not (memq c stoken
)))
133 (setq value
(buffer-substring
137 ;; We might not have reached at the end of
138 ;; the value because of non-ascii chars,
139 ;; so we should jump over them if any.
140 (while (and (not (eobp))
141 (> (char-after) ?
\177))
146 (error "Invalid header: %s" string
)))
148 (setq prev-attribute attribute
149 prev-value
(concat prev-value value
)
150 prev-encoded encoded
)
151 (push (cons attribute
153 (rfc2231-decode-encoded-string value
)
157 ;; Take care of any final continuations.
159 (push (cons prev-attribute
161 (rfc2231-decode-encoded-string prev-value
)
166 `(,type
,@(nreverse parameters
)))))))
168 (defun rfc2231-decode-encoded-string (string)
169 "Decode an RFC2231-encoded string.
170 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
172 (let ((elems (split-string string
"'")))
173 ;; The encoded string may contain zero to two single-quote
174 ;; marks. This should give us the encoded word stripped
175 ;; of any preceding values.
176 (insert (car (last elems
)))
177 (goto-char (point-min))
178 (while (search-forward "%" nil t
)
181 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
182 (delete-region (1- (point)) (+ (point) 2)))))
183 ;; Encode using the charset, if any.
184 (when (and (mm-multibyte-p)
186 (not (equal (intern (downcase (car elems
))) 'us-ascii
)))
187 (mm-decode-coding-region (point-min) (point-max)
188 (intern (downcase (car elems
)))))
191 (defun rfc2231-encode-string (param value
)
192 "Return and PARAM=VALUE string encoded according to RFC2231."
193 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token
))
194 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials
))
195 (special (ietf-drums-token-to-list "*'%\n\t"))
196 (ascii (ietf-drums-token-to-list ietf-drums-text-token
))
198 (limit (- 74 (length param
)))
199 spacep encodep charsetp charset broken
)
202 (goto-char (point-min))
205 ((or (memq (following-char) control
)
206 (memq (following-char) tspecial
)
207 (memq (following-char) special
))
209 ((eq (following-char) ?
)
211 ((not (memq (following-char) ascii
))
215 (setq charset
(mm-encode-body)))
217 ((or encodep charsetp
220 (> (current-column) (if spacep
(- limit
2) limit
))))
221 (setq limit
(- limit
6))
222 (goto-char (point-min))
223 (insert (symbol-name (or charset
'us-ascii
)) "''")
225 (if (or (not (memq (following-char) ascii
))
226 (memq (following-char) control
)
227 (memq (following-char) tspecial
)
228 (memq (following-char) special
)
229 (eq (following-char) ?
))
231 (when (>= (current-column) (1- limit
))
234 (insert "%" (format "%02x" (following-char)))
236 (when (> (current-column) limit
)
240 (goto-char (point-min))
244 (insert (if (>= num
0) " " "\n ")
245 param
"*" (format "%d" (incf num
)) "*=")
248 (goto-char (point-min))
249 (insert "\n " param
"=\"")
250 (goto-char (point-max))
253 (goto-char (point-min))
254 (insert "\n " param
"=")))
259 ;;; arch-tag: c3ab751d-d108-406a-b301-68882ad8cd63
260 ;;; rfc2231.el ends here