2020-08-27 22:53:04 +00:00
|
|
|
import sys
|
|
|
|
import cmd
|
|
|
|
import time
|
2021-11-20 00:14:32 +00:00
|
|
|
import threading
|
2020-08-27 22:53:04 +00:00
|
|
|
import logging
|
2022-05-16 19:39:10 +00:00
|
|
|
import getpass
|
2022-04-12 01:34:46 +00:00
|
|
|
from app.classes.shared.console import Console
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.shared.import3 import Import3
|
2020-08-27 22:53:04 +00:00
|
|
|
|
2022-05-26 13:28:54 +00:00
|
|
|
from app.classes.shared.helpers import Helpers
|
|
|
|
from app.classes.shared.tasks import TasksManager
|
|
|
|
from app.classes.shared.migration import MigrationManager
|
|
|
|
from app.classes.shared.main_controller import Controller
|
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-03-08 04:40:44 +00:00
|
|
|
|
2021-03-22 04:02:18 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
class MainPrompt(cmd.Cmd):
|
2022-06-05 20:02:52 +00:00
|
|
|
def __init__(
|
|
|
|
self, helper, tasks_manager, migration_manager, main_controller, import3
|
|
|
|
):
|
2021-03-22 04:02:18 +00:00
|
|
|
super().__init__()
|
2022-05-26 13:28:54 +00:00
|
|
|
self.helper: Helpers = helper
|
|
|
|
self.tasks_manager: TasksManager = tasks_manager
|
|
|
|
self.migration_manager: MigrationManager = migration_manager
|
|
|
|
self.controller: Controller = main_controller
|
2022-06-05 20:02:52 +00:00
|
|
|
self.import3: Import3 = import3
|
2022-05-16 19:39:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
# overrides the default Prompt
|
2022-06-13 00:09:46 +00:00
|
|
|
self.prompt = ""
|
2020-08-27 22:53:04 +00:00
|
|
|
|
2022-05-20 23:04:03 +00:00
|
|
|
def emptyline(self):
|
2020-08-27 22:53:04 +00:00
|
|
|
pass
|
|
|
|
|
2022-04-02 01:49:48 +00:00
|
|
|
def do_exit(self, _line):
|
2021-12-09 23:35:00 +00:00
|
|
|
self.tasks_manager._main_graceful_exit()
|
2021-04-17 21:24:54 +00:00
|
|
|
self.universal_exit()
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2021-08-18 15:11:53 +00:00
|
|
|
def do_migrations(self, line):
|
2022-03-23 02:50:12 +00:00
|
|
|
if line == "up":
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager.up()
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "down":
|
2021-08-18 15:11:53 +00:00
|
|
|
self.migration_manager.down()
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "done":
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(self.migration_manager.done)
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "todo":
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(self.migration_manager.todo)
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "diff":
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(self.migration_manager.diff)
|
2022-03-23 02:50:12 +00:00
|
|
|
elif line == "info":
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(f"Done: {self.migration_manager.done}")
|
|
|
|
Console.info(f"FS: {self.migration_manager.todo}")
|
|
|
|
Console.info(f"Todo: {self.migration_manager.diff}")
|
2022-03-23 02:50:12 +00:00
|
|
|
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:
|
2022-04-12 01:34:46 +00:00
|
|
|
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:
|
2022-06-13 20:06:36 +00:00
|
|
|
username = line
|
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:
|
2022-06-13 20:06:36 +00:00
|
|
|
Console.error(
|
|
|
|
f"No user found by the name of {username} this is case sensitive"
|
|
|
|
)
|
2022-05-18 21:37:07 +00:00
|
|
|
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):
|
2021-11-20 00:14:32 +00:00
|
|
|
for thread in threading.enumerate():
|
2022-01-15 00:23:50 +00:00
|
|
|
if sys.version_info >= (3, 8):
|
2022-03-23 02:50:12 +00:00
|
|
|
print(
|
2022-05-20 22:57:40 +00:00
|
|
|
f"Name: {thread.name}\tIdentifier: "
|
|
|
|
f"{thread.ident}\tTID/PID: {thread.native_id}"
|
2022-03-23 02:50:12 +00:00
|
|
|
)
|
2022-01-15 00:23:50 +00:00
|
|
|
else:
|
2022-05-20 22:57:40 +00:00
|
|
|
print(f"Name: {thread.name}\tIdentifier: {thread.ident}")
|
2021-11-20 00:14:32 +00:00
|
|
|
|
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):
|
2022-06-05 20:02:52 +00:00
|
|
|
self.import3.start_import()
|
2022-03-19 02:55:39 +00:00
|
|
|
|
2021-04-17 21:24:54 +00:00
|
|
|
def universal_exit(self):
|
2020-08-27 22:53:04 +00:00
|
|
|
logger.info("Stopping all server daemons / threads")
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info(
|
2022-03-23 02:50:12 +00:00
|
|
|
"Stopping all server daemons / threads - This may take a few seconds"
|
|
|
|
)
|
2022-04-11 05:23:55 +00:00
|
|
|
self.helper.websocket_helper.disconnect_all()
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.info("Waiting for main thread to stop")
|
2020-08-27 22:53:04 +00:00
|
|
|
while True:
|
2021-03-06 20:48:02 +00:00
|
|
|
if self.tasks_manager.get_main_thread_run_status():
|
2020-08-27 22:53:04 +00:00
|
|
|
sys.exit(0)
|
|
|
|
time.sleep(1)
|
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def help_exit(self):
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.help("Stops the server if running, Exits the program")
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def help_migrations(self):
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.help("Only for advanced users. Use with caution")
|
2022-03-20 17:14:51 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def help_import3(self):
|
2022-04-12 01:34:46 +00:00
|
|
|
Console.help("Import users and servers from Crafty 3")
|
2022-05-25 13:25:49 +00:00
|
|
|
|
|
|
|
def help_set_passwd(self):
|
|
|
|
Console.help("Set a user's password. Example: set_passwd admin")
|
|
|
|
|
|
|
|
def help_threads(self):
|
|
|
|
Console.help("Get all of the Python threads used by Crafty")
|