From 8f86a370f632516c540053bc8918c316fc4e05d0 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 1 Dec 2020 02:00:44 +0100 Subject: [PATCH] Handle hosts through PRIVMSG, reduce amount of cap requests --- irc.go | 60 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/irc.go b/irc.go index c666aa4..2db0c87 100644 --- a/irc.go +++ b/irc.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "net/http" + "regexp" "strings" "github.com/go-irc/irc" @@ -13,6 +14,8 @@ import ( log "github.com/sirupsen/logrus" ) +var regexpHostNotification = regexp.MustCompile(`^(?P\w+) is now(?: auto)? hosting you(?: for (?P[0-9]+) viewers)?.$`) + type ircHandler struct { conn *tls.Conn c *irc.Client @@ -51,19 +54,17 @@ func (i ircHandler) Handle(c *irc.Client, m *irc.Message) { switch m.Command { case "001": // 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{ - Command: "CAP", - Params: []string{ - "REQ", - capreq, - }, - }) - } + c.WriteMessage(&irc.Message{ + Command: "CAP", + Params: []string{ + "REQ", + strings.Join([]string{ + "twitch.tv/commands", + "twitch.tv/membership", + "twitch.tv/tags", + }, " "), + }, + }) c.Write(fmt.Sprintf("JOIN #%s", i.user)) case "NOTICE": @@ -71,6 +72,9 @@ func (i ircHandler) Handle(c *irc.Client, m *irc.Message) { // General notices from the server. i.handleTwitchNotice(m) + 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. @@ -82,6 +86,11 @@ func (i ircHandler) Handle(c *irc.Client, m *irc.Message) { i.handleTwitchUsernotice(m) default: + log.WithFields(log.Fields{ + "command": m.Command, + "tags": m.Tags, + "trailing": m.Trailing(), + }).Trace("Unhandled message") // Unhandled message type, not yet needed } } @@ -142,11 +151,34 @@ func (ircHandler) handleTwitchNotice(m *irc.Message) { case "host_success", "host_success_viewers": 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) { log.WithFields(log.Fields{ "tags": m.Tags,