text
stringlengths
1
93.6k
>>> q.expand('%{ir}.%{v}.%{l1r-}.lp._spf.%{d2}')
'3.2.0.192.in-addr.strong.lp._spf.example.com'
>>> try: q.expand('%(ir).%{v}.%{l1r-}.lp._spf.%{d2}')
... except PermError as x: print(x)
invalid-macro-char : %(ir)
>>> q.expand('%{p2}.trusted-domains.example.net')
'example.org.trusted-domains.example.net'
>>> q.expand('%{p2}.trusted-domains.example.net.')
'example.org.trusted-domains.example.net'
>>> q = query(s='@email.example.com',
... h='mx.example.org', i='192.0.2.3')
>>> q.p = 'mx.example.org'
>>> q.expand('%{l}')
'postmaster'
>>> q.expand('%{l')
'%{l'
"""
# Check for invalid macro syntax
if s.find('%') >= 0:
regex = RE_INVALID_MACRO
for label in s.split('.'):
if regex.search(s):
raise PermError ('invalid-macro-char ', label)
# expand macros
end = 0
result = ''
for i in RE_CHAR.finditer(s):
result += s[end:i.start()]
macro = s[i.start():i.end()]
if macro == '%%':
result += '%'
elif macro == '%_':
result += ' '
elif macro == '%-':
result += '%20'
else:
letter = macro[2].lower()
# print(letter)
if letter == 'p':
self.getp()
elif letter in 'crt' and stripdot:
raise PermError(
'c,r,t macros allowed in exp= text only', macro)
expansion = getattr(self, letter, self)
if expansion:
if expansion == self:
raise PermError('Unknown Macro Encountered', macro)
e = expand_one(expansion, macro[3:-1], JOINERS.get(letter))
if letter != macro[2]:
e = urllibparse.quote(e,'~')
result += e
end = i.end()
result += s[end:]
if stripdot and result.endswith('.'):
result = result[:-1]
if result.count('.') != 0:
if len(result) > 253:
result = result[(result.index('.')+1):]
return result
def dns_spf(self, domain):
"""Get the SPF record recorded in DNS for a specific domain
name. Returns None if not found, or if more than one record
is found.
"""
# Per RFC 4.3/1, check for malformed domain. This produces
# no results as a special case.
for label in domain.split('.'):
if not label or len(label) > 63:
return None
# for performance, check for most common case of TXT first
a = [t for t in self.dns_txt(domain) if RE_SPF.match(t)]
if len(a) > 1:
if self.verbose: print('cache=',self.cache)
raise PermError('Two or more type TXT spf records found.')
if len(a) == 1 and self.strict < 2:
return to_ascii(a[0])
# check official SPF type first when it becomes more popular
if self.strict > 1:
#Only check for Type SPF in harsh mode until it is more popular.
try:
b = [t for t in self.dns_txt(domain,'SPF',ignore_void=True)
if RE_SPF.match(t)]
except TempError as x:
# some braindead DNS servers hang on type 99 query
if self.strict > 1: raise TempError(x)
b = []
if len(b) > 1:
raise PermError('Two or more type SPF spf records found.')
if len(b) == 1:
if self.strict > 1 and len(a) == 1 and a[0] != b[0]:
#Changed from permerror to warning based on RFC 4408 Auth 48 change
raise AmbiguityWarning(
'v=spf1 records of both type TXT and SPF (type 99) present, but not identical')
return to_ascii(b[0])