コード例 #1
0
 def send_users_list(self, roomname, conn):
     # Get users list from room
     if roomname in self.rooms:
         users_list = list(self.rooms[roomname].users.keys())
         send_packet(ListUsersResponsePacket(roomname, users_list), conn)
     else:
         error_msg = f"No room called {roomname} found!"
         send_packet(ErrorMessagePacket(error_msg), conn)
コード例 #2
0
 def register_user(self, username, conn, addr):
     if username not in self.usernames:
         self.usernames.append(username)
         self.connections[conn] = User(username, addr, conn)
     else:
         error_msg = f"Username {username} already taken!" \
                     + " Reconnect with a new username."
         send_packet(ErrorMessagePacket(error_msg), conn)
         raise DuplicateUsernameError()
コード例 #3
0
    def create_new_chatroom(self, room_name, conn):
        # Check if there's already a room of that name
        if room_name in self.rooms:
            error_msg = f"A room with name {room_name} already exists!"
            send_packet(ErrorMessagePacket(error_msg), conn)
        else:
            # Create new room
            self.rooms[room_name] = Chatroom(room_name)

            # Send success message to user
            send_packet(CreateRoomResponsePacket(room_name), conn)
コード例 #4
0
    def send_pvt_msg(self, username, receiver, message, conn):
        # Check if receiver exists
        if receiver not in self.usernames:
            error_msg = f"No user called {receiver} found!"
            send_packet(ErrorMessagePacket(error_msg), conn)

        # Send message to receiver
        else:
            for receiver_conn, user in self.connections.items():
                if receiver == user.username:
                    send_packet(TellPvtMsgPacket(username, message),
                                receiver_conn)
コード例 #5
0
    def send_msg(self, username, room_name, message, conn):
        # Check if room exists
        if room_name not in self.rooms.keys():
            error_msg = f"No room called {room_name} found!"
            send_packet(ErrorMessagePacket(error_msg), conn)

        # Check if user is in room
        elif username not in self.rooms[room_name].users.keys():
            error_msg = f"You must first join room {room_name} " \
                + "to send a message to it!"
            send_packet(ErrorMessagePacket(error_msg), conn)

        # Broadcast message to all users in room
        else:
            self.rooms[room_name].broadcast(
                TellMsgPacket(username, room_name, message))
コード例 #6
0
    def remove_user_from_room(self, username, room_name, conn):
        # Check if room exists
        if room_name not in self.rooms.keys():
            error_msg = f"No room called {room_name} found!"
            send_packet(ErrorMessagePacket(error_msg), conn)
        # Check if user is in room
        elif username not in self.rooms[room_name].users.keys():
            error_msg = f"You have not joined room {room_name}!"
            send_packet(ErrorMessagePacket(error_msg), conn)
        else:
            # Remove from room's user list
            self.rooms[room_name].remove_user(username)

            # Announce to all users in room
            self.rooms[room_name].broadcast(
                LeaveRoomResponsePacket(username, room_name))
コード例 #7
0
    def add_user_to_room(self, username, room_name, conn, addr):
        # Check if room exists
        if room_name not in self.rooms.keys():
            error_msg = f"No room called {room_name} found!"
            send_packet(ErrorMessagePacket(error_msg), conn)
        else:
            # Check if username is already in room's list:
            if username in self.rooms[room_name].users.keys():
                error_msg = f"Username {username} already taken!" \
                            + " Join a different room or reconnect with a new username."
                send_packet(ErrorMessagePacket(error_msg), conn)

            else:
                # Add to room's user list
                self.rooms[room_name].add_user(User(username, addr, conn))

                # Announce to all users in room
                self.rooms[room_name].broadcast(
                    JoinRoomResponsePacket(username, room_name))
コード例 #8
0
ファイル: client.py プロジェクト: meeramurali/IRC_application
def process_command(command):
    command_split = command.replace('\n', '').split(':')

    if command_split[0] == "list_rooms":
        send_packet(ListRoomsPacket(username=username), server_socket)

    elif command_split[0] == "create_room":
        if (len(command_split) != 2):
            print(('*** Invalid command! Do you want to create a new chatroom?'
                   ' Try \"create_room:<room name>\". ***'))
        else:
            send_packet(
                CreateRoomPacket(username=username, roomname=command_split[1]),
                server_socket)

    elif command_split[0] == "join_room":
        if (len(command_split) != 2):
            print(('*** Invalid command! Do you want to join a chatroom?'
                   ' Try \"join_room:<room name>\". ***'))
        else:
            send_packet(
                JoinRoomPacket(username=username, roomname=command_split[1]),
                server_socket)

    elif command_split[0] == "leave_room":
        if (len(command_split) != 2):
            print(('*** Invalid command! Do you want to leave a chatroom?'
                   ' Try \"leave_room:<room name>\". ***'))
        else:
            send_packet(
                LeaveRoomPacket(username=username, roomname=command_split[1]),
                server_socket)

    elif command_split[0] == "list_users":
        if (len(command_split) != 2):
            print((
                '*** Invalid command! Do you want to list users in a chatroom?'
                ' Try \"list_users:<room name>\". ***'))
        else:
            send_packet(
                ListUsersPacket(username=username, roomname=command_split[1]),
                server_socket)

    elif command_split[0] == "send_msg":
        if (len(command_split) != 3):
            print(('*** Invalid command! Do you want to send a message?'
                   ' Try \"send_msg:<room name>:<some message>\". ***'))
        else:
            send_packet(
                SendMessagePacket(username=username,
                                  roomname=command_split[1],
                                  msg=command_split[2]), server_socket)

    elif command_split[0] == "send_pvt_msg":
        if (len(command_split) != 3):
            print((
                '*** Invalid command! Do you want to send a private message?'
                ' Try \"send_pvt_msg:<receiver username>:<some message>\". ***'
            ))
        else:
            send_packet(
                SendPvtMessagePacket(username=username,
                                     receiver=command_split[1],
                                     msg=command_split[2]), server_socket)

    elif command_split[0] == "exit":
        raise ExitIRCApp()

    else:
        print('*** Invalid command! Here\'s a list of available commands: ***')
        print_dict(title="User commands", dict_to_print=CLIENT_COMMANDS)
コード例 #9
0
ファイル: client.py プロジェクト: meeramurali/IRC_application
    else:
        print('*** Invalid command! Here\'s a list of available commands: ***')
        print_dict(title="User commands", dict_to_print=CLIENT_COMMANDS)


# Get username from command line arg
if len(sys.argv) != 2:
    print("Correct usage: python3 client.py <username>")
    exit()
username = str(sys.argv[1])

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
    server_socket.connect((SERVER_IP_ADDR, SERVER_PORT))

    try:
        send_packet(RegisterUserPacket(username=username), server_socket)

        while True:
            # list of possible input streams
            sockets_list = [sys.stdin, server_socket]
            read_sockets, _, _ = select.select(sockets_list, [], [])

            for socket in read_sockets:
                # get packet from server
                if socket == server_socket:
                    packet_json_str = socket.recv(2048)
                    if packet_json_str:
                        process_packet(packet_json_str)
                    else:
                        raise ServerCrashError()
コード例 #10
0
 def disconnect_user(self, conn):
     send_packet(DisconnectUserPacket(), conn)
コード例 #11
0
    def send_rooms_list(self, conn):
        # Get room list
        room_list = list(self.rooms.keys())

        # Send to asking user
        send_packet(ListRoomsResponsePacket(room_list), conn)