示例#1
0
	def __init__(self, root, accountstxt):
		self._root = root
		self.accountstxt = accountstxt
		
		if root.tsbanurl and BanHandler:
			self.bandb = BanHandler(root, root.tsbanurl)
		else: self.bandb = None
		
		self.accounts = {}
		self.idToAccount = {}
		self.lock = None
		self.last_id = 0
示例#2
0
 def __init__(self, root, accountstxt):
     self._root = root
     self.accountstxt = accountstxt
     
     if root.tsbanurl and BanHandler:
         self.bandb = BanHandler(root, root.tsbanurl)
     else: self.bandb = None
     
     self.accounts = {}
     self.idToAccount = {}
     self.lock = None
     self.last_id = 0
示例#3
0
class UsersHandler:
    def __init__(self, root, accountstxt):
        self._root = root
        self.accountstxt = accountstxt

        if root.tsbanurl and BanHandler:
            self.bandb = BanHandler(root, root.tsbanurl)
        else:
            self.bandb = None

        self.accounts = {}
        self.idToAccount = {}
        self.lock = None
        self.last_id = 0

    def readAccounts(self):
        f = open(self.accountstxt, "r")

        last_id = 0
        line = f.readline().rstrip()

        while line:
            user = User.fromAccountLine(line)
            if user:
                self.accounts[user.lowername] = user
                last_id = max(last_id, int(user.id))
                self.idToAccount[user.id] = user

            line = f.readline().rstrip()

        self.last_id = last_id
        f.close()

    def writeAccounts(self):
        tmpname = self.accountstxt + ".tmp"
        f = open(tmpname, "w")

        for user in self.accounts.values():
            f.write(user.toAccountLine() + "\n")
        f.close()

        if os.path.exists(self.accountstxt) and os.path.exists(tmpname):
            os.remove(self.accountstxt)
        else:
            print "FAILURE: Account database could not be written. Preserving last successful database dump."

        os.rename(tmpname, self.accountstxt)

    def clientFromID(self, db_id):
        if db_id in self.idToAccount:
            return self.idToAccount[db_id]

    def clientFromUsername(self, username):
        name = username.lower()
        if name in self.accounts:
            return self.accounts[name]

    def check_ban(self, username=None, ip=None, userid=None):
        if self.bandb:
            return self.bandb.check_ban(username, ip, userid)
        else:
            return True, None

    def login_user(self, username, password, ip, lobby_id, user_id, cpu, local_ip, country):
        name = username.lower()
        lanadmin = self._root.lanadmin
        now = int(time.time() * 1000)
        if name == lanadmin["username"].lower() and password == lanadmin["password"]:
            user = User(lanadmin["username"], password, 0, False, "admin", None, now, ip, now, country, 0)
            return True, user
        elif name == lanadmin["username"].lower():
            return False, "Bad username/password"
            # return False, 'Invalid password.'

        user = self.clientFromUsername(name)
        if not user:
            return False, "Bad username/password"
            # return False, 'No user named %s.'%username

        good, reason = self.check_ban(user.username, ip, user_id)
        if not good:
            return False, "Banned: %s" % reason

        if not password == user.password:
            return False, "Bad username/password"
            # return False, 'Invalid password.'

        user.last_login = now
        user.last_ip = ip
        user.last_id = user_id
        return True, user

    def end_session(self, username):
        pass

    def register_user(self, username, password, ip, country):
        good, reason = self.check_ban(ip=ip)
        if not good:
            return False, "Banned: %s" % reason

        if len(username) > 20:
            return False, "Username too long"
        if self._root.censor:
            if not self._root.SayHooks._nasty_word_censor(username):
                return False, "Name failed to pass profanity filter."

        name = username.lower()
        lanadmin = self._root.lanadmin
        if name == lanadmin["username"].lower() or name in self.accounts:
            return False, "Username already exists."

        now = int(time.time() * 1000)

        user = User(username, password, 0, False, "agreement", None, now, ip, now, country, self.last_id + 1)
        self.last_id += 1
        self.accounts[name] = user
        self.idToAccount[user.id] = user
        return True, "Account registered successfully."

    def ban_user(self, username, duration, reason):
        pass

    def unban_user(self, username):
        pass

    def banlist(self):
        return []

    def rename_user(self, username, newname):
        if self._root.censor and not self._root.SayHooks._nasty_word_censor(newname):
            return False, "New username failed to pass profanity filter."
        user = self.clientFromUsername(username)
        if user:
            name = newname.lower()
            old = username.lower()
            if name != old and name in self.accounts:
                return False, "Username already exists."
            else:
                user.lowername = name
                user.username = newname
                self.accounts[name] = user
                if name != old:
                    del self.accounts[old]
                return True, "Account renamed successfully."

    def change_password(self, username, oldpass, newpass):
        user = self.clientFromUsername(username)
        if user:
            if user.password == oldpass:
                user.password = newpass
            else:
                return False, "Incorrect password"

    def save_user(self, client):
        user = self.clientFromUsername(client.username)
        if user:
            user.ingame_time = client.ingame_time
            user.access = client.access
            user.bot = client.bot
            # user.hook_chars = client.hook_chars # not saved to accounts.txt

    def confirm_agreement(self, client):
        user = self.clientFromUsername(client.username)
        if user:
            self.accounts[user.lowername].access = "user"

    def get_lastlogin(self, username):
        user = self.clientFromUsername(username)
        if user:
            return True, user.last_login / 1000
        else:
            return False, "User not found."

    def get_registration_date(self, username):
        user = self.clientFromUsername(username)
        if user:
            return True, user.register_date
        else:
            return False, "User not found."

    def get_ingame_time(self, username):
        user = self.clientFromUsername(username)
        if user:
            return True, user.ingame_time
        else:
            return False, "User not found."

    def get_account_info(self, username):
        user = self.clientFromUsername(username)
        if user:
            return True, user.toAccountLine()
        else:
            return False, "User not found."

    def get_account_access(self, username):
        user = self.clientFromUsername(username)
        if user:
            return True, "%s (%s)" % (int(user.getAccess(), 2), user.access)
        else:
            return False, "User not found."

    def find_ip(self, ip):
        users = []
        if ":" in ip:  # IPv6 - needs to account for ::: vs 00, ambiguous notation
            return ["IPv6 not implemented"]
        else:  # IPv4
            ip = ip.split(".", 3)
            for user in self.accounts.values():
                user_ip = user.last_ip.split(".", 3)
                for i in xrange(len(ip)):
                    if ip[i] != "*" and not ip[i] == user_ip[i]:
                        break
                else:
                    users.append(user)
        return users

    def get_ip(self, username):
        user = self.clientFromUsername(username)
        if user:
            return user.last_ip

    def remove_user(self, username):
        user = self.clientFromUsername(username)
        if user:
            del self.accounts[user]
            if user.id:
                del self.idToAccount[user.id]
