Fix lots of py3isms. Email package is borked in py3, however.
This commit is contained in:
+3
-3
@@ -48,7 +48,7 @@
|
||||
|
||||
from __future__ import print_function
|
||||
import time
|
||||
from plock import PLock
|
||||
from Milter.plock import PLock
|
||||
|
||||
class AddrCache(object):
|
||||
time_format = '%Y%b%d %H:%M:%S %Z'
|
||||
@@ -132,8 +132,8 @@ class AddrCache(object):
|
||||
if not ts or ts > too_old:
|
||||
return res
|
||||
del self.cache[lsender]
|
||||
raise KeyError, sender
|
||||
except KeyError,x:
|
||||
raise KeyError(sender)
|
||||
except KeyError as x:
|
||||
try:
|
||||
user,host = sender.split('@',1)
|
||||
return self.__getitem__(host)
|
||||
|
||||
+4
-1
@@ -2,7 +2,10 @@ import time
|
||||
import logging
|
||||
import urllib
|
||||
import sqlite3
|
||||
import thread
|
||||
try:
|
||||
import thread
|
||||
except:
|
||||
import _thread as thread
|
||||
from datetime import datetime
|
||||
|
||||
log = logging.getLogger('milter.greylist')
|
||||
|
||||
+3
-3
@@ -11,7 +11,7 @@ class PLock(object):
|
||||
self.basename = basename
|
||||
self.fp = None
|
||||
|
||||
def lock(self,lockname=None,mode=0660,strict_perms=False):
|
||||
def lock(self,lockname=None,mode=0o660,strict_perms=False):
|
||||
"Start an update transaction. Return FILE to write new version."
|
||||
self.unlock()
|
||||
if not lockname:
|
||||
@@ -21,7 +21,7 @@ class PLock(object):
|
||||
st = os.stat(self.basename)
|
||||
mode |= st.st_mode
|
||||
except OSError: pass
|
||||
u = os.umask(0002)
|
||||
u = os.umask(0o2)
|
||||
try:
|
||||
fd = os.open(lockname,os.O_WRONLY+os.O_CREAT+os.O_EXCL,mode)
|
||||
finally:
|
||||
@@ -46,7 +46,7 @@ class PLock(object):
|
||||
def commit(self,backname=None):
|
||||
"Commit update transaction with optional backup file."
|
||||
if not self.fp:
|
||||
raise IOError,"File not locked"
|
||||
raise IOError("File not locked")
|
||||
self.fp.close()
|
||||
self.fp = None
|
||||
if backname:
|
||||
|
||||
+1
-1
@@ -85,7 +85,7 @@ def inet_pton(p):
|
||||
::1.2.3.4.5
|
||||
"""
|
||||
if p == '::':
|
||||
return '\0'*16
|
||||
return b'\0'*16
|
||||
s = p
|
||||
m = RE_IP4.search(s)
|
||||
try:
|
||||
|
||||
+15
-31
@@ -2,7 +2,7 @@
|
||||
# A test framework for milters
|
||||
|
||||
from __future__ import print_function
|
||||
import rfc822
|
||||
import mime
|
||||
try:
|
||||
from StringIO import StringIO
|
||||
except:
|
||||
@@ -62,11 +62,11 @@ class TestBase(object):
|
||||
self._body.write(chunk)
|
||||
self._bodyreplaced = True
|
||||
else:
|
||||
raise IOError,"replacebody not called from eom()"
|
||||
raise IOError("replacebody not called from eom()")
|
||||
|
||||
def chgfrom(self,sender,params=None):
|
||||
if not self._body:
|
||||
raise IOError,"chgfrom not called from eom()"
|
||||
raise IOError("chgfrom not called from eom()")
|
||||
self.log('chgfrom: sender=%s' % (sender))
|
||||
self._envfromchanged = True
|
||||
self._sender = sender
|
||||
@@ -83,7 +83,7 @@ class TestBase(object):
|
||||
# work for a %milter
|
||||
def chgheader(self,field,idx,value):
|
||||
if not self._body:
|
||||
raise IOError,"chgheader not called from eom()"
|
||||
raise IOError("chgheader not called from eom()")
|
||||
self.log('chgheader: %s[%d]=%s' % (field,idx,value))
|
||||
if value == '':
|
||||
del self._msg[field]
|
||||
@@ -93,19 +93,19 @@ class TestBase(object):
|
||||
|
||||
def addheader(self,field,value,idx=-1):
|
||||
if not self._body:
|
||||
raise IOError,"addheader not called from eom()"
|
||||
raise IOError("addheader not called from eom()")
|
||||
self.log('addheader: %s=%s' % (field,value))
|
||||
self._msg[field] = value
|
||||
self._headerschanged = True
|
||||
|
||||
def delrcpt(self,rcpt):
|
||||
if not self._body:
|
||||
raise IOError,"delrcpt not called from eom()"
|
||||
raise IOError("delrcpt not called from eom()")
|
||||
self._delrcpt.append(rcpt)
|
||||
|
||||
def addrcpt(self,rcpt):
|
||||
if not self._body:
|
||||
raise IOError,"addrcpt not called from eom()"
|
||||
raise IOError("addrcpt not called from eom()")
|
||||
self._addrcpt.append(rcpt)
|
||||
|
||||
## Save the reply codes and messages in self._reply.
|
||||
@@ -143,34 +143,21 @@ class TestBase(object):
|
||||
self._headerschanged = False
|
||||
self._reply = None
|
||||
self._sender = '<%s>'%sender
|
||||
msg = rfc822.Message(fp)
|
||||
msg = mime.message_from_file(fp)
|
||||
rc = self.envfrom(self._sender)
|
||||
if rc != Milter.CONTINUE: return rc
|
||||
for rcpt in (rcpt,) + rcpts:
|
||||
rc = self.envrcpt('<%s>'%rcpt)
|
||||
if rc != Milter.CONTINUE: return rc
|
||||
line = None
|
||||
for h in msg.headers:
|
||||
if h[:1].isspace():
|
||||
line = line + h
|
||||
continue
|
||||
if not line:
|
||||
line = h
|
||||
continue
|
||||
s = line.split(': ',1)
|
||||
if len(s) > 1: val = s[1].strip()
|
||||
else: val = ''
|
||||
rc = self.header(s[0],val)
|
||||
if rc != Milter.CONTINUE: return rc
|
||||
line = h
|
||||
if line:
|
||||
s = line.split(': ',1)
|
||||
rc = self.header(s[0],s[1])
|
||||
for h,val in msg.items():
|
||||
rc = self.header(h,val)
|
||||
if rc != Milter.CONTINUE: return rc
|
||||
rc = self.eoh()
|
||||
if rc != Milter.CONTINUE: return rc
|
||||
header,body = msg.as_bytes().split(b'\n\n',1)
|
||||
bfp = StringIO(body)
|
||||
while 1:
|
||||
buf = fp.read(8192)
|
||||
buf = bfp.read(8192)
|
||||
if len(buf) == 0: break
|
||||
rc = self.body(buf)
|
||||
if rc != Milter.CONTINUE: return rc
|
||||
@@ -179,12 +166,9 @@ class TestBase(object):
|
||||
rc = self.eom()
|
||||
if self._bodyreplaced:
|
||||
body = self._body.getvalue()
|
||||
else:
|
||||
msg.rewindbody()
|
||||
body = msg.fp.read()
|
||||
self._body = StringIO()
|
||||
self._body.writelines(msg.headers)
|
||||
self._body.write('\n')
|
||||
self._body.write(header)
|
||||
self._body.write('\n\n')
|
||||
self._body.write(body)
|
||||
return rc
|
||||
|
||||
|
||||
+10
-12
@@ -5,13 +5,11 @@
|
||||
import re
|
||||
import struct
|
||||
import socket
|
||||
import email.Errors
|
||||
import email.errors
|
||||
from email.header import decode_header
|
||||
import email.base64mime
|
||||
from fnmatch import fnmatchcase
|
||||
from email.Header import decode_header
|
||||
from binascii import a2b_base64
|
||||
#import email.Utils
|
||||
import rfc822
|
||||
|
||||
dnsre = re.compile(r'^[a-z][-a-z\d.]+$', re.IGNORECASE)
|
||||
PAT_IP4 = r'\.'.join([r'(?:\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])']*4)
|
||||
@@ -56,8 +54,8 @@ if hasattr(socket,'has_ipv6') and socket.has_ipv6:
|
||||
else:
|
||||
from pyip6 import inet_ntop, inet_pton
|
||||
|
||||
MASK = 0xFFFFFFFFL
|
||||
MASK6 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFL
|
||||
MASK = 0xFFFFFFFF
|
||||
MASK6 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
|
||||
|
||||
def cidr(i,n,mask=MASK):
|
||||
return ~(mask >> n) & mask & i
|
||||
@@ -119,7 +117,7 @@ def iniplist(ipaddr,iplist):
|
||||
return False
|
||||
|
||||
## Split email into Fullname and address.
|
||||
# This replaces <code>email.Utils.parseaddr</code> but fixes
|
||||
# This replaces <code>email.utils.parseaddr</code> but fixes
|
||||
# some <a href="http://bugs.python.org/issue1025395">tricky test cases</a>.
|
||||
# Additional tricky cases are still broken. Patches welcome.
|
||||
#
|
||||
@@ -139,8 +137,8 @@ def parseaddr(t):
|
||||
>>> parseaddr('a(WRONG)@b')
|
||||
('WRONG', 'a@b')
|
||||
"""
|
||||
#return email.Utils.parseaddr(t)
|
||||
res = rfc822.parseaddr(t)
|
||||
#return email.utils.parseaddr(t)
|
||||
res = email.utils.parseaddr(t)
|
||||
# dirty fix for some broken cases
|
||||
if not res[0]:
|
||||
pos = t.find('<')
|
||||
@@ -149,7 +147,7 @@ def parseaddr(t):
|
||||
pos1 = addrspec.rfind(':')
|
||||
if pos1 > 0:
|
||||
addrspec = addrspec[pos1+1:]
|
||||
return rfc822.parseaddr('"%s" <%s>' % (t[:pos].strip(),addrspec))
|
||||
return email.utils.parseaddr('"%s" <%s>' % (t[:pos].strip(),addrspec))
|
||||
if not res[1]:
|
||||
pos = t.find('<')
|
||||
if pos > 0 and t[-1] == '>':
|
||||
@@ -157,7 +155,7 @@ def parseaddr(t):
|
||||
pos1 = addrspec.rfind(':')
|
||||
if pos1 > 0:
|
||||
addrspec = addrspec[pos1+1:]
|
||||
return rfc822.parseaddr('%s<%s>' % (t[:pos].strip(),addrspec))
|
||||
return email.utils.parseaddr('%s<%s>' % (t[:pos].strip(),addrspec))
|
||||
return res
|
||||
|
||||
## Fix email.base64mime.decode to add any missing padding
|
||||
@@ -227,5 +225,5 @@ def parse_header(val):
|
||||
except UnicodeDecodeError: pass
|
||||
except LookupError: pass
|
||||
except ValueError: pass
|
||||
except email.Errors.HeaderParseError: pass
|
||||
except email.errors.HeaderParseError: pass
|
||||
return val
|
||||
|
||||
Reference in New Issue
Block a user