Update copyright notices for 2013
[gnus.git] / lisp / rfc2231.el
blobe8d7075bc65a0d4691546a30e03d6067fefa204b
1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
3 ;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
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 3 of the License, or
11 ;; (at your option) any later version.
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.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;;; Code:
25 (eval-when-compile (require 'cl))
26 (require 'ietf-drums)
27 (require 'rfc2047)
28 (autoload 'mm-encode-body "mm-bodies")
29 (autoload 'mail-header-remove-whitespace "mail-parse")
30 (autoload 'mail-header-remove-comments "mail-parse")
32 (defun rfc2231-get-value (ct attribute)
33 "Return the value of ATTRIBUTE from CT."
34 (cdr (assq attribute (cdr ct))))
36 (defun rfc2231-parse-qp-string (string)
37 "Parse QP-encoded string using `rfc2231-parse-string'.
38 N.B. This is in violation with RFC2047, but it seem to be in common use."
39 (rfc2231-parse-string (rfc2047-decode-string string)))
41 (defun rfc2231-parse-string (string &optional signal-error)
42 "Parse STRING and return a list.
43 The list will be on the form
44 `(name (attribute . value) (attribute . value)...)'.
46 If the optional SIGNAL-ERROR is non-nil, signal an error when this
47 function fails in parsing of parameters. Otherwise, this function
48 must never cause a Lisp error."
49 (with-temp-buffer
50 (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
51 (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
52 (ntoken (ietf-drums-token-to-list "0-9"))
53 c type attribute encoded number parameters value)
54 (ietf-drums-init
55 (condition-case nil
56 (mail-header-remove-whitespace
57 (mail-header-remove-comments string))
58 ;; The most likely cause of an error is unbalanced parentheses
59 ;; or double-quotes. If all parentheses and double-quotes are
60 ;; quoted meaninglessly with backslashes, removing them might
61 ;; make it parsable. Let's try...
62 (error
63 (let (mod)
64 (when (and (string-match "\\\\\"" string)
65 (not (string-match "\\`\"\\|[^\\]\"" string)))
66 (setq string (mm-replace-in-string string "\\\\\"" "\"")
67 mod t))
68 (when (and (string-match "\\\\(" string)
69 (string-match "\\\\)" string)
70 (not (string-match "\\`(\\|[^\\][()]" string)))
71 (setq string (mm-replace-in-string string "\\\\\\([()]\\)" "\\1")
72 mod t))
73 (or (and mod
74 (ignore-errors
75 (mail-header-remove-whitespace
76 (mail-header-remove-comments string))))
77 ;; Finally, attempt to extract only type.
78 (if (string-match
79 (concat "\\`[\t\n ]*\\([^" ietf-drums-tspecials "\t\n ]+"
80 "\\(?:/[^" ietf-drums-tspecials
81 "\t\n ]+\\)?\\)\\(?:[\t\n ;]\\|\\'\\)")
82 string)
83 (match-string 1 string)
84 ""))))))
85 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
86 (modify-syntax-entry ?\' "w" table)
87 (modify-syntax-entry ?* " " table)
88 (modify-syntax-entry ?\; " " table)
89 (modify-syntax-entry ?= " " table)
90 ;; The following isn't valid, but one should be liberal
91 ;; in what one receives.
92 (modify-syntax-entry ?\: "w" table)
93 (set-syntax-table table))
94 (setq c (char-after))
95 (when (and (memq c ttoken)
96 (not (memq c stoken))
97 (setq type (ignore-errors
98 (downcase
99 (buffer-substring (point) (progn
100 (forward-sexp 1)
101 (point)))))))
102 ;; Do the params
103 (condition-case err
104 (progn
105 (while (not (eobp))
106 (setq c (char-after))
107 (unless (eq c ?\;)
108 (error "Invalid header: %s" string))
109 (forward-char 1)
110 ;; If c in nil, then this is an invalid header, but
111 ;; since elm generates invalid headers on this form,
112 ;; we allow it.
113 (when (setq c (char-after))
114 (if (and (memq c ttoken)
115 (not (memq c stoken)))
116 (setq attribute
117 (intern
118 (downcase
119 (buffer-substring
120 (point) (progn (forward-sexp 1) (point))))))
121 (error "Invalid header: %s" string))
122 (setq c (char-after))
123 (if (eq c ?*)
124 (progn
125 (forward-char 1)
126 (setq c (char-after))
127 (if (not (memq c ntoken))
128 (setq encoded t
129 number nil)
130 (setq number
131 (string-to-number
132 (buffer-substring
133 (point) (progn (forward-sexp 1) (point)))))
134 (setq c (char-after))
135 (when (eq c ?*)
136 (setq encoded t)
137 (forward-char 1)
138 (setq c (char-after)))))
139 (setq number nil
140 encoded nil))
141 (unless (eq c ?=)
142 (error "Invalid header: %s" string))
143 (forward-char 1)
144 (setq c (char-after))
145 (cond
146 ((eq c ?\")
147 (setq value (buffer-substring (1+ (point))
148 (progn
149 (forward-sexp 1)
150 (1- (point)))))
151 (when encoded
152 (setq value (mapconcat (lambda (c) (format "%%%02x" c))
153 value ""))))
154 ((and (or (memq c ttoken)
155 ;; EXTENSION: Support non-ascii chars.
156 (> c ?\177))
157 (not (memq c stoken)))
158 (setq value
159 (buffer-substring
160 (point)
161 (progn
162 ;; Jump over asterisk, non-ASCII
163 ;; and non-boundary characters.
164 (while (and c
165 (or (eq c ?*)
166 (> c ?\177)
167 (not (eq (char-syntax c) ? ))))
168 (forward-char 1)
169 (setq c (char-after)))
170 (point)))))
172 (error "Invalid header: %s" string)))
173 (push (list attribute value number encoded)
174 parameters))))
175 (error
176 (setq parameters nil)
177 (when signal-error
178 (signal (car err) (cdr err)))))
180 ;; Now collect and concatenate continuation parameters.
181 (let ((cparams nil)
182 elem)
183 (loop for (attribute value part encoded)
184 in (sort parameters (lambda (e1 e2)
185 (< (or (caddr e1) 0)
186 (or (caddr e2) 0))))
187 do (cond
188 ;; First part.
189 ((or (not (setq elem (assq attribute cparams)))
190 (and (numberp part)
191 (zerop part)))
192 (push (list attribute value encoded) cparams))
193 ;; Repetition of a part; do nothing.
194 ((and elem
195 (null number))
197 ;; Concatenate continuation parts.
199 (setcar (cdr elem) (concat (cadr elem) value)))))
200 ;; Finally decode encoded values.
201 (cons type (mapcar
202 (lambda (elem)
203 (cons (car elem)
204 (if (nth 2 elem)
205 (rfc2231-decode-encoded-string (nth 1 elem))
206 (nth 1 elem))))
207 (nreverse cparams))))))))
209 (defun rfc2231-decode-encoded-string (string)
210 "Decode an RFC2231-encoded string.
211 These look like:
212 \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\",
213 \"us-ascii''This%20is%20%2A%2A%2Afun%2A%2A%2A\",
214 \"'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\",
215 \"''This%20is%20%2A%2A%2Afun%2A%2A%2A\", or
216 \"This is ***fun***\"."
217 (string-match "\\`\\(?:\\([^']+\\)?'\\([^']+\\)?'\\)?\\(.+\\)" string)
218 (let ((coding-system (mm-charset-to-coding-system (match-string 1 string)))
219 ;;(language (match-string 2 string))
220 (value (match-string 3 string)))
221 (mm-with-unibyte-buffer
222 (insert value)
223 (goto-char (point-min))
224 (while (re-search-forward "%\\([0-9A-Fa-f][0-9A-Fa-f]\\)" nil t)
225 (insert
226 (prog1
227 (string-to-number (match-string 1) 16)
228 (delete-region (match-beginning 0) (match-end 0)))))
229 ;; Decode using the charset, if any.
230 (if (memq coding-system '(nil ascii))
231 (buffer-string)
232 (mm-decode-coding-string (buffer-string) coding-system)))))
234 (defun rfc2231-encode-string (param value)
235 "Return and PARAM=VALUE string encoded according to RFC2231.
236 Use `mml-insert-parameter' or `mml-insert-parameter-string' to insert
237 the result of this function."
238 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
239 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
240 (special (ietf-drums-token-to-list "*'%\n\t"))
241 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
242 (num -1)
243 ;; Don't make lines exceeding 76 column.
244 (limit (- 74 (length param)))
245 spacep encodep charsetp charset broken)
246 (mm-with-multibyte-buffer
247 (insert value)
248 (goto-char (point-min))
249 (while (not (eobp))
250 (cond
251 ((or (memq (following-char) control)
252 (memq (following-char) tspecial)
253 (memq (following-char) special))
254 (setq encodep t))
255 ((eq (following-char) ? )
256 (setq spacep t))
257 ((not (memq (following-char) ascii))
258 (setq charsetp t)))
259 (forward-char 1))
260 (when charsetp
261 (setq charset (mm-encode-body)))
262 (mm-disable-multibyte)
263 (cond
264 ((or encodep charsetp
265 (progn
266 (end-of-line)
267 (> (current-column) (if spacep (- limit 2) limit))))
268 (setq limit (- limit 6))
269 (goto-char (point-min))
270 (insert (symbol-name (or charset 'us-ascii)) "''")
271 (while (not (eobp))
272 (if (or (not (memq (following-char) ascii))
273 (memq (following-char) control)
274 (memq (following-char) tspecial)
275 (memq (following-char) special)
276 (eq (following-char) ? ))
277 (progn
278 (when (>= (current-column) (1- limit))
279 (insert ";\n")
280 (setq broken t))
281 (insert "%" (format "%02x" (following-char)))
282 (delete-char 1))
283 (when (> (current-column) limit)
284 (insert ";\n")
285 (setq broken t))
286 (forward-char 1)))
287 (goto-char (point-min))
288 (if (not broken)
289 (insert param "*=")
290 (while (not (eobp))
291 (insert (if (>= num 0) " " "")
292 param "*" (format "%d" (incf num)) "*=")
293 (forward-line 1))))
294 (spacep
295 (goto-char (point-min))
296 (insert param "=\"")
297 (goto-char (point-max))
298 (insert "\""))
300 (goto-char (point-min))
301 (insert param "=")))
302 (buffer-string))))
304 (provide 'rfc2231)
306 ;;; rfc2231.el ends here