-
Notifications
You must be signed in to change notification settings - Fork 3
/
sockets.js
executable file
·174 lines (146 loc) · 5.19 KB
/
sockets.js
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
"use strict"
var settings = require('./settings')
, logger = settings.logger(module)
, hook = settings.hook
, services = require('./common/services')
, models = require('./models')
, async = require('async')
, _ = require('underscore')
module.exports = function(app, socket) {
// Socket.io
socket.on('sconnection', function(client, session) {
client.emit('connect-session')
logger.debug('connected')
function botStatusCallback(err, message) {
if (err) {
logger.error('botStatus error', err)
message = 'Could not retrieve bot status!'
}
client.emit('botStatusResponse', message)
}
function browserChatCallback(err, message) {
if (err) {
logger.error("browser chat error", err)
client.emit('browserChatResponse', 'Ack! There was a problem executing command')
} else {
if (session && session.user) {
var bot = models.User.bot(session.user)
if(bot) {
message = {message: message, bot: bot}
}
}
client.emit('browserChatResponse', message)
}
}
client.on('disconnect', function(client) {
logger.debug('disconnected')
})
// Demo Bot Events
client.on('browserChatMessage', function(message) {
logger.debug('browserChatMessage')
message.callback = browserChatCallback
if (session && session.user) {
var bot = models.User.bot(session.user)
if(bot) {
message.bot = {_id: bot._id, name: bot.name}
message.user = session.user._id
}
}
hook.emit('browserChatMessage', message)
})
client.on('setBotName', function(message) {
var name = message.message
if (!name.match(/^[A-za-z0-9]+$/)) {
return false
}
logger.debug('setting bot name to ' + name)
// TODO: Lots of opportunities for errors to be
// generated. We might want to inform the person
// on the other line that there is an issue.
// If we don't find the user here then we have
// a serious problem
models.User.findById(session.user._id, function (err, user) {
var bot = models.User.bot(user)
bot.name = name
user.save(function(err) {
if (err) return next(err)
models.Bot.spawn(user, bot)
// TODO: Abstract out the set session user
// Need to make sure that this value is in JSON
// format since it gets passed around a lot.
session.user = user.toJSON()
client.emit('userDidUpdate', {user: session.user, bot: models.User.bot(session.user)})
})
})
})
client.on('getCommands', function(message) {
var commandSets = models.allCommands()
var bot = models.User.bot(session.user)
_.each(commandSets, function(set) {
_.each(set, function(command) {
if (command.help) {
command.help = command.help.replace(/BOT/g, bot.name)
}
})
})
client.emit('getCommandsResults', commandSets)
})
client.on('getBotServices', function(message) {
var bot = models.User.bot(session.user)
var authService = function(service, callback) {
services.services()[service].authService(models.Bot.service(bot, service) || {}, callback)
}
async.map(Object.keys(services.services()), authService, function (err, results) {
if(err) {
logger.error(err.stack)
client.emit('getBotServicesResults', [])
} else {
client.emit('getBotServicesResults', results)
}
})
})
client.on('getUser', function(message) {
var response = {
user: session.user,
bot: models.User.bot(session.user)
}
client.emit('getUserResponse', response)
})
client.on('botStatus', function(message) {
logger.debug('botStatus')
if (session.user !== undefined && models.User.bot(session.user) !== undefined) {
hook.emit('botStatus', {user: session.user._id, bot: models.User.bot(session.user), callback: botStatusCallback})
}
})
client.on('authService', function(message) {
models.User.findById(session.user._id, function(err, user) {
if (err) {
logger.error(err)
client.emit('authServiceResults', {type:message.type, error: 'Could not find user'})
return
}
var bot = models.User.bot(user)
if(!bot.services || bot.services.length === 0) {
bot.services = [new models.Service(message)]
} else {
bot.services = bot.services.filter(function(service) {return service.type !== message.type}).concat([new models.Service(message)])
}
models.Bot.spawn(user, models.User.bot(user), function(err, msg) {
if(err) {
logger.error(err)
client.emit('authServiceResults', {type:message.type, error: err})
return
}
user.save(function(err) {
if (err) {
logger.error(err)
client.emit('authServiceResults', {type:message.type, error: 'Could not save user'})
return
}
client.emit('authServiceResults', msg)
})
})
})
})
})
}