143 lines
5.1 KiB
Python
143 lines
5.1 KiB
Python
# LOGIN CLIENT
|
|
# HANDLES ACCOUNT CREATION AND VERIFICATION
|
|
# user validity check: no spaces no slashes no unprintable ASCII characters no carriage returns or anything like that
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
import time
|
|
|
|
# Sets the universal current path
|
|
script_dir = Path(__file__).parent
|
|
|
|
def chat_ui():
|
|
print("CordDie")
|
|
# file finding and choosing logic
|
|
|
|
def wait_for_status(timestamp):
|
|
status_folder = script_dir / "requests" / "status"
|
|
print("Waiting for server response...", end="", flush=True)
|
|
|
|
# Try for about 5 seconds (50 iterations * 0.1s)
|
|
for _ in range(50):
|
|
success_file = status_folder / f"success_{timestamp}.txt"
|
|
fail_file = status_folder / f"fail_{timestamp}.txt"
|
|
|
|
if success_file.exists():
|
|
print("\n[SUCCESS] Action completed!")
|
|
success_file.unlink() # Cleanup the status file
|
|
return True
|
|
|
|
if fail_file.exists():
|
|
print("\n[FAILED] The server rejected the request.")
|
|
fail_file.unlink() # Cleanup
|
|
return False
|
|
|
|
print(".", end="", flush=True)
|
|
time.sleep(0.1)
|
|
|
|
print("\n[TIMEOUT] Server is taking too long.")
|
|
return False
|
|
|
|
def login_logic():
|
|
while True:
|
|
print("CordDie Login Client")
|
|
print("[1] Login [2] Sign up")
|
|
login_choice = input(" > ")
|
|
|
|
if login_choice == "1":
|
|
while True:
|
|
login_username = input("Username > ")
|
|
if login_username.isalnum():
|
|
break
|
|
else:
|
|
print("Your username contains spaces or symbols! Check again.")
|
|
while True:
|
|
login_password = input("Password > ")
|
|
if login_password.isalnum():
|
|
break
|
|
else:
|
|
print("Only 1-9, A-Z and a-z are allowed!")
|
|
|
|
# Get current time (request identifier)
|
|
timestamp_ms = int(time.time() * 1000)
|
|
|
|
# 1. Define your paths clearly
|
|
req_folder = script_dir / "requests"
|
|
tmp_path = req_folder / f"{timestamp_ms}.tmp"
|
|
final_path = req_folder / f"{timestamp_ms}.txt"
|
|
|
|
for i in range(3):
|
|
try:
|
|
# 2. Write to the temporary file
|
|
with open(tmp_path, "w") as request:
|
|
request.write(f"LOGIN:{login_username}:{login_password}")
|
|
|
|
# 3. Rename it to .txt (After the 'with' block finishes)
|
|
tmp_path.rename(final_path)
|
|
print("Request sent! Please wait...")
|
|
if wait_for_status(timestamp_ms):
|
|
print(f"Welcome Back, {login_username}!")
|
|
chat_ui()
|
|
else:
|
|
print("Login failed!")
|
|
break
|
|
except Exception as e:
|
|
print(f"Error in attempt {i+1}: {e}")
|
|
|
|
|
|
if login_choice == "2":
|
|
while True:
|
|
signup_username = input("New Username > ")
|
|
if signup_username.isalnum():
|
|
break
|
|
else:
|
|
print("Cannot include spaces or symbols!")
|
|
|
|
while True:
|
|
while True:
|
|
signup_password = input("Password > ")
|
|
if signup_password.isalnum():
|
|
break
|
|
else:
|
|
print("Only 1-9, A-Z and a-z are allowed!")
|
|
while True:
|
|
signup_password_rep = input("Repeat Password > ")
|
|
if signup_password_rep.isalnum():
|
|
break
|
|
else:
|
|
print("Only 1-9, A-Z and a-z are allowed!")
|
|
if signup_password == signup_password_rep:
|
|
break
|
|
else:
|
|
print("Passwords are not the same!")
|
|
|
|
# Get current time (request identifier)
|
|
timestamp_ms = int(time.time() * 1000)
|
|
|
|
req_folder = script_dir / "requests"
|
|
tmp_path = req_folder / f"{timestamp_ms}.tmp"
|
|
final_path = req_folder / f"{timestamp_ms}.txt"
|
|
|
|
for i in range(3):
|
|
try:
|
|
with open(tmp_path, "w") as request:
|
|
request.write(f"CREATEUSER:{signup_username}:{signup_password}")
|
|
|
|
# RENAME FILE!!!
|
|
tmp_path.rename(final_path)
|
|
print("Account creation request sent!")
|
|
|
|
if wait_for_status(timestamp_ms):
|
|
print("Account created! Please login.")
|
|
else:
|
|
print("Account creation failed, this username might already exist.")
|
|
break
|
|
except Exception as e:
|
|
print(f"Error in attempt {i+1}: {e}")
|
|
|
|
# TODO WAIT FOR SUCCESS/UNSUCCESS MESSAGE
|
|
|
|
|
|
# The main thing
|
|
if __name__ == "__main__":
|
|
login_logic()
|