*** empty log message ***
[gnus] / lisp / rfc2231.el
1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
2 ;; Copyright (C) 1998 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         (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)))))
54         ;; Do the params
55         (while (not (eobp))
56           (setq c (following-char))
57           (unless (eq c ?\;)
58             (error "Invalid header: %s" string))
59           (forward-char 1)
60           (setq c (following-char))
61           (if (and (memq c ttoken)
62                    (not (memq c stoken)))
63               (setq attribute
64                     (intern
65                      (downcase
66                       (buffer-substring
67                        (point) (progn (forward-sexp 1) (point))))))
68             (error "Invalid header: %s" string))
69           (setq c (following-char))
70           (setq encoded nil)
71           (when (eq c ?*)
72             (forward-char 1)
73             (setq c (following-char))
74             (when (memq c ntoken)
75               (setq number
76                     (string-to-number
77                      (buffer-substring
78                       (point) (progn (forward-sexp 1) (point)))))
79               (setq c (following-char))
80               (when (eq c ?*)
81                 (setq encoded t)
82                 (forward-char 1)
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
89                   prev-value ""))
90           (unless (eq c ?=)
91             (error "Invalid header: %s" string))
92           (forward-char 1)
93           (setq c (following-char))
94           (cond
95            ((eq c ?\")
96             (setq value
97                   (buffer-substring (1+ (point))
98                                     (progn (forward-sexp 1) (1- (point))))))
99            ((and (memq c ttoken)
100                  (not (memq c stoken)))
101             (setq value (buffer-substring
102                          (point) (progn (forward-sexp 1) (point)))))
103            (t
104             (error "Invalid header: %s" string)))
105           (when encoded
106             (setq value (rfc2231-decode-encoded-string value)))
107           (if number
108               (setq prev-attribute attribute
109                     prev-value (concat prev-value value))
110             (push (cons attribute value) parameters)))
111
112         ;; Take care of any final continuations.
113         (when prev-attribute
114           (push (cons prev-attribute prev-value) parameters))
115
116         `(,type ,@(nreverse parameters))))))
117
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\"."
121   (with-temp-buffer
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 (car (last elems)))
127       (goto-char (point-min))
128       (while (search-forward "%" nil t)
129         (insert
130          (prog1
131              (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
132            (delete-region (1- (point)) (+ (point) 2)))))
133       ;; Encode using the charset, if any.
134       (when (and (< (length elems) 1)
135                  (not (equal (intern (car elems)) 'us-ascii)))
136         (mm-decode-coding-region (point-min) (point-max)
137                                  (intern (car elems))))
138       (buffer-string))))
139
140 (provide 'rfc2231)
141
142 ;;; rfc2231.el ends here