Sync with Gnus.
[elisp/gnus.git-] / 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 (eval-when-compile (require 'cl))
27 (eval-when-compile (require 'gnus-clfns))
28
29 (require 'ietf-drums)
30
31 (defun rfc2231-get-value (ct attribute)
32   "Return the value of ATTRIBUTE from CT."
33   (cdr (assq attribute (cdr ct))))
34
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)...)"
39   (with-temp-buffer
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"))
43           (prev-value "")
44           display-name mailbox c display-string parameters
45           attribute value type subtype number encoded
46           prev-attribute)
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))
55       (setq c (char-after))
56       (when (and (memq c ttoken)
57                  (not (memq c stoken)))
58         (setq type (downcase (buffer-substring
59                               (point) (progn (forward-sexp 1) (point)))))
60         ;; Do the params
61         (while (not (eobp))
62           (setq c (char-after))
63           (unless (eq c ?\;)
64             (error "Invalid header: %s" string))
65           (forward-char 1)
66           ;; If c in nil, then this is an invalid header, but
67           ;; since elm generates invalid headers on this form,
68           ;; we allow it.
69           (when (setq c (char-after))
70             (if (and (memq c ttoken)
71                      (not (memq c stoken)))
72                 (setq attribute
73                       (intern
74                        (downcase
75                         (buffer-substring
76                          (point) (progn (forward-sexp 1) (point))))))
77               (error "Invalid header: %s" string))
78             (setq c (char-after))
79             (setq encoded nil)
80             (when (eq c ?*)
81               (forward-char 1)
82               (setq c (char-after))
83               (when (memq c ntoken)
84                 (setq number
85                       (string-to-number
86                        (buffer-substring
87                         (point) (progn (forward-sexp 1) (point)))))
88                 (setq c (char-after))
89                 (when (eq c ?*)
90                   (setq encoded t)
91                   (forward-char 1)
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
98                     prev-value ""))
99             (unless (eq c ?=)
100               (error "Invalid header: %s" string))
101             (forward-char 1)
102             (setq c (char-after))
103             (cond
104              ((eq c ?\")
105               (setq value
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)))))
112              (t
113               (error "Invalid header: %s" string)))
114             (when encoded
115               (setq value (rfc2231-decode-encoded-string value)))
116             (if number
117                 (setq prev-attribute attribute
118                       prev-value (concat prev-value value))
119               (push (cons attribute value) parameters))))
120
121         ;; Take care of any final continuations.
122         (when prev-attribute
123           (push (cons prev-attribute prev-value) parameters))
124
125         (when type
126           `(,type ,@(nreverse parameters)))))))
127
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\"."
131   (with-temp-buffer
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)
139         (insert
140          (prog1
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))))
148       (buffer-string))))
149
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))
156         (num -1)
157         spacep encodep charsetp charset broken)
158     (with-temp-buffer
159       (insert value)
160       (goto-char (point-min))
161       (while (not (eobp))
162         (cond
163          ((or (memq (following-char) control)
164               (memq (following-char) tspecial)
165               (memq (following-char) special))
166           (setq encodep t))
167          ((eq (following-char) ? )
168           (setq spacep t))
169          ((not (memq (following-char) ascii))
170           (setq charsetp t)))
171         (forward-char 1))
172       (when charsetp
173         (setq charset (mm-encode-body)))
174       (cond
175        ((or encodep charsetp)
176         (goto-char (point-min))
177         (while (not (eobp))
178           (when (> (current-column) 60)
179             (insert "\n")
180             (setq broken t))
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) ? ))
186               (progn
187                 (insert "%" (format "%02x" (following-char)))
188                 (delete-char 1))
189             (forward-char 1)))
190         (goto-char (point-min))
191         (insert (or charset "ascii") "''")
192         (goto-char (point-min))
193         (if (not broken)
194             (insert param "*=")
195           (while (not (eobp))
196             (insert param "*" (format "%d" (incf num)) "*=")
197             (forward-line 1))))
198        (spacep
199         (goto-char (point-min))
200         (insert param "=\"")
201         (goto-char (point-max))
202         (insert "\""))
203        (t
204         (goto-char (point-min))
205         (insert param "=")))
206       (buffer-string))))
207
208 (provide 'rfc2231)
209
210 ;;; rfc2231.el ends here