crafty-4/app/classes/shared/command.py

122 lines
4.1 KiB
Python
Raw Normal View History

import sys
import cmd
import time
import threading
import logging
2022-05-16 19:39:10 +00:00
import getpass
from app.classes.shared.console import Console
from app.classes.shared.import3 import Import3
logger = logging.getLogger(__name__)
2022-03-08 04:40:44 +00:00
class MainPrompt(cmd.Cmd):
2022-05-16 19:39:10 +00:00
def __init__(self, helper, tasks_manager, migration_manager, main_controller):
super().__init__()
self.helper = helper
self.tasks_manager = tasks_manager
2021-08-18 15:11:53 +00:00
self.migration_manager = migration_manager
2022-05-16 19:39:10 +00:00
self.controller = main_controller
# overrides the default Prompt
self.prompt = f"Crafty Controller v{self.helper.get_version_string()} > "
2022-05-20 23:04:03 +00:00
def emptyline(self):
pass
def do_exit(self, _line):
2021-12-09 23:35:00 +00:00
self.tasks_manager._main_graceful_exit()
self.universal_exit()
2021-08-18 15:11:53 +00:00
def do_migrations(self, line):
if line == "up":
2021-08-18 15:11:53 +00:00
self.migration_manager.up()
elif line == "down":
2021-08-18 15:11:53 +00:00
self.migration_manager.down()
elif line == "done":
Console.info(self.migration_manager.done)
elif line == "todo":
Console.info(self.migration_manager.todo)
elif line == "diff":
Console.info(self.migration_manager.diff)
elif line == "info":
Console.info(f"Done: {self.migration_manager.done}")
Console.info(f"FS: {self.migration_manager.todo}")
Console.info(f"Todo: {self.migration_manager.diff}")
elif line.startswith("add "):
migration_name = line[len("add ") :]
2021-08-18 15:11:53 +00:00
self.migration_manager.create(migration_name, False)
else:
Console.info("Unknown migration command")
2022-01-15 00:23:50 +00:00
2022-05-16 19:39:10 +00:00
def do_set_passwd(self, line):
try:
username = str(line).lower()
2022-05-18 21:37:07 +00:00
# If no user is found it returns None
2022-05-16 19:39:10 +00:00
user_id = self.controller.users.get_id_by_name(username)
2022-05-18 21:37:07 +00:00
if not username:
Console.error("You must enter a username. Ex: `set_passwd admin'")
return False
if not user_id:
Console.error(f"No user found by the name of {username}")
return False
2022-05-17 20:01:37 +00:00
except:
2022-05-16 19:39:10 +00:00
Console.error(f"User: {line} Not Found")
return False
new_pass = getpass.getpass(prompt=f"NEW password for: {username} > ")
new_pass_conf = getpass.getpass(prompt="Re-enter your password: > ")
if new_pass != new_pass_conf:
Console.error("Passwords do not match. Please try again.")
return False
if len(new_pass) > 512:
2022-05-18 21:22:39 +00:00
Console.warning("Passwords must be greater than 6char long and under 512")
2022-05-16 19:39:10 +00:00
return False
if len(new_pass) < 6:
2022-05-18 21:22:39 +00:00
Console.warning("Passwords must be greater than 6char long and under 512")
2022-05-16 19:39:10 +00:00
return False
self.controller.users.update_user(user_id, {"password": new_pass})
2022-01-15 00:23:50 +00:00
@staticmethod
def do_threads(_line):
for thread in threading.enumerate():
2022-01-15 00:23:50 +00:00
if sys.version_info >= (3, 8):
print(
f"Name: {thread.name}\tIdentifier: "
f"{thread.ident}\tTID/PID: {thread.native_id}"
)
2022-01-15 00:23:50 +00:00
else:
print(f"Name: {thread.name}\tIdentifier: {thread.ident}")
2022-05-20 22:53:17 +00:00
def print_prompt(self):
self.stdout.write(self.prompt)
self.stdout.flush()
2022-03-19 03:59:10 +00:00
def do_import3(self, _line):
Import3.start_import()
def universal_exit(self):
logger.info("Stopping all server daemons / threads")
Console.info(
"Stopping all server daemons / threads - This may take a few seconds"
)
self.helper.websocket_helper.disconnect_all()
Console.info("Waiting for main thread to stop")
while True:
if self.tasks_manager.get_main_thread_run_status():
sys.exit(0)
time.sleep(1)
def help_exit(self):
Console.help("Stops the server if running, Exits the program")
2022-01-15 00:23:50 +00:00
def help_migrations(self):
Console.help("Only for advanced users. Use with caution")
2022-03-20 17:14:51 +00:00
def help_import3(self):
Console.help("Import users and servers from Crafty 3")