Revision: emacs@sv.gnu.org/gnus--devo--0--patch-25
[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."
51   (with-temp-buffer
52     (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
53           (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
54           (ntoken (ietf-drums-token-to-list "0-9"))
55           (prev-value "")
56           display-name mailbox c display-string parameters
57           attribute value type subtype number encoded
58           prev-attribute prev-encoded)
59       ;; Some mailer (e.g. Thunderbird 1.5) doesn't terminate each
60       ;; line with semicolon when folding a long parameter value.
61       (while (string-match "\\([^\t\n\r ;]\\)[\t ]*\r?\n[\t ]+" string)
62         (setq string (replace-match "\\1;\n " nil nil string)))
63       (ietf-drums-init (mail-header-remove-whitespace
64                         (mail-header-remove-comments string)))
65       (let ((table (copy-syntax-table ietf-drums-syntax-table)))
66         (modify-syntax-entry ?\' "w" table)
67         (modify-syntax-entry ?* " " table)
68         (modify-syntax-entry ?\; " " table)
69         (modify-syntax-entry ?= " " table)
70         ;; The following isn't valid, but one should be liberal
71         ;; in what one receives.
72         (modify-syntax-entry ?\: "w" table)
73         (set-syntax-table table))
74       (setq c (char-after))
75       (when (and (memq c ttoken)
76                  (not (memq c stoken)))
77         (setq type (downcase (buffer-substring
78                               (point) (progn (forward-sexp 1) (point)))))
79         ;; Do the params
80         (condition-case err
81             (progn
82               (while (not (eobp))
83                 (setq c (char-after))
84                 (unless (eq c ?\;)
85                   (error "Invalid header: %s" string))
86                 (forward-char 1)
87                 ;; If c in nil, then this is an invalid header, but
88                 ;; since elm generates invalid headers on this form,
89                 ;; we allow it.
90                 (when (setq c (char-after))
91                   (if (and (memq c ttoken)
92                            (not (memq c stoken)))
93                       (setq attribute
94                             (intern
95                              (downcase
96                               (buffer-substring
97                                (point) (progn (forward-sexp 1) (point))))))
98                     (error "Invalid header: %s" string))
99                   (setq c (char-after))
100                   (when (eq c ?*)
101                     (forward-char 1)
102                     (setq c (char-after))
103                     (if (not (memq c ntoken))
104                         (setq encoded t
105                               number nil)
106                       (setq number
107                             (string-to-number
108                              (buffer-substring
109                               (point) (progn (forward-sexp 1) (point)))))
110                       (setq c (char-after))
111                       (when (eq c ?*)
112                         (setq encoded t)
113                         (forward-char 1)
114                         (setq c (char-after)))))
115                   ;; See if we have any previous continuations.
116                   (when (and prev-attribute
117                              (not (eq prev-attribute attribute)))
118                     (push (cons prev-attribute
119                                 (if prev-encoded
120                                     (rfc2231-decode-encoded-string prev-value)
121                                   prev-value))
122                           parameters)
123                     (setq prev-attribute nil
124                           prev-value ""
125                           prev-encoded nil))
126                   (unless (eq c ?=)
127                     (error "Invalid header: %s" string))
128                   (forward-char 1)
129                   (setq c (char-after))
130                   (cond
131                    ((eq c ?\")
132                     (setq value (buffer-substring (1+ (point))
133                                                   (progn
134                                                     (forward-sexp 1)
135                                                     (1- (point))))))
136                    ((and (or (memq c ttoken)
137                              ;; EXTENSION: Support non-ascii chars.
138                              (> c ?\177))
139                          (not (memq c stoken)))
140                     (setq value
141                           (buffer-substring
142                            (point)
143                            (progn
144                              (forward-sexp)
145                              ;; We might not have reached at the end of
146                              ;; the value because of non-ascii chars,
147                              ;; so we should jump over them if any.
148                              (while (and (not (eobp))
149                                          (> (char-after) ?\177))
150                                (forward-char 1)
151                                (forward-sexp))
152                              (point)))))
153                    (t
154                     (error "Invalid header: %s" string)))
155                   (if number
156                       (setq prev-attribute attribute
157                             prev-value (concat prev-value value)
158                             prev-encoded encoded)
159                     (push (cons attribute
160                                 (if encoded
161                                     (rfc2231-decode-encoded-string value)
162                                   value))
163                           parameters))))
164
165               ;; Take care of any final continuations.
166               (when prev-attribute
167                 (push (cons prev-attribute
168                             (if prev-encoded
169                                 (rfc2231-decode-encoded-string prev-value)
170                               prev-value))
171                       parameters)))
172           (error
173            (setq parameters nil)
174            (if signal-error
175                (signal (car err) (cdr err))
176              ;;(message "%s" (error-message-string err))
177              )))
178
179         (when type
180           `(,type ,@(nreverse parameters)))))))
181
182 (defun rfc2231-decode-encoded-string (string)
183   "Decode an RFC2231-encoded string.
184 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
185   (with-temp-buffer
186     (let ((elems (split-string string "'")))
187       ;; The encoded string may contain zero to two single-quote
188       ;; marks.  This should give us the encoded word stripped
189       ;; of any preceding values.
190       (insert (car (last elems)))
191       (goto-char (point-min))
192       (while (search-forward "%" nil t)
193         (insert
194          (prog1
195              (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
196            (delete-region (1- (point)) (+ (point) 2)))))
197       ;; Encode using the charset, if any.
198       (when (and (mm-multibyte-p)
199                  (> (length elems) 1)
200                  (not (equal (intern (downcase (car elems))) 'us-ascii)))
201         (mm-decode-coding-region (point-min) (point-max)
202                                  (intern (downcase (car elems)))))
203       (buffer-string))))
204
205 (defun rfc2231-encode-string (param value)
206   "Return and PARAM=VALUE string encoded according to RFC2231.
207 Use `mml-insert-parameter' or `mml-insert-parameter-string' to insert
208 the result of this function."
209   (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
210         (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
211         (special (ietf-drums-token-to-list "*'%\n\t"))
212         (ascii (ietf-drums-token-to-list ietf-drums-text-token))
213         (num -1)
214         ;; Don't make lines exceeding 76 column.
215         (limit (- 74 (length param)))
216         spacep encodep charsetp charset broken)
217     (with-temp-buffer
218       (insert value)
219       (goto-char (point-min))
220       (while (not (eobp))
221         (cond
222          ((or (memq (following-char) control)
223               (memq (following-char) tspecial)
224               (memq (following-char) special))
225           (setq encodep t))
226          ((eq (following-char) ? )
227           (setq spacep t))
228          ((not (memq (following-char) ascii))
229           (setq charsetp t)))
230         (forward-char 1))
231       (when charsetp
232         (setq charset (mm-encode-body)))
233       (cond
234        ((or encodep charsetp
235             (progn
236               (end-of-line)
237               (> (current-column) (if spacep (- limit 2) limit))))
238         (setq limit (- limit 6))
239         (goto-char (point-min))
240         (insert (symbol-name (or charset 'us-ascii)) "''")
241         (while (not (eobp))
242           (if (or (not (memq (following-char) ascii))
243                   (memq (following-char) control)
244                   (memq (following-char) tspecial)
245                   (memq (following-char) special)
246                   (eq (following-char) ? ))
247               (progn
248                 (when (>= (current-column) (1- limit))
249                   (insert ";\n")
250                   (setq broken t))
251                 (insert "%" (format "%02x" (following-char)))
252                 (delete-char 1))
253             (when (> (current-column) limit)
254               (insert ";\n")
255               (setq broken t))
256             (forward-char 1)))
257         (goto-char (point-min))
258         (if (not broken)
259             (insert param "*=")
260           (while (not (eobp))
261             (insert (if (>= num 0) " " "")
262                     param "*" (format "%d" (incf num)) "*=")
263             (forward-line 1))))
264        (spacep
265         (goto-char (point-min))
266         (insert "\n " param "=\"")
267         (goto-char (point-max))
268         (insert "\""))
269        (t
270         (goto-char (point-min))
271         (insert "\n " param "=")))
272       (buffer-string))))
273
274 (provide 'rfc2231)
275
276 ;;; arch-tag: c3ab751d-d108-406a-b301-68882ad8cd63
277 ;;; rfc2231.el ends here