mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-12-20 11:51:17 +00:00
Add raw-log functionality
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
afa340af94
commit
5a2620b316
4 changed files with 58 additions and 0 deletions
41
config.go
41
config.go
|
@ -1,7 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-irc/irc"
|
"github.com/go-irc/irc"
|
||||||
|
@ -15,7 +18,10 @@ type configFile struct {
|
||||||
Channels []string `yaml:"channels"`
|
Channels []string `yaml:"channels"`
|
||||||
PermitAllowModerator bool `yaml:"permit_allow_moderator"`
|
PermitAllowModerator bool `yaml:"permit_allow_moderator"`
|
||||||
PermitTimeout time.Duration `yaml:"permit_timeout"`
|
PermitTimeout time.Duration `yaml:"permit_timeout"`
|
||||||
|
RawLog string `yaml:"raw_log"`
|
||||||
Rules []*rule `yaml:"rules"`
|
Rules []*rule `yaml:"rules"`
|
||||||
|
|
||||||
|
rawLogWriter io.WriteCloser
|
||||||
}
|
}
|
||||||
|
|
||||||
func newConfigFile() configFile {
|
func newConfigFile() configFile {
|
||||||
|
@ -82,10 +88,40 @@ func loadConfig(filename string) error {
|
||||||
configLock.Lock()
|
configLock.Lock()
|
||||||
defer configLock.Unlock()
|
defer configLock.Unlock()
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case config != nil && config.RawLog == tmpConfig.RawLog:
|
||||||
|
tmpConfig.rawLogWriter = config.rawLogWriter
|
||||||
|
|
||||||
|
case tmpConfig.RawLog == "":
|
||||||
|
if err = config.CloseRawMessageWriter(); err != nil {
|
||||||
|
return errors.Wrap(err, "closing old raw log writer")
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpConfig.rawLogWriter = writeNoOpCloser{io.Discard}
|
||||||
|
|
||||||
|
default:
|
||||||
|
if err = config.CloseRawMessageWriter(); err != nil {
|
||||||
|
return errors.Wrap(err, "closing old raw log writer")
|
||||||
|
}
|
||||||
|
if err = os.MkdirAll(path.Dir(tmpConfig.RawLog), 0o755); err != nil {
|
||||||
|
return errors.Wrap(err, "creating directories for raw log")
|
||||||
|
}
|
||||||
|
if tmpConfig.rawLogWriter, err = os.OpenFile(tmpConfig.RawLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644); err != nil {
|
||||||
|
return errors.Wrap(err, "opening raw log for appending")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
config = &tmpConfig
|
config = &tmpConfig
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *configFile) CloseRawMessageWriter() error {
|
||||||
|
if c == nil || c.rawLogWriter == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return c.rawLogWriter.Close()
|
||||||
|
}
|
||||||
|
|
||||||
func (c configFile) GetMatchingRules(m *irc.Message, event *string) []*rule {
|
func (c configFile) GetMatchingRules(m *irc.Message, event *string) []*rule {
|
||||||
configLock.RLock()
|
configLock.RLock()
|
||||||
defer configLock.RUnlock()
|
defer configLock.RUnlock()
|
||||||
|
@ -100,3 +136,8 @@ func (c configFile) GetMatchingRules(m *irc.Message, event *string) []*rule {
|
||||||
|
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c configFile) LogRawMessage(m *irc.Message) error {
|
||||||
|
_, err := fmt.Fprintln(c.rawLogWriter, m.String())
|
||||||
|
return errors.Wrap(err, "writing raw log message")
|
||||||
|
}
|
||||||
|
|
9
irc.go
9
irc.go
|
@ -70,6 +70,15 @@ func (i ircHandler) ExecuteJoins(channels []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i ircHandler) Handle(c *irc.Client, m *irc.Message) {
|
func (i ircHandler) Handle(c *irc.Client, m *irc.Message) {
|
||||||
|
go func(m *irc.Message) {
|
||||||
|
configLock.RLock()
|
||||||
|
defer configLock.RUnlock()
|
||||||
|
|
||||||
|
if err := config.LogRawMessage(m); err != nil {
|
||||||
|
log.WithError(err).Error("Unable to log raw message")
|
||||||
|
}
|
||||||
|
}(m)
|
||||||
|
|
||||||
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
|
||||||
|
|
1
main.go
1
main.go
|
@ -75,6 +75,7 @@ func main() {
|
||||||
if err = loadConfig(cfg.Config); err != nil {
|
if err = loadConfig(cfg.Config); err != nil {
|
||||||
log.WithError(err).Fatal("Initial config load failed")
|
log.WithError(err).Fatal("Initial config load failed")
|
||||||
}
|
}
|
||||||
|
defer func() { config.CloseRawMessageWriter() }()
|
||||||
|
|
||||||
fswatch, err := fsnotify.NewWatcher()
|
fswatch, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
7
wrNoOpCloser.go
Normal file
7
wrNoOpCloser.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "io"
|
||||||
|
|
||||||
|
type writeNoOpCloser struct{ io.Writer }
|
||||||
|
|
||||||
|
func (writeNoOpCloser) Close() error { return nil }
|
Loading…
Reference in a new issue