blob: f8e48ef4f9498e034672d23d7e6cb94891efe602 [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
23from rfc822 import parseaddr as _parseaddr
Barry Warsawba925802001-09-23 03:17:28 +000024
25from quopri import decodestring as _qdecode
26import base64
27
28# Intrapackage imports
29from Encoders import _bencode, _qencode
30
31COMMASPACE = ', '
32UEMPTYSTRING = u''
Barry Warsaw409a4c02002-04-10 21:01:31 +000033CRLF = '\r\n'
34
35specialsre = re.compile(r'[][\()<>@,:;".]')
36escapesre = re.compile(r'[][\()"]')
Barry Warsawba925802001-09-23 03:17:28 +000037
38
Barry Warsawe968ead2001-10-04 17:05:11 +000039
Barry Warsawba925802001-09-23 03:17:28 +000040# Helpers
41
42def _identity(s):
43 return s
44
45
46def _bdecode(s):
47 if not s:
48 return s
49 # We can't quite use base64.encodestring() since it tacks on a "courtesy
50 # newline". Blech!
51 if not s:
52 return s
53 hasnewline = (s[-1] == '\n')
54 value = base64.decodestring(s)
55 if not hasnewline and value[-1] == '\n':
56 return value[:-1]
57 return value
58
59
Barry Warsawe968ead2001-10-04 17:05:11 +000060
Barry Warsaw409a4c02002-04-10 21:01:31 +000061def fix_eols(s):
62 """Replace all line-ending characters with \r\n."""
63 # Fix newlines with no preceding carriage return
64 s = re.sub(r'(?<!\r)\n', CRLF, s)
65 # Fix carriage returns with no following newline
66 s = re.sub(r'\r(?!\n)', CRLF, s)
67 return s
68
69
70
71def formataddr(pair):
72 """The inverse of parseaddr(), this takes a 2-tuple of the form
73 (realname, email_address) and returns the string value suitable
74 for an RFC 2822 From:, To: or Cc:.
75
76 If the first element of pair is false, then the second element is
77 returned unmodified.
78 """
79 name, address = pair
80 if name:
81 quotes = ''
82 if specialsre.search(name):
83 quotes = '"'
84 name = escapesre.sub(r'\\\g<0>', name)
85 return '%s%s%s <%s>' % (quotes, name, quotes, address)
86 return address
87
88# For backwards compatibility
89def dump_address_pair(pair):
90 warnings.warn('Use email.Utils.formataddr() instead',
91 DeprecationWarning, 2)
92 return formataddr(pair)
93
94
95
Barry Warsawba925802001-09-23 03:17:28 +000096def getaddresses(fieldvalues):
97 """Return a list of (REALNAME, EMAIL) for each fieldvalue."""
98 all = COMMASPACE.join(fieldvalues)
Barry Warsawe1df15c2002-04-12 20:50:05 +000099 a = _AddressList(all)
Barry Warsawba925802001-09-23 03:17:28 +0000100 return a.getaddrlist()
101
102
Barry Warsawe968ead2001-10-04 17:05:11 +0000103
Barry Warsawba925802001-09-23 03:17:28 +0000104ecre = re.compile(r'''
105 =\? # literal =?
106 (?P<charset>[^?]*?) # non-greedy up to the next ? is the charset
107 \? # literal ?
108 (?P<encoding>[qb]) # either a "q" or a "b", case insensitive
109 \? # literal ?
110 (?P<atom>.*?) # non-greedy up to the next ?= is the atom
111 \?= # literal ?=
112 ''', re.VERBOSE | re.IGNORECASE)
113
114
115def decode(s):
Barry Warsaw409a4c02002-04-10 21:01:31 +0000116 """Return a decoded string according to RFC 2047, as a unicode string.
117
118 NOTE: This function is deprecated. Use Header.decode_header() instead.
119 """
120 warnings.warn('Use Header.decode_header() instead.', DeprecationWarning, 2)
121 # Intra-package import here to avoid circular import problems.
122 from Header import decode_header
123 L = decode_header(s)
124 if not isinstance(L, ListType):
125 # s wasn't decoded
126 return s
127
Barry Warsawba925802001-09-23 03:17:28 +0000128 rtn = []
Barry Warsaw409a4c02002-04-10 21:01:31 +0000129 for atom, charset in L:
130 if charset is None:
131 rtn.append(atom)
Barry Warsawba925802001-09-23 03:17:28 +0000132 else:
Barry Warsaw409a4c02002-04-10 21:01:31 +0000133 # Convert the string to Unicode using the given encoding. Leave
134 # Unicode conversion errors to strict.
135 rtn.append(unicode(atom, charset))
Barry Warsawba925802001-09-23 03:17:28 +0000136 # Now that we've decoded everything, we just need to join all the parts
137 # together into the final string.
138 return UEMPTYSTRING.join(rtn)
139
140
Barry Warsawe968ead2001-10-04 17:05:11 +0000141
Barry Warsawba925802001-09-23 03:17:28 +0000142def encode(s, charset='iso-8859-1', encoding='q'):
143 """Encode a string according to RFC 2047."""
Barry Warsaw409a4c02002-04-10 21:01:31 +0000144 warnings.warn('Use Header.Header.encode() instead.', DeprecationWarning, 2)
Barry Warsawc44d2c52001-12-03 19:26:40 +0000145 encoding = encoding.lower()
146 if encoding == 'q':
Barry Warsawba925802001-09-23 03:17:28 +0000147 estr = _qencode(s)
Barry Warsawc44d2c52001-12-03 19:26:40 +0000148 elif encoding == 'b':
Barry Warsawba925802001-09-23 03:17:28 +0000149 estr = _bencode(s)
150 else:
151 raise ValueError, 'Illegal encoding code: ' + encoding
Barry Warsawc44d2c52001-12-03 19:26:40 +0000152 return '=?%s?%s?%s?=' % (charset.lower(), encoding, estr)
Barry Warsawaa79f4d2001-11-09 16:59:56 +0000153
154
155
156def formatdate(timeval=None, localtime=0):
Barry Warsaw9cff0e62001-11-09 17:07:28 +0000157 """Returns a date string as specified by RFC 2822, e.g.:
Barry Warsawaa79f4d2001-11-09 16:59:56 +0000158
159 Fri, 09 Nov 2001 01:08:47 -0000
160
Barry Warsaw9cff0e62001-11-09 17:07:28 +0000161 Optional timeval if given is a floating point time value as accepted by
162 gmtime() and localtime(), otherwise the current time is used.
163
164 Optional localtime is a flag that when true, interprets timeval, and
165 returns a date relative to the local timezone instead of UTC, properly
166 taking daylight savings time into account.
Barry Warsawaa79f4d2001-11-09 16:59:56 +0000167 """
168 # Note: we cannot use strftime() because that honors the locale and RFC
169 # 2822 requires that day and month names be the English abbreviations.
170 if timeval is None:
171 timeval = time.time()
172 if localtime:
173 now = time.localtime(timeval)
174 # Calculate timezone offset, based on whether the local zone has
175 # daylight savings time, and whether DST is in effect.
176 if time.daylight and now[-1]:
177 offset = time.altzone
178 else:
179 offset = time.timezone
Barry Warsawe5739a62001-11-19 18:36:43 +0000180 hours, minutes = divmod(abs(offset), 3600)
181 # Remember offset is in seconds west of UTC, but the timezone is in
182 # minutes east of UTC, so the signs differ.
183 if offset > 0:
184 sign = '-'
185 else:
186 sign = '+'
187 zone = '%s%02d%02d' % (sign, hours, minutes / 60)
Barry Warsawaa79f4d2001-11-09 16:59:56 +0000188 else:
189 now = time.gmtime(timeval)
190 # Timezone offset is always -0000
191 zone = '-0000'
192 return '%s, %02d %s %04d %02d:%02d:%02d %s' % (
193 ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][now[6]],
194 now[2],
195 ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
196 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][now[1] - 1],
197 now[0], now[3], now[4], now[5],
198 zone)
Barry Warsaw409a4c02002-04-10 21:01:31 +0000199
200
201
202def make_msgid(idstring=None):
203 """Returns a string suitable for RFC 2822 compliant Message-ID:, e.g:
204
205 <20020201195627.33539.96671@nightshade.la.mastaler.com>
206
207 Optional idstring if given is a string used to strengthen the
208 uniqueness of the Message-ID, otherwise an empty string is used.
209 """
210 timeval = time.time()
211 utcdate = time.strftime('%Y%m%d%H%M%S', time.gmtime(timeval))
212 pid = os.getpid()
213 randint = random.randrange(100000)
214 if idstring is None:
215 idstring = ''
216 else:
217 idstring = '.' + idstring
218 idhost = socket.getfqdn()
219 msgid = '<%s.%s.%s%s@%s>' % (utcdate, pid, randint, idstring, idhost)
220 return msgid
221
222
223
224# These functions are in the standalone mimelib version only because they've
225# subsequently been fixed in the latest Python versions. We use this to worm
226# around broken older Pythons.
227def parsedate(data):
228 if not data:
229 return None
230 return _parsedate(data)
231
232
233def parsedate_tz(data):
234 if not data:
235 return None
236 return _parsedate_tz(data)
237
238
239def parseaddr(addr):
240 realname, emailaddr = _parseaddr(addr)
241 if realname == '' and emailaddr is None:
242 return '', ''
243 return realname, emailaddr