-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
132 lines (119 loc) · 2.92 KB
/
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
132
package main
import (
"fmt"
"log"
"strings"
"sariego.dev/notifier-bot/base"
"sariego.dev/notifier-bot/services/feedback"
"sariego.dev/notifier-bot/services/identity"
"sariego.dev/notifier-bot/services/meet"
"sariego.dev/notifier-bot/services/topics"
)
type pkgHandler struct {
client base.Client
}
func (h *pkgHandler) Handle(pkg base.Package) error {
// ignore own messages
if pkg.Author == h.client.BotID() {
return nil
}
split := strings.Split(pkg.Message, " ")
// handle @<BOTNAME> <CMD> <ARGS>...
if split[0] == "@"+NAME {
log.Println("match: detected @ format, switching")
split = split[1:]
if split[0][0] != '!' {
split[0] = "!" + split[0]
}
}
// handle !<CMD> <ARGS>...
if split[0][0] == '!' {
log.Println("match: detected ! format")
cmd := split[0][1:]
msg, err := execute(instruction{h.client, pkg, cmd, split[1:]})
if err != nil {
return err
}
if len(msg) > 0 {
log.Printf("exec: %v@%v\n", cmd, pkg.Channel)
output := base.Package{
Channel: pkg.Channel,
Message: msg,
}
h.client.Send(output)
}
} else if hasMentionsSupport(h.client) {
// notify mentions
identity.Driver{Client: h.client}.NotifyMentions(pkg)
// notify subscriptions
topics.Driver{Client: h.client}.NotifySubscriptions(pkg)
}
return nil
}
type instruction struct {
client base.Client
pkg base.Package
cmd string
args []string
}
// todo markdown responses
func execute(parsed instruction) (response string, err error) {
if len(parsed.args) == 0 {
parsed.args = append(parsed.args, "")
}
switch parsed.cmd {
case "ping":
response = "pong!"
case "register", "add":
response, err = identity.Driver{Client: parsed.client}.
Register(
parsed.args[0],
parsed.pkg.Author,
parsed.pkg.Channel,
)
case "deregister", "remove", "delete":
response, err = identity.Deregister(
parsed.args[0],
parsed.pkg.Author,
)
case "whoami":
response, err = identity.WhoAmI(parsed.pkg.Author)
case "whoishere":
response, err = identity.Driver{Client: parsed.client}.
WhoIsHere(parsed.pkg.Channel)
case "subscribe", "sub":
response, err = topics.Driver{Client: parsed.client}.
Subscribe(
parsed.args[0],
parsed.pkg.Author,
parsed.pkg.Channel,
)
case "unsubscribe", "unsub":
response, err = topics.Unsubscribe(
parsed.args[0],
parsed.pkg.Author,
)
case "subscriptions", "subs", "mysubs":
response, err = topics.Subscriptions(parsed.pkg.Author)
case "meet":
response = meet.Driver{Client: parsed.client}.
NewMeeting(parsed.pkg.Author, parsed.args)
case "feedback", "bug":
response, err = feedback.Create(
parsed.pkg.Author,
parsed.pkg.Channel,
parsed.cmd,
strings.Join(parsed.args, " "),
)
case "registry":
response = registry
case "help":
response = help
case "version":
response = fmt.Sprintf("notifier-bot %v", VERSION)
}
return
}
func hasMentionsSupport(c base.Client) bool {
return len(c.ChannelURLTemplate()) > 0
}