Files
Chatter-EIAF/CHATTER/server.py
2026-04-08 19:46:30 -04:00

80 lines
2.8 KiB
Python

import time
from pathlib import Path
# Setup paths
base_path = Path(__file__).parent
req_path = base_path / "requests"
user_path = base_path / "users"
chat_path = base_path / "single_chats"
req_path.mkdir(exist_ok=True)
print("Server online. Monitoring requests...")
while True:
# 1. Grab everything in the folder
# .iterdir() is efficient for scanning
for file_path in req_path.iterdir():
# FAILSAFE 1: Ignore folders or non-txt files (like .DS_Store or .tmp)
if not file_path.is_file() or file_path.suffix != ".txt":
print(f"Skipping non-txt file: {file_path.name}")
continue
try:
# 2. Try to read the content
with open(file_path, "r") as f:
content = f.read().strip()
# FAILSAFE 2: Check if the file is empty or missing colons
if not content or ":" not in content:
print(f"Invalid or empty data in {file_path.name}, Deleting.")
# make failed request file
file_path.unlink()
continue
# 3. Parse the data
# Use the '2' limit we discussed to protect passwords with colons
parts = content.split(":", 2)
# FAILSAFE 3: Check if we have enough parts (Command:User:Pass)
if len(parts) < 3:
print(f"Incomplete data in {file_path.name}, Deleting.")
# TODO Make failed request file
file_path.unlink()
continue
command, user, data = parts
if not command.isalnum() or not user.isalnum() or not data.isalnum():
print(f"Invalid parameters in {file_path.name}, Deleting.")
# TODO Make failed request file
continue
print(f"Processing {command} for {user}...")
if command == "LOGIN":
# TODO
#1. check if user exists as a folder
#2. hash password and check against user hash.txt if exist
#3. Make success request file
#4. Fail file as try/except
pass
if command == "CREATEUSER":
# TODO
#1. Make a new folder with the name
#2. Hash password and store in hash.txt
#3. Init basic files
#4. Make success request file
#5. Fail file as try/except
pass
# 4. Clean up: Delete the request after successful processing
file_path.unlink()
except Exception as e:
# FAILSAFE 4: Catch-all for weird errors (like file being locked)
print(f"Error processing {file_path.name}: {e}")
continue
# 5. Heartbeat
time.sleep(0.1)