2018-12-21 20:49:15 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# workflow
|
2018-12-22 04:46:57 +01:00
|
|
|
# start options main.py --dry-run --outfile file
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
import requests
|
|
|
|
import os
|
2018-12-22 04:46:57 +01:00
|
|
|
import sys
|
2018-12-21 20:49:15 +01:00
|
|
|
import argparse
|
2018-12-22 04:46:57 +01:00
|
|
|
from ruamel.yaml import YAML, scalarstring
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BlacklistImporter:
|
|
|
|
def __init__(self, args):
|
|
|
|
self.outfile = args.outfile
|
|
|
|
self.dryrun = args.dryrun
|
|
|
|
self.url = "https://raw.githubusercontent.com/JabberSPAM/blacklist/master/blacklist.txt"
|
|
|
|
self.blacklist = None
|
2018-12-22 04:46:57 +01:00
|
|
|
self.change = False
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
def request(self):
|
|
|
|
# check if etag header is present if not set local_etag to ""
|
|
|
|
if os.path.isfile(".etag"):
|
2018-12-22 04:46:57 +01:00
|
|
|
with open(".etag", "r") as file:
|
2018-12-21 20:49:15 +01:00
|
|
|
local_etag = file.read()
|
|
|
|
else:
|
|
|
|
local_etag = ""
|
|
|
|
|
|
|
|
with requests.Session() as s:
|
|
|
|
# head request to check etag
|
|
|
|
head = s.head(self.url)
|
|
|
|
etag = head.headers['etag']
|
|
|
|
|
2019-02-15 19:39:27 +01:00
|
|
|
# if etags match up or if a connection is not possible fall back to local cache
|
|
|
|
if local_etag == etag or head.status_code != 200:
|
|
|
|
# check if local cache is present
|
|
|
|
if os.path.isfile("blacklist.txt"):
|
2018-12-22 04:46:57 +01:00
|
|
|
with open("blacklist.txt", "r", encoding="utf-8") as file:
|
2019-02-15 19:39:27 +01:00
|
|
|
self.blacklist = file.read()
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
# in any other case request a new file
|
|
|
|
else:
|
|
|
|
r = s.get(self.url)
|
|
|
|
r.encoding = 'utf-8'
|
|
|
|
local_etag = head.headers['etag']
|
2018-12-22 04:46:57 +01:00
|
|
|
self.blacklist = r.content.decode()
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
with open("blacklist.txt", "w") as file:
|
2018-12-22 04:46:57 +01:00
|
|
|
file.write(self.blacklist)
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
with open('.etag', 'w') as string:
|
|
|
|
string.write(local_etag)
|
|
|
|
|
|
|
|
def main(self):
|
|
|
|
# first check if blacklist is updated
|
|
|
|
self.request()
|
|
|
|
|
|
|
|
if self.dryrun:
|
|
|
|
# only output the selected software and outfile
|
|
|
|
print("outfile selected: %s" % self.outfile)
|
|
|
|
|
2018-12-22 04:46:57 +01:00
|
|
|
# select ejabberd processing
|
|
|
|
self.process()
|
2018-12-21 20:49:15 +01:00
|
|
|
|
2018-12-22 04:46:57 +01:00
|
|
|
# reload config if changes have been applied
|
|
|
|
if self.change:
|
|
|
|
os.system("ejabberdctl reload_config")
|
2018-12-21 20:49:15 +01:00
|
|
|
|
2018-12-22 04:46:57 +01:00
|
|
|
def process(self):
|
2018-12-22 14:57:13 +01:00
|
|
|
# init new YAML variable
|
|
|
|
local_file = YAML(typ="safe")
|
2019-03-04 01:17:02 +01:00
|
|
|
|
|
|
|
# prevent None errors
|
|
|
|
if self.outfile is not None:
|
|
|
|
# catch FileNotFoundError on first run or file missing
|
|
|
|
try:
|
|
|
|
local_file = local_file.load(open(self.outfile, "r", encoding="utf-8"))
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
remote_file = {
|
|
|
|
"acl": {
|
|
|
|
"spamblacklist": {
|
|
|
|
"server": []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for entry in self.blacklist.split():
|
2018-12-22 04:46:57 +01:00
|
|
|
entry = scalarstring.DoubleQuotedScalarString(entry)
|
2018-12-21 20:49:15 +01:00
|
|
|
remote_file["acl"]["spamblacklist"]["server"].append(entry)
|
|
|
|
|
2018-12-22 04:46:57 +01:00
|
|
|
yml = YAML()
|
|
|
|
yml.indent(offset=2)
|
|
|
|
yml.default_flow_style = False
|
|
|
|
|
2018-12-21 20:49:15 +01:00
|
|
|
if self.dryrun:
|
2018-12-22 04:46:57 +01:00
|
|
|
# if dryrun true print expected content
|
|
|
|
yml.dump(remote_file, sys.stdout)
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
elif local_file != remote_file:
|
2018-12-22 04:46:57 +01:00
|
|
|
self.change = True
|
2018-12-22 14:57:13 +01:00
|
|
|
# only if the local_file and remote_file are unequal write new file
|
2018-12-22 04:46:57 +01:00
|
|
|
yml.dump(remote_file, open(self.outfile, "w"))
|
2018-12-21 20:49:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('-o', '--outfile', help='set path to output file', dest='outfile', default=None)
|
2018-12-22 04:46:57 +01:00
|
|
|
parser.add_argument('-dr', '--dry-run', help='perform a dry run', action='store_true', dest='dryrun', default=False)
|
2018-12-21 20:49:15 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2018-12-22 04:46:57 +01:00
|
|
|
# run
|
2018-12-21 20:49:15 +01:00
|
|
|
BlacklistImporter(args).main()
|