2018-11-22 02:09:30 +00:00
|
|
|
from django.views.generic import View
|
|
|
|
from django.http import JsonResponse
|
2018-12-02 02:17:19 +00:00
|
|
|
from django.conf import settings
|
2019-01-14 20:22:18 +00:00
|
|
|
import traceback
|
|
|
|
import sys
|
2018-11-22 02:09:30 +00:00
|
|
|
|
2019-08-16 17:00:26 +00:00
|
|
|
from GeoffreyApp.api.commands import RequestTypes
|
2018-11-22 02:09:30 +00:00
|
|
|
import GeoffreyApp.api.commands as commands
|
2019-09-17 16:23:47 +00:00
|
|
|
from GeoffreyApp.api.key import check_request_for_key, check_key
|
2018-12-17 16:02:43 +00:00
|
|
|
from GeoffreyApp.errors import *
|
2019-09-17 16:23:47 +00:00
|
|
|
from GeoffreyApp.models import PermissionLevel
|
2018-11-22 02:09:30 +00:00
|
|
|
|
|
|
|
|
2019-08-19 21:54:17 +00:00
|
|
|
def run_command(request, command_name, req_type, key):
|
2019-08-16 17:00:26 +00:00
|
|
|
params = request.dict()
|
|
|
|
params.pop("api")
|
|
|
|
|
|
|
|
command_name = command_name.lower()
|
2018-11-22 02:09:30 +00:00
|
|
|
try:
|
2019-08-16 17:00:26 +00:00
|
|
|
if command_name in commands.command_dict[req_type]:
|
|
|
|
command = commands.command_dict[req_type][command_name]
|
2019-08-19 21:54:17 +00:00
|
|
|
|
|
|
|
if key.has_command_permission(command.permission_level):
|
|
|
|
response = command.func(**params)
|
|
|
|
else:
|
|
|
|
response = {}
|
2018-12-17 21:16:21 +00:00
|
|
|
else:
|
|
|
|
raise CommandNotFound
|
2018-11-22 02:09:30 +00:00
|
|
|
except Exception as e:
|
2018-12-30 01:41:18 +00:00
|
|
|
response = {"error": e.__class__.__name__, "error_message": str(e)}
|
2019-01-31 23:10:28 +00:00
|
|
|
if getattr(settings, "DEBUG", False):
|
2019-01-31 20:07:25 +00:00
|
|
|
traceback.print_exception(type(e), e, e.__traceback__, file=sys.stderr)
|
2018-11-22 02:09:30 +00:00
|
|
|
|
|
|
|
return JsonResponse(response, safe=False)
|
|
|
|
|
|
|
|
|
2019-08-19 21:54:17 +00:00
|
|
|
def get_commands(key):
|
2019-01-27 18:43:30 +00:00
|
|
|
command_list = []
|
|
|
|
|
|
|
|
for request in commands.command_dict:
|
2019-08-16 17:00:26 +00:00
|
|
|
for command_name in commands.command_dict[request]:
|
|
|
|
command = commands.command_dict[request][command_name]
|
|
|
|
json = command.json
|
2019-08-19 21:54:17 +00:00
|
|
|
|
|
|
|
if key.has_command_permission(command.permission_level):
|
|
|
|
command_list.append(json)
|
2019-01-27 18:43:30 +00:00
|
|
|
|
|
|
|
return command_list
|
|
|
|
|
|
|
|
|
2018-11-22 02:09:30 +00:00
|
|
|
class CommandAPI(View):
|
|
|
|
def get(self, request, command):
|
|
|
|
get = request.GET
|
2019-08-19 21:54:17 +00:00
|
|
|
key = check_request_for_key(get)
|
|
|
|
if key is not None:
|
2018-12-30 01:41:18 +00:00
|
|
|
if command.lower() == "commands":
|
2019-08-19 21:54:17 +00:00
|
|
|
return JsonResponse(get_commands(key), safe=False)
|
2018-12-30 01:41:18 +00:00
|
|
|
else:
|
2019-08-19 21:54:17 +00:00
|
|
|
return run_command(get, command, RequestTypes.GET, key)
|
|
|
|
return JsonResponse({})
|
2018-11-22 02:09:30 +00:00
|
|
|
|
|
|
|
def post(self, request, command):
|
|
|
|
post = request.POST
|
2019-09-25 23:43:02 +00:00
|
|
|
key = check_request_for_key(post)
|
2019-08-19 21:54:17 +00:00
|
|
|
|
|
|
|
if key is not None:
|
|
|
|
return run_command(post, command, RequestTypes.POST, key)
|
2018-12-30 01:41:18 +00:00
|
|
|
else:
|
|
|
|
return JsonResponse({})
|
2018-12-02 02:17:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SettingsAPI(View):
|
2018-12-08 21:37:00 +00:00
|
|
|
def get(self, request):
|
2019-08-19 21:54:17 +00:00
|
|
|
key = check_request_for_key(request.GET)
|
|
|
|
if key.has_command_permission(PermissionLevel.PLAYER):
|
2019-01-01 16:18:15 +00:00
|
|
|
response = {
|
|
|
|
"BOT_PREFIX": getattr(settings, 'GEOFFREY_BOT_PREFIX', '?'),
|
2019-01-31 20:07:25 +00:00
|
|
|
"ERROR_USERS": getattr(settings, 'GEOFFREY_BOT_ERROR_USERS', []),
|
2019-01-01 16:18:15 +00:00
|
|
|
"MOD_RANKS": getattr(settings, 'GEOFFREY_BOT_MOD_RANK', []),
|
|
|
|
"STATUS": getattr(settings, 'GEOFFREY_BOT_STATUS', 'sed')
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
response = {}
|
2018-12-02 02:17:19 +00:00
|
|
|
|
|
|
|
return JsonResponse(response)
|
|
|
|
|
|
|
|
|
|
|
|
|