2002-04-10 18:01:31 -03:00
|
|
|
|
# Copyright (C) 2001,2002 Python Software Foundation
|
2001-09-23 00:17:28 -03:00
|
|
|
|
# Author: barry@zope.com (Barry Warsaw)
|
|
|
|
|
|
|
|
|
|
"""Miscellaneous utilities.
|
|
|
|
|
"""
|
|
|
|
|
|
2001-11-09 13:45:48 -04:00
|
|
|
|
import time
|
2002-04-10 18:01:31 -03:00
|
|
|
|
import socket
|
2001-09-23 00:17:28 -03:00
|
|
|
|
import re
|
2002-04-10 18:01:31 -03:00
|
|
|
|
import random
|
|
|
|
|
import os
|
|
|
|
|
import warnings
|
|
|
|
|
from cStringIO import StringIO
|
|
|
|
|
from types import ListType
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-04-10 18:01:31 -03:00
|
|
|
|
from rfc822 import unquote, quote
|
2002-04-12 17:50:05 -03:00
|
|
|
|
from rfc822 import AddressList as _AddressList
|
2002-04-10 18:01:31 -03:00
|
|
|
|
from rfc822 import mktime_tz
|
|
|
|
|
|
|
|
|
|
# We need wormarounds for bugs in these methods in older Pythons (see below)
|
|
|
|
|
from rfc822 import parsedate as _parsedate
|
|
|
|
|
from rfc822 import parsedate_tz as _parsedate_tz
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
2002-05-19 20:44:19 -03:00
|
|
|
|
try:
|
|
|
|
|
from quopri import decodestring as _qdecode
|
|
|
|
|
except ImportError:
|
|
|
|
|
# Python 2.1 doesn't have quopri.decodestring()
|
|
|
|
|
def _qdecode(s):
|
|
|
|
|
import quopri as _quopri
|
|
|
|
|
|
|
|
|
|
if not s:
|
|
|
|
|
return s
|
|
|
|
|
hasnewline = (s[-1] == '\n')
|
|
|
|
|
infp = StringIO(s)
|
|
|
|
|
outfp = StringIO()
|
|
|
|
|
_quopri.decode(infp, outfp)
|
|
|
|
|
value = outfp.getvalue()
|
|
|
|
|
if not hasnewline and value[-1] =='\n':
|
|
|
|
|
return value[:-1]
|
|
|
|
|
return value
|
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
import base64
|
|
|
|
|
|
|
|
|
|
# Intrapackage imports
|
2002-06-02 16:07:16 -03:00
|
|
|
|
from email.Encoders import _bencode, _qencode
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
|
|
|
|
COMMASPACE = ', '
|
|
|
|
|
UEMPTYSTRING = u''
|
2002-04-10 18:01:31 -03:00
|
|
|
|
CRLF = '\r\n'
|
|
|
|
|
|
|
|
|
|
specialsre = re.compile(r'[][\()<>@,:;".]')
|
|
|
|
|
escapesre = re.compile(r'[][\()"]')
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
|
|
|
|
|
2001-10-04 14:05:11 -03:00
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
# Helpers
|
|
|
|
|
|
|
|
|
|
def _identity(s):
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _bdecode(s):
|
|
|
|
|
if not s:
|
|
|
|
|
return s
|
|
|
|
|
# We can't quite use base64.encodestring() since it tacks on a "courtesy
|
|
|
|
|
# newline". Blech!
|
|
|
|
|
if not s:
|
|
|
|
|
return s
|
|
|
|
|
hasnewline = (s[-1] == '\n')
|
|
|
|
|
value = base64.decodestring(s)
|
|
|
|
|
if not hasnewline and value[-1] == '\n':
|
|
|
|
|
return value[:-1]
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
def fix_eols(s):
|
|
|
|
|
"""Replace all line-ending characters with \r\n."""
|
|
|
|
|
# Fix newlines with no preceding carriage return
|
|
|
|
|
s = re.sub(r'(?<!\r)\n', CRLF, s)
|
|
|
|
|
# Fix carriage returns with no following newline
|
|
|
|
|
s = re.sub(r'\r(?!\n)', CRLF, s)
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def formataddr(pair):
|
|
|
|
|
"""The inverse of parseaddr(), this takes a 2-tuple of the form
|
|
|
|
|
(realname, email_address) and returns the string value suitable
|
|
|
|
|
for an RFC 2822 From:, To: or Cc:.
|
2002-05-23 12:15:30 -03:00
|
|
|
|
|
2002-04-10 18:01:31 -03:00
|
|
|
|
If the first element of pair is false, then the second element is
|
|
|
|
|
returned unmodified.
|
|
|
|
|
"""
|
|
|
|
|
name, address = pair
|
|
|
|
|
if name:
|
|
|
|
|
quotes = ''
|
|
|
|
|
if specialsre.search(name):
|
|
|
|
|
quotes = '"'
|
|
|
|
|
name = escapesre.sub(r'\\\g<0>', name)
|
|
|
|
|
return '%s%s%s <%s>' % (quotes, name, quotes, address)
|
|
|
|
|
return address
|
|
|
|
|
|
|
|
|
|
# For backwards compatibility
|
|
|
|
|
def dump_address_pair(pair):
|
|
|
|
|
warnings.warn('Use email.Utils.formataddr() instead',
|
|
|
|
|
DeprecationWarning, 2)
|
|
|
|
|
return formataddr(pair)
|
|
|
|
|
|
|
|
|
|
|
2001-10-04 14:05:11 -03:00
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
def getaddresses(fieldvalues):
|
|
|
|
|
"""Return a list of (REALNAME, EMAIL) for each fieldvalue."""
|
|
|
|
|
all = COMMASPACE.join(fieldvalues)
|
2002-04-12 17:50:05 -03:00
|
|
|
|
a = _AddressList(all)
|
2002-05-21 22:52:10 -03:00
|
|
|
|
return a.addresslist
|
2001-09-23 00:17:28 -03:00
|
|
|
|
|
|
|
|
|
|
2001-10-04 14:05:11 -03:00
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
ecre = re.compile(r'''
|
|
|
|
|
=\? # literal =?
|
|
|
|
|
(?P<charset>[^?]*?) # non-greedy up to the next ? is the charset
|
|
|
|
|
\? # literal ?
|
|
|
|
|
(?P<encoding>[qb]) # either a "q" or a "b", case insensitive
|
|
|
|
|
\? # literal ?
|
|
|
|
|
(?P<atom>.*?) # non-greedy up to the next ?= is the atom
|
|
|
|
|
\?= # literal ?=
|
|
|
|
|
''', re.VERBOSE | re.IGNORECASE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def decode(s):
|
2002-04-10 18:01:31 -03:00
|
|
|
|
"""Return a decoded string according to RFC 2047, as a unicode string.
|
|
|
|
|
|
|
|
|
|
NOTE: This function is deprecated. Use Header.decode_header() instead.
|
|
|
|
|
"""
|
|
|
|
|
warnings.warn('Use Header.decode_header() instead.', DeprecationWarning, 2)
|
|
|
|
|
# Intra-package import here to avoid circular import problems.
|
2002-06-02 16:07:16 -03:00
|
|
|
|
from email.Header import decode_header
|
2002-04-10 18:01:31 -03:00
|
|
|
|
L = decode_header(s)
|
|
|
|
|
if not isinstance(L, ListType):
|
|
|
|
|
# s wasn't decoded
|
|
|
|
|
return s
|
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
rtn = []
|
2002-04-10 18:01:31 -03:00
|
|
|
|
for atom, charset in L:
|
|
|
|
|
if charset is None:
|
|
|
|
|
rtn.append(atom)
|
2001-09-23 00:17:28 -03:00
|
|
|
|
else:
|
2002-04-10 18:01:31 -03:00
|
|
|
|
# Convert the string to Unicode using the given encoding. Leave
|
|
|
|
|
# Unicode conversion errors to strict.
|
|
|
|
|
rtn.append(unicode(atom, charset))
|
2001-09-23 00:17:28 -03:00
|
|
|
|
# Now that we've decoded everything, we just need to join all the parts
|
|
|
|
|
# together into the final string.
|
|
|
|
|
return UEMPTYSTRING.join(rtn)
|
|
|
|
|
|
|
|
|
|
|
2001-10-04 14:05:11 -03:00
|
|
|
|
|
2001-09-23 00:17:28 -03:00
|
|
|
|
def encode(s, charset='iso-8859-1', encoding='q'):
|
|
|
|
|
"""Encode a string according to RFC 2047."""
|
2002-04-10 18:01:31 -03:00
|
|
|
|
warnings.warn('Use Header.Header.encode() instead.', DeprecationWarning, 2)
|
2001-12-03 15:26:40 -04:00
|
|
|
|
encoding = encoding.lower()
|
|
|
|
|
if encoding == 'q':
|
2001-09-23 00:17:28 -03:00
|
|
|
|
estr = _qencode(s)
|
2001-12-03 15:26:40 -04:00
|
|
|
|
elif encoding == 'b':
|
2001-09-23 00:17:28 -03:00
|
|
|
|
estr = _bencode(s)
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError, 'Illegal encoding code: ' + encoding
|
2001-12-03 15:26:40 -04:00
|
|
|
|
return '=?%s?%s?%s?=' % (charset.lower(), encoding, estr)
|
2001-11-09 12:59:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def formatdate(timeval=None, localtime=0):
|
2001-11-09 13:07:28 -04:00
|
|
|
|
"""Returns a date string as specified by RFC 2822, e.g.:
|
2001-11-09 12:59:56 -04:00
|
|
|
|
|
|
|
|
|
Fri, 09 Nov 2001 01:08:47 -0000
|
|
|
|
|
|
2001-11-09 13:07:28 -04:00
|
|
|
|
Optional timeval if given is a floating point time value as accepted by
|
|
|
|
|
gmtime() and localtime(), otherwise the current time is used.
|
|
|
|
|
|
|
|
|
|
Optional localtime is a flag that when true, interprets timeval, and
|
|
|
|
|
returns a date relative to the local timezone instead of UTC, properly
|
|
|
|
|
taking daylight savings time into account.
|
2001-11-09 12:59:56 -04:00
|
|
|
|
"""
|
|
|
|
|
# Note: we cannot use strftime() because that honors the locale and RFC
|
|
|
|
|
# 2822 requires that day and month names be the English abbreviations.
|
|
|
|
|
if timeval is None:
|
|
|
|
|
timeval = time.time()
|
|
|
|
|
if localtime:
|
|
|
|
|
now = time.localtime(timeval)
|
|
|
|
|
# Calculate timezone offset, based on whether the local zone has
|
|
|
|
|
# daylight savings time, and whether DST is in effect.
|
|
|
|
|
if time.daylight and now[-1]:
|
|
|
|
|
offset = time.altzone
|
|
|
|
|
else:
|
|
|
|
|
offset = time.timezone
|
2001-11-19 14:36:43 -04:00
|
|
|
|
hours, minutes = divmod(abs(offset), 3600)
|
|
|
|
|
# Remember offset is in seconds west of UTC, but the timezone is in
|
|
|
|
|
# minutes east of UTC, so the signs differ.
|
|
|
|
|
if offset > 0:
|
|
|
|
|
sign = '-'
|
|
|
|
|
else:
|
|
|
|
|
sign = '+'
|
|
|
|
|
zone = '%s%02d%02d' % (sign, hours, minutes / 60)
|
2001-11-09 12:59:56 -04:00
|
|
|
|
else:
|
|
|
|
|
now = time.gmtime(timeval)
|
|
|
|
|
# Timezone offset is always -0000
|
|
|
|
|
zone = '-0000'
|
|
|
|
|
return '%s, %02d %s %04d %02d:%02d:%02d %s' % (
|
|
|
|
|
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][now[6]],
|
|
|
|
|
now[2],
|
|
|
|
|
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
|
|
|
|
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][now[1] - 1],
|
|
|
|
|
now[0], now[3], now[4], now[5],
|
|
|
|
|
zone)
|
2002-04-10 18:01:31 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_msgid(idstring=None):
|
|
|
|
|
"""Returns a string suitable for RFC 2822 compliant Message-ID:, e.g:
|
|
|
|
|
|
|
|
|
|
<20020201195627.33539.96671@nightshade.la.mastaler.com>
|
|
|
|
|
|
|
|
|
|
Optional idstring if given is a string used to strengthen the
|
|
|
|
|
uniqueness of the Message-ID, otherwise an empty string is used.
|
|
|
|
|
"""
|
|
|
|
|
timeval = time.time()
|
|
|
|
|
utcdate = time.strftime('%Y%m%d%H%M%S', time.gmtime(timeval))
|
|
|
|
|
pid = os.getpid()
|
|
|
|
|
randint = random.randrange(100000)
|
|
|
|
|
if idstring is None:
|
|
|
|
|
idstring = ''
|
|
|
|
|
else:
|
|
|
|
|
idstring = '.' + idstring
|
|
|
|
|
idhost = socket.getfqdn()
|
|
|
|
|
msgid = '<%s.%s.%s%s@%s>' % (utcdate, pid, randint, idstring, idhost)
|
|
|
|
|
return msgid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# These functions are in the standalone mimelib version only because they've
|
|
|
|
|
# subsequently been fixed in the latest Python versions. We use this to worm
|
|
|
|
|
# around broken older Pythons.
|
|
|
|
|
def parsedate(data):
|
|
|
|
|
if not data:
|
|
|
|
|
return None
|
|
|
|
|
return _parsedate(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parsedate_tz(data):
|
|
|
|
|
if not data:
|
|
|
|
|
return None
|
|
|
|
|
return _parsedate_tz(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parseaddr(addr):
|
2002-04-15 19:00:25 -03:00
|
|
|
|
addrs = _AddressList(addr).addresslist
|
|
|
|
|
if not addrs:
|
2002-04-10 18:01:31 -03:00
|
|
|
|
return '', ''
|
2002-04-15 19:00:25 -03:00
|
|
|
|
return addrs[0]
|