2020-12-21 00:32:39 +00:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
|
"github.com/Luzifer/twitch-bot/twitch"
|
2020-12-21 00:32:39 +00:00
|
|
|
|
"github.com/go-irc/irc"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ircHandler struct {
|
|
|
|
|
conn *tls.Conn
|
|
|
|
|
c *irc.Client
|
|
|
|
|
user string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newIRCHandler() (*ircHandler, error) {
|
|
|
|
|
h := new(ircHandler)
|
|
|
|
|
|
2021-08-19 13:33:56 +00:00
|
|
|
|
username, err := twitchClient.GetAuthorizedUsername()
|
2020-12-21 00:32:39 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "fetching username")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn, err := tls.Dial("tcp", "irc.chat.twitch.tv:6697", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "connect to IRC server")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h.c = irc.NewClient(conn, irc.ClientConfig{
|
|
|
|
|
Nick: username,
|
|
|
|
|
Pass: strings.Join([]string{"oauth", cfg.TwitchToken}, ":"),
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i ircHandler) Close() error { return i.conn.Close() }
|
|
|
|
|
|
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-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-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)
|
|
|
|
|
|
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-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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i ircHandler) Run() error { return errors.Wrap(i.c.Run(), "running IRC client") }
|
|
|
|
|
|
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-02-13 14:12:20 +00:00
|
|
|
|
func (i ircHandler) handleJoin(m *irc.Message) {
|
|
|
|
|
go handleMessage(i.c, m, eventTypeJoin)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 12:03:37 +00:00
|
|
|
|
func (i ircHandler) handlePart(m *irc.Message) {
|
|
|
|
|
go handleMessage(i.c, m, eventTypePart)
|
|
|
|
|
}
|
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
|
|
log.WithField("user", username).Debug("Added permit")
|
2020-12-24 14:59:11 +00:00
|
|
|
|
timerStore.AddPermit(m.Params[0], username)
|
2020-12-21 00:32:39 +00:00
|
|
|
|
|
|
|
|
|
go handleMessage(i.c, m, eventTypePermit)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i ircHandler) handleTwitchNotice(m *irc.Message) {
|
|
|
|
|
log.WithFields(log.Fields{
|
2021-03-31 21:16:23 +00:00
|
|
|
|
"channel": i.getChannel(m),
|
2020-12-21 00:32:39 +00:00
|
|
|
|
"tags": m.Tags,
|
2021-06-11 16:02:08 +00:00
|
|
|
|
"trailing": m.Trailing(),
|
2020-12-21 00:32:39 +00:00
|
|
|
|
}).Debug("IRC NOTICE event")
|
|
|
|
|
|
|
|
|
|
switch m.Tags["msg-id"] {
|
|
|
|
|
case "":
|
|
|
|
|
// Notices SHOULD have msg-id tags...
|
|
|
|
|
log.WithField("msg", m).Warn("Received notice without msg-id")
|
|
|
|
|
|
|
|
|
|
case "host_success", "host_success_viewers":
|
|
|
|
|
log.WithField("trailing", m.Trailing()).Warn("Incoming host")
|
|
|
|
|
|
|
|
|
|
go handleMessage(i.c, m, eventTypeHost)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i ircHandler) handleTwitchPrivmsg(m *irc.Message) {
|
|
|
|
|
log.WithFields(log.Fields{
|
2021-03-31 21:16:23 +00:00
|
|
|
|
"channel": i.getChannel(m),
|
2020-12-21 00:32:39 +00:00
|
|
|
|
"name": m.Name,
|
|
|
|
|
"user": m.User,
|
|
|
|
|
"tags": m.Tags,
|
|
|
|
|
"trailing": m.Trailing(),
|
|
|
|
|
}).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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go handleMessage(i.c, m, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i ircHandler) handleTwitchUsernotice(m *irc.Message) {
|
|
|
|
|
log.WithFields(log.Fields{
|
2021-03-31 21:16:23 +00:00
|
|
|
|
"channel": i.getChannel(m),
|
2020-12-21 00:32:39 +00:00
|
|
|
|
"tags": m.Tags,
|
2021-06-13 00:19:39 +00:00
|
|
|
|
"trailing": m.Trailing(),
|
2020-12-21 00:32:39 +00:00
|
|
|
|
}).Debug("IRC USERNOTICE event")
|
|
|
|
|
|
|
|
|
|
switch m.Tags["msg-id"] {
|
|
|
|
|
case "":
|
|
|
|
|
// Notices SHOULD have msg-id tags...
|
|
|
|
|
log.WithField("msg", m).Warn("Received usernotice without msg-id")
|
|
|
|
|
|
|
|
|
|
case "raid":
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
|
"from": m.Tags["login"],
|
|
|
|
|
"viewercount": m.Tags["msg-param-viewerCount"],
|
|
|
|
|
}).Info("Incoming raid")
|
|
|
|
|
|
|
|
|
|
go handleMessage(i.c, m, eventTypeRaid)
|
|
|
|
|
|
|
|
|
|
case "resub":
|
|
|
|
|
go handleMessage(i.c, m, eventTypeResub)
|
|
|
|
|
|
2021-05-12 15:47:03 +00:00
|
|
|
|
case "sub":
|
|
|
|
|
go handleMessage(i.c, m, eventTypeSub)
|
|
|
|
|
|
|
|
|
|
case "subgift", "anonsubgift":
|
|
|
|
|
go handleMessage(i.c, m, eventTypeSubgift)
|
|
|
|
|
|
2020-12-21 00:32:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 12:38:15 +00:00
|
|
|
|
func (i ircHandler) handleTwitchWhisper(m *irc.Message) {
|
|
|
|
|
go handleMessage(i.c, m, eventTypeWhisper)
|
|
|
|
|
}
|