From 313e98c1a2130f35c42554c8b1bc3bf3433903e5 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Sun, 5 Jan 2020 14:00:00 +0100 Subject: [PATCH] fetchmail: print unhandled exceptions, but don't crash refs #1295 --- optional/fetchmail/fetchmail.py | 84 +++++++++++++++++---------------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/optional/fetchmail/fetchmail.py b/optional/fetchmail/fetchmail.py index f3ee6a70..fb0c6f04 100755 --- a/optional/fetchmail/fetchmail.py +++ b/optional/fetchmail/fetchmail.py @@ -8,6 +8,7 @@ import subprocess import re import requests import sys +import traceback FETCHMAIL = """ @@ -45,47 +46,50 @@ def fetchmail(fetchmailrc): def run(debug): - fetches = requests.get("http://admin/internal/fetch").json() - smtphost, smtpport = extract_host_port(os.environ.get("HOST_SMTP", "smtp"), None) - if smtpport is None: - smtphostport = smtphost - else: - smtphostport = "%s/%d" % (smtphost, smtpport) - for fetch in fetches: - fetchmailrc = "" - options = "options antispam 501, 504, 550, 553, 554" - options += " sslmode wrapped" if fetch["tls"] else "" - options += " keep" if fetch["keep"] else " fetchall" - fetchmailrc += RC_LINE.format( - user_email=escape_rc_string(fetch["user_email"]), - protocol=fetch["protocol"], - host=escape_rc_string(fetch["host"]), - port=fetch["port"], - smtphost=smtphostport, - username=escape_rc_string(fetch["username"]), - password=escape_rc_string(fetch["password"]), - options=options - ) - if debug: - print(fetchmailrc) - try: - print(fetchmail(fetchmailrc)) - error_message = "" - except subprocess.CalledProcessError as error: - error_message = error.output.decode("utf8") - # No mail is not an error - if not error_message.startswith("fetchmail: No mail"): - print(error_message) - user_info = "for %s at %s" % (fetch["user_email"], fetch["host"]) - # Number of messages seen is not a error as well - if ("messages" in error_message and - "(seen " in error_message and - user_info in error_message): - print(error_message) - finally: - requests.post("http://admin/internal/fetch/{}".format(fetch["id"]), - json=error_message.split("\n")[0] + try: + fetches = requests.get("http://admin/internal/fetch").json() + smtphost, smtpport = extract_host_port(os.environ.get("HOST_SMTP", "smtp"), None) + if smtpport is None: + smtphostport = smtphost + else: + smtphostport = "%s/%d" % (smtphost, smtpport) + for fetch in fetches: + fetchmailrc = "" + options = "options antispam 501, 504, 550, 553, 554" + options += " sslmode wrapped" if fetch["tls"] else "" + options += " keep" if fetch["keep"] else " fetchall" + fetchmailrc += RC_LINE.format( + user_email=escape_rc_string(fetch["user_email"]), + protocol=fetch["protocol"], + host=escape_rc_string(fetch["host"]), + port=fetch["port"], + smtphost=smtphostport, + username=escape_rc_string(fetch["username"]), + password=escape_rc_string(fetch["password"]), + options=options ) + if debug: + print(fetchmailrc) + try: + print(fetchmail(fetchmailrc)) + error_message = "" + except subprocess.CalledProcessError as error: + error_message = error.output.decode("utf8") + # No mail is not an error + if not error_message.startswith("fetchmail: No mail"): + print(error_message) + user_info = "for %s at %s" % (fetch["user_email"], fetch["host"]) + # Number of messages seen is not a error as well + if ("messages" in error_message and + "(seen " in error_message and + user_info in error_message): + print(error_message) + finally: + requests.post("http://admin/internal/fetch/{}".format(fetch["id"]), + json=error_message.split("\n")[0] + ) + except Exception: + traceback.print_exc() if __name__ == "__main__":