[TASK] refactory Bot
This commit is contained in:
parent
5f9ad29483
commit
5c906df45d
351
bot/command.go
351
bot/command.go
|
@ -2,340 +2,45 @@ package bot
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
timeago "github.com/ararog/timeago"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type commandFunc func(func(string), string, []string)
|
||||
type commandFunc func(from string, params []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)
|
||||
type Command struct {
|
||||
Name string
|
||||
Description string
|
||||
Commands []*Command
|
||||
Action commandFunc
|
||||
}
|
||||
|
||||
// add a chat to send log to a chat
|
||||
func (b *Bot) addSend(answer func(string), from string, params []string) {
|
||||
if len(params) < 1 {
|
||||
answer("invalid: CMD IPAddress/Hostname\n or\n CMD IPAddress/Hostname to")
|
||||
return
|
||||
}
|
||||
host := params[0]
|
||||
to := from
|
||||
if len(params) > 1 {
|
||||
to = params[1]
|
||||
}
|
||||
|
||||
h := b.db.GetHost(host)
|
||||
if h == nil {
|
||||
h = b.db.NewHost(host)
|
||||
}
|
||||
if h == nil {
|
||||
answer(fmt.Sprintf("could not create host %s", host))
|
||||
return
|
||||
}
|
||||
n, ok := b.db.NotifiesByAddress[to]
|
||||
if !ok {
|
||||
n = b.db.NewNotify(to)
|
||||
}
|
||||
if n == nil {
|
||||
answer(fmt.Sprintf("could not create notify %s in list of %s", to, host))
|
||||
return
|
||||
}
|
||||
h.AddNotify(n)
|
||||
|
||||
answer(fmt.Sprintf("added %s in list of %s", to, host))
|
||||
}
|
||||
|
||||
//add a chat to send log to a chat
|
||||
func (b *Bot) delSend(answer func(string), from string, params []string) {
|
||||
if len(params) < 1 {
|
||||
answer("invalid: CMD IPAddress/Hostname\n or\n CMD IPAddress/Hostname to")
|
||||
return
|
||||
}
|
||||
host := params[0]
|
||||
to := from
|
||||
if len(params) > 1 {
|
||||
to = params[1]
|
||||
}
|
||||
|
||||
if h := b.db.GetHost(host); h != nil {
|
||||
h.DeleteNotify(to)
|
||||
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) listSend(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]
|
||||
func (c Command) Run(from string, args []string) string {
|
||||
if len(args) > 0 {
|
||||
cmdName := args[0]
|
||||
if cmdName == "help" {
|
||||
return c.Help()
|
||||
}
|
||||
}
|
||||
for _, host := range b.db.Hosts {
|
||||
toList := ""
|
||||
show := all
|
||||
for _, to := range host.Notifies {
|
||||
if all {
|
||||
toList = fmt.Sprintf("%s , %s", toList, to)
|
||||
} else if to == of {
|
||||
show = true
|
||||
if len(c.Commands) > 0 {
|
||||
for _, cmd := range c.Commands {
|
||||
if cmd.Name == cmdName {
|
||||
return cmd.Run(from, args[1:])
|
||||
}
|
||||
}
|
||||
}
|
||||
if !show {
|
||||
continue
|
||||
}
|
||||
if len(toList) > 3 {
|
||||
toList = toList[3:]
|
||||
}
|
||||
if host.Name != "" {
|
||||
msg = fmt.Sprintf("%s%s (%s): %s\n", msg, host.Address, host.Name, toList)
|
||||
} else {
|
||||
msg = fmt.Sprintf("%s%s: %s\n", msg, host.Address, toList)
|
||||
return fmt.Sprintf("command %s not found\n%s", cmdName, c.Help())
|
||||
}
|
||||
}
|
||||
|
||||
answer(msg)
|
||||
if c.Action != nil {
|
||||
return c.Action(from, args)
|
||||
}
|
||||
return c.Help()
|
||||
}
|
||||
|
||||
// add hostname
|
||||
func (b *Bot) addHostname(answer func(string), from string, params []string) {
|
||||
if len(params) < 2 {
|
||||
answer("invalid: CMD IPAddress/Hostname NewHostname")
|
||||
return
|
||||
}
|
||||
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))
|
||||
}
|
||||
|
||||
func (b *Bot) delHostname(answer func(string), from string, params []string) {
|
||||
if len(params) < 2 {
|
||||
answer("invalid: CMD IPAddress/Hostname")
|
||||
return
|
||||
}
|
||||
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))
|
||||
func (c Command) Help() string {
|
||||
if len(c.Commands) > 0 {
|
||||
msg := fmt.Sprintf("%s\n-------------------", c.Description)
|
||||
for _, cmd := range c.Commands {
|
||||
msg = fmt.Sprintf("%s\n%s: %s", msg, cmd.Name, cmd.Description)
|
||||
}
|
||||
} else {
|
||||
answer("could not found host")
|
||||
return msg
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// 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 _, 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]
|
||||
}
|
||||
if notify, ok := b.db.NotifiesByAddress[of]; ok {
|
||||
msg = fmt.Sprintf("%s %s is %s", msg, of, notify.MaxPrioIn.String())
|
||||
}
|
||||
}
|
||||
answer(msg)
|
||||
}
|
||||
|
||||
// set a filter to a max
|
||||
func (b *Bot) setMaxfilter(answer func(string), from string, params []string) {
|
||||
if len(params) < 1 {
|
||||
answer("invalid: CMD Priority\n or\n CMD Channel 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 Channel Priority")
|
||||
return
|
||||
}
|
||||
n, ok := b.db.NotifiesByAddress[to]
|
||||
if !ok {
|
||||
n = b.db.NewNotify(to)
|
||||
}
|
||||
|
||||
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" {
|
||||
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]
|
||||
}
|
||||
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
|
||||
}
|
||||
of := from
|
||||
regex := params[0]
|
||||
if len(params) > 1 {
|
||||
of = params[0]
|
||||
regex = params[1]
|
||||
}
|
||||
|
||||
n := b.db.NotifiesByAddress[of]
|
||||
if err := n.AddRegex(regex); err == nil {
|
||||
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
|
||||
}
|
||||
of := from
|
||||
regex := params[0]
|
||||
if len(params) > 1 {
|
||||
of = params[0]
|
||||
regex = params[1]
|
||||
}
|
||||
n := b.db.NotifiesByAddress[of]
|
||||
delete(n.RegexIn, regex)
|
||||
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{})
|
||||
return c.Description
|
||||
}
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"dev.sum7.eu/genofire/logmania/database"
|
||||
)
|
||||
|
||||
func NewFilter(db *database.DB) *Command {
|
||||
return &Command{
|
||||
Name: "filter",
|
||||
Description: "list and configurate regex filter for channel by message content",
|
||||
Commands: []*Command{
|
||||
&Command{
|
||||
Name: "add",
|
||||
Description: "add regex filter for channel: [channel] regex",
|
||||
Action: func(from string, params []string) string {
|
||||
if len(params) < 1 {
|
||||
return "invalid: [channel] regex"
|
||||
}
|
||||
of := from
|
||||
regex := params[0]
|
||||
if len(params) > 1 {
|
||||
of = params[0]
|
||||
regex = params[1]
|
||||
}
|
||||
|
||||
n := db.NotifiesByAddress[of]
|
||||
if err := n.AddRegex(regex); err != nil {
|
||||
return fmt.Sprintf("\"%s\" is no valid regex expression: %s", regex, err)
|
||||
}
|
||||
return fmt.Sprintf("add regex for \"%s\" to %s", of, regex)
|
||||
},
|
||||
},
|
||||
&Command{
|
||||
Name: "del",
|
||||
Description: "del regex filter for channel: [channel] regex",
|
||||
Action: func(from string, params []string) string {
|
||||
if len(params) < 1 {
|
||||
return "invalid: [channel] regex"
|
||||
}
|
||||
of := from
|
||||
regex := params[0]
|
||||
if len(params) > 1 {
|
||||
of = params[0]
|
||||
regex = params[1]
|
||||
}
|
||||
n := db.NotifiesByAddress[of]
|
||||
delete(n.RegexIn, regex)
|
||||
return "deleted"
|
||||
},
|
||||
},
|
||||
&Command{
|
||||
Name: "all",
|
||||
Description: "list of all channels",
|
||||
Action: func(from string, params []string) string {
|
||||
msg := "filter:\n"
|
||||
for _, n := range 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)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
},
|
||||
},
|
||||
&Command{
|
||||
Name: "channel",
|
||||
Description: "list of given channel: channel",
|
||||
Action: func(from string, params []string) string {
|
||||
msg := "filter:\n"
|
||||
if len(params) != 1 {
|
||||
return "invalid: no channel given"
|
||||
}
|
||||
of := params[0]
|
||||
if n, ok := 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)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
},
|
||||
},
|
||||
},
|
||||
Action: func(from string, params []string) string {
|
||||
msg := "filter:\n"
|
||||
if n, ok := db.NotifiesByAddress[from]; ok {
|
||||
msg = fmt.Sprintf("%s%s\n-------------\n", msg, from)
|
||||
for expression := range n.RegexIn {
|
||||
msg = fmt.Sprintf("%s - %s\n", msg, expression)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
},
|
||||
}
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"dev.sum7.eu/genofire/logmania/database"
|
||||
timeago "github.com/ararog/timeago"
|
||||
)
|
||||
|
||||
func NewHostname(db *database.DB) *Command {
|
||||
return &Command{
|
||||
Name: "hostname",
|
||||
Description: "alternative short (host)names for long IP-Addresses or URLs (and time of last recieved input)",
|
||||
Commands: []*Command{
|
||||
&Command{
|
||||
Name: "set",
|
||||
Description: "set or replace a hostname: IPAddress/Hostname NewHostname",
|
||||
Action: func(from string, params []string) string {
|
||||
if len(params) != 2 {
|
||||
return "invalid: IPAddress/Hostname NewHostname"
|
||||
}
|
||||
addr := params[0]
|
||||
name := params[1]
|
||||
|
||||
h := db.GetHost(addr)
|
||||
if h == nil {
|
||||
h = db.NewHost(addr)
|
||||
}
|
||||
db.ChangeHostname(h, name)
|
||||
|
||||
return fmt.Sprintf("set for %s the hostname %s", addr, name)
|
||||
},
|
||||
},
|
||||
&Command{
|
||||
Name: "del",
|
||||
Description: "delete a hostname: IPAddress/Hostname",
|
||||
Action: func(from string, params []string) string {
|
||||
if len(params) != 1 {
|
||||
return "invalid: IPAddress/Hostname"
|
||||
}
|
||||
addr := params[0]
|
||||
h := db.GetHost(addr)
|
||||
if h != nil {
|
||||
db.DeleteHost(h)
|
||||
if h.Name != "" {
|
||||
return fmt.Sprintf("remove host %s with hostname %s", h.Address, h.Name)
|
||||
}
|
||||
return fmt.Sprintf("remove host %s", h.Address)
|
||||
}
|
||||
return "could not found host"
|
||||
},
|
||||
},
|
||||
},
|
||||
Action: func(from string, params []string) string {
|
||||
msg := "hostnames:\n"
|
||||
for _, host := range 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)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
},
|
||||
}
|
||||
}
|
58
bot/main.go
58
bot/main.go
|
@ -1,54 +1,36 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"github.com/mattn/go-shellwords"
|
||||
|
||||
"dev.sum7.eu/genofire/logmania/database"
|
||||
)
|
||||
|
||||
type Bot struct {
|
||||
db *database.DB
|
||||
commandsMap map[string]commandFunc
|
||||
commands []string
|
||||
Command
|
||||
}
|
||||
|
||||
func NewBot(db *database.DB) *Bot {
|
||||
b := &Bot{
|
||||
db: db,
|
||||
}
|
||||
b.commandsMap = map[string]commandFunc{
|
||||
"help": b.help,
|
||||
"send-add": b.addSend,
|
||||
"send-list": b.listSend,
|
||||
"send-del": b.delSend,
|
||||
"hostname-set": b.addHostname,
|
||||
"hostname-list": b.listHostname,
|
||||
"hostname-del": b.delHostname,
|
||||
"filter-set": b.setMaxfilter,
|
||||
"filter-list": b.listMaxfilter,
|
||||
"regex-add": b.addRegex,
|
||||
"regex-list": b.listRegex,
|
||||
"regex-del": b.delRegex,
|
||||
"replace-add": b.addRegexReplace,
|
||||
"replace-list": b.listRegexReplace,
|
||||
"replace-del": b.delRegexReplace,
|
||||
}
|
||||
for k := range b.commandsMap {
|
||||
b.commands = append(b.commands, k)
|
||||
}
|
||||
return b
|
||||
return &Bot{Command{
|
||||
Description: "logmania bot, to configurate live all settings",
|
||||
Commands: []*Command{
|
||||
NewFilter(db),
|
||||
NewHostname(db),
|
||||
NewPriority(db),
|
||||
NewReplace(db),
|
||||
NewSend(db),
|
||||
},
|
||||
}}
|
||||
}
|
||||
|
||||
func (b *Bot) Handle(answer func(string), from, msg string) {
|
||||
msgParts := strings.Split(msg, " ")
|
||||
if len(msgParts[0]) <= 0 || msgParts[0][0] != '.' {
|
||||
return
|
||||
func (b *Bot) Handle(from, msg string) string {
|
||||
msgParts, err := shellwords.Parse(msg)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
cmdName := msgParts[0][1:]
|
||||
if cmd, ok := b.commandsMap[cmdName]; ok {
|
||||
cmd(answer, from, msgParts[1:])
|
||||
} else {
|
||||
answer(fmt.Sprintf("not found command: !%s", cmdName))
|
||||
if len(msgParts) <= 0 || msgParts[0][0] != '.' {
|
||||
return ""
|
||||
}
|
||||
msgParts[0] = msgParts[0][1:]
|
||||
return b.Run(from, msgParts)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"dev.sum7.eu/genofire/logmania/database"
|
||||
)
|
||||
|
||||
func NewPriority(db *database.DB) *Command {
|
||||
return &Command{
|
||||
Name: "priority",
|
||||
Description: "list and configurate priority in channel",
|
||||
Commands: []*Command{
|
||||
&Command{
|
||||
Name: "set",
|
||||
Description: "set max priority of channel: [channel] Priority",
|
||||
Action: func(from string, params []string) string {
|
||||
if len(params) < 1 {
|
||||
return "invalid: [channel] Priority"
|
||||
}
|
||||
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 {
|
||||
return "invalid: [Channel] Priority"
|
||||
}
|
||||
n, ok := db.NotifiesByAddress[to]
|
||||
if !ok {
|
||||
n = db.NewNotify(to)
|
||||
}
|
||||
|
||||
n.MaxPrioIn = max
|
||||
|
||||
return fmt.Sprintf("set filter for %s to %s", to, max.String())
|
||||
},
|
||||
},
|
||||
&Command{
|
||||
Name: "all",
|
||||
Description: "list of all channels",
|
||||
Action: func(from string, params []string) string {
|
||||
msg := "priority: \n"
|
||||
for _, n := range db.Notifies {
|
||||
msg = fmt.Sprintf("%s%s - %s\n", msg, n.Address(), n.MaxPrioIn.String())
|
||||
}
|
||||
return msg
|
||||
},
|
||||
},
|
||||
&Command{
|
||||
Name: "channel",
|
||||
Description: "list of given channel: channel",
|
||||
Action: func(from string, params []string) string {
|
||||
if len(params) != 1 {
|
||||
return "invalid: no channel given"
|
||||
}
|
||||
of := params[0]
|
||||
msg := "priority: \n"
|
||||
if notify, ok := db.NotifiesByAddress[of]; ok {
|
||||
msg = fmt.Sprintf("%s %s is %s", msg, of, notify.MaxPrioIn.String())
|
||||
}
|
||||
return msg
|
||||
},
|
||||
},
|
||||
},
|
||||
Action: func(from string, params []string) string {
|
||||
msg := "priority: \n"
|
||||
if notify, ok := db.NotifiesByAddress[from]; ok {
|
||||
msg = fmt.Sprintf("%s %s is %s", msg, from, notify.MaxPrioIn.String())
|
||||
}
|
||||
return msg
|
||||
},
|
||||
}
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"dev.sum7.eu/genofire/logmania/database"
|
||||
)
|
||||
|
||||
func NewReplace(db *database.DB) *Command {
|
||||
return &Command{
|
||||
Name: "replace",
|
||||
Description: "list and configurate replace content of message for channel",
|
||||
Commands: []*Command{
|
||||
&Command{
|
||||
Name: "add",
|
||||
Description: "add regex replace for channel: [channel] regex replace",
|
||||
Action: func(from string, params []string) string {
|
||||
if len(params) < 1 {
|
||||
return "invalid: [channel] regex replace"
|
||||
}
|
||||
of := from
|
||||
regex := params[0]
|
||||
value := params[1]
|
||||
if len(params) > 2 {
|
||||
of = params[0]
|
||||
regex = params[1]
|
||||
value = params[2]
|
||||
}
|
||||
|
||||
n := db.NotifiesByAddress[of]
|
||||
if err := n.AddRegexReplace(regex, value); err != nil {
|
||||
return fmt.Sprintf("\"%s\" to \"%s\" is no valid regex replace expression: %s", regex, value, err)
|
||||
}
|
||||
return fmt.Sprintf("add replace in \"%s\" for \"%s\" to \"%s\"", of, regex, value)
|
||||
},
|
||||
},
|
||||
&Command{
|
||||
Name: "del",
|
||||
Description: "del regex replace for channel: [channel] regex replace",
|
||||
Action: func(from string, params []string) string {
|
||||
if len(params) < 1 {
|
||||
return "invalid: [channel] regex replace"
|
||||
}
|
||||
of := from
|
||||
regex := params[0]
|
||||
if len(params) > 1 {
|
||||
of = params[0]
|
||||
regex = params[1]
|
||||
}
|
||||
n := db.NotifiesByAddress[of]
|
||||
|
||||
delete(n.RegexReplace, regex)
|
||||
return "deleted"
|
||||
},
|
||||
},
|
||||
|
||||
&Command{
|
||||
Name: "all",
|
||||
Description: "list of all channels",
|
||||
Action: func(from string, params []string) string {
|
||||
msg := "replaces:\n"
|
||||
for _, n := range 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)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
},
|
||||
},
|
||||
&Command{
|
||||
Name: "channel",
|
||||
Description: "list of given channel: channel",
|
||||
Action: func(from string, params []string) string {
|
||||
if len(params) != 1 {
|
||||
return "invalid: no channel given"
|
||||
}
|
||||
of := params[0]
|
||||
msg := "replaces:\n"
|
||||
if n, ok := 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)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
},
|
||||
},
|
||||
},
|
||||
Action: func(from string, params []string) string {
|
||||
msg := "replaces:\n"
|
||||
if n, ok := db.NotifiesByAddress[from]; ok {
|
||||
msg = fmt.Sprintf("%s%s\n-------------\n", msg, from)
|
||||
for expression, value := range n.RegexReplace {
|
||||
msg = fmt.Sprintf("%s - \"%s\" : \"%s\"\n", msg, expression, value)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
},
|
||||
}
|
||||
}
|
|
@ -0,0 +1,142 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"dev.sum7.eu/genofire/logmania/database"
|
||||
)
|
||||
|
||||
func NewSend(db *database.DB) *Command {
|
||||
return &Command{
|
||||
Name: "send",
|
||||
Description: "list and configurate destination for hostnames",
|
||||
Commands: []*Command{
|
||||
&Command{
|
||||
Name: "add",
|
||||
Description: "add a destination for host with: IPAddress/Hostname [to]",
|
||||
Action: func(from string, params []string) string {
|
||||
if len(params) < 1 {
|
||||
return "invalid: IPAddress/Hostname [to]"
|
||||
}
|
||||
host := params[0]
|
||||
to := from
|
||||
if len(params) > 1 {
|
||||
to = params[1]
|
||||
}
|
||||
|
||||
h := db.GetHost(host)
|
||||
if h == nil {
|
||||
h = db.NewHost(host)
|
||||
}
|
||||
if h == nil {
|
||||
return fmt.Sprintf("could not create host %s", host)
|
||||
}
|
||||
n, ok := db.NotifiesByAddress[to]
|
||||
if !ok {
|
||||
n = db.NewNotify(to)
|
||||
}
|
||||
if n == nil {
|
||||
return fmt.Sprintf("could not create notify %s in list of %s", to, host)
|
||||
}
|
||||
h.AddNotify(n)
|
||||
|
||||
return fmt.Sprintf("added %s in list of %s", to, host)
|
||||
},
|
||||
},
|
||||
&Command{
|
||||
Name: "del",
|
||||
Description: "del a destination for host with: IPAddress/Hostname [to]",
|
||||
Action: func(from string, params []string) string {
|
||||
if len(params) < 1 {
|
||||
return "invalid: IPAddress/Hostname [to]"
|
||||
}
|
||||
host := params[0]
|
||||
to := from
|
||||
if len(params) > 1 {
|
||||
to = params[1]
|
||||
}
|
||||
|
||||
if h := db.GetHost(host); h != nil {
|
||||
h.DeleteNotify(to)
|
||||
return fmt.Sprintf("removed %s in list of %s", to, host)
|
||||
}
|
||||
return "not found host"
|
||||
},
|
||||
},
|
||||
&Command{
|
||||
Name: "all",
|
||||
Description: "list of all hosts with there channels",
|
||||
Action: func(from string, params []string) string {
|
||||
msg := "sending:\n"
|
||||
for _, host := range db.Hosts {
|
||||
toList := ""
|
||||
for _, to := range host.Notifies {
|
||||
toList = fmt.Sprintf("%s , %s", toList, to)
|
||||
}
|
||||
if len(toList) > 3 {
|
||||
toList = toList[3:]
|
||||
}
|
||||
if host.Name != "" {
|
||||
msg = fmt.Sprintf("%s%s (%s): %s\n", msg, host.Address, host.Name, toList)
|
||||
} else {
|
||||
msg = fmt.Sprintf("%s%s: %s\n", msg, host.Address, toList)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
},
|
||||
},
|
||||
&Command{
|
||||
Name: "channel",
|
||||
Description: "list all host of given channel: channel",
|
||||
Action: func(from string, params []string) string {
|
||||
if len(params) != 1 {
|
||||
return "invalid: no channel given"
|
||||
}
|
||||
|
||||
of := params[0]
|
||||
msg := "sending:\n"
|
||||
|
||||
for _, host := range db.Hosts {
|
||||
show := false
|
||||
for _, to := range host.Notifies {
|
||||
if to == of {
|
||||
show = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !show {
|
||||
continue
|
||||
}
|
||||
if host.Name != "" {
|
||||
msg = fmt.Sprintf("%s%s (%s)\n", msg, host.Address, host.Name)
|
||||
} else {
|
||||
msg = fmt.Sprintf("%s%s\n", msg, host.Address)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
},
|
||||
},
|
||||
},
|
||||
Action: func(from string, params []string) string {
|
||||
msg := "sending:\n"
|
||||
for _, host := range db.Hosts {
|
||||
show := false
|
||||
for _, to := range host.Notifies {
|
||||
if to == from {
|
||||
show = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !show {
|
||||
continue
|
||||
}
|
||||
if host.Name != "" {
|
||||
msg = fmt.Sprintf("%s%s (%s)\n", msg, host.Address, host.Name)
|
||||
} else {
|
||||
msg = fmt.Sprintf("%s%s\n", msg, host.Address)
|
||||
}
|
||||
}
|
||||
return msg
|
||||
},
|
||||
}
|
||||
}
|
|
@ -47,9 +47,10 @@ func Init(configInterface interface{}, db *database.DB, bot *bot.Bot) output.Out
|
|||
logger.Warnf("receive unknown websocket message: %s", msg.Subject)
|
||||
continue
|
||||
}
|
||||
bot.Handle(func(answer string) {
|
||||
answer := bot.Handle("", msg.Body.(string))
|
||||
if answer != "" {
|
||||
msg.Answer("bot", answer)
|
||||
}, "", msg.Body.(string))
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -119,20 +119,22 @@ func Init(configInterface interface{}, db *database.DB, bot *bot.Bot) output.Out
|
|||
from = proto + ":" + from
|
||||
}
|
||||
|
||||
bot.Handle(func(answer string) {
|
||||
to := msg.From
|
||||
if msg.Type == xmpp.MessageTypeGroupchat && !to.IsBare() {
|
||||
to = to.Bare()
|
||||
}
|
||||
err := client.Send(&xmpp.MessageClient{
|
||||
Type: msg.Type,
|
||||
To: to,
|
||||
Body: answer,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("xmpp to ", msg.From.String(), " error:", err)
|
||||
}
|
||||
}, from, msg.Body)
|
||||
answer := bot.Handle(from, msg.Body)
|
||||
if answer == "" {
|
||||
continue
|
||||
}
|
||||
to := msg.From
|
||||
if msg.Type == xmpp.MessageTypeGroupchat && !to.IsBare() {
|
||||
to = to.Bare()
|
||||
}
|
||||
err := client.Send(&xmpp.MessageClient{
|
||||
Type: msg.Type,
|
||||
To: to,
|
||||
Body: answer,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("xmpp to ", msg.From.String(), " error:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
|
Loading…
Reference in New Issue