* gnus-msg.el (gnus-configure-posting-styles): Allow nil values to
[gnus] / lisp / rfc2231.el
1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
2 ;; Copyright (C) 1998,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; This file is part of GNU Emacs.
6
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)
10 ;; any later version.
11
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.
16
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.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (require 'ietf-drums)
27
28 (defun rfc2231-get-value (ct attribute)
29   "Return the value of ATTRIBUTE from CT."
30   (cdr (assq attribute (cdr ct))))
31
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)...)"
36   (with-temp-buffer
37     (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
38           (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
39           (ntoken (ietf-drums-token-to-list "0-9"))
40           (prev-value "")
41           display-name mailbox c display-string parameters
42           attribute value type subtype number encoded
43           prev-attribute)
44       (ietf-drums-init (mail-header-remove-whitespace
45                         (mail-header-remove-comments string)))
46       (let ((table (copy-syntax-table ietf-drums-syntax-table)))
47         (modify-syntax-entry ?\' "w" table)
48         ;; The following isn't valid, but one should be liberal
49         ;; in what one receives.
50         (modify-syntax-entry ?\: "w" table)
51         (set-syntax-table table))
52       (setq c (char-after))
53       (when (and (memq c ttoken)
54                  (not (memq c stoken)))
55         (setq type (downcase (buffer-substring
56                               (point) (progn (forward-sexp 1) (point)))))
57         ;; Do the params
58         (while (not (eobp))
59           (setq c (char-after))
60           (unless (eq c ?\;)
61             (error "Invalid header: %s" string))
62           (forward-char 1)
63           ;; If c in nil, then this is an invalid header, but
64           ;; since elm generates invalid headers on this form,
65           ;; we allow it.
66           (when (setq c (char-after))
67             (if (and (memq c ttoken)
68                      (not (memq c stoken)))
69                 (setq attribute
70                       (intern
71                        (downcase
72                         (buffer-substring
73                          (point) (progn (forward-sexp 1) (point))))))
74               (error "Invalid header: %s" string))
75             (setq c (char-after))
76             (setq encoded nil)
77             (when (eq c ?*)
78               (forward-char 1)
79               (setq c (char-after))
80               (when (memq c ntoken)
81                 (setq number