(rfc2231-parse-string): Remove dead code.
[gnus] / lisp / rfc2231.el
1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
2
3 ;; Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2005,
4 ;;   2006 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29 (require 'ietf-drums)
30 (require 'rfc2047)
31 (autoload 'mm-encode-body "mm-bodies")
32 (autoload 'mail-header-remove-whitespace "mail-parse")
33 (autoload 'mail-header-remove-comments "mail-parse")
34
35 (defun rfc2231-get-value (ct attribute)
36   "Return the value of ATTRIBUTE from CT."
37   (cdr (assq attribute (cdr ct))))
38
39 (defun rfc2231-parse-qp-string (string)
40   "Parse QP-encoded string using `rfc2231-parse-string'.
41 N.B.  This is in violation with RFC2047, but it seem to be in common use."
42   (rfc2231-parse-string (rfc2047-decode-string string)))
43
44 (defun rfc2231-parse-string (string &optional signal-error)
45   "Parse STRING and return a list.
46 The list will be on the form
47  `(name (attribute . value) (attribute . value)...)'.
48
49 If the optional SIGNAL-ERROR is non-nil, signal an error when this
50 function fails in parsing of parameters.  Otherwise, this function
51 must never cause a Lisp error."
52   (with-temp-buffer
53     (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
54           (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
55           (ntoken (ietf-drums-token-to-list "0-9"))
56           c type attribute encoded number prev-attribute vals
57           prev-encoded parameters value)
58       (ietf-drums-init
59        (condition-case nil
60            (mail-header-remove-whitespace
61             (mail-header-remove-comments string))
62          ;; The most likely cause of an error is unbalanced parentheses
63          ;; or double-quotes.  If all parentheses and double-quotes are
64          ;; quoted meaninglessly with backslashes, removing them might
65          ;; make it parseable.  Let's try...
66          (error
67           (let (mod)
68             (when (and (string-match "\\\\\"" string)
69                        (not (string-match "\\`\"\\|[^\\]\"" string)))
70               (setq string (mm-replace-in-string string "\\\\\"" "\"")
71                     mod t))
72             (when (and (string-match "\\\\(" string)
73                        (string-match "\\\\)" string)
74                        (not (string-match "\\`(\\|[^\\][()]" string)))
75               (setq string (mm-replace-in-string string "\\\\\\([()]\\)" "\\1")
76                     mod t))
77             (or (and mod
78                      (ignore-errors
79                        (mail-header-remove-whitespace
80                         (mail-header-remove-comments string))))
81                 ;; Finally, attempt to extract only type.
82                 (if (string-match
83                      (concat "\\`[\t\n ]*\\([^" ietf-drums-tspecials "\t\n ]+"
84                              "\\(?:/[^" ietf-drums-tspecials
85                              "\t\n ]+\\)?\\)\\(?:[\t\n ;]\\|\\'\\)")
86                      string)
87                     (match-string 1 string)
88                   ""))))))
89       (let ((table (copy-syntax-table ietf-drums-syntax-table)))
90         (modify-syntax-entry ?\' "w" table)
91         (modify-syntax-entry ?* " " table)
92         (modify-syntax-entry ?\; " " table)
93         (modify-syntax-entry ?= " " table)
94         ;; The following isn't valid, but one should be liberal
95         ;; in what one receives.
96         (modify-syntax-entry ?\: "w" table)
97         (set-syntax-table table))
98       (setq c (char-after))
99       (when (and (memq c ttoken)
100                  (not (memq c stoken))
101                  (setq type (ignore-errors
102                               (downcase
103                                (buffer-substring (point) (progn
104                                                            (forward-sexp 1)
105                                                            (point)))))))
106         ;; Do the params
107         (condition-case err
108             (progn
109               (while (not (eobp))
110                 (setq c (char-after))
111                 (unless (eq c ?\;)
112                   (error "Invalid header: %s" string))
113                 (forward-char 1)
114                 ;; If c in nil, then this is an invalid header, but
115                 ;; since elm generates invalid headers on this form,
116                 ;; we allow it.
117                 (when (setq c (char-after))
118                   (if (and (memq c ttoken)
119                            (not (memq c stoken)))
120                       (setq attribute
121                             (intern
122                              (downcase
123                               (buffer-substring
124                                (point) (progn (forward-sexp 1) (point))))))
125                     (error "Invalid header: %s" string))
126                   (setq c (char-after))
127                   (if (eq c ?*)
128                       (progn
129                         (forward-char 1)
130                         (setq c (char-after))
131                         (if (not (memq c ntoken))
132                             (setq encoded t
133                                   number nil)
134                           (setq number
135                                 (string-to-number
136                                  (buffer-substring
137                                   (point) (progn (forward-sexp 1) (point)))))
138                           (setq c (char-after))
139                           (when (eq c ?*)
140                             (setq encoded t)
141                             (forward-char 1)
142                             (setq c (char-after)))))
143                     (setq number nil
144                           encoded nil))
145                   ;; See if we have any previous continuations.
146                   (when (and prev-attribute
147                              (not (eq prev-attribute attribute)))
148                     (setq vals
149                           (mapconcat 'cdr (sort vals 'car-less-than-car) ""))
150                     (push (cons prev-attribute
151                                 (if prev-encoded
152                                     (rfc2231-decode-encoded-string vals)
153                                   vals))
154                           parameters)
155                     (setq prev-attribute nil
156                           vals nil
157                           prev-encoded nil))
158                   (unless (eq c ?=)
159                     (error "Invalid header: %s" string))
160                   (forward-char 1)
161                   (setq c (char-after))
162                   (cond
163                    ((eq c ?\")
164                     (setq value (buffer-substring (1+ (point))
165                                                   (progn
166                                                     (forward-sexp 1)
167                                                     (1- (point)))))
168                     (when encoded
169                       (setq value (mapconcat (lambda (c) (format "%%%02x" c))
170                                              value ""))))
171                    ((and (or (memq c ttoken)
172                              ;; EXTENSION: Support non-ascii chars.
173                              (> c ?\177))
174                          (not (memq c stoken)))
175                     (setq value
176                           (buffer-substring
177                            (point)
178                            (progn
179                              (forward-sexp)
180                              ;; We might not have reached at the end of
181                              ;; the value because of non-ascii chars,
182                              ;; so we should jump over them if any.
183                              (while (and (not (eobp))
184                                          (> (char-after) ?\177))
185                                (forward-char 1)
186                                (forward-sexp))
187                              (point)))))
188                    (t
189                     (error "Invalid header: %s" string)))
190                   (if number
191                       (progn
192                         (push (cons number value) vals)
193                         (setq prev-attribute attribute
194                               prev-encoded encoded))
195                     (push (cons attribute
196                                 (if encoded
197                                     (rfc2231-decode-encoded-string value)
198                                   value))
199                           parameters))))
200
201               ;; Take care of any final continuations.
202               (when prev-attribute
203                 (setq vals (mapconcat 'cdr (sort vals 'car-less-than-car) ""))
204                 (push (cons prev-attribute
205                             (if prev-encoded
206                                 (rfc2231-decode-encoded-string vals)
207                               vals))
208                       parameters)))
209           (error
210            (setq parameters nil)
211            (when signal-error
212              (signal (car err) (cdr err)))))
213
214         (cons type (nreverse parameters))))))
215
216 (defun rfc2231-decode-encoded-string (string)
217   "Decode an RFC2231-encoded string.
218 These look like:
219  \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\",
220  \"us-ascii''This%20is%20%2A%2A%2Afun%2A%2A%2A\",
221  \"'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\",
222  \"''This%20is%20%2A%2A%2Afun%2A%2A%2A\", or
223  \"This is ***fun***\"."
224   (string-match "\\`\\(?:\\([^']+\\)?'\\([^']+\\)?'\\)?\\(.+\\)" string)
225   (let ((coding-system (mm-charset-to-coding-system (match-string 1 string)))
226         ;;(language (match-string 2 string))
227         (value (match-string 3 string)))
228     (mm-with-unibyte-buffer
229       (insert value)
230       (goto-char (point-min))
231       (while (search-forward "%" nil t)
232         (insert
233          (prog1
234              (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
235            (delete-region (1- (point)) (+ (point) 2)))))
236       ;; Decode using the charset, if any.
237       (if (memq coding-system '(nil ascii))
238           (buffer-string)
239         (mm-decode-coding-string (buffer-string) coding-system)))))
240
241 (defun rfc2231-encode-string (param value)
242   "Return and PARAM=VALUE string encoded according to RFC2231.
243 Use `mml-insert-parameter' or `mml-insert-parameter-string' to insert
244 the result of this function."
245   (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
246         (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
247         (special (ietf-drums-token-to-list "*'%\n\t"))
248         (ascii (ietf-drums-token-to-list ietf-drums-text-token))
249         (num -1)
250         ;; Don't make lines exceeding 76 column.
251         (limit (- 74 (length param)))
252         spacep encodep charsetp charset broken)
253     (mm-with-multibyte-buffer
254       (insert value)
255       (goto-char (point-min))
256       (while (not (eobp))
257         (cond
258          ((or (memq (following-char) control)
259               (memq (following-char) tspecial)
260               (memq (following-char) special))
261           (setq encodep t))
262          ((eq (following-char) ? )
263           (setq spacep t))
264          ((not (memq (following-char) ascii))
265           (setq charsetp t)))
266         (forward-char 1))
267       (when charsetp
268         (setq charset (mm-encode-body)))
269       (mm-disable-multibyte)
270       (cond
271        ((or encodep charsetp
272             (progn
273               (end-of-line)
274               (> (current-column) (if spacep (- limit 2) limit))))
275         (setq limit (- limit 6))
276         (goto-char (point-min))
277         (insert (symbol-name (or charset 'us-ascii)) "''")
278         (while (not (eobp))
279           (if (or (not (memq (following-char) ascii))
280                   (memq (following-char) control)
281                   (memq (following-char) tspecial)
282                   (memq (following-char) special)
283                   (eq (following-char) ? ))
284               (progn
285                 (when (>= (current-column) (1- limit))
286                   (insert ";\n")
287                   (setq broken t))
288                 (insert "%" (format "%02x" (following-char)))
289                 (delete-char 1))
290             (when (> (current-column) limit)
291               (insert ";\n")
292               (setq broken t))
293             (forward-char 1)))
294         (goto-char (point-min))
295         (if (not broken)
296             (insert param "*=")
297           (while (not (eobp))
298             (insert (if (>= num 0) " " "")
299                     param "*" (format "%d" (incf num)) "*=")
300             (forward-line 1))))
301        (spacep
302         (goto-char (point-min))
303         (insert param "=\"")
304         (goto-char (point-max))
305         (insert "\""))
306        (t
307         (goto-char (point-min))
308         (insert param "=")))
309       (buffer-string))))
310
311 (provide 'rfc2231)
312
313 ;;; arch-tag: c3ab751d-d108-406a-b301-68882ad8cd63
314 ;;; rfc2231.el ends here