1 # Copyright (C) 2001-2010 Python Software Foundation
3 # Contact: email-sig@python.org
5 """Miscellaneous utilities."""
8 'collapse_rfc2231_value',
32 from email
._parseaddr
import quote
33 from email
._parseaddr
import AddressList
as _AddressList
34 from email
._parseaddr
import mktime_tz
36 # We need wormarounds for bugs in these methods in older Pythons (see below)
37 from email
._parseaddr
import parsedate
as _parsedate
38 from email
._parseaddr
import parsedate_tz
as _parsedate_tz
40 from quopri
import decodestring
as _qdecode
42 # Intrapackage imports
43 from email
.encoders
import _bencode
, _qencode
51 specialsre
= re
.compile(r
'[][\\()<>@,:;".]')
52 escapesre
= re
.compile(r
'[][\\()"]')
63 # We can't quite use base64.encodestring() since it tacks on a "courtesy
67 value
= base64
.decodestring(s
)
68 if not s
.endswith('\n') and value
.endswith('\n'):
75 """Replace all line-ending characters with \r\n."""
76 # Fix newlines with no preceding carriage return
77 s
= re
.sub(r
'(?<!\r)\n', CRLF
, s
)
78 # Fix carriage returns with no following newline
79 s
= re
.sub(r
'\r(?!\n)', CRLF
, s
)
85 """The inverse of parseaddr(), this takes a 2-tuple of the form
86 (realname, email_address) and returns the string value suitable
87 for an RFC 2822 From, To or Cc header.
89 If the first element of pair is false, then the second element is
95 if specialsre
.search(name
):
97 name
= escapesre
.sub(r
'\\\g<0>', name
)
98 return '%s%s%s <%s>' % (quotes
, name
, quotes
, address
)
103 def getaddresses(fieldvalues
):
104 """Return a list of (REALNAME, EMAIL) for each fieldvalue."""
105 all
= COMMASPACE
.join(fieldvalues
)
106 a
= _AddressList(all
)
111 ecre
= re
.compile(r
'''
113 (?P<charset>[^?]*?) # non-greedy up to the next ? is the charset
115 (?P<encoding>[qb]) # either a "q" or a "b", case insensitive
117 (?P<atom>.*?) # non-greedy up to the next ?= is the atom
119 ''', re
.VERBOSE | re
.IGNORECASE
)
123 def formatdate(timeval
=None, localtime
=False, usegmt
=False):
124 """Returns a date string as specified by RFC 2822, e.g.:
126 Fri, 09 Nov 2001 01:08:47 -0000
128 Optional timeval if given is a floating point time value as accepted by
129 gmtime() and localtime(), otherwise the current time is used.
131 Optional localtime is a flag that when True, interprets timeval, and
132 returns a date relative to the local timezone instead of UTC, properly
133 taking daylight savings time into account.
135 Optional argument usegmt means that the timezone is written out as
136 an ascii string, not numeric one (so "GMT" instead of "+0000"). This
137 is needed for HTTP, and is only used when localtime==False.
139 # Note: we cannot use strftime() because that honors the locale and RFC
140 # 2822 requires that day and month names be the English abbreviations.
142 timeval
= time
.time()
144 now
= time
.localtime(timeval
)
145 # Calculate timezone offset, based on whether the local zone has
146 # daylight savings time, and whether DST is in effect.
147 if time
.daylight
and now
[-1]:
148 offset
= time
.altzone
150 offset
= time
.timezone
151 hours
, minutes
= divmod(abs(offset
), 3600)
152 # Remember offset is in seconds west of UTC, but the timezone is in
153 # minutes east of UTC, so the signs differ.
158 zone
= '%s%02d%02d' % (sign
, hours
, minutes
// 60)
160 now
= time
.gmtime(timeval
)
161 # Timezone offset is always -0000
166 return '%s, %02d %s %04d %02d:%02d:%02d %s' % (
167 ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][now
[6]],
169 ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
170 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][now
[1] - 1],
171 now
[0], now
[3], now
[4], now
[5],
176 def make_msgid(idstring
=None):
177 """Returns a string suitable for RFC 2822 compliant Message-ID, e.g:
179 <20020201195627.33539.96671@nightshade.la.mastaler.com>
181 Optional idstring if given is a string used to strengthen the
182 uniqueness of the message id.
184 timeval
= time
.time()
185 utcdate
= time
.strftime('%Y%m%d%H%M%S', time
.gmtime(timeval
))
187 randint
= random
.randrange(100000)
191 idstring
= '.' + idstring
192 idhost
= socket
.getfqdn()
193 msgid
= '<%s.%s.%s%s@%s>' % (utcdate
, pid
, randint
, idstring
, idhost
)
198 # These functions are in the standalone mimelib version only because they've
199 # subsequently been fixed in the latest Python versions. We use this to worm
200 # around broken older Pythons.
204 return _parsedate(data
)
207 def parsedate_tz(data
):
210 return _parsedate_tz(data
)
214 addrs
= _AddressList(addr
).addresslist
220 # rfc822.unquote() doesn't properly de-backslash-ify in Python pre-2.3.
222 """Remove quotes from a string."""
224 if str.startswith('"') and str.endswith('"'):
225 return str[1:-1].replace('\\\\', '\\').replace('\\"', '"')
226 if str.startswith('<') and str.endswith('>'):
232 # RFC2231-related functions - parameter encoding and decoding
233 def decode_rfc2231(s
):
234 """Decode string according to RFC 2231"""
235 parts
= s
.split(TICK
, 2)
241 def encode_rfc2231(s
, charset
=None, language
=None):
242 """Encode string according to RFC 2231.
244 If neither charset nor language is given, then s is returned as-is. If
245 charset is given but not language, the string is encoded using the empty
249 s
= urllib
.quote(s
, safe
='')
250 if charset
is None and language
is None:
254 return "%s'%s'%s" % (charset
, language
, s
)
257 rfc2231_continuation
= re
.compile(r
'^(?P<name>\w+)\*((?P<num>[0-9]+)\*?)?$')
259 def decode_params(params
):
260 """Decode parameters list according to RFC 2231.
262 params is a sequence of 2-tuples containing (param name, string value).
264 # Copy params so we don't mess with the original
267 # Map parameter's name to a list of continuations. The values are a
268 # 3-tuple of the continuation number, the string value, and a flag
269 # specifying whether a particular segment is %-encoded.
271 name
, value
= params
.pop(0)
272 new_params
.append((name
, value
))
274 name
, value
= params
.pop(0)
275 if name
.endswith('*'):
279 value
= unquote(value
)
280 mo
= rfc2231_continuation
.match(name
)
282 name
, num
= mo
.group('name', 'num')
285 rfc2231_params
.setdefault(name
, []).append((num
, value
, encoded
))
287 new_params
.append((name
, '"%s"' % quote(value
)))
289 for name
, continuations
in rfc2231_params
.items():
294 # And now append all values in numerical order, converting
295 # %-encodings for the encoded segments. If any of the
296 # continuation names ends in a *, then the entire string, after
297 # decoding segments and concatenating, must have the charset and
298 # language specifiers at the beginning of the string.
299 for num
, s
, encoded
in continuations
:
301 s
= urllib
.unquote(s
)
304 value
= quote(EMPTYSTRING
.join(value
))
306 charset
, language
, value
= decode_rfc2231(value
)
307 new_params
.append((name
, (charset
, language
, '"%s"' % value
)))
309 new_params
.append((name
, '"%s"' % value
))
312 def collapse_rfc2231_value(value
, errors
='replace',
313 fallback_charset
='us-ascii'):
314 if isinstance(value
, tuple):
315 rawval
= unquote(value
[2])
316 charset
= value
[0] or 'us-ascii'
318 return unicode(rawval
, charset
, errors
)
320 # XXX charset is unknown to Python.
321 return unicode(rawval
, fallback_charset
, errors
)
323 return unquote(value
)