add bot commands + add muc notify

This commit is contained in:
Martin Geno 2017-08-11 19:59:19 +02:00
parent e25a322943
commit a37c94c4d5
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
4 changed files with 114 additions and 33 deletions

View File

@ -8,6 +8,7 @@ import (
type commandFunc func(func(string), string, []string) type commandFunc func(func(string), string, []string)
// list help
func (b *Bot) help(answer func(string), from string, params []string) { func (b *Bot) help(answer func(string), from string, params []string) {
msg := fmt.Sprintf("Hi %s there are the following commands:\n", from) msg := fmt.Sprintf("Hi %s there are the following commands:\n", from)
for cmd := range b.commands { for cmd := range b.commands {
@ -16,7 +17,32 @@ func (b *Bot) help(answer func(string), from string, params []string) {
answer(msg) answer(msg)
} }
// add a chat to send log to a chat
func (b *Bot) sendTo(answer func(string), from string, params []string) { 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] host := params[0]
to := from to := from
if len(params) > 1 { 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 { 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 { } 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) { func (b *Bot) setHostname(answer func(string), from string, params []string) {
if len(params) < 2 {
answer("invalid: CMD IPAddress NewHostname")
return
}
host := params[0] host := params[0]
name := params[1] 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)) answer(fmt.Sprintf("set for %s the hostname %s", host, name))
} }
func (b *Bot) listHostname(answer func(string), from string, params []string) { // set a filter by max
msg := "hostnames:\n"
for ip, hostname := range b.state.Hostname {
msg = fmt.Sprintf("%s%s - %s", msg, ip, hostname)
}
answer(msg)
}
func (b *Bot) listMaxfilter(answer func(string), from string, params []string) { func (b *Bot) listMaxfilter(answer func(string), from string, params []string) {
msg := "filters:\n" msg := "filters:\n"
for to, filter := range b.state.MaxPrioIn { 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) answer(msg)
} }
// set a filter to a mix
func (b *Bot) setMaxfilter(answer func(string), from string, params []string) { 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 to := from
max := log.NewLoglevel(params[0]) max := log.NewLoglevel(params[0])

View File

@ -19,6 +19,8 @@ func NewBot(state *configNotify.NotifyState) *Bot {
b.commands = map[string]commandFunc{ b.commands = map[string]commandFunc{
"help": b.help, "help": b.help,
"send-to": b.sendTo, "send-to": b.sendTo,
"send-list": b.sendList,
"send-rm": b.sendRemove,
"hostname-set": b.setHostname, "hostname-set": b.setHostname,
"hostname-list": b.listHostname, "hostname-list": b.listHostname,
"filter-set": b.setMaxfilter, "filter-set": b.setMaxfilter,
@ -29,7 +31,7 @@ func NewBot(state *configNotify.NotifyState) *Bot {
func (b *Bot) Handle(answer func(string), from, msg string) { func (b *Bot) Handle(answer func(string), from, msg string) {
msgParts := strings.Split(msg, " ") msgParts := strings.Split(msg, " ")
if msgParts[0][0] != '!' { if len(msgParts[0]) <= 0 || msgParts[0][0] != '!' {
return return
} }
cmdName := msgParts[0][1:] cmdName := msgParts[0][1:]

View File

@ -2,6 +2,7 @@ package config
import ( import (
"encoding/json" "encoding/json"
"fmt"
"os" "os"
"regexp" "regexp"
"time" "time"
@ -11,46 +12,50 @@ import (
type NotifyState struct { type NotifyState struct {
Hostname map[string]string `json:"hostname"` Hostname map[string]string `json:"hostname"`
HostTo map[string][]string `json:"host_to"` HostTo map[string]map[string]bool `json:"host_to"`
MaxPrioIn map[string]log.LogLevel `json:"maxLevel"` MaxPrioIn map[string]log.LogLevel `json:"maxLevel"`
RegexIn map[string][]string `json:"regexIn"` RegexIn map[string]map[string]bool `json:"regexIn"`
regexIn map[string][]*regexp.Regexp `json:"-"` regexIn map[string]map[string]*regexp.Regexp `json:"-"`
} }
func (state *NotifyState) SendTo(e *log.Entry) []string { func (state *NotifyState) SendTo(e *log.Entry) []string {
if to, ok := state.HostTo[e.Hostname]; ok { if to, ok := state.HostTo[e.Hostname]; ok {
var toList []string var toList []string
for _, toEntry := range to { for toEntry, _ := range to {
if lvl := state.MaxPrioIn[toEntry]; e.Level < lvl { if lvl := state.MaxPrioIn[toEntry]; e.Level < lvl {
continue continue
} }
toList = append(toList, toEntry) toList = append(toList, toEntry)
} }
e.Hostname = state.Hostname[e.Hostname] if hostname, ok := state.Hostname[e.Hostname]; ok {
e.Hostname = hostname
}
return toList return toList
} else {
state.HostTo[e.Hostname] = make(map[string]bool)
} }
return nil return nil
} }
func ReadStateFile(path string) *NotifyState { 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 f, err := os.Open(path); err == nil { // transform data to legacy meshviewer
if err = json.NewDecoder(f).Decode(state); err == nil { if err = json.NewDecoder(f).Decode(&state); err == nil {
log.Info("loaded", len(state.HostTo), "nodes") fmt.Println("loaded", len(state.HostTo), "hosts")
state.regexIn = make(map[string][]*regexp.Regexp) state.regexIn = make(map[string]map[string]*regexp.Regexp)
return state return &state
} else { } else {
log.Error("failed to unmarshal nodes:", err) fmt.Println("failed to unmarshal nodes:", err)
} }
} else { } else {
log.Error("failed to open state notify file: ", path, ":", err) fmt.Println("failed to open state notify file: ", path, ":", err)
} }
return &NotifyState{ return &NotifyState{
Hostname: make(map[string]string), Hostname: make(map[string]string),
HostTo: make(map[string][]string), HostTo: make(map[string]map[string]bool),
MaxPrioIn: make(map[string]log.LogLevel), MaxPrioIn: make(map[string]log.LogLevel),
RegexIn: make(map[string][]string), RegexIn: make(map[string]map[string]bool),
regexIn: make(map[string][]*regexp.Regexp), regexIn: make(map[string]map[string]*regexp.Regexp),
} }
} }

View File

@ -1,6 +1,9 @@
package xmpp package xmpp
import ( import (
"fmt"
"strings"
xmpp "github.com/mattn/go-xmpp" xmpp "github.com/mattn/go-xmpp"
"github.com/genofire/logmania/bot" "github.com/genofire/logmania/bot"
@ -41,7 +44,7 @@ func Init(config *lib.NotifyConfig, state *configNotify.NotifyState, bot *bot.Bo
case xmpp.Chat: case xmpp.Chat:
bot.Handle(func(answer string) { bot.Handle(func(answer string) {
client.SendHtml(xmpp.Chat{Remote: v.Remote, Type: "chat", Text: answer}) 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 { if to == nil {
return return
} }
for _, to := range to { for _, toAddr := range to {
n.client.SendHtml(xmpp.Chat{Remote: to, Type: "chat", Text: formatEntry(e)}) 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)
}
}
} }
} }