Handle hosts through PRIVMSG, reduce amount of cap requests

This commit is contained in:
Knut Ahlers 2020-12-01 02:00:44 +01:00
parent f42bc6b85e
commit 8f86a370f6
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D

48
irc.go
View file

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"regexp"
"strings" "strings"
"github.com/go-irc/irc" "github.com/go-irc/irc"
@ -13,6 +14,8 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
var regexpHostNotification = regexp.MustCompile(`^(?P<actor>\w+) is now(?: auto)? hosting you(?: for (?P<amount>[0-9]+) viewers)?.$`)
type ircHandler struct { type ircHandler struct {
conn *tls.Conn conn *tls.Conn
c *irc.Client c *irc.Client
@ -51,19 +54,17 @@ func (i ircHandler) Handle(c *irc.Client, m *irc.Message) {
switch m.Command { switch m.Command {
case "001": case "001":
// 001 is a welcome event, so we join channels there // 001 is a welcome event, so we join channels there
for _, capreq := range []string{
"twitch.tv/membership",
"twitch.tv/tags",
"twitch.tv/commands",
} {
c.WriteMessage(&irc.Message{ c.WriteMessage(&irc.Message{
Command: "CAP", Command: "CAP",
Params: []string{ Params: []string{
"REQ", "REQ",
capreq, strings.Join([]string{
"twitch.tv/commands",
"twitch.tv/membership",
"twitch.tv/tags",
}, " "),
}, },
}) })
}
c.Write(fmt.Sprintf("JOIN #%s", i.user)) c.Write(fmt.Sprintf("JOIN #%s", i.user))
case "NOTICE": case "NOTICE":
@ -71,6 +72,9 @@ func (i ircHandler) Handle(c *irc.Client, m *irc.Message) {
// General notices from the server. // General notices from the server.
i.handleTwitchNotice(m) i.handleTwitchNotice(m)
case "PRIVMSG":
i.handleTwitchPrivmsg(m)
case "RECONNECT": case "RECONNECT":
// RECONNECT (Twitch Commands) // RECONNECT (Twitch Commands)
// In this case, reconnect and rejoin channels that were on the connection, as you would normally. // In this case, reconnect and rejoin channels that were on the connection, as you would normally.
@ -82,6 +86,11 @@ func (i ircHandler) Handle(c *irc.Client, m *irc.Message) {
i.handleTwitchUsernotice(m) i.handleTwitchUsernotice(m)
default: default:
log.WithFields(log.Fields{
"command": m.Command,
"tags": m.Tags,
"trailing": m.Trailing(),
}).Trace("Unhandled message")
// Unhandled message type, not yet needed // Unhandled message type, not yet needed
} }
} }
@ -142,11 +151,34 @@ func (ircHandler) handleTwitchNotice(m *irc.Message) {
case "host_success", "host_success_viewers": case "host_success", "host_success_viewers":
log.WithField("trailing", m.Trailing()).Warn("Incoming host") log.WithField("trailing", m.Trailing()).Warn("Incoming host")
// FIXME: Doesn't work? Need to figure out why, host had no notice // NOTE: Doesn't work? Need to figure out why, host had no notice
// This used to work at some time... Maybe? Dunno. Didn't get that to work.
} }
} }
func (ircHandler) handleTwitchPrivmsg(m *irc.Message) {
log.WithFields(log.Fields{
"name": m.Name,
"user": m.User,
"tags": m.Tags,
"trailing": m.Trailing(),
}).Trace("Received privmsg")
// Handle the jtv host message for hosts
if m.User == "jtv" && regexpHostNotification.MatchString(m.Trailing()) {
matches := regexpHostNotification.FindStringSubmatch(m.Trailing())
if matches[2] == "" {
matches[2] = "0"
}
subscriptions.SendAllSockets(msgTypeHost, map[string]interface{}{
"from": matches[1],
"viewerCount": matches[2],
})
}
}
func (ircHandler) handleTwitchUsernotice(m *irc.Message) { func (ircHandler) handleTwitchUsernotice(m *irc.Message) {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"tags": m.Tags, "tags": m.Tags,