2020-12-21 00:32:39 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2023-09-12 08:48:49 +00:00
|
|
|
|
"context"
|
2020-12-21 00:32:39 +00:00
|
|
|
|
"crypto/tls"
|
|
|
|
|
"fmt"
|
2023-07-21 21:25:20 +00:00
|
|
|
|
"math"
|
2021-10-17 11:17:28 +00:00
|
|
|
|
"strconv"
|
2020-12-21 00:32:39 +00:00
|
|
|
|
"strings"
|
2021-09-02 13:01:10 +00:00
|
|
|
|
"sync"
|
2021-11-10 22:23:57 +00:00
|
|
|
|
"time"
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2023-09-11 17:51:38 +00:00
|
|
|
|
"gopkg.in/irc.v4"
|
2021-11-25 22:48:16 +00:00
|
|
|
|
|
2022-11-02 21:38:14 +00:00
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
|
|
|
|
|
"github.com/Luzifer/twitch-bot/v3/plugins"
|
2020-12-21 00:32:39 +00:00
|
|
|
|
)
|
|
|
|
|
|
2021-09-02 13:01:10 +00:00
|
|
|
|
var (
|
|
|
|
|
rawMessageHandlers []plugins.RawMessageHandlerFunc
|
|
|
|
|
rawMessageHandlersLock sync.Mutex
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func notifyRawMessageHandlers(m *irc.Message) error {
|
|
|
|
|
rawMessageHandlersLock.Lock()
|
|
|
|
|
defer rawMessageHandlersLock.Unlock()
|
|
|
|
|
|
|
|
|
|
for _, fn := range rawMessageHandlers {
|
|
|
|
|
if err := fn(m); err != nil {
|
|
|
|
|
return errors.Wrap(err, "executing raw message handlers")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func registerRawMessageHandler(fn plugins.RawMessageHandlerFunc) error {
|
|
|
|
|
rawMessageHandlersLock.Lock()
|
|
|
|
|
defer rawMessageHandlersLock.Unlock()
|
|
|
|
|
|
|
|
|
|
rawMessageHandlers = append(rawMessageHandlers, fn)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
type ircHandler struct {
|
2023-09-12 08:48:49 +00:00
|
|
|
|
c *irc.Client
|
|
|
|
|
conn *tls.Conn
|
|
|
|
|
ctx context.Context
|
|
|
|
|
ctxCancelFn func()
|
|
|
|
|
user string
|
2020-12-21 00:32:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newIRCHandler() (*ircHandler, error) {
|
|
|
|
|
h := new(ircHandler)
|
|
|
|
|
|
2022-10-25 16:47:30 +00:00
|
|
|
|
_, username, err := twitchClient.GetAuthorizedUser()
|
2020-12-21 00:32:39 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "fetching username")
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 08:48:49 +00:00
|
|
|
|
h.ctx, h.ctxCancelFn = context.WithCancel(context.Background())
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
conn, err := tls.Dial("tcp", "irc.chat.twitch.tv:6697", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "connect to IRC server")
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-31 12:42:37 +00:00
|
|
|
|
token, err := twitchClient.GetToken()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "getting auth token")
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
h.c = irc.NewClient(conn, irc.ClientConfig{
|
|
|
|
|
Nick: username,
|
2021-12-31 12:42:37 +00:00
|
|
|
|
Pass: strings.Join([]string{"oauth", token}, ":"),
|
2020-12-21 00:32:39 +00:00
|
|
|
|
User: username,
|
|
|
|
|
Name: username,
|
|
|
|
|
Handler: h,
|
2021-04-21 21:44:13 +00:00
|
|
|
|
|
|
|
|
|
SendLimit: cfg.IRCRateLimit,
|
|
|
|
|
SendBurst: 0, // Twitch uses a bucket system, we don't have anything to replicate that in this IRC client
|
2020-12-21 00:32:39 +00:00
|
|
|
|
})
|
|
|
|
|
h.conn = conn
|
|
|
|
|
h.user = username
|
|
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-02 21:26:39 +00:00
|
|
|
|
func (i ircHandler) Client() *irc.Client { return i.c }
|
|
|
|
|
|
2023-09-12 08:48:49 +00:00
|
|
|
|
func (i ircHandler) Close() error {
|
|
|
|
|
i.ctxCancelFn()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
2020-12-21 00:52:10 +00:00
|
|
|
|
func (i ircHandler) ExecuteJoins(channels []string) {
|
|
|
|
|
for _, ch := range channels {
|
|
|
|
|
i.c.Write(fmt.Sprintf("JOIN #%s", strings.TrimLeft(ch, "#")))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 21:42:40 +00:00
|
|
|
|
func (i ircHandler) ExecutePart(channel string) {
|
|
|
|
|
i.c.Write(fmt.Sprintf("PART #%s", strings.TrimLeft(channel, "#")))
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
func (i ircHandler) Handle(c *irc.Client, m *irc.Message) {
|
2021-12-11 23:23:06 +00:00
|
|
|
|
// We've received a message, update status check
|
|
|
|
|
statusIRCMessageReceived = time.Now()
|
|
|
|
|
|
2021-04-09 16:14:44 +00:00
|
|
|
|
go func(m *irc.Message) {
|
|
|
|
|
configLock.RLock()
|
|
|
|
|
defer configLock.RUnlock()
|
|
|
|
|
|
|
|
|
|
if err := config.LogRawMessage(m); err != nil {
|
|
|
|
|
log.WithError(err).Error("Unable to log raw message")
|
|
|
|
|
}
|
|
|
|
|
}(m)
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
switch m.Command {
|
|
|
|
|
case "001":
|
|
|
|
|
// 001 is a welcome event, so we join channels there
|
|
|
|
|
c.WriteMessage(&irc.Message{
|
|
|
|
|
Command: "CAP",
|
|
|
|
|
Params: []string{
|
|
|
|
|
"REQ",
|
|
|
|
|
strings.Join([]string{
|
|
|
|
|
"twitch.tv/commands",
|
|
|
|
|
"twitch.tv/membership",
|
|
|
|
|
"twitch.tv/tags",
|
|
|
|
|
}, " "),
|
|
|
|
|
},
|
|
|
|
|
})
|
2021-04-09 14:45:20 +00:00
|
|
|
|
go i.ExecuteJoins(config.Channels)
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
2021-11-10 22:23:57 +00:00
|
|
|
|
case "CLEARCHAT":
|
|
|
|
|
// CLEARCHAT (Twitch Commands)
|
|
|
|
|
// Purge a user’s messages, typically after a user is banned from
|
|
|
|
|
// chat or timed out.
|
|
|
|
|
i.handleClearChat(m)
|
|
|
|
|
|
2022-02-11 19:10:19 +00:00
|
|
|
|
case "CLEARMSG":
|
|
|
|
|
// CLEARMSG (Twitch Commands)
|
|
|
|
|
// Removes a single message from a channel. This is triggered by
|
|
|
|
|
// the/delete <target-msg-id> command on IRC.
|
|
|
|
|
i.handleClearMessage(m)
|
|
|
|
|
|
2021-02-13 14:12:20 +00:00
|
|
|
|
case "JOIN":
|
|
|
|
|
// JOIN (Default IRC Command)
|
|
|
|
|
// User enters the channel, might be triggered multiple times
|
|
|
|
|
// should not be used to greet users
|
|
|
|
|
i.handleJoin(m)
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
case "NOTICE":
|
|
|
|
|
// NOTICE (Twitch Commands)
|
|
|
|
|
// General notices from the server.
|
|
|
|
|
i.handleTwitchNotice(m)
|
|
|
|
|
|
2021-05-26 12:03:37 +00:00
|
|
|
|
case "PART":
|
|
|
|
|
// PART (Default IRC Command)
|
|
|
|
|
// User leaves the channel, might be triggered multiple times
|
|
|
|
|
i.handlePart(m)
|
|
|
|
|
|
2021-11-09 16:48:41 +00:00
|
|
|
|
case "PING":
|
2021-11-10 22:23:57 +00:00
|
|
|
|
// PING (Default IRC Command)
|
2021-11-09 16:48:41 +00:00
|
|
|
|
// Handled by the library, just here to prevent trace-logging every ping
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
case "PRIVMSG":
|
|
|
|
|
i.handleTwitchPrivmsg(m)
|
|
|
|
|
|
|
|
|
|
case "RECONNECT":
|
|
|
|
|
// RECONNECT (Twitch Commands)
|
|
|
|
|
// In this case, reconnect and rejoin channels that were on the connection, as you would normally.
|
|
|
|
|
log.Warn("We were asked to reconnect, closing connection")
|
|
|
|
|
i.Close()
|
|
|
|
|
|
|
|
|
|
case "USERNOTICE":
|
|
|
|
|
// USERNOTICE (Twitch Commands)
|
|
|
|
|
// Announces Twitch-specific events to the channel (for example, a user’s subscription notification).
|
|
|
|
|
i.handleTwitchUsernotice(m)
|
|
|
|
|
|
2021-09-29 16:23:29 +00:00
|
|
|
|
case "USERSTATE":
|
|
|
|
|
// USERSTATE (Twitch Tags)
|
|
|
|
|
// Sends user-state data when a user joins a channel or sends a PRIVMSG to a channel.
|
|
|
|
|
i.handleTwitchUserstate(m)
|
|
|
|
|
|
2021-05-26 12:38:15 +00:00
|
|
|
|
case "WHISPER":
|
|
|
|
|
// WHISPER (Twitch Commands)
|
|
|
|
|
// Delivers whisper-messages received
|
|
|
|
|
i.handleTwitchWhisper(m)
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
default:
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
|
"command": m.Command,
|
|
|
|
|
"tags": m.Tags,
|
|
|
|
|
"trailing": m.Trailing(),
|
|
|
|
|
}).Trace("Unhandled message")
|
|
|
|
|
// Unhandled message type, not yet needed
|
|
|
|
|
}
|
2021-09-02 13:01:10 +00:00
|
|
|
|
|
|
|
|
|
if err := notifyRawMessageHandlers(m); err != nil {
|
|
|
|
|
log.WithError(err).Error("Unable to notify raw message handlers")
|
|
|
|
|
}
|
2020-12-21 00:32:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 08:48:49 +00:00
|
|
|
|
func (i ircHandler) Run() error { return errors.Wrap(i.c.RunContext(i.ctx), "running IRC client") }
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
2021-08-06 22:00:22 +00:00
|
|
|
|
func (i ircHandler) SendMessage(m *irc.Message) error { return i.c.WriteMessage(m) }
|
|
|
|
|
|
2021-03-31 21:16:23 +00:00
|
|
|
|
func (ircHandler) getChannel(m *irc.Message) string {
|
|
|
|
|
if len(m.Params) > 0 {
|
|
|
|
|
return m.Params[0]
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 22:23:57 +00:00
|
|
|
|
func (i ircHandler) handleClearChat(m *irc.Message) {
|
2023-09-11 17:51:38 +00:00
|
|
|
|
seconds, secondsErr := strconv.Atoi(m.Tags["ban-duration"])
|
|
|
|
|
targetUserID, hasTargetUserID := m.Tags["target-user-id"]
|
2021-11-10 22:23:57 +00:00
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
evt *string
|
2021-11-11 13:59:08 +00:00
|
|
|
|
fields = plugins.NewFieldCollection()
|
2021-11-10 22:23:57 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-06-17 20:13:47 +00:00
|
|
|
|
fields.Set(eventFieldChannel, i.getChannel(m)) // Compatibility to plugins.DeriveChannel
|
2021-11-11 13:59:08 +00:00
|
|
|
|
|
2021-11-10 22:23:57 +00:00
|
|
|
|
switch {
|
|
|
|
|
case secondsErr == nil && hasTargetUserID:
|
|
|
|
|
// User & Duration = Timeout
|
|
|
|
|
evt = eventTypeTimeout
|
2021-11-11 13:59:08 +00:00
|
|
|
|
fields.Set("duration", time.Duration(seconds)*time.Second)
|
|
|
|
|
fields.Set("seconds", seconds)
|
|
|
|
|
fields.Set("target_id", targetUserID)
|
|
|
|
|
fields.Set("target_name", m.Trailing())
|
|
|
|
|
log.WithFields(log.Fields(fields.Data())).Info("User was timed out")
|
2021-11-10 22:23:57 +00:00
|
|
|
|
|
|
|
|
|
case hasTargetUserID:
|
|
|
|
|
// User w/o Duration = Ban
|
|
|
|
|
evt = eventTypeBan
|
2021-11-11 13:59:08 +00:00
|
|
|
|
fields.Set("target_id", targetUserID)
|
|
|
|
|
fields.Set("target_name", m.Trailing())
|
|
|
|
|
log.WithFields(log.Fields(fields.Data())).Info("User was banned")
|
2021-11-10 22:23:57 +00:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// No User = /clear
|
|
|
|
|
evt = eventTypeClearChat
|
2021-11-11 13:59:08 +00:00
|
|
|
|
log.WithFields(log.Fields(fields.Data())).Info("Chat was cleared")
|
2021-11-10 22:23:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go handleMessage(i.c, m, evt, fields)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 19:10:19 +00:00
|
|
|
|
func (i ircHandler) handleClearMessage(m *irc.Message) {
|
|
|
|
|
fields := plugins.FieldCollectionFromData(map[string]interface{}{
|
2022-06-17 20:13:47 +00:00
|
|
|
|
eventFieldChannel: i.getChannel(m), // Compatibility to plugins.DeriveChannel
|
|
|
|
|
"message_id": m.Tags["target-msg-id"],
|
|
|
|
|
"target_name": m.Tags["login"],
|
2022-02-11 19:10:19 +00:00
|
|
|
|
})
|
|
|
|
|
log.WithFields(log.Fields(fields.Data())).
|
|
|
|
|
WithField("message", m.Trailing()).
|
|
|
|
|
Info("Message was deleted")
|
|
|
|
|
go handleMessage(i.c, m, eventTypeDelete, fields)
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-13 14:12:20 +00:00
|
|
|
|
func (i ircHandler) handleJoin(m *irc.Message) {
|
2022-02-08 18:58:19 +00:00
|
|
|
|
fields := plugins.FieldCollectionFromData(map[string]interface{}{
|
2022-06-17 20:13:47 +00:00
|
|
|
|
eventFieldChannel: i.getChannel(m), // Compatibility to plugins.DeriveChannel
|
|
|
|
|
eventFieldUserName: m.User, // Compatibility to plugins.DeriveUser
|
2022-02-08 18:58:19 +00:00
|
|
|
|
})
|
|
|
|
|
go handleMessage(i.c, m, eventTypeJoin, fields)
|
2021-02-13 14:12:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 12:03:37 +00:00
|
|
|
|
func (i ircHandler) handlePart(m *irc.Message) {
|
2022-02-08 18:58:19 +00:00
|
|
|
|
fields := plugins.FieldCollectionFromData(map[string]interface{}{
|
2022-06-17 20:13:47 +00:00
|
|
|
|
eventFieldChannel: i.getChannel(m), // Compatibility to plugins.DeriveChannel
|
|
|
|
|
eventFieldUserName: m.User, // Compatibility to plugins.DeriveUser
|
2022-02-08 18:58:19 +00:00
|
|
|
|
})
|
|
|
|
|
go handleMessage(i.c, m, eventTypePart, fields)
|
2021-05-26 12:03:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
func (i ircHandler) handlePermit(m *irc.Message) {
|
2021-08-19 13:33:56 +00:00
|
|
|
|
badges := twitch.ParseBadgeLevels(m)
|
|
|
|
|
if !badges.Has(twitch.BadgeBroadcaster) && (!config.PermitAllowModerator || !badges.Has(twitch.BadgeModerator)) {
|
2020-12-21 00:32:39 +00:00
|
|
|
|
// Neither broadcaster nor moderator or moderator not permitted
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msgParts := strings.Split(m.Trailing(), " ")
|
2021-04-03 12:11:47 +00:00
|
|
|
|
if len(msgParts) != 2 { //nolint:gomnd // This is not a magic number but just an expected count
|
2020-12-21 00:32:39 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
username := msgParts[1]
|
|
|
|
|
|
2022-02-08 18:58:19 +00:00
|
|
|
|
fields := plugins.FieldCollectionFromData(map[string]interface{}{
|
2022-06-17 20:13:47 +00:00
|
|
|
|
eventFieldChannel: i.getChannel(m), // Compatibility to plugins.DeriveChannel
|
|
|
|
|
eventFieldUserName: m.User, // Compatibility to plugins.DeriveUser
|
|
|
|
|
eventFieldUserID: m.Tags["user-id"],
|
|
|
|
|
"to": username,
|
2022-02-08 18:58:19 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
log.WithFields(fields.Data()).Debug("Added permit")
|
2022-09-10 11:39:07 +00:00
|
|
|
|
timerService.AddPermit(m.Params[0], username)
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
2022-02-08 18:58:19 +00:00
|
|
|
|
go handleMessage(i.c, m, eventTypePermit, fields)
|
2020-12-21 00:32:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i ircHandler) handleTwitchNotice(m *irc.Message) {
|
|
|
|
|
log.WithFields(log.Fields{
|
2022-06-17 20:13:47 +00:00
|
|
|
|
eventFieldChannel: i.getChannel(m),
|
|
|
|
|
"tags": m.Tags,
|
|
|
|
|
"trailing": m.Trailing(),
|
2021-10-07 11:29:54 +00:00
|
|
|
|
}).Trace("IRC NOTICE event")
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
|
|
|
|
switch m.Tags["msg-id"] {
|
|
|
|
|
case "":
|
|
|
|
|
// Notices SHOULD have msg-id tags...
|
|
|
|
|
log.WithField("msg", m).Warn("Received notice without msg-id")
|
|
|
|
|
|
2023-04-08 00:03:40 +00:00
|
|
|
|
default:
|
|
|
|
|
log.WithField("id", m.Tags["msg-id"]).Debug("unhandled notice received")
|
2020-12-21 00:32:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i ircHandler) handleTwitchPrivmsg(m *irc.Message) {
|
|
|
|
|
log.WithFields(log.Fields{
|
2022-06-17 20:13:47 +00:00
|
|
|
|
eventFieldChannel: i.getChannel(m),
|
|
|
|
|
"name": m.Name,
|
|
|
|
|
eventFieldUserName: m.User,
|
|
|
|
|
eventFieldUserID: m.Tags["user-id"],
|
|
|
|
|
"tags": m.Tags,
|
|
|
|
|
"trailing": m.Trailing(),
|
2020-12-21 00:32:39 +00:00
|
|
|
|
}).Trace("Received privmsg")
|
|
|
|
|
|
2021-03-27 16:59:56 +00:00
|
|
|
|
if m.User != i.user {
|
|
|
|
|
// Count messages from other users than self
|
|
|
|
|
configLock.RLock()
|
|
|
|
|
for _, am := range config.AutoMessages {
|
|
|
|
|
am.CountMessage(m.Params[0])
|
|
|
|
|
}
|
|
|
|
|
configLock.RUnlock()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
if strings.HasPrefix(m.Trailing(), "!permit") {
|
|
|
|
|
i.handlePermit(m)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 11:54:26 +00:00
|
|
|
|
if bits := i.tagToNumeric(m, "bits", 0); bits > 0 {
|
2021-12-23 13:16:01 +00:00
|
|
|
|
fields := plugins.FieldCollectionFromData(map[string]interface{}{
|
2022-06-17 20:13:47 +00:00
|
|
|
|
"bits": bits,
|
|
|
|
|
eventFieldChannel: i.getChannel(m), // Compatibility to plugins.DeriveChannel
|
|
|
|
|
"message": m.Trailing(),
|
|
|
|
|
eventFieldUserName: m.User, // Compatibility to plugins.DeriveUser
|
|
|
|
|
eventFieldUserID: m.Tags["user-id"],
|
2021-12-23 13:16:01 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
log.WithFields(log.Fields(fields.Data())).Info("User spent bits in chat message")
|
|
|
|
|
|
|
|
|
|
go handleMessage(i.c, m, eventTypeBits, fields)
|
2021-10-17 11:17:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-21 21:25:20 +00:00
|
|
|
|
if amount := i.tagToNumeric(m, "pinned-chat-paid-amount", 0); amount > 0 {
|
|
|
|
|
fields := plugins.FieldCollectionFromData(map[string]any{
|
|
|
|
|
"amount": float64(amount) / math.Pow10(int(i.tagToNumeric(m, "pinned-chat-paid-exponent", 0))),
|
|
|
|
|
"currency": m.Tags["pinned-chat-paid-currency"],
|
|
|
|
|
eventFieldChannel: i.getChannel(m),
|
|
|
|
|
eventFieldUserID: m.Tags["user-id"],
|
|
|
|
|
eventFieldUserName: m.User,
|
|
|
|
|
"is_system_message": m.Tags["pinned-chat-paid-is-system-message"] == "1",
|
|
|
|
|
"level": m.Tags["pinned-chat-paid-level"],
|
|
|
|
|
"message": m.Trailing(),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
log.WithFields(log.Fields(fields.Data())).Info("User used hype-chat message")
|
|
|
|
|
|
|
|
|
|
go handleMessage(i.c, m, eventTypeHypeChat, fields)
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-02 15:09:30 +00:00
|
|
|
|
go handleMessage(i.c, m, nil, nil)
|
2020-12-21 00:32:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i ircHandler) handleTwitchUsernotice(m *irc.Message) {
|
|
|
|
|
log.WithFields(log.Fields{
|
2022-06-17 20:13:47 +00:00
|
|
|
|
eventFieldChannel: i.getChannel(m),
|
|
|
|
|
"tags": m.Tags,
|
|
|
|
|
"trailing": m.Trailing(),
|
2021-10-07 11:29:54 +00:00
|
|
|
|
}).Trace("IRC USERNOTICE event")
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
2022-04-17 12:56:51 +00:00
|
|
|
|
evtData := plugins.FieldCollectionFromData(map[string]any{
|
2022-06-17 20:13:47 +00:00
|
|
|
|
eventFieldChannel: i.getChannel(m), // Compatibility to plugins.DeriveChannel
|
|
|
|
|
eventFieldUserName: m.Tags["login"], // Compatibility to plugins.DeriveUser
|
|
|
|
|
eventFieldUserID: m.Tags["user-id"],
|
2022-04-17 12:56:51 +00:00
|
|
|
|
})
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
switch m.Tags["msg-id"] {
|
|
|
|
|
case "":
|
|
|
|
|
// Notices SHOULD have msg-id tags...
|
|
|
|
|
log.WithField("msg", m).Warn("Received usernotice without msg-id")
|
|
|
|
|
|
2022-04-01 00:02:14 +00:00
|
|
|
|
case "announcement":
|
2022-04-17 12:56:51 +00:00
|
|
|
|
evtData.SetFromData(map[string]any{
|
2022-04-01 00:02:14 +00:00
|
|
|
|
"color": m.Tags["msg-param-color"],
|
2022-04-15 18:20:09 +00:00
|
|
|
|
"message": m.Trailing(),
|
2022-04-01 00:02:14 +00:00
|
|
|
|
})
|
|
|
|
|
log.WithFields(log.Fields(evtData.Data())).Info("Announcement was made")
|
|
|
|
|
|
|
|
|
|
go handleMessage(i.c, m, eventTypeAnnouncement, evtData)
|
|
|
|
|
|
2021-11-19 18:54:53 +00:00
|
|
|
|
case "giftpaidupgrade":
|
2022-04-17 12:56:51 +00:00
|
|
|
|
evtData.SetFromData(map[string]interface{}{
|
|
|
|
|
"gifter": m.Tags["msg-param-sender-login"],
|
2021-11-19 18:54:53 +00:00
|
|
|
|
})
|
|
|
|
|
log.WithFields(log.Fields(evtData.Data())).Info("User upgraded to paid sub")
|
|
|
|
|
|
|
|
|
|
go handleMessage(i.c, m, eventTypeGiftPaidUpgrade, evtData)
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
case "raid":
|
2022-04-17 12:56:51 +00:00
|
|
|
|
evtData.SetFromData(map[string]interface{}{
|
2020-12-21 00:32:39 +00:00
|
|
|
|
"from": m.Tags["login"],
|
2022-04-19 11:54:26 +00:00
|
|
|
|
"viewercount": i.tagToNumeric(m, "msg-param-viewerCount", 0),
|
2021-11-11 13:59:08 +00:00
|
|
|
|
})
|
|
|
|
|
log.WithFields(log.Fields(evtData.Data())).Info("Incoming raid")
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
2021-10-16 12:06:25 +00:00
|
|
|
|
go handleMessage(i.c, m, eventTypeRaid, evtData)
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
|
|
|
|
case "resub":
|
2022-04-17 11:44:47 +00:00
|
|
|
|
message := m.Trailing()
|
|
|
|
|
if message == i.getChannel(m) {
|
|
|
|
|
// If no message is given, Trailing yields the channel name
|
|
|
|
|
message = ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-17 12:56:51 +00:00
|
|
|
|
evtData.SetFromData(map[string]interface{}{
|
2021-10-07 11:29:54 +00:00
|
|
|
|
"from": m.Tags["login"],
|
2022-04-17 11:44:47 +00:00
|
|
|
|
"message": message,
|
2022-05-20 21:35:52 +00:00
|
|
|
|
"multi_month": i.tagToNumeric(m, "msg-param-multimonth-duration", 0),
|
2022-04-19 11:54:26 +00:00
|
|
|
|
"subscribed_months": i.tagToNumeric(m, "msg-param-cumulative-months", 0),
|
2021-10-07 11:29:54 +00:00
|
|
|
|
"plan": m.Tags["msg-param-sub-plan"],
|
2021-11-11 13:59:08 +00:00
|
|
|
|
})
|
|
|
|
|
log.WithFields(log.Fields(evtData.Data())).Info("User re-subscribed")
|
2021-10-07 11:29:54 +00:00
|
|
|
|
|
2021-10-16 12:06:25 +00:00
|
|
|
|
go handleMessage(i.c, m, eventTypeResub, evtData)
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
2021-05-12 15:47:03 +00:00
|
|
|
|
case "sub":
|
2022-04-17 12:56:51 +00:00
|
|
|
|
evtData.SetFromData(map[string]interface{}{
|
2022-05-20 21:35:52 +00:00
|
|
|
|
"from": m.Tags["login"],
|
|
|
|
|
"multi_month": i.tagToNumeric(m, "msg-param-multimonth-duration", 0),
|
|
|
|
|
"plan": m.Tags["msg-param-sub-plan"],
|
2021-11-11 13:59:08 +00:00
|
|
|
|
})
|
|
|
|
|
log.WithFields(log.Fields(evtData.Data())).Info("User subscribed")
|
2021-10-07 11:29:54 +00:00
|
|
|
|
|
2021-10-16 12:06:25 +00:00
|
|
|
|
go handleMessage(i.c, m, eventTypeSub, evtData)
|
2021-05-12 15:47:03 +00:00
|
|
|
|
|
|
|
|
|
case "subgift", "anonsubgift":
|
2022-04-17 12:56:51 +00:00
|
|
|
|
evtData.SetFromData(map[string]interface{}{
|
2022-07-31 12:06:43 +00:00
|
|
|
|
"from": m.Tags["login"],
|
|
|
|
|
"gifted_months": i.tagToNumeric(m, "msg-param-gift-months", 1),
|
|
|
|
|
"multi_month": i.tagToNumeric(m, "msg-param-multimonth-duration", 0),
|
|
|
|
|
"origin_id": m.Tags["msg-param-origin-id"],
|
|
|
|
|
"plan": m.Tags["msg-param-sub-plan"],
|
|
|
|
|
"subscribed_months": i.tagToNumeric(m, "msg-param-months", 0),
|
|
|
|
|
"to": m.Tags["msg-param-recipient-user-name"],
|
|
|
|
|
"total_gifted": i.tagToNumeric(m, "msg-param-sender-count", 0),
|
2021-11-11 13:59:08 +00:00
|
|
|
|
})
|
|
|
|
|
log.WithFields(log.Fields(evtData.Data())).Info("User gifted a sub")
|
2021-10-07 11:29:54 +00:00
|
|
|
|
|
2021-10-16 12:06:25 +00:00
|
|
|
|
go handleMessage(i.c, m, eventTypeSubgift, evtData)
|
2021-05-12 15:47:03 +00:00
|
|
|
|
|
2021-10-08 14:08:32 +00:00
|
|
|
|
case "submysterygift":
|
2022-04-17 12:56:51 +00:00
|
|
|
|
evtData.SetFromData(map[string]interface{}{
|
2022-04-19 11:54:26 +00:00
|
|
|
|
"from": m.Tags["login"],
|
2022-05-20 21:35:52 +00:00
|
|
|
|
"multi_month": i.tagToNumeric(m, "msg-param-multimonth-duration", 0),
|
2022-04-19 11:54:26 +00:00
|
|
|
|
"number": i.tagToNumeric(m, "msg-param-mass-gift-count", 0),
|
|
|
|
|
"origin_id": m.Tags["msg-param-origin-id"],
|
|
|
|
|
"plan": m.Tags["msg-param-sub-plan"],
|
|
|
|
|
"total_gifted": i.tagToNumeric(m, "msg-param-sender-count", 0),
|
2021-11-11 13:59:08 +00:00
|
|
|
|
})
|
|
|
|
|
log.WithFields(log.Fields(evtData.Data())).Info("User gifted subs to the community")
|
2021-10-16 12:06:25 +00:00
|
|
|
|
|
|
|
|
|
go handleMessage(i.c, m, eventTypeSubmysterygift, evtData)
|
2021-10-08 14:08:32 +00:00
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-29 16:23:29 +00:00
|
|
|
|
func (i ircHandler) handleTwitchUserstate(m *irc.Message) {
|
|
|
|
|
state, err := parseTwitchUserState(m)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.WithError(err).Error("Unable to parse bot user-state")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
botUserstate.Set(plugins.DeriveChannel(m, nil), state)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 12:38:15 +00:00
|
|
|
|
func (i ircHandler) handleTwitchWhisper(m *irc.Message) {
|
2021-09-02 15:09:30 +00:00
|
|
|
|
go handleMessage(i.c, m, eventTypeWhisper, nil)
|
2021-05-26 12:38:15 +00:00
|
|
|
|
}
|
2022-04-19 11:54:26 +00:00
|
|
|
|
|
|
|
|
|
func (ircHandler) tagToNumeric(m *irc.Message, tag string, fallback int64) int64 {
|
2023-09-11 17:51:38 +00:00
|
|
|
|
tv := m.Tags[tag]
|
2022-04-19 11:54:26 +00:00
|
|
|
|
if tv == "" {
|
|
|
|
|
return fallback
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v, err := strconv.ParseInt(tv, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fallback
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return v
|
|
|
|
|
}
|