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