logmania/database/notify.go

68 lines
1.3 KiB
Go
Raw Normal View History

2018-04-15 01:52:08 +02:00
package database
import (
"regexp"
"strings"
log "github.com/sirupsen/logrus"
)
type Notify struct {
Protocoll string `json:"proto"`
To string `json:"to"`
RegexIn map[string]*regexp.Regexp `json:"regexIn"`
MaxPrioIn log.Level `json:"maxLevel"`
}
func (n *Notify) Init() {
if n.RegexIn == nil {
n.RegexIn = make(map[string]*regexp.Regexp)
}
2018-04-18 14:32:28 +02:00
for exp := range n.RegexIn {
2018-04-15 01:52:08 +02:00
regex, err := regexp.Compile(exp)
if err == nil {
n.RegexIn[exp] = regex
}
}
}
func (n *Notify) AddRegex(expression string) error {
regex, err := regexp.Compile(expression)
if err == nil {
n.RegexIn[expression] = regex
}
return err
}
func (n *Notify) Address() string {
return n.Protocoll + ":" + n.To
}
// -- global notify
func (db *DB) InitNotify() {
if db.NotifiesByAddress == nil {
db.NotifiesByAddress = make(map[string]*Notify)
}
for _, n := range db.Notifies {
n.Init()
db.NotifiesByAddress[n.Address()] = n
}
}
func (db *DB) AddNotify(n *Notify) {
db.Notifies = append(db.Notifies, n)
db.NotifiesByAddress[n.Address()] = n
}
func (db *DB) NewNotify(to string) *Notify {
addr := strings.Split(to, ":")
n := &Notify{
Protocoll: addr[0],
To: addr[1],
RegexIn: make(map[string]*regexp.Regexp),
}
db.AddNotify(n)
return n
}