milter

package module
v0.0.0-...-886f4a0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2019 License: BSD-2-Clause Imports: 11 Imported by: 0

README

GoDoc

milter

A Go library for milter support heavily inspired from https://github.com/andybalholm/milter For example how to use the library see https://github.com/phalaaxx/pf-milters - postfix milter for email classification with bogofilter and blacklisting messages which contain files with executable extensions.

CHANGES

I changed the original package for better usage.

  • Better Integrating Interface
  • Callback Interfaces with Context

import "github.com/x-mod/milter"

import (
	"context"
	"net"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	"github.com/x-mod/milter"
	"github.com/x-mod/routine"
	"github.com/x-mod/tcpserver"
)

func Main(cmd *cobra.Command, args []string) error {
	srv := tcpserver.NewServer(
		tcpserver.Network(viper.GetString("inet")),
		tcpserver.Address(viper.GetString("address")),
		tcpserver.Handler(Handler),
	)
	return routine.Main(
		routine.ExecutorFunc(srv.Serve),
		routine.Interrupts(routine.DefaultCancelInterruptors...),
		routine.Cleanup(
			routine.ExecutorFunc(func(ctx context.Context) error {
				srv.Close()
				return nil
			})),
	)
}

func Handler(ctx context.Context, conn net.Conn) error {
    ssn := milter.NewMilterSession(conn, 
            milter.WithMilter(&MyMilter{}), 
            milter.WithContext(ctx))
	return ssn.Serve()
}

//MyMilter implement the Milter interface
type MyMilter struct{}

///Callback with Context

// Connect(ctx context.Context, host string, family string, port uint16, addr net.IP, m *Modifier) (Response, error)

// Helo(ctx context.Context, name string, m *Modifier) (Response, error)

Documentation

Overview

A Go library for milter support

Index

Constants

View Source
const (
	RespAccept   = SimpleResponse(accept)
	RespContinue = SimpleResponse(continue_)
	RespDiscard  = SimpleResponse(discard)
	RespReject   = SimpleResponse(reject)
	RespTempFail = SimpleResponse(tempFail)
)

Define standard responses with no data

