pymilter  0.9.5
Public Member Functions | Private Member Functions | Private Attributes
Milter.Base Class Reference

A do "nothing" Milter base class representing an SMTP connection. More...

Inheritance diagram for Milter.Base:
Milter.Milter

List of all members.

Public Member Functions

def log
 Defined by subclasses to write log messages.
def connect
 Called for each connection to the MTA.
def hello
 Called when the SMTP client says HELO.
def envfrom
 Called when the SMTP client says MAIL FROM.
def envrcpt
 Called when the SMTP client says RCPT TO.
def data
 Called when the SMTP client says DATA.
def header
 Called for each header field in the message body.
def eoh
 Called at the blank line that terminates the header fields.
def body
 Called to supply the body of the message to the Milter by chunks.
def unknown
 Called when the SMTP client issues an unknown command.
def eom
 Called at the end of the message body.
def abort
 Called when the connection is abnormally terminated.
def close
 Called when the connection is closed.
def protocol_mask
 Return mask of SMFIP_N.
def negotiate
 Negotiate milter protocol options.
def getsymval
 Return the value of an MTA macro.
def setreply
 Set the SMTP reply code and message.
def setsmlist
 Tell the MTA which macro names will be used.
def addheader
 Add a mail header field.
def chgheader
 Change the value of a mail header field.
def addrcpt
 Add a recipient to the message.
def delrcpt
 Delete a recipient from the message.
def replacebody
 Replace the message body.
def chgfrom
 Change the SMTP envelope sender address.
def quarantine
 Quarantine the message.
def progress
 Tell the MTA to wait a bit longer.

Private Member Functions

def _setctx
 Attach this Milter to the low level milter.milterContext object.

Private Attributes

 _ctx
 The low level milter.milterContext object.
 _actions
 A bitmask of actions this connection has negotiated to use.
 _protocol
 A bitmask of protocol options this connection has negotiated.

Detailed Description

A do "nothing" Milter base class representing an SMTP connection.

Python milters should derive from this class unless they are using the low level milter module directly.

Most of the methods are either "actions" or "callbacks". Callbacks are invoked by the MTA at certain points in the SMTP protocol. For instance when the HELO command is seen, the MTA calls the helo callback before returning a response code. All callbacks must return one of these constants: CONTINUE, TEMPFAIL, REJECT, ACCEPT, DISCARD, SKIP. The NOREPLY response is supplied automatically by the @noreply decorator if negotiation with the MTA is successful. @noreply and @nocallback methods should return CONTINUE for two reasons: the MTA may not support negotiation, and the class may be running in a test harness.

Optional callbacks are disabled with the @nocallback decorator, and automatically reenabled when overridden. Disabled callbacks should still return CONTINUE for testing and MTAs that do not support negotiation.


Member Function Documentation

def Milter.Base._setctx (   self,
  ctx 
) [private]

Attach this Milter to the low level milter.milterContext object.

def Milter.Base.abort (   self)

Called when the connection is abnormally terminated.

The close callback is still called also.

Reimplemented in Milter.Milter.

def Milter.Base.addheader (   self,
  field,
  value,
  idx = -1 
)

Add a mail header field.

Calls smfi_addheader. The Milter.ADDHDRS action flag must be set.

May be called from eom callback only.

Parameters:
fieldthe header field name
valuethe header field value
idxheader field index from the top of the message to insert at
Exceptions:
DisabledActionif ADDHDRS is not enabled
def Milter.Base.addrcpt (   self,
  rcpt,
  params = None 
)

Add a recipient to the message.

Calls smfi_addrcpt. If no corresponding mail header is added, this is like a Bcc. The syntax of the recipient is the same as used in the SMTP RCPT TO command (and as delivered to the envrcpt callback), for example "self.addrcpt('<foo@example.com>')". The Milter.ADDRCPT action flag must be set. If the optional params argument is used, then the Milter.ADDRCPT_PAR action flag must be set.

May be called from eom callback only.

Parameters:
rcptthe message recipient
paramsan optional list of ESMTP parameters
Exceptions:
DisabledActionif ADDRCPT or ADDRCPT_PAR is not enabled
def Milter.Base.body (   self,
  blk 
)

Called to supply the body of the message to the Milter by chunks.

