diff --git a/public/css/default.css b/public/css/default.css index 34bcd93..8ae99b3 100644 --- a/public/css/default.css +++ b/public/css/default.css @@ -40,6 +40,7 @@ body, html { #chat li { white-space: pre; padding: 2px 15px; + color: #343434; } /* Message types */ @@ -49,9 +50,10 @@ body, html { } #chat i.motd { color: #99FF99; display:inline-block; line-height: 12px !important; } #chat i.info { color: #999999; } -#chat i.server { color: #999999; } +#chat i.server { color: #99FFFF; } #chat i.error { color: #ff7777; } #chat i.message { color: #eeeeee; } +#chat i.nick { color: #99FF99; } /*------------------------------------*\ INPUT diff --git a/public/js/cryptalk_modules/cryptalk.js b/public/js/cryptalk_modules/cryptalk.js index d56c3f6..2b7fc01 100644 --- a/public/js/cryptalk_modules/cryptalk.js +++ b/public/js/cryptalk_modules/cryptalk.js @@ -58,6 +58,15 @@ define('cryptalk', { }, + count: function () { + if( room ) { + socket.emit('room:count'); + } else { + post('error', templates.messages.not_in_room); + } + + }, + key: function (payload) { // Make sure the key meets the length requirements if (payload.length < 8) { @@ -177,6 +186,9 @@ define('cryptalk', { .on('room:joined', function (data) { room = data; post('info', $.template(templates.messages.joined_room, { roomName: room })); + + // Automatically count persons on join + socket.emit('room:count'); }) .on('room:left', function () { @@ -198,8 +210,21 @@ define('cryptalk', { }) .on('message:server', function (data) { - var sanitized = $.escapeHtml(data); - post('server', data); + if( data.msg ) { + var sanitized = $.escapeHtml(data.msg); + if( templates.server[sanitized] ) { + if( data.payload !== undefined ) { + var sanitized_payload = $.escapeHtml(data.payload); + post('server', $.template(templates.server[sanitized], { payload: sanitized_payload })); + } else { + post('server', templates.server[sanitized]); + } + } else { + post('error', templates.server.bogus); + } + } else { + post('error', templates.server.bogus); + } }); // Post the help/welcome message diff --git a/public/js/cryptalk_modules/templates.js b/public/js/cryptalk_modules/templates.js index dc2f305..4ff4d85 100644 --- a/public/js/cryptalk_modules/templates.js +++ b/public/js/cryptalk_modules/templates.js @@ -26,9 +26,10 @@ define({ 'Available commands: \n' + ' /generate Generate random room \n' + ' /join RoomId Join a room \n' + - ' /leave Leave the room \n' + + ' /count Count participants of room \n' + ' /nick NickName Sets an optional nick \n' + ' /key OurStrongPassphrase Sets encryption key \n' + + ' /leave Leave the room \n' + ' /clear Clear on-screen buffer \n' + ' /help This \n' + ' \n' + @@ -43,10 +44,10 @@ define({ post: { motd: '
  • {text}
  • ', - info: '
  • INF> {text}
  • ', - server: '
  • SRV> {text}
  • ', - error: '
  • ERR> {text}
  • ', - message: '
  • {nick}> {text}
  • ' + info: '
  • INF> {text}
  • ', + server: '
  • SRV> {text}
  • ', + error: '
  • ERR> {text}
  • ', + message: '
  • {nick}> {text}
  • ' }, messages: { @@ -54,6 +55,7 @@ define({ key_ok_ready: 'Key set, you can now 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.', + not_in_room: 'You have to be in a room to count participants...', 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}\'.', @@ -68,5 +70,13 @@ define({ already_in_room: 'You are already in a room ({roomName}), stoopid.', unable_to_decrypt: 'Unabled to decrypt received message, keys does not match.' + }, + + server: { + person_joined: 'A person joined this room.', + person_left: 'A person left this room.', + person_count: 'There is {payload} person(s) in this room, including you.', + command_failed: 'Server command failed, you\'re probably trying to du something bogus.', + bogus: 'Received a bogus message from server.', } }); \ No newline at end of file diff --git a/server.js b/server.js index bd53199..07c3952 100644 --- a/server.js +++ b/server.js @@ -12,21 +12,34 @@ app.io.route('room', { req.socket.emit('room:generated',room); }, join: function(req) { - if(req.data) { + if( req.data ) { req.socket.emit('room:joined',req.data); req.socket.join(req.data); - req.socket.broadcast.to(req.data).emit('message:server', 'A person joined this room'); + req.socket.broadcast.to(req.data).emit('message:server', {msg:'person_joined'} ); req.socket.current_room = req.data; + } else { + req.socket.emit('message:server', {msg:'command_failed'} ); } }, leave: function(req) { - if(req.data) { + if( req.data ) { req.socket.emit('room:left'); req.socket.leave(req.data); - req.socket.broadcast.to(req.data).emit('message:server', 'A person left this room'); + req.socket.broadcast.to(req.data).emit('message:server', {msg:'person_left'} ); req.socket.current_room = undefined; + } else { + req.socket.emit('message:server', {msg:'command_failed'} ); } - } + }, + count: function(req) { + if( req.socket.current_room !== undefined ) { + // This will fail on socket.io >= 1.0 + var client_count = app.io.sockets.clients(req.socket.current_room).length; + req.socket.emit('message:server', {msg:'person_count', payload: client_count } ); + } else { + req.socket.emit('message:server', {msg:'command_failed'} ); + } + } }); app.io.route('message', { @@ -40,7 +53,7 @@ app.io.sockets.on('connection', function(socket) { socket.on('disconnect', function() { // Notify other users of the room if( socket.current_room !== undefined ) { - socket.broadcast.to(socket.current_room).emit('message:server', 'A person left this room'); + socket.broadcast.to(socket.current_room).emit('message:server', {msg:'person_left'} ); } }); });