# Path: Milter/policy.py
# class MTAPolicy(object):
#   "Get SPF policy by result from sendmail style access file."
#   def __init__(self,sender,conf,access_file=None):
#     if not access_file:
#       access_file = conf.access_file
#     self.use_nulls = conf.access_file_nulls
#     self.sender = sender
#     self.domain = sender.split('@')[-1].lower()
#     self.acf = None
#     self.access_file = access_file
# 
#   def close(self):
#     if self.acf:
#       self.acf.close()
# 
#   def __enter__(self): 
#     self.acf = None
#     if self.access_file:
#       try:
#         self.acf = dbmopen(self.access_file,'r')
#       except:
#         print('%s: Cannot open for reading'%self.access_file)
#         raise
#     return self
#   def __exit__(self,t,v,b): self.close()
# 
#   def getPolicy(self,pfx):
#     acf = self.acf
#     if not acf: return None
#     if self.use_nulls: sfx = b'\x00'
#     else: sfx = b''
#     pfx = pfx.encode() + b'!'
#     try:
#       return acf[pfx + self.sender.encode() + sfx].rstrip(b'\x00').decode()
#     except KeyError:
#       try:
#         return acf[pfx + self.domain.encode() + sfx].rstrip(b'\x00').decode()
#       except KeyError:
#         try:
#           return acf[pfx + sfx].rstrip(b'\x00').decode()
#         except KeyError:
#           try:
#             return acf[pfx[:-1] + sfx].rstrip(b'\x00').decode()
#           except KeyError:
#             return None


import unittest
import sys
import os
from Milter.policy import MTAPolicy
          os.path.getmtime('test/access') > os.path.getmtime('test/access.db'):
        cmd = 'tr : ! <test/access | makemap hash test/access.db'
        if os.system(cmd):
          print('failed!')
    else:
      print("Missing test/access")

  def testPolicy(self):
    with MTAPolicy('good@example.com',conf=self.config) as p:
      pol = p.getPolicy('smtp-auth')
    self.assertEqual(pol,'OK')
    with MTAPolicy('bad@example.com',conf=self.config) as p:
      pol = p.getPolicy('smtp-auth')
    self.assertEqual(pol,'REJECT')
    with MTAPolicy('bad@bad.example.com',conf=self.config) as p:
      pol = p.getPolicy('smtp-auth')
    self.assertEqual(pol,None)
    with MTAPolicy('any@random.com',conf=self.config) as p:
      pol = p.getPolicy('smtp-test')
    self.assertEqual(pol,'REJECT')

def suite(): return unittest.makeSuite(PolicyTestCase,'test')

if __name__ == '__main__':
  if len(sys.argv) < 2:
    unittest.main()
  else:
    a = sys.argv[1:]
    while len(a) >= 2:
      e,k = a[:2]

# Path: Milter/policy.py
# class MTAPolicy(object):
#   "Get SPF policy by result from sendmail style access file."
#   def __init__(self,sender,conf,access_file=None):
#     if not access_file:
#       access_file = conf.access_file
#     self.use_nulls = conf.access_file_nulls
#     self.sender = sender
#     self.domain = sender.split('@')[-1].lower()
#     self.acf = None
#     self.access_file = access_file
# 
#   def close(self):
#     if self.acf:
#       self.acf.close()
# 
#   def __enter__(self): 
#     self.acf = None
#     if self.access_file:
#       try:
#         self.acf = dbmopen(self.access_file,'r')
#       except:
#         print('%s: Cannot open for reading'%self.access_file)
#         raise
#     return self
#   def __exit__(self,t,v,b): self.close()
# 
#   def getPolicy(self,pfx):
#     acf = self.acf
#     if not acf: return None
#     if self.use_nulls: sfx = b'\x00'
#     else: sfx = b''
#     pfx = pfx.encode() + b'!'
#     try:
#       return acf[pfx + self.sender.encode() + sfx].rstrip(b'\x00').decode()
#     except KeyError:
#       try:
#         return acf[pfx + self.domain.encode() + sfx].rstrip(b'\x00').decode()
#       except KeyError:
#         try:
#           return acf[pfx + sfx].rstrip(b'\x00').decode()
#         except KeyError:
#           try:
#             return acf[pfx[:-1] + sfx].rstrip(b'\x00').decode()
#           except KeyError:
#             return None


# Path: testpolicy.py
import unittest
import sys
import os
from Milter.policy import MTAPolicy

          os.path.getmtime('test/access') > os.path.getmtime('test/access.db'):
        cmd = 'tr : ! <test/access | makemap hash test/access.db'
        if os.system(cmd):
          print('failed!')
    else:
      print("Missing test/access")

  def testPolicy(self):
    with MTAPolicy('good@example.com',conf=self.config) as p:
      pol = p.getPolicy('smtp-auth')
    self.assertEqual(pol,'OK')
    with MTAPolicy('bad@example.com',conf=self.config) as p:
      pol = p.getPolicy('smtp-auth')
    self.assertEqual(pol,'REJECT')
    with MTAPolicy('bad@bad.example.com',conf=self.config) as p:
      pol = p.getPolicy('smtp-auth')
    self.assertEqual(pol,None)
    with MTAPolicy('any@random.com',conf=self.config) as p:
      pol = p.getPolicy('smtp-test')
    self.assertEqual(pol,'REJECT')

def suite(): return unittest.makeSuite(PolicyTestCase,'test')

if __name__ == '__main__':
  if len(sys.argv) < 2:
    unittest.main()
  else:
    a = sys.argv[1:]
    while len(a) >= 2:
      e,k = a[:2]

      with MTAPolicy(e,conf=Config()) as p: