new: added events.on (and other related commands) to trigger specific actions when an events happens

This commit is contained in:
evilsocket 2019-02-24 20:21:18 +01:00
parent 78c341c2b3
commit 1492bf5e40
No known key found for this signature in database
GPG key ID: 1564D7F30393A456
24 changed files with 4475 additions and 0 deletions

View file

@ -0,0 +1,62 @@
package events_stream
import (
"os"
"github.com/bettercap/bettercap/session"
"github.com/evilsocket/islazy/tui"
)
func (mod *EventsStream) addTrigger(tag string, command string) error {
if err, id := mod.triggerList.Add(tag, command); err != nil {
return err
} else {
mod.Info("trigger for event %s added with identifier '%s'", tui.Green(tag), tui.Bold(id))
}
return nil
}
func (mod *EventsStream) clearTrigger(id string) error {
if err := mod.triggerList.Del(id); err != nil {
return err
}
return nil
}
func (mod *EventsStream) showTriggers() error {
colNames := []string{
"ID",
"Event",
"Action",
}
rows := [][]string{}
mod.triggerList.Each(func(id string, t Trigger) {
rows = append(rows, []string{
tui.Bold(id),
tui.Green(t.For),
t.Action,
})
})
if len(rows) > 0 {
tui.Table(os.Stdout, colNames, rows)
mod.Session.Refresh()
}
return nil
}
func (mod *EventsStream) dispatchTriggers(e session.Event) {
if id, cmds, err, found := mod.triggerList.Dispatch(e); err != nil {
mod.Error("error while dispatching event %s: %v", e.Tag, err)
} else if found {
mod.Debug("running trigger %s (cmds:'%s') for event %v", id, cmds, e)
for _, cmd := range session.ParseCommands(cmds) {
if err := mod.Session.Run(cmd); err != nil {
mod.Error("%s", err.Error())
}
}
}
}