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