forked from priscillachat/priscilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponder.go
147 lines (120 loc) · 3.47 KB
/
responder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"container/list"
"os/exec"
"regexp"
"strconv"
"strings"
)
func triggerActiveResponders(responders *list.List, trimmed, source string,
m *messageBlock, metionMode bool, dispatch chan<- *dispatcherRequest) bool {
for eAr := responders.Front(); eAr != nil; eAr = eAr.Next() {
ar := eAr.Value.(*activeResponderConfig)
if ar.regex.MatchString(trimmed) {
q := &query{
Type: "message",
Source: source,
To: ar.source,
Message: m,
}
logger.Debug.Println("Active responder match for:", ar.source)
dispatch <- &dispatcherRequest{Query: q}
if !ar.matchNext {
return true
}
}
}
return false
}
func triggerPassiveResponders(responders *list.List, message,
source, room, from string, mentionMode bool,
dispatch chan<- *dispatcherRequest) (matched bool) {
ResponderLoop:
for epr := responders.Front(); epr != nil; epr = epr.Next() {
pr := epr.Value.(*passiveResponderConfig)
var patterns []*regexp.Regexp
if mentionMode {
logger.Debug.Println("Using mention pattern")
patterns = pr.mRegex
} else {
logger.Debug.Println("Using regular pattern")
patterns = pr.regex
}
for _, rg := range patterns {
logger.Debug.Println("Trying to match:", pr.Name)
logger.Debug.Println("Pattern:", *rg)
matches := rg.FindAllStringSubmatch(message, 1)
if len(matches) == 0 {
continue
}
match := matches[0]
logger.Debug.Println("Match:", match)
var output []byte
var err error
var subArgs []string
subbed := false
// submatch, may need to substitute
logger.Debug.Println("Match len:", len(match))
logger.Debug.Println("Substitution:", len(pr.substitute))
logger.Debug.Println("Room substitution:", len(pr.roomParam))
if len(match) >= 1 &&
(len(pr.substitute) > 0 || len(pr.roomParam) > 0) {
logger.Debug.Println("Substitution necessary")
subArgs = make([]string, len(pr.Args))
copy(subArgs, pr.Args)
for i, _ := range pr.substitute {
logger.Debug.Println("Try sub:", subArgs[i])
matchIds :=
subRegex.FindAllStringSubmatch(subArgs[i], -1)
logger.Debug.Println("MatchIds:", matchIds)
for _, matchId := range matchIds {
logger.Debug.Println("MatchId:", matchId)
mId, _ := strconv.Atoi(matchId[1])
if mId < len(match)-1 {
logger.Debug.Println("Subbed:", match[mId+1])
subArgs[i] = strings.Replace(subArgs[i],
"__"+matchId[1]+"__", match[mId+1], -1)
subbed = true
}
}
}
for i, _ := range pr.roomParam {
logger.Debug.Println("Room substitution")
subArgs[i] =
strings.Replace(subArgs[i], "__room__", room, -1)
subbed = true
}
}
if subbed {
output, err = exec.Command(pr.Cmd, subArgs...).Output()
} else {
output, err = exec.Command(pr.Cmd, pr.Args...).Output()
}
matched = true
if err != nil {
logger.Error.Println("Passive responder error:", err)
} else {
logger.Debug.Println("Passive responder executed:",
string(output))
request := dispatcherRequest{
Query: &query{
Type: "message",
Source: "Passive Responder: " + pr.Name,
To: source,
Message: &messageBlock{
Message: strings.Trim(string(output), " \n"),
Room: room,
},
},
}
if mentionMode {
request.Query.Message.MentionNotify = []string{from}
}
dispatch <- &request
}
// one regex in the match is good, continue onto next responder
continue ResponderLoop
}
}
return
}