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))
31 (defun rfc2231-get-value (ct attribute)
32 "Return the value of ATTRIBUTE from CT."
33 (cdr (assq attribute (cdr ct))))
35 (defun rfc2231-parse-string (string)
36 "Parse STRING and return a list.
37 The list will be on the form
38 `(name (attribute . value) (attribute . value)...)"
40 (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
41 (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
42 (ntoken (ietf-drums-token-to-list "0-9"))
44 display-name mailbox c display-string parameters
45 attribute value type subtype number encoded
47 (ietf-drums-init (mail-header-remove-whitespace
48 (mail-header-remove-comments string)))
49 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
50 (modify-syntax-entry ?\' "w" table)
51 ;; The following isn't valid, but one should be liberal
52 ;; in what one receives.
53 (modify-syntax-entry ?\: "w" table)
54 (set-syntax-table table))
56 (when (and (memq c ttoken)
57 (not (memq c stoken)))
58 (setq type (downcase (buffer-substring
59 (point) (progn (forward-sexp 1) (point)))))
64 (error "Invalid header: %s" string))
66 ;; If c in nil, then this is an invalid header, but
67 ;; since elm generates invalid headers on this form,
69 (when (setq c (char-after))
70 (if (and (memq c ttoken)
71 (not (memq c stoken)))
76 (point) (progn (forward-sexp 1) (point))))))
77 (error "Invalid header: %s" string))
87 (point) (progn (forward-sexp 1) (point)))))
92 (setq c (char-after)))))
93 ;; See if we have any previous continuations.
94 (when (and prev-attribute
95 (not (eq prev-attribute attribute)))
96 (push (cons prev-attribute prev-value) parameters)
97 (setq prev-attribute nil
100 (error "Invalid header: %s" string))
102 (setq c (char-after))
106 (buffer-substring (1+ (point))
107 (progn (forward-sexp 1) (1- (point))))))
108 ((and (memq c ttoken)
109 (not (memq c stoken)))
110 (setq value (buffer-substring
111 (point) (progn (forward-sexp 1) (point)))))
113 (error "Invalid header: %s" string)))
115 (setq value (rfc2231-decode-encoded-string value)))
117 (setq prev-attribute attribute
118 prev-value (concat prev-value value))
119 (push (cons attribute value) parameters))))
121 ;; Take care of any final continuations.
123 (push (cons prev-attribute prev-value) parameters))
126 `(,type ,@(nreverse parameters)))))))
128 (defun rfc2231-decode-encoded-string (string)
129 "Decode an RFC2231-encoded string.
130 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
132 (let ((elems (split-string string "'")))
133 ;; The encoded string may contain zero to two single-quote
134 ;; marks. This should give us the encoded word stripped
135 ;; of any preceding values.
136 (insert (car (last elems)))
137 (goto-char (point-min))
138 (while (search-forward "%" nil t)
141 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
142 (delete-region (1- (point)) (+ (point) 2)))))
143 ;; Encode using the charset, if any.
144 (when (and (< (length elems) 1)
145 (not (equal (intern (car elems)) 'us-ascii)))
146 (mm-decode-coding-region (point-min) (point-max)
147 (intern (car elems))))
150 (defun rfc2231-encode-string (param value)
151 "Return and PARAM=VALUE string encoded according to RFC2231."
152 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
153 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
154 (special (ietf-drums-token-to-list "*'%\n\t"))
155 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
157 spacep encodep charsetp charset broken)
160 (goto-char (point-min))
163 ((or (memq (following-char) control)
164 (memq (following-char) tspecial)
165 (memq (following-char) special))
167 ((eq (following-char) ? )
169 ((not (memq (following-char) ascii))
173 (setq charset (mm-encode-body)))
175 ((or encodep charsetp)
176 (goto-char (point-min))
178 (when (> (current-column) 60)
181 (if (or (not (memq (following-char) ascii))
182 (memq (following-char) control)
183 (memq (following-char) tspecial)
184 (memq (following-char) special)
185 (eq (following-char) ? ))
187 (insert "%" (format "%02x" (following-char)))
190 (goto-char (point-min))
191 (insert (or charset "ascii") "''")
192 (goto-char (point-min))
196 (insert param "*" (format "%d" (incf num)) "*=")
199 (goto-char (point-min))
201 (goto-char (point-max))
204 (goto-char (point-min))
210 ;;; rfc2231.el ends here