Added settings module and exposed it to the templates
The server should require the same settings module as the client; so that the checks is performed on both sides.
This commit is contained in:
		
							parent
							
								
									42016ac9bc
								
							
						
					
					
						commit
						482d352f30
					
				
					 4 changed files with 54 additions and 30 deletions
				
			
		| 
						 | 
				
			
			@ -62,7 +62,7 @@ define(['fandango', 'websocket', 'aes'], function (fandango, websocket, aes) {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * A very simple implementation of sprintf()
 | 
			
		||||
	 * A very simple templating function.
 | 
			
		||||
	 * @param  {} str [description]
 | 
			
		||||
	 * @param  {[type]} map [description]
 | 
			
		||||
	 * @return {[type]}     [description]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,14 +1,11 @@
 | 
			
		|||
// Main cryptalk module
 | 
			
		||||
define({
 | 
			
		||||
	data: {
 | 
			
		||||
		// If no host is given it will default to localhost.
 | 
			
		||||
		host: ''
 | 
			
		||||
	},
 | 
			
		||||
	compiles: ['$'],
 | 
			
		||||
	requires: ['templates', 'sound', 'fandango']
 | 
			
		||||
	requires: ['settings', 'templates', 'sound', 'fandango']
 | 
			
		||||
}, function ($, requires, data) {
 | 
			
		||||
	var socket,
 | 
			
		||||
		key,
 | 
			
		||||
		host,
 | 
			
		||||
		room,
 | 
			
		||||
		hash,
 | 
			
		||||
		nick,
 | 
			
		||||
| 
						 | 
				
			
			@ -27,6 +24,7 @@ define({
 | 
			
		|||
		},
 | 
			
		||||
 | 
			
		||||
		// Shortcut
 | 
			
		||||
		settings = requires.settings;
 | 
			
		||||
		fandango = requires.fandango;
 | 
			
		||||
		templates = requires.templates;
 | 
			
		||||
		sound = requires.sound;
 | 
			
		||||
| 
						 | 
				
			
			@ -34,11 +32,16 @@ define({
 | 
			
		|||
		// Adds a new message to the DOM
 | 
			
		||||
		post = function (type, text, clearChat, clearBuffer, nick) {
 | 
			
		||||
			var tpl = templates.post[type],
 | 
			
		||||
				post = $.template(tpl, text && {
 | 
			
		||||
					text: text,
 | 
			
		||||
					nick: nick
 | 
			
		||||
				post,
 | 
			
		||||
				data = fandango.merge({}, settings, {
 | 
			
		||||
					nick: nick,
 | 
			
		||||
					room: room,
 | 
			
		||||
					mute: mute
 | 
			
		||||
				});
 | 
			
		||||
 | 
			
		||||
			data.text = $.template(text, data);
 | 
			
		||||
			post = $.template(tpl, data);
 | 
			
		||||
 | 
			
		||||
			// Always clear the input after a post
 | 
			
		||||
			if (clearBuffer) {
 | 
			
		||||
				clearInput();
 | 
			
		||||
| 
						 | 
				
			
			@ -79,8 +82,10 @@ define({
 | 
			
		|||
 | 
			
		||||
			key: function (payload) {
 | 
			
		||||
				// Make sure the key meets the length requirements
 | 
			
		||||
				if (payload.length < 8) {
 | 
			
		||||
					return post('error', templates.messages.key_weak);
 | 
			
		||||
				if (payload.length > settings.key_maxLen) {
 | 
			
		||||
					return post('error', templates.messages.key_to_long);
 | 
			
		||||
				} else if (payload.length < settings.key_minLen) {
 | 
			
		||||
					return post('error', templates.messages.key_to_short);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				// Set key
 | 
			
		||||
| 
						 | 
				
			
			@ -91,9 +96,11 @@ define({
 | 
			
		|||
			},
 | 
			
		||||
 | 
			
		||||
			nick: function (payload) {
 | 
			
		||||
				// Make sure the nick meets the length requirements
 | 
			
		||||
				if (payload.length < 2) {
 | 
			
		||||
					return post('error', templates.messages.nick_short);
 | 
			
		||||
				// Make sure the key meets the length requirements
 | 
			
		||||
				if (payload.length > settings.nick_maxLen) {
 | 
			
		||||
					return post('error', templates.messages.nick_to_long);
 | 
			
		||||
				} else if (payload.length < settings.nick_minLen) {
 | 
			
		||||
					return post('error', templates.messages.nick_to_short);
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				// Set nick
 | 
			
		||||
| 
						 | 
				
			
			@ -114,7 +121,7 @@ define({
 | 
			
		|||
			join: function (payload) {
 | 
			
		||||
				return (
 | 
			
		||||
					room
 | 
			
		||||
						? post('error', $.template(templates.messages.already_in_room, { roomName: room}))
 | 
			
		||||
						? post('error', templates.messages.already_in_room)
 | 
			
		||||
						: socket.emit('room:join', payload)
 | 
			
		||||
				);
 | 
			
		||||
			},
 | 
			
		||||
| 
						 | 
				
			
			@ -122,7 +129,7 @@ define({
 | 
			
		|||
			generate: function (payload) {
 | 
			
		||||
				return (
 | 
			
		||||
					room
 | 
			
		||||
						? post('error', $.template(templates.messages.already_in_room, { roomName: room}))
 | 
			
		||||
						? post('error', templates.messages.already_in_room)
 | 
			
		||||
						: socket.emit('room:generate')
 | 
			
		||||
				);
 | 
			
		||||
			}
 | 
			
		||||
| 
						 | 
				
			
			@ -250,16 +257,18 @@ define({
 | 
			
		|||
			}
 | 
			
		||||
		};
 | 
			
		||||
 | 
			
		||||
	host = settings.host;
 | 
			
		||||
 | 
			
		||||
	// Post the help/welcome message
 | 
			
		||||
	post('motd', templates.motd, true);
 | 
			
		||||
 | 
			
		||||
	// Push 'Connecting...' message
 | 
			
		||||
	post('info', $.template(templates.messages.connecting, {
 | 
			
		||||
		host: data.host || 'localhost'
 | 
			
		||||
		host: host || 'localhost'
 | 
			
		||||
	}));
 | 
			
		||||
 | 
			
		||||
	// The one  and only socket
 | 
			
		||||
	socket = $.Websocket.connect(data.host);
 | 
			
		||||
	socket = $.Websocket.connect(host);
 | 
			
		||||
 | 
			
		||||
	// Bind socket events
 | 
			
		||||
	socket
 | 
			
		||||
| 
						 | 
				
			
			@ -330,7 +339,7 @@ define({
 | 
			
		|||
 | 
			
		||||
			// Tell the user that the chat is ready to interact with
 | 
			
		||||
			post('info', $.template(templates.messages.connected, {
 | 
			
		||||
				host: data.host || 'localhost'
 | 
			
		||||
				host: host || 'localhost'
 | 
			
		||||
			}));
 | 
			
		||||
 | 
			
		||||
			// It's possible to provide room and key using the hashtag.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										10
									
								
								public/js/cryptalk_modules/settings.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								public/js/cryptalk_modules/settings.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,10 @@
 | 
			
		|||
define({
 | 
			
		||||
	// If no host is given it will default to localhost.
 | 
			
		||||
	host: '',
 | 
			
		||||
 | 
			
		||||
	nick_maxLen: 20,
 | 
			
		||||
	nick_minLen: 3,
 | 
			
		||||
 | 
			
		||||
	key_maxLen: Infinity,
 | 
			
		||||
	key_minLen: 8
 | 
			
		||||
});
 | 
			
		||||
| 
						 | 
				
			
			@ -48,6 +48,8 @@ define({
 | 
			
		|||
 | 
			
		||||
	default_nick: 'Anonymous',
 | 
			
		||||
 | 
			
		||||
	// All post templates will have access to the properties in the 'settings' module, 
 | 
			
		||||
	// along with the current nick, room, mute-status and of course the message ('text').
 | 
			
		||||
	post: {
 | 
			
		||||
		motd: 		'<li><i class="motd">{text}</i></li>',
 | 
			
		||||
		info: 		'<li>INF> <i class="info">{text}</i></li>',
 | 
			
		||||
| 
						 | 
				
			
			@ -56,34 +58,37 @@ define({
 | 
			
		|||
		message: 	'<li><i class="nick">{nick}></i> <i class="message">{text}</i></li>'
 | 
			
		||||
	},
 | 
			
		||||
 | 
			
		||||
	// All message templates will have access to the properties in the 'settings' module, 
 | 
			
		||||
	// along with the current nick, room and mute-status.
 | 
			
		||||
	messages: {
 | 
			
		||||
		key_weak: 				'Hmm, that\'s a weak key, try again...',
 | 
			
		||||
		key_to_short: 			'Hmm, that\'s a weak key, try again...',
 | 
			
		||||
		key_to_long: 			'Man that\'s a long key. Make it a tad short, \'kay?',
 | 
			
		||||
		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.',
 | 
			
		||||
 | 
			
		||||
		nick_to_short: 			'Nickname is too short, it has to be at least {nick_minLen} characters long. Try again.',
 | 
			
		||||
		nick_to_long: 			'Nickname is too long, it can be at most {nick_maxLen} characters long. Try again.',
 | 
			
		||||
		nick_set: 				'From now on, you\'re referred to as \'{nick}\'.',
 | 
			
		||||
 | 
			
		||||
		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}\'.',
 | 
			
		||||
		leave_from_nowhere: 	'How are you supposed to leave, while being nowhere?',
 | 
			
		||||
 | 
			
		||||
		// Sounds
 | 
			
		||||
		muted: 					'Notification sounds is now muted.',
 | 
			
		||||
		unmuted: 					'Notifications sounds is now on.',
 | 
			
		||||
		unmuted: 				'Notifications sounds is now on.',
 | 
			
		||||
 | 
			
		||||
		// Available variables: 'commandName'
 | 
			
		||||
		// Extra variables: 'commandName'
 | 
			
		||||
		unrecognized_command: 	'Unrecognized command: "{commandName}"',
 | 
			
		||||
 | 
			
		||||
		// Available variables: 'roomName'
 | 
			
		||||
		joined_room: 			'Joined room {roomName}',
 | 
			
		||||
		left_room: 				'Left room {roomName}',
 | 
			
		||||
		already_in_room: 		'You are already in a room ({roomName}), stoopid.',
 | 
			
		||||
		joined_room: 			'Joined room {room}',
 | 
			
		||||
		left_room: 				'Left room {room}',
 | 
			
		||||
		already_in_room: 		'You are already in a room ({room}), stoopid.',
 | 
			
		||||
 | 
			
		||||
		unable_to_decrypt: 		'Unabled to decrypt received message, keys does not match.',
 | 
			
		||||
 | 
			
		||||
		socket_error: 			'A network error has occurred. A restart may be required to bring back full functionality.<br>Examine the logs for more details.',
 | 
			
		||||
 | 
			
		||||
		// Available variable: 'host'
 | 
			
		||||
		connecting: 			'Connecting to host {host}...',
 | 
			
		||||
		connected: 				'A connection to the server has been established. Happy chatting!'
 | 
			
		||||
	},
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue