1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
3 ;; Copyright (C) 1998, 1999, 2000 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))
31 (defun rfc2231-get-value (ct attribute)
32 "Return the value of ATTRIBUTE from CT."
33 (cdr (assq attribute (cdr ct))))
35 (defun rfc2231-parse-qp-string (string)
36 "Parse QP-encoded string using `rfc2231-parse-string'.
37 N.B. This is in violation with RFC2047, but it seem to be in common use."
38 (rfc2231-parse-string (rfc2047-decode-string string)))
40 (defun rfc2231-parse-string (string)
41 "Parse STRING and return a list.
42 The list will be on the form
43 `(name (attribute . value) (attribute . value)...)"
45 (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
46 (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
47 (ntoken (ietf-drums-token-to-list "0-9"))
49 display-name mailbox c display-string parameters
50 attribute value type subtype number encoded
52 (ietf-drums-init (mail-header-remove-whitespace
53 (mail-header-remove-comments string)))
54 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
55 (modify-syntax-entry ?\' "w" table)
56 ;; The following isn't valid, but one should be liberal
57 ;; in what one receives.
58 (modify-syntax-entry ?\: "w" table)
59 (set-syntax-table table))
61 (when (and (memq c ttoken)
62 (not (memq c stoken)))
63 (setq type (downcase (buffer-substring
64 (point) (progn (forward-sexp 1) (point)))))
69 (error "Invalid header: %s" string))
71 ;; If c in nil, then this is an invalid header, but
72 ;; since elm generates invalid headers on this form,
74 (when (setq c (char-after))
75 (if (and (memq c ttoken)
76 (not (memq c stoken)))
81 (point) (progn (forward-sexp 1) (point))))))
82 (error "Invalid header: %s" string))
88 (if (not (memq c ntoken))
94 (point) (progn (forward-sexp 1) (point)))))
99 (setq c (char-after)))))
100 ;; See if we have any previous continuations.
101 (when (and prev-attribute
102 (not (eq prev-attribute attribute)))
103 (push (cons prev-attribute prev-value) parameters)
104 (setq prev-attribute nil
107 (error "Invalid header: %s" string))
109 (setq c (char-after))
113 (buffer-substring (1+ (point))
114 (progn (forward-sexp 1) (1- (point))))))
115 ((and (memq c ttoken)
116 (not (memq c stoken)))
117 (setq value (buffer-substring
118 (point) (progn (forward-sexp 1) (point)))))
120 (error "Invalid header: %s" string)))
122 (setq value (rfc2231-decode-encoded-string value)))
124 (setq prev-attribute attribute
125 prev-value (concat prev-value value))
126 (push (cons attribute value) parameters))))
128 ;; Take care of any final continuations.
130 (push (cons prev-attribute prev-value) parameters))
133 `(,type ,@(nreverse parameters)))))))
135 (defun rfc2231-decode-encoded-string (string)
136 "Decode an RFC2231-encoded string.
137 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
139 (let ((elems (split-string string "'")))
140 ;; The encoded string may contain zero to two single-quote
141 ;; marks. This should give us the encoded word stripped
142 ;; of any preceding values.
143 (insert (car (last elems)))
144 (goto-char (point-min))
145 (while (search-forward "%" nil t)
148 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
149 (delete-region (1- (point)) (+ (point) 2)))))
150 ;; Encode using the charset, if any.
151 (when (and (mm-multibyte-p)
153 (not (equal (intern (car elems)) 'us-ascii)))
154 (mm-decode-coding-region (point-min) (point-max)
155 (intern (car elems))))
158 (defun rfc2231-encode-string (param value)
159 "Return and PARAM=VALUE string encoded according to RFC2231."
160 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
161 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
162 (special (ietf-drums-token-to-list "*'%\n\t"))
163 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
165 spacep encodep charsetp charset broken)
168 (goto-char (point-min))
171 ((or (memq (following-char) control)
172 (memq (following-char) tspecial)
173 (memq (following-char) special))
175 ((eq (following-char) ? )
177 ((not (memq (following-char) ascii))
181 (setq charset (mm-encode-body)))
183 ((or encodep charsetp)
184 (goto-char (point-min))
186 (when (> (current-column) 60)
189 (if (or (not (memq (following-char) ascii))
190 (memq (following-char) control)
191 (memq (following-char) tspecial)
192 (memq (following-char) special)
193 (eq (following-char) ? ))
195 (insert "%" (format "%02x" (following-char)))
198 (goto-char (point-min))
199 (insert (symbol-name (or charset 'us-ascii)) "''")
200 (goto-char (point-min))
204 (insert (if (>= num 0) " " "\n ")
205 param "*" (format "%d" (incf num)) "*=")
208 (goto-char (point-min))
210 (goto-char (point-max))
213 (goto-char (point-min))
219 ;;; rfc2231.el ends here