def verify_login(self, text=""): self.draw_ui(text if text else "Login to Chatterbox") self.username = raw_input("Enter username: "******"\n" pwd = raw_input("Enter password: "******"\n" message.send_msg(message.USER, self.username, self.sock) message.send_msg(message.PASS, md5_pwd, self.sock) res, text = message.receive_msg_from(self.sock) if res == message.REJECT: self.verify_login(text)
def direct_message(self, data, client_socket): match = re.match(r"^/direct\s(\w+)\s(.*)", data) user = self.client_users[client_socket] if match: target = match.group(1) msg = match.group(2) out_message = "{} <direct>: {}".format(user.name, msg) send_to = self.find_socket_by_name(target) self.log_message( "CLIENT", "{} to {} <direct>: {}".format(user.name, target, msg)) if send_to: message.send_msg(message.DIRECT, out_message + "\n", send_to) else: message.send_msg(message.NORMAL, "User {} is not online\n".format(target), client_socket)
def register_user(self, data, client_socket): username = data.split(" ")[0] match = False with open("users.txt", "r+a") as f: for line in f: user, _ = line.strip().split(":") if username.strip() == user: match = True message.send_msg(message.REJECT, "Username already registered\n", client_socket) break if not match: f.write("\n") f.write(data) self.log_message( "SERVER", "User {} registered to server".format(username)) message.send_msg(message.OK, "\n", client_socket)
def user_login(self, user, pwd, client_socket): for log_user in self.client_users.values(): if user == log_user.name: message.send_msg(message.REJECT, "User is already logged in\n", client_socket) self.log_message("SERVER", "User {} is already logged in".format(user)) return 0 if self.authenticate_user(user, pwd): new_user = u.User(user, self.user_is_admin(user), client_socket) self.chatrooms['default'].add_user(new_user) self.client_users[client_socket] = new_user self.log_message("SERVER", "User {} is now online".format(user)) self.broadcast(message.NORMAL, "User {} is now online".format(user)) else: message.send_msg(message.REJECT, "Couldn't authenticate user {}\n".format(user), client_socket) self.log_message("SERVER", "Failed login for user {}".format(user))
def register(self, err=""): if err: self.draw_ui(err) else: self.draw_ui("Registering new user") self.username = raw_input("Enter username: "******"Enter password: "******"Confirm password: "******"Passwords don't match!") pwd = raw_input("Enter password: "******"Confirm password: "******"\n" message.send_msg(message.REGISTER, ":".join([self.username, md5_pwd]), self.sock) msg_type, text = message.receive_msg_from(self.sock) if msg_type == message.REJECT: self.register(text) else: self.verify_login( "Registration successful. Login to your new account")
def parse_input(self, user_input): direct_match = re.match(r"^/direct\s(\w+)\s(.*)", user_input) if re.search(r"^/join", user_input): message.send_msg(message.JOIN, user_input, self.sock) elif direct_match: message.send_msg(message.DIRECT, user_input, self.sock) self.pretty_print_message("You <direct to {}>: {}".format( direct_match.group(1), direct_match.group(2))) elif re.search(r"^/quit", user_input): self.sock.close() sys.exit(0) elif re.search(r"^/", user_input): message.send_msg(message.COMMAND, user_input, self.sock) else: message.send_msg(message.NORMAL, user_input, self.sock) self.pretty_print_message("You: " + user_input.strip())
def kick_user(self, client_socket, args): user = self.client_users[client_socket] if user.is_admin: d_parse = re.match(r"^(\w+)\s(\w+)", args) if d_parse: target = self.chatrooms[user.chatroom].get_user( d_parse.group(2)) try: if d_parse.group(1) == 'server': message.send_msg( message.NORMAL, "You have been kicked from the server\n", target.sock) self.close_connection(target.sock) self.chatrooms[user.chatroom].broadcast( "User {} was kicked from the server".format( target)) self.log_message( "SERVER", "User {} was kicked from the server by {}".format( target, user)) else: message.send_msg( message.NORMAL, "You have been kicked from the room\n", target.sock) self.chatrooms['default'].add_user(target) self.chatrooms[user.chatroom].remove_user(target, True) self.log_message( "SERVER", "User {} was kicked from the room by {}".format( target, user)) except AttributeError: message.send_msg( message.NORMAL, "You need to enter the same room as the user to kick\n", client_socket)
def broadcast(self, msg_type, msg_text, client_socket=None): broad_range = [x for x in self.client_sockets if x != client_socket] for sock in broad_range: message.send_msg(msg_type, msg_text + "\n", sock)