-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (50 loc) · 1.04 KB
/
main.go
File metadata and controls
60 lines (50 loc) · 1.04 KB
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
57
58
59
60
package main
import (
"flag"
"fmt"
"github.com/chrj/smtpd"
"net"
"os"
)
var (
listenAddr string
hookDir string
)
func init() {
flag.StringVar(&listenAddr, "listen", ":25", "address to listen to")
flag.StringVar(&hookDir, "hook-dir", "", "path to look for hook files")
}
func main() {
flag.Parse()
if len(hookDir) == 0 {
fmt.Println("a hook directory (-hook-dir) is required")
os.Exit(1)
}
hooks, err := NewHookDir(hookDir).Hooks()
if err != nil {
fmt.Println("could not read hook file:", err)
os.Exit(1)
}
hostname, err := os.Hostname()
if err != nil {
fmt.Println("could not get hostname:", err)
os.Exit(1)
}
l, err := net.Listen(netOfAddr(listenAddr), listenAddr)
if err != nil {
fmt.Println("could not setup listener:", err)
os.Exit(1)
}
defer l.Close()
smtp := &smtpd.Server{
Hostname: hostname,
WelcomeMessage: fmt.Sprintf("%s ESMTP mailhook", hostname),
}
NewServer(smtp, hooks).Serve(l)
}
func netOfAddr(addr string) string {
if len(addr) > 0 && addr[0] == '/' {
return "unix"
}
return "tcp"
}