class documentation

A POP3 client protocol.

Instances of this class provide a convenient, efficient API for retrieving and deleting messages from a POP3 server.

This API provides a pipelining interface but POP3 pipelining on the network is not yet supported.

Method apop Send an APOP command to perform authenticated login.
Method capabilities Send a CAPA command to retrieve the capabilities supported by the server.
Method connectionLost Clean up when the connection has been lost.
Method connectionMade Wait for a greeting from the server after the connection has been made.
Method delete Send a DELE command to delete a message from the server.
Method lineLengthExceeded Drop the connection when a server response exceeds the maximum line length (LineOnlyReceiver.MAX_LENGTH).
Method lineReceived Pass a received line to a state machine function and transition to the next state.
Method listSize Send a LIST command to retrieve the sizes of all messages on the server.
Method listUID Send a UIDL command to retrieve the UIDs of all messages on the server.
Method login Log in to the server.
Method noop Send a NOOP command asking the server to do nothing but respond.
Method password Send a PASS command to perform the second half of plaintext login.
Method quit Send a QUIT command to disconnect from the server.
Method reset Send a RSET command to unmark any messages that have been flagged for deletion on the server.
Method retrieve Send a RETR or TOP command to retrieve all or part of a message from the server.
Method sendLong Send a POP3 command to which a multi-line response is expected.
Method sendShort Send a POP3 command to which a short response is expected.
Method serverGreeting Handle the server greeting.
Method startTLS Switch to encrypted communication using TLS.
Method stat Send a STAT command to get information about the size of the mailbox.
Method state_LONG Handle server responses for the LONG state in which the server is expected to send a non-initial line of a multi-line response.
Method state_LONG_INITIAL Handle server responses for the LONG_INITIAL state in which the server is expected to send the first line of a multi-line response.
Method state_SHORT Handle server responses for the SHORT state in which the server is expected to send a single line response.
Method state_WAITING Log an error for server responses received in the WAITING state during which the server is not expected to send anything.
Method state_WELCOME Handle server responses for the WELCOME state in which the server greeting is expected.
Method timeoutConnection Drop the connection when the server does not respond in time.
Method user Send a USER command to perform the first half of plaintext login.
Instance Variable allowInsecureLogin An indication of whether plaintext login should be allowed when the server offers no authentication challenge and the transport does not offer any protection via encryption.
Instance Variable serverChallenge The challenge received in the server greeting.
Instance Variable startedTLS An indication of whether TLS has been negotiated successfully.
Instance Variable state The state which indicates what type of response is expected from the server. Valid states are: 'WELCOME', 'WAITING', 'SHORT', 'LONG_INITIAL', 'LONG'.
Instance Variable timeout The number of seconds to wait on a response from the server before timing out a connection. If the number is <= 0, no timeout checking will be performed.
Instance Variable transport Undocumented
Method _apop Perform an APOP login.
Method _blocked Block a command, if necessary.
Method _consumeOrAppend Send a command to which a long response is expected and process the multi-line response into a list.
Method _consumeOrSetItem Send a command to which a long response is expected and process the multi-line response into a list accounting for deleted messages.
Method _getContextFactory Get a context factory with which to negotiate TLS.
Method _login Continue the process of logging in to the server.
Method _loginTLS Do a plaintext login over an encrypted transport.
Method _plaintext Perform a plaintext login.
Method _startedTLS Complete the process of switching to encrypted communication.
Method _startTLS Continue the process of switching to encrypted communication.
Method _unblock Send the next blocked command.
Instance Variable _blockedQueue A list of blocked commands. While a command is awaiting a response from the server, other commands are blocked. When no command is outstanding, _blockedQueue is set to None. Otherwise, it contains a list of information about blocked commands...
Instance Variable _capCache The cached server capabilities. Capabilities are not allowed to change during the session (except when TLS is negotiated), so the first response to a capabilities command can be used for later lookups.
Instance Variable _challengeMagicRe A regular expression which matches the challenge in the server greeting.
Instance Variable _consumer The consumer function which is used to store the values derived by the transform function from each line of a multi-line response into a list.
Instance Variable _greetingError The server greeting minus the status indicator, when the connection was dropped because of an error in the server greeting. Otherwise, None.
Instance Variable _timedOut An indication of whether the connection was dropped because of a timeout.
Instance Variable _waiting A deferred which fires when the response to the outstanding command is received from the server.
Instance Variable _xform The transform function which is used to convert each line of a multi-line response into usable values for use by the consumer function. If None, each line of the multi-line response is sent directly to the consumer function.

