Add arch taglines
[gnus] / lisp / rfc2231.el
1 ;;; rfc2231.el --- Functions for decoding rfc2231 headers
2
3 ;; Copyright (C) 1998, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; This file is part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (require 'ietf-drums)
29 (require 'rfc2047)
30 (autoload 'mm-encode-body "mm-bodies")
31 (autoload 'mail-header-remove-whitespace "mail-parse")
32 (autoload 'mail-header-remove-comments "mail-parse")
33
34 (defun rfc2231-get-value (ct attribute)
35   "Return the value of ATTRIBUTE from CT."
36   (cdr (assq attribute (cdr ct))))
37
38 (defun rfc2231-parse-qp-string (string)
39   "Parse QP-encoded string using `rfc2231-parse-string'.
40 N.B.  This is in violation with RFC2047, but it seem to be in common use."
41   (rfc2231-parse-string (rfc2047-decode-string string)))
42
43 (defun rfc2231-parse-string (string)
44   "Parse STRING and return a list.
45 The list will be on the form
46  `(name (attribute . value) (attribute . value)...)"
47   (with-temp-buffer
48     (let ((ttoken (ietf-drums-token-to-list ietf-drums-text-token))
49           (stoken (ietf-drums-token-to-list ietf-drums-tspecials))
50           (ntoken (ietf-drums-token-to-list "0-9"))
51           (prev-value "")
52           display-name mailbox c display-string parameters
53           attribute value type subtype number encoded
54           prev-attribute)
55       (ietf-drums-init (mail-header-remove-whitespace
56                         (mail-header-remove-comments string)))
57       (let ((table (copy-syntax-table ietf-drums-syntax-table)))
58         (modify-syntax-entry ?\' "w" table)
59         (modify-syntax-entry ?= " " table)
60         ;; The following isn't valid, but one should be liberal
61         ;; in what one receives.
62         (modify-syntax-entry ?\: "w" table)
63         (set-syntax-table table))
64       (setq c (char-after))
65       (when (and (memq c ttoken)
66                  (not (memq c stoken)))
67         (setq type (downcase (buffer-substring
68                               (point) (progn (forward-sexp 1) (point)))))
69         ;; Do the params
70         (while (not (eobp))
71           (setq c (char-after))
72           (unless (eq c ?\;)
73             (error "Invalid header: %s" string))
74           (forward-char 1)
75           ;; If c in nil, then this is an invalid header, but
76           ;; since elm generates invalid headers on this form,
77           ;; we allow it.
78           (when (setq c (char-after))
79             (if (and (memq c ttoken)
80                      (not (memq c stoken)))
81                 (setq attribute
82                       (intern
83                        (downcase
84                         (buffer-substring
85                          (point) (progn (forward-sexp 1) (point))))))
86               (error "Invalid header: %s" string))
87             (setq c (char-after))
88             (setq encoded nil)
89             (when (eq c ?*)
90               (forward-char 1)
91               (setq c (char-after))
92               (if (not (memq c ntoken))
93                   (setq encoded t
94                         number nil)
95                 (setq number
96                       (string-to-number
97                        (buffer-substring
98                         (point) (progn (forward-sexp 1) (point)))))
99                 (setq c (char-after))
100                 (when (eq c ?*)
101                   (setq encoded t)
102                   (forward-char 1)
103                   (setq c (char-after)))))
104             ;; See if we have any previous continuations.
105             (when (and prev-attribute
106                        (not (eq prev-attribute attribute)))
107               (push (cons prev-attribute prev-value) parameters)
108               (setq prev-attribute nil
109                     prev-value ""))
110             (unless (eq c ?=)
111               (error "Invalid header: %s" string))
112             (forward-char 1)
113             (setq c (char-after))
114             (cond
115              ((eq c ?\")
116               (setq value
117                     (buffer-substring (1+ (point))
118                                       (progn (forward-sexp 1) (1- (point))))))
119              ((and (or (memq c ttoken)
120                        (> c ?\177)) ;; EXTENSION: Support non-ascii chars.
121                    (not (memq c stoken)))
122               (setq value (buffer-substring
123                            (point) (progn (forward-sexp) (point)))))
124              (t
125               (error "Invalid header: %s" string)))
126             (when encoded
127               (setq value (rfc2231-decode-encoded-string value)))
128             (if number
129                 (setq prev-attribute attribute
130                       prev-value (concat prev-value value))
131               (push (cons attribute value) parameters))))
132
133         ;; Take care of any final continuations.
134         (when prev-attribute
135           (push (cons prev-attribute prev-value) parameters))
136
137         (when type
138           `(,type ,@(nreverse parameters)))))))
139
140 (defun rfc2231-decode-encoded-string (string)
141   "Decode an RFC2231-encoded string.
142 These look like \"us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A\"."
143   (with-temp-buffer
144     (let ((elems (split-string string "'")))
145       ;; The encoded string may contain zero to two single-quote
146       ;; marks.  This should give us the encoded word stripped
147       ;; of any preceding values.
148       (insert (car (last elems)))
149       (goto-char (point-min))
150       (while (search-forward "%" nil t)
151         (insert
152          (prog1
153              (string-to-number (buffer-substring (point) (+ (point) 2)) 16)
154            (delete-region (1- (point)) (+ (point) 2)))))
155       ;; Encode using the charset, if any.
156       (when (and (mm-multibyte-p)
157                  (> (length elems) 1)
158                  (not (equal (intern (downcase (car elems))) 'us-ascii)))
159         (mm-decode-coding-region (point-min) (point-max)
160                                  (intern (downcase (car elems)))))
161       (buffer-string))))
162
163 (defun rfc2231-encode-string (param value)
164   "Return and PARAM=VALUE string encoded according to RFC2231."
165   (let ((control (ietf-drums-token-to-list ietf-drums-no-ws-ctl-token))
166         (tspecial (ietf-drums-token-to-list ietf-drums-tspecials))
167         (special (ietf-drums-token-to-list "*'%\n\t"))
168         (ascii (ietf-drums-token-to-list ietf-drums-text-token))
169         (num -1)
170         spacep encodep charsetp charset broken)
171     (with-temp-buffer
172       (insert value)
173       (goto-char (point-min))
174       (while (not (eobp))
175         (cond
176          ((or (memq (following-char) control)
177               (memq (following-char) tspecial)
178               (memq (following-char) special))
179           (setq encodep t))
180          ((eq (following-char) ? )
181           (setq spacep t))
182          ((not (memq (following-char) ascii))
183           (setq charsetp t)))
184         (forward-char 1))
185       (when charsetp
186         (setq charset (mm-encode-body)))
187       (cond
188        ((or encodep charsetp)
189         (goto-char (point-min))
190         (while (not (eobp))
191           (when (> (current-column) 60)
192             (insert ";\n")
193             (setq broken t))
194           (if (or (not (memq (following-char) ascii))
195                   (memq (following-char) control)
196                   (memq (following-char) tspecial)
197                   (memq (following-char) special)
198                   (eq (following-char) ? ))
199               (progn
200                 (insert "%" (format "%02x" (following-char)))
201                 (delete-char 1))
202             (forward-char 1)))
203         (goto-char (point-min))
204         (insert (symbol-name (or charset 'us-ascii)) "''")
205         (goto-char (point-min))
206         (if (not broken)
207             (insert param "*=")
208           (while (not (eobp))
209             (insert (if (>= num 0) " " "\n ")
210                     param "*" (format "%d" (incf num)) "*=")
211             (forward-line 1))))
212        (spacep
213         (goto-char (point-min))
214         (insert param "=\"")
215         (goto-char (point-max))
216         (insert "\""))
217        (t
218         (goto-char (point-min))
219         (insert param "=")))
220       (buffer-string))))
221
222 (provide 'rfc2231)
223
224 ;;; arch-tag: c3ab751d-d108-406a-b301-68882ad8cd63
225 ;;; rfc2231.el ends here