-
Notifications
You must be signed in to change notification settings - Fork 0
/
graceless.go
219 lines (189 loc) · 5.62 KB
/
graceless.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package graceless // github.com/justanotherorganization/graceless
import (
"context"
"strings"
cmds "github.com/justanotherorganization/graceless/commands"
"github.com/justanotherorganization/graceless/config"
"github.com/justanotherorganization/graceless/internal"
"github.com/justanotherorganization/justanotherbotkit/commands"
"github.com/justanotherorganization/justanotherbotkit/proto"
"github.com/justanotherorganization/justanotherbotkit/transport"
"github.com/justanotherorganization/l5424"
"github.com/justanotherorganization/l5424/x5424"
"github.com/pkg/errors"
)
type (
// Graceless is a clumsy bot.
Graceless struct {
config *config.Config
}
)
// New creates a new Graceless instance.
func New(conf *config.Config) (*Graceless, error) {
if err := conf.Validate(); err != nil {
return nil, err
}
return &Graceless{
config: conf,
}, nil
}
// Start our Graceless bot.
func (g *Graceless) Start(ctx context.Context, cancel context.CancelFunc, errCh chan error) {
startMsg := "Starting Graceless"
if g.Safemode() {
startMsg += " in safemode"
}
g.config.Log(x5424.Severity, l5424.InfoLvl, startMsg, "\n")
// Start building our database (as early as possible).
if !g.Safemode() {
cmds.SetUserDB(g.config.UserDB)
go func() {
// Create users for our admins, anyone else will get created when they speak.
// if they don't speak, there's no reason for them to be in the database anyway.
for _, rootUser := range g.config.RootUsers {
// We wouldn't be building the database if we hadn't passed in
// a backend so treat errors in this instance as fatal.
user, err := g.config.Transport.GetUser(rootUser)
if err != nil {
errCh <- err
cancel()
return
}
_user, err := g.config.UserDB.GetUser(context.Background(), user.GetID())
if err != nil {
errCh <- err
cancel()
return
}
if _user == nil {
_user, err = g.config.UserDB.CreateUser(context.Background(), user)
if err != nil {
errCh <- err
cancel()
return
}
}
if _user.GetID() == rootUser ||
strings.EqualFold(_user.GetName(), rootUser) {
isSet := false
for _, p := range _user.GetPermissions() {
if p == "root" {
isSet = true
break
}
}
if !isSet {
// Update our transport.User.BaseUser with the contents from our database saved user.
user.BaseUser = _user.(*pb.BaseUser)
user.Permissions = append(user.Permissions, "root")
_, err := g.config.UserDB.UpdateUser(context.Background(), user)
if err != nil {
errCh <- err
cancel()
return
}
}
}
}
}()
}
eventCh := make(chan *transport.Event)
go func() {
g.config.Transport.TunnelEvents(ctx, eventCh, errCh)
}()
cmds.Register(
&commands.Command{
Use: "shutdown",
Short: "Shutdown the bot",
Hidden: true,
Perms: []string{"shutdown"},
ExecFunc: func(ev *transport.Event) error {
cancel()
return nil
},
},
// &commands.Command{
// Use: "safemode",
// Short: "toggle safemode status",
// Hidden: true,
// Perms: []string{"safemode"},
// ExecFunc: func(ev *transport.Event) error {
// // TODO:
// }
// }
)
for stop := false; !stop; {
select {
case <-ctx.Done():
stop = true
case ev := <-eventCh:
// Handle messages in their own goroutines so they don't block.
go func(ev *transport.Event) {
g.config.Log(x5424.Severity, l5424.DebugLvl, "%s (%s): %v", ev.Origin.Sender.Name, ev.Origin.Sender.ID, ev.Body)
if ev.Origin.Sender.ID == "USLACKBOT" {
return
}
user, err := g.config.Transport.GetUser(ev.Origin.Sender.ID)
if err != nil {
errCh <- err
return
}
// FIXME:
// if _user.IsBot {
// return
// }
if !g.Safemode() {
// Ensure that our user exists before operating on it, this shouldn't be an issue
// _unless_ a command comes in before the above database build has completed.
_user, err := g.config.UserDB.GetUser(context.Background(), user.GetID())
if err != nil {
errCh <- errors.Wrap(err, "config.UserDB.GetUser")
return
}
if _user == nil {
_user, err = g.config.UserDB.CreateUser(context.Background(), user)
if err != nil {
errCh <- errors.Wrap(err, "config.UserDB.CreateUser")
return
}
}
// Update our transport.User.BaseUser with the contents from our database saved user.
user.BaseUser = _user.(*pb.BaseUser)
saidHello, err := internal.SayHello(user, g.config)
if err != nil {
errCh <- errors.Wrap(err, "internal.SayHello")
// Don't return, we still want the command to get processed...
}
if saidHello {
user.Permissions = append(user.Permissions, "hello")
_, err := g.config.UserDB.UpdateUser(context.Background(), user)
if err != nil {
errCh <- errors.Wrap(err, "config.UserDB.UpdateUser")
// Don't return, we still want the command to get processed...
}
}
}
if prefix := g.config.CmdPrefix; strings.HasPrefix(ev.Body, prefix) {
ev.Body = strings.TrimSpace(strings.TrimPrefix(ev.Body, prefix))
if err := cmds.Execute(ev); err != nil {
errCh <- errors.Wrap(err, "cmds.Execute")
return
}
}
}(ev)
}
}
g.config.Log(x5424.Severity, l5424.InfoLvl, "Shutting down...\n")
g.config.Log(x5424.Severity, l5424.InfoLvl, "Goodbye\n")
}
// Safemode returns whather we are currently running in safemode.
func (g *Graceless) Safemode() (b bool) {
if g.config.Safemode {
return true
}
// This shouldn't be possible if safemode is false, but just in case...
if g.config.UserDB == nil {
return true
}
return false
}