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))
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-qp-string (string)
35 "Parse QP-encoded string using `rfc2231-parse-string'.
36 N.B. This is in violation with RFC2047, but it seem to be in common use."
37 (rfc2231-parse-string (rfc2047-decode-string string)))
39 (defun rfc2231-parse-string (string)
40 "Parse STRING and return a list.
41 The list will be on the form
42 `(name (attribute . value) (attribute . value)...)"
44 (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
45 (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
46 (ntoken (ietf-drums-token-to-list "0-9"))
48 display-name mailbox c display-string parameters
49 attribute value type subtype number encoded
51 (ietf-drums-init (mail-header-remove-whitespace
52 (mail-header-remove-comments string)))
53 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
54 (modify-syntax-entry ?\' "w" table)
55 ;; The following isn't valid, but one should be liberal
56 ;; in what one receives.
57 (modify-syntax-entry ?\: "w" table)
58 (set-syntax-table table))
60 (when (and (memq c ttoken)
61 (not (memq c stoken)))
62 (setq type (downcase (buffer-substring
63 (point) (progn (forward-sexp 1) (point)))))
68 (error "Invalid header: %s" string))
70 ;; If c in nil, then this is an invalid header, but
71 ;; since elm generates invalid headers on this form,
73 (when (setq c (char-after))
74 (if (and (memq c ttoken)
75 (not (memq c stoken)))
80 (point) (progn (forward-sexp 1) (point))))))
81 (error "Invalid header: %s" string))
87 (if (not (memq c ntoken))
93 (point) (progn (forward-sexp 1) (point)))))
98 (setq c (char-after)))))
99 ;; See if we have any previous continuations.
100 (when (and prev-attribute
101 (not (eq prev-attribute attribute)))
102 (push (cons prev-attribute prev-value) parameters)
103 (setq prev-attribute nil
106 (error "Invalid header: %s" string))
108 (setq c (char-after))
112 (buffer-substring (1+ (point))
113 (progn (forward-sexp 1) (1- (point))))))
114 ((and (memq c ttoken)
115 (not (memq c stoken)))
116 (setq value (buffer-substring
117 (point) (progn (forward-sexp 1) (point)))))
119 (error "Invalid header: %s" string)))
121 (setq value (rfc2231-decode-encoded-string value)))
123 (setq prev-attribute attribute
124 prev-value (concat prev-value value))
125 (push (cons attribute value) parameters))))
127 ;; Take care of any final continuations.
129 (push (cons prev-attribute prev-value) parameters))
132 `(,type ,@(nreverse parameters)))))))
134 (defun rfc2231-decode-encoded-string (string)
135 "Decode an RFC2231-encoded string.
136 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
138 (let ((elems (split-string string "'")))
139 ;; The encoded string may contain zero to two single-quote
140 ;; marks. This should give us the encoded word stripped
141 ;; of any preceding values.
142 (insert (car (last elems)))
143 (goto-char (point-min))
144 (while (search-forward "%" nil t)
147 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
148 (delete-region (1- (point)) (+ (point) 2)))))
149 ;; Encode using the charset, if any.
150 (when (and (mm-multibyte-p)
152 (not (equal (intern (car elems)) 'us-ascii)))
153 (mm-decode-coding-region (point-min) (point-max)
154 (intern (car elems))))
157 (defun rfc2231-encode-string (param value)
158 "Return and PARAM=VALUE string encoded according to RFC2231."
159 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
160 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
161 (special (ietf-drums-token-to-list "*'%\n\t"))
162 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
164 spacep encodep charsetp charset broken)
167 (goto-char (point-min))
170 ((or (memq (following-char) control)
171 (memq (following-char) tspecial)
172 (memq (following-char) special))
174 ((eq (following-char) ? )
176 ((not (memq (following-char) ascii))
180 (setq charset (mm-encode-body)))
182 ((or encodep charsetp)
183 (goto-char (point-min))
185 (when (> (current-column) 60)
188 (if (or (not (memq (following-char) ascii))
189 (memq (following-char) control)
190 (memq (following-char) tspecial)
191 (memq (following-char) special)
192 (eq (following-char) ? ))
194 (insert "%" (format "%02x" (following-char)))
197 (goto-char (point-min))
198 (insert (or (symbol-name charset) "ascii") "''")
199 (goto-char (point-min))
203 (insert param "*" (format "%d" (incf num)) "*=")
206 (goto-char (point-min))
208 (goto-char (point-max))
211 (goto-char (point-min))
217 ;;; rfc2231.el ends here