-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (75 loc) · 1.81 KB
/
main.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
57
58
59
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
package main
import (
"log"
"os"
"github.com/emersion/go-smtp"
botapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"knatofs.se/crapmail/pkg/client"
"knatofs.se/crapmail/pkg/server"
)
func main() {
config, err := server.GetConfig("config.json")
if err != nil {
log.Fatal(err)
}
dkimBytes, err := client.ReadFile("dkim.pem")
if err != nil {
log.Panic(err)
}
bot, err := botapi.NewBotAPI(config.Token)
if err != nil {
log.Panic(err)
}
h := &server.SimpleHash{
Salt: config.HashSalt,
}
smtpClient, err := client.MakeSmtpClient(config.Domain, config.DkimSelector, dkimBytes)
if err != nil {
log.Fatal("Error creating smtp client", err)
}
spm := &server.Spam{
SpamWords: config.StopWords,
WarningWords: []string{},
BlockedIps: []string{},
MaxSpamCount: 5,
}
go func() {
if config.BlockedIpUrl != "" {
err := spm.UpdateBlockedIpsFromUrl(config.BlockedIpUrl)
if err != nil {
log.Fatal("Error updating blocked ips", err)
}
}
if config.WarningWordsUrl != "" {
err := spm.UpdateWarningWordsFromUrl(config.WarningWordsUrl)
if err != nil {
log.Fatal("Error updating warning words", err)
}
}
}()
s := smtp.NewServer(&server.Backend{
SpamClassifier: server.MakeAiClassifier(&config.OpenAi),
HashGenerator: h,
Bot: bot,
Config: config,
SpamChecker: spm,
})
go server.WebServer(h)
log.Printf("Bot authorized [%s]", bot.Self.UserName)
s.Addr = config.Listen
s.Domain = config.Domain
s.AllowInsecureAuth = true
if os.Getenv("DEBUG") == "true" {
bot.Debug = true
spm.Debug = true
s.Debug = os.Stdout
}
go server.ListenForMessages(bot, &commandHandler{
smtpClient: smtpClient,
spam: spm,
config: config,
bot: bot,
})
log.Printf("Starting SMTP server at %s", s.Addr)
log.Fatal(s.ListenAndServe())
}