forked from zephyrtronium/robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
410 lines (392 loc) · 10.9 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
Copyright (C) 2020 Branden J Brown
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// robot is an advanced Markov chain bot that operates on Twitch IRC.
package main
import (
"bufio"
"context"
"crypto/tls"
"errors"
"flag"
"io/ioutil"
"log"
"net"
"os"
"os/signal"
"runtime"
"strings"
"sync"
"time"
"golang.org/x/time/rate"
"github.com/AbsyntheSyne/robot/brain"
"github.com/AbsyntheSyne/robot/commands"
"github.com/AbsyntheSyne/robot/dash"
"github.com/AbsyntheSyne/robot/irc"
)
const copying = `
robot Copyright (C) 2020 Branden J Brown
This program comes with ABSOLUTELY NO WARRANTY; for details type 'warranty'.
This is free software, and you are welcome to redistribute it
under certain conditions; see the GNU General Public License, Version 3,
for details.
`
func main() {
var source, remote, token, dsh string
var secure bool
var checkp time.Duration
var botlvl, echo string
flag.StringVar(&source, "source", "", "SQL database source (required)")
flag.StringVar(&remote, "remote", "irc.chat.twitch.tv:6697", "remote address, IRC protocol")
flag.StringVar(&token, "token", "", "OAuth token")
flag.StringVar(&dsh, "dash", "", "dashboard address (no dash if not given)")
flag.BoolVar(&secure, "secure", true, "use TLS")
flag.DurationVar(&checkp, "period", time.Minute, "period between checking broadcaster online statuses")
flag.StringVar(&botlvl, "level", "", `bot level, "" or "known" or "verified"`)
flag.StringVar(&echo, "echo", "", "directory to echo generated messages (no echoing if not given)")
flag.Parse()
// Print GPLv3 information.
os.Stderr.WriteString(copying)
ctx, cancel := context.WithCancel(context.Background())
br, err := brain.Open(ctx, source)
if err != nil {
log.Fatalln(err)
}
br.SetEchoDir(echo)
switch botlvl {
case "": // do nothing
case "known":
br.SetFallbackWait(rate.NewLimiter(10, 1), rate.NewLimiter(rate.Every(time.Minute), 200))
case "verified":
br.SetFallbackWait(rate.NewLimiter(20, 1), rate.NewLimiter(rate.Every(time.Minute), 1200))
default:
log.Fatalf(`unknown bot level %q, must be "" or "known" or "verified"`, botlvl)
}
cfg := connectConfig{
addr: remote,
nick: strings.ToLower(br.Name()),
pass: "oauth:" + token,
timeout: 300 * time.Second,
retries: []time.Duration{5 * time.Second, 15 * time.Second, time.Minute, time.Minute, 3 * time.Minute},
}
if secure {
cfg.dialer = &tls.Dialer{}
} else {
cfg.dialer = &net.Dialer{}
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill)
go func() {
<-sig
cancel()
signal.Stop(sig)
}()
go onlineLoop(ctx, br, token, checkp, log.New(os.Stderr, "(online)", log.Ltime))
send := make(chan irc.Message)
recv := make(chan irc.Message, 1)
lg := log.New(os.Stderr, "(irc)", log.Ltime)
go func() {
connect(ctx, cfg, send, recv, lg)
cancel()
}()
var wg sync.WaitGroup
procs := runtime.GOMAXPROCS(0)
wg.Add(procs + 2)
go stdin(ctx, cancel, &wg, br, log.New(os.Stderr, "(stdin)", log.Ltime))
lgp := log.New(os.Stderr, "(worker)", log.Ltime)
for i := 0; i < procs; i++ {
go loop(ctx, &wg, br, send, recv, lgp)
}
if dsh != "" {
go func() {
lg := log.New(os.Stderr, "(owner-dash)", log.Ltime)
if err := dash.Owner(ctx, &wg, br, dsh, nil, lg); err != nil {
lg.Fatalf("dashboard server error: %v", err)
}
}()
}
wg.Wait()
br.Close()
}
func loop(ctx context.Context, wg *sync.WaitGroup, br *brain.Brain, send, recv chan irc.Message, lg *log.Logger) {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case msg, ok := <-recv:
if !ok {
return
}
switch msg.Command {
case "PRIVMSG":
if err := privmsg(ctx, br, send, msg, lg); err != nil {
lg.Println("error processing message:", err)
}
case "WHISPER":
// TODO: this
case "NOTICE":
// nothing yet
case "CLEARCHAT":
// Delay forgetting messages to ensure that we finish learning
// before removing what is learned.
go func(msg irc.Message) {
select {
case <-ctx.Done():
// It's more important to clear the message than to
// shutdown quickly.
br.ClearChat(context.Background(), msg.To(), msg.Nick)
return
case <-time.After(time.Second): // do nothing
}
if err := br.ClearChat(ctx, msg.To(), msg.Nick); err != nil {
lg.Println("error clearing chat:", err)
} else {
lg.Println("cleared messages due to", msg.Text())
}
}(msg)
case "CLEARMSG":
user, _ := msg.Tag("login")
if strings.EqualFold(user, br.Name()) {
// The deleted message was sent by the bot. Forget it.
err := br.ClearText(ctx, msg.To(), msg.Trailing)
if err != nil {
lg.Println("error clearing chat:", err)
} else {
lg.Println("forgot how to say that")
}
break
}
id, ok := msg.Tag("target-msg-id")
if !ok {
lg.Println("??? CLEARMSG with no target-msg-id")
break
}
go func(id string) {
select {
case <-ctx.Done():
// It's more important to clear the message than to
// shutdown quickly.
br.ClearMsg(context.Background(), id)
return
case <-time.After(time.Second): // do nothing
}
if err := br.ClearMsg(ctx, id); err != nil {
lg.Println("error clearing message:", err)
} else {
lg.Println("cleared message with id", id)
}
}(id)
case "HOSTTARGET":
// nothing yet
case "USERSTATE":
// Check our own badges and update the hard rate limit for this
// channel.
setWait(ctx, br, msg)
case "376": // End MOTD
ch := br.Channels()
send <- irc.Message{Command: "JOIN", Params: []string{strings.Join(ch, ",")}}
}
}
}
}
func privmsg(ctx context.Context, br *brain.Brain, send chan<- irc.Message, msg irc.Message, lg *log.Logger) error {
var bb [4]string
badges := msg.Badges(bb[:0])
priv, err := br.Privilege(ctx, msg.To(), msg.Nick, badges)
if err != nil {
return err
}
if priv == "ignore" {
return nil
}
if cmd, ok := commands.Parse(br.Name(), msg.Trailing); ok {
nm := commands.Do(ctx, br, lg, send, msg, priv, cmd)
if nm != "" {
lg.Println("executed command", nm)
return nil
}
}
uid, _ := msg.Tag("user-id")
if err := br.AddAffection(ctx, msg.To(), "", 1); err != nil {
lg.Printf("couldn't add affection to everyone in %s: %v", msg.To(), err)
}
if br.ShouldTalk(ctx, msg, true) == nil {
m := br.TalkIn(ctx, msg.To(), nil)
if m != "" {
eff := br.EffectIn(ctx, msg.To())
if eff != "" {
lg.Println("applying", eff, "to", m)
m = commands.Effect(eff, m)
if err := br.Said(ctx, msg.To(), m); err != nil {
lg.Println("error marking message as said:", err)
}
}
br.Wait(ctx, msg.To())
if echo := br.EchoTo(msg.To()); echo != "" {
go doEcho(ctx, lg, m, echo, msg.To())
}
send <- irc.Privmsg(msg.To(), m)
if err := br.AddAffection(ctx, msg.To(), uid, 30); err != nil {
lg.Println("couldn't add random chance affection:", err)
}
}
}
if err := br.Learn(ctx, msg); err != nil {
lg.Println("error learning message:", err)
}
if err := br.CheckCopypasta(ctx, msg); err != nil {
if !errors.Is(err, brain.NoCopypasta) {
lg.Println(err)
return nil
}
} else {
// This message is copypasta.
if err := br.ShouldTalk(ctx, msg, false); err != nil {
lg.Println("won't copypasta:", err)
return nil
}
eff := br.EffectIn(ctx, msg.To())
cp := msg.Trailing
if eff != "" {
lg.Println("applying", eff, "to", msg.Trailing)
cp = commands.Effect(eff, msg.Trailing)
}
send <- msg.Reply("%s", cp)
if err := br.AddAffection(ctx, msg.To(), uid, 20); err != nil {
lg.Println("couldn't add copypasta affection:", err)
}
}
ok, err := br.DidSay(ctx, msg.To(), msg.Trailing)
if err != nil {
lg.Println(err)
}
if ok {
if err := br.AddAffection(ctx, msg.To(), uid, 7); err != nil {
lg.Println("couldn't add affection for copying me:", err)
}
}
return nil
}
func stdin(ctx context.Context, cancel context.CancelFunc, wg *sync.WaitGroup, br *brain.Brain, lg *log.Logger) {
defer wg.Done()
scan := bufio.NewScanner(os.Stdin)
ch := make(chan string, 1)
send := make(chan irc.Message, 1)
go func() {
for {
select {
case <-ctx.Done():
return
case msg, ok := <-send:
if !ok {
return
}
lg.Println(msg)
}
}
}()
for {
go func() {
for {
if !scan.Scan() {
if scan.Err() == nil {
// EOF, i.e. ^D at terminal. We're done here.
cancel()
return
}
lg.Println("scan error:", scan.Err())
continue
}
break
}
ch <- scan.Text()
}()
select {
case <-ctx.Done():
return
case cmd := <-ch:
msg := irc.Message{
Command: "PRIVMSG",
Params: []string{"(terminal)"},
Sender: irc.Sender{Nick: "(terminal)"},
Trailing: cmd,
}
nm := commands.Do(ctx, br, lg, send, msg, "owner", cmd)
if nm != "" {
lg.Println("executed command", nm)
}
}
}
}
func onlineLoop(ctx context.Context, br *brain.Brain, token string, period time.Duration, lg *log.Logger) {
clientID, err := fetchClientID(ctx, token)
if err != nil {
lg.Fatal("An error occurred while fetching client ID:", err)
}
lim := rate.NewLimiter(rate.Every(period), 1)
var b strings.Builder
for {
if err := lim.Wait(ctx); err != nil {
return
}
channels := br.Channels()
on, err := online(ctx, token, clientID, channels)
if err != nil {
lg.Println(err)
continue
}
b.Reset()
for _, channel := range channels {
b.WriteString(channel)
if on[channel] {
b.WriteString(" ONLINE ")
} else {
b.WriteString(" offline ")
if err := br.ClearSince(ctx, channel, time.Now().Add(-period)); err != nil {
lg.Printf("couldn't clear offline messages from %s: %v", channel, err)
}
}
br.SetOnline(channel, on[channel])
}
lg.Println(b.String())
}
}
func setWait(ctx context.Context, br *brain.Brain, msg irc.Message) {
var bb [4]string
badges := msg.Badges(bb[:0])
for _, badge := range badges {
switch badge {
case "broadcaster", "moderator", "vip":
br.SetWait(ctx, msg.To(), 100/30.0)
return
}
}
br.SetWait(ctx, msg.To(), 20/30.0)
}
// doEcho writes a message as a file to echo.
func doEcho(ctx context.Context, lg *log.Logger, msg, echo, channel string) {
f, err := ioutil.TempFile(echo, channel)
if err != nil {
lg.Println("couldn't open echo file:", err)
return
}
if _, err := f.WriteString(msg); err != nil {
lg.Println("couldn't write message to echo file:", err)
return
}
if err := f.Close(); err != nil {
lg.Println("error closing echo file:", err)
return
}
}