Fixes and nickname support

This commit is contained in:
Hexagon 2014-09-18 19:17:41 +02:00
parent 138eff9c96
commit d05fa2cff3
3 changed files with 55 additions and 22 deletions

View File

@ -11,6 +11,7 @@ define('cryptalk', {
key, key,
room, room,
hash, hash,
nick,
// Collection of DOM components // Collection of DOM components
components = { components = {
@ -22,10 +23,11 @@ define('cryptalk', {
templates = requires.templates, templates = requires.templates,
// Adds a new message to the DOM // Adds a new message to the DOM
post = function (type, text, clearChat, clearBuffer) { post = function (type, text, clearChat, clearBuffer, nick) {
var tpl = templates.post[type], var tpl = templates.post[type],
post = $.template(tpl, text && { post = $.template(tpl, text && {
text: text text: text,
nick: nick
}); });
// Always clear the input after a post // Always clear the input after a post
@ -49,13 +51,18 @@ define('cryptalk', {
}, },
leave: function () { leave: function () {
socket.emit('room:leave', room); if( room ) {
socket.emit('room:leave', room);
} else {
post('error', templates.messages.leave_from_nowhere);
}
}, },
key: function (payload) { key: function (payload) {
// Make sure the key meets the length requirements // Make sure the key meets the length requirements
if (payload.length < 8) { if (payload.length < 8) {
return post('info', templates.messages.key_weak); return post('error', templates.messages.key_weak);
} }
// Set key // Set key
@ -65,16 +72,33 @@ define('cryptalk', {
post('info', (room ? templates.messages.key_ok_ready : templates.messages.key_ok_but_no_room)); post('info', (room ? templates.messages.key_ok_ready : templates.messages.key_ok_but_no_room));
}, },
nick: function (payload) {
// Make sure the nick meets the length requirements
if (payload.length < 2) {
return post('error', templates.messages.nick_short);
}
// Set nick
nick = payload;
// Inform that the key has been set
post('info', $.template(templates.messages.nick_set, { nick: nick}));
},
join: function (payload) { join: function (payload) {
return ( return (
room room
? post('info', $.template(templates.messages.already_in_room, { roomName: room})) ? post('error', $.template(templates.messages.already_in_room, { roomName: room}))
: socket.emit('room:join', payload) : socket.emit('room:join', payload)
); );
}, },
create: function (payload) { generate: function (payload) {
socket.emit('room:create'); return (
room
? post('error', $.template(templates.messages.already_in_room, { roomName: room}))
: socket.emit('room:generate')
);
} }
}, },
@ -124,7 +148,8 @@ define('cryptalk', {
// Encrypt message using room UUID as salt and key as pepper. // Encrypt message using room UUID as salt and key as pepper.
socket.emit('message:send', { socket.emit('message:send', {
room: room, room: room,
msg: $.AES.encrypt(buffer, room + key) msg: $.AES.encrypt(buffer, room + key).toString(),
nick: nick
}); });
// Adn the the buffer // Adn the the buffer
@ -142,7 +167,7 @@ define('cryptalk', {
components.input.focus(); components.input.focus();
}) })
.on('room:created', function (data) { .on('room:generated', function (data) {
socket.emit('room:join', data); socket.emit('room:join', data);
}) })
@ -157,18 +182,20 @@ define('cryptalk', {
}) })
.on('message:send', function (data) { .on('message:send', function (data) {
var decrypted = $.AES.decrypt(data, room + key), var decrypted = $.AES.decrypt(data.msg, room + key),
sanitized = $.escapeHtml(decrypted); sanitized = $.escapeHtml(decrypted),
nick = (data.nick == undefined || !data.nick ) ? templates.default_nick : $.escapeHtml(data.nick);
if (!decrypted) { if (!decrypted) {
post('info', templates.messages.unable_to_decrypt); post('error', templates.messages.unable_to_decrypt);
} else { } else {
// Post the message, but do not clear either the chat nor the buffer. // Post the message, but do not clear either the chat nor the buffer.
post('message', sanitized, false, false); post('message', sanitized, false, false, nick);
} }
}) })
.on('message:server', function (data) { .on('message:server', function (data) {
var sanitized = $.escapeHtml(data);
post('server', data); post('server', data);
}); });

View File

@ -8,9 +8,10 @@ define({
'---------------------------------------------------------------------------------- \n' + '---------------------------------------------------------------------------------- \n' +
' \n' + ' \n' +
'Available commands: \n' + 'Available commands: \n' +
' /create Creates a room \n' + ' /generate Generate a random room id \n' +
' /join RoomId Joins a room \n' + ' /join RoomId Joins a room \n' +
' /leave RoomId Leaves a room \n' + ' /leave Leaves the current room \n' +
' /nick NickName Sets an optinal nickname \n' +
' /key OurStrongPassphrase Sets the password used for \n' + ' /key OurStrongPassphrase Sets the password used for \n' +
' encryption/decryption \n' + ' encryption/decryption \n' +
' /clear Clears on-screen buffer \n' + ' /clear Clears on-screen buffer \n' +
@ -23,11 +24,13 @@ define({
'--------------------------------------------------------------------------------- \n' + '--------------------------------------------------------------------------------- \n' +
'</li> ', '</li> ',
default_nick: 'Anonymous',
post: { post: {
info: '<li>INF> <i class="info">{text}</i></li>', info: '<li>INF> <i class="info">{text}</i></li>',
server: '<li>SRV> <i class="server">{text}</i></li>', server: '<li>SRV> <i class="server">{text}</i></li>',
error: '<li>ERR> <i class="error">{text}</i></li>', error: '<li>ERR> <i class="error">{text}</i></li>',
message: '<li>MSG> <i class="message">{text}</i></li>' message: '<li>{nick}> <i class="message">{text}</i></li>'
}, },
messages: { messages: {
@ -36,6 +39,9 @@ define({
key_ok_but_no_room: 'Key set, you can now join a room and start communicating.', key_ok_but_no_room: 'Key set, you can now join a room and start communicating.',
msg_no_room: 'You have to join a room before sending messages. See /help.', msg_no_room: 'You have to join a room before sending messages. See /help.',
msg_no_key: 'You have to set an encryption key before sending a message. See /help.', msg_no_key: 'You have to set an encryption key before sending a message. See /help.',
nick_short: 'Nickname is too short, try again.',
nick_set: 'From now on, you\'re referred to as \'{nick}\'.',
leave_from_nowhere: 'How are you supposed to leave, while being nowhere?',
// Available variables: 'commandName' // Available variables: 'commandName'
unrecognized_command: 'Unrecognized command: "{commandName}"', unrecognized_command: 'Unrecognized command: "{commandName}"',
@ -43,7 +49,7 @@ define({
// Available variables: 'roomName' // Available variables: 'roomName'
joined_room: 'Joined room {roomName}', joined_room: 'Joined room {roomName}',
left_room: 'Left room {roomName}', left_room: 'Left room {roomName}',
already_in_room: 'You are already in room {roomName}, stoopid.', already_in_room: 'You are already in a room ({roomName}), stoopid.',
unable_to_decrypt: 'Unabled to decrypt received message, keys does not match.' unable_to_decrypt: 'Unabled to decrypt received message, keys does not match.'
} }

View File

@ -6,10 +6,10 @@ app = express();app.http().io();
app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/public'));
app.io.route('room', { app.io.route('room', {
create: function(req) { generate: function(req) {
var room = uuid.v4(); var room = uuid.v4();
req.socket.emit('message:server', 'Room ' + room + ' created'); req.socket.emit('message:server', 'Room ' + room + ' generated');
req.socket.emit('room:created',room); req.socket.emit('room:generated',room);
}, },
join: function(req) { join: function(req) {
if(req.data) { if(req.data) {
@ -29,8 +29,8 @@ app.io.route('room', {
app.io.route('message', { app.io.route('message', {
send: function(req) { send: function(req) {
if(req.data && req.data.room) req.socket.broadcast.to(req.data.room).emit('message:send', req.data.msg); if(req.data && req.data.room) req.socket.broadcast.to(req.data.room).emit('message:send', { msg: req.data.msg, nick: req.data.nick} );
req.socket.emit('message:send', req.data.msg); req.socket.emit('message:send', { msg: req.data.msg, nick: req.data.nick} );
} }
}); });