-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
226 lines (199 loc) · 4.91 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"fmt"
"github.com/nlopes/slack"
"html"
"math/rand"
"os"
"strings"
"time"
)
var debug = false
func getenv(name string) string {
v := os.Getenv(name)
if v == "" {
panic("missing required environment variable " + name)
}
return v
}
// Debug enables or disables debug mode
func Debug(state bool) {
debug = state
}
// ContainsLowercase signals if a given string endWith lowercase, ignoring Slack @mentions, #channels, URLs and :emoji:
func ContainsLowercase(m string) bool {
// Split string into chars
chars := strings.Split(m, "")
l := len(chars)
out := ""
before := ""
next := ""
// State
channel := -1
mention := -1
url := -1
emoji := -1
for i, char := range chars {
if i < l-1 {
next = chars[i+1]
}
switch char {
// #channel
case "#":
if url != -1 {
break
}
if emoji != -1 {
// Reset emoji mode and append characters since emoji begin was encountered
out += m[emoji:i]
emoji = -1
break
}
if before == "" || before == " " {
channel = i
} else {
out += "#"
}
// @mention
case "@":
if url != -1 {
break
}
if emoji != -1 {
// Reset emoji mode and append characters since emoji begin was encountered
out += m[emoji:i]
emoji = -1
break
}
if before == "" || before == " " {
mention = i
} else {
out += "@"
}
// URL
case "h":
if channel != -1 || mention != -1 || url != -1 || emoji != -1 {
break
}
if (before == "" || before == " ") && (l-i > 7 && strings.Join(chars[i:i+7], "") == "http://" || l-i > 8 && strings.Join(chars[i:i+8], "") == "https://") {
url = i
} else {
out += "h"
}
// :emoji:
case ":":
if url != -1 {
break
}
if channel != -1 {
// Reset channel mode and append characters since channel begin was encountered
out += m[channel:i]
channel = -1
break
}
if mention != -1 {
// Reset mention mode and append characters since mention begin was encountered
out += m[mention:i]
mention = -1
break
}
if emoji != -1 && next != ":" && before != ":" {
emoji = -1
break
}
if before == "" || before == " " {
emoji = i
} else {
out += ":"
}
// Terminate when seeing a space
case " ":
if channel != -1 || mention != -1 || url != -1 || emoji != -1 {
channel = -1
mention = -1
url = -1
emoji = -1
}
out += " "
default:
if !(channel != -1 || mention != -1 || url != -1 || emoji != -1) {
out += char
}
}
if debug {
fmt.Printf("'%s' '%s' '%s' (%d %d %d %d) %d %d %d\n", char, before, out, channel, mention, url, emoji, i, l, l-i)
}
before = char
}
if debug {
fmt.Printf("'%s' => '%s'\n", m, out)
}
return strings.ToUpper(out) != out
}
var (
responses = []string{
"%s ACPD %s IS YOUR CAPS LOCK BROKEN? %s\n\n> %s",
"%s ACPD %s CAPS AND REGISTRATION PLEASE! %s\n\n> %s",
"%s ACPD %s PLEASE KEEP YOUR CAPS ON THE LOCK! %s\n\n> %s",
"%s ACPD %s HAVE YOU SEEN THESE CAPS BEFORE? %s\n\n> %s",
}
rl = len(responses)
officers = []string{"male-police-officer", "female-police-officer"}
ol = len(officers)
skinTones = []string{"skin-tone-2", "skin-tone-3", "skin-tone-4", "skin-tone-5", "skin-tone-6"}
sl = len(skinTones)
props = []string{"doughnut", "coffee", "police_car", "oncoming_police_car", "rotating_light"}
pl = len(props)
)
// OfficerMoji returns a random officer from the ACPD
func OfficerMoji() string {
return fmt.Sprintf(":%s::%s:", officers[rand.Intn(ol)], skinTones[rand.Intn(sl)])
}
// Prop returns an ACPD officer's tool of the trade
func Prop() string {
return fmt.Sprintf(":%s:", props[rand.Intn(pl)])
}
// Enforcement wraps a given message into a helpful ACPD response
func Enforcement(m string) string {
// Ignore empty string
if m == "" {
return fmt.Sprintf("%s ACPD %s NOTHING TO SEE HERE, MOVE ALONG CAPS %s", OfficerMoji(), OfficerMoji(), Prop())
}
// Avoid double quoting
if strings.HasPrefix(m, "> ") {
m = m[2:]
}
up := strings.ToUpper(m)
// Replace skin-tone modifier with lowercase
up = strings.Replace(up, ":SKIN-TONE-", ":skin-tone-", -1)
return fmt.Sprintf(responses[rand.Intn(rl)], OfficerMoji(), OfficerMoji(), Prop(), up)
}
func main() {
token := getenv("SLACKTOKEN")
api := slack.New(token)
rtm := api.NewRTM()
rand.Seed(time.Now().UnixNano())
go rtm.ManageConnection()
Loop:
for {
select {
case msg := <-rtm.IncomingEvents:
//fmt.Printf("Event Received: %s %+v\n", msg.Type, msg.Data)
switch ev := msg.Data.(type) {
case *slack.MessageEvent:
info := rtm.GetInfo()
text := html.UnescapeString(ev.Text)
if ev.Msg.User != info.User.ID && ev.SubType == "" && ContainsLowercase(text) {
rtm.SendMessage(rtm.NewOutgoingMessage(Enforcement(text), ev.Channel))
}
case *slack.RTMError:
fmt.Printf("Error: %s\n", ev.Error())
case *slack.InvalidAuthEvent:
fmt.Printf("Invalid credentials")
break Loop
default:
// Take no action
}
}
}
}