[TASK] refactory Bot
This commit is contained in:
		
							parent
							
								
									5f9ad29483
								
							
						
					
					
						commit
						5c906df45d
					
				
							
								
								
									
										347
									
								
								bot/command.go
								
								
								
								
							
							
						
						
									
										347
									
								
								bot/command.go
								
								
								
								
							| 
						 | 
					@ -2,340 +2,45 @@ package bot
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"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
 | 
					type Command struct {
 | 
				
			||||||
func (b *Bot) help(answer func(string), from string, params []string) {
 | 
						Name        string
 | 
				
			||||||
	msg := fmt.Sprintf("Hi %s there are the following commands:\n", from)
 | 
						Description string
 | 
				
			||||||
	for _, cmd := range b.commands {
 | 
						Commands    []*Command
 | 
				
			||||||
		msg = fmt.Sprintf("%s - .%s\n", msg, cmd)
 | 
						Action      commandFunc
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	answer(msg)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// add a chat to send log to a chat
 | 
					func (c Command) Run(from string, args []string) string {
 | 
				
			||||||
func (b *Bot) addSend(answer func(string), from string, params []string) {
 | 
						if len(args) > 0 {
 | 
				
			||||||
	if len(params) < 1 {
 | 
							cmdName := args[0]
 | 
				
			||||||
		answer("invalid: CMD IPAddress/Hostname\n or\n CMD IPAddress/Hostname to")
 | 
							if cmdName == "help" {
 | 
				
			||||||
		return
 | 
								return c.Help()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	host := params[0]
 | 
							if len(c.Commands) > 0 {
 | 
				
			||||||
	to := from
 | 
								for _, cmd := range c.Commands {
 | 
				
			||||||
	if len(params) > 1 {
 | 
									if cmd.Name == cmdName {
 | 
				
			||||||
		to = params[1]
 | 
										return cmd.Run(from, args[1:])
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	h := b.db.GetHost(host)
 | 
					 | 
				
			||||||
	if h == nil {
 | 
					 | 
				
			||||||
		h = b.db.NewHost(host)
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
	if h == nil {
 | 
								return fmt.Sprintf("command %s not found\n%s", cmdName, c.Help())
 | 
				
			||||||
		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 {
 | 
						if c.Action != nil {
 | 
				
			||||||
		answer(fmt.Sprintf("could not create notify %s in list of %s", to, host))
 | 
							return c.Action(from, args)
 | 
				
			||||||
		return
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	h.AddNotify(n)
 | 
						return c.Help()
 | 
				
			||||||
 | 
					 | 
				
			||||||
	answer(fmt.Sprintf("added %s in list of %s", to, host))
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//add a chat to send log to a chat
 | 
					func (c Command) Help() string {
 | 
				
			||||||
func (b *Bot) delSend(answer func(string), from string, params []string) {
 | 
						if len(c.Commands) > 0 {
 | 
				
			||||||
	if len(params) < 1 {
 | 
							msg := fmt.Sprintf("%s\n-------------------", c.Description)
 | 
				
			||||||
		answer("invalid: CMD IPAddress/Hostname\n or\n CMD IPAddress/Hostname to")
 | 
							for _, cmd := range c.Commands {
 | 
				
			||||||
		return
 | 
								msg = fmt.Sprintf("%s\n%s: %s", msg, cmd.Name, cmd.Description)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	host := params[0]
 | 
							return msg
 | 
				
			||||||
	to := from
 | 
					 | 
				
			||||||
	if len(params) > 1 {
 | 
					 | 
				
			||||||
		to = params[1]
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						return c.Description
 | 
				
			||||||
	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]
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	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 !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)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	answer(msg)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 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))
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		answer("could not found host")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// 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{})
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
					package bot
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"github.com/mattn/go-shellwords"
 | 
				
			||||||
	"strings"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"dev.sum7.eu/genofire/logmania/database"
 | 
						"dev.sum7.eu/genofire/logmania/database"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Bot struct {
 | 
					type Bot struct {
 | 
				
			||||||
	db          *database.DB
 | 
						Command
 | 
				
			||||||
	commandsMap map[string]commandFunc
 | 
					 | 
				
			||||||
	commands    []string
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewBot(db *database.DB) *Bot {
 | 
					func NewBot(db *database.DB) *Bot {
 | 
				
			||||||
	b := &Bot{
 | 
						return &Bot{Command{
 | 
				
			||||||
		db: db,
 | 
							Description: "logmania bot, to configurate live all settings",
 | 
				
			||||||
	}
 | 
							Commands: []*Command{
 | 
				
			||||||
	b.commandsMap = map[string]commandFunc{
 | 
								NewFilter(db),
 | 
				
			||||||
		"help":          b.help,
 | 
								NewHostname(db),
 | 
				
			||||||
		"send-add":      b.addSend,
 | 
								NewPriority(db),
 | 
				
			||||||
		"send-list":     b.listSend,
 | 
								NewReplace(db),
 | 
				
			||||||
		"send-del":      b.delSend,
 | 
								NewSend(db),
 | 
				
			||||||
		"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
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (b *Bot) Handle(answer func(string), from, msg string) {
 | 
					func (b *Bot) Handle(from, msg string) string {
 | 
				
			||||||
	msgParts := strings.Split(msg, " ")
 | 
						msgParts, err := shellwords.Parse(msg)
 | 
				
			||||||
	if len(msgParts[0]) <= 0 || msgParts[0][0] != '.' {
 | 
						if err != nil {
 | 
				
			||||||
		return
 | 
							return ""
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	cmdName := msgParts[0][1:]
 | 
						if len(msgParts) <= 0 || msgParts[0][0] != '.' {
 | 
				
			||||||
	if cmd, ok := b.commandsMap[cmdName]; ok {
 | 
							return ""
 | 
				
			||||||
		cmd(answer, from, msgParts[1:])
 | 
					 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		answer(fmt.Sprintf("not found command: !%s", cmdName))
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						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)
 | 
									logger.Warnf("receive unknown websocket message: %s", msg.Subject)
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			bot.Handle(func(answer string) {
 | 
								answer := bot.Handle("", msg.Body.(string))
 | 
				
			||||||
 | 
								if answer != "" {
 | 
				
			||||||
				msg.Answer("bot", answer)
 | 
									msg.Answer("bot", answer)
 | 
				
			||||||
			}, "", msg.Body.(string))
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -119,7 +119,10 @@ func Init(configInterface interface{}, db *database.DB, bot *bot.Bot) output.Out
 | 
				
			||||||
					from = proto + ":" + from
 | 
										from = proto + ":" + from
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				bot.Handle(func(answer string) {
 | 
									answer := bot.Handle(from, msg.Body)
 | 
				
			||||||
 | 
									if answer == "" {
 | 
				
			||||||
 | 
										continue
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
				to := msg.From
 | 
									to := msg.From
 | 
				
			||||||
				if msg.Type == xmpp.MessageTypeGroupchat && !to.IsBare() {
 | 
									if msg.Type == xmpp.MessageTypeGroupchat && !to.IsBare() {
 | 
				
			||||||
					to = to.Bare()
 | 
										to = to.Bare()
 | 
				
			||||||
| 
						 | 
					@ -132,7 +135,6 @@ func Init(configInterface interface{}, db *database.DB, bot *bot.Bot) output.Out
 | 
				
			||||||
				if err != nil {
 | 
									if err != nil {
 | 
				
			||||||
					logger.Error("xmpp to ", msg.From.String(), " error:", err)
 | 
										logger.Error("xmpp to ", msg.From.String(), " error:", err)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				}, from, msg.Body)
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}()
 | 
						}()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue