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))
28 (eval-when-compile (require 'gnus-clfns))
30 (eval-when-compile (require 'cl))
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 (memq c ttoken)
119 (not (memq c stoken)))
120 (setq value (buffer-substring
121 (point) (progn (forward-sexp 1) (point)))))
123 (error "Invalid header: %s" string)))
125 (setq value (rfc2231-decode-encoded-string value)))
127 (setq prev-attribute attribute
128 prev-value (concat prev-value value))
129 (push (cons attribute value) parameters))))
131 ;; Take care of any final continuations.
133 (push (cons prev-attribute prev-value) parameters))
136 `(,type ,@(nreverse parameters)))))))
138 (defun rfc2231-decode-encoded-string (string)
139 "Decode an RFC2231-encoded string.
140 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
142 (let ((elems (split-string string "'")))
143 ;; The encoded string may contain zero to two single-quote
144 ;; marks. This should give us the encoded word stripped
145 ;; of any preceding values.
146 (insert (car (last elems)))
147 (goto-char (point-min))
148 (while (search-forward "%" nil t)
151 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
152 (delete-region (1- (point)) (+ (point) 2)))))
153 ;; Encode using the charset, if any.
154 (when (and (mm-multibyte-p)
156 (not (equal (intern (car elems)) 'us-ascii)))
157 (mm-decode-coding-region (point-min) (point-max)
158 (intern (car elems))))
161 (defun rfc2231-encode-string (param value)
162 "Return and PARAM=VALUE string encoded according to RFC2231."
163 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
164 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
165 (special (ietf-drums-token-to-list "*'%\n\t"))
166 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
168 spacep encodep charsetp charset broken)
171 (goto-char (point-min))
174 ((or (memq (following-char) control)
175 (memq (following-char) tspecial)
176 (memq (following-char) special))
178 ((eq (following-char) ? )
180 ((not (memq (following-char) ascii))
184 (setq charset (mm-encode-body)))
186 ((or encodep charsetp)
187 (goto-char (point-min))
189 (when (> (current-column) 60)
192 (if (or (not (memq (following-char) ascii))
193 (memq (following-char) control)
194 (memq (following-char) tspecial)
195 (memq (following-char) special)
196 (eq (following-char) ? ))
198 (insert "%" (format "%02x" (following-char)))
201 (goto-char (point-min))
202 (insert (symbol-name (or charset 'us-ascii)) "''")
203 (goto-char (point-min))
207 (insert (if (>= num 0) " " "\n ")
208 param "*" (format "%d" (incf num)) "*=")
211 (goto-char (point-min))
213 (goto-char (point-max))
216 (goto-char (point-min))
222 ;;; rfc2231.el ends here