cryptalk/public/js/lib/host.js

252 lines
6.5 KiB
JavaScript
Raw Normal View History

2014-09-26 08:46:35 -04:00
/*
Accepts:
mediator.on('command:host', host);
mediator.on('command:hosts', hosts);
mediator.on('command:connect', connect);
mediator.on('command:disconnect', disconnect);
mediator.on('command:reconnect', disconnect);
Emits:
mediator.on('socket:emit', emit);
*/
2017-02-22 17:09:50 -05:00
define(['$', 'castrato','settings','templates','hosts','window'], function ($, mediator, settings, templates, hostconfig) {
2014-09-26 08:46:35 -04:00
var
// Private properties
socket,
host,
// Collection of parameters
parameters = {},
emit = function(payload) {
// Route message from mediator to socket
if(socket) socket.emit(payload.data,payload.payload);
},
2017-02-22 17:09:50 -05:00
hostInfo = function () {
2014-09-26 08:46:35 -04:00
mediator.emit('info', JSON.stringify(host || {}));
},
hosts = function (force, done) {
var i = 0,
left = hostconfig.hosts.length,
host,
strhosts = '\n',
callback = function (host, index, isUp) {
return function (hostSettings) {
host.settings = (isUp ? hostSettings : 0);
strhosts += $.template(templates.messages[(isUp ? 'host_available' : 'host_unavailable')], {
name: host.name,
path: host.path,
index: index
});
if (--left === 0) {
2015-10-04 08:24:55 -04:00
mediator.emit('console:info', strhosts);
2014-09-26 08:46:35 -04:00
done();
}
};
};
2014-10-20 13:14:44 -04:00
2014-09-26 08:46:35 -04:00
force = (force && force.toLowerCase() === 'force');
// Loop through all the hosts
while ((host = hostconfig.hosts[i])) {
if (!force && host.settings !== undefined) {
if (host.settings) {
callback(host, i, 1)();
} else {
callback(host, i, 0)();
}
} else {
require([host.path], callback(host, i, 1), callback(host, i, 0));
}
i++;
}
},
connect = function (toHost, done) {
2014-10-20 13:14:44 -04:00
mediator.emit('console:lockInput');
2014-09-26 08:46:35 -04:00
var
2017-02-22 17:09:50 -05:00
request;
2014-09-26 08:46:35 -04:00
2017-02-22 17:09:50 -05:00
// Use hostconfig.autoconnect as default host
toHost = (toHost === undefined) ? hostconfig.autoconnect : toHost;
2014-09-26 08:46:35 -04:00
if (host && host.connected) {
mediator.emit('console:error', $.template(templates.messages.already_connected, {
host: host.name || 'localhost'
}));
2014-10-20 13:14:44 -04:00
mediator.emit('console:unlockInput');
2014-09-26 08:46:35 -04:00
return;
}
if ($.isDigits(toHost)) {
if ((host = hostconfig.hosts[+toHost])) {
if (host.settings) {
settings = host.settings;
} else {
request = host.path;
}
} else {
mediator.emit('console:error', 'Undefined host index: ' + toHost);
2014-10-20 13:14:44 -04:00
mediator.emit('console:unlockInput');
2014-09-26 08:46:35 -04:00
return;
}
2017-02-07 15:19:23 -05:00
} else if (!toHost) {
2014-09-26 08:46:35 -04:00
settings = toHost.settings;
} else { // Assume string
request = toHost.settings;
2014-09-26 08:46:35 -04:00
}
if (request) {
return require([request], function (settings) {
host.settings = settings;
return connect(toHost, done);
}, function () {
mediator.emit('console:error', 'Could not fetch host settings: ' + request);
2014-10-20 13:14:44 -04:00
mediator.emit('console:unlockInput');
2014-09-26 08:46:35 -04:00
return;
});
}
// Push 'Connecting...' message
mediator.emit('console:info', $.template(templates.messages.connecting, {host: host.name || 'localhost'}));
// Show motd (placed here to enable server specific motds in future)
mediator.emit('console:motd', host.settings.motd);
// The one and only socket
socket = $.io(host.host, {
2014-09-26 08:46:35 -04:00
forceNew: true,
'force new connection': true
});
// Bind socket events
socket
2017-02-22 17:09:50 -05:00
.on('room:joined', function () {
2014-09-26 08:46:35 -04:00
mediator.emit('console:info', $.template(templates.messages.joined_room, { roomName: $.escapeHtml(parameters.room) } ));
// Automatically count persons on join
socket.emit('room:count');
})
.on('room:left', function () {
mediator.emit('console:info', $.template(templates.messages.left_room, { roomName: $.escapeHtml(parameters.room) } ));
mediator.emit('room:changed',false);
})
.on('message:send', function (data) {
var decrypted = $.AES.decrypt(data.msg, $.SHA1(parameters.room) + parameters.key),
sanitized = $.escapeHtml(decrypted),
nick = !data.nick ? templates.default_nick : $.escapeHtml($.AES.decrypt(data.nick, $.SHA1(parameters.room) + parameters.key));
if (!decrypted) {
mediator.emit('console:error', templates.messages.unable_to_decrypt);
} else {
mediator.emit('console:message', { message: sanitized, nick: nick } );
}
})
.on('message:server', function (data) {
if( data.msg ) {
var sanitized = $.escapeHtml(data.msg);
if( templates.server[sanitized] ) {
if( data.payload !== undefined ) {
var sanitized_payload = $.escapeHtml(data.payload);
mediator.emit('console:server', $.template(templates.server[sanitized], { payload: sanitized_payload }));
} else {
mediator.emit('console:server', templates.server[sanitized]);
}
} else {
mediator.emit('console:error', templates.server.bogus);
}
} else {
mediator.emit('console:error', templates.server.bogus);
}
})
.on('connect', function () {
2014-09-26 08:46:35 -04:00
// Tell the user that the chat is ready to interact with
mediator.emit('console:info', $.template(templates.messages.connected, {
host: host.name || 'localhost'
}));
// Set window title
mediator.emit('window:title', host.settings.title);
// Unlock input
2014-10-20 13:14:44 -04:00
mediator.emit('console:unlockInput');
2014-09-26 08:46:35 -04:00
done();
host.connected = 1;
})
.on('disconnect', function () {
2014-09-26 08:46:35 -04:00
host.connected = 0;
// Tell the user that the chat is ready to interact with
mediator.emit('console:info', $.template(templates.messages.disconnected, {
host: host.name || 'localhost'
}));
// Revert title
mediator.emit('room:changed',undefined);
mediator.emit('window:title',templates.client.title);
})
.on('connect_error', function () {
2014-09-26 08:46:35 -04:00
host.connected = 0;
mediator.emit('console:error', templates.messages.socket_error);
// Unlock input
2014-10-20 13:14:44 -04:00
mediator.emit('console:unlockInput');
2014-09-26 08:46:35 -04:00
});
return;
},
reconnect = function (foo, done) {
if (host) {
if (host.connected) {
disconnect();
connect(host, done);
} else {
connect(host, done);
}
} else {
done();
return mediator.emit('console:error', templates.messages.reconnect_no_host);
}
},
disconnect = function () {
socket.disconnect();
},
param = function (p) {
2017-02-07 15:19:23 -05:00
parameters = Object.assign({}, parameters, p );
2014-09-26 08:46:35 -04:00
};
2014-09-27 08:56:57 -04:00
2017-02-22 17:09:50 -05:00
mediator.on('command:host', hostInfo);
2014-09-26 08:46:35 -04:00
mediator.on('command:hosts', hosts);
mediator.on('command:connect', connect);
mediator.on('command:disconnect', disconnect);
2017-02-22 17:09:50 -05:00
mediator.on('command:reconnect', reconnect);
2014-09-26 08:46:35 -04:00
mediator.on('socket:emit', emit);
mediator.on('host:param', param);
});