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