logmania/bot/command.go

221 lines
5.1 KiB
Go
Raw Normal View History

package bot
import (
"fmt"
"strings"
"time"
timeago "github.com/ararog/timeago"
"github.com/genofire/logmania/log"
)
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)
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
func (b *Bot) sendTo(answer func(string), from string, params []string) {
2017-08-11 19:59:19 +02:00
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]
}
2017-08-11 19:59:19 +02:00
if _, ok := b.state.HostTo[host]; !ok {
b.state.HostTo[host] = make(map[string]bool)
}
2017-08-11 19:59:19 +02:00
b.state.HostTo[host][to] = true
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
2017-08-11 19:59:19 +02:00
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]
2017-08-11 19:59:19 +02:00
to := from
if len(params) > 1 {
to = params[1]
}
2017-08-11 19:59:19 +02:00
if list, ok := b.state.HostTo[host]; ok {
delete(list, to)
b.state.HostTo[host] = list
answer(fmt.Sprintf("added %s in list of %s", to, host))
} else {
answer("not found host")
}
}
2017-08-11 19:59:19 +02:00
// list all hostname with the chat where it send to
func (b *Bot) sendList(answer func(string), from string, params []string) {
msg := "sending:\n"
all := false
of := from
if len(params) > 0 {
if params[0] == "all" {
all = true
} else {
of = params[0]
}
}
2017-08-11 19:59:19 +02:00
for ip, toMap := range b.state.HostTo {
toList := ""
show := all
2017-08-11 19:59:19 +02:00
for to := range toMap {
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:]
}
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)
}
}
2017-08-11 19:59:19 +02:00
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 {
if last, ok := b.state.Lastseen[ip]; ok {
got, _ := timeago.TimeAgoWithTime(time.Now(), last)
msg = fmt.Sprintf("%s%s - %s (%s)\n", msg, ip, hostname, got)
} else {
msg = fmt.Sprintf("%s%s - %s\n", msg, ip, hostname)
}
}
answer(msg)
}
2017-08-11 19:59:19 +02:00
// 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]
b.state.Hostname[host] = name
answer(fmt.Sprintf("set for %s the hostname %s", host, name))
}
// 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)
for to, filter := range b.state.MaxPrioIn {
msg = fmt.Sprintf("%s%s - %s\n", msg, to, filter.String())
}
} else {
of := from
if len(params) > 0 {
of = params[0]
}
if filter, ok := b.state.MaxPrioIn[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 {
answer("invalid: CMD Priority\n or\n CMD IPAddress Priority")
return
}
to := from
max := log.NewLoglevel(params[0])
if len(params) > 1 {
to = params[0]
max = log.NewLoglevel(params[1])
}
b.state.MaxPrioIn[to] = 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" {
for to, regexs := range b.state.RegexIn {
msg = fmt.Sprintf("%s%s\n-------------\n", msg, to)
for expression := range regexs {
msg = fmt.Sprintf("%s - %s\n", msg, expression)
}
}
} else {
of := from
if len(params) > 0 {
of = params[0]
}
if regexs, ok := b.state.RegexIn[of]; ok {
msg = fmt.Sprintf("%s%s\n-------------\n", msg, from)
for expression := range regexs {
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
}
regex := strings.Join(params, " ")
if err := b.state.AddRegex(from, regex); err == nil {
answer(fmt.Sprintf("add regex for \"%s\" to %s", from, 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
}
regex := strings.Join(params, " ")
delete(b.state.RegexIn[from], regex)
b.listRegex(answer, from, []string{})
}