From 291968fbc7090cc36055ff73c8ba8d892a78baae Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sat, 13 Aug 2022 20:36:51 +0100 Subject: [PATCH] Implement updating registration via API --- api/views.py | 43 ++++++++++++++++++++++++++++++++++++++++++- modules/regproc.py | 23 +++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/api/views.py b/api/views.py index a19a4a1..2137f52 100644 --- a/api/views.py +++ b/api/views.py @@ -6,7 +6,7 @@ from klein import Klein from twisted.web.server import Request import main -from modules import chankeep, helpers, provision, userinfo +from modules import chankeep, helpers, provision, regproc, userinfo from modules.network import Network from utils.logging.log import warn @@ -207,6 +207,7 @@ class API(object): network["port"] = inst.port network["security"] = inst.security network["relays"] = len(inst.relays) + network["chanlimit"] = inst.chanlimit network["channels"] = userinfo.getTotalChanNum(net) network["records"] = userinfo.getNumWhoEntries(net) return dumps(network) @@ -247,6 +248,14 @@ class API(object): if not port.isdigit(): 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."}) + else: + chanlimit = int(chanlimit) elif item == "security": security = data[item][0] if security not in ["ssl", "plain"]: @@ -256,6 +265,7 @@ class API(object): inst.last = last inst.port = port inst.security = security + inst.chanlimit = chanlimit main.saveConf("network") return dumps({"success": True}) @@ -535,3 +545,34 @@ class API(object): if name not in main.IRCPool.keys(): return dumps({"success": False, "reason": f"relay {num} not on {net}"}) return dumps({"nickname": main.IRCPool[name].nickname}) + + @app.route("/irc/reg//", methods=["GET"]) + @login_required + def irc_get_unreg_net(self, request, net): + if net not in main.network.keys(): + return dumps({"success": False, "reason": "no such net."}) + unreg = regproc.get_unregistered_relays(net) + return dumps({"success": True, "unreg": unreg}) + + @app.route("/irc/reg/", methods=["GET"]) + @login_required + def irc_get_unreg(self, request): + unreg = regproc.get_unregistered_relays() + return dumps({"success": True, "unreg": unreg}) + + @app.route("/irc/reg/", methods=["PUT"]) + @login_required + def irc_confirm_accounts(self, request): + try: + data = loads(request.content.read()) + except JSONDecodeError: + return "Invalid JSON" + for item, token in data.items(): + if "|" not in item: + return dumps({"success": False, "reason": f"malformed item: {item}"}) + spl = item.split("|") + if not len(spl) == 2: + return dumps({"success": False, "reason": f"malformed item: {item}"}) + net, num = spl + regproc.confirmAccount(net, num, token) + return dumps({"success": True}) diff --git a/modules/regproc.py b/modules/regproc.py index 3cc35bc..facf120 100644 --- a/modules/regproc.py +++ b/modules/regproc.py @@ -120,6 +120,29 @@ def enableAuthentication(net, num): confirmRegistration(net, num) +def get_unregistered_relays(net=None): + """ + Get a dict of unregistereed relays, either globally or + for a network. + Returns: + {"net": [["nick1", 1], ["nick2", 2], ...]} + """ + unreg = {} + if net: + nets = [net] + else: + nets = main.network.keys() + for i in nets: + for num in main.network[i].relays.keys(): + if not main.network[i].relays[num]["registered"]: + nick = main.alias[num]["nick"] + if net in unreg: + unreg[i].append([nick, num]) + else: + unreg[i] = [[nick, num]] + return unreg + + def registerTest(c): sinst = substitute(c["net"], c["num"]) name = c["net"] + str(c["num"])