Esempio n. 1
0
    def get_sign_in_details(self):
        userpasw = []
        stdscr.erase()
        stdscr.addstr("Insert your credentials\n\n", curses.A_UNDERLINE)

        curses.echo()
        curses.curs_set(2)
        stdscr.keypad(False)

        stdscr.addstr("Username: \n")
        stdscr.addstr("Password: "******";".join(map(str, userpasw)))

        return credentials
Esempio n. 2
0
    def handle_home_screen(self, client_socket, address, username_logged):
        logged_loop = True
        while logged_loop is True:

            client_reply = utilities.decode_utf_8(client_socket.recv(1024))

            if self.is_the_reply_ctrlc(client_reply):
                self.list_of_logs.append("	({}) {}:{} --> CRASHED".format(
                    utilities.get_date_and_hour(), address[0], address[1]))
                self.remove_connection(address[0], address[1])
                self.remove_user_from_online_list(username_logged)
                logged_loop = False

            else:
                self.list_of_logs.append(
                    "	({}) {}:{} --> {} requested.".format(
                        utilities.get_date_and_hour(), address[0], address[1],
                        client_reply))

                if client_reply == UIText.PLAY_NEW_GAME.value:
                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
                        utilities.get_date_and_hour(), address[0], address[1],
                        client_reply))

                if client_reply == UIText.PLAY_COOP_GAME.value:
                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
                        utilities.get_date_and_hour(), address[0], address[1],
                        client_reply))

                if client_reply == UIText.CONTINUE_GAME.value:
                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
                        utilities.get_date_and_hour(), address[0], address[1],
                        client_reply))

                if client_reply == UIText.PROFILE.value:

                    need_to_disconnect_the_client_or_quit_the_loop = self.handle_profile_screen(
                        client_reply, client_socket, address, username_logged)
                    if need_to_disconnect_the_client_or_quit_the_loop:
                        self.remove_user_from_online_list(username_logged)
                        logged_loop = False

                if client_reply == UIText.LOGOUT.value:
                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
                        utilities.get_date_and_hour(), address[0], address[1],
                        client_reply))

                    self.remove_user_from_online_list(username_logged)
                    logged_loop = False
Esempio n. 3
0
    def get_sign_up_details(self):
        credentials = []
        stdscr.erase()
        stdscr.addstr(
            "Register your details here. Fields with (*) are necessaries.\n\n",
            curses.A_UNDERLINE)

        curses.echo()
        stdscr.keypad(False)
        curses.curs_set(2)

        stdscr.addstr("Username(*): \n")
        stdscr.addstr("Password(*): \n")
        stdscr.addstr("Gender: \n")
        stdscr.addstr("Age: \n")
        stdscr.addstr("Email(*): ")

        username = stdscr.getstr(2, 13, 30)
        password = stdscr.getstr(3, 13, 30)
        gender = stdscr.getstr(4, 8, 30)
        age = stdscr.getstr(5, 5, 30)
        email = stdscr.getstr(6, 10, 30)

        curses.noecho()
        curses.curs_set(0)
        stdscr.keypad(True)

        credentials.append(utilities.decode_utf_8(username))
        credentials.append(utilities.decode_utf_8(password))
        credentials.append(utilities.decode_utf_8(gender))
        credentials.append(utilities.decode_utf_8(age))
        credentials.append(utilities.decode_utf_8(email))

        formatted_credentials = str(";".join(map(str, credentials)))

        return formatted_credentials
Esempio n. 4
0
    def handle_client_connection(self, client_socket, address):
        current_client_running = True
        try:
            while current_client_running:

                client_reply = utilities.decode_utf_8(client_socket.recv(1024))

                if self.is_the_reply_ctrlc(client_reply):
                    self.handle_client_ctrlc_request(address)
                    current_client_running = False

                elif self.is_the_reply_exit(client_reply):
                    self.handle_client_exit_request(address)
                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    current_client_running = False

                elif client_reply == UIText.SIGN_UP.value:
                    self.handle_sign_up_screen(client_reply, client_socket,
                                               address)

                elif client_reply == UIText.SIGN_IN.value:
                    username = self.handle_sign_in_screen(
                        client_reply, client_socket, address)
                    if username:
                        self.handle_home_screen(client_socket, address,
                                                username)

        except socket.error as sock_er:
            self.handle_any_exception(address, sock_er)

        except IOError as ioe:
            if ioe.errno == errno.EPIPE:
                self.handle_any_exception(address, ioe)
            else:
                self.handle_any_exception(address, ioe)
        finally:
            self.remove_connection(address[0], address[1])
            return
Esempio n. 5
0
 def read_decoded_message_via_socket(self):
     return utilities.decode_utf_8(self.socket.recv(1024))
