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))
29 (defun rfc2231-get-value (ct attribute)
30 "Return the value of ATTRIBUTE from CT."
31 (cdr (assq attribute (cdr ct))))
33 (defun rfc2231-parse-string (string)
34 "Parse STRING and return a list.
35 The list will be on the form
36 `(name (attribute . value) (attribute . value)...)"
38 (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
39 (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
40 (ntoken (ietf-drums-token-to-list "0-9"))
42 display-name mailbox c display-string parameters
43 attribute value type subtype number encoded
45 (ietf-drums-init (mail-header-remove-whitespace
46 (mail-header-remove-comments string)))
47 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
48 (modify-syntax-entry ?\' "w" table)
49 ;; The following isn't valid, but one should be liberal
50 ;; in what one receives.
51 (modify-syntax-entry ?\: "w" table)
52 (set-syntax-table table))
54 (when (and (memq c ttoken)
55 (not (memq c stoken)))
56 (setq type (downcase (buffer-substring
57 (point) (progn (forward-sexp 1) (point)))))
62 (error "Invalid header: %s" string))
64 ;; If c in nil, then this is an invalid header, but
65 ;; since elm generates invalid headers on this form,
67 (when (setq c (char-after))
68 (if (and (memq c ttoken)
69 (not (memq c stoken)))
74 (point) (progn (forward-sexp 1) (point))))))
75 (error "Invalid header: %s" string))
85 (point) (progn (forward-sexp 1) (point)))))
90 (setq c (char-after)))))
91 ;; See if we have any previous continuations.
92 (when (and prev-attribute
93 (not (eq prev-attribute attribute)))
94 (push (cons prev-attribute prev-value) parameters)
95 (setq prev-attribute nil
98 (error "Invalid header: %s" string))
100 (setq c (char-after))
104 (buffer-substring (1+ (point))
105 (progn (forward-sexp 1) (1- (point))))))
106 ((and (memq c ttoken)
107 (not (memq c stoken)))
108 (setq value (buffer-substring
109 (point) (progn (forward-sexp 1) (point)))))
111 (error "Invalid header: %s" string)))
113 (setq value (rfc2231-decode-encoded-string value)))
115 (setq prev-attribute attribute
116 prev-value (concat prev-value value))
117 (push (cons attribute value) parameters))))
119 ;; Take care of any final continuations.
121 (push (cons prev-attribute prev-value) parameters))
124 `(,type ,@(nreverse parameters)))))))
126 (defun rfc2231-decode-encoded-string (string)
127 "Decode an RFC2231-encoded string.
128 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
130 (let ((elems (split-string string "'")))
131 ;; The encoded string may contain zero to two single-quote
132 ;; marks. This should give us the encoded word stripped
133 ;; of any preceding values.
134 (insert (car (last elems)))
135 (goto-char (point-min))
136 (while (search-forward "%" nil t)
139 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
140 (delete-region (1- (point)) (+ (point) 2)))))
141 ;; Encode using the charset, if any.
142 (when (and (< (length elems) 1)
143 (not (equal (intern (car elems)) 'us-ascii)))
144 (mm-decode-coding-region (point-min) (point-max)
145 (intern (car elems))))
148 (defun rfc2231-encode-string (param value)
149 "Return and PARAM=VALUE string encoded according to RFC2231."
150 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
151 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
152 (special (ietf-drums-token-to-list "*'%\n\t"))
153 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
155 spacep encodep charsetp charset broken)
158 (goto-char (point-min))
161 ((or (memq (following-char) control)
162 (memq (following-char) tspecial)
163 (memq (following-char) special))
165 ((eq (following-char) ? )
167 ((not (memq (following-char) ascii))
171 (setq charset (mm-encode-body)))
173 ((or encodep charsetp)
174 (goto-char (point-min))
176 (when (> (current-column) 60)
179 (if (or (not (memq (following-char) ascii))
180 (memq (following-char) control)
181 (memq (following-char) tspecial)
182 (memq (following-char) special)
183 (eq (following-char) ? ))
185 (insert "%" (format "%02x" (following-char)))
188 (goto-char (point-min))
189 (insert (or charset "ascii") "''")
190 (goto-char (point-min))
194 (insert param "*" (format "%d" (incf num)) "*=")
197 (goto-char (point-min))
199 (goto-char (point-max))
202 (goto-char (point-min))
208 ;;; rfc2231.el ends here