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