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))
30 (defun rfc2231-get-value (ct attribute)
31 "Return the value of ATTRIBUTE from CT."
32 (cdr (assq attribute (cdr ct))))
34 (defun rfc2231-parse-string (string)
35 "Parse STRING and return a list.
36 The list will be on the form
37 `(name (attribute . value) (attribute . value)...)"
39 (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
40 (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
41 (ntoken (ietf-drums-token-to-list "0-9"))
43 display-name mailbox c display-string parameters
44 attribute value type subtype number encoded
46 (ietf-drums-init (mail-header-remove-whitespace
47 (mail-header-remove-comments string)))
48 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
49 (modify-syntax-entry ?\' "w" table)
50 ;; The following isn't valid, but one should be liberal
51 ;; in what one receives.
52 (modify-syntax-entry ?\: "w" table)
53 (set-syntax-table table))
55 (when (and (memq c ttoken)
56 (not (memq c stoken)))
57 (setq type (downcase (buffer-substring
58 (point) (progn (forward-sexp 1) (point)))))
63 (error "Invalid header: %s" string))
65 ;; If c in nil, then this is an invalid header, but
66 ;; since elm generates invalid headers on this form,
68 (when (setq c (char-after))
69 (if (and (memq c ttoken)
70 (not (memq c stoken)))
75 (point) (progn (forward-sexp 1) (point))))))
76 (error "Invalid header: %s" string))
86 (point) (progn (forward-sexp 1) (point)))))
91 (setq c (char-after)))))
92 ;; See if we have any previous continuations.
93 (when (and prev-attribute
94 (not (eq prev-attribute attribute)))
95 (push (cons prev-attribute prev-value) parameters)
96 (setq prev-attribute nil
99 (error "Invalid header: %s" string))
101 (setq c (char-after))
105 (buffer-substring (1+ (point))
106 (progn (forward-sexp 1) (1- (point))))))
107 ((and (memq c ttoken)
108 (not (memq c stoken)))
109 (setq value (buffer-substring
110 (point) (progn (forward-sexp 1) (point)))))
112 (error "Invalid header: %s" string)))
114 (setq value (rfc2231-decode-encoded-string value)))
116 (setq prev-attribute attribute
117 prev-value (concat prev-value value))
118 (push (cons attribute value) parameters))))
120 ;; Take care of any final continuations.
122 (push (cons prev-attribute prev-value) parameters))
125 `(,type ,@(nreverse parameters)))))))
127 (defun rfc2231-decode-encoded-string (string)
128 "Decode an RFC2231-encoded string.
129 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
131 (let ((elems (split-string string "'")))
132 ;; The encoded string may contain zero to two single-quote
133 ;; marks. This should give us the encoded word stripped
134 ;; of any preceding values.
135 (insert (car (last elems)))
136 (goto-char (point-min))
137 (while (search-forward "%" nil t)
140 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
141 (delete-region (1- (point)) (+ (point) 2)))))
142 ;; Encode using the charset, if any.
143 (when (and (< (length elems) 1)
144 (not (equal (intern (car elems)) 'us-ascii)))
145 (mm-decode-coding-region (point-min) (point-max)
146 (intern (car elems))))
149 (defun rfc2231-encode-string (param value)
150 "Return and PARAM=VALUE string encoded according to RFC2231."
151 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
152 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
153 (special (ietf-drums-token-to-list "*'%\n\t"))
154 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
156 spacep encodep charsetp charset broken)
159 (goto-char (point-min))
162 ((or (memq (following-char) control)
163 (memq (following-char) tspecial)
164 (memq (following-char) special))
166 ((eq (following-char) ? )
168 ((not (memq (following-char) ascii))
172 (setq charset (mm-encode-body)))
174 ((or encodep charsetp)
175 (goto-char (point-min))
177 (when (> (current-column) 60)
180 (if (or (not (memq (following-char) ascii))
181 (memq (following-char) control)
182 (memq (following-char) tspecial)
183 (memq (following-char) special)
184 (eq (following-char) ? ))
186 (insert "%" (format "%02x" (following-char)))
189 (goto-char (point-min))
190 (insert (or charset "ascii") "''")
191 (goto-char (point-min))
195 (insert param "*" (format "%d" (incf num)) "*=")
198 (goto-char (point-min))
200 (goto-char (point-max))
203 (goto-char (point-min))
209 ;;; arch-tag: c3ab751d-d108-406a-b301-68882ad8cd63
210 ;;; rfc2231.el ends here