Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 1 | # Copyright (C) 2001,2002 Python Software Foundation |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 2 | # Author: barry@zope.com (Barry Warsaw) |
| 3 | |
| 4 | """Miscellaneous utilities. |
| 5 | """ |
| 6 | |
Barry Warsaw | 9aa6435 | 2001-11-09 17:45:48 +0000 | [diff] [blame] | 7 | import time |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 8 | import socket |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 9 | import re |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 10 | import random |
| 11 | import os |
| 12 | import warnings |
| 13 | from cStringIO import StringIO |
| 14 | from types import ListType |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 15 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 16 | from rfc822 import unquote, quote |
Barry Warsaw | e1df15c | 2002-04-12 20:50:05 +0000 | [diff] [blame] | 17 | from rfc822 import AddressList as _AddressList |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 18 | from rfc822 import mktime_tz |
| 19 | |
| 20 | # We need wormarounds for bugs in these methods in older Pythons (see below) |
| 21 | from rfc822 import parsedate as _parsedate |
| 22 | from rfc822 import parsedate_tz as _parsedate_tz |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 23 | |
Barry Warsaw | 8c1aac2 | 2002-05-19 23:44:19 +0000 | [diff] [blame] | 24 | try: |
| 25 | from quopri import decodestring as _qdecode |
| 26 | except 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 Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 42 | import base64 |
| 43 | |
| 44 | # Intrapackage imports |
| 45 | from Encoders import _bencode, _qencode |
| 46 | |
| 47 | COMMASPACE = ', ' |
| 48 | UEMPTYSTRING = u'' |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 49 | CRLF = '\r\n' |
| 50 | |
| 51 | specialsre = re.compile(r'[][\()<>@,:;".]') |
| 52 | escapesre = re.compile(r'[][\()"]') |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 53 | |
| 54 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 55 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 56 | # Helpers |
| 57 | |
| 58 | def _identity(s): |
| 59 | return s |
| 60 | |
| 61 | |
| 62 | def _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 Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 76 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 77 | def 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 | |
| 87 | def 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 Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame^] | 91 | |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 92 | 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 |
| 105 | def dump_address_pair(pair): |
| 106 | warnings.warn('Use email.Utils.formataddr() instead', |
| 107 | DeprecationWarning, 2) |
| 108 | return formataddr(pair) |
| 109 | |
| 110 | |
| 111 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 112 | def getaddresses(fieldvalues): |
| 113 | """Return a list of (REALNAME, EMAIL) for each fieldvalue.""" |
| 114 | all = COMMASPACE.join(fieldvalues) |
Barry Warsaw | e1df15c | 2002-04-12 20:50:05 +0000 | [diff] [blame] | 115 | a = _AddressList(all) |
Barry Warsaw | 4be9ecc | 2002-05-22 01:52:10 +0000 | [diff] [blame] | 116 | return a.addresslist |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 117 | |
| 118 | |
Barry Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 119 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 120 | ecre = 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 | |
| 131 | def decode(s): |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 132 | """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 Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 144 | rtn = [] |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 145 | for atom, charset in L: |
| 146 | if charset is None: |
| 147 | rtn.append(atom) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 148 | else: |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 149 | # Convert the string to Unicode using the given encoding. Leave |
| 150 | # Unicode conversion errors to strict. |
| 151 | rtn.append(unicode(atom, charset)) |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 152 | # 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 Warsaw | e968ead | 2001-10-04 17:05:11 +0000 | [diff] [blame] | 157 | |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 158 | def encode(s, charset='iso-8859-1', encoding='q'): |
| 159 | """Encode a string according to RFC 2047.""" |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 160 | warnings.warn('Use Header.Header.encode() instead.', DeprecationWarning, 2) |
Barry Warsaw | c44d2c5 | 2001-12-03 19:26:40 +0000 | [diff] [blame] | 161 | encoding = encoding.lower() |
| 162 | if encoding == 'q': |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 163 | estr = _qencode(s) |
Barry Warsaw | c44d2c5 | 2001-12-03 19:26:40 +0000 | [diff] [blame] | 164 | elif encoding == 'b': |
Barry Warsaw | ba92580 | 2001-09-23 03:17:28 +0000 | [diff] [blame] | 165 | estr = _bencode(s) |
| 166 | else: |
| 167 | raise ValueError, 'Illegal encoding code: ' + encoding |
Barry Warsaw | c44d2c5 | 2001-12-03 19:26:40 +0000 | [diff] [blame] | 168 | return '=?%s?%s?%s?=' % (charset.lower(), encoding, estr) |
Barry Warsaw | aa79f4d | 2001-11-09 16:59:56 +0000 | [diff] [blame] | 169 | |
| 170 | |
| 171 | |
| 172 | def formatdate(timeval=None, localtime=0): |
Barry Warsaw | 9cff0e6 | 2001-11-09 17:07:28 +0000 | [diff] [blame] | 173 | """Returns a date string as specified by RFC 2822, e.g.: |
Barry Warsaw | aa79f4d | 2001-11-09 16:59:56 +0000 | [diff] [blame] | 174 | |
| 175 | Fri, 09 Nov 2001 01:08:47 -0000 |
| 176 | |
Barry Warsaw | 9cff0e6 | 2001-11-09 17:07:28 +0000 | [diff] [blame] | 177 | 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 Warsaw | aa79f4d | 2001-11-09 16:59:56 +0000 | [diff] [blame] | 183 | """ |
| 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 Warsaw | e5739a6 | 2001-11-19 18:36:43 +0000 | [diff] [blame] | 196 | 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 Warsaw | aa79f4d | 2001-11-09 16:59:56 +0000 | [diff] [blame] | 204 | 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 Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 215 | |
| 216 | |
| 217 | |
| 218 | def 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. |
| 243 | def parsedate(data): |
| 244 | if not data: |
| 245 | return None |
| 246 | return _parsedate(data) |
| 247 | |
| 248 | |
| 249 | def parsedate_tz(data): |
| 250 | if not data: |
| 251 | return None |
| 252 | return _parsedate_tz(data) |
| 253 | |
| 254 | |
| 255 | def parseaddr(addr): |
Barry Warsaw | 24fd025 | 2002-04-15 22:00:25 +0000 | [diff] [blame] | 256 | addrs = _AddressList(addr).addresslist |
| 257 | if not addrs: |
Barry Warsaw | 409a4c0 | 2002-04-10 21:01:31 +0000 | [diff] [blame] | 258 | return '', '' |
Barry Warsaw | 24fd025 | 2002-04-15 22:00:25 +0000 | [diff] [blame] | 259 | return addrs[0] |