First fetchmail implementation
parent
c56a51f7b7
commit
30ecbf81cd
@ -0,0 +1,9 @@
|
||||
{% extends "form.html" %}
|
||||
|
||||
{% block title %}
|
||||
Add a fetched account
|
||||
{% endblock %}
|
||||
|
||||
{% block subtitle %}
|
||||
{{ user }}
|
||||
{% endblock %}
|
@ -0,0 +1,9 @@
|
||||
{% extends "form.html" %}
|
||||
|
||||
{% block title %}
|
||||
Update a fetched account
|
||||
{% endblock %}
|
||||
|
||||
{% block subtitle %}
|
||||
{{ user }}
|
||||
{% endblock %}
|
@ -0,0 +1,44 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}
|
||||
Fetched accounts
|
||||
{% endblock %}
|
||||
|
||||
{% block subtitle %}
|
||||
{{ user }}
|
||||
{% endblock %}
|
||||
|
||||
{% block main_action %}
|
||||
<a class="btn btn-primary" href="{{ url_for('.fetch_create', user_address=user.address) }}">Add an account</a>
|
||||
{% endblock %}
|
||||
|
||||
{% block box %}
|
||||
<table class="table table-bordered">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>Actions</th>
|
||||
<th>Protocol</th>
|
||||
<th>Hostname</th>
|
||||
<th>Port</th>
|
||||
<th>TLS</th>
|
||||
<th>Username</th>
|
||||
<th>Created</th>
|
||||
<th>Last edit</th>
|
||||
</tr>
|
||||
{% for fetch in user.fetches %}
|
||||
<tr>
|
||||
<td>
|
||||
<a href="{{ url_for('.fetch_edit', fetch_id=fetch.id) }}" title="Edit"><i class="fa fa-pencil"></i></a>
|
||||
<a href="{{ url_for('.fetch_delete', fetch_id=fetch.id) }}" onclick="return confirm('Are you sure?')" 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.created_at }}</td>
|
||||
<td>{{ fetch.updated_at or '' }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endblock %}
|
@ -1 +0,0 @@
|
||||
{% extends "working.html" %}
|
@ -0,0 +1,68 @@
|
||||
from freeposte.admin import app, db, models, forms, utils
|
||||
from flask.ext import login as flask_login
|
||||
|
||||
import os
|
||||
import flask
|
||||
import wtforms_components
|
||||
|
||||
|
||||
@app.route('/fetch/list', methods=['GET', 'POST'], defaults={'user_address': None})
|
||||
@app.route('/fetch/list/<user_address>', methods=['GET'])
|
||||
@flask_login.login_required
|
||||
def fetch_list(user_address):
|
||||
user = utils.get_user(user_address, True)
|
||||
return flask.render_template('fetch/list.html', user=user)
|
||||
|
||||
|
||||
@app.route('/fetch/list', methods=['GET', 'POST'], defaults={'user_address': None})
|
||||
@app.route('/fetch/create/<user_address>', methods=['GET', 'POST'])
|
||||
@flask_login.login_required
|
||||
def fetch_create(user_address):
|
||||
user = utils.get_user(user_address)
|
||||
form = forms.FetchForm()
|
||||
if form.validate_on_submit():
|
||||
fetch = models.Fetch(user=user)
|
||||
fetch.protocol = form.protocol.data
|
||||
fetch.host = form.host.data
|
||||
fetch.port = form.port.data
|
||||
fetch.tls = form.tls.data
|
||||
fetch.username = form.username.data
|
||||
fetch.password = form.password.data
|
||||
db.session.add(fetch)
|
||||
db.session.commit()
|
||||
flask.flash('Fetch configuration created')
|
||||
return flask.redirect(
|
||||
flask.url_for('.fetch_create', user_address=user.address))
|
||||
return flask.render_template('fetch/create.html', form=form)
|
||||
|
||||
|
||||
@app.route('/fetch/edit/<fetch_id>', methods=['GET', 'POST'])
|
||||
@flask_login.login_required
|
||||
def fetch_edit(fetch_id):
|
||||
fetch = utils.get_fetch(fetch_id)
|
||||
form = forms.FetchForm(obj=fetch)
|
||||
if form.validate_on_submit():
|
||||
fetch.protocol = form.protocol.data
|
||||
fetch.host = form.host.data
|
||||
fetch.port = form.port.data
|
||||
fetch.tls = form.tls.data
|
||||
fetch.username = form.username.data
|
||||
fetch.password = form.password.data
|
||||
db.session.add(fetch)
|
||||
db.session.commit()
|
||||
flask.flash('Fetch configuration updated')
|
||||
return flask.redirect(
|
||||
flask.url_for('.fetch_list', user_address=fetch.user.address))
|
||||
return flask.render_template('fetch/edit.html',
|
||||
form=form, fetch=fetch)
|
||||
|
||||
|
||||
@app.route('/fetch/delete/<fetch_id>', methods=['GET'])
|
||||
@flask_login.login_required
|
||||
def fetch_delete(fetch_id):
|
||||
fetch = utils.get_fetch(fetch_id)
|
||||
db.session.delete(fetch)
|
||||
db.session.commit()
|
||||
flask.flash('Fetch configuration delete')
|
||||
return flask.redirect(
|
||||
flask.url_for('.fetch_list', user_address=fetch.user.address))
|
@ -0,0 +1,13 @@
|
||||
FROM python:alpine
|
||||
|
||||
RUN apk add --update \
|
||||
fetchmail \
|
||||
&& rm -rf /var/cache/apk/*
|
||||
|
||||
COPY fetchmail.py /fetchmail.py
|
||||
|
||||
RUN mkdir /var/spool/mail \
|
||||
&& chown mail: /var/spool/mail
|
||||
USER mail
|
||||
|
||||
CMD ["/fetchmail.py"]
|
@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import sqlite3
|
||||
import time
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
|
||||
RC_LINE = """
|
||||
poll {host} proto {protocol} port {port}
|
||||
user "{username}" password "{password}"
|
||||
smtphost "smtp"
|
||||
smtpname {user_address}
|
||||
{options}
|
||||
"""
|
||||
|
||||
|
||||
def fetchmail(fetchmailrc):
|
||||
print(fetchmailrc)
|
||||
with tempfile.NamedTemporaryFile() as handler:
|
||||
handler.write(fetchmailrc.encode("utf8"))
|
||||
handler.flush()
|
||||
os.system("fetchmail -N -f '{}'".format(handler.name))
|
||||
|
||||
|
||||
def run(cursor):
|
||||
cursor.execute("""
|
||||
SELECT user_address, protocol, host, port, tls, username, password
|
||||
FROM fetch
|
||||
""")
|
||||
fetchmailrc = ""
|
||||
for line in cursor.fetchall():
|
||||
user_address, protocol, host, port, tls, username, password = line
|
||||
options = "options ssl" if tls else ""
|
||||
fetchmailrc += RC_LINE.format(
|
||||
user_address=user_address,
|
||||
protocol=protocol,
|
||||
host=host,
|
||||
port=port,
|
||||
username=username,
|
||||
password=password,
|
||||
options=options
|
||||
)
|
||||
fetchmail(fetchmailrc)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
db_path = os.environ.get("DB_PATH", "/data/freeposte.db")
|
||||
connection = sqlite3.connect(db_path)
|
||||
while True:
|
||||
time.sleep(int(os.environ.get("FETCHMAIL_DELAY", 10)))
|
||||
cursor = connection.cursor()
|
||||
run(cursor)
|
||||
cursor.close()
|
Loading…
Reference in New Issue