Parameters:
blka block of message bytes
def Milter.Base.chgfrom (   self,
  sender,
  params = None 
)

Change the SMTP envelope sender address.

Calls smfi_chgfrom. The syntax of the sender is that same as used in the SMTP MAIL FROM command (and as delivered to the envfrom callback), for example self.chgfrom('<bar@example.com>'). The Milter.CHGFROM action flag must be set.

May be called from eom callback only.

Since:
0.9.1
Parameters:
senderthe new sender address
paramsan optional list of ESMTP parameters
Exceptions:
DisabledActionif CHGFROM is not enabled
def Milter.Base.chgheader (   self,
  field,
  idx,
  value 
)

Change the value of a mail header field.

Calls smfi_chgheader. The Milter.CHGHDRS action flag must be set.

May be called from eom callback only.

Parameters:
fieldthe name of the field to change
idxindex of the field to change when there are multiple instances
valuethe new value of the field
Exceptions:
DisabledActionif CHGHDRS is not enabled
def Milter.Base.close (   self)

Called when the connection is closed.

Reimplemented in Milter.Milter.

def Milter.Base.connect (   self,
  hostname,
  family,
  hostaddr 
)

Called for each connection to the MTA.

The hostname provided by the local MTA is either the PTR name or the IP in the form "[1.2.3.4]" if no PTR is available. The format of hostaddr depends on the socket family:

socket.AF_INET
A tuple of (IP as string in dotted quad form, integer port)
socket.AF_INET6
A tuple of (IP as a string in standard representation, integer port, integer flow info, integer scope id)
socket.AF_UNIX
A string with the socketname
Parameters:
hostnamethe PTR name or bracketed IP of the SMTP client
familysocket.AF_INET, socket.AF_INET6, or socket.AF_UNIX
hostaddra tuple or string with peer IP or socketname

Reimplemented in Milter.Milter.

def Milter.Base.data (   self)

Called when the SMTP client says DATA.

Returning REJECT rejects the message without wasting bandwidth on the unwanted message.

Since:
0.9.2
def Milter.Base.delrcpt (   self,
  rcpt 
)

Delete a recipient from the message.

Calls smfi_delrcpt. The recipient should match one passed to the envrcpt callback. The Milter.DELRCPT action flag must be set.

May be called from eom callback only.

Parameters:
rcptthe message recipient to delete
Exceptions:
DisabledActionif DELRCPT is not enabled
def Milter.Base.envfrom (   self,
  f,
  str 
)

Called when the SMTP client says MAIL FROM.

Returning REJECT rejects the message, but not the connection.

Reimplemented in Milter.Milter.

def Milter.Base.envrcpt (   self,
  to,
  str 
)

Called when the SMTP client says RCPT TO.

Returning REJECT rejects the current recipient, not the entire message.

Reimplemented in Milter.Milter.

def Milter.Base.eoh (   self)

Called at the blank line that terminates the header fields.

Reimplemented in Milter.Milter.

def Milter.Base.eom (   self)

Called at the end of the message body.

Most of the message manipulation actions can only take place from the eom callback.

Reimplemented in Milter.Milter.

def Milter.Base.getsymval (   self,
  sym 
)

Return the value of an MTA macro.

Sendmail macro names are either single chars (e.g. "j") or multiple chars enclosed in braces (e.g. "{auth_type}"). Macro names are MTA dependent.

Parameters:
symthe macro name
def Milter.Base.header (   self,
  field,
  value 
)

Called for each header field in the message body.

Reimplemented in Milter.Milter.

def Milter.Base.hello (   self,
  hostname 
)

Called when the SMTP client says HELO.

Returning REJECT prevents progress until a valid HELO is provided; this almost always results in terminating the connection.

Reimplemented in Milter.Milter.

def Milter.Base.log (   self,
  msg 
)

Defined by subclasses to write log messages.

Reimplemented in Milter.Milter.

def Milter.Base.negotiate (   self,
  opts 
)

Negotiate milter protocol options.

Called by the xffi_negotiate callback. Options are passed as a list of 4 32-bit ints which can be modified and are passed back to libmilter on return. Default negotiation sets P_NO* and P_NR* for callbacks marked @nocallback and @noreply respectively, leaves all actions enabled, and enables Milter.SKIP. The @enable_protocols class decorator can customize which protocol steps are implemented.

