4.13.2.2. Calling methods

For calling a method, the hash must contain a tuple containing two values. The first value is IMAP_REQ_POLICY and the second is the function to call. The function must return with one of the IMAP_REQ_* values (excluding IMAP_*_POLICY), displayed in the table above.

The function is called with three arguments (apart from 'self'): the command tag, the command name, and its arguments. The representation of arguments used by IMAP is described in Section 4.13.2.4, The IMAP command structure in policies.

If the proxy is to answer instead of the server, the action tuples must contain the following three items: The value IMAP_REQ_RESPOND; the STRING to be sent back followed by a command tag, and a LIST containing untagged lines to be sent back to the client.

For example, to reply to every CAPABILITY request on behalf of the server:

Example 4.24. Rewriting IMAP capability response
self.request["CAPABILITY"] = (IMAP_REQ_RESPOND, "OK CAPABILITY completed", ("[IMAP4rev1]", ))

There are other methods to control which CAPABILITYs are known by the client. There is a separate capability hash for this, indexed by the name of the capabilities. The valid values are listed below.

ActionDescription
IMAP_CAP_ACCEPT Allow use of the capability.
IMAP_CAP_DROP Reject the capability.

Table 4.34.  Action codes for IMAP capabilities

This hash has nothing to do with capabilities known by the proxy; it defines which answers can arrive to the client for a CAPABILITY command.

Modifying the IMAP greeting string

The IMAP greeting string can be modified (rewritten) by the proxy to hide sensitive information about the server. This can be realized as a rule defined as a tuple containing the following three items:

  • The value IMAP_REQ_REWRITE;

  • a default return value (e.g.: IMAP_REQ_ACCEPT);

  • and a string.

Example 4.25. Changing the greeting string in IMAP
def config(self):
        ...
        self.response["GREETING", "UNTAGGED", "OK"] = /
        (IMAP_REQ_REWRITE, IMAP_REQ_ACCEPT, "Welcome to IMAP proxy")
        ...
IMAP states

In IMAP there are some defined states, and some commands are allowed only in certain states. On the policy level these states may be examined and modified if necessary. This can be accomplished by setting two attributes, imap_state_old and imap_state_new. The possible values for these variables are listed in the following table.

NameValue
IMAP_IS_INITIALBefore any command arrived.
IMAP_IS_NONAUTHBefore authentication.
IMAP_IS_AUTHENTICATINGAuthentication is in progress.
IMAP_IS_AUTHAuthentication performed.
IMAP_IS_SELECTEDA mailbox is selected.
IMAP_IS_QUITLogged out.

Table 4.35.  IMAP states