/
handler.go
62 lines (58 loc) · 1.62 KB
/
handler.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
package main
import (
"github.com/ArjenSchwarz/igor/config"
"github.com/ArjenSchwarz/igor/plugins"
"github.com/ArjenSchwarz/igor/slack"
)
// handle is the main handling function. It parses the received message and
// ensures that a response is collected.
// It also ensures that the resulting response is properly escaped
func handle(body body) slack.Response {
request := slack.LoadRequestFromQuery(body.Body)
config, err := config.GeneralConfig()
if err != nil {
response := slack.SomethingWrongResponse(request)
response.Escape()
return response
}
response := slack.Response{}
if !request.Validate(config) {
response = slack.ValidationErrorResponse()
} else {
response = determineResponse(request, config)
}
response.Escape()
return response
}
// determineResponse parses the responses from a list of plugin triggers
func determineResponse(request slack.Request, config config.Config) slack.Response {
forcePublic := false
if request.Text[0] == '!' {
forcePublic = true
request.Text = request.Text[1:]
}
pluginlist := plugins.GetPlugins(request, config)
hasError := false
//TODO clean this up
for _, value := range pluginlist {
response, err := value.Work()
if err == nil {
if forcePublic {
response.SetPublic()
}
return response
}
switch err.(type) {
case *plugins.NoMatchError:
default:
// Something actually went wrong with one of the plugins,
// return that something went wrong if nothing matches
// Don't send the actual message though
hasError = true
}
}
if hasError {
return slack.SomethingWrongResponse(request)
}
return slack.NothingFoundResponse(request)
}