chatServer/modules/account.py

69 lines
2.6 KiB
Python

global configparser
import configparser
global accountPath
accountPath = p(sp,"accounts")
global accountUserChars
accountUserChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"
global accountUsernameLengthMin
accountUsernameLengthMin = 2
global accountUsernameLengthMax
accountUsernameLengthMax = 32
global accountPasswordLengthMin
accountPasswordLengthMin = 0
global accountPasswordLengthMax
accountPasswordLengthMax = 128
global accountExists
def accountExists(acc):
if os.path.isdir(p(accountPath,acc)): return True
return False
global accountNameValid
def accountNameValid(name):
for char in name:
if not char in accountUserChars:
return False
return True
def f(self,cmd,*args):
with fileLock:
if len(args) != 2:
return ["error","nonfatal","syntax","Correct syntax: " +cmd+ ",<username>,<password>"]
if len(args[0]) < accountUsernameLengthMin or len(args[0]) > accountUsernameLengthMax:
return ["error","nonfatal","invalid_name","Your username has to be between " +str(accountUsernameLengthMin)+ " and " +str(accountUsernameLengthMax)+ " characters long"]
if len(args[1]) < accountPasswordLengthMin or len(args[1]) > accountPasswordLengthMax:
return ["error","nonfatal","invalid_password","Your password has to be between " +str(accountPasswordLengthMin)+ " and " +str(accountPasswordLengthMax)+ " characters long"]
if not accountNameValid(args[0]):
return ["error","nonfatal","invalid_name","Your username can only contain the following characters: '" +accountUserChars+ "'"]
if accountExists(args[0]):
return ["error","nonfatal","user_exists","This user already exists"]
dbSet(p(accountPath,args[0],"user.db"),"DEFAULT","password",args[1])
commands["register"] = f
def f(self,cmd,*args):
with fileLock:
if len(args) != 2:
return ["error","nonfatal","syntax","Correct syntax: " +cmd+ ",<username>,<password>"]
if not accountNameValid(args[0]):
return ["error","nonfatal","invalid_name","Your username can only contain the following characters: '" +accountUserChars+ "'"]
passw = dbGet(p(accountPath,args[0],"user.db"),"DEFAULT","password")
if passw == False:
return ["error","nonfatal","wrong_user_or_password"]
if passw != args[1]:
return ["error","nonfatal","wrong_user_or_password"]
with connectionsLock:
if "user" in connections[self.cid]:
return ["error","nonfatal","logged_in","You are already logged in"]
connections[self.cid]["user"] = args[0]
connections[self.cid]["lastMessage"] = "0"
commands["login"] = f