Esempio n. 6
0
    def show_profile_screen(self):

        login_color_attributes = ncurses.init_curses_attributes()

        self.get_from_server_all_user_details()

        username = self.UserDetails.username
        password = self.UserDetails.password
        gender = self.UserDetails.gender
        age = self.UserDetails.age
        email = self.UserDetails.email

        screen_title = "Profile area"
        screen_options = [
            "Username(*) = " + username, "Password(*) = " + password,
            "Gender = " + gender, "Age = " + age, "Email(*) = " + email,
            "Save", "Back"
        ]

        loop_for_the_correct_details = True
        while loop_for_the_correct_details:

            screen_option_index = -1
            last_option_pressed = 0

            while screen_option_index != screen_options.index(
                    "Save") and screen_option_index != screen_options.index(
                        "Back"):
                screen_option_index = self.screen_option_selector(
                    login_color_attributes, screen_options, screen_title,
                    last_option_pressed)
                last_option_pressed = screen_option_index

                curses.echo()
                stdscr.keypad(False)
                curses.curs_set(2)

                if screen_option_index == screen_options.index(
                        screen_options[0]):
                    username = utilities.decode_utf_8(stdscr.getstr(
                        2, 17, 30)).strip()
                    screen_options[0] = "Username(*)" + " = " + username

                elif screen_option_index == screen_options.index(
                        screen_options[1]):
                    password = utilities.decode_utf_8(stdscr.getstr(
                        3, 17, 30)).strip()
                    screen_options[1] = "Password(*)" + " = " + password

                elif screen_option_index == screen_options.index(
                        screen_options[2]):
                    gender = utilities.decode_utf_8(stdscr.getstr(4, 12,
                                                                  30)).strip()
                    screen_options[2] = "Gender" + " = " + gender

                elif screen_option_index == screen_options.index(
                        screen_options[3]):
                    age = utilities.decode_utf_8(stdscr.getstr(5, 19,
                                                               30)).strip()
                    screen_options[3] = "Age" + " = " + age

                elif screen_option_index == screen_options.index(
                        screen_options[4]):
                    email = utilities.decode_utf_8(stdscr.getstr(6, 14,
                                                                 30)).strip()
                    screen_options[4] = "Email(*)" + " = " + email

                curses.noecho()
                curses.curs_set(0)
                stdscr.keypad(True)

            if screen_option_index == screen_options.index("Save"):
                new_details = []
                new_details.append(username)
                new_details.append(password)
                new_details.append(gender)
                new_details.append(age)
                new_details.append(email)

                formatted_credentials = str(";".join(map(str, new_details)))

                message_for_the_server = utilities.UIText.SAVE.value + "-" + formatted_credentials

                self.send_encoded_message_via_socket(message_for_the_server)

                server_reply = self.read_decoded_message_via_socket()

                if server_reply == utilities.ServerResponseStatus.ACK.value:
                    self.print_message_and_press_enter_to_continue(
                        "\nNew details saved.")
                    loop_for_the_correct_details = False

                elif server_reply == utilities.ServerResponseStatus.EMAIL_OR_USER_ALREADY_EXISTS.value:
                    self.print_message_and_press_enter_to_continue(
                        "\nThis username or email already exists.")
                    loop_for_the_correct_details = True
                else:
                    splitted_message = server_reply.split(";")
                    if splitted_message[
                            0] == utilities.ServerResponseStatus.WRONG_USER_DETAILS.value:
                        stdscr.addstr("\n\nServer response:")
                        if "0" in splitted_message[1]:
                            stdscr.addstr(
                                "\n - Username: No special chars or spaces are allowed"
                            )
                        if "1" in splitted_message[1]:
                            stdscr.addstr(
                                "\n - Password: No empty string allowed")
                        if "2" in splitted_message[1]:
                            stdscr.addstr(
                                "\n - Gender: can't contains special chars or digits"
                            )
                        if "3" in splitted_message[1]:
                            stdscr.addstr("\n - Age: digits only.")
                        if "4" in splitted_message[1]:
                            stdscr.addstr(
                                "\n - Email: wrong format. (i.e.: [email protected])"
                            )

                        self.print_message_and_press_enter_to_continue(
                            "\n\nPress ENTER..")
                        loop_for_the_correct_details = True

            elif screen_option_index == screen_options.index("Back"):

                message_for_the_server = utilities.UIText.BACK.value

                self.send_encoded_message_via_socket(message_for_the_server)

                server_reply = self.read_decoded_message_via_socket()

                if server_reply == utilities.ServerResponseStatus.ACK.value:
                    loop_for_the_correct_details = False

        if screen_option_index == screen_options.index("Back"):
            no_need_to_disconnect = False
            return no_need_to_disconnect

        elif screen_option_index == screen_options.index("Save"):
            need_to_disconnect = True
            return need_to_disconnect
