text
stringlengths
1
93.6k
element += c
result.append(element)
return result
def insert_libspf_local_policy(spftxt, local=None):
"""Returns spftxt with local inserted just before last non-fail
mechanism. This is how the libspf{2} libraries handle "local-policy".
Examples:
>>> insert_libspf_local_policy('v=spf1 -all')
'v=spf1 -all'
>>> insert_libspf_local_policy('v=spf1 -all','mx')
'v=spf1 -all'
>>> insert_libspf_local_policy('v=spf1','a mx ptr')
'v=spf1 a mx ptr'
>>> insert_libspf_local_policy('v=spf1 mx -all','a ptr')
'v=spf1 mx a ptr -all'
>>> insert_libspf_local_policy('v=spf1 mx -include:foo.co +all','a ptr')
'v=spf1 mx a ptr -include:foo.co +all'
# FIXME: is this right? If so, "last non-fail" is a bogus description.
>>> insert_libspf_local_policy('v=spf1 mx ?include:foo.co +all','a ptr')
'v=spf1 mx a ptr ?include:foo.co +all'
>>> spf='v=spf1 ip4:1.2.3.4 -a:example.net -all'
>>> local='ip4:192.0.2.3 a:example.org'
>>> insert_libspf_local_policy(spf,local)
'v=spf1 ip4:1.2.3.4 ip4:192.0.2.3 a:example.org -a:example.net -all'
"""
# look to find the all (if any) and then put local
# just after last non-fail mechanism. This is how
# libspf2 handles "local policy", and some people
# apparently find it useful (don't ask me why).
if not local: return spftxt
spf = spftxt.split()[1:]
if spf:
# local policy is SPF mechanisms/modifiers with no
# 'v=spf1' at the start
spf.reverse() #find the last non-fail mechanism
for mech in spf:
# map '?' '+' or '-' to 'neutral' 'pass'
# or 'fail'
if not RESULTS.get(mech[0]):
# actually finds last mech with default result
where = spf.index(mech)
spf[where:where] = [local]
spf.reverse()
local = ' '.join(spf)
break
else:
return spftxt # No local policy adds for v=spf1 -all
# Processing limits not applied to local policy. Suggest
# inserting 'local' mechanism to handle this properly
#MAX_LOOKUP = 100
return 'v=spf1 '+local
if sys.version_info[0] == 2:
def to_ascii(s):
"Raise PermError if arg is not 7-bit ascii."
try:
return s.encode('ascii')
except UnicodeError:
raise PermError('Non-ascii characters found',repr(s))
else:
def to_ascii(s):
"Raise PermError if arg is not 7-bit ascii."
try:
return s.decode('ascii')
except UnicodeError:
raise PermError('Non-ascii characters found',repr(s))
def _test():
import doctest, spf
return doctest.testmod(spf)
if __name__ == '__main__':
import getopt
try:
opts,argv = getopt.getopt(sys.argv[1:],"hvs:",
["help","verbose","strict"])
except getopt.GetoptError as err:
print(str(err))
print(USAGE)
sys.exit(2)
verbose = False
strict = True
for o,a in opts:
if o in ('-v','--verbose'):
verbose = True
if o in ('-s','--strict'):
strict = int(a)
elif o in ('-h','--help'):
print(USAGE)
if len(argv) == 0:
print(USAGE)
_test()
elif len(argv) == 1:
try:
q = query(i='127.0.0.1', s='localhost', h='unknown',
receiver=socket.gethostname())