@@ -20,19 +20,21 @@ def get_psl():
2020
2121 def download_psl ():
2222 fresh_psl = publicsuffix .fetch ()
23- with open (PublicSuffixListFilename , 'w' , encoding = ' utf-8' ) as fresh_psl_file :
23+ with open (PublicSuffixListFilename , "w" , encoding = " utf-8" ) as fresh_psl_file :
2424 fresh_psl_file .write (fresh_psl .read ())
2525
2626 # Download the psl if necessary
2727 if not PublicSuffixListReadOnly :
2828 if not path .exists (PublicSuffixListFilename ):
2929 download_psl ()
3030 else :
31- psl_age = datetime .now () - datetime .fromtimestamp (stat (PublicSuffixListFilename ).st_mtime )
31+ psl_age = datetime .now () - datetime .fromtimestamp (
32+ stat (PublicSuffixListFilename ).st_mtime
33+ )
3234 if psl_age > timedelta (hours = 24 ):
3335 download_psl ()
3436
35- with open (PublicSuffixListFilename , encoding = ' utf-8' ) as psl_file :
37+ with open (PublicSuffixListFilename , encoding = " utf-8" ) as psl_file :
3638 psl = publicsuffix .PublicSuffixList (psl_file )
3739
3840 return psl
@@ -54,13 +56,22 @@ def format_list(record_list):
5456 if not record_list :
5557 return None
5658
57- return ', ' .join (record_list )
59+ return ", " .join (record_list )
5860
5961
6062class Domain :
6163 base_domains = {}
6264
63- def __init__ (self , domain_name , timeout , smtp_timeout , smtp_localhost , smtp_ports , smtp_cache , dns_hostnames ):
65+ def __init__ (
66+ self ,
67+ domain_name ,
68+ timeout ,
69+ smtp_timeout ,
70+ smtp_localhost ,
71+ smtp_ports ,
72+ smtp_cache ,
73+ dns_hostnames ,
74+ ):
6475 self .domain_name = domain_name .lower ()
6576
6677 self .base_domain_name = get_public_suffix (self .domain_name )
@@ -71,7 +82,16 @@ def __init__(self, domain_name, timeout, smtp_timeout, smtp_localhost, smtp_port
7182 self .is_base_domain = False
7283 if self .base_domain_name not in Domain .base_domains :
7384 # Populate DMARC for parent.
74- domain = trustymail .scan (self .base_domain_name , timeout , smtp_timeout , smtp_localhost , smtp_ports , smtp_cache , {'mx' : False , 'starttls' : False , 'spf' : False , 'dmarc' : True }, dns_hostnames )
85+ domain = trustymail .scan (
86+ self .base_domain_name ,
87+ timeout ,
88+ smtp_timeout ,
89+ smtp_localhost ,
90+ smtp_ports ,
91+ smtp_cache ,
92+ {"mx" : False , "starttls" : False , "spf" : False , "dmarc" : True },
93+ dns_hostnames ,
94+ )
7595 Domain .base_domains [self .base_domain_name ] = domain
7696 self .base_domain = Domain .base_domains [self .base_domain_name ]
7797
@@ -127,8 +147,15 @@ def has_supports_smtp(self):
127147 """
128148 result = None
129149 if len (self .starttls_results ) > 0 :
130- result = len (filter (lambda x : self .starttls_results [x ]['supports_smtp' ],
131- self .starttls_results .keys ())) > 0
150+ result = (
151+ len (
152+ filter (
153+ lambda x : self .starttls_results [x ]["supports_smtp" ],
154+ self .starttls_results .keys (),
155+ )
156+ )
157+ > 0
158+ )
132159 return result
133160
134161 def has_starttls (self ):
@@ -138,8 +165,15 @@ def has_starttls(self):
138165 """
139166 result = None
140167 if len (self .starttls_results ) > 0 :
141- result = len (filter (lambda x : self .starttls_results [x ]['starttls' ],
142- self .starttls_results .keys ())) > 0
168+ result = (
169+ len (
170+ filter (
171+ lambda x : self .starttls_results [x ]["starttls" ],
172+ self .starttls_results .keys (),
173+ )
174+ )
175+ > 0
176+ )
143177 return result
144178
145179 def has_spf (self ):
@@ -160,7 +194,7 @@ def add_mx_record(self, record):
160194 # the record will contain a trailing period if it is a FQDN.
161195 if self .mail_servers is None :
162196 self .mail_servers = []
163- self .mail_servers .append (record .exchange .to_text ().rstrip ('.' ).lower ())
197+ self .mail_servers .append (record .exchange .to_text ().rstrip ("." ).lower ())
164198
165199 def parent_has_dmarc (self ):
166200 ans = self .has_dmarc ()
@@ -190,7 +224,7 @@ def get_dmarc_policy(self):
190224 ans = self .dmarc_policy
191225 # If the policy was never set, or isn't in the list of valid
192226 # policies, check the parents.
193- if ans is None or ans .lower () not in [' quarantine' , ' reject' , ' none' ]:
227+ if ans is None or ans .lower () not in [" quarantine" , " reject" , " none" ]:
194228 if self .base_domain :
195229 # We check the *subdomain* policy in case one was
196230 # explicitly set. If one was not explicitly set then
@@ -207,7 +241,7 @@ def get_dmarc_subdomain_policy(self):
207241 ans = self .dmarc_subdomain_policy
208242 # If the policy was never set, or isn't in the list of valid
209243 # policies, check the parents.
210- if ans is None or ans .lower () not in [' quarantine' , ' reject' , ' none' ]:
244+ if ans is None or ans .lower () not in [" quarantine" , " reject" , " none" ]:
211245 if self .base_domain :
212246 ans = self .base_domain .get_dmarc_subdomain_policy ()
213247 else :
@@ -256,57 +290,84 @@ def generate_results(self):
256290 mail_servers_that_support_smtp = None
257291 mail_servers_that_support_starttls = None
258292 else :
259- mail_servers_that_support_smtp = [x for x in self .starttls_results .keys () if self .starttls_results [x ][
260- 'supports_smtp' ]]
261- mail_servers_that_support_starttls = [x for x in self .starttls_results .keys () if self .starttls_results [x ][
262- 'starttls' ]]
293+ mail_servers_that_support_smtp = [
294+ x
295+ for x in self .starttls_results .keys ()
296+ if self .starttls_results [x ]["supports_smtp" ]
297+ ]
298+ mail_servers_that_support_starttls = [
299+ x
300+ for x in self .starttls_results .keys ()
301+ if self .starttls_results [x ]["starttls" ]
302+ ]
263303 domain_supports_smtp = bool (mail_servers_that_support_smtp )
264- domain_supports_starttls = domain_supports_smtp and all ([self .starttls_results [x ]['starttls' ] for x in mail_servers_that_support_smtp ])
265-
266- results = OrderedDict ([
267- ('Domain' , self .domain_name ),
268- ('Base Domain' , self .base_domain_name ),
269- ('Live' , self .is_live ),
270-
271- ('MX Record' , self .has_mail ()),
272- ('MX Record DNSSEC' , self .mx_records_dnssec ),
273- ('Mail Servers' , format_list (self .mail_servers )),
274- ('Mail Server Ports Tested' , format_list ([str (port ) for port in self .ports_tested ])),
275- ('Domain Supports SMTP Results' , format_list (mail_servers_that_support_smtp )),
276- # True if and only if at least one mail server speaks SMTP
277- ('Domain Supports SMTP' , domain_supports_smtp ),
278- ('Domain Supports STARTTLS Results' , format_list (mail_servers_that_support_starttls )),
279- # True if and only if all mail servers that speak SMTP
280- # also support STARTTLS
281- ('Domain Supports STARTTLS' , domain_supports_starttls ),
282-
283- ('SPF Record' , self .has_spf ()),
284- ('SPF Record DNSSEC' , self .spf_dnssec ),
285- ('Valid SPF' , self .valid_spf ),
286- ('SPF Results' , format_list (self .spf )),
287-
288- ('DMARC Record' , self .has_dmarc ()),
289- ('DMARC Record DNSSEC' , self .dmarc_dnssec ),
290- ('Valid DMARC' , self .has_dmarc () and self .valid_dmarc ),
291- ('DMARC Results' , format_list (self .dmarc )),
292-
293- ('DMARC Record on Base Domain' , self .parent_has_dmarc ()),
294- ('DMARC Record on Base Domain DNSSEC' , self .parent_dmarc_dnssec ()),
295- ('Valid DMARC Record on Base Domain' , self .parent_has_dmarc () and self .parent_valid_dmarc ()),
296- ('DMARC Results on Base Domain' , self .parent_dmarc_results ()),
297- ('DMARC Policy' , self .get_dmarc_policy ()),
298- ('DMARC Subdomain Policy' , self .get_dmarc_subdomain_policy ()),
299- ('DMARC Policy Percentage' , self .get_dmarc_pct ()),
300-
301- ("DMARC Aggregate Report URIs" , format_list (self .get_dmarc_aggregate_uris ())),
302- ("DMARC Forensic Report URIs" , format_list (self .get_dmarc_forensic_uris ())),
303-
304- ('DMARC Has Aggregate Report URI' , self .get_dmarc_has_aggregate_uri ()),
305- ('DMARC Has Forensic Report URI' , self .get_dmarc_has_forensic_uri ()),
306- ('DMARC Reporting Address Acceptance Error' , self .dmarc_reports_address_error ),
307-
308- ('Syntax Errors' , format_list (self .syntax_errors )),
309- ('Debug Info' , format_list (self .debug_info ))
310- ])
304+ domain_supports_starttls = domain_supports_smtp and all (
305+ [
306+ self .starttls_results [x ]["starttls" ]
307+ for x in mail_servers_that_support_smtp
308+ ]
309+ )
310+
311+ results = OrderedDict (
312+ [
313+ ("Domain" , self .domain_name ),
314+ ("Base Domain" , self .base_domain_name ),
315+ ("Live" , self .is_live ),
316+ ("MX Record" , self .has_mail ()),
317+ ("MX Record DNSSEC" , self .mx_records_dnssec ),
318+ ("Mail Servers" , format_list (self .mail_servers )),
319+ (
320+ "Mail Server Ports Tested" ,
321+ format_list ([str (port ) for port in self .ports_tested ]),
322+ ),
323+ (
324+ "Domain Supports SMTP Results" ,
325+ format_list (mail_servers_that_support_smtp ),
326+ ),
327+ # True if and only if at least one mail server speaks SMTP
328+ ("Domain Supports SMTP" , domain_supports_smtp ),
329+ (
330+ "Domain Supports STARTTLS Results" ,
331+ format_list (mail_servers_that_support_starttls ),
332+ ),
333+ # True if and only if all mail servers that speak SMTP
334+ # also support STARTTLS
335+ ("Domain Supports STARTTLS" , domain_supports_starttls ),
336+ ("SPF Record" , self .has_spf ()),
337+ ("SPF Record DNSSEC" , self .spf_dnssec ),
338+ ("Valid SPF" , self .valid_spf ),
339+ ("SPF Results" , format_list (self .spf )),
340+ ("DMARC Record" , self .has_dmarc ()),
341+ ("DMARC Record DNSSEC" , self .dmarc_dnssec ),
342+ ("Valid DMARC" , self .has_dmarc () and self .valid_dmarc ),
343+ ("DMARC Results" , format_list (self .dmarc )),
344+ ("DMARC Record on Base Domain" , self .parent_has_dmarc ()),
345+ ("DMARC Record on Base Domain DNSSEC" , self .parent_dmarc_dnssec ()),
346+ (
347+ "Valid DMARC Record on Base Domain" ,
348+ self .parent_has_dmarc () and self .parent_valid_dmarc (),
349+ ),
350+ ("DMARC Results on Base Domain" , self .parent_dmarc_results ()),
351+ ("DMARC Policy" , self .get_dmarc_policy ()),
352+ ("DMARC Subdomain Policy" , self .get_dmarc_subdomain_policy ()),
353+ ("DMARC Policy Percentage" , self .get_dmarc_pct ()),
354+ (
355+ "DMARC Aggregate Report URIs" ,
356+ format_list (self .get_dmarc_aggregate_uris ()),
357+ ),
358+ (
359+ "DMARC Forensic Report URIs" ,
360+ format_list (self .get_dmarc_forensic_uris ()),
361+ ),
362+ ("DMARC Has Aggregate Report URI" , self .get_dmarc_has_aggregate_uri ()),
363+ ("DMARC Has Forensic Report URI" , self .get_dmarc_has_forensic_uri ()),
364+ (
365+ "DMARC Reporting Address Acceptance Error" ,
366+ self .dmarc_reports_address_error ,
367+ ),
368+ ("Syntax Errors" , format_list (self .syntax_errors )),
369+ ("Debug Info" , format_list (self .debug_info )),
370+ ]
371+ )
311372
312373 return results
0 commit comments