def register(self, login, password, sock): """ create a new account and store the login, the password and the role in the database We have to check if the login exists. """ create = True answer = "" # If login has a valid form if all(c in ascii_letters+'_-0123456789' for c in login): # we read the database with open("logins.txt", "a+") as liste: data = liste.readlines() print "Debug: Spectator.register()" # and we check that the login is not already used for line in data: line = line.split(" ") if line[1] == login: create = False print create # if it is not used, we can create the user if create: password = md5.new(password).hexdigest() liste.write("3 " + login + " " + password + "\n") auth = RSAauth() auth.register(login, sock) if create: answer = "Registration successfull" else: answer = "Registration failed, please choose an other login" return answer
def register(self, login, password, sock): """ create a new account and store the login, the password and the role in the database We have to check if the login exists. """ create = True answer = "" # If login has a valid form if all(c in ascii_letters + '_-0123456789' for c in login): # we read the database with open("logins.txt", "a+") as liste: data = liste.readlines() print "Debug: Spectator.register()" # and we check that the login is not already used for line in data: line = line.split(" ") if line[1] == login: create = False print create # if it is not used, we can create the user if create: password = md5.new(password).hexdigest() liste.write("3 " + login + " " + password + "\n") auth = RSAauth() auth.register(login, sock) if create: answer = "Registration successfull" else: answer = "Registration failed, please choose an other login" return answer
def recvMsg(self, userList, queue): """ client send message to server Here, we have to check its rights to know what to do from the message. Every message should begin with the nickname, followed by an action or the message to post. This is also a message dispatcher """ sock = self.clientSock userListning = True role = self.role while userListning: response = sock.recv(3000) data = "" try: data = self.encryption.decipher(response) except: # If there is any problem, we disconnect the user # Normally, it happens if the user disconnected print "Exception in RSA.decipher()!" break print data data = data.split(" ") if len(data) == 1: # not concerned here by these packets print data continue elif data[1] == "/identify" and len(data) == 3: response = "" authentication = "" print "Identification" if "RSA" in data: authentication = RSAauth() else: authentication = Pass() response = authentication.identify(data, self) print response # change Session class if self.level == 10: print "coucou" # change this role = Admin() elif data[1] == "/register" and len(data) == 3: result = role.register(data[0], data[2], sock) elif data[1] == "/nick": self.nick = role.changeNick(data[2]) # verify nicks if self.nick == data[2]: sock.send(self.encryption.encipher("newNick:" + data[2])) queue.put( str(" " + data[0] + " is now known as " + data[2]).split(" ")) elif data[1] == "/quit": sock.send("quit") userListning = False elif data[1] == "/ban" and self.level > 5 and len(data) == 3: queue.put(str(" " + data[2] + " has been banned").split(" ")) role.ban(data[2]) elif self.level > 1: print "sending to all" queue.put(data) else: print "No right to talk" sock.send( self.encryption.encipher("You have no right to talk!")) self.close(userList)
def recvMsg(self, userList, queue): """ client send message to server Here, we have to check its rights to know what to do from the message. Every message should begin with the nickname, followed by an action or the message to post. This is also a message dispatcher """ sock = self.clientSock userListning = True role = self.role while userListning: response = sock.recv(3000) data = "" try: data = self.encryption.decipher(response) except: # If there is any problem, we disconnect the user # Normally, it happens if the user disconnected print "Exception in RSA.decipher()!" break print data data = data.split(" ") if len(data) == 1: # not concerned here by these packets print data continue elif data[1] == "/identify" and len(data) == 3: response = "" authentication = "" print "Identification" if "RSA" in data: authentication = RSAauth() else: authentication = Pass() response = authentication.identify(data, self) print response # change Session class if self.level == 10: print "coucou" # change this role = Admin() elif data[1] == "/register" and len(data) == 3: result = role.register(data[0], data[2], sock) elif data[1] == "/nick": self.nick = role.changeNick(data[2]) # verify nicks if self.nick == data[2]: sock.send(self.encryption.encipher("newNick:" + data[2])) queue.put(str(" " + data[0] + " is now known as " + data[2]).split(" ")) elif data[1] == "/quit": sock.send("quit") userListning = False elif data[1] == "/ban" and self.level > 5 and len(data) == 3: queue.put(str(" " + data[2] + " has been banned").split(" ")) role.ban(data[2]) elif self.level > 1: print "sending to all" queue.put(data) else: print "No right to talk" sock.send(self.encryption.encipher("You have no right to talk!")) self.close(userList)