示例#4
0
class UsersHandler:
	def __init__(self, root, accountstxt):
		self._root = root
		self.accountstxt = accountstxt
		
		if root.tsbanurl and BanHandler:
			self.bandb = BanHandler(root, root.tsbanurl)
		else: self.bandb = None
		
		self.accounts = {}
		self.idToAccount = {}
		self.lock = None
		self.last_id = 0
	
	def readAccounts(self):
		f = open(self.accountstxt, 'r')
		
		last_id = 0
		line = f.readline().rstrip()
		
		while line:
			user = User.fromAccountLine(line)
			if user:
				self.accounts[user.lowername] = user
				last_id = max(last_id, int(user.id))
				self.idToAccount[user.id] = user
				
			line = f.readline().rstrip()
		
		self.last_id = last_id
		f.close()
	
	def writeAccounts(self):
		tmpname = self.accountstxt+'.tmp'
		f = open(tmpname, 'w')
		
		for user in self.accounts.values():
			f.write(user.toAccountLine()+'\n')
		f.close()
		
		if os.path.exists(self.accountstxt) and os.path.exists(tmpname):
			os.remove(self.accountstxt)
		else:
			print 'FAILURE: Account database could not be written. Preserving last successful database dump.'

		os.rename(tmpname, self.accountstxt)
		
	def clientFromID(self, db_id):
		if db_id in self.idToAccount:
			return self.idToAccount[db_id]
	
	def clientFromUsername(self, username):
		name = username.lower()
		if name in self.accounts:
			return self.accounts[name]
	
	def check_ban(self, username=None, ip=None, userid=None):
		if self.bandb:
			return self.bandb.check_ban(username, ip, userid)
		else: return True, None
	
	def login_user(self, username, password, ip, lobby_id, user_id, cpu, local_ip, country):
		name = username.lower()
		lanadmin = self._root.lanadmin
		now = int(time.time())
		if name == lanadmin['username'].lower() and password == lanadmin['password']:
			user = User(lanadmin['username'], password, 0, False, 'admin', None, now, ip, now, country, 0)
			return True, user
		elif name == lanadmin['username'].lower():
			return False, 'Bad username/password'
			#return False, 'Invalid password.'
		
		user = self.clientFromUsername(name)
		if not user:
			return False, 'Bad username/password'
			#return False, 'No user named %s.'%username
		
		good, reason = self.check_ban(user.username, ip, user_id)
		if not good:
			return False, 'Banned: %s' % reason
		
		if not password == user.password:
			return False, 'Bad username/password'
			#return False, 'Invalid password.'
		
		user.last_login = now
		user.last_ip = ip
		user.last_id = user_id
		return True, user

	def end_session(self, db_id): pass
	
	def register_user(self, username, password, ip, country):
		good, reason = self.check_ban(ip=ip)
		if not good: return False, 'Banned: %s' % reason
		
		if len(username) > 20: return False, 'Username too long'
		if self._root.censor:
			if not self._root.SayHooks._nasty_word_censor(username):
				return False, 'Name failed to pass profanity filter.'
		
		name = username.lower()
		lanadmin = self._root.lanadmin
		if name == lanadmin['username'].lower() or name in self.accounts:
			return False, 'Username already exists.'
		
		now = int(time.time())
		
		user = User(username, password, 0, False, 'agreement', None, now, ip, now, country, self.last_id+1)
		self.last_id += 1
		self.accounts[name] = user
		self.idToAccount[user.id] = user
		return True, 'Account registered successfully.'
	
	def ban_user(self, owner, username, duration, reason):
		if self.bandb:
			user = self.clientFromUsername(username)
			client_id = None
			if user:
				client_id = user.last_id

			self.bandb.ban_user(owner, username, client_id, duration, reason)

	def unban_user(self, username):
		if self.bandb:
			self.bandb.unban_user(username)

	def ban_ip(self, owner, ip, duration, reason):
		if self.bandb:
			self.bandb.ban_ip(owner, ip, duration, reason)

	def unban_ip(self, ip):
		if self.bandb:
			self.bandb.unban_ip(ip)

	def banlist(self):
		if self.bandb:
			return self.bandb.banlist()
	
	def rename_user(self, username, newname):
		if self._root.censor and not self._root.SayHooks._nasty_word_censor(newname):
			return False, 'New username failed to pass profanity filter.'
		user = self.clientFromUsername(username)
		if user:
			name = newname.lower()
			old = username.lower()
			if name != old and name in self.accounts:
				return False, 'Username already exists.'
			else:
				user.lowername = name
				user.username = newname
				self.accounts[name] = user
				if name != old:
					del self.accounts[old]
				return True, 'Account renamed successfully.'
	
	def change_password(self, username, oldpass, newpass):
		user = self.clientFromUsername(username)
		if user:
			if user.password == oldpass:
				user.password = newpass
			else:
				return False, 'Incorrect password'
	
	def save_user(self, client):
		user = self.clientFromUsername(client.username)
		if user:
			user.ingame_time = client.ingame_time
			user.access = client.access
			user.bot = client.bot
			# user.hook_chars = client.hook_chars # not saved to accounts.txt
	
	def confirm_agreement(self, client):
		user = self.clientFromUsername(client.username)
		if user:
			self.accounts[user.lowername].access = 'user'
	
	def get_lastlogin(self, username):
		user = self.clientFromUsername(username)
		if user:
			return True, user.last_login
		else:
			return False, 'User not found.'
	
	def get_registration_date(self, username):
		user = self.clientFromUsername(username)
		if user:
			return True, user.register_date
		else:
			return False, 'User not found.'
	
	def get_ingame_time(self, username):
		user = self.clientFromUsername(username)
		if user:
			return True, user.ingame_time
		else:
			return False, 'User not found.'
	
	def get_account_info(self, username):
		user = self.clientFromUsername(username)
		if user:
			return True, user.toAccountLine()
		else:
			return False, 'User not found.'
	
	def get_account_access(self, username):
		user = self.clientFromUsername(username)
		if user:
			return True, '%s (%s)' % (int(user.getAccess(), 2), user.access)
		else:
			return False, 'User not found.'
	
	def find_ip(self, ip):
		users = []
		if ':' in ip: # IPv6 - needs to account for ::: vs 00, ambiguous notation
			return ['IPv6 not implemented']
		else: # IPv4
			ip = ip.split('.', 3)
			for user in self.accounts.values():
				user_ip = user.last_ip.split('.', 3)
				for i in xrange(len(ip)):
					if ip[i] != '*' and not ip[i] == user_ip[i]:
						break
				else:
					users.append(user)
		return users
	
	def get_ip(self, username):
		user = self.clientFromUsername(username)
		if user:
			return user.last_ip
	
	def remove_user(self, username):
		user = self.clientFromUsername(username)
		if user:
			del self.accounts[user]
			if user.id:
				del self.idToAccount[user.id]