1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
2 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
28 (defun rfc2231-get-value (ct attribute)
29 "Return the value of ATTRIBUTE from CT."
30 (cdr (assq attribute (cdr ct))))
32 (defun rfc2231-parse-string (string)
33 "Parse STRING and return a list.
34 The list will be on the form
35 `(name (attribute . value) (attribute . value)...)"
37 (let ((ttoken (drums-token-to-list drums-text-token))
38 (stoken (drums-token-to-list drums-tspecials))
39 (ntoken (drums-token-to-list "0-9"))
41 display-name mailbox c display-string parameters
42 attribute value type subtype number encoded
44 (drums-init (mail-header-remove-whitespace
45 (mail-header-remove-comments string)))
46 (let ((table (copy-syntax-table drums-syntax-table)))
47 (modify-syntax-entry ?\' "w" table)
48 (set-syntax-table table))
49 (setq c (following-char))
50 (when (and (memq c ttoken)
51 (not (memq c stoken)))
52 (setq type (downcase (buffer-substring
53 (point) (progn (forward-sexp 1) (point)))))
56 (setq c (following-char))
58 (error "Invalid header: %s" string))
60 (setq c (following-char))
61 (if (and (memq c ttoken)
62 (not (memq c stoken)))
67 (point) (progn (forward-sexp 1) (point))))))
68 (error "Invalid header: %s" string))
69 (setq c (following-char))
73 (setq c (following-char))
78 (point) (progn (forward-sexp 1) (point)))))
79 (setq c (following-char))
83 (setq c (following-char)))))
84 ;; See if we have any previous continuations.
85 (when (and prev-attribute
86 (not (eq prev-attribute attribute)))
87 (push (cons prev-attribute prev-value) parameters)
88 (setq prev-attribute nil
91 (error "Invalid header: %s" string))
93 (setq c (following-char))
97 (buffer-substring (1+ (point))
98 (progn (forward-sexp 1) (1- (point))))))
100 (not (memq c stoken)))
101 (setq value (buffer-substring
102 (point) (progn (forward-sexp 1) (point)))))
104 (error "Invalid header: %s" string)))
106 (setq value (rfc2231-decode-encoded-string value)))
108 (setq prev-attribute attribute
109 prev-value (concat prev-value value))
110 (push (cons attribute value) parameters)))
112 ;; Take care of any final continuations.
114 (push (cons prev-attribute prev-value) parameters))
116 `(,type ,@(nreverse parameters))))))
118 (defun rfc2231-decode-encoded-string (string)
119 "Decode an RFC2231-encoded string.
120 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
122 (let ((elems (split-string string "'")))
123 ;; The encoded string may contain zero to two single-quote
124 ;; marks. This should give us the encoded word stripped
125 ;; of any preceding values.
126 (insert (or (car (last elems))
128 (goto-char (point-min))
129 (while (search-forward "%" nil t)
132 (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
133 (delete-region (1- (point)) (+ (point) 2)))))
134 ;; Encode using the charset, if any.
136 (not (equal (car elems) 'us-ascii)))
137 (mm-decode-coding-region (point-min) (point-max) (car elems)))
142 ;;; rfc2231.el ends here