1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
26 (eval-when-compile (require 'cl))
27 (eval-when-compile (require 'gnus-clfns))
29 (eval-when-compile (require 'cl))
33 (defun rfc2231-get-value (ct attribute)
34 "Return the value of ATTRIBUTE from CT."
35 (cdr (assq attribute (cdr ct))))
37 (defun rfc2231-parse-qp-string (string)
38 "Parse QP-encoded string using `rfc2231-parse-string'.
39 N.B. This is in violation with RFC2047, but it seem to be in common use."
40 (rfc2231-parse-string (rfc2047-decode-string string)))
42 (defun rfc2231-parse-string (string)
43 "Parse STRING and return a list.
44 The list will be on the form
45 `(name (attribute . value) (attribute . value)...)"
47 (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
48 (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
49 (ntoken (ietf-drums-token-to-list "0-9"))
51 display-name mailbox c display-string parameters
52 attribute value type subtype number encoded
54 (ietf-drums-init (mail-header-remove-whitespace
55 (mail-header-remove-comments string)))
56 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
57 (modify-syntax-entry ?\' "w" table)
58 ;; The following isn't valid, but one should be liberal
59 ;; in what one receives.
60 (modify-syntax-entry ?\: "w" table)
61 (set-syntax-table table))
63 (when (and (memq c ttoken)
64 (not (memq c stoken)))
65 (setq type (downcase (buffer-substring
66 (point) (progn (forward-sexp 1) (point)))))
71 (error "Invalid header: %s" string))
73 ;; If c in nil, then this is an invalid header, but
74 ;; since elm generates invalid headers on this form,
76 (when (setq c (char-after))
77 (if (and (memq c ttoken)
78 (not (memq c stoken)))
83 (point) (progn (forward-sexp 1) (point))))))
84 (error "Invalid header: %s" string))
90 (if (not (memq c ntoken))
96 (point) (progn (forward-sexp 1) (point)))))
101 (setq c (char-after)))))
102 ;; See if we have any previous continuations.
103 (when (and prev-attribute
104 (not (eq prev-attribute attribute)))
105 (push (cons prev-attribute prev-value) parameters)
106 (setq prev-attribute nil
109 (error "Invalid header: %s" string))
111 (setq c (char-after))
115 (buffer-substring (1+ (point))
116 (progn (forward-sexp 1) (1- (point))))))
117 ((and (memq c ttoken)
118 (not (memq c stoken)))
119 (setq value (buffer-substring
120 (point) (progn (forward-sexp 1) (point)))))
122 (error "Invalid header: %s" string)))
124 (setq value (rfc2231-decode-encoded-string value)))
126 (setq prev-attribute attribute
127 prev-value (concat prev-value value))
128 (push (cons attribute value) parameters))))
130 ;; Take care of any final continuations.
132 (push (cons prev-attribute prev-value) parameters))
135 `(,type ,@(nreverse parameters)))))))
137 (defun rfc2231-decode-encoded-string (string)
138 "Decode an RFC2231-encoded string.
139 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
141 (let ((elems (split-string string "'")))
142 ;; The encoded string may contain zero to two single-quote
143 ;; marks. This should give us the encoded word stripped
144 ;; of any preceding values.
145 (insert (car (last elems)))
146 (goto-char (point-min))
147 (while (search-forward "%" nil t)
150 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
151 (delete-region (1- (point)) (+ (point) 2)))))
152 ;; Encode using the charset, if any.
153 (when (and (mm-multibyte-p)
155 (not (equal (intern (car elems)) 'us-ascii)))
156 (mm-decode-coding-region (point-min) (point-max)
157 (intern (car elems))))
160 (defun rfc2231-encode-string (param value)
161 "Return and PARAM=VALUE string encoded according to RFC2231."
162 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
163 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
164 (special (ietf-drums-token-to-list "*'%\n\t"))
165 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
167 spacep encodep charsetp charset broken)
170 (goto-char (point-min))
173 ((or (memq (following-char) control)
174 (memq (following-char) tspecial)
175 (memq (following-char) special))
177 ((eq (following-char) ? )
179 ((not (memq (following-char) ascii))
183 (setq charset (mm-encode-body)))
185 ((or encodep charsetp)
186 (goto-char (point-min))
188 (when (> (current-column) 60)
191 (if (or (not (memq (following-char) ascii))
192 (memq (following-char) control)
193 (memq (following-char) tspecial)
194 (memq (following-char) special)
195 (eq (following-char) ? ))
197 (insert "%" (format "%02x" (following-char)))
200 (goto-char (point-min))
201 (insert (symbol-name (or charset 'us-ascii)) "''")
202 (goto-char (point-min))
206 (insert (if (>= num 0) " " "\n ")
207 param "*" (format "%d" (incf num)) "*=")
210 (goto-char (point-min))
212 (goto-char (point-max))
215 (goto-char (point-min))
221 ;;; rfc2231.el ends here