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))
29 (eval-when-compile (require 'cl))
32 (defun rfc2231-get-value (ct attribute)
33 "Return the value of ATTRIBUTE from CT."
34 (cdr (assq attribute (cdr ct))))
36 (defun rfc2231-parse-string (string)
37 "Parse STRING and return a list.
38 The list will be on the form
39 `(name (attribute . value) (attribute . value)...)"
41 (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
42 (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
43 (ntoken (ietf-drums-token-to-list "0-9"))
45 display-name mailbox c display-string parameters
46 attribute value type subtype number encoded
48 (ietf-drums-init (mail-header-remove-whitespace
49 (mail-header-remove-comments string)))
50 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
51 (modify-syntax-entry ?\' "w" table)
52 ;; The following isn't valid, but one should be liberal
53 ;; in what one receives.
54 (modify-syntax-entry ?\: "w" table)
55 (set-syntax-table table))
57 (when (and (memq c ttoken)
58 (not (memq c stoken)))
59 (setq type (downcase (buffer-substring
60 (point) (progn (forward-sexp 1) (point)))))
65 (error "Invalid header: %s" string))
67 ;; If c in nil, then this is an invalid header, but
68 ;; since elm generates invalid headers on this form,
70 (when (setq c (char-after))
71 (if (and (memq c ttoken)
72 (not (memq c stoken)))
77 (point) (progn (forward-sexp 1) (point))))))
78 (error "Invalid header: %s" string))
88 (point) (progn (forward-sexp 1) (point)))))
93 (setq c (char-after)))))
94 ;; See if we have any previous continuations.
95 (when (and prev-attribute
96 (not (eq prev-attribute attribute)))
97 (push (cons prev-attribute prev-value) parameters)
98 (setq prev-attribute nil
101 (error "Invalid header: %s" string))
103 (setq c (char-after))
107 (buffer-substring (1+ (point))
108 (progn (forward-sexp 1) (1- (point))))))
109 ((and (memq c ttoken)
110 (not (memq c stoken)))
111 (setq value (buffer-substring
112 (point) (progn (forward-sexp 1) (point)))))
114 (error "Invalid header: %s" string)))
116 (setq value (rfc2231-decode-encoded-string value)))
118 (setq prev-attribute attribute
119 prev-value (concat prev-value value))
120 (push (cons attribute value) parameters))))
122 ;; Take care of any final continuations.
124 (push (cons prev-attribute prev-value) parameters))
127 `(,type ,@(nreverse parameters)))))))
129 (defun rfc2231-decode-encoded-string (string)
130 "Decode an RFC2231-encoded string.
131 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
133 (let ((elems (split-string string "'")))
134 ;; The encoded string may contain zero to two single-quote
135 ;; marks. This should give us the encoded word stripped
136 ;; of any preceding values.
137 (insert (car (last elems)))
138 (goto-char (point-min))
139 (while (search-forward "%" nil t)
142 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
143 (delete-region (1- (point)) (+ (point) 2)))))
144 ;; Encode using the charset, if any.
145 (when (and (< (length elems) 1)
146 (not (equal (intern (car elems)) 'us-ascii)))
147 (mm-decode-coding-region (point-min) (point-max)
148 (intern (car elems))))
151 (defun rfc2231-encode-string (param value)
152 "Return and PARAM=VALUE string encoded according to RFC2231."
153 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
154 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
155 (special (ietf-drums-token-to-list "*'%\n\t"))
156 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
158 spacep encodep charsetp charset broken)
161 (goto-char (point-min))
164 ((or (memq (following-char) control)
165 (memq (following-char) tspecial)
166 (memq (following-char) special))
168 ((eq (following-char) ? )
170 ((not (memq (following-char) ascii))
174 (setq charset (mm-encode-body)))
176 ((or encodep charsetp)
177 (goto-char (point-min))
179 (when (> (current-column) 60)
182 (if (or (not (memq (following-char) ascii))
183 (memq (following-char) control)
184 (memq (following-char) tspecial)
185 (memq (following-char) special)
186 (eq (following-char) ? ))
188 (insert "%" (format "%02x" (following-char)))
191 (goto-char (point-min))
192 (insert (or charset "ascii") "''")
193 (goto-char (point-min))
197 (insert param "*" (format "%d" (incf num)) "*=")
200 (goto-char (point-min))
202 (goto-char (point-max))
205 (goto-char (point-min))
211 ;;; rfc2231.el ends here