Synch 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 (eval-when-compile (require 'cl))
30 (require 'ietf-drums)
31 (require 'rfc2047)
32
33 (defun rfc2231-get-value (ct attribute)
34   "Return the value of ATTRIBUTE from CT."
35   (cdr (assq attribute (cdr ct))))
36
37 (defun rfc2231-parse-qp-string (string)
38   "Parse QP-encoded string using `rfc2231-parse-string'.
39 N.B.  This is in violation with RFC2047, but it seem to be in common use."
40   (rfc2231-parse-string (rfc2047-decode-string string)))
41
42 (defun rfc2231-parse-string (string)
43   "Parse STRING and return a list.
44 The list will be on the form
45  `(name (attribute . value) (attribute . value)...)"
46   (with-temp-buffer
47     (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
48           (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
49           (ntoken (ietf-drums-token-to-list "0-9"))
50           (prev-value "")
51           display-name mailbox c display-string parameters
52           attribute value type subtype number encoded
53           prev-attribute)
54       (ietf-drums-init (mail-header-remove-whitespace
55                         (mail-header-remove-comments string)))
56       (let ((table (copy-syntax-table ietf-drums-syntax-table)))
57         (modify-syntax-entry ?\' "w" table)
58         ;; The following isn't valid, but one should be liberal
59         ;; in what one receives.
60         (modify-syntax-entry ?\: "w" table)
61         (set-syntax-table table))
62       (setq c (char-after))
63       (when (and (memq c ttoken)
64                  (not (memq c stoken)))
65         (setq type (downcase (buffer-substring
66                               (point) (progn (forward-sexp 1) (point)))))
67         ;; Do the params
68         (while (not (eobp))
69           (setq c (char-after))
70           (unless (eq c ?\;)
71             (error "Invalid header: %s" string))
72           (forward-char 1)
73           ;; If c in nil, then this is an invalid header, but
74           ;; since elm generates invalid headers on this form,
75           ;; we allow it.
76           (when (setq c (char-after))
77             (if (and (memq c ttoken)
78                      (not (memq c stoken)))
79                 (setq attribute
80                       (intern
81                        (downcase
82                         (buffer-substring
83                          (point) (progn (forward-sexp 1) (point))))))
84               (error "Invalid header: %s" string))
85             (setq c (char-after))
86             (setq encoded nil)
87             (when (eq c ?*)
88               (forward-char 1)
89               (setq c (char-after))
90               (if (not (memq c ntoken))
91                   (setq encoded t
92                         number nil)
93                 (setq number
94                       (string-to-number
95                        (buffer-substring
96                         (point) (progn (forward-sexp 1) (point)))))
97                 (setq c (char-after))
98                 (when (eq c ?*)
99                   (setq encoded t)
100                   (forward-char 1)
101                   (setq c (char-after)))))
102             ;; See if we have any previous continuations.
103             (when (and prev-attribute
104                        (not (eq prev-attribute attribute)))
105               (push (cons prev-attribute prev-value) parameters)
106               (setq prev-attribute nil
107                     prev-value ""))
108             (unless (eq c ?=)
109               (error "Invalid header: %s" string))
110             (forward-char 1)
111             (setq c (char-after))
112             (cond
113              ((eq c ?\")
114               (setq value
115                     (buffer-substring (1+ (point))
116                                       (progn (forward-sexp 1) (1- (point))))))
117              ((and (memq c ttoken)
118                    (not (memq c stoken)))
119               (setq value (buffer-substring
120                            (point) (progn (forward-sexp 1) (point)))))
121              (t
122               (error "Invalid header: %s" string)))
123             (when encoded
124               (setq value (rfc2231-decode-encoded-string value)))
125             (if number
126                 (setq prev-attribute attribute
127                       prev-value (concat prev-value value))
128               (push (cons attribute value) parameters))))
129
130         ;; Take care of any final continuations.
131         (when prev-attribute
132           (push (cons prev-attribute prev-value) parameters))
133
134         (when type
135           `(,type ,@(nreverse parameters)))))))
136
137 (defun rfc2231-decode-encoded-string (string)
138   "Decode an RFC2231-encoded string.
139 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
140   (with-temp-buffer
141     (let ((elems (split-string string "'")))
142       ;; The encoded string may contain zero to two single-quote
143       ;; marks.  This should give us the encoded word stripped
144       ;; of any preceding values.
145       (insert (car (last elems)))
146       (goto-char (point-min))
147       (while (search-forward "%" nil t)
148         (insert
149          (prog1
150              (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
151            (delete-region (1- (point)) (+ (point) 2)))))
152       ;; Encode using the charset, if any.
153       (when (and (mm-multibyte-p)
154                  (> (length elems) 1)
155                  (not (equal (intern (car elems)) 'us-ascii)))
156         (mm-decode-coding-region (point-min) (point-max)
157                                  (intern (car elems))))
158       (buffer-string))))
159
160 (defun rfc2231-encode-string (param value)
161   "Return and PARAM=VALUE string encoded according to RFC2231."
162   (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
163         (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
164         (special (ietf-drums-token-to-list "*'%\n\t"))
165         (ascii (ietf-drums-token-to-list ietf-drums-text-token))
166         (num -1)
167         spacep encodep charsetp charset broken)
168     (with-temp-buffer
169       (insert value)
170       (goto-char (point-min))
171       (while (not (eobp))
172         (cond
173          ((or (memq (following-char) control)
174               (memq (following-char) tspecial)
175               (memq (following-char) special))
176           (setq encodep t))
177          ((eq (following-char) ? )
178           (setq spacep t))
179          ((not (memq (following-char) ascii))
180           (setq charsetp t)))
181         (forward-char 1))
182       (when charsetp
183         (setq charset (mm-encode-body)))
184       (cond
185        ((or encodep charsetp)
186         (goto-char (point-min))
187         (while (not (eobp))
188           (when (> (current-column) 60)
189             (insert ";\n")
190             (setq broken t))
191           (if (or (not (memq (following-char) ascii))
192                   (memq (following-char) control)
193                   (memq (following-char) tspecial)
194                   (memq (following-char) special)
195                   (eq (following-char) ? ))
196               (progn
197                 (insert "%" (format "%02x" (following-char)))
198                 (delete-char 1))
199             (forward-char 1)))
200         (goto-char (point-min))
201         (insert (symbol-name (or charset 'us-ascii)) "''")
202         (goto-char (point-min))
203         (if (not broken)
204             (insert param "*=")
205           (while (not (eobp))
206             (insert (if (>= num 0) " " "\n ")
207                     param "*" (format "%d" (incf num)) "*=")
208             (forward-line 1))))
209        (spacep
210         (goto-char (point-min))
211         (insert param "=\"")
212         (goto-char (point-max))
213         (insert "\""))
214        (t
215         (goto-char (point-min))
216         (insert param "=")))
217       (buffer-string))))
218
219 (provide 'rfc2231)
220
221 ;;; rfc2231.el ends here