Inherited from LineOnlyReceiver:

Method dataReceived Translates bytes into lines, and calls lineReceived.
Method sendLine Sends a line to the other end of the connection.
Constant MAX_LENGTH The maximum length of a line to allow (If a sent line is longer than this, the connection is dropped). Default is 16384.
Class Variable delimiter The line-ending delimiter to use. By default this is b'\r\n'.
Instance Variable _buffer Undocumented

Inherited from Protocol (via LineOnlyReceiver):

Method logPrefix Return a prefix matching the class name, to identify log messages related to this protocol instance.
Class Variable factory Undocumented

Inherited from BaseProtocol (via LineOnlyReceiver, Protocol):

Method makeConnection Make a connection to a transport and a server.
Instance Variable connected Undocumented

Inherited from TimeoutMixin (via LineOnlyReceiver, Protocol, BaseProtocol):

Method callLater Wrapper around reactor.callLater for test purpose.
Method resetTimeout Reset the timeout count down.
Method setTimeout Change the timeout period
Instance Variable timeOut The number of seconds after which to timeout the connection.
Method __timedOut Undocumented
Instance Variable __timeoutCall Undocumented
def apop(self, username, digest): (source)

Send an APOP command to perform authenticated login.

This should be used in special circumstances only, when it is known that the server supports APOP authentication, and APOP authentication is absolutely required. For the common case, use login instead.

Parameters
username:bytesThe username with which to log in.
digest:bytesThe challenge response to authenticate with.
Returns
Deferred which successfully fires with bytes or fails with ServerErrorResponseA deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure.
def capabilities(self, useCache=True): (source)

Send a CAPA command to retrieve the capabilities supported by the server.

Not all servers support this command. If the server does not support this, it is treated as though it returned a successful response listing no capabilities. At some future time, this may be changed to instead seek out information about a server's capabilities in some other fashion (only if it proves useful to do so, and only if there are servers still in use which do not support CAPA but which do support POP3 extensions that are useful).

Parameters
useCache:boolA flag that determines whether previously retrieved results should be used if available.
Returns
Deferred which successfully results in dict mapping bytes to list of bytes and/or bytes to None

A deferred which fires with a mapping of capability name to parameters. For example:

    C: CAPA
    S: +OK Capability list follows
    S: TOP
    S: USER
    S: SASL CRAM-MD5 KERBEROS_V4
    S: RESP-CODES
    S: LOGIN-DELAY 900
    S: PIPELINING
    S: EXPIRE 60
    S: UIDL
    S: IMPLEMENTATION Shlemazle-Plotz-v302
    S: .

will be lead to a result of:

    | {'TOP': None,
    |  'USER': None,
    |  'SASL': ['CRAM-MD5', 'KERBEROS_V4'],
    |  'RESP-CODES': None,
    |  'LOGIN-DELAY': ['900'],
    |  'PIPELINING': None,
    |  'EXPIRE': ['60'],
    |  'UIDL': None,
    |  'IMPLEMENTATION': ['Shlemazle-Plotz-v302']}
def connectionLost(self, reason): (source)

Clean up when the connection has been lost.

When the loss of connection was initiated by the client due to a timeout, the _timedOut flag will be set. When it was initiated by the client due to an error in the server greeting, _greetingError will be set to the server response minus the status indicator.

Parameters
reason:FailureThe reason the connection was terminated.
def connectionMade(self): (source)

Wait for a greeting from the server after the connection has been made.