Esempio n. 7
0
    def handle_sign_in_screen(self, client_reply, client_socket, address):
        self.list_of_logs.append("	({}) {}:{} --> {} requested.".format(
            utilities.get_date_and_hour(), address[0], address[1],
            client_reply))

        ack = utilities.ServerResponseStatus.ACK.value
        client_socket.send(utilities.encode_utf_8(ack))
        self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
            utilities.get_date_and_hour(), address[0], address[1],
            client_reply))

        client_reply = utilities.decode_utf_8(client_socket.recv(1024))
        self.list_of_logs.append(
            "	({}) {}:{} --> Sign in credentials sent.".format(
                utilities.get_date_and_hour(), address[0], address[1]))

        if self.is_the_reply_ctrlc(client_reply):
            self.list_of_logs.append(
                "	({}) {}:{} has disconnected: CTRL+C detected.".format(
                    utilities.get_date_and_hour(), address[0], address[1]))
            self.remove_connection(address[0], address[1])
            self.close_client_connection(client_socket)

            msg = None

        else:

            splitted_credential = client_reply.split(";")
            username = splitted_credential[0]
            password = splitted_credential[1]

            if self.validate_user(username, password):

                if self.is_user_online(username):
                    rejected = utilities.ServerResponseStatus.USER_ALREADY_ONLINE.value
                    client_socket.send(utilities.encode_utf_8(rejected))
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- Sign in REJECTED - User already online."
                        .format(utilities.get_date_and_hour(), address[0],
                                address[1]))

                    msg = None

                else:
                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- Sign in VALIDATED.".format(
                            utilities.get_date_and_hour(), address[0],
                            address[1]))
                    self.add_user_to_online_list(username)

                    msg = username
            else:
                deny = utilities.ServerResponseStatus.DENY.value
                client_socket.send(utilities.encode_utf_8(deny))
                self.list_of_logs.append(
                    "	({}) {}:{} <-- Sign in DENIED.".format(
                        utilities.get_date_and_hour(), address[0], address[1]))

                msg = None

        return msg
Esempio n. 8
0
    def handle_sign_up_screen(self, client_reply, client_socket, address):
        self.list_of_logs.append("	({}) {}:{} --> {} requested.".format(
            utilities.get_date_and_hour(), address[0], address[1],
            client_reply))

        ack = utilities.ServerResponseStatus.ACK.value

        client_socket.send(utilities.encode_utf_8(ack))

        self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
            utilities.get_date_and_hour(), address[0], address[1],
            client_reply))

        sign_up_loop = True
        while sign_up_loop is True:

            client_reply = utilities.decode_utf_8(client_socket.recv(1024))
            self.list_of_logs.append(
                "	({}) {}:{} --> Sign up details sent.".format(
                    utilities.get_date_and_hour(), address[0], address[1]))

            if self.is_the_reply_ctrlc(client_reply):
                self.list_of_logs.append("	({}) {}:{} --> CRASHED.".format(
                    utilities.get_date_and_hour(), address[0], address[1]))
                self.remove_connection(address[0], address[1])
                sign_up_loop = False

            else:
                splitted_details = client_reply.split(";")

                username = splitted_details[0]
                password = splitted_details[1]
                gender = splitted_details[2]
                age = splitted_details[3]
                email = splitted_details[4]

                sign_up_loop = False
                indexs_wrong_field = []
                if utilities.is_valid_format(username) or len(username) == 0:
                    sign_up_loop = True
                    indexs_wrong_field.append(0)
                if len(password) == 0:
                    sign_up_loop = True
                    indexs_wrong_field.append(1)
                if utilities.is_valid_format(gender) or gender.isdigit():
                    sign_up_loop = True
                    indexs_wrong_field.append(2)
                if not age.isdigit() and len(age) is not 0:
                    sign_up_loop = True
                    indexs_wrong_field.append(3)
                if not utilities.is_valid_email(email):
                    sign_up_loop = True
                    indexs_wrong_field.append(4)

                if sign_up_loop is True:
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- Wrong sign up details received.".
                        format(utilities.get_date_and_hour(), address[0],
                               address[1]))

                    wrongdetails = utilities.ServerResponseStatus.WRONG_USER_DETAILS.value
                    formatted_indexs_wrong_field = str("-".join(
                        map(str, indexs_wrong_field)))
                    msg = []
                    msg.append(wrongdetails)
                    msg.append(formatted_indexs_wrong_field)
                    serverMessage = str(";".join(map(str, msg)))
                    client_socket.send(utilities.encode_utf_8(serverMessage))

                else:
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- Correct sign up details received.".
                        format(utilities.get_date_and_hour(), address[0],
                               address[1]))

                    new_user_details = {
                        "username": splitted_details[0],
                        "password": splitted_details[1],
                        "gender": splitted_details[2],
                        "age": splitted_details[3],
                        "email": splitted_details[4]
                    }

                    if not self.is_already_signed(splitted_details[0],
                                                  splitted_details[4]):
                        self.save_user_credentials(new_user_details)

                        signed = utilities.ServerResponseStatus.SIGNED.value
                        client_socket.send(utilities.encode_utf_8(signed))
                        self.list_of_logs.append(
                            "	({}) {}:{} <-- Sign up completed.".format(
                                utilities.get_date_and_hour(), address[0],
                                address[1]))

                    else:
                        already_signed = utilities.ServerResponseStatus.EMAIL_OR_USER_ALREADY_EXISTS.value
                        client_socket.send(
                            utilities.encode_utf_8(already_signed))
                        self.list_of_logs.append(
                            "	({}) {}:{} <-- Sign up denied - User or email already exists."
                            .format(utilities.get_date_and_hour(), address[0],
                                    address[1]))

                        sign_up_loop = True
