From b3e3448f9647075a1aab192165ea9b6b09c9515a Mon Sep 17 00:00:00 2001 From: calvin Date: Tue, 11 Jan 2005 13:29:34 +0000 Subject: [PATCH] sort MX hosts according to preference git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@2132 e7d03fd6-7b0d-0410-9947-9c21f3af8025 --- ChangeLog | 5 +++ linkcheck/checker/mailtourl.py | 78 +++++++++++++++++++--------------- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/ChangeLog b/ChangeLog index e296e4c8..f89e382a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -18,6 +18,11 @@ Type: feature Changed: linkcheck/url.py + * Sort according to preference when checking MX hosts so that + preferred MX hosts get checked first. + Type: bugfix + Changed: linkcheck/checker/mailtourl.py + 2.0 "I Kina spiser de hunde" (released 7.12.2004) * Regenerate the HTML parser with new Bison version 1.875d. diff --git a/linkcheck/checker/mailtourl.py b/linkcheck/checker/mailtourl.py index 7bc4c7d4..b97383e6 100644 --- a/linkcheck/checker/mailtourl.py +++ b/linkcheck/checker/mailtourl.py @@ -107,45 +107,55 @@ class MailtoUrl (urlbase.UrlBase): linkcheck.log.debug(linkcheck.LOG_CHECK, "looking up MX mailhost %r", domain) answers = linkcheck.dns.resolver.query(domain, 'MX') - linkcheck.log.debug(linkcheck.LOG_CHECK, - "found %d MX mailhosts", len(answers)) if len(answers) == 0: self.add_warning(_("No MX mail host for %(domain)s found.") %\ {'domain': domain}) return - smtpconnect = 0 - for rdata in answers: - try: - host = rdata.exchange.to_text(omit_final_dot=True) - preference = rdata.preference - linkcheck.log.debug(linkcheck.LOG_CHECK, - "SMTP check for %r (pref %d)", host, preference) - self.url_connection = smtplib.SMTP() - if self.consumer.config.get("debug"): - self.url_connection.set_debuglevel(1) - self.url_connection.connect(host) - linkcheck.log.debug(linkcheck.LOG_CHECK, - "SMTP connected!") - smtpconnect = 1 - self.url_connection.helo() - info = self.url_connection.verify(username) - linkcheck.log.debug(linkcheck.LOG_CHECK, - "SMTP user info %r", info) - if info[0] == 250: - self.add_info(_("Verified address: %(info)s.") % \ - {'info': str(info[1])}) - except smtplib.SMTPException, msg: - self.add_warning( + # sort according to preference (lower preference means this + # host should be preferred + mxdata = [(rdata.preference, + rdata.exchange.to_text(omit_final_dot=True)) + for rdata in answers] + mxdata.sort() + # debug output + linkcheck.log.debug(linkcheck.LOG_CHECK, + "found %d MX mailhosts:", len(answers)) + for preference, host in mxdata: + linkcheck.log.debug(linkcheck.LOG_CHECK, + "MX host %r, preference %d", host, preference) + # connect + self.check_smtp_connect(mxdata) + + def check_smtp_connect (self, mxdata): + """mxdata is a list of (preference, host) tuples to check for""" + smtpconnect = 0 + for preference, host in mxdata: + try: + linkcheck.log.debug(linkcheck.LOG_CHECK, + "SMTP check for %r (preference %d)", host, preference) + self.url_connection = smtplib.SMTP() + if self.consumer.config.get("debug"): + self.url_connection.set_debuglevel(1) + self.url_connection.connect(host) + linkcheck.log.debug(linkcheck.LOG_CHECK, "SMTP connected!") + smtpconnect = 1 + self.url_connection.helo() + info = self.url_connection.verify(username) + linkcheck.log.debug(linkcheck.LOG_CHECK, + "SMTP user info %r", info) + if info[0] == 250: + self.add_info(_("Verified address: %(info)s.") % \ + {'info': str(info[1])}) + except smtplib.SMTPException, msg: + self.add_warning( _("MX mail host %(host)s did not accept connections: "\ - "%(error)s.") % \ - {'host': rdata.exchange, 'error': str(msg)}) - if smtpconnect: - break - if not smtpconnect: - self.set_result(_("Could not connect, but syntax is correct")) - else: - self.set_result(_("Found MX mail host %(host)s") % \ - {'host': rdata.exchange}) + "%(error)s.") % {'host': host, 'error': str(msg)}) + if smtpconnect: + break + if not smtpconnect: + self.set_result(_("Could not connect, but syntax is correct")) + else: + self.set_result(_("Found MX mail host %(host)s") % {'host': host}) def _split_address (self, address): split = address.split("@", 1)