blob: bfffb457dcd60145e25fc5469163fcf1d1cad772 [file] [log] [blame]
R David Murrayc27e5222012-05-25 15:01:48 -04001"""This will be the home for the policy that hooks in the new
2code that adds all the email6 features.
R David Murray3edd22a2011-04-18 13:59:37 -04003"""
4
R David Murray0b6f6c82012-05-25 18:42:14 -04005from email._policybase import Policy, Compat32, compat32
6from email.utils import _has_surrogates
R David Murrayea976682012-05-27 15:03:38 -04007from email.headerregistry import HeaderRegistry as HeaderRegistry
R David Murray3edd22a2011-04-18 13:59:37 -04008
R David Murray0b6f6c82012-05-25 18:42:14 -04009__all__ = [
10 'Compat32',
11 'compat32',
12 'Policy',
13 'EmailPolicy',
14 'default',
15 'strict',
16 'SMTP',
17 'HTTP',
18 ]
R David Murray3edd22a2011-04-18 13:59:37 -040019
R David Murray0b6f6c82012-05-25 18:42:14 -040020class EmailPolicy(Policy):
21
22 """+
23 PROVISIONAL
24
25 The API extensions enabled by this this policy are currently provisional.
26 Refer to the documentation for details.
27
28 This policy adds new header parsing and folding algorithms. Instead of
29 simple strings, headers are custom objects with custom attributes
30 depending on the type of the field. The folding algorithm fully
31 implements RFCs 2047 and 5322.
32
33 In addition to the settable attributes listed above that apply to
34 all Policies, this policy adds the following additional attributes:
35
36 refold_source -- if the value for a header in the Message object
37 came from the parsing of some source, this attribute
38 indicates whether or not a generator should refold
39 that value when transforming the message back into
40 stream form. The possible values are:
41
42 none -- all source values use original folding
43 long -- source values that have any line that is
44 longer than max_line_length will be
45 refolded
46 all -- all values are refolded.
47
48 The default is 'long'.
49
50 header_factory -- a callable that takes two arguments, 'name' and
51 'value', where 'name' is a header field name and
52 'value' is an unfolded header field value, and
53 returns a string-like object that represents that
54 header. A default header_factory is provided that
55 understands some of the RFC5322 header field types.
56 (Currently address fields and date fields have
57 special treatment, while all other fields are
58 treated as unstructured. This list will be
59 completed before the extension is marked stable.)
60 """
61
62 refold_source = 'long'
R David Murrayea976682012-05-27 15:03:38 -040063 header_factory = HeaderRegistry()
R David Murray0b6f6c82012-05-25 18:42:14 -040064
65 def __init__(self, **kw):
66 # Ensure that each new instance gets a unique header factory
67 # (as opposed to clones, which share the factory).
68 if 'header_factory' not in kw:
R David Murrayea976682012-05-27 15:03:38 -040069 object.__setattr__(self, 'header_factory', HeaderRegistry())
R David Murray0b6f6c82012-05-25 18:42:14 -040070 super().__init__(**kw)
71
R David Murrayabfc3742012-05-29 09:14:44 -040072 def header_max_count(self, name):
73 """+
74 The implementation for this class returns the max_count attribute from
75 the specialized header class that would be used to construct a header
76 of type 'name'.
77 """
78 return self.header_factory[name].max_count
79
R David Murray0b6f6c82012-05-25 18:42:14 -040080 # The logic of the next three methods is chosen such that it is possible to
81 # switch a Message object between a Compat32 policy and a policy derived
82 # from this class and have the results stay consistent. This allows a
83 # Message object constructed with this policy to be passed to a library
84 # that only handles Compat32 objects, or to receive such an object and
85 # convert it to use the newer style by just changing its policy. It is
86 # also chosen because it postpones the relatively expensive full rfc5322
87 # parse until as late as possible when parsing from source, since in many
88 # applications only a few headers will actually be inspected.
89
90 def header_source_parse(self, sourcelines):
91 """+
92 The name is parsed as everything up to the ':' and returned unmodified.
93 The value is determined by stripping leading whitespace off the
94 remainder of the first line, joining all subsequent lines together, and
95 stripping any trailing carriage return or linefeed characters. (This
96 is the same as Compat32).
97
98 """
99 name, value = sourcelines[0].split(':', 1)
100 value = value.lstrip(' \t') + ''.join(sourcelines[1:])
101 return (name, value.rstrip('\r\n'))
102
103 def header_store_parse(self, name, value):
104 """+
105 The name is returned unchanged. If the input value has a 'name'
106 attribute and it matches the name ignoring case, the value is returned
107 unchanged. Otherwise the name and value are passed to header_factory
108 method, and the resulting custom header object is returned as the
109 value. In this case a ValueError is raised if the input value contains
110 CR or LF characters.
111
112 """
113 if hasattr(value, 'name') and value.name.lower() == name.lower():
114 return (name, value)
R David Murraydcaf2ec2012-05-25 22:53:12 -0400115 if isinstance(value, str) and len(value.splitlines())>1:
R David Murray0b6f6c82012-05-25 18:42:14 -0400116 raise ValueError("Header values may not contain linefeed "
117 "or carriage return characters")
118 return (name, self.header_factory(name, value))
119
120 def header_fetch_parse(self, name, value):
121 """+
122 If the value has a 'name' attribute, it is returned to unmodified.
123 Otherwise the name and the value with any linesep characters removed
124 are passed to the header_factory method, and the resulting custom
125 header object is returned. Any surrogateescaped bytes get turned
126 into the unicode unknown-character glyph.
127
128 """
129 if hasattr(value, 'name'):
130 return value
131 return self.header_factory(name, ''.join(value.splitlines()))
132
133 def fold(self, name, value):
134 """+
135 Header folding is controlled by the refold_source policy setting. A
136 value is considered to be a 'source value' if and only if it does not
137 have a 'name' attribute (having a 'name' attribute means it is a header
138 object of some sort). If a source value needs to be refolded according
139 to the policy, it is converted into a custom header object by passing
140 the name and the value with any linesep characters removed to the
141 header_factory method. Folding of a custom header object is done by
142 calling its fold method with the current policy.
143
144 Source values are split into lines using splitlines. If the value is
145 not to be refolded, the lines are rejoined using the linesep from the
146 policy and returned. The exception is lines containing non-ascii
147 binary data. In that case the value is refolded regardless of the
148 refold_source setting, which causes the binary data to be CTE encoded
149 using the unknown-8bit charset.
150
151 """
152 return self._fold(name, value, refold_binary=True)
153
154 def fold_binary(self, name, value):
155 """+
156 The same as fold if cte_type is 7bit, except that the returned value is
157 bytes.
158
159 If cte_type is 8bit, non-ASCII binary data is converted back into
160 bytes. Headers with binary data are not refolded, regardless of the
161 refold_header setting, since there is no way to know whether the binary
162 data consists of single byte characters or multibyte characters.
163
164 """
165 folded = self._fold(name, value, refold_binary=self.cte_type=='7bit')
166 return folded.encode('ascii', 'surrogateescape')
167
168 def _fold(self, name, value, refold_binary=False):
169 if hasattr(value, 'name'):
170 return value.fold(policy=self)
171 maxlen = self.max_line_length if self.max_line_length else float('inf')
172 lines = value.splitlines()
173 refold = (self.refold_source == 'all' or
174 self.refold_source == 'long' and
175 (len(lines[0])+len(name)+2 > maxlen or
176 any(len(x) > maxlen for x in lines[1:])))
177 if refold or refold_binary and _has_surrogates(value):
178 return self.header_factory(name, ''.join(lines)).fold(policy=self)
179 return name + ': ' + self.linesep.join(lines) + self.linesep
180
181
182default = EmailPolicy()
183# Make the default policy use the class default header_factory
184del default.header_factory
R David Murray3edd22a2011-04-18 13:59:37 -0400185strict = default.clone(raise_on_defect=True)
186SMTP = default.clone(linesep='\r\n')
187HTTP = default.clone(linesep='\r\n', max_line_length=None)