Every now and then I throw together quick little one-off scripts for testing things that might benefit a wider audience.

Sometimes it's handy to test how particular devices or platforms handle specific rejection codes, so I threw together this little tool in node.

Basically, it runs at the command line and listens on a non-standard port (8877) and replies to every SIP INVITE message it receives with the rejection code you enter on the command line.

You can change the rejection code just by entering a new one at the prompt, thereafter any calls will be rejected with that new code.

It's not perfect, but it's far easier for testing SIP rejections than my old way of editing an Asterisk or FreeSWITCH config file, reloading it, making another call, then re-opening the config, reloading etc etc!

Someone might find it useful.

#!/usr/bin/node
var sip = require('sip');
var readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);

var defcode = 500;
rl.setPrompt('replycode [500]> ');
rl.prompt();
rl.on('line', function(line) {
    if (line.trim().match(/^[3456][0-9][0-9]$/)) {
      defcode = line.trim();
      console.log("All calls will be rejected with "+defcode);
      rl.setPrompt('replycode ['+defcode+']> ');
    } else {
      console.log("I can only respond with sensible cause codes!");
    }
    rl.prompt();
}).on('close',function(){
    process.exit(0);
});


sip.start({port: 8877}, function(request) {
  if (request.method == 'INVITE') {
    var uri = sip.parseUri(request.uri);
    var from = sip.parseUri(request.headers["from"].uri);
    var cid = request.headers["call-id"];
    var cli = from.user;
    var replycode = uri.user;
    var response = sip.makeResponse(request,	defcode, 'Cause '+defcode);
    console.log('causeCodeTester ['+cid+'] call from '+cli+' to '+uri.user+' replying with '+defcode);
    response.headers["Server"] = 'SIP Cause Code Tester (RMK)/0.2';
    sip.send(response);
  } else {
    // ignore
  }
})