Fix ID generation handling different automessages as same

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-06-13 15:45:19 +02:00
parent fbf1ae2f03
commit dbb36bc941
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
4 changed files with 24 additions and 19 deletions

View file

@ -1,13 +1,13 @@
package main
import (
"crypto/sha256"
"fmt"
"strings"
"sync"
"time"
"github.com/go-irc/irc"
"github.com/mitchellh/hashstructure/v2"
"github.com/pkg/errors"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
@ -16,6 +16,8 @@ import (
var cronParser = cron.NewParser(cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
type autoMessage struct {
UUID string `hash:"-" yaml:"uuid"`
Channel string `yaml:"channel"`
Message string `yaml:"message"`
UseAction bool `yaml:"use_action"`
@ -101,13 +103,15 @@ func (a *autoMessage) CountMessage(channel string) {
}
func (a *autoMessage) ID() string {
sum := sha256.New()
if a.UUID != "" {
return a.UUID
}
fmt.Fprintf(sum, "channel:%q", a.Channel)
fmt.Fprintf(sum, "message:%q", a.Message)
fmt.Fprintf(sum, "action:%v", a.UseAction)
return fmt.Sprintf("sha256:%x", sum.Sum(nil))
h, err := hashstructure.Hash(a, hashstructure.FormatV2, nil)
if err != nil {
panic(errors.Wrap(err, "hashing automessage"))
}
return fmt.Sprintf("hashstructure:%x", h)
}
func (a *autoMessage) IsValid() bool {

1
go.mod
View file

@ -10,6 +10,7 @@ require (
github.com/Luzifer/rconfig/v2 v2.2.1
github.com/go-irc/irc v2.1.0+incompatible
github.com/kr/pretty v0.1.0 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/onsi/gomega v1.12.0 // indirect
github.com/pkg/errors v0.9.1
github.com/robfig/cron/v3 v3.0.1

2
go.sum
View file

@ -95,6 +95,8 @@ github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4NcD46KavDd4=
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=

22
rule.go
View file

@ -2,7 +2,6 @@ package main
import (
"bytes"
"crypto/sha256"
"encoding/json"
"fmt"
"regexp"
@ -11,11 +10,14 @@ import (
"github.com/Luzifer/go_helpers/v2/str"
"github.com/go-irc/irc"
"github.com/mitchellh/hashstructure/v2"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type Rule struct {
UUID string `hash:"-" yaml:"uuid"`
Actions []*RuleAction `yaml:"actions"`
Cooldown *time.Duration `yaml:"cooldown"`
@ -42,19 +44,15 @@ type Rule struct {
}
func (r Rule) MatcherID() string {
out := sha256.New()
for _, e := range []*string{
ptrStr(strings.Join(r.MatchChannels, "|")),
r.MatchEvent,
r.MatchMessage,
} {
if e != nil {
fmt.Fprintf(out, *e)
}
if r.UUID != "" {
return r.UUID
}
return fmt.Sprintf("sha256:%x", out.Sum(nil))
h, err := hashstructure.Hash(r, hashstructure.FormatV2, nil)
if err != nil {
panic(errors.Wrap(err, "hashing automessage"))
}
return fmt.Sprintf("hashstructure:%x", h)
}
func (r *Rule) matches(m *irc.Message, event *string) bool {