Start the connection in the WELCOME state.

def delete(self, index): (source)

Send a DELE command to delete a message from the server.

Parameters
index:intThe 0-based index of the message to delete.
Returns
Deferred which successfully fires with bytes or fails with ServerErrorResponseA deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure.
def lineLengthExceeded(self, buffer): (source)

Drop the connection when a server response exceeds the maximum line length (LineOnlyReceiver.MAX_LENGTH).

Parameters
buffer:bytesA received line which exceeds the maximum line length.
def lineReceived(self, line): (source)

Pass a received line to a state machine function and transition to the next state.

Parameters
line:bytesA received line.
def listSize(self, consumer=None): (source)

Send a LIST command to retrieve the sizes of all messages on the server.

Parameters
consumer:None or callable that takes 2-tuple of (0) int, (1) intA function which consumes the 0-based message index and message size derived from the server response.
Returns
Deferred which fires list of int or callable that takes 2-tuple of (0) int, (1) intA deferred which fires when the entire response has been received. When a consumer is not provided, the return value is a list of message sizes. Otherwise, it returns the consumer itself.
def listUID(self, consumer=None): (source)

Send a UIDL command to retrieve the UIDs of all messages on the server.

Parameters
consumer:None or callable that takes 2-tuple of (0) int, (1) bytesA function which consumes the 0-based message index and UID derived from the server response.
Returns
Deferred which fires with list of object or callable that takes 2-tuple of (0) int, (1) bytesA deferred which fires when the entire response has been received. When a consumer is not provided, the return value is a list of message sizes. Otherwise, it returns the consumer itself.
def login(self, username, password): (source)

Log in to the server.

If APOP is available it will be used. Otherwise, if TLS is available, an encrypted session will be started and plaintext login will proceed. Otherwise, if allowInsecureLogin is set, insecure plaintext login will proceed. Otherwise, InsecureAuthenticationDisallowed will be raised.

The first step of logging into the server is obtaining the server's capabilities. When that is complete, the _login callback function continues the login process.

Parameters
username:bytesThe username with which to log in.
password:bytesThe password with which to log in.
Returns
Deferred which successfully fires with bytesA deferred which fires when the login process is complete. On a successful login, it returns the server's response minus the status indicator.
def noop(self): (source)

Send a NOOP command asking the server to do nothing but respond.

Returns
Deferred which successfully fires with bytes or fails with ServerErrorResponseA deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure.
def password(self, password): (source)

Send a PASS command to perform the second half of plaintext login.

Unless this is absolutely required, use the login method instead.

Parameters
password:bytesThe plaintext password with which to authenticate.
Returns
Deferred which successfully fires with bytes or fails with ServerErrorResponseA deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure.
def quit(self): (source)

Send a QUIT command to disconnect from the server.

Returns
Deferred which successfully fires with bytes or fails with ServerErrorResponseA deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure.
def reset(self): (source)

Send a RSET command to unmark any messages that have been flagged for deletion on the server.

Returns
Deferred which successfully fires with bytes or fails with ServerErrorResponseA deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure.
def retrieve(self, index, consumer=None, lines=None): (source)

Send a RETR or TOP command to retrieve all or part of a message from the server.

Parameters
index:intA 0-based message index.
consumer:None or callable that takes bytesA function which consumes each transformed line from a multi-line response as it is received.
lines:None or intIf specified, the number of lines of the message to be retrieved. Otherwise, the entire message is retrieved.
Returns
Deferred which fires with list of bytes, or callable that takes 2-tuple of (0) int, (1) objectA deferred which fires when the entire response has been received. When a consumer is not provided, the return value is a list of the transformed lines. Otherwise, it returns the consumer itself.
def sendLong(self, cmd, args, consumer, xform): (source)

Send a POP3 command to which a multi-line response is expected.

Block all further commands from being sent until the entire response is received. Transition the state to LONG_INITIAL.

