diff --git a/public/js/cryptalk_modules/cryptalk.js b/public/js/cryptalk_modules/cryptalk.js index 75cc3f4..7aa2b16 100644 --- a/public/js/cryptalk_modules/cryptalk.js +++ b/public/js/cryptalk_modules/cryptalk.js @@ -11,6 +11,7 @@ define('cryptalk', { key, room, hash, + nick, // Collection of DOM components components = { @@ -22,10 +23,11 @@ define('cryptalk', { templates = requires.templates, // 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], post = $.template(tpl, text && { - text: text + text: text, + nick: nick }); // Always clear the input after a post @@ -49,13 +51,18 @@ define('cryptalk', { }, 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) { // Make sure the key meets the length requirements if (payload.length < 8) { - return post('info', templates.messages.key_weak); + return post('error', templates.messages.key_weak); } // Set key @@ -65,16 +72,33 @@ define('cryptalk', { 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) { return ( 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) ); }, - create: function (payload) { - socket.emit('room:create'); + generate: function (payload) { + 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. socket.emit('message:send', { room: room, - msg: $.AES.encrypt(buffer, room + key) + msg: $.AES.encrypt(buffer, room + key).toString(), + nick: nick }); // Adn the the buffer @@ -142,7 +167,7 @@ define('cryptalk', { components.input.focus(); }) - .on('room:created', function (data) { + .on('room:generated', function (data) { socket.emit('room:join', data); }) @@ -157,18 +182,20 @@ define('cryptalk', { }) .on('message:send', function (data) { - var decrypted = $.AES.decrypt(data, room + key), - sanitized = $.escapeHtml(decrypted); + var decrypted = $.AES.decrypt(data.msg, room + key), + sanitized = $.escapeHtml(decrypted), + nick = (data.nick == undefined || !data.nick ) ? templates.default_nick : $.escapeHtml(data.nick); if (!decrypted) { - post('info', templates.messages.unable_to_decrypt); + post('error', templates.messages.unable_to_decrypt); } else { // 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) { + var sanitized = $.escapeHtml(data); post('server', data); }); diff --git a/public/js/cryptalk_modules/templates.js b/public/js/cryptalk_modules/templates.js index 1f87fea..d26b6ea 100644 --- a/public/js/cryptalk_modules/templates.js +++ b/public/js/cryptalk_modules/templates.js @@ -8,9 +8,10 @@ define({ '---------------------------------------------------------------------------------- \n' + ' \n' + 'Available commands: \n' + - ' /create Creates a room \n' + + ' /generate Generate a random room id \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' + ' encryption/decryption \n' + ' /clear Clears on-screen buffer \n' + @@ -23,11 +24,13 @@ define({ '--------------------------------------------------------------------------------- \n' + ' ', + default_nick: 'Anonymous', + post: { info: '
  • INF> {text}
  • ', server: '
  • SRV> {text}
  • ', error: '
  • ERR> {text}
  • ', - message: '
  • MSG> {text}
  • ' + message: '
  • {nick}> {text}
  • ' }, messages: { @@ -36,6 +39,9 @@ define({ 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_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' unrecognized_command: 'Unrecognized command: "{commandName}"', @@ -43,7 +49,7 @@ define({ // Available variables: 'roomName' joined_room: 'Joined 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.' } diff --git a/server.js b/server.js index 52f8889..b0b303e 100644 --- a/server.js +++ b/server.js @@ -6,10 +6,10 @@ app = express();app.http().io(); app.use(express.static(__dirname + '/public')); app.io.route('room', { - create: function(req) { + generate: function(req) { var room = uuid.v4(); - req.socket.emit('message:server', 'Room ' + room + ' created'); - req.socket.emit('room:created',room); + req.socket.emit('message:server', 'Room ' + room + ' generated'); + req.socket.emit('room:generated',room); }, join: function(req) { if(req.data) { @@ -29,8 +29,8 @@ app.io.route('room', { app.io.route('message', { send: function(req) { - if(req.data && req.data.room) req.socket.broadcast.to(req.data.room).emit('message:send', req.data.msg); - req.socket.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', { msg: req.data.msg, nick: req.data.nick} ); } });