annotate lisp/gnus/rfc2047.el @ 89340:1cc27231b263

(update_compositions): Fix type error.
author Dave Love <fx@gnu.org>
date Tue, 07 Jan 2003 17:54:11 +0000
parents b39c11cf3b5d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38413
a26d9b55abb6 Some fixes to follow coding conventions in files from Gnus.
Pavel Janík <Pavel@Janik.cz>
parents: 35985
diff changeset
1 ;;; rfc2047.el --- functions for encoding and decoding rfc2047 messages
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
2 ;; Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
3
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
5 ;; MORIOKA Tomohiko <morioka@jaist.ac.jp>
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
6 ;; This file is part of GNU Emacs.
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
7
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
9 ;; it under the terms of the GNU General Public License as published by
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
10 ;; the Free Software Foundation; either version 2, or (at your option)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
11 ;; any later version.
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
12
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
13 ;; GNU Emacs is distributed in the hope that it will be useful,
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
16 ;; GNU General Public License for more details.
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
17
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
18 ;; You should have received a copy of the GNU General Public License
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
21 ;; Boston, MA 02111-1307, USA.
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
22
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
23 ;;; Commentary:
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
24
34674
79dde983f45b Add some commentary.
Dave Love <fx@gnu.org>
parents: 33815
diff changeset
25 ;; RFC 2047 is "MIME (Multipurpose Internet Mail Extensions) Part
79dde983f45b Add some commentary.
Dave Love <fx@gnu.org>
parents: 33815
diff changeset
26 ;; Three: Message Header Extensions for Non-ASCII Text".
79dde983f45b Add some commentary.
Dave Love <fx@gnu.org>
parents: 33815
diff changeset
27
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
28 ;;; Code:
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
29
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
30 (eval-when-compile
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
31 (require 'cl)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
32 (defvar message-posting-charset))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
33
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
34 (require 'qp)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
35 (require 'mm-util)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
36 (require 'ietf-drums)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
37 (require 'mail-prsvr)
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
38 (require 'base64)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
39 ;; Fixme: Avoid this (for gnus-point-at-...) mm dependence on gnus.
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
40 (require 'gnus-util)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
41 (autoload 'mm-body-7-or-8 "mm-bodies")
33127
eca95f9d7f05 (base64): Require unconditionally.
Dave Love <fx@gnu.org>
parents: 31764
diff changeset
42
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
43 (defvar rfc2047-header-encoding-alist
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
44 '(("Newsgroups" . nil)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
45 ("Message-ID" . nil)
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
46 ("\\(Resent-\\)?\\(From\\|Cc\\|To\\|Bcc\\|Reply-To\\|Sender\\)" .
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
47 address-mime)
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
48 (t . mime))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
49 "*Header/encoding method alist.
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
50 The list is traversed sequentially. The keys can either be
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
51 header regexps or t.
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
52
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
53 The values can be:
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
54
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
55 1) nil, in which case no encoding is done;
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
56 2) `mime', in which case the header will be encoded according to RFC2047;
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
57 3) `address-mime', like `mime', but takes account of the rules for address
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
58 fields (where quoted strings and comments must be treated separately);
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
59 4) a charset, in which case it will be encoded as that charset;
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
60 5) `default', in which case the field will be encoded as the rest
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
61 of the article.")
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
62
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
63 (defvar rfc2047-charset-encoding-alist
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
64 '((us-ascii . nil)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
65 (iso-8859-1 . Q)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
66 (iso-8859-2 . Q)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
67 (iso-8859-3 . Q)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
68 (iso-8859-4 . Q)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
69 (iso-8859-5 . B)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
70 (koi8-r . B)
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
71 (iso-8859-7 . B)
88920
4cda993e8f6e (rfc2047-charset-encoding-alist): Use B for Hebrew.
Dave Love <fx@gnu.org>
parents: 88792
diff changeset
72 (iso-8859-8 . B)
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
73 (iso-8859-9 . Q)
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
74 (iso-8859-14 . Q)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
75 (iso-8859-15 . Q)
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
76 (iso-2022-jp . B)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
77 (iso-2022-kr . B)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
78 (gb2312 . B)
35838
53eebdb81828 2001-02-01 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents: 35453
diff changeset
79 (big5 . B)
53eebdb81828 2001-02-01 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents: 35453
diff changeset
80 (cn-big5 . B)
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
81 (cn-gb . B)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
82 (cn-gb-2312 . B)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
83 (euc-kr . B)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
84 (iso-2022-jp-2 . B)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
85 (iso-2022-int-1 . B))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
86 "Alist of MIME charsets to RFC2047 encodings.
88920
4cda993e8f6e (rfc2047-charset-encoding-alist): Use B for Hebrew.
Dave Love <fx@gnu.org>
parents: 88792
diff changeset
87 Valid encodings are nil, `Q' and `B'. These indicate binary (no) encoding,
4cda993e8f6e (rfc2047-charset-encoding-alist): Use B for Hebrew.
Dave Love <fx@gnu.org>
parents: 88792
diff changeset
88 quoted-printable and base64 respectively.")
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
89
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
90 (defvar rfc2047-encoding-function-alist
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
91 '((Q . rfc2047-q-encode-region)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
92 (B . rfc2047-b-encode-region)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
93 (nil . ignore))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
94 "Alist of RFC2047 encodings to encoding functions.")
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
95
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
96 (defvar rfc2047-q-encoding-alist
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
97 '(("\\(Resent-\\)?\\(From\\|Cc\\|To\\|Bcc\\|Reply-To\\|Sender\\):"
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
98 . "-A-Za-z0-9!*+/" )
31764
54ae1def18cf Merge from Gnus trunk.
Dave Love <fx@gnu.org>
parents: 31717
diff changeset
99 ;; = (\075), _ (\137), ? (\077) are used in the encoded word.
35985
b9c371244b90 (rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents: 35838
diff changeset
100 ;; Avoid using 8bit characters.
31764
54ae1def18cf Merge from Gnus trunk.
Dave Love <fx@gnu.org>
parents: 31717
diff changeset
101 ;; Equivalent to "^\000-\007\011\013\015-\037\200-\377=_?"
54ae1def18cf Merge from Gnus trunk.
Dave Love <fx@gnu.org>
parents: 31717
diff changeset
102 ("." . "\010\012\014\040-\074\076\100-\136\140-\177"))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
103 "Alist of header regexps and valid Q characters.")
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
104
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
105 ;;;
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
106 ;;; Functions for encoding RFC2047 messages
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
107 ;;;
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
108
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
109 (defun rfc2047-narrow-to-field ()
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
110 "Narrow the buffer to the header on the current line."
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
111 (beginning-of-line)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
112 (narrow-to-region
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
113 (point)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
114 (progn
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
115 (forward-line 1)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
116 (if (re-search-forward "^[^ \n\t]" nil t)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
117 (progn
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
118 (beginning-of-line)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
119 (point))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
120 (point-max))))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
121 (goto-char (point-min)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
122
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
123 (defun rfc2047-encode-message-header ()
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
124 "Encode the message header according to `rfc2047-header-encoding-alist'.
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
125 Should be called narrowed to the head of the message."
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
126 (interactive "*")
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
127 (save-excursion
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
128 (goto-char (point-min))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
129 (let (alist elem method)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
130 (while (not (eobp))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
131 (save-restriction
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
132 (rfc2047-narrow-to-field)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
133 (if (not (rfc2047-encodable-p))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
134 (if (and (eq (mm-body-7-or-8) '8bit)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
135 (mm-multibyte-p)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
136 (mm-coding-system-p
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
137 (car message-posting-charset)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
138 ;; 8 bit must be decoded.
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
139 ;; Is message-posting-charset a coding system?
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
140 (mm-encode-coding-region
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
141 (point-min) (point-max)
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
142 (car message-posting-charset)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
143 ;; We found something that may perhaps be encoded.
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
144 (setq method nil
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
145 alist rfc2047-header-encoding-alist)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
146 (while (setq elem (pop alist))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
147 (when (or (and (stringp (car elem))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
148 (looking-at (car elem)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
149 (eq (car elem) t))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
150 (setq alist nil
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
151 method (cdr elem))))
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
152 (goto-char (point-min))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
153 (re-search-forward "^[^:]+: *" nil t)
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
154 (cond
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
155 ((eq method 'address-mime)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
156 (rfc2047-encode-region (point) (point-max)))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
157 ((eq method 'mime)
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
158 (let (rfc2047-special-chars)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
159 (rfc2047-encode-region (point) (point-max))))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
160 ((eq method 'default)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
161 (if (and (featurep 'mule)
33815
61c7f3065929 (rfc2047-encode-message-header): Don't encode if
Dave Love <fx@gnu.org>
parents: 33304
diff changeset
162 (if (boundp 'default-enable-multibyte-characters)
61c7f3065929 (rfc2047-encode-message-header): Don't encode if
Dave Love <fx@gnu.org>
parents: 33304
diff changeset
163 default-enable-multibyte-characters)
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
164 mail-parse-charset)
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
165 (mm-encode-coding-region (point) (point-max)
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
166 mail-parse-charset)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
167 ((mm-coding-system-p method)
33815
61c7f3065929 (rfc2047-encode-message-header): Don't encode if
Dave Love <fx@gnu.org>
parents: 33304
diff changeset
168 (if (and (featurep 'mule)
61c7f3065929 (rfc2047-encode-message-header): Don't encode if
Dave Love <fx@gnu.org>
parents: 33304
diff changeset
169 (if (boundp 'default-enable-multibyte-characters)
61c7f3065929 (rfc2047-encode-message-header): Don't encode if
Dave Love <fx@gnu.org>
parents: 33304
diff changeset
170 default-enable-multibyte-characters))
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
171 (mm-encode-coding-region (point) (point-max) method)))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
172 ;; Hm.
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
173 (t)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
174 (goto-char (point-max)))))))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
175
35985
b9c371244b90 (rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents: 35838
diff changeset
176 ;; Fixme: This, and the require below may not be the Right Thing, but
b9c371244b90 (rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents: 35838
diff changeset
177 ;; should be safe just before release. -- fx 2001-02-08
b9c371244b90 (rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents: 35838
diff changeset
178 (eval-when-compile (defvar message-posting-charset))
b9c371244b90 (rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents: 35838
diff changeset
179
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
180 (defun rfc2047-encodable-p ()
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
181 "Return non-nil if any characters in current buffer need encoding in headers.
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
182 The buffer may be narrowed."
35985
b9c371244b90 (rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents: 35838
diff changeset
183 (require 'message) ; for message-posting-charset
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
184 (let ((charsets
88792
690e82320649 (rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents: 38413
diff changeset
185 (mm-find-mime-charset-region (point-min) (point-max))))
690e82320649 (rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents: 38413
diff changeset
186 (and charsets (not (equal charsets (list message-posting-charset))))))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
187
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
188 ;; ietf-drums-specials-token less \ . @
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
189 (defconst rfc2047-special-chars (append "()<>[]:;,\"" nil)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
190 "List of characters treated as special when rfc207-encoding address fields.
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
191 When encoding other sorts of fields, bin it to nil to avoid treating
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
192 RFC 2822 quoted words and comments specially.")
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
193
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
194 (defconst rfc2047-non-special-chars (concat "^" rfc2047-special-chars))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
195
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
196 (defun rfc2047-dissect-region (b e)
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
197 "Dissect the region between B and E into tokens.
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
198 The tokens comprise sequences of atoms, quoted strings, special
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
199 characters and whitespace."
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
200 (save-restriction
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
201 (narrow-to-region b e)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
202 (if (null rfc2047-special-chars)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
203 ;; simple `mime' case -- no need to tokenize
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
204 (list (buffer-substring b e))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
205 ;; `address-mime' case -- take care of quoted words, comments
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
206 (with-syntax-table ietf-drums-syntax-table
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
207 (let ((start (point))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
208 words)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
209 (goto-char (point-min))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
210 (condition-case nil ; in case of unbalanced specials
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
211 ;; Dissect into: sequences of atoms, quoted strings,
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
212 ;; specials, whitespace. (Specials mustn't be encoded.)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
213 (while (not (eobp))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
214 (setq start (point))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
215 (unless (= 0 (skip-chars-forward ietf-drums-wsp-token))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
216 (push (buffer-substring start (point)) words)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
217 (setq start (point)))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
218 (cond
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
219 ((memq (char-after) rfc2047-special-chars)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
220 ;; Grab string or special char.
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
221 (if (eq ?\" (char-after))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
222 (progn
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
223 (forward-sexp)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
224 (push (buffer-substring start (point)) words))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
225 (push (string (char-after)) words)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
226 (forward-char)))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
227 ((not (char-after))) ; eob
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
228 (t ; normal token/whitespace sequence
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
229 (skip-chars-forward rfc2047-non-special-chars)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
230 (skip-chars-backward ietf-drums-wsp-token)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
231 (push (buffer-substring start (point)) words))))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
232 (error (error "Invalid data for rfc2047 encoding: %s"
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
233 (buffer-substring b e))))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
234 (nreverse words))))))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
235
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
236 ;; Fixme: why does this cons a list of words and insert them, rather
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
237 ;; than encoding in place?
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
238 (defun rfc2047-encode-region (b e)
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
239 "Encode all encodable words in region B to E.
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
240 By default, the region is treated as containing addresses (see
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
241 `rfc2047-special-chars')."
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
242 (let ((words (rfc2047-dissect-region b e)) word)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
243 (save-restriction
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
244 (narrow-to-region b e)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
245 (delete-region (point-min) (point-max))
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
246 (dolist (word words)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
247 ;; Quoted strings can't contain encoded words. Strip the
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
248 ;; quotes.
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
249 (if rfc2047-special-chars
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
250 (if (eq ?\" (aref word 0))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
251 (setq word (substring word 1 -1))))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
252 (if (string-match "\\`[\0-\177]*\\'" word) ; including whitespace
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
253 (insert word)
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
254 (rfc2047-fold-region (gnus-point-at-bol) (point))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
255 (goto-char (point-max))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
256 (if (> (- (point) (save-restriction
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
257 (widen)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
258 (gnus-point-at-bol))) 76)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
259 (insert "\n "))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
260 ;; Insert blank between encoded words
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
261 (if (eq (char-before) ?=) (insert " "))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
262 (rfc2047-encode (point)
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
263 (progn (insert word) (point)))))
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
264 (rfc2047-fold-region (point-min) (point-max)))))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
265
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
266 (defun rfc2047-encode-string (string)
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
267 "Encode words in STRING.
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
268 By default, the string is treated as containing addresses (see
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
269 `rfc2047-special-chars')."
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
270 (with-temp-buffer
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
271 (insert string)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
272 (rfc2047-encode-region (point-min) (point-max))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
273 (buffer-string)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
274
88792
690e82320649 (rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents: 38413
diff changeset
275 (defun rfc2047-encode (b e)
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
276 "Encode the word(s) in the region B to E.
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
277 By default, the region is treated as containing addresses (see
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
278 `rfc2047-special-chars')."
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
279 (let* ((mime-charset (mm-find-mime-charset-region b e))
88792
690e82320649 (rfc2047-encodable-p): Avoid mm-find-charset-region.
Dave Love <fx@gnu.org>
parents: 38413
diff changeset
280 (cs (if (> (length mime-charset) 1)
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
281 ;; Fixme: instead of this, try to break region into
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
282 ;; parts that can be encoded separately.
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
283 (error "Can't rfc2047-encode `%s'"
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
284 (buffer-substring b e))
89000
56933b338052 (rfc2047-encode): Fix last change.
Dave Love <fx@gnu.org>
parents: 88920
diff changeset
285 (setq mime-charset (car mime-charset))
56933b338052 (rfc2047-encode): Fix last change.
Dave Love <fx@gnu.org>
parents: 88920
diff changeset
286 (mm-charset-to-coding-system mime-charset)))
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
287 (encoding (if (assq mime-charset
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
288 rfc2047-charset-encoding-alist)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
289 (cdr (assq mime-charset
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
290 rfc2047-charset-encoding-alist))
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
291 'B))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
292 (start (concat
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
293 "=?" (downcase (symbol-name mime-charset)) "?"
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
294 (downcase (symbol-name encoding)) "?"))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
295 (first t))
89077
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
296 (if mime-charset
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
297 (save-restriction
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
298 (narrow-to-region b e)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
299 (when (eq encoding 'B)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
300 ;; break into lines before encoding
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
301 (goto-char (point-min))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
302 (while (not (eobp))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
303 (goto-char (min (point-max) (+ 15 (point))))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
304 (unless (eobp)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
305 (insert "\n"))))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
306 (if (and (mm-multibyte-p)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
307 (mm-coding-system-p cs))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
308 (mm-encode-coding-region (point-min) (point-max) cs))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
309 (funcall (cdr (assq encoding rfc2047-encoding-function-alist))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
310 (point-min) (point-max))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
311 (goto-char (point-min))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
312 (while (not (eobp))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
313 (unless first
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
314 (insert " "))
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
315 (setq first nil)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
316 (insert start)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
317 (end-of-line)
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
318 (insert "?=")
b39c11cf3b5d (message-posting-charset): defvar when compiling.
Dave Love <fx@gnu.org>
parents: 89000
diff changeset
319 (forward-line 1))))))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
320
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
321 (defun rfc2047-fold-region (b e)
35985
b9c371244b90 (rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents: 35838
diff changeset
322 "Fold long lines in region B to E."
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
323 (save-restriction
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
324 (narrow-to-region b e)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
325 (goto-char (point-min))
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
326 (let ((break nil)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
327 (qword-break nil)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
328 (bol (save-restriction
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
329 (widen)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
330 (gnus-point-at-bol))))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
331 (while (not (eobp))
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
332 (when (and (or break qword-break) (> (- (point) bol) 76))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
333 (goto-char (or break qword-break))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
334 (setq break nil
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
335 qword-break nil)
35453
26726eff41ca 2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents: 34674
diff changeset
336 (if (looking-at " \t")
26726eff41ca 2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents: 34674
diff changeset
337 (insert "\n")
26726eff41ca 2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents: 34674
diff changeset
338 (insert "\n "))
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
339 (setq bol (1- (point)))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
340 ;; Don't break before the first non-LWSP characters.
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
341 (skip-chars-forward " \t")
35985
b9c371244b90 (rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents: 35838
diff changeset
342 (unless (eobp) (forward-char 1)))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
343 (cond
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
344 ((eq (char-after) ?\n)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
345 (forward-char 1)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
346 (setq bol (point)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
347 break nil
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
348 qword-break nil)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
349 (skip-chars-forward " \t")
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
350 (unless (or (eobp) (eq (char-after) ?\n))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
351 (forward-char 1)))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
352 ((eq (char-after) ?\r)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
353 (forward-char 1))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
354 ((memq (char-after) '(? ?\t))
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
355 (skip-chars-forward " \t")
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
356 (setq break (1- (point))))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
357 ((not break)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
358 (if (not (looking-at "=\\?[^=]"))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
359 (if (eq (char-after) ?=)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
360 (forward-char 1)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
361 (skip-chars-forward "^ \t\n\r="))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
362 (setq qword-break (point))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
363 (skip-chars-forward "^ \t\n\r")))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
364 (t
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
365 (skip-chars-forward "^ \t\n\r"))))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
366 (when (and (or break qword-break) (> (- (point) bol) 76))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
367 (goto-char (or break qword-break))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
368 (setq break nil
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
369 qword-break nil)
35453
26726eff41ca 2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents: 34674
diff changeset
370 (if (looking-at " \t")
26726eff41ca 2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents: 34674
diff changeset
371 (insert "\n")
26726eff41ca 2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
ShengHuo ZHU <zsh@cs.rochester.edu>
parents: 34674
diff changeset
372 (insert "\n "))
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
373 (setq bol (1- (point)))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
374 ;; Don't break before the first non-LWSP characters.
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
375 (skip-chars-forward " \t")
35985
b9c371244b90 (rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents: 35838
diff changeset
376 (unless (eobp) (forward-char 1))))))
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
377
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
378 (defun rfc2047-unfold-region (b e)
35985
b9c371244b90 (rfc2047-fold-region): Don't forward-char at EOB.
Dave Love <fx@gnu.org>
parents: 35838
diff changeset
379 "Unfold lines in region B to E."
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
380 (save-restriction
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
381 (narrow-to-region b e)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
382 (goto-char (point-min))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
383 (let ((bol (save-restriction
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
384 (widen)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
385 (gnus-point-at-bol)))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
386 (eol (gnus-point-at-eol))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
387 leading)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
388 (forward-line 1)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
389 (while (not (eobp))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
390 (looking-at "[ \t]*")
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
391 (setq leading (- (match-end 0) (match-beginning 0)))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
392 (if (< (- (gnus-point-at-eol) bol leading) 76)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
393 (progn
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
394 (goto-char eol)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
395 (delete-region eol (progn
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
396 (skip-chars-forward "[ \t\n\r]+")
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
397 (1- (point)))))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
398 (setq bol (gnus-point-at-bol)))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
399 (setq eol (gnus-point-at-eol))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
400 (forward-line 1)))))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
401
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
402 (defun rfc2047-b-encode-region (b e)
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
403 "Base64-encode the header contained in region B to E."
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
404 (save-restriction
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
405 (narrow-to-region (goto-char b) e)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
406 (while (not (eobp))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
407 (base64-encode-region (point) (progn (end-of-line) (point)) t)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
408 (if (and (bolp) (eolp))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
409 (delete-backward-char 1))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
410 (forward-line))))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
411
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
412 (defun rfc2047-q-encode-region (b e)
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
413 "Quoted-printable-encode the header in region B to E."
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
414 (save-excursion
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
415 (save-restriction
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
416 (narrow-to-region (goto-char b) e)
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
417 (let ((alist rfc2047-q-encoding-alist)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
418 (bol (save-restriction
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
419 (widen)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
420 (gnus-point-at-bol))))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
421 (while alist
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
422 (when (looking-at (caar alist))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
423 (quoted-printable-encode-region b e nil (cdar alist))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
424 (subst-char-in-region (point-min) (point-max) ? ?_)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
425 (setq alist nil))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
426 (pop alist))
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
427 ;; The size of QP encapsulation is about 20, so set limit to
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
428 ;; 56=76-20.
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
429 (unless (< (- (point-max) (point-min)) 56)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
430 ;; Don't break if it could fit in one line.
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
431 ;; Let rfc2047-encode-region break it later.
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
432 (goto-char (1+ (point-min)))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
433 (while (and (not (bobp)) (not (eobp)))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
434 (goto-char (min (point-max) (+ 56 bol)))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
435 (search-backward "=" (- (point) 2) t)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
436 (unless (or (bobp) (eobp))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
437 (insert "\n")
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
438 (setq bol (point)))))))))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
439
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
440 ;;;
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
441 ;;; Functions for decoding RFC2047 messages
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
442 ;;;
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
443
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
444 (defvar rfc2047-encoded-word-regexp
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
445 "=\\?\\([^][\000-\040()<>@,\;:\\\"/?.=]+\\)\\?\\(B\\|Q\\)\\?\\([!->@-~ +]+\\)\\?=")
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
446
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
447 (defun rfc2047-decode-region (start end)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
448 "Decode MIME-encoded words in region between START and END."
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
449 (interactive "r")
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
450 (let ((case-fold-search t)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
451 b e)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
452 (save-excursion
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
453 (save-restriction
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
454 (narrow-to-region start end)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
455 (goto-char (point-min))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
456 ;; Remove whitespace between encoded words.
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
457 (while (re-search-forward
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
458 (concat "\\(" rfc2047-encoded-word-regexp "\\)"
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
459 "\\(\n?[ \t]\\)+"
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
460 "\\(" rfc2047-encoded-word-regexp "\\)")
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
461 nil t)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
462 (delete-region (goto-char (match-end 1)) (match-beginning 6)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
463 ;; Decode the encoded words.
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
464 (setq b (goto-char (point-min)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
465 (while (re-search-forward rfc2047-encoded-word-regexp nil t)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
466 (setq e (match-beginning 0))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
467 (insert (rfc2047-parse-and-decode
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
468 (prog1
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
469 (match-string 0)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
470 (delete-region (match-beginning 0) (match-end 0)))))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
471 (when (and (mm-multibyte-p)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
472 mail-parse-charset
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
473 (not (eq mail-parse-charset 'gnus-decoded)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
474 (mm-decode-coding-region b e mail-parse-charset))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
475 (setq b (point)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
476 (when (and (mm-multibyte-p)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
477 mail-parse-charset
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
478 (not (eq mail-parse-charset 'us-ascii))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
479 (not (eq mail-parse-charset 'gnus-decoded)))
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
480 (mm-decode-coding-region b (point-max) mail-parse-charset))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
481 (rfc2047-unfold-region (point-min) (point-max))))))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
482
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
483 (defun rfc2047-decode-string (string)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
484 "Decode the quoted-printable-encoded STRING and return the results."
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
485 (let ((m (mm-multibyte-p)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
486 (with-temp-buffer
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
487 (when m
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
488 (mm-enable-multibyte))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
489 (insert string)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
490 (inline
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
491 (rfc2047-decode-region (point-min) (point-max)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
492 (buffer-string))))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
493
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
494 (defun rfc2047-parse-and-decode (word)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
495 "Decode WORD and return it if it is an encoded word.
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
496 Return WORD if not."
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
497 (if (not (string-match rfc2047-encoded-word-regexp word))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
498 word
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
499 (or
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
500 (condition-case nil
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
501 (rfc2047-decode
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
502 (match-string 1 word)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
503 (upcase (match-string 2 word))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
504 (match-string 3 word))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
505 (error word))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
506 word)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
507
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
508 (defun rfc2047-decode (charset encoding string)
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
509 "Decode STRING from the given MIME CHARSET in the given ENCODING.
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
510 Valid ENCODINGs are \"B\" and \"Q\".
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
511 If your Emacs implementation can't decode CHARSET, return nil."
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
512 (if (stringp charset)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
513 (setq charset (intern (downcase charset))))
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
514 (if (or (not charset)
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
515 (eq 'gnus-all mail-parse-ignored-charsets)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
516 (memq 'gnus-all mail-parse-ignored-charsets)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
517 (memq charset mail-parse-ignored-charsets))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
518 (setq charset mail-parse-charset))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
519 (let ((cs (mm-charset-to-coding-system charset)))
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
520 (if (and (not cs) charset
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
521 (listp mail-parse-ignored-charsets)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
522 (memq 'gnus-unknown mail-parse-ignored-charsets))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
523 (setq cs (mm-charset-to-coding-system mail-parse-charset)))
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
524 (when cs
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
525 (when (and (eq cs 'ascii)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
526 mail-parse-charset)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
527 (setq cs mail-parse-charset))
33304
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
528 ;; Ensure unibyte result in Emacs 20.
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
529 (let (default-enable-multibyte-characters)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
530 (with-temp-buffer
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
531 (mm-decode-coding-string
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
532 (cond
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
533 ((equal "B" encoding)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
534 (base64-decode-string string))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
535 ((equal "Q" encoding)
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
536 (quoted-printable-decode-string
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
537 (mm-replace-chars-in-string string ?_ ? )))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
538 (t (error "Invalid encoding: %s" encoding)))
d401dfab680a Require gnus-util.
Dave Love <fx@gnu.org>
parents: 33127
diff changeset
539 cs))))))
31717
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
540
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
541 (provide 'rfc2047)
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
542
6b20b7e85e3c *** empty log message ***
Gerd Moellmann <gerd@gnu.org>
parents:
diff changeset
543 ;;; rfc2047.el ends here