Parameters
cmd:bytesA POP3 command.
args:bytesThe command arguments.
consumer:callable that takes objectA consumer function which should be used to put the values derived by a transform function from each line of the multi-line response into a list.
xform:None or callable that takes bytes and returns objectA transform function which should be used to transform each line of the multi-line response into usable values for use by a consumer function. If None, each line of the multi-line response should be sent directly to the consumer function.
Returns
Deferred which successfully fires with callable that takes object and fails with ServerErrorResponseA deferred which fires when the entire response is received. On an OK response, it returns the consumer function. On an ERR response, it issues a server error response failure with the response from the server minus the status indicator and the consumer function.
def sendShort(self, cmd, args): (source)

Send a POP3 command to which a short response is expected.

Block all further commands from being sent until the response is received. Transition the state to SHORT.

Parameters
cmd:bytesA POP3 command.
args:bytesThe command arguments.
Returns
Deferred which successfully fires with bytes or fails with ServerErrorResponseA deferred which fires when the entire response is received. On an OK response, it returns the response from the server minus the status indicator. On an ERR response, it issues a server error response failure with the response from the server minus the status indicator.
def serverGreeting(self, greeting): (source)

Handle the server greeting.

Parameters
greeting:bytesThe server greeting minus the status indicator. For servers implementing APOP authentication, this will contain a challenge string.
def startTLS(self, contextFactory=None): (source)

Switch to encrypted communication using TLS.

The first step of switching to encrypted communication is obtaining the server's capabilities. When that is complete, the _startTLS callback function continues the switching process.

Parameters
contextFactory:None or ClientContextFactoryThe context factory with which to negotiate TLS. If not provided, try to create a new one.
Returns
Deferred which successfully results in dict mapping bytes to list of bytes and/or bytes to None or fails with TLSErrorA deferred which fires when the transport has been secured according to the given context factory with the server capabilities, or which fails with a TLS error if the transport cannot be secured.
def stat(self): (source)

Send a STAT command to get information about the size of the mailbox.

Returns
Deferred which successfully fires with a 2-tuple of (0) int, (1) int or fails with ServerErrorResponseA deferred which fires when the server response is received. On an OK response, the deferred succeeds with the number of messages in the mailbox and the size of the mailbox in octets. On an ERR response, the deferred fails with a server error response failure.
def state_LONG(self, line): (source)

Handle server responses for the LONG state in which the server is expected to send a non-initial line of a multi-line response.

On receipt of the last line of the response, clean up, fire the deferred which is waiting on receipt of a complete response, and transition the state to WAITING. Otherwise, pass the line to the transform function, if provided, and then the consumer function.

Parameters
line:bytesA line received from the server.
Returns
bytesThe next state.
def state_LONG_INITIAL(self, line): (source)

Handle server responses for the LONG_INITIAL state in which the server is expected to send the first line of a multi-line response.

Parse the response. On an OK response, transition the state to LONG. On an ERR response, cleanup and transition the state to WAITING.

Parameters
line:bytesA line received from the server.
Returns
bytesThe next state.
def state_SHORT(self, line): (source)

Handle server responses for the SHORT state in which the server is expected to send a single line response.

Parse the response and fire the deferred which is waiting on receipt of a complete response. Transition the state back to WAITING.

Parameters
line:bytesA line received from the server.
Returns
bytesThe next state.
def state_WAITING(self, line): (source)

Log an error for server responses received in the WAITING state during which the server is not expected to send anything.

Parameters
line:bytesA line received from the server.
def state_WELCOME(self, line): (source)

Handle server responses for the WELCOME state in which the server greeting is expected.

WELCOME is the first state. The server should send one line of text with a greeting and possibly an APOP challenge. Transition the state to WAITING.

Parameters
line:bytesA line received from the server.
Returns
bytesThe next state.
def timeoutConnection(self): (source)

Drop the connection when the server does not respond in time.

def user(self, username): (source)

Send a USER command to perform the first half of plaintext login.

Unless this is absolutely required, use the login method instead.

