From 6c7d0d5c4575f2a4b5e97347f355c3ff17a3b87c Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Thu, 21 Jul 2022 13:40:03 +0100 Subject: [PATCH] Reformat and fix circular import --- commands/admall.py | 2 +- commands/email.py | 4 +++- core/bot.py | 8 +------- core/relay.py | 5 ++++- main.py | 4 +++- modules/network.py | 4 +++- modules/provision.py | 6 ++++-- modules/regproc.py | 11 ++++++++--- threshold | 10 ++++++++-- utils/deliver_relay_commands.py | 7 +++++++ utils/loaders/command_loader.py | 20 ++++++++++---------- 11 files changed, 52 insertions(+), 29 deletions(-) create mode 100644 utils/deliver_relay_commands.py diff --git a/commands/admall.py b/commands/admall.py index 8d74545..9ae8636 100644 --- a/commands/admall.py +++ b/commands/admall.py @@ -1,5 +1,5 @@ import main -from core.bot import deliverRelayCommands +from utils.deliver_relay_commands import deliverRelayCommands class AdmallCommand: diff --git a/commands/email.py b/commands/email.py index 4817988..8905813 100644 --- a/commands/email.py +++ b/commands/email.py @@ -74,7 +74,9 @@ class EmailCommand: elif length == 3: 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"} + filtered = { + f"{k}:{k2}": v2 for k, v in main.irc.items() for k2, v2 in v.items() if k2 == "domains" + } info(dump(filtered)) return else: diff --git a/core/bot.py b/core/bot.py index 375703d..054e8e0 100644 --- a/core/bot.py +++ b/core/bot.py @@ -35,13 +35,7 @@ from utils.parsing import parsen from twisted.internet.ssl import DefaultOpenSSLContextFactory -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 = IRCBotFactory(net=None, num=num, relayCommands=relayCommands, user=user, stage2=stage2) - host, port = getRelay(num) - rct = reactor.connectSSL(host, port, bot, contextFactory) +from utils.deliver_relay_commands import deliverRelayCommands # Copied from the Twisted source so we can fix a bug diff --git a/core/relay.py b/core/relay.py index 66a9830..8ea6c92 100644 --- a/core/relay.py +++ b/core/relay.py @@ -114,7 +114,10 @@ class Relay(Protocol): def handleHello(self, parsed): if parsed["key"] in main.tokens.keys(): - if parsed["hello"] == main.tokens[parsed["key"]]["hello"] and main.tokens[parsed["key"]]["usage"] == "relay": + if ( + 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.authed = True else: diff --git a/main.py b/main.py index 473fd3e..1b01f4d 100644 --- a/main.py +++ b/main.py @@ -106,5 +106,7 @@ def initConf(): def initMain(): global r, g initConf() - r = StrictRedis(unix_socket_path=config["RedisSocket"], db=config["RedisDBEphemeral"]) # Ephemeral - flushed on quit + r = StrictRedis( + unix_socket_path=config["RedisSocket"], db=config["RedisDBEphemeral"] + ) # Ephemeral - flushed on quit g = StrictRedis(unix_socket_path=config["RedisSocket"], db=config["RedisDBPersistent"]) # Persistent diff --git a/modules/network.py b/modules/network.py index 21f1753..71fd3dc 100644 --- a/modules/network.py +++ b/modules/network.py @@ -77,7 +77,9 @@ class Network: # e.g. freenode1 for the first relay on freenode network keyFN = main.certPath + main.config["Key"] certFN = main.certPath + main.config["Certificate"] - contextFactory = DefaultOpenSSLContextFactory(keyFN.encode("utf-8", "replace"), certFN.encode("utf-8", "replace")) + contextFactory = DefaultOpenSSLContextFactory( + keyFN.encode("utf-8", "replace"), certFN.encode("utf-8", "replace") + ) bot = IRCBotFactory(self.net, num) # host, port = self.relays[num]["host"], self.relays[num]["port"] host, port = getRelay(num) diff --git a/modules/provision.py b/modules/provision.py index 1f17a20..c32aaca 100644 --- a/modules/provision.py +++ b/modules/provision.py @@ -1,11 +1,13 @@ import main -from core.bot import deliverRelayCommands +from utils.deliver_relay_commands import deliverRelayCommands from utils.logging.log import * from twisted.internet import reactor import modules.regproc -def provisionUserNetworkData(num, nick, altnick, ident, realname, emails, network, host, port, security, auth, password): +def provisionUserNetworkData( + num, nick, altnick, ident, realname, emails, network, host, port, security, auth, password +): print("nick", nick) print("altnick", altnick) print("emails", emails) diff --git a/modules/regproc.py b/modules/regproc.py index ce0e5a2..f0a8335 100644 --- a/modules/regproc.py +++ b/modules/regproc.py @@ -5,6 +5,7 @@ from utils.logging.debug import * from copy import deepcopy from random import choice + def needToRegister(net): # Check if the network does not support authentication networkObj = main.network[net] @@ -51,7 +52,7 @@ def substitute(net, num, token=None): nickname = alias["nick"] username = nickname + "/" + net password = main.network[net].aliases[num]["password"] - #inst["email"] = inst["email"].replace("{nickname}", nickname) + # inst["email"] = inst["email"].replace("{nickname}", nickname) for i in inst.keys(): if not isinstance(inst[i], str): continue @@ -129,10 +130,14 @@ def registerTest(c): if not main.IRCPool[name]._negativePass == True: if c["type"] == "query" and c["nick"] == sinst["entity"]: if sinst["checknegativemsg"] in c["msg"]: - confirmRegistration(c["net"], c["num"], negativepass=False) # Not passed negative check, report back + confirmRegistration( + c["net"], c["num"], negativepass=False + ) # Not passed negative check, report back return if sinst["checkendnegative"] in c["msg"]: - confirmRegistration(c["net"], c["num"], negativepass=True) # Passed the negative check, report back + confirmRegistration( + c["net"], c["num"], negativepass=True + ) # Passed the negative check, report back return if sinst["ping"]: if sinst["checkmsg2"] in c["msg"] and c["nick"] == sinst["entity"]: diff --git a/threshold b/threshold index dc186b4..e844b23 100755 --- a/threshold +++ b/threshold @@ -46,7 +46,10 @@ if __name__ == "__main__": ), interface=main.config["Listener"]["Address"], ) - log("Threshold running with SSL on %s:%s" % (main.config["Listener"]["Address"], main.config["Listener"]["Port"])) + log( + "Threshold running with SSL on %s:%s" + % (main.config["Listener"]["Address"], main.config["Listener"]["Port"]) + ) else: reactor.listenTCP( main.config["Listener"]["Port"], @@ -76,7 +79,10 @@ if __name__ == "__main__": relay, interface=main.config["RelayAPI"]["Address"], ) - log("Threshold relay running on %s:%s" % (main.config["RelayAPI"]["Address"], main.config["RelayAPI"]["Port"])) + log( + "Threshold relay running on %s:%s" + % (main.config["RelayAPI"]["Address"], main.config["RelayAPI"]["Port"]) + ) for net in main.network.keys(): main.network[net].start_bots() modules.counters.setupCounterLoop() diff --git a/utils/deliver_relay_commands.py b/utils/deliver_relay_commands.py new file mode 100644 index 0000000..c0b44df --- /dev/null +++ b/utils/deliver_relay_commands.py @@ -0,0 +1,7 @@ +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 = IRCBotFactory(net=None, num=num, relayCommands=relayCommands, user=user, stage2=stage2) + host, port = getRelay(num) + rct = reactor.connectSSL(host, port, bot, contextFactory) \ No newline at end of file diff --git a/utils/loaders/command_loader.py b/utils/loaders/command_loader.py index 9c4095b..0fa6d5c 100644 --- a/utils/loaders/command_loader.py +++ b/utils/loaders/command_loader.py @@ -12,16 +12,16 @@ def loadCommands(allowDup=False): if filename.endswith(".py") and filename != "__init__.py": commandName = filename[0:-3] className = commandName.capitalize() + "Command" - try: - module = __import__("commands.%s" % commandName) - if not commandName in CommandMap: + # try: + module = __import__("commands.%s" % commandName) + if not commandName in CommandMap: + CommandMap[commandName] = getattr(getattr(module, commandName), className) + debug("Registered command: %s" % commandName) + else: + if allowDup: CommandMap[commandName] = getattr(getattr(module, commandName), className) debug("Registered command: %s" % commandName) - else: - if allowDup: - CommandMap[commandName] = getattr(getattr(module, commandName), className) - debug("Registered command: %s" % commandName) - error("Duplicate command: %s" % (commandName)) - except Exception as err: - error("Exception while loading command %s:\n%s" % (commandName, err)) + error("Duplicate command: %s" % (commandName)) + # except Exception as err: + # error("Exception while loading command %s:\n%s" % (commandName, err))