Implement updating registration via API

This commit is contained in:
Mark Veidemanis 2022-08-13 20:36:51 +01:00
parent dd67e9cc8b
commit 291968fbc7
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 65 additions and 1 deletions

View File

@ -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/<net>/", 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})

View File

@ -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"])