Parameters
username:bytesThe username with which to log in.
Returns
Deferred which successfully fires with bytes or fails with ServerErrorResponseA deferred which fires when the server response is received. On an OK response, the deferred succeeds with the server response minus the status indicator. On an ERR response, the deferred fails with a server error response failure.
allowInsecureLogin: bool = (source)

An indication of whether plaintext login should be allowed when the server offers no authentication challenge and the transport does not offer any protection via encryption.

serverChallenge: bytes or None = (source)

The challenge received in the server greeting.

startedTLS: bool = (source)

An indication of whether TLS has been negotiated successfully.

The state which indicates what type of response is expected from the server. Valid states are: 'WELCOME', 'WAITING', 'SHORT', 'LONG_INITIAL', 'LONG'.

The number of seconds to wait on a response from the server before timing out a connection. If the number is <= 0, no timeout checking will be performed.

def _apop(self, username, password, challenge): (source)

Perform an APOP login.

Parameters
username:bytesThe username with which to log in.
password:bytesThe password with which to log in.
challenge:bytesA challenge string.
Returns
Deferred which successfully fires with bytes or fails with ServerErrorResponseA deferred which fires when the server response is received. On a successful login, it returns the server response minus the status indicator.
def _blocked(self, f, *a): (source)

Block a command, if necessary.

If commands are being blocked, append information about the function which sends the command to a list and return a deferred that will be chained with the return value of the function when it eventually runs. Otherwise, set up for subsequent commands to be blocked and return None.

Parameters
f:callableA function which sends a command.
*a:tupleArguments to the function.
Returns
None or DeferredNone if the command can run immediately. Otherwise, a deferred that will eventually trigger with the return value of the function.
def _consumeOrAppend(self, cmd, args, consumer, xform): (source)

Send a command to which a long response is expected and process the multi-line response into a list.

Parameters
cmd:bytesA POP3 command which expects a long response.
args:bytesThe command arguments.
consumer:None or callable that takes objectNone or a function that consumes the output from the transform function.
xform:None or callable that takes bytes and returns objectA function that transforms a line from a multi-line response into usable form for input to the consumer function. If no transform function is specified, the line is used as is.
Returns
Deferred which fires with list of 2-tuple of (0) int, (1) object or callable that takes 2-tuple of (0) int, (1) objectA deferred which fires when the entire response has been received. When a consumer is not provided, the return value is a list of the transformed lines. Otherwise, it returns the consumer itself.
def _consumeOrSetItem(self, cmd, args, consumer, xform): (source)

Send a command to which a long response is expected and process the multi-line response into a list accounting for deleted messages.

Parameters
cmd:bytesA POP3 command to which a long response is expected.
args:bytesThe command arguments.
consumer:None or callable that takes objectNone or a function that consumes the output from the transform function.
xform:None, callable that takes bytes and returns 2-tuple of (0) int, (1) object, or callable that takes bytes and returns objectA function that parses a line from a multi-line response and transforms the values into usable form for input to the consumer function. If no consumer function is specified, the output must be a message index and corresponding value. If no transform function is specified, the line is used as is.
Returns
Deferred which fires with list of object or callable that takes list of objectA deferred which fires when the entire response has been received. When a consumer is not provided, the return value is a list of the value for each message or None for deleted messages. Otherwise, it returns the consumer itself.
def _getContextFactory(self): (source)

Get a context factory with which to negotiate TLS.

Returns
None or ClientContextFactoryA context factory or None if TLS is not supported on the client.
def _login(self, caps, username, password): (source)

Continue the process of logging in to the server.

This callback function runs after the server capabilities are received.

If the server provided a challenge in the greeting, proceed with an APOP login. Otherwise, if the server and the transport support encrypted communication, try to switch to TLS and then complete the login process with the _loginTLS callback function. Otherwise, if insecure authentication is allowed, do a plaintext login. Otherwise, fail with an InsecureAuthenticationDisallowed error.

