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.
28 (defun rfc2231-get-value (ct attribute)
29 "Return the value of ATTRIBUTE from CT."
30 (cdr (assq attribute (cdr ct))))
32 (defun rfc2231-parse-string (string)
33 "Parse STRING and return a list.
34 The list will be on the form
35 `(name (attribute . value) (attribute . value)...)"
37 (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
38 (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
39 (ntoken (ietf-drums-token-to-list "0-9"))
41 display-name mailbox c display-string parameters
42 attribute value type subtype number encoded
44 (ietf-drums-init (mail-header-remove-whitespace
45 (mail-header-remove-comments string)))
46 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
47 (modify-syntax-entry ?\' "w" table)
48 ;; The following isn't valid, but one should be liberal
49 ;; in what one receives.
50 (modify-syntax-entry ?\: "w" table)
51 (set-syntax-table table))
53 (when (and (memq c ttoken)
54 (not (memq c stoken)))
55 (setq type (downcase (buffer-substring
56 (point) (progn (forward-sexp 1) (point)))))
61 (error "Invalid header: %s" string))
63 ;; If c in nil, then this is an invalid header, but
64 ;; since elm generates invalid headers on this form,
66 (when (setq c (char-after))
67 (if (and (memq c ttoken)
68 (not (memq c stoken)))
73 (point) (progn (forward-sexp 1) (point))))))
74 (error "Invalid header: %s" string))
84 (point) (progn (forward-sexp 1) (point)))))
89 (setq c (char-after)))))
90 ;; See if we have any previous continuations.
91 (when (and prev-attribute
92 (not (eq prev-attribute attribute)))
93 (push (cons prev-attribute prev-value) parameters)
94 (setq prev-attribute nil
97 (error "Invalid header: %s" string))
103 (buffer-substring (1+ (point))
104 (progn (forward-sexp 1) (1- (point))))))
105 ((and (memq c ttoken)
106 (not (memq c stoken)))
107 (setq value (buffer-substring
108 (point) (progn (forward-sexp 1) (point)))))
110 (error "Invalid header: %s" string)))
112 (setq value (rfc2231-decode-encoded-string value)))
114 (setq prev-attribute attribute
115 prev-value (concat prev-value value))
116 (push (cons attribute value) parameters))))
118 ;; Take care of any final continuations.
120 (push (cons prev-attribute prev-value) parameters))
123 `(,type ,@(nreverse parameters)))))))
125 (defun rfc2231-decode-encoded-string (string)
126 "Decode an RFC2231-encoded string.
127 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
129 (let ((elems (split-string string "'")))
130 ;; The encoded string may contain zero to two single-quote
131 ;; marks. This should give us the encoded word stripped
132 ;; of any preceding values.
133 (insert (car (last elems)))
134 (goto-char (point-min))
135 (while (search-forward "%" nil t)
138 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
139 (delete-region (1- (point)) (+ (point) 2)))))
140 ;; Encode using the charset, if any.
141 (when (and (< (length elems) 1)
142 (not (equal (intern (car elems)) 'us-ascii)))
143 (mm-decode-coding-region (point-min) (point-max)
144 (intern (car elems))))
147 (defun rfc2231-encode-string (param value)
148 "Return and PARAM=VALUE string encoded according to RFC2231."
149 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
150 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
151 (special (ietf-drums-token-to-list "*'%\n\t"))
152 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
154 spacep encodep charsetp charset broken)
157 (goto-char (point-min))
160 ((or (memq (following-char) control)
161 (memq (following-char) tspecial)
162 (memq (following-char) special))
164 ((eq (following-char) ? )
166 ((not (memq (following-char) ascii))
170 (setq charset (mm-encode-body)))
172 ((or encodep charsetp)
173 (goto-char (point-min))
175 (when (> (current-column) 60)
178 (if (or (not (memq (following-char) ascii))
179 (memq (following-char) control)
180 (memq (following-char) tspecial)
181 (memq (following-char) special)
182 (eq (following-char) ? ))
184 (insert "%" (format "%02x" (following-char)))
187 (goto-char (point-min))
188 (insert (or charset "ascii") "''")
189 (goto-char (point-min))
193 (insert param "*" (format "%d" (incf num)) "*=")
196 (goto-char (point-min))
198 (goto-char (point-max))
201 (goto-char (point-min))
207 ;;; rfc2231.el ends here