blob: 0c57392206c81a0331d391ba312b905165f6d654 [file] [log] [blame]
Barry Warsaw409a4c02002-04-10 21:01:31 +00001# Copyright (C) 2001,2002 Python Software Foundation
Barry Warsawba925802001-09-23 03:17:28 +00002# Author: barry@zope.com (Barry Warsaw)
3
4"""Miscellaneous utilities.
5"""
6
Barry Warsaw9aa64352001-11-09 17:45:48 +00007import time
Barry Warsaw409a4c02002-04-10 21:01:31 +00008import socket
Barry Warsawba925802001-09-23 03:17:28 +00009import re
Barry Warsaw409a4c02002-04-10 21:01:31 +000010import random
11import os
12import warnings
13from cStringIO import StringIO
14from types import ListType
Barry Warsawba925802001-09-23 03:17:28 +000015
Barry Warsaw409a4c02002-04-10 21:01:31 +000016from rfc822 import unquote, quote
Barry Warsawe1df15c2002-04-12 20:50:05 +000017from rfc822 import AddressList as _AddressList
Barry Warsaw409a4c02002-04-10 21:01:31 +000018from rfc822 import mktime_tz
19
20# We need wormarounds for bugs in these methods in older Pythons (see below)
21from rfc822 import parsedate as _parsedate
22from rfc822 import parsedate_tz as _parsedate_tz
Barry Warsawba925802001-09-23 03:17:28 +000023
Barry Warsaw8c1aac22002-05-19 23:44:19 +000024try:
25 from quopri import decodestring as _qdecode
26except ImportError:
27 # Python 2.1 doesn't have quopri.decodestring()
28 def _qdecode(s):
29 import quopri as _quopri
30
31 if not s:
32 return s
33 hasnewline = (s[-1] == '\n')
34 infp = StringIO(s)
35 outfp = StringIO()
36 _quopri.decode(infp, outfp)
37 value = outfp.getvalue()
38 if not hasnewline and value[-1] =='\n':
39 return value[:-1]
40 return value
41
Barry Warsawba925802001-09-23 03:17:28 +000042import base64
43
44# Intrapackage imports
45from Encoders import _bencode, _qencode
46
47COMMASPACE = ', '
48UEMPTYSTRING = u''
Barry Warsaw409a4c02002-04-10 21:01:31 +000049CRLF = '\r\n'
50
51specialsre = re.compile(r'[][\()<>@,:;".]')
52escapesre = re.compile(r'[][\()"]')
Barry Warsawba925802001-09-23 03:17:28 +000053
54
Barry Warsawe968ead2001-10-04 17:05:11 +000055
Barry Warsawba925802001-09-23 03:17:28 +000056# Helpers
57
58def _identity(s):
59 return s
60
61
62def _bdecode(s):
63 if not s:
64 return s
65 # We can't quite use base64.encodestring() since it tacks on a "courtesy
66 # newline". Blech!
67 if not s:
68 return s
69 hasnewline = (s[-1] == '\n')
70 value = base64.decodestring(s)
71 if not hasnewline and value[-1] == '\n':
72 return value[:-1]
73 return value
74
75
Barry Warsawe968ead2001-10-04 17:05:11 +000076
Barry Warsaw409a4c02002-04-10 21:01:31 +000077def fix_eols(s):
78 """Replace all line-ending characters with \r\n."""
79 # Fix newlines with no preceding carriage return
80 s = re.sub(r'(?<!\r)\n', CRLF, s)
81 # Fix carriage returns with no following newline
82 s = re.sub(r'\r(?!\n)', CRLF, s)
83 return s
84
85
86
87def formataddr(pair):
88 """The inverse of parseaddr(), this takes a 2-tuple of the form
89 (realname, email_address) and returns the string value suitable
90 for an RFC 2822 From:, To: or Cc:.
Tim Peters8ac14952002-05-23 15:15:30 +000091
Barry Warsaw409a4c02002-04-10 21:01:31 +000092 If the first element of pair is false, then the second element is
93 returned unmodified.
94 """
95 name, address = pair
96 if name:
97 quotes = ''
98 if specialsre.search(name):
99 quotes = '"'
100 name = escapesre.sub(r'\\\g<0>', name)
101 return '%s%s%s <%s>' % (quotes, name, quotes, address)
102 return address
103
104# For backwards compatibility
105def dump_address_pair(pair):
106 warnings.warn('Use email.Utils.formataddr() instead',
107 DeprecationWarning, 2)
108 return formataddr(pair)
109
110
111
Barry Warsawba925802001-09-23 03:17:28 +0000112def getaddresses(fieldvalues):
113 """Return a list of (REALNAME, EMAIL) for each fieldvalue."""
114 all = COMMASPACE.join(fieldvalues)
Barry Warsawe1df15c2002-04-12 20:50:05 +0000115 a = _AddressList(all)
Barry Warsaw4be9ecc2002-05-22 01:52:10 +0000116 return a.addresslist
Barry Warsawba925802001-09-23 03:17:28 +0000117
118
Barry Warsawe968ead2001-10-04 17:05:11 +0000119
Barry Warsawba925802001-09-23 03:17:28 +0000120ecre = re.compile(r'''
121 =\? # literal =?
122 (?P<charset>[^?]*?) # non-greedy up to the next ? is the charset
123 \? # literal ?
124 (?P<encoding>[qb]) # either a "q" or a "b", case insensitive
125 \? # literal ?
126 (?P<atom>.*?) # non-greedy up to the next ?= is the atom
127 \?= # literal ?=
128 ''', re.VERBOSE | re.IGNORECASE)
129
130
131def decode(s):
Barry Warsaw409a4c02002-04-10 21:01:31 +0000132 """Return a decoded string according to RFC 2047, as a unicode string.
133
134 NOTE: This function is deprecated. Use Header.decode_header() instead.
135 """
136 warnings.warn('Use Header.decode_header() instead.', DeprecationWarning, 2)
137 # Intra-package import here to avoid circular import problems.
138 from Header import decode_header
139 L = decode_header(s)
140 if not isinstance(L, ListType):
141 # s wasn't decoded
142 return s
143
Barry Warsawba925802001-09-23 03:17:28 +0000144 rtn = []
Barry Warsaw409a4c02002-04-10 21:01:31 +0000145 for atom, charset in L:
146 if charset is None:
147 rtn.append(atom)
Barry Warsawba925802001-09-23 03:17:28 +0000148 else:
Barry Warsaw409a4c02002-04-10 21:01:31 +0000149 # Convert the string to Unicode using the given encoding. Leave
150 # Unicode conversion errors to strict.
151 rtn.append(unicode(atom, charset))
Barry Warsawba925802001-09-23 03:17:28 +0000152 # Now that we've decoded everything, we just need to join all the parts
153 # together into the final string.
154 return UEMPTYSTRING.join(rtn)
155
156
Barry Warsawe968ead2001-10-04 17:05:11 +0000157
Barry Warsawba925802001-09-23 03:17:28 +0000158def encode(s, charset='iso-8859-1', encoding='q'):
159 """Encode a string according to RFC 2047."""
Barry Warsaw409a4c02002-04-10 21:01:31 +0000160 warnings.warn('Use Header.Header.encode() instead.', DeprecationWarning, 2)
Barry Warsawc44d2c52001-12-03 19:26:40 +0000161 encoding = encoding.lower()
162 if encoding == 'q':
Barry Warsawba925802001-09-23 03:17:28 +0000163 estr = _qencode(s)
Barry Warsawc44d2c52001-12-03 19:26:40 +0000164 elif encoding == 'b':
Barry Warsawba925802001-09-23 03:17:28 +0000165 estr = _bencode(s)
166 else:
167 raise ValueError, 'Illegal encoding code: ' + encoding
Barry Warsawc44d2c52001-12-03 19:26:40 +0000168 return '=?%s?%s?%s?=' % (charset.lower(), encoding, estr)
Barry Warsawaa79f4d2001-11-09 16:59:56 +0000169
170
171
172def formatdate(timeval=None, localtime=0):
Barry Warsaw9cff0e62001-11-09 17:07:28 +0000173 """Returns a date string as specified by RFC 2822, e.g.:
Barry Warsawaa79f4d2001-11-09 16:59:56 +0000174
175 Fri, 09 Nov 2001 01:08:47 -0000
176
Barry Warsaw9cff0e62001-11-09 17:07:28 +0000177 Optional timeval if given is a floating point time value as accepted by
178 gmtime() and localtime(), otherwise the current time is used.
179
180 Optional localtime is a flag that when true, interprets timeval, and
181 returns a date relative to the local timezone instead of UTC, properly
182 taking daylight savings time into account.
Barry Warsawaa79f4d2001-11-09 16:59:56 +0000183 """
184 # Note: we cannot use strftime() because that honors the locale and RFC
185 # 2822 requires that day and month names be the English abbreviations.
186 if timeval is None:
187 timeval = time.time()
188 if localtime:
189 now = time.localtime(timeval)
190 # Calculate timezone offset, based on whether the local zone has
191 # daylight savings time, and whether DST is in effect.
192 if time.daylight and now[-1]:
193 offset = time.altzone
194 else:
195 offset = time.timezone
Barry Warsawe5739a62001-11-19 18:36:43 +0000196 hours, minutes = divmod(abs(offset), 3600)
197 # Remember offset is in seconds west of UTC, but the timezone is in
198 # minutes east of UTC, so the signs differ.
199 if offset > 0:
200 sign = '-'
201 else:
202 sign = '+'
203 zone = '%s%02d%02d' % (sign, hours, minutes / 60)
Barry Warsawaa79f4d2001-11-09 16:59:56 +0000204 else:
205 now = time.gmtime(timeval)
206 # Timezone offset is always -0000
207 zone = '-0000'
208 return '%s, %02d %s %04d %02d:%02d:%02d %s' % (
209 ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][now[6]],
210 now[2],
211 ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
212 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][now[1] - 1],
213 now[0], now[3], now[4], now[5],
214 zone)
Barry Warsaw409a4c02002-04-10 21:01:31 +0000215
216
217
218def make_msgid(idstring=None):
219 """Returns a string suitable for RFC 2822 compliant Message-ID:, e.g:
220
221 <20020201195627.33539.96671@nightshade.la.mastaler.com>
222
223 Optional idstring if given is a string used to strengthen the
224 uniqueness of the Message-ID, otherwise an empty string is used.
225 """
226 timeval = time.time()
227 utcdate = time.strftime('%Y%m%d%H%M%S', time.gmtime(timeval))
228 pid = os.getpid()
229 randint = random.randrange(100000)
230 if idstring is None:
231 idstring = ''
232 else:
233 idstring = '.' + idstring
234 idhost = socket.getfqdn()
235 msgid = '<%s.%s.%s%s@%s>' % (utcdate, pid, randint, idstring, idhost)
236 return msgid
237
238
239
240# These functions are in the standalone mimelib version only because they've
241# subsequently been fixed in the latest Python versions. We use this to worm
242# around broken older Pythons.
243def parsedate(data):
244 if not data:
245 return None
246 return _parsedate(data)
247
248
249def parsedate_tz(data):
250 if not data:
251 return None
252 return _parsedate_tz(data)
253
254
255def parseaddr(addr):
Barry Warsaw24fd0252002-04-15 22:00:25 +0000256 addrs = _AddressList(addr).addresslist
257 if not addrs:
Barry Warsaw409a4c02002-04-10 21:01:31 +0000258 return '', ''
Barry Warsaw24fd0252002-04-15 22:00:25 +0000259 return addrs[0]