2016-04-28 20:07:38 +02:00
|
|
|
#!/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"
|
2016-05-01 20:04:40 +02:00
|
|
|
smtpname {user_email}
|
2016-04-28 20:07:38 +02:00
|
|
|
{options}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def fetchmail(fetchmailrc):
|
|
|
|
with tempfile.NamedTemporaryFile() as handler:
|
|
|
|
handler.write(fetchmailrc.encode("utf8"))
|
|
|
|
handler.flush()
|
2016-06-26 12:50:57 +02:00
|
|
|
os.system("fetchmail --sslcertck -N -f '{}'".format(handler.name))
|
2016-04-28 20:07:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
def run(cursor):
|
|
|
|
cursor.execute("""
|
2016-05-01 20:04:40 +02:00
|
|
|
SELECT user_email, protocol, host, port, tls, username, password
|
2016-04-28 20:07:38 +02:00
|
|
|
FROM fetch
|
|
|
|
""")
|
|
|
|
fetchmailrc = ""
|
|
|
|
for line in cursor.fetchall():
|
2016-05-01 20:04:40 +02:00
|
|
|
user_email, protocol, host, port, tls, username, password = line
|
2016-04-28 20:07:38 +02:00
|
|
|
options = "options ssl" if tls else ""
|
|
|
|
fetchmailrc += RC_LINE.format(
|
2016-05-01 20:04:40 +02:00
|
|
|
user_email=user_email,
|
2016-04-28 20:07:38 +02:00
|
|
|
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()
|