Skip to content

Commit

Permalink
Display fetchmail errors to the user, fixes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiyou committed Sep 10, 2016
1 parent 709869d commit 2cb4a44
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
2 changes: 2 additions & 0 deletions admin/freeposte/admin/models.py
Expand Up @@ -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)
14 changes: 6 additions & 8 deletions admin/freeposte/admin/templates/fetch/list.html
Expand Up @@ -17,11 +17,10 @@
<tbody>
<tr>
<th>Actions</th>
<th>Protocol</th>
<th>Hostname</th>
<th>Port</th>
<th>TLS</th>
<th>Endpoint</th>
<th>Username</th>
<th>Last check</th>
<th>Status</th>
<th>Created</th>
<th>Last edit</th>
</tr>
Expand All @@ -31,11 +30,10 @@
<a href="{{ url_for('.fetch_edit', fetch_id=fetch.id) }}" title="Edit"><i class="fa fa-pencil"></i></a>&nbsp;
<a href="{{ url_for('.fetch_delete', fetch_id=fetch.id) }}" title="Delete"><i class="fa fa-trash"></i></a>
</td>
<td>{{ fetch.protocol }}</td>
<td>{{ fetch.host }}</td>
<td>{{ fetch.port }}</td>
<td>{{ fetch.tls }}</td>
<td>{{ fetch.protocol }}{{ 's' if fetch.tls else '' }}://{{ fetch.host }}:{{ fetch.port }}</td>
<td>{{ fetch.username }}</td>
<td>{{ fetch.last_check or '-' }}</td>
<td>{{ fetch.error or '-' }}</td>
<td>{{ fetch.created_at }}</td>
<td>{{ fetch.updated_at or '' }}</td>
</tr>
Expand Down
25 changes: 25 additions & 0 deletions 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')
4 changes: 0 additions & 4 deletions fetchmail/Dockerfile
Expand Up @@ -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"]
26 changes: 20 additions & 6 deletions fetchmail/fetchmail.py
Expand Up @@ -5,6 +5,7 @@
import os
import tempfile
import shlex
import subprocess


FETCHMAIL = """
Expand All @@ -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(
Expand All @@ -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__":
Expand All @@ -62,5 +76,5 @@ def run(cursor):
while True:
time.sleep(int(os.environ.get("FETCHMAIL_DELAY", 10)))
cursor = connection.cursor()
run(cursor)
run(connection, cursor)
cursor.close()

0 comments on commit 2cb4a44

Please sign in to comment.