neptune/core/views/manage/threshold/irc.py

445 lines
14 KiB
Python
Raw Permalink Normal View History

from django.shortcuts import render
2022-07-25 18:08:28 +00:00
from django.views import View
2022-07-27 07:29:20 +00:00
from rest_framework.parsers import FormParser
from rest_framework.views import APIView
2022-07-25 18:08:28 +00:00
2022-07-27 19:53:41 +00:00
from core.lib.manage import threshold
from core.views.manage.permissions import SuperUserRequiredMixin
2022-07-25 18:08:28 +00:00
class ThresholdIRCStats(SuperUserRequiredMixin, View):
stats_template = "manage/threshold/irc/overview/stats.html"
2022-07-29 21:22:22 +00:00
def get(self, request):
2022-07-27 19:53:41 +00:00
stats = threshold.get_irc_stats()
context = {"stats": stats}
return render(request, self.stats_template, context)
2022-08-03 08:24:13 +00:00
class ThresholdIRCOverviewAlerts(SuperUserRequiredMixin, View):
stats_template = "manage/threshold/irc/overview/alerts.html"
def get(self, request):
alerts = threshold.get_irc_alerts(request.user)
context = {"alerts": alerts}
return render(request, self.stats_template, context)
class ThresholdIRCNetworks(SuperUserRequiredMixin, View):
template_name = "manage/threshold/irc/overview/networks.html"
2022-07-29 21:22:22 +00:00
def get(self, request):
2022-07-27 19:53:41 +00:00
networks = threshold.get_irc_networks()
2022-07-26 21:15:30 +00:00
context = {"networks": networks}
return render(request, self.template_name, context)
2022-08-02 08:04:23 +00:00
class ThresholdIRCNetworkDel(SuperUserRequiredMixin, View):
template_name = "manage/threshold/irc/overview/networks.html"
def delete(self, request, net):
deleted = threshold.del_network(net)
message = f"Deleted network {net}"
message_class = "success"
if not deleted["success"]:
message = deleted["reason"]
message_class = "danger"
networks = threshold.get_irc_networks()
context = {
"networks": networks,
"message": message,
"class": message_class,
}
return render(request, self.template_name, context)
2022-07-26 21:15:30 +00:00
class ThresholdIRCNetworkInfo(SuperUserRequiredMixin, View):
template_name = "manage/threshold/irc/network/info.html"
2022-07-26 21:15:30 +00:00
2022-07-29 21:22:22 +00:00
def get(self, request, net):
2022-07-27 19:53:41 +00:00
network = threshold.get_irc_network(net)
2022-07-26 21:15:30 +00:00
context = {"network": network}
return render(request, self.template_name, context)
2022-07-27 07:29:20 +00:00
class ThresholdIRCNetworkInfoEdit(SuperUserRequiredMixin, APIView):
template_name = "manage/threshold/irc/network/edit-network.html"
2022-07-27 07:29:20 +00:00
parser_classes = [FormParser]
def get(self, request, net):
"""
Return the form to edit a network.
"""
2022-07-27 19:53:41 +00:00
network = threshold.get_irc_network(net)
2022-07-27 07:29:20 +00:00
editable = ["auth", "host", "last", "port", "security"]
context = {
"net": net,
"network": {k: v for k, v in network.items() if k in editable},
}
return render(request, self.template_name, context)
def post(self, request, net):
"""
Actually edit the network.
Returns the info pane with a message about the success.
"""
template_name = "manage/threshold/irc/network/info.html"
2022-07-27 19:53:41 +00:00
edited = threshold.edit_irc_network(net, request.data)
2022-07-27 07:58:57 +00:00
if edited["success"]:
message = "Successfully edited!"
message_class = "success"
else:
if "reason" in edited:
message = f"Error editing network: {edited['reason']}"
else:
message = "Error editing network"
message_class = "danger"
2022-07-27 19:53:41 +00:00
network = threshold.get_irc_network(net)
2022-07-27 07:58:57 +00:00
context = {"network": network, "message": message, "class": message_class}
2022-07-27 07:29:20 +00:00
return render(request, template_name, context)
2022-07-26 21:15:30 +00:00
class ThresholdIRCNetworkRelays(SuperUserRequiredMixin, View):
template_name = "manage/threshold/irc/network/relays.html"
2022-07-26 21:15:30 +00:00
2022-07-27 19:53:41 +00:00
def get(self, request, net):
relays = threshold.get_irc_relays(net)
2022-07-26 21:15:30 +00:00
context = {"relays": relays["relays"]}
return render(request, self.template_name, context)
2022-07-29 21:11:34 +00:00
class ThresholdIRCNetworkRelayDel(SuperUserRequiredMixin, APIView):
template_name = "manage/threshold/irc/network/relays.html"
def delete(self, request, net, num):
"""
Delete a relay
"""
deleted = threshold.del_relay(net, num)
if deleted["success"]:
message = f"Deleted relay {num}"
message_class = "success"
else:
message = deleted["reason"]
message_class = "danger"
relays = threshold.get_irc_relays(net)
context = {
"net": net,
"message": message,
"class": message_class,
"relays": relays["relays"],
}
return render(request, self.template_name, context)
2022-07-27 19:53:41 +00:00
class ThresholdIRCNetworkRelayStatus(SuperUserRequiredMixin, APIView):
template_name = "manage/threshold/irc/network/relays.html"
def get(self, request, net, num, status):
"""
Change the status of a relay.
"""
edited = threshold.change_network_status(net, num, status)
if edited["success"]:
message = "Successfully completed!"
message_class = "success"
else:
if "reason" in edited:
message = f"Error updating status: {edited['reason']}"
else:
message = "Error updating status"
message_class = "danger"
relays = threshold.get_irc_relays(net)
context = {
"relays": relays["relays"],
"message": message,
"class": message_class,
}
return render(request, self.template_name, context)
class ThresholdIRCNetworkChannels(SuperUserRequiredMixin, APIView):
"""
List the channels a network is on.
Allow parting and joining new ones.
Re-render the same template if we join/part to make it simple.
"""
template_name = "manage/threshold/irc/network/channels.html"
2022-07-27 19:53:41 +00:00
parser_classes = [FormParser]
2022-07-26 21:15:30 +00:00
2022-07-29 21:22:22 +00:00
def get(self, request, net):
2022-07-27 19:53:41 +00:00
"""
Get list of channels for network.
:param net: network name
"""
channels = threshold.get_irc_channels(net)
context = {"net": net, "channels": channels["channels"]}
return render(request, self.template_name, context)
def delete(self, request, net, channel):
"""
Part a channel.
:param net: network name
:param channel: channel name
"""
parted = threshold.part_channel(net, channel)
2022-07-29 16:28:41 +00:00
if parted["success"]:
2022-07-27 19:53:41 +00:00
message = f"Requested part on relays: {', '.join(parted['relays'])}"
message_class = "success"
else:
message = parted["reason"]
message_class = "danger"
channels = threshold.get_irc_channels(net)
context = {
"net": net,
"channels": channels["channels"],
"message": message,
"class": message_class,
}
return render(request, self.template_name, context)
def put(self, request, net):
"""
Join a channel.
:param net: network name
"""
if "channel" not in request.data:
message = "No channel specified"
message_class = "danger"
else:
channel = request.data["channel"]
joined = threshold.join_channel(net, channel)
2022-07-29 16:28:41 +00:00
if joined["success"]:
2022-07-27 19:53:41 +00:00
message = f"Requested join on relay: {joined['relays']}"
message_class = "success"
else:
message = joined["reason"]
message_class = "danger"
channels = threshold.get_irc_channels(net)
context = {
"net": net,
"channels": channels["channels"],
"message": message,
"class": message_class,
}
2022-07-26 21:15:30 +00:00
return render(request, self.template_name, context)
2022-07-29 21:22:22 +00:00
class ThresholdIRCAliases(SuperUserRequiredMixin, APIView):
template_name = "manage/threshold/irc/overview/aliases.html"
def get(self, request):
"""
Get aliases.
"""
aliases = threshold.get_aliases()
context = {
"aliases": aliases["aliases"],
}
return render(request, self.template_name, context)
2022-08-01 18:06:56 +00:00
2022-08-01 22:02:35 +00:00
class ThresholdIRCActions(SuperUserRequiredMixin, APIView):
template_name = "manage/threshold/irc/overview/actions.html"
def get(self, request):
"""
Get actions pane.
"""
return render(request, self.template_name)
class ThresholdIRCActionsAddNetwork(SuperUserRequiredMixin, APIView):
template_name = "manage/threshold/irc/overview/modals/add-network.html"
parser_classes = [FormParser]
def get(self, request):
"""
Get actions pane.
"""
return render(request, self.template_name)
def put(self, request):
"""
Create a network.
"""
template_name = "manage/threshold/irc/overview/networks.html"
created = threshold.create_network(request.data)
message = "Network created successfully."
message_class = "success"
if not created["success"]:
message = created["reason"]
message_class = "danger"
networks = threshold.get_irc_networks()
context = {
"networks": networks,
"message": message,
"class": message_class,
}
return render(request, template_name, context)
2022-08-01 17:47:12 +00:00
class ThresholdIRCAliasesEdit(SuperUserRequiredMixin, APIView):
template_name = "manage/threshold/irc/overview/modals/edit-aliases.html"
parser_classes = [FormParser]
def get(self, request):
"""
Get aliases edit form.
"""
aliases = threshold.get_aliases()
context = {
"aliases": aliases["aliases"],
}
return render(request, self.template_name, context)
def post(self, request):
"""
Set the aliases
"""
message = "Successfully updated aliases"
message_class = "success"
new_aliases = {}
for item in request.data:
2022-08-01 18:06:56 +00:00
if "|" not in item:
2022-08-01 17:47:12 +00:00
continue
spl = item.split("|")
if len(spl) != 2:
message = f"Invalid item: {item}"
message_class = "danger"
break
num, field = spl
value = request.data[item]
if field == "emails":
value = request.data[item].split("\n")
for email in value:
# Check field isn't just empty
if len(email) != 0:
# Simple check for valid email
if "@" not in email:
message = f"Invalid email: {email}"
message_class = "danger"
break
2022-08-01 18:06:56 +00:00
2022-08-01 17:47:12 +00:00
if num not in new_aliases:
new_aliases[num] = {field: value}
else:
new_aliases[num][field] = value
2022-08-01 18:06:56 +00:00
if not message_class == "danger":
updated = threshold.update_aliases(new_aliases)
if not updated:
message = updated["reason"]
message_class = "danger"
2022-08-01 17:47:12 +00:00
template_name = "manage/threshold/irc/overview/aliases.html"
aliases = threshold.get_aliases()
context = {
"aliases": aliases["aliases"],
"message": message,
"class": message_class,
}
return render(request, template_name, context)
2022-07-29 16:28:41 +00:00
2022-08-01 18:06:56 +00:00
2022-07-29 21:22:22 +00:00
class ThresholdIRCNetworkActions(SuperUserRequiredMixin, View):
template_name = "manage/threshold/irc/network/actions.html"
def get(self, request, net):
"""
Get actions page.
"""
2022-07-29 16:28:41 +00:00
context = {"net": net}
return render(request, self.template_name, context)
2022-08-01 18:34:36 +00:00
class ThresholdIRCNetworkActionsAuto(SuperUserRequiredMixin, View):
template_name = "manage/threshold/irc/network/actions.html"
def post(self, request, net):
"""
Run network automation.
"""
ran = threshold.run_auto(net)
if ran["success"]:
message = f"Ran auto on network {net}"
message_class = "success"
2022-08-01 20:39:11 +00:00
if "message" in ran:
message = ran["message"]
else:
message = ran["reason"]
message_class = "danger"
context = {
"net": net,
"message": message,
"class": message_class,
}
return render(request, self.template_name, context)
class ThresholdIRCNetworkActionsList(SuperUserRequiredMixin, View):
template_name = "manage/threshold/irc/network/actions.html"
def post(self, request, net):
"""
Run LIST on network.
"""
ran = threshold.run_list(net)
if ran["success"]:
message = f"Ran list on network {net}"
message_class = "success"
if "message" in ran:
message = ran["message"]
2022-08-01 18:34:36 +00:00
else:
message = ran["reason"]
message_class = "danger"
context = {
"net": net,
"message": message,
"class": message_class,
}
return render(request, self.template_name, context)
2022-07-29 21:11:34 +00:00
class ThresholdIRCNetworkActionsRelay(SuperUserRequiredMixin, APIView):
2022-07-29 16:28:41 +00:00
template_name = "manage/threshold/irc/network/actions.html"
parser_classes = [FormParser]
def put(self, request, net):
"""
Create a relay
"""
if "num" not in request.data:
2022-07-29 21:41:53 +00:00
num = 0
2022-07-29 16:28:41 +00:00
else:
num = request.data["num"]
if not num.isdigit():
message = "Num is not an integer"
message_class = "danger"
else:
num = int(num)
2022-07-29 21:41:53 +00:00
created = threshold.add_relay(net, num)
if created["success"]:
id = created["id"]
alias = created["alias"]
message = f"Created new relay {id} with alias {alias}"
message_class = "success"
else:
message = created["reason"]
message_class = "danger"
2022-07-29 21:22:22 +00:00
2022-07-29 16:28:41 +00:00
context = {
"net": net,
"message": message,
"class": message_class,
}
return render(request, self.template_name, context)