Feedback from the t-gnus-6_15 branch.
[elisp/gnus.git-] / lisp / rfc2231.el
1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
2
3 ;; Copyright (C) 1998, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (eval-when-compile (require 'gnus-clfns))
29
30 (eval-when-compile (require 'cl))
31 (require 'ietf-drums)
32 (require 'rfc2047)
33
34 (defun rfc2231-get-value (ct attribute)
35   "Return the value of ATTRIBUTE from CT."
36   (cdr (assq attribute (cdr ct))))
37
38 (defun rfc2231-parse-qp-string (string)
39   "Parse QP-encoded string using `rfc2231-parse-string'.
40 N.B.  This is in violation with RFC2047, but it seem to be in common use."
41   (rfc2231-parse-string (rfc2047-decode-string string)))
42
43 (defun rfc2231-parse-string (string)
44   "Parse STRING and return a list.
45 The list will be on the form
46  `(name (attribute . value) (attribute . value)...)"
47   (with-temp-buffer
48     (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
49           (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
50           (ntoken (ietf-drums-token-to-list "0-9"))
51           (prev-value "")
52           display-name mailbox c display-string parameters
53           attribute value type subtype number encoded
54           prev-attribute)
55       (ietf-drums-init (mail-header-remove-whitespace
56                         (mail-header-remove-comments string)))
57       (let ((table (copy-syntax-table ietf-drums-syntax-table)))
58         (modify-syntax-entry ?\' "w" table)
59         ;; The following isn't valid, but one should be liberal
60         ;; in what one receives.
61         (modify-syntax-entry ?\: "w" table)
62         (set-syntax-table table))
63       (setq c (char-after))
64       (when (and (memq c ttoken)
65                  (not (memq c stoken)))
66         (setq type (downcase (buffer-substring
67                               (point) (progn (forward-sexp 1) (point)))))
68         ;; Do the params
69         (while (not (eobp))
70           (setq c (char-after))
71           (unless (eq c ?\;)
72             (error "Invalid header: %s" string))
73           (forward-char 1)
74           ;; If c in nil, then this is an invalid header, but
75           ;; since elm generates invalid headers on this form,
76           ;; we allow it.
77           (when (setq c (char-after))
78             (if (and (memq c ttoken)
79                      (not (memq c stoken)))
80                 (setq attribute
81                       (intern
82                        (downcase
83                         (buffer-substring
84                          (point) (progn (forward-sexp 1) (point))))))
85               (error "Invalid header: %s" string))
86             (setq c (char-after))
87             (setq encoded nil)
88             (when (eq c ?*)
89               (forward-char 1)
90               (setq c (char-after))
91               (if (not (memq c ntoken))
92                   (setq encoded t
93                         number nil)
94                 (setq number
95                       (string-to-number
96                        (buffer-substring
97                         (point) (progn (forward-sexp 1) (point)))))
98                 (setq c (char-after))
99                 (when (eq c ?*)
100                   (setq encoded t)
101                   (forward-char 1)
102                   (setq c (char-after)))))
103             ;; See if we have any previous continuations.
104             (when (and prev-attribute
105                        (not (eq prev-attribute attribute)))
106               (push (cons prev-attribute prev-value) parameters)
107               (setq prev-attribute nil
108                     prev-value ""))
109             (unless (eq c ?=)
110               (error "Invalid header: %s" string))
111             (forward-char 1)
112             (setq c (char-after))
113             (cond
114              ((eq c ?\")
115               (setq value
116                     (buffer-substring (1+ (point))
117                                       (progn (forward-sexp 1) (1- (point))))))
118              ((and (or (memq c ttoken)
119                        (> c ?\177)) ;; EXTENSION: Support non-ascii chars.
120                    (not (memq c stoken)))
121               (setq value (buffer-substring
122                            (point) (progn (forward-sexp) (point)))))
123              (t
124               (error "Invalid header: %s" string)))
125             (when encoded
126               (setq value (rfc2231-decode-encoded-string value)))
127             (if number
128                 (setq prev-attribute attribute
129                       prev-value (concat prev-value value))
130               (push (cons attribute value) parameters))))
131
132         ;; Take care of any final continuations.
133         (when prev-attribute
134           (push (cons prev-attribute prev-value) parameters))
135
136         (when type
137           `(,type ,@(nreverse parameters)))))))
138
139 (defun rfc2231-decode-encoded-string (string)
140   "Decode an RFC2231-encoded string.
141 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
142   (with-temp-buffer
143     (let ((elems (split-string string "'")))
144       ;; The encoded string may contain zero to two single-quote
145       ;; marks.  This should give us the encoded word stripped
146       ;; of any preceding values.
147       (insert (car (last elems)))
148       (goto-char (point-min))
149       (while (search-forward "%" nil t)
150         (insert
151          (prog1
152              (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
153            (delete-region (1- (point)) (+ (point) 2)))))
154       ;; Encode using the charset, if any.
155       (when (and (mm-multibyte-p)
156                  (> (length elems) 1)
157                  (not (equal (intern (downcase (car elems))) 'us-ascii)))
158         (mm-decode-coding-region (point-min) (point-max)
159                                  (intern (downcase (car elems)))))
160       (buffer-string))))
161
162 (defun rfc2231-encode-string (param value)
163   "Return and PARAM=VALUE string encoded according to RFC2231."
164   (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
165         (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
166         (special (ietf-drums-token-to-list "*'%\n\t"))
167         (ascii (ietf-drums-token-to-list ietf-drums-text-token))
168         (num -1)
169         spacep encodep charsetp charset broken)
170     (with-temp-buffer
171       (insert value)
172       (goto-char (point-min))
173       (while (not (eobp))
174         (cond
175          ((or (memq (following-char) control)
176               (memq (following-char) tspecial)
177               (memq (following-char) special))
178           (setq encodep t))
179          ((eq (following-char) ? )
180           (setq spacep t))
181          ((not (memq (following-char) ascii))
182           (setq charsetp t)))
183         (forward-char 1))
184       (when charsetp
185         (setq charset (mm-encode-body)))
186       (cond
187        ((or encodep charsetp)
188         (goto-char (point-min))
189         (while (not (eobp))
190           (when (> (current-column) 60)
191             (insert ";\n")
192             (setq broken t))
193           (if (or (not (memq (following-char) ascii))
194                   (memq (following-char) control)
195                   (memq (following-char) tspecial)
196                   (memq (following-char) special)
197                   (eq (following-char) ? ))
198               (progn
199                 (insert "%" (format "%02x" (following-char)))
200                 (delete-char 1))
201             (forward-char 1)))
202         (goto-char (point-min))
203         (insert (symbol-name (or charset 'us-ascii)) "''")
204         (goto-char (point-min))
205         (if (not broken)
206             (insert param "*=")
207           (while (not (eobp))
208             (insert (if (>= num 0) " " "\n ")
209                     param "*" (format "%d" (incf num)) "*=")
210             (forward-line 1))))
211        (spacep
212         (goto-char (point-min))
213         (insert param "=\"")
214         (goto-char (point-max))
215         (insert "\""))
216        (t
217         (goto-char (point-min))
218         (insert param "=")))
219       (buffer-string))))
220
221 (provide 'rfc2231)
222
223 ;;; rfc2231.el ends here