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.
95 lines
2.9 KiB
Python
95 lines
2.9 KiB
Python
6 years ago
|
#!/usr/bin/python3
|
||
9 years ago
|
|
||
|
import time
|
||
|
import os
|
||
|
import tempfile
|
||
8 years ago
|
import shlex
|
||
8 years ago
|
import subprocess
|
||
7 years ago
|
import re
|
||
6 years ago
|
import requests
|
||
9 years ago
|
|
||
|
|
||
9 years ago
|
FETCHMAIL = """
|
||
|
fetchmail -N \
|
||
|
--sslcertck --sslcertpath /etc/ssl/certs \
|
||
|
-f {}
|
||
|
"""
|
||
|
|
||
6 years ago
|
|
||
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}"
|
||
7 years ago
|
smtphost "{smtphost}"
|
||
9 years ago
|
{options}
|
||
|
"""
|
||
|
|
||
6 years ago
|
|
||
7 years ago
|
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
|
||
8 years ago
|
|
||
6 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
|
|
||
|
|
||
6 years ago
|
def run(debug):
|
||
|
fetches = requests.get("http://admin/internal/fetch").json()
|
||
7 years ago
|
smtphost, smtpport = extract_host_port(os.environ.get("HOST_SMTP", "smtp"), None)
|
||
|
if smtpport is None:
|
||
|
smtphostport = smtphost
|
||
|
else:
|
||
|
smtphostport = "%s/%d" % (smtphost, smtpport)
|
||
6 years ago
|
for fetch in fetches:
|
||
8 years ago
|
fetchmailrc = ""
|
||
8 years ago
|
options = "options antispam 501, 504, 550, 553, 554"
|
||
6 years ago
|
options += " sslmode wrapped" if fetch["tls"] else ""
|
||
6 years ago
|
options += " keep" if fetch["keep"] else " fetchall"
|
||
9 years ago
|
fetchmailrc += RC_LINE.format(
|
||
6 years ago
|
user_email=escape_rc_string(fetch["user_email"]),
|
||
|
protocol=fetch["protocol"],
|
||
|
host=escape_rc_string(fetch["host"]),
|
||
|
port=fetch["port"],
|
||
7 years ago
|
smtphost=smtphostport,
|
||
6 years ago
|
username=escape_rc_string(fetch["username"]),
|
||
|
password=escape_rc_string(fetch["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)
|
||
6 years ago
|
user_info = "for %s at %s" % (fetch["user_email"], fetch["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:
|
||
6 years ago
|
requests.post("http://admin/internal/fetch/{}".format(fetch["id"]),
|
||
|
json=error_message.split("\n")[0]
|
||
|
)
|
||
8 years ago
|
|
||
9 years ago
|
|
||
|
if __name__ == "__main__":
|
||
|
while True:
|
||
8 years ago
|
time.sleep(int(os.environ.get("FETCHMAIL_DELAY", 60)))
|
||
6 years ago
|
run(os.environ.get("DEBUG", None) == "True")
|
||
|
|