Add global variables to be used in templates
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
714834e184
commit
b6d59045d0
2 changed files with 25 additions and 17 deletions
13
config.go
13
config.go
|
@ -14,12 +14,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type configFile struct {
|
type configFile struct {
|
||||||
AutoMessages []*autoMessage `yaml:"auto_messages"`
|
AutoMessages []*autoMessage `yaml:"auto_messages"`
|
||||||
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"`
|
RawLog string `yaml:"raw_log"`
|
||||||
Rules []*rule `yaml:"rules"`
|
Rules []*rule `yaml:"rules"`
|
||||||
|
Variables map[string]interface{} `yaml:"variables"`
|
||||||
|
|
||||||
rawLogWriter io.WriteCloser
|
rawLogWriter io.WriteCloser
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,34 +10,41 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func formatMessage(tplString string, m *irc.Message, r *rule, fields map[string]interface{}) (string, error) {
|
func formatMessage(tplString string, m *irc.Message, r *rule, fields map[string]interface{}) (string, error) {
|
||||||
if fields == nil {
|
compiledFields := map[string]interface{}{}
|
||||||
fields = map[string]interface{}{}
|
|
||||||
|
if config != nil {
|
||||||
|
configLock.RLock()
|
||||||
|
for k, v := range config.Variables {
|
||||||
|
compiledFields[k] = v
|
||||||
|
}
|
||||||
|
compiledFields["permitTimeout"] = int64(config.PermitTimeout / time.Second)
|
||||||
|
configLock.RUnlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range fields {
|
||||||
|
compiledFields[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
if m != nil {
|
if m != nil {
|
||||||
fields["msg"] = m
|
compiledFields["msg"] = m
|
||||||
fields["username"] = m.User
|
compiledFields["username"] = m.User
|
||||||
|
|
||||||
if m.Command == "PRIVMSG" && len(m.Params) > 0 {
|
if m.Command == "PRIVMSG" && len(m.Params) > 0 {
|
||||||
fields["channel"] = m.Params[0]
|
compiledFields["channel"] = m.Params[0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if config != nil {
|
|
||||||
fields["permitTimeout"] = int64(config.PermitTimeout / time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse and execute template
|
// Parse and execute template
|
||||||
tpl, err := template.
|
tpl, err := template.
|
||||||
New(tplString).
|
New(tplString).
|
||||||
Funcs(tplFuncs.GetFuncMap(m, r, fields)).
|
Funcs(tplFuncs.GetFuncMap(m, r, compiledFields)).
|
||||||
Parse(tplString)
|
Parse(tplString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrap(err, "parse template")
|
return "", errors.Wrap(err, "parse template")
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
err = tpl.Execute(buf, fields)
|
err = tpl.Execute(buf, compiledFields)
|
||||||
|
|
||||||
return buf.String(), errors.Wrap(err, "execute template")
|
return buf.String(), errors.Wrap(err, "execute template")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue