You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.8 KiB
Python
93 lines
2.8 KiB
Python
9 years ago
|
#!/usr/bin/env python
|
||
|
|
||
|
import sqlite3
|
||
|
import time
|
||
|
import os
|
||
|
import tempfile
|
||
8 years ago
|
import shlex
|
||
8 years ago
|
import subprocess
|
||
9 years ago
|
|
||
|
|
||
9 years ago
|
FETCHMAIL = """
|
||
|
fetchmail -N \
|
||
|
--sslcertck --sslcertpath /etc/ssl/certs \
|
||
|
-f {}
|
||
|
"""
|
||
|
|
||
9 years ago
|
RC_LINE = """
|
||
8 years ago
|
poll "{host}" proto {protocol} port {port}
|
||
9 years ago
|
user "{username}" password "{password}"
|
||
8 years ago
|
is "{user_email}"
|
||
9 years ago
|
smtphost "smtp"
|
||
|
{options}
|
||
8 years ago
|
sslproto 'AUTO'
|
||
9 years ago
|
"""
|
||
|
|
||
8 years ago
|
|
||
8 years ago
|
def escape_rc_string(arg):
|
||
|
return arg.replace("\\", "\\\\").replace('"', '\\"')
|
||
|
|
||
|
|
||
9 years ago
|
def fetchmail(fetchmailrc):
|
||
|
with tempfile.NamedTemporaryFile() as handler:
|
||
|
handler.write(fetchmailrc.encode("utf8"))
|
||
|
handler.flush()
|
||
8 years ago
|
command = FETCHMAIL.format(shlex.quote(handler.name))
|
||
|
output = subprocess.check_output(command, shell=True)
|
||
|
return output
|
||
9 years ago
|
|
||
|
|
||
8 years ago
|
def run(connection, cursor, debug):
|
||
9 years ago
|
cursor.execute("""
|
||
8 years ago
|
SELECT user_email, protocol, host, port, tls, username, password, keep
|
||
9 years ago
|
FROM fetch
|
||
|
""")
|
||
|
for line in cursor.fetchall():
|
||
8 years ago
|
fetchmailrc = ""
|
||
8 years ago
|
user_email, protocol, host, port, tls, username, password, keep = line
|
||
8 years ago
|
options = "options antispam 501, 504, 550, 553, 554"
|
||
8 years ago
|
options += " ssl" if tls else ""
|
||
8 years ago
|
options += " keep" if keep else " fetchall"
|
||
9 years ago
|
fetchmailrc += RC_LINE.format(
|
||
8 years ago
|
user_email=escape_rc_string(user_email),
|
||
9 years ago
|
protocol=protocol,
|
||
8 years ago
|
host=escape_rc_string(host),
|
||
9 years ago
|
port=port,
|
||
8 years ago
|
username=escape_rc_string(username),
|
||
|
password=escape_rc_string(password),
|
||
9 years ago
|
options=options
|
||
|
)
|
||
8 years ago
|
if debug:
|
||
8 years ago
|
print(fetchmailrc)
|
||
8 years ago
|
try:
|
||
8 years ago
|
print(fetchmail(fetchmailrc))
|
||
8 years ago
|
error_message = ""
|
||
|
except subprocess.CalledProcessError as error:
|
||
8 years ago
|
error_message = error.output.decode("utf8")
|
||
8 years ago
|
# No mail is not an error
|
||
8 years ago
|
if not error_message.startswith("fetchmail: No mail"):
|
||
8 years ago
|
print(error_message)
|
||
|
user_info = "for %s at %s" % (user_email, host)
|
||
8 years ago
|
# Number of messages seen is not a error as well
|
||
8 years ago
|
if ("messages" in error_message and
|
||
|
"(seen " in error_message and
|
||
|
user_info in error_message):
|
||
|
print(error_message)
|
||
8 years ago
|
finally:
|
||
|
cursor.execute("""
|
||
|
UPDATE fetch SET error=?, last_check=datetime('now')
|
||
|
WHERE user_email=?
|
||
|
""", (error_message.split("\n")[0], user_email))
|
||
|
connection.commit()
|
||
|
|
||
9 years ago
|
|
||
|
if __name__ == "__main__":
|
||
8 years ago
|
debug = os.environ.get("DEBUG", None) == "True"
|
||
8 years ago
|
db_path = os.environ.get("DB_PATH", "/data/main.db")
|
||
9 years ago
|
connection = sqlite3.connect(db_path)
|
||
|
while True:
|
||
|
cursor = connection.cursor()
|
||
8 years ago
|
run(connection, cursor, debug)
|
||
9 years ago
|
cursor.close()
|
||
8 years ago
|
time.sleep(int(os.environ.get("FETCHMAIL_DELAY", 60)))
|