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