Parameters
caps:dict mapping bytes to list of bytes and/or bytes to NoneThe server capabilities.
username:bytesThe username with which to log in.
password:bytesThe password with which to log in.
Returns
Deferred which successfully fires with bytesA deferred which fires when the login process is complete. On a successful login, it returns the server's response minus the status indicator.
def _loginTLS(self, res, username, password): (source)

Do a plaintext login over an encrypted transport.

This callback function runs after the transport switches to encrypted communication.

Parameters
res:dict mapping bytes to list of bytes and/or bytes to NoneThe server capabilities.
username:bytesThe username with which to log in.
password:bytesThe password with which to log in.
Returns
Deferred which successfully fires with bytes or fails with ServerErrorResponseA deferred which fires when the server accepts the username and password or fails when the server rejects either. On a successful login, it returns the server's response minus the status indicator.
def _plaintext(self, username, password): (source)

Perform a plaintext login.

Parameters
username:bytesThe username with which to log in.
password:bytesThe password with which to log in.
Returns
Deferred which successfully fires with bytes or fails with ServerErrorResponseA deferred which fires when the server accepts the username and password or fails when the server rejects either. On a successful login, it returns the server's response minus the status indicator.
def _startedTLS(self, result, context, tls): (source)

Complete the process of switching to encrypted communication.

This callback function runs after the response to the STLS command has been received.

The final steps are discarding the cached capabilities and initiating TLS negotiation on the transport.

Parameters
result:dict mapping bytes to list of bytes and/or bytes to NoneThe server capabilities.
context:ClientContextFactoryA context factory with which to negotiate TLS.
tls:ITLSTransportA TCP transport that supports switching to TLS midstream.
Returns
dict mapping bytes to list of bytes and/or bytes to NoneThe server capabilities.
def _startTLS(self, caps, contextFactory, tls): (source)

Continue the process of switching to encrypted communication.

This callback function runs after the server capabilities are received.

The next step is sending the server an STLS command to request a switch to encrypted communication. When an OK response is received, the _startedTLS callback function completes the switch to encrypted communication. Then, the new server capabilities are requested.

Parameters
caps:dict mapping bytes to list of bytes and/or bytes to NoneThe server capabilities.
contextFactory:ClientContextFactoryA context factory with which to negotiate TLS.
tls:ITLSTransportA TCP transport that supports switching to TLS midstream.
Returns
Deferred which successfully triggers with dict mapping bytes to list of bytes and/or bytes to None or fails with TLSNotSupportedErrorA deferred which successfully fires when the response from the server to the request to start TLS has been received and the new server capabilities have been received or fails when the server does not support TLS.
def _unblock(self): (source)

Send the next blocked command.

If there are no more commands in the blocked queue, set up for the next command to be sent immediately.

_blockedQueue: None or list of 3-tuple of (0) Deferred, (1) callable which results in a Deferred, (2) tuple = (source)

A list of blocked commands. While a command is awaiting a response from the server, other commands are blocked. When no command is outstanding, _blockedQueue is set to None. Otherwise, it contains a list of information about blocked commands. Each list entry provides the following information about a blocked command: the deferred that should be called when the response to the command is received, the function that sends the command, and the arguments to the function.

_capCache: None or dict mapping bytes to list of bytes and/or bytes to None = (source)

The cached server capabilities. Capabilities are not allowed to change during the session (except when TLS is negotiated), so the first response to a capabilities command can be used for later lookups.

_challengeMagicRe: Pattern = (source)

A regular expression which matches the challenge in the server greeting.

_consumer: callable that takes object = (source)

The consumer function which is used to store the values derived by the transform function from each line of a multi-line response into a list.

_greetingError: bytes or None = (source)

The server greeting minus the status indicator, when the connection was dropped because of an error in the server greeting. Otherwise, None.

_timedOut: bool = (source)

An indication of whether the connection was dropped because of a timeout.

A deferred which fires when the response to the outstanding command is received from the server.

_xform: None or callable that takes bytes and returns object = (source)

The transform function which is used to convert each line of a multi-line response into usable values for use by the consumer function. If None, each line of the multi-line response is sent directly to the consumer function.