forked from sdslabs/jinora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userinfo.coffee
85 lines (76 loc) · 2.69 KB
/
userinfo.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
platform = require('platform')
request = require('request')
randomColor = () ->
return '#' + (0x1000000 + (Math.random()) * 0xffffff).toString(16).substr(1, 6);
module.exports = (ioObject, slackObject, setConnectNotify) ->
io = ioObject
slack = slackObject
users = {}
addUser: (req) ->
user =
nick: req.data.nick
platform: platform.parse(req.headers['user-agent'])
ip:
public: req.headers['x-forwarded-for']
local: []
users[req.io.socket.id] = user
removeUser: (id)->
if !!users[id]
nick = users[id].nick
setTimeout ->
delete users[id]
, 60000
nick
addUserIp: (req) ->
user = users[req.io.socket.id]
user.ip.local.push req.data.localip if user
getOnlineUsers: ()->
onlineUsers = []
for id, user of users
onlineUsers.push user.nick
onlineUsers
interpret: (message) ->
index = message.indexOf(' ')
if index == -1
command = message
nick = ''
else
command = message.substr(0, index)
nick = message.substr(index + 1)
command = command.toLowerCase()
if command == 'info'
if nick == ''
slack.postMessage "No nick supplied", process.env.SLACK_CHANNEL, 'Jinora'
return
userExist = Object.keys(users).some (id) ->
user = users[id]
if user.nick == nick
text = "Info for #{nick}:\n"
text += "\t*Platform:* #{user.platform}\n"
text += "\t*Public ip:* #{user.ip.public}\n"
text += "\t*Local ip:* #{user.ip.local.join '|'}"
attachments = [
{
"color": randomColor()
"pretext": "Info for #{nick}:"
"title": "#{nick}"
"text": "*Platform:* #{user.platform}" +
"\n*Public ip:* #{user.ip.public} " +
"\n*Local ip:* #{user.ip.local.join '|'}"
}
]
if user.ip.public
request.get {
url: "http://ipwhois.app/json/#{user.ip.public}?objects=country,city,region,org,isp",
json: true
}, (err, res, data)->
if !err
text += "\n\t*Location:* #{data.city}, #{data.region}, #{data.country}"
text += "\n\t*Org:* #{data.org}, *ISP*: #{data.isp}"
attachments[0]["fallback"] = text
attachments[0]["text"] += "\n*Location:* #{data.city}, #{data.region}, #{data.country}"
attachments[0]["footer"] = "#{data.org}, #{data.isp}"
slack.postMessage "", process.env.SLACK_CHANNEL, 'Jinora', null, attachments
return true
false
slack.postMessage "No such user found", process.env.SLACK_CHANNEL, 'Jinora' if !userExist