diff --git a/api/views.py b/api/views.py index b35463c..76abaa4 100644 --- a/api/views.py +++ b/api/views.py @@ -1,11 +1,13 @@ 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 modules import chankeep, provision, userinfo +from modules.network import Network from utils.logging.log import warn @@ -248,6 +250,43 @@ class API(object): 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//relays/", methods=["POST"]) @login_required def irc_network_relays(self, request, net):