Use macro from w3. For details see ChangeLog.
[gnus] / lisp / rfc2231.el
1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
6
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)
10 ;; any later version.
11
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.
16
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.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (require 'ietf-drums)
27
28 (defun rfc2231-get-value (ct attribute)
29   "Return the value of ATTRIBUTE from CT."
30   (cdr (assq attribute (cdr ct))))
31
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)...)"
36   (with-temp-buffer
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"))
40           (prev-value "")
41           display-name mailbox c display-string parameters
42           attribute value type subtype number encoded
43           prev-attribute)
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))
52       (setq c (char-after))
53       (when (and (memq c ttoken)
54                  (not (memq c stoken)))
55         (setq type (downcase (buffer-substring
56                               (point) (progn (forward-sexp 1) (point)))))
57         ;; Do the params
58         (while (not (eobp))
59           (setq c (char-after))
60           (unless (eq c ?\;)
61             (error "Invalid header: %s" string))
62           (forward-char 1)
63           ;; If c in nil, then this is an invalid header, but
64           ;; since elm generates invalid headers on this form,
65           ;; we allow it.
66           (when (setq c (char-after))
67             (if (and (memq c ttoken)
68                      (not (memq c stoken)))
69                 (setq attribute
70                       (intern
71                        (downcase
72                         (buffer-substring
73                          (point) (progn (forward-sexp 1) (point))))))
74               (error "Invalid header: %s" string))
75             (setq c (char-after))
76             (setq encoded nil)
77             (when (eq c ?*)
78               (forward-char 1)
79               (setq c (char-after))
80               (when (memq c ntoken)
81                 (setq number
82                       (string-to-number
83                        (buffer-substring
84                         (point) (progn (forward-sexp 1) (point)))))
85                 (setq c (char-after))
86                 (when (eq c ?*)
87                   (setq encoded t)
88                   (forward-char 1)
89                   (setq c (char-after)))))
90             ;; See if we have any previous continuations.
91             (when (and prev-attribute
92                        (not (eq prev-attribute attribute)))
93               (push (cons prev-attribute prev-value) parameters)
94               (setq prev-attribute nil
95                     prev-value ""))
96             (unless (eq c ?=)
97               (error "Invalid header: %s" string))
98             (forward-char 1)
99             (setq c (char-after))
100             (cond
101              ((eq c ?\")
102               (setq value
103                     (buffer-substring (1+ (point))
104                                       (progn (forward-sexp 1) (1- (point))))))
105              ((and (memq c ttoken)
106                    (not (memq c stoken)))
107               (setq value (buffer-substring
108                            (point) (progn (forward-sexp 1) (point)))))
109              (t
110               (error "Invalid header: %s" string)))
111             (when encoded
112               (setq value (rfc2231-decode-encoded-string value)))
113             (if number
114                 (setq prev-attribute attribute
115                       prev-value (concat prev-value value))
116               (push (cons attribute value) parameters))))
117
118         ;; Take care of any final continuations.
119         (when prev-attribute
120           (push (cons prev-attribute prev-value) parameters))
121
122         (when type
123           `(,type ,@(nreverse parameters)))))))
124
125 (defun rfc2231-decode-encoded-string (string)
126   "Decode an RFC2231-encoded string.
127 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
128   (with-temp-buffer
129     (let ((elems (split-string string "'")))
130       ;; The encoded string may contain zero to two single-quote
131       ;; marks.  This should give us the encoded word stripped
132       ;; of any preceding values.
133       (insert (car (last elems)))
134       (goto-char (point-min))
135       (while (search-forward "%" nil t)
136         (insert
137          (prog1
138              (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
139            (delete-region (1- (point)) (+ (point) 2)))))
140       ;; Encode using the charset, if any.
141       (when (and (< (length elems) 1)
142                  (not (equal (intern (car elems)) 'us-ascii)))
143         (mm-decode-coding-region (point-min) (point-max)
144                                  (intern (car elems))))
145       (buffer-string))))
146
147 (defun rfc2231-encode-string (param value)
148   "Return and PARAM=VALUE string encoded according to RFC2231."
149   (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
150         (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
151         (special (ietf-drums-token-to-list "*'%\n\t"))
152         (ascii (ietf-drums-token-to-list ietf-drums-text-token))
153         (num -1)
154         spacep encodep charsetp charset broken)
155     (with-temp-buffer
156       (insert value)
157       (goto-char (point-min))
158       (while (not (eobp))
159         (cond
160          ((or (memq (following-char) control)
161               (memq (following-char) tspecial)
162               (memq (following-char) special))
163           (setq encodep t))
164          ((eq (following-char) ? )
165           (setq spacep t))
166          ((not (memq (following-char) ascii))
167           (setq charsetp t)))
168         (forward-char 1))
169       (when charsetp
170         (setq charset (mm-encode-body)))
171       (cond
172        ((or encodep charsetp)
173         (goto-char (point-min))
174         (while (not (eobp))
175           (when (> (current-column) 60)
176             (insert "\n")
177             (setq broken t))
178           (if (or (not (memq (following-char) ascii))
179                   (memq (following-char) control)
180                   (memq (following-char) tspecial)
181                   (memq (following-char) special)
182                   (eq (following-char) ? ))
183               (progn
184                 (insert "%" (format "%02x" (following-char)))
185                 (delete-char 1))
186             (forward-char 1)))
187         (goto-char (point-min))
188         (insert (or charset "ascii") "''")
189         (goto-char (point-min))
190         (if (not broken)
191             (insert param "*=")
192           (while (not (eobp))
193             (insert param "*" (format "%d" (incf num)) "*=")
194             (forward-line 1))))
195        (spacep
196         (goto-char (point-min))
197         (insert param "=\"")
198         (goto-char (point-max))
199         (insert "\""))
200        (t
201         (goto-char (point-min))
202         (insert param "=")))
203       (buffer-string))))
204
205 (provide 'rfc2231)
206
207 ;;; rfc2231.el ends here