2016-04-28 20:07:38 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import sqlite3
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import tempfile
|
2016-09-10 12:27:43 +02:00
|
|
|
import shlex
|
2016-09-10 13:05:55 +02:00
|
|
|
import subprocess
|
2018-01-31 23:24:54 +02:00
|
|
|
import re
|
2016-04-28 20:07:38 +02:00
|
|
|
|
|
|
|
|
2016-06-26 13:54:16 +02:00
|
|
|
FETCHMAIL = """
|
|
|
|
fetchmail -N \
|
|
|
|
--sslcertck --sslcertpath /etc/ssl/certs \
|
|
|
|
-f {}
|
|
|
|
"""
|
|
|
|
|
2016-04-28 20:07:38 +02:00
|
|
|
RC_LINE = """
|
2016-10-26 13:21:02 +02:00
|
|
|
poll "{host}" proto {protocol} port {port}
|
2016-04-28 20:07:38 +02:00
|
|
|
user "{username}" password "{password}"
|
2016-10-26 13:21:02 +02:00
|
|
|
is "{user_email}"
|
2018-01-31 23:24:54 +02:00
|
|
|
smtphost "{smtphost}"
|
2016-04-28 20:07:38 +02:00
|
|
|
{options}
|
2016-10-26 13:21:02 +02:00
|
|
|
sslproto 'AUTO'
|
2016-04-28 20:07:38 +02:00
|
|
|
"""
|
|
|
|
|
2018-01-31 23:24:54 +02:00
|
|
|
def extract_host_port(host_and_port, default_port):
|
|
|
|
host, _, port = re.match('^(.*)(:([0-9]*))?$', host_and_port).groups()
|
|
|
|
return host, int(port) if port else default_port
|
2016-12-02 11:57:17 +02:00
|
|
|
|
2016-09-10 12:27:43 +02:00
|
|
|
def escape_rc_string(arg):
|
|
|
|
return arg.replace("\\", "\\\\").replace('"', '\\"')
|
|
|
|
|
|
|
|
|
2016-04-28 20:07:38 +02:00
|
|
|
def fetchmail(fetchmailrc):
|
|
|
|
with tempfile.NamedTemporaryFile() as handler:
|
|
|
|
handler.write(fetchmailrc.encode("utf8"))
|
|
|
|
handler.flush()
|
2016-09-10 13:05:55 +02:00
|
|
|
command = FETCHMAIL.format(shlex.quote(handler.name))
|
|
|
|
output = subprocess.check_output(command, shell=True)
|
|
|
|
return output
|
2016-04-28 20:07:38 +02:00
|
|
|
|
|
|
|
|
2017-02-02 23:45:43 +02:00
|
|
|
def run(connection, cursor, debug):
|
2016-04-28 20:07:38 +02:00
|
|
|
cursor.execute("""
|
2017-02-02 23:45:43 +02:00
|
|
|
SELECT user_email, protocol, host, port, tls, username, password, keep
|
2016-04-28 20:07:38 +02:00
|
|
|
FROM fetch
|
|
|
|
""")
|
2018-01-31 23:24:54 +02:00
|
|
|
smtphost, smtpport = extract_host_port(os.environ.get("HOST_SMTP", "smtp"), None)
|
|
|
|
if smtpport is None:
|
|
|
|
smtphostport = smtphost
|
|
|
|
else:
|
|
|
|
smtphostport = "%s/%d" % (smtphost, smtpport)
|
2016-04-28 20:07:38 +02:00
|
|
|
for line in cursor.fetchall():
|
2016-09-10 13:05:55 +02:00
|
|
|
fetchmailrc = ""
|
2017-02-02 23:45:43 +02:00
|
|
|
user_email, protocol, host, port, tls, username, password, keep = line
|
2016-11-18 15:27:32 +02:00
|
|
|
options = "options antispam 501, 504, 550, 553, 554"
|
2016-10-26 13:21:02 +02:00
|
|
|
options += " ssl" if tls else ""
|
2016-11-18 15:27:32 +02:00
|
|
|
options += " keep" if keep else " fetchall"
|
2016-04-28 20:07:38 +02:00
|
|
|
fetchmailrc += RC_LINE.format(
|
2016-09-10 12:27:43 +02:00
|
|
|
user_email=escape_rc_string(user_email),
|
2016-04-28 20:07:38 +02:00
|
|
|
protocol=protocol,
|
2016-09-10 12:27:43 +02:00
|
|
|
host=escape_rc_string(host),
|
2016-04-28 20:07:38 +02:00
|
|
|
port=port,
|
2018-01-31 23:24:54 +02:00
|
|
|
smtphost=smtphostport,
|
2016-09-10 12:27:43 +02:00
|
|
|
username=escape_rc_string(username),
|
|
|
|
password=escape_rc_string(password),
|
2016-04-28 20:07:38 +02:00
|
|
|
options=options
|
|
|
|
)
|
2016-11-09 10:57:53 +02:00
|
|
|
if debug:
|
2016-12-02 11:57:17 +02:00
|
|
|
print(fetchmailrc)
|
2016-09-10 13:05:55 +02:00
|
|
|
try:
|
2016-12-02 11:57:17 +02:00
|
|
|
print(fetchmail(fetchmailrc))
|
2016-09-10 13:05:55 +02:00
|
|
|
error_message = ""
|
|
|
|
except subprocess.CalledProcessError as error:
|
2016-12-02 11:57:17 +02:00
|
|
|
error_message = error.output.decode("utf8")
|
2016-10-26 13:21:02 +02:00
|
|
|
# No mail is not an error
|
2016-11-09 10:57:53 +02:00
|
|
|
if not error_message.startswith("fetchmail: No mail"):
|
2016-12-02 11:57:17 +02:00
|
|
|
print(error_message)
|
|
|
|
user_info = "for %s at %s" % (user_email, host)
|
2016-11-18 15:27:32 +02:00
|
|
|
# Number of messages seen is not a error as well
|
2016-12-02 11:57:17 +02:00
|
|
|
if ("messages" in error_message and
|
|
|
|
"(seen " in error_message and
|
|
|
|
user_info in error_message):
|
|
|
|
print(error_message)
|
2016-09-10 13:05:55 +02:00
|
|
|
finally:
|
|
|
|
cursor.execute("""
|
|
|
|
UPDATE fetch SET error=?, last_check=datetime('now')
|
|
|
|
WHERE user_email=?
|
|
|
|
""", (error_message.split("\n")[0], user_email))
|
|
|
|
connection.commit()
|
|
|
|
|
2016-04-28 20:07:38 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-11-09 10:57:53 +02:00
|
|
|
debug = os.environ.get("DEBUG", None) == "True"
|
2016-10-29 13:42:39 +02:00
|
|
|
db_path = os.environ.get("DB_PATH", "/data/main.db")
|
2016-04-28 20:07:38 +02:00
|
|
|
connection = sqlite3.connect(db_path)
|
|
|
|
while True:
|
|
|
|
cursor = connection.cursor()
|
2017-02-02 23:45:43 +02:00
|
|
|
run(connection, cursor, debug)
|
2016-04-28 20:07:38 +02:00
|
|
|
cursor.close()
|
2016-10-26 13:21:02 +02:00
|
|
|
time.sleep(int(os.environ.get("FETCHMAIL_DELAY", 60)))
|