Commit d37f0f76 authored by sergey's avatar sergey

add new functions

parent ec81511f
......@@ -2,14 +2,14 @@ var Readable = require('readable-stream');
var EventEmitter = require('events').EventEmitter;
var state = require('./state');
var Context = function(stream) {
var Context = function (stream) {
EventEmitter.call(this);
this.stream = new Readable();
this.stream.wrap(stream);
this.state = state.init;
this.msg = "";
var self = this;
this.stream.on('readable', function() {
this.stream.on('readable', function () {
//always keep the 'leftover' part of the message
self.msg = self.read();
});
......@@ -36,7 +36,7 @@ Context.prototype.read = function() {
return "";
};
Context.prototype.readVariables = function(msg) {
Context.prototype.readVariables = function (msg) {
var lines = msg.split('\n');
for(var i = 0; i < lines.length; i++) {
var line = lines[i];
......@@ -50,7 +50,7 @@ Context.prototype.readVariables = function(msg) {
return "";
};
Context.prototype.readResponse = function(msg) {
Context.prototype.readResponse = function (msg) {
var lines = msg.split('\n');
for(var i = 0; i < lines.length; i++) {
this.readResponseLine(lines[i]);
......@@ -58,9 +58,11 @@ Context.prototype.readResponse = function(msg) {
return "";
};
Context.prototype.readResponseLine = function(line) {
Context.prototype.readResponseLine = function (line) {
if(!line) return;
var parsed = /^(\d{3})(?: result=)(.*)/.exec(line);
if(!parsed) {
return this.emit('hangup');
}
......@@ -92,7 +94,7 @@ Context.prototype.exec = function() {
var last = args.pop();
if(typeof last !== 'function') {
args.push(last);
last = function() { }
last = function () { }
}
this.send('EXEC ' + args.join(' ') + '\n', last);
};
......@@ -102,39 +104,63 @@ Context.prototype.dial = function (num, timeout, params, cb) {
};
Context.prototype.getVariable = function (name, cb) {
this.send('GET VARIABLE ' + name + '\n', cb || function() { });
this.send('GET VARIABLE ' + name + '\n', cb || function () { });
};
Context.prototype.getFullVariable = function (variable, channel, cb) {
this.send('GET FULL VARIABLE ' + variable + ' ' + channel + '\n', cb || function() { });
this.send('GET FULL VARIABLE ' + variable + ' ' + channel + '\n', cb || function () { });
};
Context.prototype.getData = function (file, timeout, maxdigits, cb) {
this.send('GET DATA ' + file + ' ' + timeout + ' ' + maxdigits + '\n', cb || function () { });
};
Context.prototype.getOption = function (file, escape_digits, timeout, cb) {
this.send('GET OPTION ' + file + ' "' + escape_digits + '" ' + timeout + '\n', cb || function () { });
};
Context.prototype.receiveChar = function (timeout, cb) {
this.send('RECEIVE CHAR ' + timeout + '\n', cb || function () { });
};
Context.prototype.receiveText = function (timeout, cb) {
this.send('RECEIVE TEXT ' + timeout + '\n', cb || function () { });
};
Context.prototype.setAutoHangup = function (seconds, cb) {
this.send('SET AUTOHANGUP ' + seconds + '\n', cb || function() { });
this.send('SET AUTOHANGUP ' + seconds + '\n', cb || function () { });
};
Context.prototype.setCallerID = function (number, cb) {
this.send('SET CALLERID ' + number + '\n', cb || function() { });
this.send('SET CALLERID ' + number + '\n', cb || function () { });
};
Context.prototype.setContext = function (context, cb) {
this.send('SET CONTEXT ' + context + '\n', cb || function() { });
this.send('SET CONTEXT ' + context + '\n', cb || function () { });
};
Context.prototype.setExtension = function (extension, cb) {
this.send('SET EXTENSION ' + extension + '\n', cb || function() { });
this.send('SET EXTENSION ' + extension + '\n', cb || function () { });
};
Context.prototype.setPriority = function (priority, cb) {
this.send('SET PRIORITY ' + priority + '\n', cb || function() { });
this.send('SET PRIORITY ' + priority + '\n', cb || function () { });
};
Context.prototype.setMusic = function (musicclass, cb) {
this.send('SET MUSIC ' + musicclass + '\n', cb || function() { });
this.send('SET MUSIC ' + musicclass + '\n', cb || function () { });
};
Context.prototype.setVariable = function (name, value, cb) {
this.send('SET VARIABLE ' + name + ' ' + value + '\n', cb || function() { });
this.send('SET VARIABLE ' + name + ' ' + value + '\n', cb || function () { });
};
Context.prototype.sendImage = function (image, cb) {
this.send('SEND IMAGE ' + image + '\n', cb || function() { });
};
Context.prototype.sendText = function (text, cb) {
this.send('SEND TEXT "' + text + '"\n', cb || function() { });
};
Context.prototype.channelStatus = function (name, cb) {
......@@ -142,12 +168,29 @@ Context.prototype.channelStatus = function (name, cb) {
};
Context.prototype.answer = function (cb) {
this.send('ANSWER' + '\n', cb || function() { });
this.send('ANSWER\n', cb || function () { });
};
Context.prototype.verbose = function (message, level, cb) {
this.send('VERBOSE "' + message + '" ' + level + '\n', cb || function () { });
};
Context.prototype.tddMode = function (value, cb) {
this.send('TDD MODE ' + value + '\n', cb || function () { });
};
Context.prototype.noop = function (cb) {
this.send('NOOP\n', cb || function () { });
};
Context.prototype.gosub = function (context, extension, priority, option, cb) {
var str = [context, extension, priority, option].join(' ');
this.send('GOSUB ' + str + '\n', cb || function () { });
};
Context.prototype.recordFile = function (filename, format, escape_digits, timeout, cb) {
var str = [
'"'+filename+'"',
'"' + filename + '"',
format,
escape_digits,
parseInt(timeout)*1000,
......
......@@ -2,7 +2,7 @@
"author": "Sergey Dmitriev <serge.dmitriev@gmail.com>",
"name": "ding-dong",
"description": "AGI (Asterisk Gateway Interface) for writing dialplan scripts",
"version": "0.0.6",
"version": "0.0.7",
"repository": {
"type": "git",
"url": "git://github.com/antirek/ding-dong.git"
......
......@@ -209,6 +209,20 @@ describe('Context', function() {
});
});
describe('getData', function() {
it('sends correct command', function() {
this.context.getData('test', 10, 5);
expect(this.context.sent.join('')).to.eql('GET DATA test 10 5\n');
});
});
describe('getOption', function() {
it('sends correct command', function() {
this.context.getOption('test', '#', 5);
expect(this.context.sent.join('')).to.eql('GET OPTION test "#" 5\n');
});
});
describe('getVariable', function() {
it('sends correct command', function() {
this.context.getVariable('test');
......@@ -227,6 +241,20 @@ describe('Context', function() {
});
});
describe('receiveChar', function() {
it('sends correct command', function() {
this.context.receiveChar(5);
expect(this.context.sent.join('')).to.eql('RECEIVE CHAR 5\n');
});
});
describe('receiveText', function() {
it('sends correct command', function() {
this.context.receiveText(5);
expect(this.context.sent.join('')).to.eql('RECEIVE TEXT 5\n');
});
});
describe('stream file', function() {
it('sends', function () {
this.context.streamFile('test', '1234567890#*', function() {});
......@@ -303,6 +331,20 @@ describe('Context', function() {
});
});
describe('send image', function() {
it('send image', function () {
this.context.sendImage('1234', function() {});
expect(this.context.sent.join('')).to.eql('SEND IMAGE 1234\n');
});
});
describe('send text', function() {
it('send text', function () {
this.context.sendText('1234', function() {});
expect(this.context.sent.join('')).to.eql('SEND TEXT "1234"\n');
});
});
describe('waitForDigit', function () {
it('sends with default timeout', function() {
this.context.waitForDigit(function() {});
......@@ -336,6 +378,34 @@ describe('Context', function() {
});
});
describe('verbose', function() {
it('sends correct command', function() {
this.context.verbose("good", 2);
expect(this.context.sent.join('')).to.eql('VERBOSE "good" 2\n');
});
});
describe('tddMode', function() {
it('sends correct command', function() {
this.context.tddMode("on");
expect(this.context.sent.join('')).to.eql('TDD MODE on\n');
});
});
describe('noop', function() {
it('sends correct command', function() {
this.context.noop();
expect(this.context.sent.join('')).to.eql('NOOP\n');
});
});
describe('gosub', function() {
it('sends correct command', function() {
this.context.gosub('out','241','6','do');
expect(this.context.sent.join('')).to.eql('GOSUB out 241 6 do\n');
});
});
describe('events', function() {
describe('error', function () {
it('is emitted when socket emits error', function(done) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment