logmania/bot/command.go

334 lines
7.7 KiB
Go
Raw Permalink Normal View History

package bot
import (
"fmt"
timeago "github.com/ararog/timeago"
2017-10-25 00:36:16 +02:00
log "github.com/sirupsen/logrus"
)
type commandFunc func(func(string), string, []string)
2017-08-11 19:59:19 +02:00
// 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)
2018-04-15 01:52:08 +02:00
for _, cmd := range b.commands {
msg = fmt.Sprintf("%s - !%s\n", msg, cmd)
}
answer(msg)
}
2017-08-11 19:59:19 +02:00
// add a chat to send log to a chat
2018-04-15 01:52:08 +02:00
func (b *Bot) addSend(answer func(string), from string, params []string) {
2017-08-11 19:59:19 +02:00
if len(params) < 1 {
2018-04-15 01:52:08 +02:00
answer("invalid: CMD IPAddress/Hostname\n or\n CMD IPAddress/Hostname to")
2017-08-11 19:59:19 +02:00
return
}
host := params[0]
to := from
if len(params) > 1 {
to = params[1]
}
2018-04-15 01:52:08 +02:00
h := b.db.GetHost(host)
if h == nil {
h = b.db.NewHost(host)
}
2018-04-15 01:52:08 +02:00
n, ok := b.db.NotifiesByAddress[to]
if !ok {
n = b.db.NewNotify(to)
}
h.AddNotify(n)
2017-08-11 19:59:19 +02:00
answer(fmt.Sprintf("added %s in list of %s", to, host))
}
//add a chat to send log to a chat
2018-04-15 01:52:08 +02:00
func (b *Bot) delSend(answer func(string), from string, params []string) {
2017-08-11 19:59:19 +02:00
if len(params) < 1 {
2018-04-15 01:52:08 +02:00
answer("invalid: CMD IPAddress/Hostname\n or\n CMD IPAddress/Hostname to")
2017-08-11 19:59:19 +02:00
return
}
host := params[0]
2017-08-11 19:59:19 +02:00
to := from
if len(params) > 1 {
to = params[1]
}
2018-04-15 01:52:08 +02:00
if h := b.db.GetHost(host); h != nil {
h.DeleteNotify(to)
2017-08-17 21:45:58 +02:00
answer(fmt.Sprintf("removed %s in list of %s", to, host))
2017-08-11 19:59:19 +02:00
} else {
answer("not found host")
}
}
2017-08-11 19:59:19 +02:00
// list all hostname with the chat where it send to
2018-04-15 01:52:08 +02:00
func (b *Bot) listSend(answer func(string), from string, params []string) {
2017-08-11 19:59:19 +02:00
msg := "sending:\n"
all := false
of := from
if len(params) > 0 {
if params[0] == "all" {
all = true
} else {
of = params[0]
}
}
2018-04-15 01:52:08 +02:00
for _, host := range b.db.Hosts {
2017-08-11 19:59:19 +02:00
toList := ""
show := all
2018-04-15 01:52:08 +02:00
for _, to := range host.Notifies {
if all {
toList = fmt.Sprintf("%s , %s", toList, to)
} else if to == of {
show = true
}
}
if !show {
continue
2017-08-11 19:59:19 +02:00
}
if len(toList) > 3 {
toList = toList[3:]
}
2018-04-15 01:52:08 +02:00
if host.Name != "" {
msg = fmt.Sprintf("%s%s (%s): %s\n", msg, host.Address, host.Name, toList)
2017-08-11 19:59:19 +02:00
} else {
2018-04-15 01:52:08 +02:00
msg = fmt.Sprintf("%s%s: %s\n", msg, host.Address, toList)
2017-08-11 19:59:19 +02:00
}
}
2017-08-11 19:59:19 +02:00
answer(msg)
}
2018-04-15 01:52:08 +02:00
// add hostname
func (b *Bot) addHostname(answer func(string), from string, params []string) {
if len(params) < 2 {
answer("invalid: CMD IPAddress/Hostname NewHostname")
return
}
2018-04-15 01:52:08 +02:00
addr := params[0]
name := params[1]
h := b.db.GetHost(addr)
if h == nil {
h = b.db.NewHost(addr)
}
b.db.ChangeHostname(h, name)
answer(fmt.Sprintf("set for %s the hostname %s", addr, name))
}
2018-04-15 01:52:08 +02:00
func (b *Bot) delHostname(answer func(string), from string, params []string) {
2017-08-11 19:59:19 +02:00
if len(params) < 2 {
2018-04-15 01:52:08 +02:00
answer("invalid: CMD IPAddress/Hostname")
2017-08-11 19:59:19 +02:00
return
}
2018-04-15 01:52:08 +02:00
addr := params[0]
h := b.db.GetHost(addr)
if h != nil {
b.db.DeleteHost(h)
if h.Name != "" {
answer(fmt.Sprintf("remove host %s with hostname %s", h.Address, h.Name))
} else {
answer(fmt.Sprintf("remove host %s", h.Address))
}
} else {
answer("could not found host")
}
}
2017-08-11 19:59:19 +02:00
2018-04-15 01:52:08 +02:00
// list all host with his ip
func (b *Bot) listHostname(answer func(string), from string, params []string) {
msg := "hostnames:\n"
for _, host := range b.db.Hosts {
if host.Lastseen.Year() > 1 {
got, _ := timeago.TimeAgoFromNowWithTime(host.Lastseen)
msg = fmt.Sprintf("%s%s - %s (%s)\n", msg, host.Address, host.Name, got)
} else {
msg = fmt.Sprintf("%s%s - %s\n", msg, host.Address, host.Name)
}
}
answer(msg)
2017-08-11 19:59:19 +02:00
}
// set a filter by max
func (b *Bot) listMaxfilter(answer func(string), from string, params []string) {
msg := "filters: "
if len(params) > 0 && params[0] == "all" {
msg = fmt.Sprintf("%s\n", msg)
2018-04-15 01:52:08 +02:00
for _, n := range b.db.Notifies {
msg = fmt.Sprintf("%s%s - %s\n", msg, n.Address(), n.MaxPrioIn.String())
}
} else {
of := from
if len(params) > 0 {
of = params[0]
}
2018-04-15 01:52:08 +02:00
if filter, ok := b.db.NotifiesByAddress[of]; ok {
msg = fmt.Sprintf("%s of %s is %s", msg, of, filter)
}
}
answer(msg)
}
2017-08-11 19:59:19 +02:00
// set a filter to a mix
func (b *Bot) setMaxfilter(answer func(string), from string, params []string) {
2017-08-11 19:59:19 +02:00
if len(params) < 1 {
2018-04-15 01:52:08 +02:00
answer("invalid: CMD Priority\n or\n CMD Channel Priority")
2017-08-11 19:59:19 +02:00
return
}
to := from
2017-10-25 00:36:16 +02:00
var max log.Level
var err error
if len(params) > 1 {
to = params[0]
2017-10-25 00:36:16 +02:00
max, err = log.ParseLevel(params[1])
} else {
max, err = log.ParseLevel(params[0])
}
if err != nil {
2018-04-15 01:52:08 +02:00
answer("invalid priority: CMD Priority\n or\n CMD Channel Priority")
2017-10-25 00:36:16 +02:00
return
}
2018-04-15 01:52:08 +02:00
n, ok := b.db.NotifiesByAddress[to]
if !ok {
n = b.db.NewNotify(to)
}
2018-04-15 01:52:08 +02:00
n.MaxPrioIn = max
answer(fmt.Sprintf("set filter for %s to %s", to, max.String()))
}
// list of regex filter
func (b *Bot) listRegex(answer func(string), from string, params []string) {
msg := "regexs:\n"
if len(params) > 0 && params[0] == "all" {
2018-04-15 01:52:08 +02:00
for _, n := range b.db.Notifies {
msg = fmt.Sprintf("%s%s\n-------------\n", msg, n.Address())
for expression := range n.RegexIn {
msg = fmt.Sprintf("%s - %s\n", msg, expression)
}
}
} else {
of := from
if len(params) > 0 {
of = params[0]
}
2018-04-15 01:52:08 +02:00
if n, ok := b.db.NotifiesByAddress[of]; ok {
msg = fmt.Sprintf("%s%s\n-------------\n", msg, of)
for expression := range n.RegexIn {
msg = fmt.Sprintf("%s - %s\n", msg, expression)
}
}
}
answer(msg)
}
// add a regex filter
func (b *Bot) addRegex(answer func(string), from string, params []string) {
if len(params) < 1 {
answer("invalid: CMD regex\n or\n CMD channel regex")
return
}
2018-05-18 11:28:01 +02:00
of := from
regex := params[0]
if len(params) > 1 {
of = params[0]
regex = params[1]
}
2018-05-18 11:28:01 +02:00
n := b.db.NotifiesByAddress[of]
2018-04-15 01:52:08 +02:00
if err := n.AddRegex(regex); err == nil {
2018-05-18 11:28:01 +02:00
answer(fmt.Sprintf("add regex for \"%s\" to %s", of, regex))
} else {
answer(fmt.Sprintf("\"%s\" is no valid regex expression: %s", regex, err))
}
}
// del a regex filter
func (b *Bot) delRegex(answer func(string), from string, params []string) {
if len(params) < 1 {
answer("invalid: CMD regex\n or\n CMD channel regex")
return
}
2018-05-18 11:28:01 +02:00
of := from
regex := params[0]
if len(params) > 1 {
of = params[0]
regex = params[1]
}
n := b.db.NotifiesByAddress[of]
2018-04-15 01:52:08 +02:00
delete(n.RegexIn, regex)
2018-05-18 11:28:01 +02:00
b.listRegex(answer, of, []string{})
}
// list of regex replace
func (b *Bot) listRegexReplace(answer func(string), from string, params []string) {
msg := "replaces:\n"
if len(params) > 0 && params[0] == "all" {
for _, n := range b.db.Notifies {
msg = fmt.Sprintf("%s%s\n-------------\n", msg, n.Address())
for expression, value := range n.RegexReplace {
msg = fmt.Sprintf("%s - \"%s\" : \"%s\"\n", msg, expression, value)
}
}
} else {
of := from
if len(params) > 0 {
of = params[0]
}
if n, ok := b.db.NotifiesByAddress[of]; ok {
msg = fmt.Sprintf("%s%s\n-------------\n", msg, of)
for expression, value := range n.RegexReplace {
msg = fmt.Sprintf("%s - \"%s\" : \"%s\"\n", msg, expression, value)
}
}
}
answer(msg)
}
// add a regex replace
func (b *Bot) addRegexReplace(answer func(string), from string, params []string) {
if len(params) < 1 {
answer("invalid: CMD regex replace\n or\n CMD channel regex replace")
return
}
of := from
regex := params[0]
value := params[1]
if len(params) > 2 {
of = params[0]
regex = params[1]
value = params[2]
}
n := b.db.NotifiesByAddress[of]
if err := n.AddRegexReplace(regex, value); err == nil {
answer(fmt.Sprintf("add replace in \"%s\" for \"%s\" to \"%s\"", of, regex, value))
} else {
answer(fmt.Sprintf("\"%s\" to \"%s\" is no valid regex replace expression: %s", regex, value, err))
}
}
// del a regex replace
func (b *Bot) delRegexReplace(answer func(string), from string, params []string) {
if len(params) < 1 {
answer("invalid: CMD regex\n or\n CMD channel regex")
return
}
of := from
regex := params[0]
if len(params) > 1 {
of = params[0]
regex = params[1]
}
n := b.db.NotifiesByAddress[of]
delete(n.RegexReplace, regex)
b.listRegexReplace(answer, of, []string{})
}