(info-xref-xfile-alist, info-xref-filename-heading, info-xref-good,
[emacs.git] / lisp / gnus / rfc2231.el
blobf328f2dbc6a3251bed6af04763523e7657df2ff4
1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
3 ;; Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004,
4 ;; 2005 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 (ietf-drums-init (mail-header-remove-whitespace
57 (mail-header-remove-comments string)))
58 (let ((table (copy-syntax-table ietf-drums-syntax-table)))
59 (modify-syntax-entry ?\' "w" table)
60 (modify-syntax-entry ?* " " table)
61 (modify-syntax-entry ?\; " " table)
62 (modify-syntax-entry ?= " " table)
63 ;; The following isn't valid, but one should be liberal
64 ;; in what one receives.
65 (modify-syntax-entry ?\: "w" table)
66 (set-syntax-table table))
67 (setq c (char-after))
68 (when (and (memq c ttoken)
69 (not (memq c stoken)))
70 (setq type (downcase (buffer-substring
71 (point) (progn (forward-sexp 1) (point)))))
72 ;; Do the params
73 (while (not (eobp))
74 (setq c (char-after))
75 (unless (eq c ?\;)
76 (error "Invalid header: %s" string))
77 (forward-char 1)
78 ;; If c in nil, then this is an invalid header, but
79 ;; since elm generates invalid headers on this form,
80 ;; we allow it.
81 (when (setq c (char-after))
82 (if (and (memq c ttoken)
83 (not (memq c stoken)))
84 (setq attribute
85 (intern
86 (downcase
87 (buffer-substring
88 (point) (progn (forward-sexp 1) (point))))))
89 (error "Invalid header: %s" string))
90 (setq c (char-after))
91 (when (eq c ?*)
92 (forward-char 1)
93 (setq c (char-after))
94 (if (not (memq c ntoken))
95 (setq encoded t
96 number nil)
97 (setq number
98 (string-to-number
99 (buffer-substring
100 (point) (progn (forward-sexp 1) (point)))))
101 (setq c (char-after))
102 (when (eq c ?*)
103 (setq encoded t)
104 (forward-char 1)
105 (setq c (char-after)))))
106 ;; See if we have any previous continuations.
107 (when (and prev-attribute
108 (not (eq prev-attribute attribute)))
109 (push (cons prev-attribute
110 (if prev-encoded
111 (rfc2231-decode-encoded-string prev-value)
112 prev-value))
113 parameters)
114 (setq prev-attribute nil
115 prev-value ""
116 prev-encoded nil))
117 (unless (eq c ?=)
118 (error "Invalid header: %s" string))
119 (forward-char 1)
120 (setq c (char-after))
121 (cond
122 ((eq c ?\")
123 (setq value
124 (buffer-substring (1+ (point))
125 (progn (forward-sexp 1) (1- (point))))))
126 ((and (or (memq c ttoken)
127 (> c ?\177)) ;; EXTENSION: Support non-ascii chars.
128 (not (memq c stoken)))
129 (setq value (buffer-substring
130 (point) (progn (forward-sexp) (point)))))
132 (error "Invalid header: %s" string)))
133 (if number
134 (setq prev-attribute attribute
135 prev-value (concat prev-value value)
136 prev-encoded encoded)
137 (push (cons attribute
138 (if encoded
139 (rfc2231-decode-encoded-string value)
140 value))
141 parameters))))
143 ;; Take care of any final continuations.
144 (when prev-attribute
145 (push (cons prev-attribute
146 (if prev-encoded
147 (rfc2231-decode-encoded-string prev-value)
148 prev-value))
149 parameters))
151 (when type
152 `(,type ,@(nreverse parameters)))))))
154 (defun rfc2231-decode-encoded-string (string)
155 "Decode an RFC2231-encoded string.
156 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
157 (with-temp-buffer
158 (let ((elems (split-string string "'")))
159 ;; The encoded string may contain zero to two single-quote
160 ;; marks. This should give us the encoded word stripped
161 ;; of any preceding values.
162 (insert (car (last elems)))
163 (goto-char (point-min))
164 (while (search-forward "%" nil t)
165 (insert
166 (prog1
167 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
168 (delete-region (1- (point)) (+ (point) 2)))))
169 ;; Encode using the charset, if any.
170 (when (and (mm-multibyte-p)
171 (> (length elems) 1)
172 (not (equal (intern (downcase (car elems))) 'us-ascii)))
173 (mm-decode-coding-region (point-min) (point-max)
174 (intern (downcase (car elems)))))
175 (buffer-string))))
177 (defun rfc2231-encode-string (param value)
178 "Return and PARAM=VALUE string encoded according to RFC2231."
179 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
180 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
181 (special (ietf-drums-token-to-list "*'%\n\t"))
182 (ascii (ietf-drums-token-to-list ietf-drums-text-token))
183 (num -1)
184 spacep encodep charsetp charset broken)
185 (with-temp-buffer
186 (insert value)
187 (goto-char (point-min))
188 (while (not (eobp))
189 (cond
190 ((or (memq (following-char) control)
191 (memq (following-char) tspecial)
192 (memq (following-char) special))
193 (setq encodep t))
194 ((eq (following-char) ? )
195 (setq spacep t))
196 ((not (memq (following-char) ascii))
197 (setq charsetp t)))
198 (forward-char 1))
199 (when charsetp
200 (setq charset (mm-encode-body)))
201 (cond
202 ((or encodep charsetp)
203 (goto-char (point-min))
204 (while (not (eobp))
205 (when (> (current-column) 60)
206 (insert ";\n")
207 (setq broken t))
208 (if (or (not (memq (following-char) ascii))
209 (memq (following-char) control)
210 (memq (following-char) tspecial)
211 (memq (following-char) special)
212 (eq (following-char) ? ))
213 (progn
214 (insert "%" (format "%02x" (following-char)))
215 (delete-char 1))
216 (forward-char 1)))
217 (goto-char (point-min))
218 (insert (symbol-name (or charset 'us-ascii)) "''")
219 (goto-char (point-min))
220 (if (not broken)
221 (insert param "*=")
222 (while (not (eobp))
223 (insert (if (>= num 0) " " "\n ")
224 param "*" (format "%d" (incf num)) "*=")
225 (forward-line 1))))
226 (spacep
227 (goto-char (point-min))
228 (insert param "=\"")
229 (goto-char (point-max))
230 (insert "\""))
232 (goto-char (point-min))
233 (insert param "=")))
234 (buffer-string))))
236 (provide 'rfc2231)
238 ;;; arch-tag: c3ab751d-d108-406a-b301-68882ad8cd63
239 ;;; rfc2231.el ends here