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