-
Notifications
You must be signed in to change notification settings - Fork 6
/
tmi.go
196 lines (188 loc) · 5.75 KB
/
tmi.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
package main
import (
"context"
"log/slog"
"strings"
"time"
"gitlab.com/zephyrtronium/tmi"
"golang.org/x/sync/errgroup"
"github.com/zephyrtronium/robot/brain"
"github.com/zephyrtronium/robot/metrics"
"github.com/zephyrtronium/robot/userhash"
)
func (robo *Robot) tmiLoop(ctx context.Context, group *errgroup.Group, send chan<- *tmi.Message, recv <-chan *tmi.Message) {
for {
select {
case <-ctx.Done():
return
case msg, ok := <-recv:
if !ok {
return
}
switch msg.Command {
case "PRIVMSG":
group.Go(func() error {
robo.tmiMessage(ctx, send, msg)
return nil
})
case "WHISPER":
// TODO(zeph): this
case "NOTICE":
// nothing yet
case "CLEARCHAT":
group.Go(func() error {
robo.clearchat(ctx, msg)
return nil
})
case "CLEARMSG":
group.Go(func() error {
robo.clearmsg(ctx, msg)
return nil
})
case "HOSTTARGET":
// nothing yet
case "USERSTATE":
// We used to check our badges and update our hard rate limit
// per-channel, but per-channel rate limits only really make
// sense for verified bots which have a relaxed global limit.
case "GLOBALUSERSTATE":
slog.InfoContext(ctx, "connected to TMI", slog.String("GLOBALUSERSTATE", msg.Tags))
case "376": // End MOTD
go robo.joinTwitch(ctx, send)
}
}
}
}
func (robo *Robot) joinTwitch(ctx context.Context, send chan<- *tmi.Message) {
ls := make([]string, 0, robo.channels.Len())
for _, ch := range robo.channels.All() {
ls = append(ls, ch.Name)
}
burst := 20
for len(ls) > 0 {
l := ls[:min(burst, len(ls))]
ls = ls[len(l):]
msg := tmi.Message{
Command: "JOIN",
Params: []string{strings.Join(l, ",")},
}
select {
case <-ctx.Done():
return
case send <- &msg:
// do nothing
}
if len(ls) > 0 {
// Per https://dev.twitch.tv/docs/irc/#rate-limits we get 20 join
// attempts per ten seconds. Use a slightly longer delay to ensure
// we don't get globaled by clock drift.
time.Sleep(11 * time.Second)
}
}
}
func (robo *Robot) clearchat(ctx context.Context, msg *tmi.Message) {
if len(msg.Params) == 0 {
return
}
ch, _ := robo.channels.Load(msg.To())
if ch == nil {
return
}
t, _ := msg.Tag("target-user-id")
switch t {
case "":
// Delete all recent chat.
tag := ch.Learn
slog.InfoContext(ctx, "clear all chat", slog.String("channel", msg.To()), slog.String("tag", tag))
err := robo.brain.ForgetDuring(ctx, tag, msg.Time().Add(-15*time.Minute), msg.Time())
if err != nil {
slog.ErrorContext(ctx, "failed to forget from all chat", slog.Any("err", err), slog.String("channel", msg.To()))
}
case robo.tmi.userID:
// We use the send tag because we are forgetting something we sent.
tag := ch.Send
slog.InfoContext(ctx, "forget recent generated", slog.String("channel", msg.To()), slog.String("tag", tag))
for id, err := range robo.spoken.Since(ctx, tag, msg.Time().Add(-15*time.Minute)) {
if err != nil {
slog.ErrorContext(ctx, "failed to get recent traces",
slog.Any("err", err),
slog.String("channel", msg.To()),
slog.String("tag", tag),
)
continue
}
robo.Metrics.ForgotCount.Observe(1)
if err := robo.brain.ForgetMessage(ctx, tag, id); err != nil {
slog.ErrorContext(ctx, "failed to forget from recent trace",
slog.Any("err", err),
slog.String("channel", msg.To()),
slog.String("tag", tag),
slog.String("id", id),
)
}
}
default:
// Delete from user.
// We use the user's current and previous userhash, since userhashes
// are time-based.
hr := robo.hashes()
h := hr.Hash(new(userhash.Hash), t, msg.To(), msg.Time())
if err := robo.brain.ForgetUser(ctx, h); err != nil {
slog.ErrorContext(ctx, "failed to forget recent messages from user", slog.Any("err", err), slog.String("channel", msg.To()))
// Try the previous userhash anyway.
}
h = hr.Hash(h, t, msg.To(), msg.Time().Add(-userhash.TimeQuantum))
if err := robo.brain.ForgetUser(ctx, h); err != nil {
slog.ErrorContext(ctx, "failed to forget older messages from user", slog.Any("err", err), slog.String("channel", msg.To()))
}
}
}
func (robo *Robot) clearmsg(ctx context.Context, msg *tmi.Message) {
if len(msg.Params) == 0 {
return
}
ch, _ := robo.channels.Load(msg.To())
if ch == nil {
return
}
t, _ := msg.Tag("target-msg-id")
u, _ := msg.Tag("login")
log := slog.With(slog.String("trace", t), slog.String("in", msg.To()))
if u != robo.tmi.name {
// Forget a message from someone else.
log.InfoContext(ctx, "forget message", slog.String("tag", ch.Learn), slog.String("id", t))
forget(ctx, log, robo.Metrics.ForgotCount, robo.brain, ch.Learn, t)
return
}
// Forget a message from the robo.
// This may or may not be a generated message; it could be a command
// output or copypasta. Regardless, if it was deleted, we should try
// not to say it.
// Note that we use the send tag rather than the learn tag for this,
// because we are unlearning something that we sent.
trace, tm, err := robo.spoken.Trace(ctx, ch.Send, msg.Trailing)
if err != nil {
log.ErrorContext(ctx, "failed to get message trace",
slog.Any("err", err),
slog.String("tag", ch.Send),
slog.String("text", msg.Trailing),
slog.String("id", t),
)
return
}
log.InfoContext(ctx, "forget trace", slog.String("tag", ch.Send), slog.Any("spoken", tm), slog.Any("trace", trace))
forget(ctx, log, robo.Metrics.ForgotCount, robo.brain, ch.Send, trace...)
}
func forget(ctx context.Context, log *slog.Logger, forgetCount metrics.Observer, brain brain.Brain, tag string, trace ...string) {
forgetCount.Observe(1)
for _, id := range trace {
err := brain.ForgetMessage(ctx, tag, id)
if err != nil {
log.ErrorContext(ctx, "failed to forget message",
slog.Any("err", err),
slog.String("tag", tag),
slog.String("id", id),
)
}
}
}