Expose user_id in events

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-06-17 22:13:47 +02:00
parent 40b2e9b21c
commit 4818a3105f
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
3 changed files with 79 additions and 38 deletions

7
fields.go Normal file
View file

@ -0,0 +1,7 @@
package main
const (
eventFieldChannel = "channel"
eventFieldUserID = "user_id"
eventFieldUserName = "user"
)

71
irc.go
View file

@ -219,7 +219,7 @@ func (i ircHandler) handleClearChat(m *irc.Message) {
fields = plugins.NewFieldCollection()
)
fields.Set("channel", i.getChannel(m)) // Compatibility to plugins.DeriveChannel
fields.Set(eventFieldChannel, i.getChannel(m)) // Compatibility to plugins.DeriveChannel
switch {
case secondsErr == nil && hasTargetUserID:
@ -249,9 +249,9 @@ func (i ircHandler) handleClearChat(m *irc.Message) {
func (i ircHandler) handleClearMessage(m *irc.Message) {
fields := plugins.FieldCollectionFromData(map[string]interface{}{
"channel": i.getChannel(m), // Compatibility to plugins.DeriveChannel
"message_id": m.Tags["target-msg-id"],
"target_name": m.Tags["login"],
eventFieldChannel: i.getChannel(m), // Compatibility to plugins.DeriveChannel
"message_id": m.Tags["target-msg-id"],
"target_name": m.Tags["login"],
})
log.WithFields(log.Fields(fields.Data())).
WithField("message", m.Trailing()).
@ -261,16 +261,16 @@ func (i ircHandler) handleClearMessage(m *irc.Message) {
func (i ircHandler) handleJoin(m *irc.Message) {
fields := plugins.FieldCollectionFromData(map[string]interface{}{
"channel": i.getChannel(m), // Compatibility to plugins.DeriveChannel
"user": m.User, // Compatibility to plugins.DeriveUser
eventFieldChannel: i.getChannel(m), // Compatibility to plugins.DeriveChannel
eventFieldUserName: m.User, // Compatibility to plugins.DeriveUser
})
go handleMessage(i.c, m, eventTypeJoin, fields)
}
func (i ircHandler) handlePart(m *irc.Message) {
fields := plugins.FieldCollectionFromData(map[string]interface{}{
"channel": i.getChannel(m), // Compatibility to plugins.DeriveChannel
"user": m.User, // Compatibility to plugins.DeriveUser
eventFieldChannel: i.getChannel(m), // Compatibility to plugins.DeriveChannel
eventFieldUserName: m.User, // Compatibility to plugins.DeriveUser
})
go handleMessage(i.c, m, eventTypePart, fields)
}
@ -290,10 +290,10 @@ func (i ircHandler) handlePermit(m *irc.Message) {
username := msgParts[1]
fields := plugins.FieldCollectionFromData(map[string]interface{}{
"channel": i.getChannel(m), // Compatibility to plugins.DeriveChannel
"user": m.User, // Compatibility to plugins.DeriveUser
"username": username, // DEPRECATED but kept for comapatibility
"to": username,
eventFieldChannel: i.getChannel(m), // Compatibility to plugins.DeriveChannel
eventFieldUserName: m.User, // Compatibility to plugins.DeriveUser
eventFieldUserID: m.Tags["user-id"],
"to": username,
})
log.WithFields(fields.Data()).Debug("Added permit")
@ -304,9 +304,9 @@ func (i ircHandler) handlePermit(m *irc.Message) {
func (i ircHandler) handleTwitchNotice(m *irc.Message) {
log.WithFields(log.Fields{
"channel": i.getChannel(m),
"tags": m.Tags,
"trailing": m.Trailing(),
eventFieldChannel: i.getChannel(m),
"tags": m.Tags,
"trailing": m.Trailing(),
}).Trace("IRC NOTICE event")
switch m.Tags["msg-id"] {
@ -318,8 +318,8 @@ func (i ircHandler) handleTwitchNotice(m *irc.Message) {
log.WithField("trailing", m.Trailing()).Warn("Incoming host")
fields := plugins.FieldCollectionFromData(map[string]interface{}{
"channel": i.getChannel(m), // Compatibility to plugins.DeriveChannel
"user": m.User, // Compatibility to plugins.DeriveUser
eventFieldChannel: i.getChannel(m), // Compatibility to plugins.DeriveChannel
eventFieldUserName: m.User, // Compatibility to plugins.DeriveUser
})
go handleMessage(i.c, m, eventTypeHost, fields)
@ -328,11 +328,12 @@ func (i ircHandler) handleTwitchNotice(m *irc.Message) {
func (i ircHandler) handleTwitchPrivmsg(m *irc.Message) {
log.WithFields(log.Fields{
"channel": i.getChannel(m),
"name": m.Name,
"user": m.User,
"tags": m.Tags,
"trailing": m.Trailing(),
eventFieldChannel: i.getChannel(m),
"name": m.Name,
eventFieldUserName: m.User,
eventFieldUserID: m.Tags["user-id"],
"tags": m.Tags,
"trailing": m.Trailing(),
}).Trace("Received privmsg")
if m.User != i.user {
@ -352,9 +353,9 @@ func (i ircHandler) handleTwitchPrivmsg(m *irc.Message) {
}
fields := plugins.FieldCollectionFromData(map[string]interface{}{
"channel": fmt.Sprintf("#%s", i.user),
"from": matches[1],
"viewerCount": 0,
eventFieldChannel: fmt.Sprintf("#%s", i.user),
"from": matches[1],
"viewerCount": 0,
})
if v, err := strconv.Atoi(matches[2]); err == nil {
@ -374,10 +375,11 @@ func (i ircHandler) handleTwitchPrivmsg(m *irc.Message) {
if bits := i.tagToNumeric(m, "bits", 0); bits > 0 {
fields := plugins.FieldCollectionFromData(map[string]interface{}{
"bits": bits,
"channel": i.getChannel(m), // Compatibility to plugins.DeriveChannel
"message": m.Trailing(),
"user": m.User, // Compatibility to plugins.DeriveUser
"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"],
})
log.WithFields(log.Fields(fields.Data())).Info("User spent bits in chat message")
@ -390,14 +392,15 @@ func (i ircHandler) handleTwitchPrivmsg(m *irc.Message) {
func (i ircHandler) handleTwitchUsernotice(m *irc.Message) {
log.WithFields(log.Fields{
"channel": i.getChannel(m),
"tags": m.Tags,
"trailing": m.Trailing(),
eventFieldChannel: i.getChannel(m),
"tags": m.Tags,
"trailing": m.Trailing(),
}).Trace("IRC USERNOTICE event")
evtData := plugins.FieldCollectionFromData(map[string]any{
"channel": i.getChannel(m), // Compatibility to plugins.DeriveChannel
"user": m.Tags["login"], // Compatibility to plugins.DeriveUser
eventFieldChannel: i.getChannel(m), // Compatibility to plugins.DeriveChannel
eventFieldUserName: m.Tags["login"], // Compatibility to plugins.DeriveUser
eventFieldUserID: m.Tags["user-id"],
})
switch m.Tags["msg-id"] {

View file

@ -17,6 +17,13 @@ var (
_ plugins.MsgFormatter = formatMessage
stripNewline = regexp.MustCompile(`(?m)\s*\n\s*`)
formatMessageFieldSetters = []func(compiledFields *plugins.FieldCollection, m *irc.Message, fields *plugins.FieldCollection){
formatMessageFieldChannel,
formatMessageFieldMessage,
formatMessageFieldUserID,
formatMessageFieldUsername,
}
)
func formatMessage(tplString string, m *irc.Message, r *plugins.Rule, fields *plugins.FieldCollection) (string, error) {
@ -31,11 +38,9 @@ func formatMessage(tplString string, m *irc.Message, r *plugins.Rule, fields *pl
compiledFields.SetFromData(fields.Data())
if m != nil {
compiledFields.Set("msg", m)
for _, fn := range formatMessageFieldSetters {
fn(compiledFields, m, fields)
}
compiledFields.Set("username", plugins.DeriveUser(m, fields))
compiledFields.Set("channel", plugins.DeriveChannel(m, fields))
// Template in frontend supports newlines, messages do not
tplString = stripNewline.ReplaceAllString(tplString, " ")
@ -54,3 +59,29 @@ func formatMessage(tplString string, m *irc.Message, r *plugins.Rule, fields *pl
return buf.String(), errors.Wrap(err, "execute template")
}
func formatMessageFieldChannel(compiledFields *plugins.FieldCollection, m *irc.Message, fields *plugins.FieldCollection) {
compiledFields.Set(eventFieldChannel, plugins.DeriveChannel(m, fields))
}
func formatMessageFieldMessage(compiledFields *plugins.FieldCollection, m *irc.Message, fields *plugins.FieldCollection) {
if m == nil {
return
}
compiledFields.Set("msg", m)
}
func formatMessageFieldUserID(compiledFields *plugins.FieldCollection, m *irc.Message, fields *plugins.FieldCollection) {
if m == nil {
return
}
if uid := m.Tags["user-id"]; uid != "" {
compiledFields.Set(eventFieldUserID, uid)
}
}
func formatMessageFieldUsername(compiledFields *plugins.FieldCollection, m *irc.Message, fields *plugins.FieldCollection) {
compiledFields.Set("username", plugins.DeriveUser(m, fields))
}