gdumitrescu
4/6/2011 - 6:26 PM

A simple SIP registrar that uses Node.js and Redis

A simple SIP registrar that uses Node.js and Redis

var sip = require('sip');
var sys = require('sys');
var redis = require('redis');

//Trim leading and trailing whitespace from string values.
function trim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}

sip.start({},function(request) {

	try {

		// Parse the URI in the reuqest header.
		var address = sip.parseUri(request.headers.to.uri);

		// Create a redis client to manage the registration
		// info.
		var client = redis.createClient();

		// Handle SIP Registrations.
		if (trim(request.method) == 'REGISTER') {
			var contact = request.headers.contact;

			// Store the registration info.
			if (Array.isArray(contact)
					&& contact.length
					&& (+(contact[0].params.expires
							|| request.headers.expires || 300)) > 0) {
				sys.puts('Registering user ' + request.headers.to.name + ' at ' + contact[0].uri);
				client.set(address.user, contact[0].uri);
			}

			// Remove the registration info.
			else {
				sys.puts('Logging off user ' + request.headers.to.name);
				client.del(address.user);
			}

			// Build the response.
			var response = sip.makeResponse(request, 200, 'OK');

			// Send the response to the SIP client
			sip.send(response);

		}

		// Handle SIP Invites.
		if (trim(request.method) == 'INVITE') {
			sip.send(sip.makeResponse(request, 100, 'Trying'));

			// Look up the registration info. for the user being
			// called.
			address = sip.parseUri(request.uri);
			client.get(address.user, function(err, contact) {
				// If not found or error return 404.
				if (err || contact === null) {
					sys.puts('User ' + address.user + ' is not found');
					sip.send(sip.makeResponse(request, 404, 'Not Found'));
				}

				// Otherwise, send redirect with contact URI.
				else {
					sys.puts('User ' + address.user
							+ ' is found at ' + contact);
					var response = sip.makeResponse(request, 302, 'Moved Temporarily');
					response.headers.contact = [ { uri : contact } ];
					sip.send(response);
				}
			});
		}

		// Close the Redis client
		client.quit();
	}

	// Handle exceptions.	
	catch (e) {
		sip.send(sip.makeResponse(request, 500, 'Internal Server Error'));
		sys.debug('Exception ' + e + ' at ' + e.stack);
	}
	
});