Debian Codegraph @

modules/events_stream/events_triggers.go

raw symbols SCIP
1 package events_stream
2
3 import (
4 "github.com/bettercap/bettercap/session"
5
6 "github.com/evilsocket/islazy/tui"
7 )
8
9 func (mod *EventsStream) addTrigger(tag string, command string) error {
10 if err, id := mod.triggerList.Add(tag, command); err != nil {
11 return err
12 } else {
13 mod.Info("trigger for event %s added with identifier '%s'", tui.Green(tag), tui.Bold(id))
14 }
15 return nil
16 }
17
18 func (mod *EventsStream) clearTrigger(id string) error {
19 if err := mod.triggerList.Del(id); err != nil {
20 return err
21 }
22 return nil
23 }
24
25 func (mod *EventsStream) showTriggers() error {
26 colNames := []string{
27 "ID",
28 "Event",
29 "Action",
30 }
31 rows := [][]string{}
32
33 mod.triggerList.Each(func(id string, t Trigger) {
34 rows = append(rows, []string{
35 tui.Bold(id),
36 tui.Green(t.For),
37 t.Action,
38 })
39 })
40
41 if len(rows) > 0 {
42 tui.Table(mod.Session.Events.Stdout, colNames, rows)
43 mod.Session.Refresh()
44 }
45
46 return nil
47 }
48
49 func (mod *EventsStream) dispatchTriggers(e session.Event) {
50 if id, cmds, err, found := mod.triggerList.Dispatch(e); err != nil {
51 mod.Error("error while dispatching event %s: %v", e.Tag, err)
52 } else if found {
53 mod.Debug("running trigger %s (cmds:'%s') for event %v", id, cmds, e)
54 for _, cmd := range session.ParseCommands(cmds) {
55 if err := mod.Session.Run(cmd); err != nil {
56 mod.Error("%s", err.Error())
57 }
58 }
59 }
60 }