diff --git a/irc.go b/irc.go index 64fd700..fcada84 100644 --- a/irc.go +++ b/irc.go @@ -3,6 +3,7 @@ package main import ( "crypto/tls" "fmt" + "regexp" "strconv" "strings" "sync" @@ -18,6 +19,8 @@ import ( var ( rawMessageHandlers []plugins.RawMessageHandlerFunc rawMessageHandlersLock sync.Mutex + + hostNotificationRegex = regexp.MustCompile(`^(?P\w+) is now(?: auto)? hosting you(?: for(?: up to)? (?P[0-9]+) viewers)?.$`) ) func notifyRawMessageHandlers(m *irc.Message) error { @@ -295,6 +298,28 @@ func (i ircHandler) handleTwitchPrivmsg(m *irc.Message) { configLock.RUnlock() } + // Handle the jtv host message for hosts + if m.User == "jtv" && hostNotificationRegex.MatchString(m.Trailing()) { + matches := hostNotificationRegex.FindStringSubmatch(m.Trailing()) + if matches[2] == "" { + matches[2] = "0" + } + + fields := plugins.FieldCollectionFromData(map[string]interface{}{ + "from": matches[1], + "viewerCount": 0, + }) + + if v, err := strconv.Atoi(matches[2]); err == nil { + fields.Set("viewerCount", v) + } + + log.WithFields(log.Fields(fields.Data())).Info("Incoming Host (jtv announce)") + + go handleMessage(i.c, m, eventTypeHost, fields) + return + } + if strings.HasPrefix(m.Trailing(), "!permit") { i.handlePermit(m) return