def SPF(domain, record='SPF', nameserver=None):
 spf_re = re.compile('(?:\\+|~)?(ip[46]|include):(.+)')
 cmd = ['dig', '+short', str(domain), record]
 if (nameserver is not None):
  cmd.append('@{0}'.format(nameserver))
 result = __salt__['cmd.run_all'](cmd, python_shell=False)
 if (result['retcode'] != 0):
  log.warning("dig returned exit code '{0}'. Returning empty list as fallback.".format(result['retcode']))
  return []
 if ((result['stdout'] == '') and (record == 'SPF')):
  return SPF(domain, 'TXT', nameserver)
 sections = re.sub('"', '', result['stdout']).split()
 if ((len(sections) == 0) or (sections[0] != 'v=spf1')):
  return []
 if sections[1].startswith('redirect='):
  return SPF(sections[1][9:], 'SPF', nameserver)
 ret = []
 for section in sections[1:]:
  try:
   (mechanism, address) = spf_re.match(section).groups()
  except AttributeError:
   continue
  if (mechanism == 'include'):
   ret.extend(SPF(address, 'SPF', nameserver))
  elif ((mechanism in ('ip4', 'ip6')) and check_ip(address)):
   ret.append(address)
 return ret

def SPF ( domain , record = 'SPF' , nameserver = None ) : spf_re = re . compile ( '(?:\\+|~)?(ip[46]|include):(.+)' ) cmd = [ 'dig' , '+short' , str ( domain ) , record ] if ( nameserver is not None ) : cmd . append ( '@{0}' . format ( nameserver ) ) result = __salt__ [ 'cmd.run_all' ] ( cmd , python_shell = False ) if ( result [ 'retcode' ] != 0 ) : log . warning ( "dig returned exit code '{0}'. Returning empty list as fallback." . format ( result [ 'retcode' ] ) ) return [ ] if ( ( result [ 'stdout' ] == '' ) and ( record == 'SPF' ) ) : return SPF ( domain , 'TXT' , nameserver ) sections = re . sub ( '"' , '' , result [ 'stdout' ] ) . split ( ) if ( ( len ( sections ) == 0 ) or ( sections [ 0 ] != 'v=spf1' ) ) : return [ ] if sections [ 1 ] . startswith ( 'redirect=' ) : return SPF ( sections [ 1 ] [ 9 : ] , 'SPF' , nameserver ) ret = [ ] for section in sections [ 1 : ] : try : ( mechanism , address ) = spf_re . match ( section ) . groups ( ) except AttributeError : continue if ( mechanism == 'include' ) : ret . extend ( SPF ( address , 'SPF' , nameserver ) ) elif ( ( mechanism in ( 'ip4' , 'ip6' ) ) and check_ip ( address ) ) : ret . append ( address ) return ret
return the allowed ipv4 ranges in the spf record for domain .