pymilter  0.9.8
Classes | Functions | Variables
milter Namespace Reference

A thin wrapper around libmilter. More...

Classes

class  error
 
class  milterContext
 Hold context for a milter connection. More...
 

Functions

def set_flags (flags)
 Enable optional milter actions. More...
 
def set_connect_callback (cb)
 
def set_helo_callback (cb)
 
def set_envfrom_callback (cb)
 
def set_envrcpt_callback (cb)
 
def set_header_callback (cb)
 
def set_eoh_callback (cb)
 
def set_body_callback (cb)
 
def set_abort_callback (cb)
 
def set_close_callback (cb)
 
def set_exception_policy (code)
 Sets the return code for untrapped Python exceptions during a callback. More...
 
def register
 Register python milter with libmilter. More...
 
def opensocket (rmsock)
 Attempt to create the socket used to communicate with the MTA. More...
 
def main ()
 Transfer control to libmilter. More...
 
def setdbg (lev)
 Set the libmilter debugging level. More...
 
def settimeout (secs)
 Set timeout for MTA communication. More...
 
def setbacklog (n)
 Set socket backlog. More...
 
def setconn (s)
 Set the socket used to communicate with the MTA. More...
 
def stop ()
 Stop the milter gracefully. More...
 
def getdiag ()
 Retrieve diagnostic info. More...
 
def getversion ()
 Retrieve the runtime libmilter version. More...
 

Variables

int CONTINUE = 0
 Continue processing the current connection, message, or recipient. More...
 
int REJECT = 1
 For a connection-oriented routine, reject this connection; call Milter.Base.close(). More...
 
int DISCARD = 2
 For a message- or recipient-oriented routine, accept this message, but silently discard it. More...
 
int ACCEPT = 3
 For a connection-oriented routine, accept this connection without further filter processing; call Milter.Base.close(). More...
 
int TEMPFAIL = 4
 Return a temporary failure, i.e., the corresponding SMTP command will return an appropriate 4xx status code. More...
 
int SKIP = 5
 Skip further callbacks of the same type in this transaction. More...
 
int NOREPLY = 6
 Do not send a reply back to the MTA. More...
 
int VERSION = 0x1000001
 The compile time libmilter version. More...
 

Detailed Description

A thin wrapper around libmilter.

Most users will not import milter directly, but will instead import Milter and subclass Milter.Base. This module gives you ultimate low level control from python.

Function Documentation

def milter.getdiag ( )

Retrieve diagnostic info.

Return a tuple with diagnostic info gathered by the milter module. The first two fields are counts of milterContext objects created and deleted. Additional fields may be added later.

Returns
a tuple of diagnostic data
def milter.getversion ( )

Retrieve the runtime libmilter version.

Return the runtime libmilter version. This can be different from the compile time version when sendmail or libmilter is upgraded after pymilter is compiled.

Returns
a tuple of (major,minor,patchlevel)

Referenced by Milter.runmilter().

def milter.main ( )

Transfer control to libmilter.

Calls smfi_main.

Referenced by Milter.runmilter().

def milter.opensocket (   rmsock)

Attempt to create the socket used to communicate with the MTA.

milter.opensocket() attempts to create the socket specified previously by a call to milter.setconn() which will be the interface between MTAs and the milter. This allows the calling application to ensure that the socket can be created. If this is not called, milter.main() will do so implicitly. Calls smfi_opensocket. While not documented for libmilter, my experiments indicate that you must call register() before calling opensocket().

Parameters
rmsockTry to remove an existing unix domain socket if true.

Referenced by Milter.runmilter().

def milter.register (   name,
  negotiate = None,
  unknown = None,
  data = None 
)

Register python milter with libmilter.

The name we pass is used to identify the milter in the MTA configuration. Callback functions must be set using the set_*_callback() functions before registering the milter. Three additional callbacks are specified as keyword parameters. These were added by recent versions of libmilter. The keyword parameters is a nicer way to do it, I think, since it makes clear that you have to do it before registering. I may move all the callbacks in the future (perhaps keeping the set functions for compatibility). Note that Milter.Base automatically maps all callbacks to member functions, and negotiates which member functions are actually overridden by an application class.

Parameters
namethe milter name by which the MTA finds us
negotiatethe xxfi_negotiate callback, called to negotiate supported actions, callbacks, and protocol steps.
unknownthe xxfi_unknown callback, called when for SMTP commands not recognized by the MTA. (Extend SMTP in your milter!)
datathe xxfi_data callback, called when the DATA SMTP command is received.

Referenced by Milter.runmilter().

def milter.set_exception_policy (   code)

