Implement adding networks

This commit is contained in:
Mark Veidemanis 2022-08-01 23:02:20 +01:00
parent db4b6cc6f9
commit 4c9ac3ec42
1 changed files with 39 additions and 0 deletions

View File

@ -1,11 +1,13 @@
import functools import functools
from json import JSONDecodeError, dumps, loads from json import JSONDecodeError, dumps, loads
from string import digits
from klein import Klein from klein import Klein
from twisted.web.server import Request from twisted.web.server import Request
import main import main
from modules import chankeep, provision, userinfo from modules import chankeep, provision, userinfo
from modules.network import Network
from utils.logging.log import warn from utils.logging.log import warn
@ -248,6 +250,43 @@ class API(object):
return dumps({"success": True}) return dumps({"success": True})
@app.route("/irc/network/create/", methods=["PUT"])
@login_required
def irc_network_create(self, request):
try:
data = loads(request.content.read())
except JSONDecodeError:
return "Invalid JSON"
fields = ["net", "auth", "host", "port", "security"]
if not set(fields).issubset(set(data)):
return dumps({"success": False, "reason": "not enough fields."})
for item in data:
if item == "net":
net = data[item]
if net in main.network.keys():
return dumps({"success": False, "reason": "network already exists."})
if set(net).intersection(set(digits)):
return dumps({"success": False, "reason": "network name cannot contain numbers."})
elif item == "auth":
auth = data[item]
if auth not in ["sasl", "ns", "none"]:
return dumps({"success": False, "reason": "invalid auth."})
elif item == "host":
host = data[item]
elif item == "port":
port = data[item]
if not port.isdigit():
return dumps({"success": False, "reason": "invalid port: not a number."})
port = int(port)
elif item == "security":
security = data[item]
if security not in ["ssl", "plain"]:
return dumps({"success": False, "reason": "invalid security."})
main.network[net] = Network(net, host, int(port), security, auth)
main.saveConf("network")
return dumps({"success": True})
@app.route("/irc/network/<net>/relays/", methods=["POST"]) @app.route("/irc/network/<net>/relays/", methods=["POST"])
@login_required @login_required
def irc_network_relays(self, request, net): def irc_network_relays(self, request, net):