Esempio n. 9
0
    def handle_profile_screen(self, client_reply, client_socket, address,
                              username_logged):
        ack = utilities.ServerResponseStatus.ACK.value
        client_socket.send(utilities.encode_utf_8(ack))
        self.list_of_logs.append("	({}) {}:{} <-- ACK {}.".format(
            utilities.get_date_and_hour(), address[0], address[1],
            client_reply))

        need_to_disconnect_the_client = False
        client_is_crashed = False
        profile_loop = True

        while profile_loop is True:

            client_reply = utilities.decode_utf_8(client_socket.recv(1024))
            client_reply_split = client_reply.split("-")

            if self.is_the_reply_ctrlc(client_reply_split):
                self.list_of_logs.append("	({}) {}:{} --> CRASHED.".format(
                    utilities.get_date_and_hour(), address[0], address[1]))
                self.remove_connection(address[0], address[1])
                profile_loop = False
                client_is_crashed = True

            elif client_reply_split[0] == UIText.SAVE.value:

                split_details = client_reply_split[1].split(";")

                username = split_details[0]
                password = split_details[1]
                gender = split_details[2]
                age = split_details[3]
                email = split_details[4]

                new_user_details = {
                    "username": username,
                    "password": password,
                    "gender": gender,
                    "age": age,
                    "email": email
                }

                profile_loop = False
                indexs_wrong_field = []
                if utilities.is_valid_format(username) or len(username) == 0:
                    profile_loop = True
                    indexs_wrong_field.append(0)
                if len(password) == 0:
                    profile_loop = True
                    indexs_wrong_field.append(1)
                if utilities.is_valid_format(gender) or gender.isdigit():
                    profile_loop = True
                    indexs_wrong_field.append(2)
                if not age.isdigit() and len(age) is not 0:
                    profile_loop = True
                    indexs_wrong_field.append(3)
                if not utilities.is_valid_email(email):
                    profile_loop = True
                    indexs_wrong_field.append(4)

                if profile_loop is True:
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- Wrong new credentials received.".
                        format(utilities.get_date_and_hour(), address[0],
                               address[1]))

                    wrongdetails = utilities.ServerResponseStatus.WRONG_USER_DETAILS.value
                    formatted_indexs_wrong_field = str("-".join(
                        map(str, indexs_wrong_field)))
                    msg = []
                    msg.append(wrongdetails)
                    msg.append(formatted_indexs_wrong_field)
                    serverMessage = str(";".join(map(str, msg)))
                    client_socket.send(utilities.encode_utf_8(serverMessage))

                elif not self.is_already_signed(
                        new_user_details.get("username"),
                        new_user_details.get("password")):
                    self.save_existing_user_new_credentials(
                        new_user_details, username_logged)

                    ack = utilities.ServerResponseStatus.ACK.value
                    client_socket.send(utilities.encode_utf_8(ack))
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- New credentials saved.".format(
                            utilities.get_date_and_hour(),
                            address[0],
                            address[1],
                        ))

                    profile_loop = False
                    need_to_disconnect_the_client = True
                else:
                    already_signed = utilities.ServerResponseStatus.EMAIL_OR_USER_ALREADY_EXISTS.value
                    client_socket.send(utilities.encode_utf_8(already_signed))
                    self.list_of_logs.append(
                        "	({}) {}:{} <-- New credential not saved - Username or email already exists."
                        .format(utilities.get_date_and_hour(), address[0],
                                address[1]))

                    profile_loop = True

            elif client_reply_split[0] == UIText.BACK.value:

                ack = utilities.ServerResponseStatus.ACK.value
                client_socket.send(utilities.encode_utf_8(ack))
                self.list_of_logs.append(
                    "	({}) {}:{} <-- No changes applied. Profile closed.".
                    format(utilities.get_date_and_hour(), address[0],
                           address[1]))
                profile_loop = False

        if need_to_disconnect_the_client or client_is_crashed:
            return True
        else:
            return False