package bot import ( "fmt" "strings" "time" timeago "github.com/ararog/timeago" log "github.com/sirupsen/logrus" ) 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 { msg = fmt.Sprintf("%s - !%s\n", msg, cmd) } 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.db.HostTo[host]; !ok { b.db.HostTo[host] = make(map[string]bool) } b.db.HostTo[host][to] = true answer(fmt.Sprintf("added %s in list of %s", to, host)) } //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 { to = params[1] } if list, ok := b.db.HostTo[host]; ok { delete(list, to) b.db.HostTo[host] = list answer(fmt.Sprintf("removed %s in list of %s", to, host)) } else { answer("not found host") } } // 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] } } for ip, toMap := range b.db.HostTo { toList := "" show := all for to := range toMap { if all { toList = fmt.Sprintf("%s , %s", toList, to) } else if to == of { show = true } } if !show { continue } if len(toList) > 3 { toList = toList[3:] } if hostname, ok := b.db.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.db.Hostname { if last, ok := b.db.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) } // 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.db.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.db.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.db.MaxPrioIn[of]; ok { msg = fmt.Sprintf("%s of %s is %s", msg, of, filter) } } 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 var max log.Level var err error if len(params) > 1 { to = params[0] max, err = log.ParseLevel(params[1]) } else { max, err = log.ParseLevel(params[0]) } if err != nil { answer("invalid priority: CMD Priority\n or\n CMD IPAddress Priority") return } b.db.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.db.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.db.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.db.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.db.RegexIn[from], regex) b.listRegex(answer, from, []string{}) }