-
Notifications
You must be signed in to change notification settings - Fork 0
/
command-handler.go
131 lines (118 loc) · 3.33 KB
/
command-handler.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
botapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"knatofs.se/crapmail/pkg/client"
"knatofs.se/crapmail/pkg/server"
)
type commandHandler struct {
smtpClient *client.SmtpClient
spam *server.Spam
config *server.Config
bot *botapi.BotAPI
}
func getMessageJson(data interface{}) string {
configString, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "<i>Error serializing data</i>"
}
return fmt.Sprintf("<code><pre>%s</pre></code>", configString)
}
func sendConfig(bot *botapi.BotAPI, chatId int64, data interface{}) error {
m := botapi.NewMessage(chatId, getMessageJson(data))
m.ParseMode = "HTML"
_, err := bot.Send(m)
return err
}
func getValidEmailAddresses(input string) []string {
emails := strings.Split(input, " ")
var validEmails []string
for _, email := range emails {
if strings.Contains(email, "@") {
validEmails = append(validEmails, email)
}
}
return validEmails
}
func (cmd *commandHandler) findUser(chatId int64) *server.User {
for _, user := range cmd.config.Users {
if user.ChatId == chatId {
return &user
}
}
return nil
}
func getDefaultSubject(user *server.User) string {
if user.DefaultSubject != "" {
return user.DefaultSubject
}
return "Chat message"
}
func (cmd *commandHandler) OnMessage(msg *botapi.Message) error {
if msg.IsCommand() {
switch command := msg.Command(); command {
case "send":
user := cmd.findUser(msg.Chat.ID)
if user == nil {
return fmt.Errorf("user not found")
}
subject := getDefaultSubject(user)
message, err := client.ParseMessage(msg.CommandArguments(), user.Email, subject)
if err != nil {
return err
}
err = cmd.smtpClient.Send(*message)
return err
case "config":
sendConfig(cmd.bot, msg.Chat.ID, cmd.config)
case "users":
sendConfig(cmd.bot, msg.Chat.ID, cmd.config.Users)
case "add":
p := getValidEmailAddresses(msg.CommandArguments())
for _, email := range p {
log.Printf("Adding %s", email)
cmd.config.Users = append(cmd.config.Users, server.User{ChatId: msg.Chat.ID, Email: email})
}
sendConfig(cmd.bot, msg.Chat.ID, cmd.config.Users)
case "block":
ip := msg.CommandArguments()
if ip != "" {
cmd.spam.BlockedIps = append(cmd.spam.BlockedIps, ip)
m := botapi.NewMessage(msg.Chat.ID, fmt.Sprintf("Blocked ip %s", ip))
_, err := cmd.bot.Send(m)
return err
}
case "ips":
m := botapi.NewMessage(msg.Chat.ID, "Updating blocked ips...")
cmd.bot.Send(m)
if err := cmd.spam.UpdateBlockedIpsFromUrl(cmd.config.BlockedIpUrl); err != nil {
return err
}
m.Text = "Updated blocked ips"
_, err := cmd.bot.Send(m)
return err
case "words":
m := botapi.NewMessage(msg.Chat.ID, "Updating warning words...")
cmd.bot.Send(m)
if err := cmd.spam.UpdateWarningWordsFromUrl(cmd.config.WarningWordsUrl); err != nil {
return err
}
m.Text = "Updated warning words"
_, err := cmd.bot.Send(m)
return err
case "start":
m := botapi.NewMessage(msg.Chat.ID, "Hello! I got your message, id logged on the server")
log.Printf("[%s %s] %d", msg.From.FirstName, msg.From.LastName, msg.Chat.ID)
_, err := cmd.bot.Send(m)
return err
default:
log.Printf("Unknown command %s", command)
}
} else {
log.Printf("Unknown message %s", msg.Text)
}
return nil
}