forked from sdslabs/jinora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.coffee
171 lines (143 loc) · 4.77 KB
/
user.coffee
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
if process.env.NODE_ENV != 'production'
dotenv = require 'dotenv'
dotenv.load()
request = require('request')
slack =
msg = {}
nickSessionMap = {}
bannedSessions = []
HEADERS = {
"Content-Type": "application/json",
"Accept": "application/json"
}
reservedNicks = []
# Get json object from RESERVED_NICKS_URL and set the value of reservedNicks
getJsonBlob = () ->
options = {
method: "GET"
url: process.env.RESERVED_NICKS_URL,
headers: HEADERS
}
callback = (error, response, body) ->
if !error and response.statusCode == 200
res = JSON.parse body ;
reservedNicks = res.nicks || []
return request options, callback
updateJsonBlob = () ->
options = {
method: "PUT"
url: process.env.RESERVED_NICKS_URL,
headers: HEADERS
body: JSON.stringify {"nicks": reservedNicks}
}
callback = (error, response, body) ->
if !error and response.statusCode == 200
msg.cmdStatus = true
else
msg.cmdStatus = "error"
msg.message = makeSlackMessage()
slack.postMessage msg.message, process.env.SLACK_CHANNEL, "Jinora"
return request options, callback
# Form custom message to be sent to slack
makeSlackMessage = () ->
if msg.cmdStatus == true
if msg.banStatus[0] == "ban"
msg.message = "#{msg.adminNick} just #{msg.type}-banned #{msg.userNick}"
else if msg.banStatus[0] == "unban"
msg.message = "#{msg.adminNick} just removed #{msg.type}-ban from #{msg.userNick}"
else if msg.cmdStatus == "error"
msg.message = "Error while #{msg.type}-banning #{msg.userNick}"
else if msg.cmdStatus == "invalid"
if msg.banStatus.length == 1
msg.message = "Invalid command for #{msg.banStatus}ning.\n"
else
msg.message = "Invalid command.\n"
msg.message += "Sample commands:\n"
for ban in msg.banStatus
msg.message += "`!#{ban} nick cat` for #{ban}ning nick `cat`\n`!#{ban} user cat` for shadow-#{ban}ning user `cat`\n"
return msg.message
# Verify the nick of user
verifyNick = (nick) ->
verified = () ->
nick = nick.toLowerCase()
for reservedNick in reservedNicks
if nick.indexOf(reservedNick) != -1
return false
return true
return !!nick and if !verified() then false else true
# Verify the session of user
verifyUser = (sessionId) ->
return !!sessionId and if sessionId in bannedSessions then false else true
banFunction = {
nick: {
ban: (nick) ->
banned = () ->
nick = nick.toLowerCase()
reservedNicks.push nick
updateJsonBlob()
return true
return !!nick and (verifyNick(nick) and banned())
unban: (nick) ->
unbanned = () ->
nick = nick.toLowerCase()
if nick in reservedNicks
reservedNicks.splice(reservedNicks.indexOf(nick), 1)
updateJsonBlob()
return true
return !!nick and unbanned()
}
user: {
ban: (nick) ->
banned = () ->
nick = nick.toLowerCase()
if nickSessionMap[nick]
bannedSessions.push nickSessionMap[nick] if nickSessionMap[nick] not in bannedSessions
msg.cmdStatus = true
msg.message = makeSlackMessage()
slack.postMessage msg.message, process.env.SLACK_CHANNEL, "Jinora"
return true
return !!nick and banned()
unban: (nick) ->
unbanned = () ->
nick = nick.toLowerCase()
if nickSessionMap[nick]
bannedSessions.pop nickSessionMap[nick] if nickSessionMap[nick] in bannedSessions
msg.cmdStatus = true
msg.message = makeSlackMessage()
slack.postMessage msg.message, process.env.SLACK_CHANNEL, "Jinora"
return true
return !!nick and unbanned()
}
}
module.exports = (slackObject) ->
slack = slackObject
return {
# Verify and return the auth status
verify: (nick, sessionId) ->
nick = (nick or "").toLowerCase()
nickSessionMap[nick] = sessionId
status = {}
status['nick'] = verifyNick(nick)
status['session'] = verifyUser(sessionId)
return status
# Intepret private messages from slack to jinora and send a message to slack accordingly
interpret: (message, adminNick) ->
msg.adminNick = adminNick
msg.message = message.toLowerCase()
msg.type = ""
msg.cmdStatus = "invalid"
words = msg.message.split(' ')
types = ["nick", "user"]
bans = ["ban", "unban"]
msg.banStatus = bans
if words[0] in bans
msg.banStatus = [words[0]]
msg.userNick = words[2..].join(" ") or ""
if words[1] in types
msg.type = words[1]
msg.cmdStatus = eval("banFunction.#{msg.type}.#{words[0]}")(msg.userNick) || "error"
if msg.cmdStatus != true
msg.message = makeSlackMessage()
slack.postMessage msg.message, process.env.SLACK_CHANNEL, "Jinora"
}
getJsonBlob()