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