# encoding: utf-8 # Author: Zhang Huangbin import web from libs import iredutils cfg = web.iredconfig session = web.config.get('_session') ############## # Validators # def isWblistIP(s): s = str(s) fields = s.split('.') if len(fields) != 4: return False # Valid IP address formats: # - 192.168.10.5 # Single address # - 192.168.10.% # Subnet # - 192.%.10.5 # Subnet # -- Validate each field -- # First field must be an integer number, and (0 < number < 255) if not fields[0].isdigit(): return False else: if not 0 < int(fields[0]) < 255: return False # Validate field 1, 2, 3. # Must be an interger number (0 < number < 255) or string '%'. for order in [1, 2, 3,]: if not fields[order].isdigit() and fields[order] != '%': return False if fields[order].isdigit(): if not 0 < int(fields[order]) < 255: return False return True def isWblistSender(s): s = web.safestr(s) # Must be a valid email address or '@domain.ltd'. tmpDomain = '' if s.startswith('@'): tmpDomain = s.lstrip('@') if not iredutils.isEmail(s) and not iredutils.isDomain(tmpDomain): return False return True def isWblistDnsName(s): s = str(s) tmpName = '' if s.startswith('%.'): tmpName = s.lstrip('%.') if not iredutils.isDomain(s) and not iredutils.isDomain(tmpName): return False return True def isThrottlingAccount(s): s = web.safestr(s) if iredutils.isEmail(s): return True elif iredutils.isDomain(s.lstrip('@')): return True else: return False # # End Validators ################## ################################### # Initialize database connection. # class PolicydWrap: def __init__(self, app=web.app, session=session, **settings): try: self.db = web.database( dbn='mysql', db=cfg.policyd.get('db', 'policyd'), user=cfg.policyd.get('user', 'policyd'), passwd=cfg.policyd.get('passwd'), charset='utf8', ) self.db.supports_multiple_insert = True except Exception, e: return (False, str(e))