View Source
const (
	// set which actions the milter wants to perform
	OptAddHeader    OptAction = 0x01
	OptChangeBody   OptAction = 0x02
	OptAddRcpt      OptAction = 0x04
	OptRemoveRcpt   OptAction = 0x08
	OptChangeHeader OptAction = 0x10
	OptQuarantine   OptAction = 0x20

	// mask out unwanted parts of the SMTP transaction
	OptNoConnect  OptProtocol = 0x01
	OptNoHelo     OptProtocol = 0x02
	OptNoMailFrom OptProtocol = 0x04
	OptNoRcptTo   OptProtocol = 0x08
	OptNoBody     OptProtocol = 0x10
	OptNoHeaders  OptProtocol = 0x20
	OptNoEOH      OptProtocol = 0x40
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomResponse

type CustomResponse struct {
	// contains filtered or unexported fields
}

CustomResponse is a response instance used by callback handlers to indicate how the milter should continue processing of current message

func NewResponse

func NewResponse(code byte, data []byte) *CustomResponse

NewResponse generates a new CustomResponse suitable for WritePacket

func NewResponseStr

func NewResponseStr(code byte, data string) *CustomResponse

NewResponseStr generates a new CustomResponse with string payload

func (*CustomResponse) Continue

func (c *CustomResponse) Continue() bool

Continue returns false if milter chain should be stopped, true otherwise

func (*CustomResponse) Response

func (c *CustomResponse) Response() *Message

Response returns message instance with data

type Message

type Message struct {
	Code byte
	Data []byte
}

Message represents a command sent from milter client

type Milter

type Milter interface {
	// Connect is called to provide SMTP connection data for incoming message
	//   supress with NoConnect
	Connect(ctx context.Context, host string, family string, port uint16, addr net.IP, m *Modifier) (Response, error)

	// Helo is called to process any HELO/EHLO related filters
	//   supress with NoHelo
	Helo(ctx context.Context, name string, m *Modifier) (Response, error)

	// MailFrom is called to process filters on envelope FROM address
	//   supress with NoMailForm
	MailFrom(ctx context.Context, from string, m *Modifier) (Response, error)

	// RcptTo is called to process filters on envelope TO address
	//   supress with NoRcptTo
	RcptTo(ctx context.Context, rcptTo string, m *Modifier) (Response, error)

	// Header is called once for each header in incoming message
	//   supress with NoHeaders
	Header(ctx context.Context, name string, value string, m *Modifier) (Response, error)

	// Headers is called when all message headers have been processed
	//   supress with NoHeaders
	Headers(ctx context.Context, h textproto.MIMEHeader, m *Modifier) (Response, error)

	// BodyChunk is called to process next message body chunk data (up to 64KB in size)
	//   supress with NoBody
	BodyChunk(ctx context.Context, chunk []byte, m *Modifier) (Response, error)

	// Body is called at the end of each message
	//   all changes to message's content & attributes must be done here
	Body(ctx context.Context, m *Modifier) (Response, error)
}

Milter is an interface for milter callback handlers

type MilterSession

type MilterSession struct {
	// contains filtered or unexported fields
}

MilterSession keeps session state during MTA communication

func NewMilterSession

func NewMilterSession(conn net.Conn, opts ...MilterSessionOpt) *MilterSession

func (*MilterSession) Process

func (m *MilterSession) Process(msg *Message) (Response, error)

Process processes incoming milter commands

func (*MilterSession) ReadPacket

func (c *MilterSession) ReadPacket() (*Message, error)

ReadPacket reads incoming milter packet

func (*MilterSession) Serve

func (m *MilterSession) Serve() error

Serve serving session

func (*MilterSession) WritePacket

func (m *MilterSession) WritePacket(msg *Message) error

WritePacket sends a milter response packet to socket stream

type MilterSessionOpt

type MilterSessionOpt func(*MilterSession)

func WithAction

func WithAction(action OptAction) MilterSessionOpt

func WithContext

func WithContext(ctx context.Context) MilterSessionOpt

func WithMilter

func WithMilter(milter Milter) MilterSessionOpt

func WithProtocol

func WithProtocol(proto OptProtocol) MilterSessionOpt

type Modifier

type Modifier struct {
	Macros  map[string]string
	Headers textproto.MIMEHeader
	// contains filtered or unexported fields
}

Modifier provides access to Macros, Headers and Body data to callback handlers. It also defines a number of functions that can be used by callback handlers to modify processing of the email message

func (*Modifier) AddHeader

func (m *Modifier) AddHeader(name, value string) error

AddHeader appends a new email message header the message

func (*Modifier) AddRecipient

func (m *Modifier) AddRecipient(r string) error

AddRecipient appends a new envelope recipient for current message

func (*Modifier) ChangeHeader

func (m *Modifier) ChangeHeader(index int, name, value string) error

ChangeHeader replaces the header at the specified position with a new one

func (*Modifier) DeleteRecipient

func (m *Modifier) DeleteRecipient(r string) error

DeleteRecipient removes an envelope recipient address from message

func (*Modifier) Quarantine

func (m *Modifier) Quarantine(reason string) error

Quarantine a message by giving a reason to hold it

func (*Modifier) ReplaceBody

func (m *Modifier) ReplaceBody(body []byte) error

ReplaceBody substitutes message body with provided body

type OptAction

type OptAction uint32

OptAction sets which actions the milter wants to perform. Multiple options can be set using a bitmask.

type OptProtocol

type OptProtocol uint32

OptProtocol masks out unwanted parts of the SMTP transaction. Multiple options can be set using a bitmask.

type Response

type Response interface {
	Response() *Message
	Continue() bool
}

Response represents a response structure returned by callback handlers to indicate how the milter server should proceed

type SimpleResponse

type SimpleResponse byte

SimpleResponse type to define list of pre-defined responses

func (SimpleResponse) Continue

func (r SimpleResponse) Continue() bool

Continue to process milter messages only if current code is Continue

func (SimpleResponse) Response

func (r SimpleResponse) Response() *Message

Response returns a Message object reference

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL