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 ;; The following isn't valid, but one should be liberal
60 ;; in what one receives.
61 (modify-syntax-entry ?\: "w" table)
62 (set-syntax-table table))
64 (when (and (memq c ttoken)
65 (not (memq c stoken)))
66 (setq type (downcase (buffer-substring
67 (point) (progn (forward-sexp 1) (point)))))
72 (error "Invalid header: %s" string))
74 ;; If c in nil, then this is an invalid header, but
75 ;; since elm generates invalid headers on this form,
77 (when (setq c (char-after))
78 (if (and (memq c ttoken)
79 (not (memq c stoken)))
84 (point) (progn (forward-sexp 1) (point))))))
85 (error "Invalid header: %s" string))
91 (if (not (memq c ntoken))
97 (point) (progn (forward-sexp 1) (point)))))
102 (setq c (char-after)))))
103 ;; See if we have any previous continuations.
104 (when (and prev-attribute
105 (not (eq prev-attribute attribute)))
106 (push (cons prev-attribute prev-value) parameters)
107 (setq prev-attribute nil
110 (error "Invalid header: %s" string))
112 (setq c (char-after))
116 (buffer-substring (1+ (point))
117 (progn (forward-sexp 1) (1- (point))))))
118 ((and (or (memq c ttoken)
119 (> c ?\177)) ;; EXTENSION: Support non-ascii chars.
120 (not (memq c stoken)))
121 (setq value (buffer-substring
122 (point) (progn (forward-sexp) (point)))))
124 (error "Invalid header: %s" string)))
126 (setq value (rfc2231-decode-encoded-string value)))
128 (setq prev-attribute attribute
129 prev-value (concat prev-value value))
130 (push (cons attribute value) parameters))))
132 ;; Take care of any final continuations.
134 (push (cons prev-attribute prev-value) parameters))
137 `(,type ,@(nreverse parameters)))))))
139 (defun rfc2231-decode-encoded-string (string)
140 "Decode an RFC2231-encoded string.
141 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
143 (let ((elems (split-string string "'")))
144 ;; The encoded string may contain zero to two single-quote
145 ;; marks. This should give us the encoded word stripped
146 ;; of any preceding values.
147 (insert (car (last elems)))
148 (goto-char (point-min))
149 (while (search-forward "%" nil t)
152 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
153 (delete-region (1- (point)) (+ (point) 2)))))
154 ;; Encode using the charset, if any.
155 (when (and (mm-multibyte-p)
157 (not (equal (intern (downcase (car elems))) 'us-ascii)))
158 (mm-decode-coding-region (point-min) (point-max)
159 (intern (downcase (car elems)))))
162 (defun rfc2231-encode-string (param value)
163 "Return and PARAM=VALUE string encoded according to RFC2231."
164 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
165 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
166 (special (ietf-drums-token-to-list "*'%\n\t"))
167 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
169 spacep encodep charsetp charset broken)
172 (goto-char (point-min))
175 ((or (memq (following-char) control)
176 (memq (following-char) tspecial)
177 (memq (following-char) special))
179 ((eq (following-char) ? )
181 ((not (memq (following-char) ascii))
185 (setq charset (mm-encode-body)))
187 ((or encodep charsetp)
188 (goto-char (point-min))
190 (when (> (current-column) 60)
193 (if (or (not (memq (following-char) ascii))
194 (memq (following-char) control)
195 (memq (following-char) tspecial)
196 (memq (following-char) special)
197 (eq (following-char) ? ))
199 (insert "%" (format "%02x" (following-char)))
202 (goto-char (point-min))
203 (insert (symbol-name (or charset 'us-ascii)) "''")
204 (goto-char (point-min))
208 (insert (if (>= num 0) " " "\n ")
209 param "*" (format "%d" (incf num)) "*=")
212 (goto-char (point-min))
214 (goto-char (point-max))
217 (goto-char (point-min))
223 ;;; rfc2231.el ends here