8e0c3412048c7d6ca54ef319cc3a796e0cdb1266
[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 (eval-when-compile (require 'cl))
27 (require 'ietf-drums)
28
29 (defun rfc2231-get-value (ct attribute)
30   "Return the value of ATTRIBUTE from CT."
31   (cdr (assq attribute (cdr ct))))
32
33 (defun rfc2231-parse-string (string)
34   "Parse STRING and return a list.
35 The list will be on the form
36  `(name (attribute . value) (attribute . value)...)"
37   (with-temp-buffer
38     (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
39           (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
40           (ntoken (ietf-drums-token-to-list "0-9"))
41           (prev-value "")
42           display-name mailbox c display-string parameters
43           attribute value type subtype number encoded
44           prev-attribute)
45       (ietf-drums-init (mail-header-remove-whitespace
46                         (mail-header-remove-comments string)))
47       (let ((table (copy-syntax-table ietf-drums-syntax-table)))
48         (modify-syntax-entry ?\' "w" table)
49         ;; The following isn't valid, but one should be liberal
50         ;; in what one receives.
51         (modify-syntax-entry ?\: "w" table)
52         (set-syntax-table table))
53       (setq c (char-after))
54       (when (and (memq c ttoken)
55                  (not (memq c stoken)))
56         (setq type (downcase (buffer-substring
57                               (point) (progn (forward-sexp 1) (point)))))
58         ;; Do the params
59         (while (not (eobp))
60           (setq c (char-after))
61           (unless (eq c ?\;)
62             (error "Invalid header: %s" string))
63           (forward-char 1)
64           ;; If c in nil, then this is an invalid header, but
65           ;; since elm generates invalid headers on this form,
66           ;; we allow it.
67           (when (setq c (char-after))
68             (if (and (memq c ttoken)
69                      (not (memq c stoken)))
70                 (setq attribute
71                       (intern
72                        (downcase
73                         (buffer-substring
74                          (point) (progn (forward-sexp 1) (point))))))
75               (error "Invalid header: %s" string))
76             (setq c (char-after))
77             (setq encoded nil)
78             (when (eq c ?*)
79               (forward-char 1)
80               (setq c (char-after))
81               (if (not (memq c ntoken))
82                   (setq encoded t
83                         number nil)
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 (symbol-name 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