From 2cb4a44b5a476316367d5cf4a4fc2dedb8e6c84e Mon Sep 17 00:00:00 2001 From: Pierre Jaury Date: Sat, 10 Sep 2016 13:05:55 +0200 Subject: [PATCH] Display fetchmail errors to the user, fixes #23 --- admin/freeposte/admin/models.py | 2 ++ .../freeposte/admin/templates/fetch/list.html | 14 +++++----- admin/migrations/versions/dc8c25cf5b98_.py | 25 ++++++++++++++++++ fetchmail/Dockerfile | 4 --- fetchmail/fetchmail.py | 26 ++++++++++++++----- 5 files changed, 53 insertions(+), 18 deletions(-) create mode 100644 admin/migrations/versions/dc8c25cf5b98_.py diff --git a/admin/freeposte/admin/models.py b/admin/freeposte/admin/models.py index b9b18f68..69570215 100644 --- a/admin/freeposte/admin/models.py +++ b/admin/freeposte/admin/models.py @@ -208,3 +208,5 @@ class Fetch(Base): tls = db.Column(db.Boolean(), nullable=False) username = db.Column(db.String(255), nullable=False) password = db.Column(db.String(255), nullable=False) + last_check = db.Column(db.DateTime, nullable=True) + error = db.Column(db.String(1023), nullable=True) diff --git a/admin/freeposte/admin/templates/fetch/list.html b/admin/freeposte/admin/templates/fetch/list.html index 8eb26631..b4782c3d 100644 --- a/admin/freeposte/admin/templates/fetch/list.html +++ b/admin/freeposte/admin/templates/fetch/list.html @@ -17,11 +17,10 @@ Fetched accounts Actions - Protocol - Hostname - Port - TLS + Endpoint Username + Last check + Status Created Last edit @@ -31,11 +30,10 @@ Fetched accounts   - {{ fetch.protocol }} - {{ fetch.host }} - {{ fetch.port }} - {{ fetch.tls }} + {{ fetch.protocol }}{{ 's' if fetch.tls else '' }}://{{ fetch.host }}:{{ fetch.port }} {{ fetch.username }} + {{ fetch.last_check or '-' }} + {{ fetch.error or '-' }} {{ fetch.created_at }} {{ fetch.updated_at or '' }} diff --git a/admin/migrations/versions/dc8c25cf5b98_.py b/admin/migrations/versions/dc8c25cf5b98_.py new file mode 100644 index 00000000..74c57182 --- /dev/null +++ b/admin/migrations/versions/dc8c25cf5b98_.py @@ -0,0 +1,25 @@ +""" Add metadata related to fetches + +Revision ID: dc8c25cf5b98 +Revises: a4accda8a8c7 +Create Date: 2016-09-10 12:41:01.161357 + +""" + +# revision identifiers, used by Alembic. +revision = 'dc8c25cf5b98' +down_revision = 'a4accda8a8c7' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + op.add_column('fetch', sa.Column('error', sa.String(length=1023), nullable=True)) + op.add_column('fetch', sa.Column('last_check', sa.DateTime(), nullable=True)) + + +def downgrade(): + with op.batch_alter_table('fetch') as batch: + batch.drop_column('last_check') + batch.drop_column('error') diff --git a/fetchmail/Dockerfile b/fetchmail/Dockerfile index 84926e19..7c7d9f3d 100644 --- a/fetchmail/Dockerfile +++ b/fetchmail/Dockerfile @@ -7,8 +7,4 @@ RUN apk add --update \ COPY fetchmail.py /fetchmail.py -RUN mkdir /var/spool/mail \ - && chown mail: /var/spool/mail -USER mail - CMD ["/fetchmail.py"] diff --git a/fetchmail/fetchmail.py b/fetchmail/fetchmail.py index 59c69877..8a81243c 100755 --- a/fetchmail/fetchmail.py +++ b/fetchmail/fetchmail.py @@ -5,6 +5,7 @@ import time import os import tempfile import shlex +import subprocess FETCHMAIL = """ @@ -28,20 +29,21 @@ def escape_rc_string(arg): def fetchmail(fetchmailrc): - print(fetchmailrc) with tempfile.NamedTemporaryFile() as handler: handler.write(fetchmailrc.encode("utf8")) handler.flush() - os.system(FETCHMAIL.format(shlex.quote(handler.name))) + command = FETCHMAIL.format(shlex.quote(handler.name)) + output = subprocess.check_output(command, shell=True) + return output -def run(cursor): +def run(connection, cursor): cursor.execute(""" SELECT user_email, protocol, host, port, tls, username, password FROM fetch """) - fetchmailrc = "" for line in cursor.fetchall(): + fetchmailrc = "" user_email, protocol, host, port, tls, username, password = line options = "options ssl" if tls else "" fetchmailrc += RC_LINE.format( @@ -53,7 +55,19 @@ def run(cursor): password=escape_rc_string(password), options=options ) - fetchmail(fetchmailrc) + try: + print(fetchmail(fetchmailrc)) + error_message = "" + except subprocess.CalledProcessError as error: + error_message = error.output.decode("utf8") + print(error.output) + finally: + cursor.execute(""" + UPDATE fetch SET error=?, last_check=datetime('now') + WHERE user_email=? + """, (error_message.split("\n")[0], user_email)) + connection.commit() + if __name__ == "__main__": @@ -62,5 +76,5 @@ if __name__ == "__main__": while True: time.sleep(int(os.environ.get("FETCHMAIL_DELAY", 10))) cursor = connection.cursor() - run(cursor) + run(connection, cursor) cursor.close()