Mercurial > emacs
annotate lisp/gnus/rfc2231.el @ 63360:527b5bc891fb
*** empty log message ***
author | Carsten Dominik <dominik@science.uva.nl> |
---|---|
date | Mon, 13 Jun 2005 06:42:41 +0000 |
parents | 22da0004ae3c |
children | 18a818a2ee7c 95879cc1ed20 |
rev | line source |
---|---|
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers |
31717 | 2 |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
3 ;; Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004 |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
4 ;; Free Software Foundation, Inc. |
31717 | 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., 59 Temple Place - Suite 330, | |
22 ;; Boston, MA 02111-1307, USA. | |
23 | |
24 ;;; Commentary: | |
25 | |
26 ;;; Code: | |
27 | |
33121
228696a7231c
2000-11-01 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31717
diff
changeset
|
28 (eval-when-compile (require 'cl)) |
31717 | 29 (require 'ietf-drums) |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
30 (require 'rfc2047) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
31 (autoload 'mm-encode-body "mm-bodies") |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
32 (autoload 'mail-header-remove-whitespace "mail-parse") |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
33 (autoload 'mail-header-remove-comments "mail-parse") |
31717 | 34 |
35 (defun rfc2231-get-value (ct attribute) | |
36 "Return the value of ATTRIBUTE from CT." | |
37 (cdr (assq attribute (cdr ct)))) | |
38 | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
39 (defun rfc2231-parse-qp-string (string) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
40 "Parse QP-encoded string using `rfc2231-parse-string'. |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
41 N.B. This is in violation with RFC2047, but it seem to be in common use." |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
42 (rfc2231-parse-string (rfc2047-decode-string string))) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
43 |
31717 | 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) | |
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) | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
60 (modify-syntax-entry ?* " " table) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
61 (modify-syntax-entry ?\; " " table) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
62 (modify-syntax-entry ?= " " table) |
31717 | 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)) | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
94 (if (not (memq c ntoken)) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
95 (setq encoded t |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
96 number nil) |
31717 | 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 prev-value) parameters) | |
110 (setq prev-attribute nil | |
111 prev-value "")) | |
112 (unless (eq c ?=) | |
113 (error "Invalid header: %s" string)) | |
114 (forward-char 1) | |
115 (setq c (char-after)) | |
116 (cond | |
117 ((eq c ?\") | |
118 (setq value | |
119 (buffer-substring (1+ (point)) | |
120 (progn (forward-sexp 1) (1- (point)))))) | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
121 ((and (or (memq c ttoken) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
122 (> c ?\177)) ;; EXTENSION: Support non-ascii chars. |
31717 | 123 (not (memq c stoken))) |
124 (setq value (buffer-substring | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
125 (point) (progn (forward-sexp) (point))))) |
31717 | 126 (t |
127 (error "Invalid header: %s" string))) | |
128 (if number | |
129 (setq prev-attribute attribute | |
130 prev-value (concat prev-value value)) | |
59089
22da0004ae3c
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-750
Miles Bader <miles@gnu.org>
parents:
56927
diff
changeset
|
131 (push (cons attribute |
22da0004ae3c
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-750
Miles Bader <miles@gnu.org>
parents:
56927
diff
changeset
|
132 (if encoded |
22da0004ae3c
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-750
Miles Bader <miles@gnu.org>
parents:
56927
diff
changeset
|
133 (rfc2231-decode-encoded-string value) |
22da0004ae3c
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-750
Miles Bader <miles@gnu.org>
parents:
56927
diff
changeset
|
134 value)) |
22da0004ae3c
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-750
Miles Bader <miles@gnu.org>
parents:
56927
diff
changeset
|
135 parameters)))) |
31717 | 136 |
137 ;; Take care of any final continuations. | |
138 (when prev-attribute | |
59089
22da0004ae3c
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-750
Miles Bader <miles@gnu.org>
parents:
56927
diff
changeset
|
139 (push (cons prev-attribute |
22da0004ae3c
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-750
Miles Bader <miles@gnu.org>
parents:
56927
diff
changeset
|
140 (if encoded |
22da0004ae3c
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-750
Miles Bader <miles@gnu.org>
parents:
56927
diff
changeset
|
141 (rfc2231-decode-encoded-string prev-value) |
22da0004ae3c
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-750
Miles Bader <miles@gnu.org>
parents:
56927
diff
changeset
|
142 prev-value)) |
22da0004ae3c
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-750
Miles Bader <miles@gnu.org>
parents:
56927
diff
changeset
|
143 parameters)) |
31717 | 144 |
145 (when type | |
146 `(,type ,@(nreverse parameters))))))) | |
147 | |
148 (defun rfc2231-decode-encoded-string (string) | |
149 "Decode an RFC2231-encoded string. | |
150 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"." | |
151 (with-temp-buffer | |
152 (let ((elems (split-string string "'"))) | |
153 ;; The encoded string may contain zero to two single-quote | |
154 ;; marks. This should give us the encoded word stripped | |
155 ;; of any preceding values. | |
156 (insert (car (last elems))) | |
157 (goto-char (point-min)) | |
158 (while (search-forward "%" nil t) | |
159 (insert | |
160 (prog1 | |
161 (string-to-number (buffer-substring (point) (+ (point) 2)) 16) | |
162 (delete-region (1- (point)) (+ (point) 2))))) | |
163 ;; Encode using the charset, if any. | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
164 (when (and (mm-multibyte-p) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
165 (> (length elems) 1) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
166 (not (equal (intern (downcase (car elems))) 'us-ascii))) |
31717 | 167 (mm-decode-coding-region (point-min) (point-max) |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
168 (intern (downcase (car elems))))) |
31717 | 169 (buffer-string)))) |
170 | |
171 (defun rfc2231-encode-string (param value) | |
172 "Return and PARAM=VALUE string encoded according to RFC2231." | |
173 (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token)) | |
174 (tspecial (ietf-drums-token-to-list ietf-drums-tspecials)) | |
175 (special (ietf-drums-token-to-list "*'%\n\t")) | |
176 (ascii (ietf-drums-token-to-list ietf-drums-text-token)) | |
177 (num -1) | |
178 spacep encodep charsetp charset broken) | |
179 (with-temp-buffer | |
180 (insert value) | |
181 (goto-char (point-min)) | |
182 (while (not (eobp)) | |
183 (cond | |
184 ((or (memq (following-char) control) | |
185 (memq (following-char) tspecial) | |
186 (memq (following-char) special)) | |
187 (setq encodep t)) | |
188 ((eq (following-char) ? ) | |
189 (setq spacep t)) | |
190 ((not (memq (following-char) ascii)) | |
191 (setq charsetp t))) | |
192 (forward-char 1)) | |
193 (when charsetp | |
194 (setq charset (mm-encode-body))) | |
195 (cond | |
196 ((or encodep charsetp) | |
197 (goto-char (point-min)) | |
198 (while (not (eobp)) | |
199 (when (> (current-column) 60) | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
200 (insert ";\n") |
31717 | 201 (setq broken t)) |
202 (if (or (not (memq (following-char) ascii)) | |
203 (memq (following-char) control) | |
204 (memq (following-char) tspecial) | |
205 (memq (following-char) special) | |
206 (eq (following-char) ? )) | |
207 (progn | |
208 (insert "%" (format "%02x" (following-char))) | |
209 (delete-char 1)) | |
210 (forward-char 1))) | |
211 (goto-char (point-min)) | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
212 (insert (symbol-name (or charset 'us-ascii)) "''") |
31717 | 213 (goto-char (point-min)) |
214 (if (not broken) | |
215 (insert param "*=") | |
216 (while (not (eobp)) | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
217 (insert (if (>= num 0) " " "\n ") |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
218 param "*" (format "%d" (incf num)) "*=") |
31717 | 219 (forward-line 1)))) |
220 (spacep | |
221 (goto-char (point-min)) | |
222 (insert param "=\"") | |
223 (goto-char (point-max)) | |
224 (insert "\"")) | |
225 (t | |
226 (goto-char (point-min)) | |
227 (insert param "="))) | |
228 (buffer-string)))) | |
229 | |
230 (provide 'rfc2231) | |
231 | |
52401 | 232 ;;; arch-tag: c3ab751d-d108-406a-b301-68882ad8cd63 |
31717 | 233 ;;; rfc2231.el ends here |