chatServer/modules/[text server]/[api]/users/module.py

87 lines
2.3 KiB
Python

moduleDepends([
p("[text server]","[api]","commands"),
p("[text server]","[api]","threadedFiles")
])
global textUserFolder
textUserFolder = p(textBaseFolder,"users")
textQuickFolder(textUserFolder)
global textUserAllowedCharacters
textUserAllowedCharacters = "abcdefghijklmnopqrstuvwxyz0123456789.-_ "
global textUserGetPath
def textUserGetPath(user):
return p(textUserFolder,user)
global textUserRegister
def textUserRegister(self,command,args):
if len(args) != 2:
return ["error","nonfatal","syntax","Correct syntax: " +command+ ",<user>,<password>"]
user = args[0].lower()
if len(user) < 1:
return ["error","nonfatal","name_too_short","Needs to be at least 1 character in length."]
for symbol in user:
if not symbol in textUserAllowedCharacters:
return ["error","nonfatal","invalid_name","Allowed characters: " +", ".join([char for char in textUserAllowedCharacters])]
userpath = textUserGetPath(user)
with fileLock:
if os.path.isdir(userpath):
return ["error","nonfatal","user_exists"]
password = args[1]
os.makedirs(userpath)
passFile = open(p(userpath,"pass.txt"),"w")
passFile.write(password)
passFile.close()
return ["ok"]
textCommandAddHandler("register",textUserRegister)
global textUserLogin
def textUserLogin(self,command,args):
if len(args) != 2:
return ["error","nonfatal","syntax","Correct syntax: " +command+ ",<user>,<password>"]
user = args[0].lower()
if len(user) < 1:
return ["error","nonfatal","name_too_short","Needs to be at least 1 character in length."]
for symbol in user:
if not symbol in textUserAllowedCharacters:
return ["error","nonfatal","invalid_name","Allowed characters: " +", ".join([char for char in textUserAllowedCharacters])]
userpath = textUserGetPath(user)
with fileLock:
if not os.path.isdir(userpath):
return ["error","nonfatal","wrong_user_or_password"]
password = args[1]
passFile = open(p(userpath,"pass.txt"),"r")
passw = passFile.read()
passFile.close()
if password != passw:
return ["error","nonfatal","wrong_user_or_password"]
with self.lock:
self.user = user
return ["ok"]
textCommandAddHandler("login",textUserLogin)
global textUserGet
def textUserGet(self,command,args):
with self.lock:
user = self.user
if not user:
return ["error","nonfatal","not_logged_in"]
return [user]
textCommandAddHandler("whoami",textUserGet)