forked from emersion/go-milter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_server_test.go
56 lines (47 loc) · 1.57 KB
/
example_server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package milter_test
import (
"log"
"net"
"sync"
"github.com/d--j/go-milter"
)
type ExampleBackend struct {
milter.NoOpMilter
}
func (b *ExampleBackend) RcptTo(rcptTo string, esmtpArgs string, m *milter.Modifier) (*milter.Response, error) {
// reject the mail when it goes to other-spammer@example.com and is a local delivery
if rcptTo == "other-spammer@example.com" && m.Macros.Get(milter.MacroRcptMailer) == "local" {
return milter.RejectWithCodeAndReason(550, "We do not like you\r\nvery much, please go away")
}
return milter.RespContinue, nil
}
func ExampleServer() {
// create socket to listen on
socket, err := net.Listen("tcp4", "127.0.0.1:6785")
if err != nil {
log.Fatal(err)
}
defer socket.Close()
// define the backend, required actions, protocol options and macros we want
server := milter.NewServer(
milter.WithMilter(func() milter.Milter {
return &ExampleBackend{}
}),
milter.WithProtocol(milter.OptNoConnect|milter.OptNoHelo|milter.OptNoMailFrom|milter.OptNoBody|milter.OptNoHeaders|milter.OptNoEOH|milter.OptNoUnknown|milter.OptNoData),
milter.WithAction(milter.OptChangeFrom|milter.OptAddRcpt|milter.OptRemoveRcpt),
milter.WithMacroRequest(milter.StageRcpt, []milter.MacroName{milter.MacroRcptMailer}),
)
defer server.Close()
// start the milter
var wgDone sync.WaitGroup
wgDone.Add(1)
go func(socket net.Listener) {
if err := server.Serve(socket); err != nil {
log.Fatal(err)
}
wgDone.Done()
}(socket)
log.Printf("Started milter on %s:%s", socket.Addr().Network(), socket.Addr().String())
// quit when milter quits
wgDone.Wait()
}