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))
64 (if (and (memq c ttoken)
65 (not (memq c stoken)))
70 (point) (progn (forward-sexp 1) (point))))))
71 (error "Invalid header: %s" string))
81 (point) (progn (forward-sexp 1) (point)))))
86 (setq c (char-after)))))
87 ;; See if we have any previous continuations.
88 (when (and prev-attribute
89 (not (eq prev-attribute attribute)))
90 (push (cons prev-attribute prev-value) parameters)
91 (setq prev-attribute nil
94 (error "Invalid header: %s" string))
100 (buffer-substring (1+ (point))
101 (progn (forward-sexp 1) (1- (point))))))
102 ((and (memq c ttoken)
103 (not (memq c stoken)))
104 (setq value (buffer-substring
105 (point) (progn (forward-sexp 1) (point)))))
107 (error "Invalid header: %s" string)))
109 (setq value (rfc2231-decode-encoded-string value)))
111 (setq prev-attribute attribute
112 prev-value (concat prev-value value))
113 (push (cons attribute value) parameters)))
115 ;; Take care of any final continuations.
117 (push (cons prev-attribute prev-value) parameters))
120 `(,type ,@(nreverse parameters)))))))
122 (defun rfc2231-decode-encoded-string (string)
123 "Decode an RFC2231-encoded string.
124 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
126 (let ((elems (split-string string "'")))
127 ;; The encoded string may contain zero to two single-quote
128 ;; marks. This should give us the encoded word stripped
129 ;; of any preceding values.
130 (insert (car (last elems)))
131 (goto-char (point-min))
132 (while (search-forward "%" nil t)
135 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
136 (delete-region (1- (point)) (+ (point) 2)))))
137 ;; Encode using the charset, if any.
138 (when (and (< (length elems) 1)
139 (not (equal (intern (car elems)) 'us-ascii)))
140 (mm-decode-coding-region (point-min) (point-max)
141 (intern (car elems))))
144 (defun rfc2231-encode-string (param value)
145 "Return and PARAM=VALUE string encoded according to RFC2231."
146 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
147 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
148 (special (ietf-drums-token-to-list "*'%\n\t"))
149 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
151 spacep encodep charsetp charset broken)
154 (goto-char (point-min))
157 ((or (memq (following-char) control)
158 (memq (following-char) tspecial)
159 (memq (following-char) special))
161 ((eq (following-char) ? )
163 ((not (memq (following-char) ascii))
167 (setq charset (mm-encode-body)))
169 ((or encodep charsetp)
170 (goto-char (point-min))
172 (when (> (current-column) 60)
175 (if (or (not (memq (following-char) ascii))
176 (memq (following-char) control)
177 (memq (following-char) tspecial)
178 (memq (following-char) special)
179 (eq (following-char) ? ))
181 (insert "%" (format "%02x" (following-char)))
184 (goto-char (point-min))
185 (insert (or charset "ascii") "''")
186 (goto-char (point-min))
190 (insert param "*" (format "%d" (incf num)) "*=")
193 (goto-char (point-min))
195 (goto-char (point-max))
198 (goto-char (point-min))
204 ;;; rfc2231.el ends here