diff --git a/bot/command.go b/bot/command.go index 3d4b466..3a498b2 100644 --- a/bot/command.go +++ b/bot/command.go @@ -8,6 +8,7 @@ import ( type commandFunc func(func(string), string, []string) +// list help func (b *Bot) help(answer func(string), from string, params []string) { msg := fmt.Sprintf("Hi %s there are the following commands:\n", from) for cmd := range b.commands { @@ -16,7 +17,32 @@ func (b *Bot) help(answer func(string), from string, params []string) { answer(msg) } +// add a chat to send log to a chat func (b *Bot) sendTo(answer func(string), from string, params []string) { + if len(params) < 1 { + answer("invalid: CMD IPAddress\n or\n CMD IPAddress to") + return + } + host := params[0] + to := from + if len(params) > 1 { + to = params[1] + } + + if _, ok := b.state.HostTo[host]; !ok { + b.state.HostTo[host] = make(map[string]bool) + } + b.state.HostTo[host][to] = true + + answer(fmt.Sprintf("added %s in list of %s", to, host)) +} + +//TODO add a chat to send log to a chat +func (b *Bot) sendRemove(answer func(string), from string, params []string) { + if len(params) < 1 { + answer("invalid: CMD IPAddress\n or\n CMD IPAddress to") + return + } host := params[0] to := from if len(params) > 1 { @@ -24,15 +50,50 @@ func (b *Bot) sendTo(answer func(string), from string, params []string) { } if list, ok := b.state.HostTo[host]; ok { - b.state.HostTo[host] = append(list, to) + delete(list, to) + b.state.HostTo[host] = list + answer(fmt.Sprintf("added %s in list of %s", to, host)) } else { - b.state.HostTo[host] = []string{to} + answer("not found host") } - answer(fmt.Sprintf("added %s in list of %s", to, from)) } +// list all hostname with the chat where it send to +func (b *Bot) sendList(answer func(string), from string, params []string) { + msg := "sending:\n" + for ip, toMap := range b.state.HostTo { + toList := "" + for to := range toMap { + toList = fmt.Sprintf("%s , %s", toList, to) + } + if len(toList) > 3 { + toList = toList[3:] + } + if hostname, ok := b.state.Hostname[ip]; ok { + msg = fmt.Sprintf("%s%s (%s): %s\n", msg, ip, hostname, toList) + } else { + msg = fmt.Sprintf("%s%s: %s\n", msg, ip, toList) + } + } + answer(msg) +} + +// list all host with his ip +func (b *Bot) listHostname(answer func(string), from string, params []string) { + msg := "hostnames:\n" + for ip, hostname := range b.state.Hostname { + msg = fmt.Sprintf("%s%s - %s\n", msg, ip, hostname) + } + answer(msg) +} + +// list all hostname to a ip func (b *Bot) setHostname(answer func(string), from string, params []string) { + if len(params) < 2 { + answer("invalid: CMD IPAddress NewHostname") + return + } host := params[0] name := params[1] @@ -41,23 +102,21 @@ func (b *Bot) setHostname(answer func(string), from string, params []string) { answer(fmt.Sprintf("set for %s the hostname %s", host, name)) } -func (b *Bot) listHostname(answer func(string), from string, params []string) { - msg := "hostnames:\n" - for ip, hostname := range b.state.Hostname { - msg = fmt.Sprintf("%s%s - %s", msg, ip, hostname) - } - answer(msg) -} - +// set a filter by max func (b *Bot) listMaxfilter(answer func(string), from string, params []string) { msg := "filters:\n" for to, filter := range b.state.MaxPrioIn { - msg = fmt.Sprintf("%s%s - %s", msg, to, filter.String()) + msg = fmt.Sprintf("%s%s - %s\n", msg, to, filter.String()) } answer(msg) } +// set a filter to a mix func (b *Bot) setMaxfilter(answer func(string), from string, params []string) { + if len(params) < 1 { + answer("invalid: CMD Priority\n or\n CMD IPAddress Priority") + return + } to := from max := log.NewLoglevel(params[0]) diff --git a/bot/main.go b/bot/main.go index 550da23..6580250 100644 --- a/bot/main.go +++ b/bot/main.go @@ -19,6 +19,8 @@ func NewBot(state *configNotify.NotifyState) *Bot { b.commands = map[string]commandFunc{ "help": b.help, "send-to": b.sendTo, + "send-list": b.sendList, + "send-rm": b.sendRemove, "hostname-set": b.setHostname, "hostname-list": b.listHostname, "filter-set": b.setMaxfilter, @@ -29,7 +31,7 @@ func NewBot(state *configNotify.NotifyState) *Bot { func (b *Bot) Handle(answer func(string), from, msg string) { msgParts := strings.Split(msg, " ") - if msgParts[0][0] != '!' { + if len(msgParts[0]) <= 0 || msgParts[0][0] != '!' { return } cmdName := msgParts[0][1:] diff --git a/notify/config/config.go b/notify/config/config.go index f7a0abb..70ed5f2 100644 --- a/notify/config/config.go +++ b/notify/config/config.go @@ -2,6 +2,7 @@ package config import ( "encoding/json" + "fmt" "os" "regexp" "time" @@ -10,47 +11,51 @@ import ( ) type NotifyState struct { - Hostname map[string]string `json:"hostname"` - HostTo map[string][]string `json:"host_to"` - MaxPrioIn map[string]log.LogLevel `json:"maxLevel"` - RegexIn map[string][]string `json:"regexIn"` - regexIn map[string][]*regexp.Regexp `json:"-"` + Hostname map[string]string `json:"hostname"` + HostTo map[string]map[string]bool `json:"host_to"` + MaxPrioIn map[string]log.LogLevel `json:"maxLevel"` + RegexIn map[string]map[string]bool `json:"regexIn"` + regexIn map[string]map[string]*regexp.Regexp `json:"-"` } func (state *NotifyState) SendTo(e *log.Entry) []string { if to, ok := state.HostTo[e.Hostname]; ok { var toList []string - for _, toEntry := range to { + for toEntry, _ := range to { if lvl := state.MaxPrioIn[toEntry]; e.Level < lvl { continue } toList = append(toList, toEntry) } - e.Hostname = state.Hostname[e.Hostname] + if hostname, ok := state.Hostname[e.Hostname]; ok { + e.Hostname = hostname + } return toList + } else { + state.HostTo[e.Hostname] = make(map[string]bool) } return nil } func ReadStateFile(path string) *NotifyState { - var state *NotifyState + var state NotifyState if f, err := os.Open(path); err == nil { // transform data to legacy meshviewer - if err = json.NewDecoder(f).Decode(state); err == nil { - log.Info("loaded", len(state.HostTo), "nodes") - state.regexIn = make(map[string][]*regexp.Regexp) - return state + if err = json.NewDecoder(f).Decode(&state); err == nil { + fmt.Println("loaded", len(state.HostTo), "hosts") + state.regexIn = make(map[string]map[string]*regexp.Regexp) + return &state } else { - log.Error("failed to unmarshal nodes:", err) + fmt.Println("failed to unmarshal nodes:", err) } } else { - log.Error("failed to open state notify file: ", path, ":", err) + fmt.Println("failed to open state notify file: ", path, ":", err) } return &NotifyState{ Hostname: make(map[string]string), - HostTo: make(map[string][]string), + HostTo: make(map[string]map[string]bool), MaxPrioIn: make(map[string]log.LogLevel), - RegexIn: make(map[string][]string), - regexIn: make(map[string][]*regexp.Regexp), + RegexIn: make(map[string]map[string]bool), + regexIn: make(map[string]map[string]*regexp.Regexp), } } diff --git a/notify/xmpp/main.go b/notify/xmpp/main.go index 658dc97..fe8c823 100644 --- a/notify/xmpp/main.go +++ b/notify/xmpp/main.go @@ -1,6 +1,9 @@ package xmpp import ( + "fmt" + "strings" + xmpp "github.com/mattn/go-xmpp" "github.com/genofire/logmania/bot" @@ -41,7 +44,7 @@ func Init(config *lib.NotifyConfig, state *configNotify.NotifyState, bot *bot.Bo case xmpp.Chat: bot.Handle(func(answer string) { client.SendHtml(xmpp.Chat{Remote: v.Remote, Type: "chat", Text: answer}) - }, v.Remote, v.Text) + }, fmt.Sprintf("xmpp:%s", strings.Split(v.Remote, "/")[0]), v.Text) } } }() @@ -54,8 +57,20 @@ func (n *Notifier) Send(e *log.Entry) { if to == nil { return } - for _, to := range to { - n.client.SendHtml(xmpp.Chat{Remote: to, Type: "chat", Text: formatEntry(e)}) + for _, toAddr := range to { + to := strings.TrimPrefix(toAddr, "xmpp:") + if strings.Contains(toAddr, "conference") || strings.Contains(toAddr, "irc") { + n.client.JoinMUCNoHistory(to, "logmania") + _, err := n.client.SendHtml(xmpp.Chat{Remote: to, Type: "groupchat", Text: formatEntry(e)}) + if err != nil { + fmt.Println("xmpp to ", to, " error:", err) + } + } else { + _, err := n.client.SendHtml(xmpp.Chat{Remote: to, Type: "chat", Text: formatEntry(e)}) + if err != nil { + fmt.Println("xmpp to ", to, " error:", err) + } + } } }