From a198f2a48782eb2183bb32c71cce4d1ed3d2d000 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 5 Sep 2022 07:20:30 +0100 Subject: [PATCH] Reformat legacy project --- legacy/api/views.py | 87 ++++++++++++---- legacy/commands/admall.py | 4 +- legacy/commands/alias.py | 7 +- legacy/commands/all.py | 9 +- legacy/commands/allc.py | 12 ++- legacy/commands/authcheck.py | 12 ++- legacy/commands/auto.py | 24 ++++- legacy/commands/blacklist.py | 7 +- legacy/commands/chans.py | 4 +- legacy/commands/cmd.py | 4 +- legacy/commands/confirm.py | 9 +- legacy/commands/dedup.py | 4 +- legacy/commands/disable.py | 4 +- legacy/commands/dist.py | 4 +- legacy/commands/email.py | 50 ++++++--- legacy/commands/enable.py | 4 +- legacy/commands/exec.py | 4 +- legacy/commands/getstr.py | 4 +- legacy/commands/help.py | 4 +- legacy/commands/join.py | 4 +- legacy/commands/list.py | 4 +- legacy/commands/load.py | 4 +- legacy/commands/loadmod.py | 4 +- legacy/commands/logout.py | 4 +- legacy/commands/mod.py | 8 +- legacy/commands/msg.py | 9 +- legacy/commands/network.py | 11 +- legacy/commands/part.py | 4 +- legacy/commands/pass.py | 4 +- legacy/commands/pending.py | 14 ++- legacy/commands/recheckauth.py | 4 +- legacy/commands/reg.py | 4 +- legacy/commands/relay.py | 21 ++-- legacy/commands/save.py | 4 +- legacy/commands/stats.py | 4 +- legacy/commands/swho.py | 4 +- legacy/commands/token.py | 7 +- legacy/commands/users.py | 4 +- legacy/commands/who.py | 4 +- legacy/core/bot.py | 135 +++++++++++++++++++------ legacy/core/logstash.py | 1 - legacy/core/parser.py | 4 +- legacy/core/relay.py | 12 ++- legacy/core/server.py | 8 +- legacy/main.py | 4 +- legacy/modules/chankeep.py | 33 ++++-- legacy/modules/counters.py | 3 +- legacy/modules/monitor.py | 4 +- legacy/modules/network.py | 5 +- legacy/modules/provision.py | 27 +++-- legacy/modules/regproc.py | 8 +- legacy/modules/userinfo.py | 32 ++++-- legacy/tests/test_chankeep.py | 4 +- legacy/threshold | 24 +++-- legacy/utils/cleanup.py | 3 +- legacy/utils/dedup.py | 17 +++- legacy/utils/deliver_relay_commands.py | 15 +-- legacy/utils/loaders/command_loader.py | 8 +- legacy/utils/loaders/single_loader.py | 4 +- 59 files changed, 537 insertions(+), 197 deletions(-) diff --git a/legacy/api/views.py b/legacy/api/views.py index 443e546..83ab8d3 100644 --- a/legacy/api/views.py +++ b/legacy/api/views.py @@ -2,12 +2,11 @@ import functools from json import JSONDecodeError, dumps, loads from string import digits -from klein import Klein -from twisted.web.server import Request - import main +from klein import Klein from modules import chankeep, helpers, provision, regproc, userinfo from modules.network import Network +from twisted.web.server import Request from utils.logging.log import warn @@ -217,7 +216,12 @@ class API(object): for net_name in nets: conns = helpers.get_connected_relays(net_name) if not conns: - return dumps({"success": False, "reason": f"failed to get instances for {net_name}."}) + return dumps( + { + "success": False, + "reason": f"failed to get instances for {net_name}.", + } + ) if func == "recheckauth": for conn in conns: conn.regPing(reset=True) @@ -279,18 +283,24 @@ class API(object): elif item == "last": last = data[item][0] if not last.isdigit(): - return dumps({"success": False, "reason": "invalid last: not a number."}) + return dumps( + {"success": False, "reason": "invalid last: not a number."} + ) elif item == "port": port = data[item][0] if not port.isdigit(): - return dumps({"success": False, "reason": "invalid port: not a number."}) + return dumps( + {"success": False, "reason": "invalid port: not a number."} + ) port = int(port) elif item == "chanlimit": chanlimit = data[item][0] if chanlimit == "None": chanlimit = None elif not chanlimit.isdigit(): - return dumps({"success": False, "reason": "invalid chanlimit: not a number."}) + return dumps( + {"success": False, "reason": "invalid chanlimit: not a number."} + ) else: chanlimit = int(chanlimit) online_relays = helpers.get_connected_relays(net) @@ -324,9 +334,16 @@ class API(object): if item == "net": net = data[item] if net in main.network.keys(): - return dumps({"success": False, "reason": "network already exists."}) + return dumps( + {"success": False, "reason": "network already exists."} + ) if set(net).intersection(set(digits)): - return dumps({"success": False, "reason": "network name cannot contain numbers."}) + return dumps( + { + "success": False, + "reason": "network name cannot contain numbers.", + } + ) elif item == "auth": auth = data[item] if auth not in ["sasl", "ns", "none"]: @@ -336,7 +353,9 @@ class API(object): elif item == "port": port = data[item] if not port.isdigit(): - return dumps({"success": False, "reason": "invalid port: not a number."}) + return dumps( + {"success": False, "reason": "invalid port: not a number."} + ) port = int(port) elif item == "security": security = data[item] @@ -408,7 +427,9 @@ class API(object): num = int(num) net_inst = main.network[net] if num in net_inst.relays: - return dumps({"success": False, "reason": "network already has this relay."}) + return dumps( + {"success": False, "reason": "network already has this relay."} + ) id, alias = net_inst.add_relay(num) main.saveConf("network") return dumps({"success": True, "id": id, "alias": alias}) @@ -433,7 +454,9 @@ class API(object): num = int(num) net_inst = main.network[net] if num not in net_inst.relays: - return dumps({"success": False, "reason": "network does not have this relay."}) + return dumps( + {"success": False, "reason": "network does not have this relay."} + ) net_inst.delete_relay(num) main.saveConf("network") return dumps({"success": True}) @@ -448,7 +471,9 @@ class API(object): num = int(num) net_inst = main.network[net] if num not in net_inst.relays: - return dumps({"success": False, "reason": "network does not have this relay."}) + return dumps( + {"success": False, "reason": "network does not have this relay."} + ) provision.provisionRelay(num, net) return dumps({"success": True}) @@ -465,7 +490,11 @@ class API(object): net_chans = main.IRCPool[name].channels channels_annotated = userinfo.getUserNum(net, net_chans) for channel in net_chans: - channel_inst = {"name": channel, "users": channels_annotated[channel], "num": num} + channel_inst = { + "name": channel, + "users": channels_annotated[channel], + "num": num, + } channels.append(channel_inst) # channels[channel] = channels_annotated[channel] @@ -504,7 +533,9 @@ class API(object): return dumps({"success": False, "reason": "no such net."}) joined = chankeep.joinSingle(net, channel) if not joined: - return dumps({"success": False, "reason": "could not allocate channel to relay."}) + return dumps( + {"success": False, "reason": "could not allocate channel to relay."} + ) return dumps({"success": True, "relays": joined}) @app.route("/irc/network//channel//", methods=["PUT"]) @@ -514,7 +545,9 @@ class API(object): return dumps({"success": False, "reason": "no such net."}) joined = chankeep.joinSingle(net, channel) if not joined: - return dumps({"success": False, "reason": "could not allocate channel to relay."}) + return dumps( + {"success": False, "reason": "could not allocate channel to relay."} + ) return dumps({"success": True, "relays": joined}) @app.route("/aliases/", methods=["GET"]) @@ -563,7 +596,12 @@ class API(object): provision.provisionRelay(num, net) main.saveConf("network") - return dumps({"success": True, "message": f"created relay {num} with alias {alias} on {net}"}) + return dumps( + { + "success": True, + "message": f"created relay {num} with alias {alias} on {net}", + } + ) @app.route("/irc/list//", methods=["POST"]) @login_required @@ -572,9 +610,16 @@ class API(object): return dumps({"success": False, "reason": "no such net."}) first_relay = helpers.get_first_relay(net) if not first_relay: - return dumps({"success": False, "reason": f"could not get first relay for {net}"}) + return dumps( + {"success": False, "reason": f"could not get first relay for {net}"} + ) first_relay.list() - return dumps({"success": True, "message": f"requested list with instance {first_relay.num} of {net}"}) + return dumps( + { + "success": True, + "message": f"requested list with instance {first_relay.num} of {net}", + } + ) @app.route("/irc/list//", methods=["GET"]) @login_required @@ -623,7 +668,9 @@ class API(object): main.IRCPool[name].sendmsg(nick, msg, in_query=in_query) else: main.IRCPool[name].sendmsg(channel, msg, in_query=in_query) - return dumps({"success": True, "message": f"sent message to {channel} on {name}"}) + return dumps( + {"success": True, "message": f"sent message to {channel} on {name}"} + ) @app.route("/irc/nick///", methods=["GET"]) @login_required diff --git a/legacy/commands/admall.py b/legacy/commands/admall.py index 9ae8636..e4afc82 100644 --- a/legacy/commands/admall.py +++ b/legacy/commands/admall.py @@ -6,7 +6,9 @@ class AdmallCommand: def __init__(self, *args): self.admall(*args) - def admall(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def admall( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length > 2: for i in main.network.keys(): diff --git a/legacy/commands/alias.py b/legacy/commands/alias.py index fd5e644..bb50b84 100644 --- a/legacy/commands/alias.py +++ b/legacy/commands/alias.py @@ -1,14 +1,15 @@ -from yaml import dump - import main from modules import alias +from yaml import dump class AliasCommand: def __init__(self, *args): self.alias(*args) - def alias(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def alias( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 1: info(dump(main.alias)) diff --git a/legacy/commands/all.py b/legacy/commands/all.py index 509f0f5..c6d9150 100644 --- a/legacy/commands/all.py +++ b/legacy/commands/all.py @@ -6,7 +6,9 @@ class AllCommand: def __init__(self, *args): self.all(*args) - def all(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def all( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length > 2: for i in main.network.keys(): @@ -15,7 +17,10 @@ class AllCommand: net = main.network[i].relays[x]["net"] alias = main.alias[x]["nick"] commands = {spl[1]: [" ".join(spl[2:])]} - success("Sending commands to relay %s as user %s" % (num, alias + "/" + net)) + success( + "Sending commands to relay %s as user %s" + % (num, alias + "/" + net) + ) deliverRelayCommands(num, commands, user=alias + "/" + net) return else: diff --git a/legacy/commands/allc.py b/legacy/commands/allc.py index cd55596..be0a218 100644 --- a/legacy/commands/allc.py +++ b/legacy/commands/allc.py @@ -6,7 +6,9 @@ class AllcCommand: def __init__(self, *args): self.allc(*args) - def allc(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def allc( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length > 4: targets = [] @@ -20,7 +22,8 @@ class AllcCommand: [ targets.append((i, x)) for x in main.alias.keys() - if main.alias[x]["nick"] == spl[2] and x in main.network[i].aliases.keys() + if main.alias[x]["nick"] == spl[2] + and x in main.network[i].aliases.keys() ] else: incUsage("allc") @@ -33,7 +36,10 @@ class AllcCommand: num = i[1] alias = main.alias[num]["nick"] commands = {spl[3]: [" ".join(spl[4:])]} - success("Sending commands to relay %i as user %s" % (num, alias + "/" + net)) + success( + "Sending commands to relay %i as user %s" + % (num, alias + "/" + net) + ) deliverRelayCommands(num, commands, user=alias + "/" + net) return else: diff --git a/legacy/commands/authcheck.py b/legacy/commands/authcheck.py index 26cc14e..7c41dd1 100644 --- a/legacy/commands/authcheck.py +++ b/legacy/commands/authcheck.py @@ -5,7 +5,9 @@ class AuthcheckCommand: def __init__(self, *args): self.authcheck(*args) - def authcheck(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def authcheck( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 1: results = [] @@ -13,7 +15,9 @@ class AuthcheckCommand: num = main.IRCPool[i].num net = main.IRCPool[i].net if not main.IRCPool[i].authenticated: - results.append("%s - %s: %s" % (net, num, main.alias[num]["nick"])) + results.append( + "%s - %s: %s" % (net, num, main.alias[num]["nick"]) + ) info("\n".join(results)) return elif length == 2: @@ -27,7 +31,9 @@ class AuthcheckCommand: if not net == spl[1]: continue if not main.IRCPool[i].authenticated: - results.append("%s - %s: %s" % (net, num, main.alias[num]["nick"])) + results.append( + "%s - %s: %s" % (net, num, main.alias[num]["nick"]) + ) info("\n".join(results)) return else: diff --git a/legacy/commands/auto.py b/legacy/commands/auto.py index ffc6df5..378a102 100644 --- a/legacy/commands/auto.py +++ b/legacy/commands/auto.py @@ -6,7 +6,9 @@ class AutoCommand: def __init__(self, *args): self.auto(*args) - def auto(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def auto( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 1: for i in main.network.keys(): @@ -14,9 +16,15 @@ class AutoCommand: info("Skipping %s - first relay exists" % i) else: num, alias = main.network[i].add_relay(1) - success("Successfully created first relay on network %s with alias %s" % (i, alias)) + success( + "Successfully created first relay on network %s with alias %s" + % (i, alias) + ) provision.provisionRelay(num, i) - success("Started provisioning network %s on first relay for alias %s" % (i, alias)) + success( + "Started provisioning network %s on first relay for alias %s" + % (i, alias) + ) main.saveConf("network") return elif length == 2: @@ -27,9 +35,15 @@ class AutoCommand: failure("First relay exists on %s" % spl[1]) return num, alias = main.network[spl[1]].add_relay(1) - success("Successfully created relay %i on network %s with alias %s" % (num, spl[1], alias)) + success( + "Successfully created relay %i on network %s with alias %s" + % (num, spl[1], alias) + ) provision.provisionRelay(num, spl[1]) - success("Started provisioning network %s on relay %s for alias %s" % (spl[1], num, alias)) + success( + "Started provisioning network %s on relay %s for alias %s" + % (spl[1], num, alias) + ) main.saveConf("network") return else: diff --git a/legacy/commands/blacklist.py b/legacy/commands/blacklist.py index 7dc3c19..20efae4 100644 --- a/legacy/commands/blacklist.py +++ b/legacy/commands/blacklist.py @@ -1,13 +1,14 @@ -from yaml import dump - import main +from yaml import dump class BlacklistCommand: def __init__(self, *args): self.blacklist(*args) - def blacklist(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def blacklist( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 1: info(dump(main.blacklist)) diff --git a/legacy/commands/chans.py b/legacy/commands/chans.py index 663d82a..01277b5 100644 --- a/legacy/commands/chans.py +++ b/legacy/commands/chans.py @@ -5,7 +5,9 @@ class ChansCommand: def __init__(self, *args): self.chans(*args) - def chans(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def chans( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if len(spl) < 2: incUsage("chans") diff --git a/legacy/commands/cmd.py b/legacy/commands/cmd.py index e269da2..860886a 100644 --- a/legacy/commands/cmd.py +++ b/legacy/commands/cmd.py @@ -5,7 +5,9 @@ class CmdCommand: def __init__(self, *args): self.cmd(*args) - def cmd(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def cmd( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length > 4: if not spl[1].isdigit(): diff --git a/legacy/commands/confirm.py b/legacy/commands/confirm.py index 86d657c..53a70a2 100644 --- a/legacy/commands/confirm.py +++ b/legacy/commands/confirm.py @@ -6,7 +6,9 @@ class ConfirmCommand: def __init__(self, *args): self.confirm(*args) - def confirm(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def confirm( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 4: if not spl[1] in main.network.keys(): @@ -19,7 +21,10 @@ class ConfirmCommand: failure("No such relay on %s: %s" % (spl[1], spl[2])) return regproc.confirmAccount(spl[1], int(spl[2]), spl[3]) - success("Requested confirmation on %s - %s with token %s" % (spl[1], spl[2], spl[3])) + success( + "Requested confirmation on %s - %s with token %s" + % (spl[1], spl[2], spl[3]) + ) return else: incUsage("confirm") diff --git a/legacy/commands/dedup.py b/legacy/commands/dedup.py index 0a286fc..723c23a 100644 --- a/legacy/commands/dedup.py +++ b/legacy/commands/dedup.py @@ -8,7 +8,9 @@ class DedupCommand: def __init__(self, *args): self.dedup(*args) - def dedup(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def dedup( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 1: dupes = chankeep.getDuplicateChannels() diff --git a/legacy/commands/disable.py b/legacy/commands/disable.py index 78b0984..edd58d7 100644 --- a/legacy/commands/disable.py +++ b/legacy/commands/disable.py @@ -6,7 +6,9 @@ class DisableCommand: def __init__(self, *args): self.disable(*args) - def disable(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def disable( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 3: if not spl[1] in main.network.keys(): diff --git a/legacy/commands/dist.py b/legacy/commands/dist.py index a126e07..7e94c4e 100644 --- a/legacy/commands/dist.py +++ b/legacy/commands/dist.py @@ -7,7 +7,9 @@ class DistCommand: def __init__(self, *args): self.dist(*args) - def dist(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def dist( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if main.config["Dist"]["Enabled"]: rtrn = run([main.config["Dist"]["File"]], shell=True, stdout=PIPE) diff --git a/legacy/commands/email.py b/legacy/commands/email.py index fa99fa0..d5af598 100644 --- a/legacy/commands/email.py +++ b/legacy/commands/email.py @@ -1,13 +1,14 @@ -from yaml import dump - import main +from yaml import dump class EmailCommand: def __init__(self, *args): self.email(*args) - def email(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def email( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 4: if spl[1] == "add": @@ -19,10 +20,15 @@ class EmailCommand: if not spl[3] in main.alias[num]["emails"]: main.alias[num]["emails"].append(spl[3]) main.saveConf("alias") - success("Successfully added email %s to alias %i" % (spl[3], num)) + success( + "Successfully added email %s to alias %i" + % (spl[3], num) + ) return else: - failure("Email already exists in alias %i: %s" % (num, spl[3])) + failure( + "Email already exists in alias %i: %s" % (num, spl[3]) + ) return else: failure("Must be a number, not %s" % spl[2]) @@ -33,10 +39,16 @@ class EmailCommand: return if domain not in main.irc["_"]["domains"]: main.irc["_"]["domains"].append(domain) - success("Successfully added domain %s to default config" % domain) + success( + "Successfully added domain %s to default config" + % domain + ) main.saveConf("irc") else: - failure("Domain already exists in default config: %s" % domain) + failure( + "Domain already exists in default config: %s" + % domain + ) return elif spl[1] == "del": @@ -47,10 +59,16 @@ class EmailCommand: if domain in main.irc["_"]["domains"]: main.irc["_"]["domains"].remove(domain) - success("Successfully removed domain %s to default config" % domain) + success( + "Successfully removed domain %s to default config" + % domain + ) main.saveConf("irc") else: - failure("Domain does not exist in default config: %s" % domain) + failure( + "Domain does not exist in default config: %s" + % domain + ) return elif spl[1] == "del": @@ -60,10 +78,15 @@ class EmailCommand: if spl[3] in main.alias[num]["emails"]: main.alias[num]["emails"].remove(spl[3]) main.saveConf("alias") - success("Successfully removed email %s from alias %i" % (spl[3], num)) + success( + "Successfully removed email %s from alias %i" + % (spl[3], num) + ) return else: - failure("Email does not exist in alias %i: %s" % (spl[3], num)) + failure( + "Email does not exist in alias %i: %s" % (spl[3], num) + ) return elif length == 2: if spl[1] == "list": @@ -76,7 +99,10 @@ class EmailCommand: if spl[1] == "list": if spl[2] == "domain": filtered = { - f"{k}:{k2}": v2 for k, v in main.irc.items() for k2, v2 in v.items() if k2 == "domains" + f"{k}:{k2}": v2 + for k, v in main.irc.items() + for k2, v2 in v.items() + if k2 == "domains" } info(dump(filtered)) return diff --git a/legacy/commands/enable.py b/legacy/commands/enable.py index 3553446..fb49099 100644 --- a/legacy/commands/enable.py +++ b/legacy/commands/enable.py @@ -6,7 +6,9 @@ class EnableCommand: def __init__(self, *args): self.enable(*args) - def enable(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def enable( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 3: if not spl[1] in main.network.keys(): diff --git a/legacy/commands/exec.py b/legacy/commands/exec.py index 9b7ea28..9c79eef 100644 --- a/legacy/commands/exec.py +++ b/legacy/commands/exec.py @@ -2,7 +2,9 @@ class ExecCommand: def __init__(self, *args): self.exec(*args) - def exec(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def exec( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length > 1: try: diff --git a/legacy/commands/getstr.py b/legacy/commands/getstr.py index c4fe6a9..57f3a48 100644 --- a/legacy/commands/getstr.py +++ b/legacy/commands/getstr.py @@ -6,7 +6,9 @@ class GetstrCommand: def __init__(self, *args): self.getstr(*args) - def getstr(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def getstr( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 3: net = spl[1] diff --git a/legacy/commands/help.py b/legacy/commands/help.py index 6dcf937..0ca3ff7 100644 --- a/legacy/commands/help.py +++ b/legacy/commands/help.py @@ -5,7 +5,9 @@ class HelpCommand: def __init__(self, *args): self.help(*args) - def help(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def help( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: helpMap = [] for i in main.help.keys(): diff --git a/legacy/commands/join.py b/legacy/commands/join.py index e6cbc20..d6147a0 100644 --- a/legacy/commands/join.py +++ b/legacy/commands/join.py @@ -6,7 +6,9 @@ class JoinCommand: def __init__(self, *args): self.join(*args) - def join(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def join( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 3: if not spl[1] in main.network.keys(): diff --git a/legacy/commands/list.py b/legacy/commands/list.py index 9dccb8b..e507cfa 100644 --- a/legacy/commands/list.py +++ b/legacy/commands/list.py @@ -6,7 +6,9 @@ class ListCommand: def __init__(self, *args): self.list(*args) - def list(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def list( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 1: for i in main.network.keys(): diff --git a/legacy/commands/load.py b/legacy/commands/load.py index 31e66f4..b62da89 100644 --- a/legacy/commands/load.py +++ b/legacy/commands/load.py @@ -5,7 +5,9 @@ class LoadCommand: def __init__(self, *args): self.load(*args) - def load(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def load( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 2: if spl[1] in main.filemap.keys(): diff --git a/legacy/commands/loadmod.py b/legacy/commands/loadmod.py index 0dbc8a8..f288ce3 100644 --- a/legacy/commands/loadmod.py +++ b/legacy/commands/loadmod.py @@ -5,7 +5,9 @@ class LoadmodCommand: def __init__(self, *args): self.loadmod(*args) - def loadmod(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def loadmod( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 2: rtrn = loadSingle(spl[1]) diff --git a/legacy/commands/logout.py b/legacy/commands/logout.py index 776dfd0..91e1e9d 100644 --- a/legacy/commands/logout.py +++ b/legacy/commands/logout.py @@ -2,7 +2,9 @@ class LogoutCommand: def __init__(self, *args): self.logout(*args) - def logout(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def logout( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: obj.authed = False success("Logged out") diff --git a/legacy/commands/mod.py b/legacy/commands/mod.py index 85e7814..d481382 100644 --- a/legacy/commands/mod.py +++ b/legacy/commands/mod.py @@ -6,7 +6,9 @@ class ModCommand: def __init__(self, *args): self.mod(*args) - def mod(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def mod( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 4: if not spl[1] in main.network.keys(): @@ -20,7 +22,9 @@ class ModCommand: return main.saveConf("network") - success("Successfully set key %s to %s on %s" % (spl[2], spl[3], spl[1])) + success( + "Successfully set key %s to %s on %s" % (spl[2], spl[3], spl[1]) + ) return # Find a better way to do this # elif length == 6: diff --git a/legacy/commands/msg.py b/legacy/commands/msg.py index 452a532..5b3f231 100644 --- a/legacy/commands/msg.py +++ b/legacy/commands/msg.py @@ -5,7 +5,9 @@ class MsgCommand: def __init__(self, *args): self.msg(*args) - def msg(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def msg( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length >= 5: if not spl[1] in main.network.keys(): @@ -20,7 +22,10 @@ class MsgCommand: if not spl[3] in main.IRCPool[spl[1] + spl[2]].channels: info("Bot not on channel: %s" % spl[3]) main.IRCPool[spl[1] + spl[2]].msg(spl[3], " ".join(spl[4:])) - success("Sent %s to %s on relay %s on network %s" % (" ".join(spl[4:]), spl[3], spl[2], spl[1])) + success( + "Sent %s to %s on relay %s on network %s" + % (" ".join(spl[4:]), spl[3], spl[2], spl[1]) + ) return else: incUsage("msg") diff --git a/legacy/commands/network.py b/legacy/commands/network.py index a1da160..3c6f0d6 100644 --- a/legacy/commands/network.py +++ b/legacy/commands/network.py @@ -1,16 +1,17 @@ from string import digits -from yaml import dump - import main from modules.network import Network +from yaml import dump class NetworkCommand: def __init__(self, *args): self.network(*args) - def network(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def network( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 7: if spl[1] == "add": @@ -30,7 +31,9 @@ class NetworkCommand: failure("Auth must be sasl, ns or none, not %s" % spl[5]) return else: - main.network[spl[2]] = Network(spl[2], spl[3], int(spl[4]), spl[5].lower(), spl[6].lower()) + main.network[spl[2]] = Network( + spl[2], spl[3], int(spl[4]), spl[5].lower(), spl[6].lower() + ) success("Successfully created network: %s" % spl[2]) main.saveConf("network") return diff --git a/legacy/commands/part.py b/legacy/commands/part.py index 249c32a..5a2de7e 100644 --- a/legacy/commands/part.py +++ b/legacy/commands/part.py @@ -5,7 +5,9 @@ class PartCommand: def __init__(self, *args): self.part(*args) - def part(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def part( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 4: if not spl[1] in main.network.keys(): diff --git a/legacy/commands/pass.py b/legacy/commands/pass.py index e37a51e..4b9e601 100644 --- a/legacy/commands/pass.py +++ b/legacy/commands/pass.py @@ -5,7 +5,9 @@ class PassCommand: def __init__(self, *args): self.password(*args) - def password(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def password( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: info("You are already authenticated") return diff --git a/legacy/commands/pending.py b/legacy/commands/pending.py index b65f2ad..5f962dd 100644 --- a/legacy/commands/pending.py +++ b/legacy/commands/pending.py @@ -5,14 +5,19 @@ class PendingCommand: def __init__(self, *args): self.pending(*args) - def pending(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def pending( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 1: results = [] for i in main.network.keys(): for x in main.network[i].relays.keys(): if not main.network[i].relays[x]["registered"]: - results.append("%s: confirm %s %s [code]" % (main.alias[x]["nick"], i, x)) + results.append( + "%s: confirm %s %s [code]" + % (main.alias[x]["nick"], i, x) + ) info("\n".join(results)) return elif length == 2: @@ -22,7 +27,10 @@ class PendingCommand: results = [] for x in main.network[spl[1]].relays.keys(): if not main.network[spl[1]].relays[x]["registered"]: - results.append("%s: confirm %s %s [code]" % (main.alias[x]["nick"], spl[1], x)) + results.append( + "%s: confirm %s %s [code]" + % (main.alias[x]["nick"], spl[1], x) + ) info("\n".join(results)) return else: diff --git a/legacy/commands/recheckauth.py b/legacy/commands/recheckauth.py index 7cdf527..d1ea82b 100644 --- a/legacy/commands/recheckauth.py +++ b/legacy/commands/recheckauth.py @@ -5,7 +5,9 @@ class RecheckauthCommand: def __init__(self, *args): self.recheckauth(*args) - def recheckauth(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def recheckauth( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 1: for i in main.IRCPool.keys(): diff --git a/legacy/commands/reg.py b/legacy/commands/reg.py index 41bb5f0..fb3794a 100644 --- a/legacy/commands/reg.py +++ b/legacy/commands/reg.py @@ -6,7 +6,9 @@ class RegCommand: def __init__(self, *args): self.reg(*args) - def reg(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def reg( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 2: if not spl[1] in main.network.keys(): diff --git a/legacy/commands/relay.py b/legacy/commands/relay.py index 2b80f5e..5c27709 100644 --- a/legacy/commands/relay.py +++ b/legacy/commands/relay.py @@ -1,19 +1,23 @@ -from yaml import dump - import main +from yaml import dump class RelayCommand: def __init__(self, *args): self.relay(*args) - def relay(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def relay( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 3: if spl[1] == "add": if spl[2] in main.network.keys(): id, alias = main.network[spl[2]].add_relay() - success("Successfully created relay %s on network %s with alias %s" % (str(id), spl[2], alias)) + success( + "Successfully created relay %s on network %s with alias %s" + % (str(id), spl[2], alias) + ) main.saveConf("network") return else: @@ -36,7 +40,10 @@ class RelayCommand: failure("Must be a number, not %s" % spl[3]) return id, alias = main.network[spl[2]].add_relay(int(spl[3])) - success("Successfully created relay %s on network %s with alias %s" % (str(id), spl[2], alias)) + success( + "Successfully created relay %s on network %s with alias %s" + % (str(id), spl[2], alias) + ) main.saveConf("network") return else: @@ -53,7 +60,9 @@ class RelayCommand: failure("No such relay: %s on network %s" % (spl[3], spl[2])) return main.network[spl[2]].delete_relay(int(spl[3])) - success("Successfully deleted relay %s on network %s" % (spl[3], spl[2])) + success( + "Successfully deleted relay %s on network %s" % (spl[3], spl[2]) + ) main.saveConf("network") return else: diff --git a/legacy/commands/save.py b/legacy/commands/save.py index 12624cd..0dd22b6 100644 --- a/legacy/commands/save.py +++ b/legacy/commands/save.py @@ -5,7 +5,9 @@ class SaveCommand: def __init__(self, *args): self.save(*args) - def save(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def save( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 2: if spl[1] in main.filemap.keys(): diff --git a/legacy/commands/stats.py b/legacy/commands/stats.py index e2e8eb9..af89ac8 100644 --- a/legacy/commands/stats.py +++ b/legacy/commands/stats.py @@ -9,7 +9,9 @@ class StatsCommand: def __init__(self, *args): self.stats(*args) - def stats(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def stats( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 1: stats = [] diff --git a/legacy/commands/swho.py b/legacy/commands/swho.py index 18dbc12..aa3a78c 100644 --- a/legacy/commands/swho.py +++ b/legacy/commands/swho.py @@ -5,7 +5,9 @@ class SwhoCommand: def __init__(self, *args): self.swho(*args) - def swho(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def swho( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 2: if not spl[1] in main.network.keys(): diff --git a/legacy/commands/token.py b/legacy/commands/token.py index 738f537..989e760 100644 --- a/legacy/commands/token.py +++ b/legacy/commands/token.py @@ -1,15 +1,16 @@ from uuid import uuid4 -from yaml import dump - import main +from yaml import dump class TokenCommand: def __init__(self, *args): self.token(*args) - def token(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def token( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 2: if spl[1] == "list": diff --git a/legacy/commands/users.py b/legacy/commands/users.py index be7a7b1..34ac46c 100644 --- a/legacy/commands/users.py +++ b/legacy/commands/users.py @@ -5,7 +5,9 @@ class UsersCommand: def __init__(self, *args): self.users(*args) - def users(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def users( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if len(spl) < 2: incUsage("users") diff --git a/legacy/commands/who.py b/legacy/commands/who.py index 10faa93..4046210 100644 --- a/legacy/commands/who.py +++ b/legacy/commands/who.py @@ -5,7 +5,9 @@ class WhoCommand: def __init__(self, *args): self.who(*args) - def who(self, addr, authed, data, obj, spl, success, failure, info, incUsage, length): + def who( + self, addr, authed, data, obj, spl, success, failure, info, incUsage, length + ): if authed: if length == 2: result = userinfo.getWho(spl[1]) diff --git a/legacy/core/bot.py b/legacy/core/bot.py index d4b095d..d62decb 100644 --- a/legacy/core/bot.py +++ b/legacy/core/bot.py @@ -3,6 +3,9 @@ from copy import deepcopy from datetime import datetime from random import randint +import main +from core.relay import sendRelayNotification +from modules import chankeep, counters, helpers, monitor, regproc, userinfo from twisted.internet import reactor from twisted.internet.defer import Deferred from twisted.internet.protocol import ReconnectingClientFactory @@ -13,10 +16,6 @@ from twisted.words.protocols.irc import ( lowDequote, numeric_to_symbolic, ) - -import main -from core.relay import sendRelayNotification -from modules import chankeep, counters, helpers, monitor, regproc, userinfo from utils.dedup import dedup from utils.logging.debug import debug, trace from utils.logging.log import error, log, warn @@ -83,7 +82,9 @@ class IRCBot(IRCClient): self.listAttempted = False # we asked for a list self.listSimple = False # after asking again we got the list, so use the simple # syntax from now on - self.wantList = False # we want to send a LIST, but not all relays are active yet + self.wantList = ( + False # we want to send a LIST, but not all relays are active yet + ) self.chanlimit = 0 self.prefix = {} self.servername = None @@ -115,9 +116,14 @@ class IRCBot(IRCClient): if i not in self.channels: if self.net in main.blacklist.keys(): if i in main.blacklist[self.net]: - debug("Not joining blacklisted channel %s on %s - %i" % (i, self.net, self.num)) + debug( + "Not joining blacklisted channel %s on %s - %i" + % (i, self.net, self.num) + ) continue - trace(self.net, "-", self.num, ": joining", i, "in", sleeptime, "seconds") + trace( + self.net, "-", self.num, ": joining", i, "in", sleeptime, "seconds" + ) reactor.callLater(sleeptime, self.join, i) sleeptime += increment if sleeptime == 10: @@ -125,13 +131,19 @@ class IRCBot(IRCClient): increment = 0.7 increment += 0.1 else: - error("%s - Cannot join channel we are already on: %s - %i" % (i, self.net, self.num)) + error( + "%s - Cannot join channel we are already on: %s - %i" + % (i, self.net, self.num) + ) def checkChannels(self): if chankeep.allRelaysActive(self.net): debug(f"checkChannels() all relays active for {self.net}") else: - debug("checkChannels() skipping channel check as we have inactive relays: %s - %i" % (self.net, self.num)) + debug( + "checkChannels() skipping channel check as we have inactive relays: %s - %i" + % (self.net, self.num) + ) return if self.net in main.TempChan.keys(): if self.num in main.TempChan[self.net].keys(): @@ -145,7 +157,9 @@ class IRCBot(IRCClient): cast["ts"] = str(datetime.now().isoformat()) # remove odd stuff - for i in list(cast.keys()): # Make a copy of the .keys() as Python 3 cannot handle iterating over + for i in list( + cast.keys() + ): # Make a copy of the .keys() as Python 3 cannot handle iterating over if cast[i] == "": # a dictionary that changes length with each iteration del cast[i] # remove server stuff @@ -201,7 +215,9 @@ class IRCBot(IRCClient): castDup["type"] = "highlight" self.event(**castDup) else: - if cast["channel"].lower() == self.nickname.lower() or not cast["channel"].startswith("#"): + if cast["channel"].lower() == self.nickname.lower() or not cast[ + "channel" + ].startswith("#"): cast["mtype"] = cast["type"] cast["type"] = "query" # self.event(**castDup) @@ -277,10 +293,33 @@ class IRCBot(IRCClient): nick, ident, host = parsen(hostmask) # We sent someone a query reply if in_query: - self.event(type="self", mtype="msg", channel=self.nickname, nick=channel, ident=ident, host=host, msg=msg) + self.event( + type="self", + mtype="msg", + channel=self.nickname, + nick=channel, + ident=ident, + host=host, + msg=msg, + ) else: - self.event(type="self", mtype="msg", channel=channel, nick=self.nickname, ident=ident, host=host, msg=msg) - self.event(type="msg", channel=channel, nick=self.nickname, ident=ident, host=host, msg=msg) + self.event( + type="self", + mtype="msg", + channel=channel, + nick=self.nickname, + ident=ident, + host=host, + msg=msg, + ) + self.event( + type="msg", + channel=channel, + nick=self.nickname, + ident=ident, + host=host, + msg=msg, + ) self.msg(channel, msg) def get(self, var): @@ -309,7 +348,9 @@ class IRCBot(IRCClient): def irc_ERR_PASSWDMISMATCH(self, prefix, params): log("%s - %i: password mismatch as %s" % (self.net, self.num, self.username)) - sendAll("%s - %i: password mismatch as %s" % (self.net, self.num, self.username)) + sendAll( + "%s - %i: password mismatch as %s" % (self.net, self.num, self.username) + ) def _who(self, channel): d = Deferred() @@ -424,12 +465,17 @@ class IRCBot(IRCClient): debug("Will not send LIST, unauthenticated: %s - %i" % (self.net, self.num)) return if self.listAttempted: - debug("List request dropped, already asked for LIST - %s - %i" % (self.net, self.num)) + debug( + "List request dropped, already asked for LIST - %s - %i" + % (self.net, self.num) + ) return else: self.listAttempted = True if self.listOngoing: - debug("LIST request dropped, already ongoing - %s - %i" % (self.net, self.num)) + debug( + "LIST request dropped, already ongoing - %s - %i" % (self.net, self.num) + ) return else: if nocheck: @@ -454,7 +500,9 @@ class IRCBot(IRCClient): self._tempList[1].append([channel, users, topic]) def irc_RPL_LISTEND(self, prefix, params): - if not len(self._tempList[0]) > 0: # there are no callbacks, can't do anything there + if ( + not len(self._tempList[0]) > 0 + ): # there are no callbacks, can't do anything there debug("We didn't ask for this LIST, discarding") self._tempList[0].clear() self._tempList[1].clear() @@ -479,7 +527,10 @@ class IRCBot(IRCClient): else: if self.listRetried: self.listRetried = False - debug("List received after retry - defaulting to simple list syntax: %s - %i" % (self.net, self.num)) + debug( + "List received after retry - defaulting to simple list syntax: %s - %i" + % (self.net, self.num) + ) self.listSimple = True def got_list(self, listinfo): @@ -500,9 +551,13 @@ class IRCBot(IRCClient): if first_relay: if first_relay.wantList is True: first_relay.list(nocheck=True) - debug(f"recheckList() asking for a list for {self.net} after final relay {self.num} connected") + debug( + f"recheckList() asking for a list for {self.net} after final relay {self.num} connected" + ) else: - debug(f"recheckList() first relay wantList is False for {self.net} ({first_relay.num})") + debug( + f"recheckList() first relay wantList is False for {self.net} ({first_relay.num})" + ) # name = self.net + "1" # if self.num == 1: # Only one instance should do a list @@ -511,7 +566,9 @@ class IRCBot(IRCClient): if self.chanlimit: if allRelays: self.list() - debug(f"recheckList() requested a list for {self.net} from {self.num}") + debug( + f"recheckList() requested a list for {self.net} from {self.num}" + ) else: debug(f"recheckList() not all relays active for {self.net}") self.wantList = True @@ -525,7 +582,9 @@ class IRCBot(IRCClient): ]: # TODO: add check for register request sent, only send it once if main.config["AutoReg"]: if not self.authenticated: - self._regAttempt = reactor.callLater(5, regproc.registerAccount, self.net, self.num) + self._regAttempt = reactor.callLater( + 5, regproc.registerAccount, self.net, self.num + ) # regproc.registerAccount(self.net, self.num) try: self.chanlimit = int(chanlimit) @@ -539,7 +598,9 @@ class IRCBot(IRCClient): self.chanlimit = net_inst_chanlimit # warn(f"Chanlimit on {self.net} too high, setting to {self.chanlimit}") - if not regproc.needToRegister(self.net): # if we need to register, only recheck on auth confirmation + if not regproc.needToRegister( + self.net + ): # if we need to register, only recheck on auth confirmation if main.config["ChanKeep"]["Enabled"]: self.recheckList() @@ -642,7 +703,9 @@ class IRCBot(IRCClient): if reset: self._negativePass = None - debug(f"regPing() {self.net} - {self.num}: _negativePass:{self._negativePass} negativepass:{negativepass}") + debug( + f"regPing() {self.net} - {self.num}: _negativePass:{self._negativePass} negativepass:{negativepass}" + ) if self._negativePass is None: # We have failed, the blacklisted message has been found if negativepass is False: @@ -665,7 +728,9 @@ class IRCBot(IRCClient): ) ) else: - debug(f"regPing() {self.net}: negative registration check - {self.num}") + debug( + f"regPing() {self.net}: negative registration check - {self.num}" + ) return else: if sinst["negative"]: @@ -678,14 +743,18 @@ class IRCBot(IRCClient): ) return else: - self._negativePass = True # if it's disabled, we still want the next block to run + self._negativePass = ( + True # if it's disabled, we still want the next block to run + ) # Only run if negativepass has completed, or exempted as above if self._negativePass: if sinst["check"]: if sinst["ping"]: self.msg(sinst["entity"], sinst["pingmsg"]) - debug(f"regPing() {self.net}: sent ping '{sinst['pingmsg']}' to {sinst['entity']} - {self.num}") + debug( + f"regPing() {self.net}: sent ping '{sinst['pingmsg']}' to {sinst['entity']} - {self.num}" + ) return else: self.authenticated = True @@ -719,7 +788,11 @@ class IRCBot(IRCClient): self.channels.append(channel) self.names(channel).addCallback(self.got_names) if main.config["Toggles"]["Who"]: - reactor.callLater(main.config["Tweaks"]["Delays"]["WhoDelay"], self.setup_who_loop, channel) + reactor.callLater( + main.config["Tweaks"]["Delays"]["WhoDelay"], + self.setup_who_loop, + channel, + ) # lc = LoopingCall(self.who, channel) # self._getWho[channel] = lc # intrange = main.config["Tweaks"]["Delays"]["WhoRange"] @@ -734,7 +807,9 @@ class IRCBot(IRCClient): lc = self._getWho[channel] lc.stop() del self._getWho[channel] - userinfo.delChannels(self.net, [channel]) # < we do not need to deduplicate this + userinfo.delChannels( + self.net, [channel] + ) # < we do not need to deduplicate this # log("Can no longer cover %s, removing records" % channel)# as it will only be matched once -- # other bots have different nicknames so diff --git a/legacy/core/logstash.py b/legacy/core/logstash.py index 1e3c2c9..4e98933 100644 --- a/legacy/core/logstash.py +++ b/legacy/core/logstash.py @@ -2,7 +2,6 @@ import logging from json import dumps import logstash - import main logger = None diff --git a/legacy/core/parser.py b/legacy/core/parser.py index da0312c..efbf025 100644 --- a/legacy/core/parser.py +++ b/legacy/core/parser.py @@ -19,7 +19,9 @@ def parseCommand(addr, authed, data): incUsage = lambda mode: incorrectUsage(addr, mode) # noqa: E731 length = len(spl) if spl[0] in main.CommandMap.keys(): - main.CommandMap[spl[0]](addr, authed, data, obj, spl, success, failure, info, incUsage, length) + main.CommandMap[spl[0]]( + addr, authed, data, obj, spl, success, failure, info, incUsage, length + ) return incUsage(None) return diff --git a/legacy/core/relay.py b/legacy/core/relay.py index 7ae0cba..eba52f1 100644 --- a/legacy/core/relay.py +++ b/legacy/core/relay.py @@ -1,8 +1,7 @@ from json import dumps, loads -from twisted.internet.protocol import Factory, Protocol - import main +from twisted.internet.protocol import Factory, Protocol from utils.logging.log import log, warn validTypes = [ @@ -118,7 +117,9 @@ class Relay(Protocol): parsed["hello"] == main.tokens[parsed["key"]]["hello"] and main.tokens[parsed["key"]]["usage"] == "relay" ): - self.sendMsg({"type": "hello", "hello": main.tokens[parsed["key"]]["counter"]}) + self.sendMsg( + {"type": "hello", "hello": main.tokens[parsed["key"]]["counter"]} + ) self.authed = True else: self.transport.loseConnection() @@ -133,7 +134,10 @@ class Relay(Protocol): def connectionLost(self, reason): self.authed = False - log("Relay connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage())) + log( + "Relay connection lost from %s:%s -- %s" + % (self.addr.host, self.addr.port, reason.getErrorMessage()) + ) if self.addr in main.relayConnections.keys(): del main.relayConnections[self.addr] else: diff --git a/legacy/core/server.py b/legacy/core/server.py index 581d237..4074843 100644 --- a/legacy/core/server.py +++ b/legacy/core/server.py @@ -1,7 +1,6 @@ -from twisted.internet.protocol import Factory, Protocol - import main from core.parser import parseCommand +from twisted.internet.protocol import Factory, Protocol from utils.logging.log import log, warn @@ -34,7 +33,10 @@ class Server(Protocol): def connectionLost(self, reason): self.authed = False - log("Connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage())) + log( + "Connection lost from %s:%s -- %s" + % (self.addr.host, self.addr.port, reason.getErrorMessage()) + ) if self.addr in main.connections.keys(): del main.connections[self.addr] else: diff --git a/legacy/main.py b/legacy/main.py index 0c7e24c..142b1fa 100644 --- a/legacy/main.py +++ b/legacy/main.py @@ -126,4 +126,6 @@ def initMain(): r = StrictRedis( unix_socket_path=config["RedisSocket"], db=config["RedisDBEphemeral"] # noqa ) # Ephemeral - flushed on quit - g = StrictRedis(unix_socket_path=config["RedisSocket"], db=config["RedisDBPersistent"]) # noqa + g = StrictRedis( + unix_socket_path=config["RedisSocket"], db=config["RedisDBPersistent"] + ) # noqa diff --git a/legacy/modules/chankeep.py b/legacy/modules/chankeep.py index d4f1b39..5235db4 100644 --- a/legacy/modules/chankeep.py +++ b/legacy/modules/chankeep.py @@ -1,10 +1,9 @@ from copy import deepcopy from math import ceil -from twisted.internet.threads import deferToThread - import main from modules import helpers +from twisted.internet.threads import deferToThread from utils.logging.debug import debug, trace from utils.logging.log import error, log, warn @@ -95,7 +94,11 @@ def getEnabledRelays(net): :rtype: list of int :return: list of enabled relay numbers """ - enabledRelays = [x for x in main.network[net].relays.keys() if main.network[net].relays[x]["enabled"]] + enabledRelays = [ + x + for x in main.network[net].relays.keys() + if main.network[net].relays[x]["enabled"] + ] # debug(f"getEnabledRelays() {net}: {enabledRelays}") return enabledRelays @@ -259,7 +262,9 @@ def emptyChanAllocate(net, flist): chan_slots_used = getTotalChans(net) max_chans = getSumChanlimit(net) - chan_slots_used trunc_list = newlist[:max_chans] - debug(f"emptyChanAllocate() {net}: newlist:{len(newlist)} trunc_list:{len(trunc_list)}") + debug( + f"emptyChanAllocate() {net}: newlist:{len(newlist)} trunc_list:{len(trunc_list)}" + ) for i in chanfree.keys(): for x in range(chanfree[i]): @@ -359,9 +364,13 @@ def keepChannels(net, listinfo, mean, sigrelay, relay): listinfo = minifyChans(net, listinfo) if not listinfo: return - if relay <= main.config["ChanKeep"]["SigSwitch"]: # we can cover all of the channels + if ( + relay <= main.config["ChanKeep"]["SigSwitch"] + ): # we can cover all of the channels coverAll = True - elif relay > main.config["ChanKeep"]["SigSwitch"]: # we cannot cover all of the channels + elif ( + relay > main.config["ChanKeep"]["SigSwitch"] + ): # we cannot cover all of the channels coverAll = False # if not sigrelay <= main.config["ChanKeep"]["MaxRelay"]: # error("Network %s is too big to cover: %i relays required" % (net, sigrelay)) @@ -389,7 +398,9 @@ def keepChannels(net, listinfo, mean, sigrelay, relay): flist = [i[0] for i in listinfo_sort] flist = flist[:max_chans] - debug(f"keepChannels() {net}: joining {len(flist)}/{len(listinfo_sort)} channels") + debug( + f"keepChannels() {net}: joining {len(flist)}/{len(listinfo_sort)} channels" + ) trace(f"keepChannels() {net}: joining:{flist}") populateChans(net, flist) else: @@ -411,8 +422,12 @@ def keepChannels(net, listinfo, mean, sigrelay, relay): siglist = siglist[:max_chans] trace(f"keepChannels() {net}: truncated siglist:{siglist}") - trace(f"keepChannels() {net}: siglist:{siglist} max_chans:{max_chans} len_sig:{len(listinfo_sort)}") - debug(f"keepChannels() {net}: joining {len(siglist)}/{len(listinfo_sort)} channels") + trace( + f"keepChannels() {net}: siglist:{siglist} max_chans:{max_chans} len_sig:{len(listinfo_sort)}" + ) + debug( + f"keepChannels() {net}: joining {len(siglist)}/{len(listinfo_sort)} channels" + ) trace(f"keepChannels() {net}: joining:{siglist}") populateChans(net, siglist) notifyJoin(net) diff --git a/legacy/modules/counters.py b/legacy/modules/counters.py index 64407e7..21ad775 100644 --- a/legacy/modules/counters.py +++ b/legacy/modules/counters.py @@ -1,6 +1,5 @@ -from twisted.internet.task import LoopingCall - import main +from twisted.internet.task import LoopingCall def event(name, eventType): diff --git a/legacy/modules/monitor.py b/legacy/modules/monitor.py index ae4905c..7c216aa 100644 --- a/legacy/modules/monitor.py +++ b/legacy/modules/monitor.py @@ -64,7 +64,9 @@ def parsemeta(numName, c): ) -def event(numName, c): # yes I'm using a short variable because otherwise it goes off the screen +def event( + numName, c +): # yes I'm using a short variable because otherwise it goes off the screen if dedup(numName, c): return diff --git a/legacy/modules/network.py b/legacy/modules/network.py index b07f831..fafaa4f 100644 --- a/legacy/modules/network.py +++ b/legacy/modules/network.py @@ -1,14 +1,13 @@ from copy import deepcopy -from twisted.internet import reactor -from twisted.internet.ssl import DefaultOpenSSLContextFactory - import main from core.bot import IRCBotFactory from modules import alias from modules.chankeep import nukeNetwork from modules.provision import provisionRelay from modules.regproc import needToRegister +from twisted.internet import reactor +from twisted.internet.ssl import DefaultOpenSSLContextFactory from utils.deliver_relay_commands import deliverRelayCommands from utils.get import getRelay from utils.logging.log import log diff --git a/legacy/modules/provision.py b/legacy/modules/provision.py index db5ddcd..b6641bd 100644 --- a/legacy/modules/provision.py +++ b/legacy/modules/provision.py @@ -1,28 +1,37 @@ -from twisted.internet import reactor - import main import modules.regproc +from twisted.internet import reactor from utils.deliver_relay_commands import deliverRelayCommands from utils.logging.log import warn -def provisionUserNetworkData(num, nick, altnick, ident, realname, network, host, port, security): +def provisionUserNetworkData( + num, nick, altnick, ident, realname, network, host, port, security +): commands = {} stage2commands = {} stage2commands["status"] = [] commands["controlpanel"] = [] user = nick.lower() - commands["controlpanel"].append("AddUser %s %s" % (user, main.config["Relay"]["Password"])) + commands["controlpanel"].append( + "AddUser %s %s" % (user, main.config["Relay"]["Password"]) + ) commands["controlpanel"].append("AddNetwork %s %s" % (user, network)) commands["controlpanel"].append("Set Nick %s %s" % (user, nick)) commands["controlpanel"].append("Set Altnick %s %s" % (user, altnick)) commands["controlpanel"].append("Set Ident %s %s" % (user, ident)) commands["controlpanel"].append("Set RealName %s %s" % (user, realname)) if security == "ssl": - commands["controlpanel"].append("SetNetwork TrustAllCerts %s %s true" % (user, network)) # Don't judge me - commands["controlpanel"].append("AddServer %s %s %s +%s" % (user, network, host, port)) + commands["controlpanel"].append( + "SetNetwork TrustAllCerts %s %s true" % (user, network) + ) # Don't judge me + commands["controlpanel"].append( + "AddServer %s %s %s +%s" % (user, network, host, port) + ) elif security == "plain": - commands["controlpanel"].append("AddServer %s %s %s %s" % (user, network, host, port)) + commands["controlpanel"].append( + "AddServer %s %s %s %s" % (user, network, host, port) + ) if not main.config["ConnectOnCreate"]: stage2commands["status"].append("Disconnect") if main.config["Toggles"]["CycleChans"]: @@ -84,7 +93,9 @@ def provisionMultipleRelays(net, relaysNeeded): if not relaysNeeded: return [] if not main.config["ChanKeep"]["Provision"]: - warn(f"Asked to create {relaysNeeded} relays for {net}, but provisioning is disabled") + warn( + f"Asked to create {relaysNeeded} relays for {net}, but provisioning is disabled" + ) return [] numsProvisioned = [] for i in range(relaysNeeded): diff --git a/legacy/modules/regproc.py b/legacy/modules/regproc.py index e9e2e1f..4dbcc41 100644 --- a/legacy/modules/regproc.py +++ b/legacy/modules/regproc.py @@ -145,9 +145,13 @@ def enableAuthentication(net, num, jump=True, run_now=False): return # uname = main.alias[num]["nick"] + "/" + net password = main.network[net].aliases[num]["password"] - provision.provisionAuthenticationData(num, nick, net, auth, password) # Set up for auth + provision.provisionAuthenticationData( + num, nick, net, auth, password + ) # Set up for auth if jump: - main.IRCPool[name].msg(main.config["Tweaks"]["ZNC"]["Prefix"] + "status", "Jump") + main.IRCPool[name].msg( + main.config["Tweaks"]["ZNC"]["Prefix"] + "status", "Jump" + ) if run_now: attemptManualAuthentication(net, num) if selectInst(net)["check"] is False: diff --git a/legacy/modules/userinfo.py b/legacy/modules/userinfo.py index 5180394..effdaf0 100644 --- a/legacy/modules/userinfo.py +++ b/legacy/modules/userinfo.py @@ -1,7 +1,6 @@ -from twisted.internet.threads import deferToThread - import main from modules import chankeep +from twisted.internet.threads import deferToThread from utils.logging.debug import debug, trace from utils.logging.log import warn from utils.parsing import parsen @@ -175,7 +174,9 @@ def delUser(name, channel, nick, user): p.srem(namespace, nick) if channels == {channel.encode()}: # can we only see them on this channel? p.delete(chanspace) # remove channel tracking entry - p.hdel("live.prefix." + name + "." + channel, nick) # remove prefix tracking entry + p.hdel( + "live.prefix." + name + "." + channel, nick + ) # remove prefix tracking entry p.hdel(mapspace, nick) # remove nick mapping entry if user: p.srem(gnamespace, user) # remove global userinfo entry @@ -200,7 +201,10 @@ def getUserByNick(name, nick): if main.r.hexists(mapspace, nick): return main.r.hget(mapspace, nick) else: - warn("Entry doesn't exist: %s on %s - attempting auxiliary lookup" % (nick, mapspace)) + warn( + "Entry doesn't exist: %s on %s - attempting auxiliary lookup" + % (nick, mapspace) + ) # return False # legacy code below - remove when map is reliable usermatch = main.r.sscan(gnamespace, match=escape(nick) + "!*", count=999999999) @@ -230,10 +234,18 @@ def renameUser(name, oldnick, olduser, newnick, newuser): p.sadd("live.who." + name + "." + i, newnick) p.hdel(mapspace, oldnick) p.hset(mapspace, newnick, newuser) - if main.r.exists("live.prefix." + name + "." + i): # if there's a prefix entry for the channel - if main.r.hexists("live.prefix." + name + "." + i, oldnick): # if the old nick is in it - mode = main.r.hget("live.prefix." + name + "." + i, oldnick) # retrieve old modes - p.hset("live.prefix." + name + "." + i, newnick, mode) # set old modes to new nickname + if main.r.exists( + "live.prefix." + name + "." + i + ): # if there's a prefix entry for the channel + if main.r.hexists( + "live.prefix." + name + "." + i, oldnick + ): # if the old nick is in it + mode = main.r.hget( + "live.prefix." + name + "." + i, oldnick + ) # retrieve old modes + p.hset( + "live.prefix." + name + "." + i, newnick, mode + ) # set old modes to new nickname if main.r.exists(chanspace): p.rename(chanspace, newchanspace) else: @@ -299,6 +311,8 @@ def delChannels(net, channels): # we have left a channel if channel in dupes[net]: if dupes[net][channel] != 0: channels.remove(channel) - debug(f"Not removing channel {channel} as {net} has {dupes[net][channel]} other relays covering it") + debug( + f"Not removing channel {channel} as {net} has {dupes[net][channel]} other relays covering it" + ) deferToThread(_delChannels, net, channels) # d.addCallback(testCallback) diff --git a/legacy/tests/test_chankeep.py b/legacy/tests/test_chankeep.py index 491a8ce..a2ddbc6 100644 --- a/legacy/tests/test_chankeep.py +++ b/legacy/tests/test_chankeep.py @@ -49,7 +49,9 @@ class TestChanKeep(TestCase): instances = 1 chanlimit = 5 max_chans = instances * chanlimit - listinfo = self.generate_listinfo(ranges=[[1000, 1, 2], [200, 400, 800], [10, 1000, 2000]]) + listinfo = self.generate_listinfo( + ranges=[[1000, 1, 2], [200, 400, 800], [10, 1000, 2000]] + ) # listinfo_num = [x[1] for x in listinfo] listlength = len(listinfo) diff --git a/legacy/threshold b/legacy/threshold index 3b4bbbf..54250a0 100755 --- a/legacy/threshold +++ b/legacy/threshold @@ -5,18 +5,17 @@ from os import getenv from signal import SIGINT, signal from sys import stderr, stdout -from twisted.internet import reactor - -# Webapp stuff -from twisted.internet.protocol import Factory -from twisted.internet.ssl import DefaultOpenSSLContextFactory - import core.logstash import main import modules.counters from api.views import API from core.relay import RelayFactory from core.server import ServerFactory +from twisted.internet import reactor + +# Webapp stuff +from twisted.internet.protocol import Factory +from twisted.internet.ssl import DefaultOpenSSLContextFactory from utils.cleanup import handler from utils.loaders.command_loader import loadCommands from utils.logging.log import log @@ -48,10 +47,14 @@ trues = ("true", "1", "t", True) # Main listener listener_address = getenv("THRESHOLD_LISTENER_HOST", main.config["Listener"]["Address"]) listener_port = int(getenv("THRESHOLD_LISTENER_PORT", main.config["Listener"]["Port"])) -listener_ssl = getenv("THRESHOLD_LISTENER_SSL", main.config["Listener"]["UseSSL"]) in trues +listener_ssl = ( + getenv("THRESHOLD_LISTENER_SSL", main.config["Listener"]["UseSSL"]) in trues +) # RelayAPI -relay_enabled = getenv("THRESHOLD_RELAY_ENABLED", main.config["RelayAPI"]["Enabled"]) in trues +relay_enabled = ( + getenv("THRESHOLD_RELAY_ENABLED", main.config["RelayAPI"]["Enabled"]) in trues +) relay_address = getenv("THRESHOLD_RELAY_HOST", main.config["RelayAPI"]["Address"]) relay_port = int(getenv("THRESHOLD_RELAY_PORT", main.config["RelayAPI"]["Port"])) relay_ssl = getenv("THRESHOLD_RELAY_SSL", main.config["RelayAPI"]["UseSSL"]) in trues @@ -102,7 +105,10 @@ if __name__ == "__main__": ), interface=relay_address, ) - log("Threshold relay running with SSL on %s:%s" % (relay_address, relay_port)) + log( + "Threshold relay running with SSL on %s:%s" + % (relay_address, relay_port) + ) else: reactor.listenTCP( relay_port, diff --git a/legacy/utils/cleanup.py b/legacy/utils/cleanup.py index 057b1bc..aef11bc 100644 --- a/legacy/utils/cleanup.py +++ b/legacy/utils/cleanup.py @@ -1,6 +1,5 @@ -from twisted.internet import reactor - import main +from twisted.internet import reactor from utils.logging.debug import debug from utils.logging.log import log diff --git a/legacy/utils/dedup.py b/legacy/utils/dedup.py index b4c44e3..5ae3617 100644 --- a/legacy/utils/dedup.py +++ b/legacy/utils/dedup.py @@ -2,9 +2,8 @@ from copy import deepcopy from datetime import datetime from json import dumps -from siphashc import siphash - import main +from siphashc import siphash from utils.logging.debug import debug @@ -12,16 +11,24 @@ def dedup(numName, b): c = deepcopy(b) if "ts" in c.keys(): del c["ts"] - c["approxtime"] = str(datetime.utcnow().timestamp())[: main.config["Tweaks"]["DedupPrecision"]] + c["approxtime"] = str(datetime.utcnow().timestamp())[ + : main.config["Tweaks"]["DedupPrecision"] + ] castHash = siphash(main.hashKey, dumps(c, sort_keys=True)) del c["approxtime"] - isDuplicate = any(castHash in main.lastEvents[x] for x in main.lastEvents.keys() if not x == numName) + isDuplicate = any( + castHash in main.lastEvents[x] + for x in main.lastEvents.keys() + if not x == numName + ) if isDuplicate: debug("Duplicate: %s" % (c)) return True if numName in main.lastEvents.keys(): main.lastEvents[numName].insert(0, castHash) - main.lastEvents[numName] = main.lastEvents[numName][0 : main.config["Tweaks"]["MaxHash"]] # noqa + main.lastEvents[numName] = main.lastEvents[numName][ + 0 : main.config["Tweaks"]["MaxHash"] + ] # noqa else: main.lastEvents[numName] = [castHash] return False diff --git a/legacy/utils/deliver_relay_commands.py b/legacy/utils/deliver_relay_commands.py index 84f30f0..6caf436 100644 --- a/legacy/utils/deliver_relay_commands.py +++ b/legacy/utils/deliver_relay_commands.py @@ -1,13 +1,12 @@ from datetime import datetime +import main +from core.relay import sendRelayNotification +from modules import userinfo from twisted.internet import reactor from twisted.internet.protocol import ReconnectingClientFactory from twisted.internet.ssl import DefaultOpenSSLContextFactory from twisted.words.protocols.irc import IRCClient - -import main -from core.relay import sendRelayNotification -from modules import userinfo from utils.get import getRelay from utils.logging.log import error, log from utils.logging.send import sendAll @@ -157,7 +156,11 @@ class IRCRelayFactory(ReconnectingClientFactory): def deliverRelayCommands(num, relayCommands, user=None, stage2=None): keyFN = main.certPath + main.config["Key"] certFN = main.certPath + main.config["Certificate"] - contextFactory = DefaultOpenSSLContextFactory(keyFN.encode("utf-8", "replace"), certFN.encode("utf-8", "replace")) - bot = IRCRelayFactory(net=None, num=num, relayCommands=relayCommands, user=user, stage2=stage2) + contextFactory = DefaultOpenSSLContextFactory( + keyFN.encode("utf-8", "replace"), certFN.encode("utf-8", "replace") + ) + bot = IRCRelayFactory( + net=None, num=num, relayCommands=relayCommands, user=user, stage2=stage2 + ) host, port = getRelay(num) reactor.connectSSL(host, port, bot, contextFactory) diff --git a/legacy/utils/loaders/command_loader.py b/legacy/utils/loaders/command_loader.py index 2f0756a..a6c113b 100644 --- a/legacy/utils/loaders/command_loader.py +++ b/legacy/utils/loaders/command_loader.py @@ -13,11 +13,15 @@ def loadCommands(allowDup=False): # try: module = __import__("commands.%s" % commandName) if commandName not in CommandMap: - CommandMap[commandName] = getattr(getattr(module, commandName), className) + CommandMap[commandName] = getattr( + getattr(module, commandName), className + ) debug("Registered command: %s" % commandName) else: if allowDup: - CommandMap[commandName] = getattr(getattr(module, commandName), className) + CommandMap[commandName] = getattr( + getattr(module, commandName), className + ) debug("Registered command: %s" % commandName) error("Duplicate command: %s" % (commandName)) diff --git a/legacy/utils/loaders/single_loader.py b/legacy/utils/loaders/single_loader.py index 65ce147..c704b64 100644 --- a/legacy/utils/loaders/single_loader.py +++ b/legacy/utils/loaders/single_loader.py @@ -12,7 +12,9 @@ def loadSingle(commandName): try: if commandName in CommandMap.keys(): reload(sys.modules["commands." + commandName]) - CommandMap[commandName] = getattr(sys.modules["commands." + commandName], className) + CommandMap[commandName] = getattr( + sys.modules["commands." + commandName], className + ) debug("Reloaded command: %s" % commandName) return "RELOAD" module = __import__("commands.%s" % commandName)