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