Parameters:
optsa modifiable list of 4 ints with negotiated options
Since:
0.9.2
def Milter.Base.progress (   self)

Tell the MTA to wait a bit longer.

Calls smfi_progress. Resets timeouts in the MTA that detect a "hung" milter.

def Milter.Base.protocol_mask (   klass)

Return mask of SMFIP_N.

. protocol option bits to clear for this class The @nocallback and @noreply decorators set the milter_protocol function attribute to the protocol mask bit to pass to libmilter, causing that callback or its reply to be skipped. Overriding a method creates a new function object, so that milter_protocol defaults to 0. Libmilter passes the protocol bits that the current MTA knows how to skip. We clear the ones we don't want to skip. The negation is somewhat mind bending, but it is simple.

Since:
0.9.2
def Milter.Base.quarantine (   self,
  reason 
)

Quarantine the message.

Calls smfi_quarantine. When quarantined, a message goes into the mailq as if to be delivered, but delivery is deferred until the message is unquarantined. The Milter.QUARANTINE action flag must be set.

May be called from eom callback only.

Parameters:
reasona string describing the reason for quarantine
Exceptions:
DisabledActionif QUARANTINE is not enabled
def Milter.Base.replacebody (   self,
  body 
)

Replace the message body.

Calls smfi_replacebody. The entire message body must be replaced. Call repeatedly with blocks of data until the entire body is transferred. The Milter.MODBODY action flag must be set.

May be called from eom callback only.

Parameters:
bodya chunk of body data
Exceptions:
DisabledActionif MODBODY is not enabled
def Milter.Base.setreply (   self,
  rcode,
  xcode = None,
  msg = None,
  ml 
)

Set the SMTP reply code and message.

If the MTA does not support setmlreply, then only the first msg line is used.

def Milter.Base.setsmlist (   self,
  stage,
  macros 
)

Tell the MTA which macro names will be used.

The Milter.SETSMLIST action flag must be set.

May only be called from negotiate callback.

Since:
0.9.2
Parameters:
stagethe protocol stage to set to macro list for
macrosa string with a space delimited list of macros
def Milter.Base.unknown (   self,
  cmd 
)

Called when the SMTP client issues an unknown command.

Parameters:
cmdthe unknown command
Since:
0.9.2

Member Data Documentation

A bitmask of actions this connection has negotiated to use.

By default, all actions are enabled. High throughput milters may want to disable unused actions to increase efficiency. Some optional actions may be disabled by calling milter.set_flags(), or by overriding the negotiate callback. The bits include: ADDHDRS,CHGBODY,MODBODY,ADDRCPT,ADDRCPT_PAR,DELRCPT CHGHDRS,QUARANTINE,CHGFROM,SETSMLIST. The Milter.CURR_ACTS bitmask is all actions known when the milter module was compiled. Application code can also inspect this field to determine which actions are available. This is especially useful in generic library code designed to work in multiple milters.

Since:
0.9.2
Milter.Base::_ctx [private]

The low level milter.milterContext object.

A bitmask of protocol options this connection has negotiated.

An application may inspect this variable to determine which protocol steps are supported. Options of interest to applications: the SKIP result code is allowed only if the P_SKIP bit is set, rejected recipients are passed to the milter application only if the P_RCPT_REJ bit is set, and header values are sent and received with leading spaces (in the continuation lines) intact if the P_HDR_LEADSPC bit is set (so that the application can customize indenting).

The P_N* bits should be negotiated via the @noreply and @nocallback method decorators, and P_RCPT_REJ, P_HDR_LEADSPC should be enabled using the enable_protocols class decorator.

The bits include: P_RCPT_REJ P_NR_CONN P_NR_HELO P_NR_MAIL P_NR_RCPT P_NR_DATA P_NR_UNKN P_NR_EOH P_NR_BODY P_NR_HDR P_NOCONNECT P_NOHELO P_NOMAIL P_NORCPT P_NODATA P_NOUNKNOWN P_NOEOH P_NOBODY P_NOHDRS P_HDR_LEADSPC P_SKIP (all under the Milter namespace).

Since:
0.9.2

The documentation for this class was generated from the following file: