From 8746179952cdb00594b99f160e8e26d7ce45ae07 Mon Sep 17 00:00:00 2001 From: Leon Baker Date: Mon, 15 Jun 2015 16:44:41 +0200 Subject: [PATCH] Update README and fix sample filter --- .gitignore | 3 ++ README.md | 95 ++++++++++++++++++++++++++++++++++++++++++++++++- samplefilter.go | 3 +- 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d4efd9b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# Compiled test programs +samplefilter +samplefilter2 diff --git a/README.md b/README.md index 6e0897e..29b422a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,95 @@ # gomilter -Go Bindings for libmilter +Go Bindings for Sendmail's libmilter + +Tested on Linux and FreeBSD + +## Installation + +The Sendmail header file libmilter/mfapi.h is required. For Redhat/CentOS, install the sendmail-devel package: +```sh +yum install sendmail-devel +``` + +Install the gomilter package: + +```sh +go get github.com/leonrbaker/gomilter +``` + +##Usage + +The milter is implemented in a struct. Start by defining your own struct type and embeding the gomilter MilterRaw struct. + +```go +type Mymilter struct { + gomilter.MilterRaw // Embed the basic functionality. +} +``` + +Milter callbacks are added by implementing methods for the struct with matching predefined names. + +### Callbacks + +* Connect +* Helo +* EnvFrom +* EnvRcpt +* Header +* Eoh +* Body +* Eom +* Abort +* Close + +Not all the callbacks need to be defined. The callbacks are explained on the milter.org site. Unfortunately the milter.org site has been shut down but it is still on [web.archive.org](http://web.archive.org/web/20150510034154/https://www.milter.org/developers/api/index) + +### Message Modification Functions + +* AddHeader +* ChgHeader +* InsHeader +* ChgFrom +* AddRcpt +* AddRcpt_Par +* DelRcpt +* ReplaceBody + +### Other Message Handling Functions +* progress + +### Startup + +The Socket field of the milter struct must be set. For example: +```go +mymilter.Socket = "unix:/var/gomilter/socket" +``` + +Control is handed over to the libmilter smfi_main function by calling the Run method and passing it a pointer to your milter struct +```go +gomilter.Run(mymilter) +``` + +The milter has a Stop method which calls the libmilter smfi_stop function. + +### Private Data + +libmilter is able to store private data for a connection. This data can be accessed from other functions and callbacks for the same connection. You can pass a pointer to any data structure to SetPriv. The data is retrieved with GetPriv +```go +t := T{1, 2, 3} +m.SetPriv(ctx, &t) +``` +Retrieve the data with +```go +var t T +m.GetPriv(ctx, &t)) +``` + +GetPriv should only be called once. If the private data is needed in another function or callback then call SetPriv again. + +## Sample Programs +There are two sample programs included, samplefilter.go and samplefilter2.go + +##Other Libraries + +A usefull MIME parsing library is [go.enmime](https://github.com/jhillyerd/go.enmime) + diff --git a/samplefilter.go b/samplefilter.go index 0b28bd5..f0dcd40 100644 --- a/samplefilter.go +++ b/samplefilter.go @@ -40,7 +40,8 @@ func main() { mymilter := new(Mymilter) mymilter.FilterName = "TestFilter" mymilter.Debug = true - mymilter.Flags = gomilter.AddHdrs | gomilter.AddRcpt + mymilter.Flags = gomilter.ADDHDRS | gomilter.ADDRCPT + mymilter.Socket = "unix:/var/milterattachcheck/socket" // Start Milter gomilter.Run(mymilter)