Skip to content

POP3 Protocol

pop3.pop3_states

handle_authorization(protocol, command)

Handle the authorization phase of the POP3 protocol.

This function processes USER and PASS commands to authenticate the user. If the user is authenticated, the state transitions to TRANSACTION.

Parameters:

Name Type Description Default
protocol

The current protocol instance.

required
command str

The command issued by the client.

required

Returns:

Name Type Description
str

A response string indicating the result of the command.

Source code in src/pop3/pop3_states.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def handle_authorization(protocol, command):
    """
    Handle the authorization phase of the POP3 protocol.

    This function processes USER and PASS commands to authenticate the user.
    If the user is authenticated, the state transitions to TRANSACTION.

    Args:
        protocol: The current protocol instance.
        command (str): The command issued by the client.

    Returns:
        str: A response string indicating the result of the command.
    """
    command_upper = command.upper()
    if command_upper.startswith("USER"):
        protocol.user = command.split(" ", 1)[1] if len(command.split(" ", 1)) > 1 else None
        if protocol.user:
            logger.debug(f"USER command received. User set to: {protocol.user}")
            return "+OK User accepted"
        return "-ERR Missing username"
    elif command_upper.startswith("PASS"):
        protocol.passwd = command.split(" ", 1)[1] if len(command.split(" ", 1)) > 1 else None
        if protocol.passwd:
            protocol.state = 'TRANSACTION'
            logger.debug(f"PASS command received. Password set. Moving to TRANSACTION state.")
            return "+OK Password accepted"
        return "-ERR Missing password"
    return "-ERR Please authenticate first"

handle_transaction(protocol, command)

Handle the transaction phase of the POP3 protocol.

This function processes various commands such as STAT, LIST, RETR, DELE, and QUIT, which operate on the emails available in the session.

Parameters:

Name Type Description Default
protocol

The current protocol instance.

required
command str

The command issued by the client.

required

Returns:

Name Type Description
str

A response string indicating the result of the command.

Source code in src/pop3/pop3_states.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def handle_transaction(protocol, command):
    """
    Handle the transaction phase of the POP3 protocol.

    This function processes various commands such as STAT, LIST, RETR, DELE, and QUIT,
    which operate on the emails available in the session.

    Args:
        protocol: The current protocol instance.
        command (str): The command issued by the client.

    Returns:
        str: A response string indicating the result of the command.
    """
    command_upper = command.upper()
    if command_upper.startswith("STAT"):
        return _handle_stat(protocol)
    elif command_upper.startswith("LIST"):
        return _handle_list(protocol)
    elif command_upper.startswith("RETR"):
        return _handle_retr(protocol, command)
    elif command_upper.startswith("DELE"):
        return _handle_dele(protocol, command)
    elif command_upper.startswith("QUIT"):
        return _handle_quit(protocol, command)
    return "-ERR Command unrecognized"