Sets the return code for untrapped Python exceptions during a callback.

The default is TEMPFAIL. You should not depend on this handler. Your application should have its own top level exception handler for each callback. You can then choose your own reply message, log the stack track were you please, and so on. However, if you miss one, this last ditch handler will print a standard stack trace to sys.stderr, and return to sendmail.

Parameters
codeone of TEMPFAIL,REJECT,CONTINUE, or since 1.0, ACCEPT
def milter.set_flags (   flags)

Enable optional milter actions.

Certain milter actions need to be enabled before calling main() or they throw an exception. Pymilter enables them all by default (since 0.9.2), but you may wish to disable unneeded actions as an optimization.

Parameters
flagsBit or mask of optional actions to enable
def milter.setbacklog (   n)

Set socket backlog.

Calls smfi_setbacklog. Must be called before calling main().

def milter.setconn (   s)

Set the socket used to communicate with the MTA.

The MTA can communicate with the milter by means of a unix, inet, or inet6 socket. By default, a unix domain socket is used. It must not exist, and sendmail will throw warnings if, eg, the file is under a group or world writable directory. milter.setconn() will not fail with an invalid socket - this will be detected only when calling milter.main() or milter.opensocket().

Parameters
sthe socket address in proto:address format
 milter.setconn('unix:/var/run/pythonfilter')  # a named pipe
 milter.setconn('local:/var/run/pythonfilter') # a named pipe
 milter.setconn('inet:8800')                    # listen on ANY interface
 milter.setconn('inet:7871@publichost')        # listen on a specific interface
 milter.setconn('inet6:8020')
 milter.setconn('inet6:8020@[2001:db8:1234::1]')      # listen on specific IP
 

Referenced by Milter.runmilter().

def milter.setdbg (   lev)

Set the libmilter debugging level.

smfi_setdbg sets the milter library's internal debugging level to a new level so that code details may be traced. A level of zero turns off debugging. The greater (more positive) the level the more detailed the debugging. Six is the current, highest, useful value. Must be called before calling main().

def milter.settimeout (   secs)

Set timeout for MTA communication.

Calls smfi_settimeout. Must be called before calling main().

Referenced by Milter.runmilter().

def milter.stop ( )

Stop the milter gracefully.

Variable Documentation

int milter.ACCEPT = 3

For a connection-oriented routine, accept this connection without further filter processing; call Milter.Base.close().

For a message- or recipient-oriented routine, accept this message without further filtering.

int milter.CONTINUE = 0

Continue processing the current connection, message, or recipient.

int milter.DISCARD = 2

For a message- or recipient-oriented routine, accept this message, but silently discard it.

SMFIS_DISCARD should not be returned by a connection-oriented routine.

int milter.NOREPLY = 6

Do not send a reply back to the MTA.

The milter must negotiate this behavior with the MTA, i.e., it must check whether the appropriate protocol action P_NR_* is available and if so, the milter must request it. If you set the P_NR_* protocol action for a callback, that callback must always reply with NOREPLY. Using any other reply code is a violation of the API. If in some cases your callback may return another value (e.g., due to some resource shortages), then you must not set P_NR_* and you must use CONTINUE as the default return code. (Alternatively you can try to delay reporting the problem to a later callback for which P_NR_* is not set.)

This is negotiated and returned automatically by the Milter.noreply function decorator.

int milter.REJECT = 1

For a connection-oriented routine, reject this connection; call Milter.Base.close().

For a message-oriented routine, except Milter.Base.eom() or Milter.Base.abort(), reject this message. For a recipient-oriented routine, reject the current recipient (but continue processing the current message).

int milter.SKIP = 5

Skip further callbacks of the same type in this transaction.

Currently this return value is only allowed in Milter.Base.body(). It can be used if a milter has received sufficiently many body chunks to make a decision, but still wants to invoke message modification functions that are only allowed to be called from Milter.Base.eom(). Note: the milter must negotiate this behavior with the MTA, i.e., it must check whether the protocol action SMFIP_SKIP is available and if so, the milter must request it.

int milter.TEMPFAIL = 4

Return a temporary failure, i.e., the corresponding SMTP command will return an appropriate 4xx status code.

For a message-oriented routine, except Milter.Base.envfrom(), fail for this message. For a connection-oriented routine, fail for this connection; call Milter.Base.close(). For a recipient-oriented routine, only fail for the current recipient; continue message processing.

int milter.VERSION = 0x1000001

The compile time libmilter version.

Python code might need to deal with pymilter compiled against various versions of libmilter. This module constant contains the contents of the SMFI_VERSION macro when the milter module was compiled.