1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
3 ;; Copyright (C) 1998, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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.
27 (eval-when-compile (require 'cl))
30 (autoload 'mm-encode-body "mm-bodies")
31 (autoload 'mail-header-remove-whitespace "mail-parse")
32 (autoload 'mail-header-remove-comments "mail-parse")
34 (defun rfc2231-get-value (ct attribute)
35 "Return the value of ATTRIBUTE from CT."
36 (cdr (assq attribute (cdr ct))))
38 (defun rfc2231-parse-qp-string (string)
39 "Parse QP-encoded string using `rfc2231-parse-string'.
40 N.B. This is in violation with RFC2047, but it seem to be in common use."
41 (rfc2231-parse-string (rfc2047-decode-string string)))
43 (defun rfc2231-parse-string (string)
44 "Parse STRING and return a list.
45 The list will be on the form
46 `(name (attribute . value) (attribute . value)...)"
48 (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
49 (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
50 (ntoken (ietf-drums-token-to-list "0-9"))
52 display-name mailbox c display-string parameters
53 attribute value type subtype number encoded
55 (ietf-drums-init (mail-header-remove-whitespace
56 (mail-header-remove-comments string)))
57 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
58 (modify-syntax-entry ?\' "w" table)
59 (modify-syntax-entry ?= " " table)
60 ;; The following isn't valid, but one should be liberal
61 ;; in what one receives.
62 (modify-syntax-entry ?\: "w" table)
63 (set-syntax-table table))
65 (when (and (memq c ttoken)
66 (not (memq c stoken)))
67 (setq type (downcase (buffer-substring
68 (point) (progn (forward-sexp 1) (point)))))
73 (error "Invalid header: %s" string))
75 ;; If c in nil, then this is an invalid header, but
76 ;; since elm generates invalid headers on this form,
78 (when (setq c (char-after))
79 (if (and (memq c ttoken)
80 (not (memq c stoken)))
85 (point) (progn (forward-sexp 1) (point))))))
86 (error "Invalid header: %s" string))
92 (if (not (memq c ntoken))
98 (point) (progn (forward-sexp 1) (point)))))
103 (setq c (char-after)))))
104 ;; See if we have any previous continuations.
105 (when (and prev-attribute
106 (not (eq prev-attribute attribute)))
107 (push (cons prev-attribute prev-value) parameters)
108 (setq prev-attribute nil
111 (error "Invalid header: %s" string))
113 (setq c (char-after))
117 (buffer-substring (1+ (point))
118 (progn (forward-sexp 1) (1- (point))))))
119 ((and (or (memq c ttoken)
120 (> c ?\177)) ;; EXTENSION: Support non-ascii chars.
121 (not (memq c stoken)))
122 (setq value (buffer-substring
123 (point) (progn (forward-sexp) (point)))))
125 (error "Invalid header: %s" string)))
127 (setq value (rfc2231-decode-encoded-string value)))
129 (setq prev-attribute attribute
130 prev-value (concat prev-value value))
131 (push (cons attribute value) parameters))))
133 ;; Take care of any final continuations.
135 (push (cons prev-attribute prev-value) parameters))
138 `(,type ,@(nreverse parameters)))))))
140 (defun rfc2231-decode-encoded-string (string)
141 "Decode an RFC2231-encoded string.
142 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
144 (let ((elems (split-string string "'")))
145 ;; The encoded string may contain zero to two single-quote
146 ;; marks. This should give us the encoded word stripped
147 ;; of any preceding values.
148 (insert (car (last elems)))
149 (goto-char (point-min))
150 (while (search-forward "%" nil t)
153 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
154 (delete-region (1- (point)) (+ (point) 2)))))
155 ;; Encode using the charset, if any.
156 (when (and (mm-multibyte-p)
158 (not (equal (intern (downcase (car elems))) 'us-ascii)))
159 (mm-decode-coding-region (point-min) (point-max)
160 (intern (downcase (car elems)))))
163 (defun rfc2231-encode-string (param value)
164 "Return and PARAM=VALUE string encoded according to RFC2231."
165 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
166 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
167 (special (ietf-drums-token-to-list "*'%\n\t"))
168 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
170 spacep encodep charsetp charset broken)
173 (goto-char (point-min))
176 ((or (memq (following-char) control)
177 (memq (following-char) tspecial)
178 (memq (following-char) special))
180 ((eq (following-char) ? )
182 ((not (memq (following-char) ascii))
186 (setq charset (mm-encode-body)))
188 ((or encodep charsetp)
189 (goto-char (point-min))
191 (when (> (current-column) 60)
194 (if (or (not (memq (following-char) ascii))
195 (memq (following-char) control)
196 (memq (following-char) tspecial)
197 (memq (following-char) special)
198 (eq (following-char) ? ))
200 (insert "%" (format "%02x" (following-char)))
203 (goto-char (point-min))
204 (insert (symbol-name (or charset 'us-ascii)) "''")
205 (goto-char (point-min))
209 (insert (if (>= num 0) " " "\n ")
210 param "*" (format "%d" (incf num)) "*=")
213 (goto-char (point-min))
215 (goto-char (point-max))
218 (goto-char (point-min))
224 ;;; rfc2231.el ends here