smtpd.go 2.86 KB
Newer Older
ale's avatar
ale committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Simple SMTP server that runs lurker-index.
//
// Use the --addr-rx command-line option to extract the list name from
// the recipient address. The same regexp defines valid recipient
// addresses (messages whose recipient do not match are rejected).
//
package main

import (
	"bytes"
	"flag"
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"

	"github.com/chrj/smtpd"
)

var (
ale's avatar
ale committed
25
26
27
28
	port          = flag.Int("port", getenvInt("SMTP_PORT"), "smtp port")
	hostname      = flag.String("ehlo-hostname", os.Getenv("SMTP_EHLO_HOSTNAME"), "EHLO hostname")
	lurkerDataDir = flag.String("lurker-data-dir", getenvDefault("LURKER_DATA_DIR", "/var/lib/lurker/data"), "lurker data dir")
	addrPattern   = flag.String("addr-rx", getenvDefault("LURKER_ADDR_RX", `lurker\+([^@]+)@.*`), "recipient address regular expression")
ale's avatar
ale committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
)

var addrRx *regexp.Regexp

func getenvInt(key string) int {
	n, _ := strconv.Atoi(os.Getenv(key))
	return n
}

func getenvDefault(key, dflt string) string {
	if s := os.Getenv(key); s != "" {
		return s
	}
	return dflt
}

func isMaintenanceModeOn() bool {
ale's avatar
ale committed
46
	_, err := os.Stat(filepath.Join(*lurkerDataDir, ".tmp"))
ale's avatar
ale committed
47
48
49
50
51
52
	return !os.IsNotExist(err)
}

func lurkerIndexMsg(listName string, data []byte) error {
	cmd := exec.Command("lurker-index", "-m", "-l", listName)
	cmd.Stdin = bytes.NewReader(data)
ale's avatar
ale committed
53
54
55
56
57
58
59
	out, err := cmd.CombinedOutput()
	if err != nil {
		outStr := strings.Replace(strings.TrimSpace(string(out)), "\n", "; ", -1)
		log.Printf("lurker-index error: %v, %s", err, outStr)
		return smtpd.Error{550, outStr}
	}
	return nil
ale's avatar
ale committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
}

func parseRecipientAddr(addr string) (string, error) {
	m := addrRx.FindStringSubmatch(addr)
	if len(m) == 0 {
		return "", smtpd.Error{550, "Unknown recipient"}
	}
	listName := m[1]
	if listName == "" {
		return "", smtpd.Error{550, "No list name found"}
	}
	return listName, nil
}

func checkRecipient(peer smtpd.Peer, addr string) error {
	_, err := parseRecipientAddr(addr)
	return err
}

func handleMessage(peer smtpd.Peer, env smtpd.Envelope) error {
	if isMaintenanceModeOn() {
		return smtpd.Error{421, "Maintenance mode, retry later"}
	}

	log.Printf("received msg: %s -> %s", env.Sender, strings.Join(env.Recipients, ","))
	for _, rcpt := range env.Recipients {
		listName, err := parseRecipientAddr(rcpt)
		if err != nil {
			return err
		}
		if err := lurkerIndexMsg(listName, env.Data); err != nil {
ale's avatar
ale committed
91
			return err
ale's avatar
ale committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
		}
	}
	return nil
}

func main() {
	log.SetFlags(0)
	flag.Parse()

	// Auto-anchor the addr regexp.
	var err error
	addrRx, err = regexp.Compile(fmt.Sprintf("^%s$", *addrPattern))
	if err != nil {
		log.Fatalf("error in --addr-rx: %v", err)
	}

	server := &smtpd.Server{
		Hostname:         *hostname,
		RecipientChecker: checkRecipient,
		Handler:          handleMessage,
ale's avatar
ale committed
112
		//ProtocolLogger:   log.New(os.Stderr, "smtp: ", 0),
ale's avatar
ale committed
113
114
115
116
117
118
	}

	if err := server.ListenAndServe(fmt.Sprintf(":%d", *port)); err != nil {
		log.Fatal(err)
	}
}