Debian Codegraph @

modules/events_stream/trigger_list.go

raw symbols SCIP
1 package events_stream
2
3 import (
4 "encoding/json"
5 "fmt"
6 "regexp"
7 "strings"
8 "sync"
9
10 "github.com/bettercap/bettercap/session"
11
12 "github.com/antchfx/jsonquery"
13 "github.com/evilsocket/islazy/str"
14 "github.com/evilsocket/islazy/tui"
15 )
16
17 var reQueryCapture = regexp.MustCompile(`{{([^}]+)}}`)
18
19 type Trigger struct {
20 For string
21 Action string
22 }
23
24 type TriggerList struct {
25 sync.Mutex
26 triggers map[string]Trigger
27 }
28
29 func NewTriggerList() *TriggerList {
30 return &TriggerList{
31 triggers: make(map[string]Trigger),
32 }
33 }
34
35 func (l *TriggerList) Add(tag string, command string) (error, string) {
36 l.Lock()
37 defer l.Unlock()
38
39 idNum := 0
40 command = str.Trim(command)
41
42 for id, t := range l.triggers {
43 if t.For == tag {
44 if t.Action == command {
45 return fmt.Errorf("duplicate: trigger '%s' found for action '%s'", tui.Bold(id), command), ""
46 }
47 idNum++
48 }
49 }
50
51 id := fmt.Sprintf("%s-%d", tag, idNum)
52 l.triggers[id] = Trigger{
53 For: tag,
54 Action: command,
55 }
56
57 return nil, id
58 }
59
60 func (l *TriggerList) Del(id string) (err error) {
61 l.Lock()
62 defer l.Unlock()
63 if _, found := l.triggers[id]; found {
64 delete(l.triggers, id)
65 } else {
66 err = fmt.Errorf("trigger '%s' not found", tui.Bold(id))
67 }
68 return err
69 }
70
71 func (l *TriggerList) Each(cb func(id string, t Trigger)) {
72 l.Lock()
73 defer l.Unlock()
74 for id, t := range l.triggers {
75 cb(id, t)
76 }
77 }
78
79 func (l *TriggerList) Completer(prefix string) []string {
80 ids := []string{}
81 l.Each(func(id string, t Trigger) {
82 if prefix == "" || strings.HasPrefix(id, prefix) {
83 ids = append(ids, id)
84 }
85 })
86 return ids
87 }
88
89 func (l *TriggerList) Dispatch(e session.Event) (ident string, cmd string, err error, found bool) {
90 l.Lock()
91 defer l.Unlock()
92
93 for id, t := range l.triggers {
94 if e.Tag == t.For {
95 // this is ugly but it's also the only way to allow
96 // the user to do this easily - since each event Data
97 // field is an interface and type casting is not possible
98 // via golang default text/template system, we transform
99 // the field to JSON, parse it again and then allow the
100 // user to access it in the command via JSON-Query, example:
101 //
102 // events.on wifi.client.new "wifi.deauth {{Client\mac}}"
103 cmd = t.Action
104 found = true
105 ident = id
106 buf := ([]byte)(nil)
107 doc := (*jsonquery.Node)(nil)
108 // parse each {EXPR}
109 for _, m := range reQueryCapture.FindAllString(t.Action, -1) {
110 // parse the event Data field as a JSON objects once
111 if doc == nil {
112 if buf, err = json.Marshal(e.Data); err != nil {
113 err = fmt.Errorf("error while encoding event for trigger %s: %v", tui.Bold(id), err)
114 return
115 } else if doc, err = jsonquery.Parse(strings.NewReader(string(buf))); err != nil {
116 err = fmt.Errorf("error while parsing event for trigger %s: %v", tui.Bold(id), err)
117 return
118 }
119 }
120 // {EXPR} -> EXPR
121 expr := strings.Trim(m, "{}")
122 // use EXPR as a JSON query
123 if node := jsonquery.FindOne(doc, expr); node != nil {
124 cmd = strings.Replace(cmd, m, node.InnerText(), -1)
125 } else {
126 err = fmt.Errorf(
127 "error while parsing expressionfor trigger %s: '%s' doesn't resolve any object: %v",
128 tui.Bold(id),
129 expr,
130 err,
131 )
132 return
133 }
134 }
135
136 return
137 }
138 }
139
140 return
141 }