1 # Copyright (C) 2001-2006 Python Software Foundation
3 # Contact: email-sig@python.org
5 """Miscellaneous utilities."""
8 'collapse_rfc2231_value',
29 from cStringIO
import StringIO
31 from email
._parseaddr
import quote
32 from email
._parseaddr
import AddressList
as _AddressList
33 from email
._parseaddr
import mktime_tz
35 # We need wormarounds for bugs in these methods in older Pythons (see below)
36 from email
._parseaddr
import parsedate
as _parsedate
37 from email
._parseaddr
import parsedate_tz
as _parsedate_tz
39 from quopri
import decodestring
as _qdecode
41 # Intrapackage imports
42 from email
.encoders
import _bencode
, _qencode
49 specialsre
= re
.compile(r
'[][\\()<>@,:;".]')
50 escapesre
= re
.compile(r
'[][\\()"]')
61 # We can't quite use base64.encodestring() since it tacks on a "courtesy
65 value
= base64
.decodestring(s
)
66 if not s
.endswith('\n') and value
.endswith('\n'):
73 """Replace all line-ending characters with \r\n."""
74 # Fix newlines with no preceding carriage return
75 s
= re
.sub(r
'(?<!\r)\n', CRLF
, s
)
76 # Fix carriage returns with no following newline
77 s
= re
.sub(r
'\r(?!\n)', CRLF
, s
)
83 """The inverse of parseaddr(), this takes a 2-tuple of the form
84 (realname, email_address) and returns the string value suitable
85 for an RFC 2822 From, To or Cc header.
87 If the first element of pair is false, then the second element is
93 if specialsre
.search(name
):
95 name
= escapesre
.sub(r
'\\\g<0>', name
)
96 return '%s%s%s <%s>' % (quotes
, name
, quotes
, address
)
101 def getaddresses(fieldvalues
):
102 """Return a list of (REALNAME, EMAIL) for each fieldvalue."""
103 all
= COMMASPACE
.join(fieldvalues
)
104 a
= _AddressList(all
)
109 ecre
= re
.compile(r
'''
111 (?P<charset>[^?]*?) # non-greedy up to the next ? is the charset
113 (?P<encoding>[qb]) # either a "q" or a "b", case insensitive
115 (?P<atom>.*?) # non-greedy up to the next ?= is the atom
117 ''', re
.VERBOSE | re
.IGNORECASE
)
121 def formatdate(timeval
=None, localtime
=False, usegmt
=False):
122 """Returns a date string as specified by RFC 2822, e.g.:
124 Fri, 09 Nov 2001 01:08:47 -0000
126 Optional timeval if given is a floating point time value as accepted by
127 gmtime() and localtime(), otherwise the current time is used.
129 Optional localtime is a flag that when True, interprets timeval, and
130 returns a date relative to the local timezone instead of UTC, properly
131 taking daylight savings time into account.
133 Optional argument usegmt means that the timezone is written out as
134 an ascii string, not numeric one (so "GMT" instead of "+0000"). This
135 is needed for HTTP, and is only used when localtime==False.
137 # Note: we cannot use strftime() because that honors the locale and RFC
138 # 2822 requires that day and month names be the English abbreviations.
140 timeval
= time
.time()
142 now
= time
.localtime(timeval
)
143 # Calculate timezone offset, based on whether the local zone has
144 # daylight savings time, and whether DST is in effect.
145 if time
.daylight
and now
[-1]:
146 offset
= time
.altzone
148 offset
= time
.timezone
149 hours
, minutes
= divmod(abs(offset
), 3600)
150 # Remember offset is in seconds west of UTC, but the timezone is in
151 # minutes east of UTC, so the signs differ.
156 zone
= '%s%02d%02d' % (sign
, hours
, minutes
// 60)
158 now
= time
.gmtime(timeval
)
159 # Timezone offset is always -0000
164 return '%s, %02d %s %04d %02d:%02d:%02d %s' % (
165 ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][now
[6]],
167 ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
168 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][now
[1] - 1],
169 now
[0], now
[3], now
[4], now
[5],
174 def make_msgid(idstring
=None):
175 """Returns a string suitable for RFC 2822 compliant Message-ID, e.g:
177 <20020201195627.33539.96671@nightshade.la.mastaler.com>
179 Optional idstring if given is a string used to strengthen the
180 uniqueness of the message id.
182 timeval
= time
.time()
183 utcdate
= time
.strftime('%Y%m%d%H%M%S', time
.gmtime(timeval
))
185 randint
= random
.randrange(100000)
189 idstring
= '.' + idstring
190 idhost
= socket
.getfqdn()
191 msgid
= '<%s.%s.%s%s@%s>' % (utcdate
, pid
, randint
, idstring
, idhost
)
196 # These functions are in the standalone mimelib version only because they've
197 # subsequently been fixed in the latest Python versions. We use this to worm
198 # around broken older Pythons.
202 return _parsedate(data
)
205 def parsedate_tz(data
):
208 return _parsedate_tz(data
)
212 addrs
= _AddressList(addr
).addresslist
218 # rfc822.unquote() doesn't properly de-backslash-ify in Python pre-2.3.
220 """Remove quotes from a string."""
222 if str.startswith('"') and str.endswith('"'):
223 return str[1:-1].replace('\\\\', '\\').replace('\\"', '"')
224 if str.startswith('<') and str.endswith('>'):
230 # RFC2231-related functions - parameter encoding and decoding
231 def decode_rfc2231(s
):
232 """Decode string according to RFC 2231"""
234 parts
= s
.split("'", 2)
236 return None, None, urllib
.unquote(s
)
237 charset
, language
, s
= parts
238 return charset
, language
, urllib
.unquote(s
)
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 (content type, string value).
265 # maps parameter's name to a list of continuations
267 # params is a sequence of 2-tuples containing (content_type, string value)
268 name
, value
= params
[0]
269 new_params
.append((name
, value
))
270 # Cycle through each of the rest of the parameters.
271 for name
, value
in params
[1:]:
272 value
= unquote(value
)
273 mo
= rfc2231_continuation
.match(name
)
275 name
, num
= mo
.group('name', 'num')
278 rfc2231_param1
= rfc2231_params
.setdefault(name
, [])
279 rfc2231_param1
.append((num
, value
))
281 new_params
.append((name
, '"%s"' % quote(value
)))
283 for name
, continuations
in rfc2231_params
.items():
287 # And now append all values in num order
288 for num
, continuation
in continuations
:
289 value
.append(continuation
)
290 charset
, language
, value
= decode_rfc2231(EMPTYSTRING
.join(value
))
292 (name
, (charset
, language
, '"%s"' % quote(value
))))
295 def collapse_rfc2231_value(value
, errors
='replace',
296 fallback_charset
='us-ascii'):
297 if isinstance(value
, tuple):
298 rawval
= unquote(value
[2])
299 charset
= value
[0] or 'us-ascii'
301 return unicode(rawval
, charset
, errors
)
303 # XXX charset is unknown to Python.
304 return unicode(rawval
, fallback_charset
, errors
)
306 return unquote(value
)