Exemplo n.º 1
0
def runmilter(name, socketname, timeout=0, rmsock=True):

    # The default flags set include everything
    # milter.set_flags(milter.ADDHDRS)
    milter.set_connect_callback(connect_callback)
    milter.set_helo_callback(lambda ctx, host: ctx.getpriv().hello(host))
    # For envfrom and envrcpt, we would like to convert ESMTP parms to keyword
    # parms, but then all existing users would have to include **kw to accept
    # arbitrary keywords without crashing.  We do provide envcallback and
    # dictfromlist to make parsing the ESMTP args convenient.
    if sys.version < '3.0.0':
        milter.set_envfrom_callback(lambda ctx, *s: ctx.getpriv().envfrom(*s))
        milter.set_envrcpt_callback(lambda ctx, *s: ctx.getpriv().envrcpt(*s))
        milter.set_header_callback(
            lambda ctx, f, v: ctx.getpriv().header(f, v))
    else:
        milter.set_envfrom_callback(
            lambda ctx, *b: ctx.getpriv().envfrom_bytes(*b))
        milter.set_envrcpt_callback(
            lambda ctx, *b: ctx.getpriv().envrcpt_bytes(*b))
        milter.set_header_callback(
            lambda ctx, f, v: ctx.getpriv().header_bytes(f, v))
    milter.set_eoh_callback(lambda ctx: ctx.getpriv().eoh())
    milter.set_body_callback(lambda ctx, chunk: ctx.getpriv().body(chunk))
    milter.set_eom_callback(lambda ctx: ctx.getpriv().eom())
    milter.set_abort_callback(lambda ctx: ctx.getpriv().abort())
    milter.set_close_callback(close_callback)

    milter.setconn(socketname)
    if timeout > 0: milter.settimeout(timeout)
    # disable negotiate callback if runtime version < (1,0,1)
    ncb = negotiate_callback
    if milter.getversion() < (1, 0, 1):
        ncb = None
    # The name *must* match the X line in sendmail.cf (supposedly)
    milter.register(name,
                    data=lambda ctx: ctx.getpriv().data(),
                    unknown=lambda ctx, cmd: ctx.getpriv().unknown(cmd),
                    negotiate=ncb)

    # We remove the socket here by default on the assumption that you will be
    # starting this filter before sendmail.  If sendmail is not running and the
    # socket already exists, libmilter will throw a warning.  If sendmail is
    # running, this is still safe if there are no messages currently being
    # processed.  It's safer to shutdown sendmail, kill the filter process,
    # restart the filter, and then restart sendmail.
    milter.opensocket(rmsock)
    start_seq = _seq
    try:
        milter.main()
    except milter.error:
        if start_seq == _seq: raise  # couldn't start
        # milter has been running for a while, but now it can't start new threads
        raise milter.error("out of thread resources")
Exemplo n.º 2
0
def runmilter(name,socketname,timeout = 0,rmsock=True):

  # The default flags set include everything
  # milter.set_flags(milter.ADDHDRS)
  milter.set_connect_callback(connect_callback)
  milter.set_helo_callback(lambda ctx, host: ctx.getpriv().hello(host))
  # For envfrom and envrcpt, we would like to convert ESMTP parms to keyword
  # parms, but then all existing users would have to include **kw to accept
  # arbitrary keywords without crashing.  We do provide envcallback and
  # dictfromlist to make parsing the ESMTP args convenient.
  milter.set_envfrom_callback(lambda ctx,*str: ctx.getpriv().envfrom(*str))
  milter.set_envrcpt_callback(lambda ctx,*str: ctx.getpriv().envrcpt(*str))
  milter.set_header_callback(lambda ctx,fld,val: ctx.getpriv().header(fld,val))
  milter.set_eoh_callback(lambda ctx: ctx.getpriv().eoh())
  milter.set_body_callback(lambda ctx,chunk: ctx.getpriv().body(chunk))
  milter.set_eom_callback(lambda ctx: ctx.getpriv().eom())
  milter.set_abort_callback(lambda ctx: ctx.getpriv().abort())
  milter.set_close_callback(close_callback)

  milter.setconn(socketname)
  if timeout > 0: milter.settimeout(timeout)
  # disable negotiate callback if runtime version < (1,0,1)
  ncb = negotiate_callback
  if milter.getversion() < (1,0,1):
    ncb = None
  # The name *must* match the X line in sendmail.cf (supposedly)
  milter.register(name,
        data=lambda ctx: ctx.getpriv().data(),
        unknown=lambda ctx,cmd: ctx.getpriv().unknown(cmd),
        negotiate=ncb
  )

  # We remove the socket here by default on the assumption that you will be
  # starting this filter before sendmail.  If sendmail is not running and the
  # socket already exists, libmilter will throw a warning.  If sendmail is
  # running, this is still safe if there are no messages currently being
  # processed.  It's safer to shutdown sendmail, kill the filter process,
  # restart the filter, and then restart sendmail.
  milter.opensocket(rmsock)
  start_seq = _seq
  try:
    milter.main()
  except milter.error:
    if start_seq == _seq: raise	# couldn't start
    # milter has been running for a while, but now it can't start new threads
    raise milter.error("out of thread resources")