Commit 3309cfa1 authored by antirek's avatar antirek

remove some lines

parent fb764f1a
var AGIServer = require('./../lib/index');
var handler = function (context) {
context.onEvent('variables')
context.onEvent('variables')
.then(function (vars) {
return context.streamFile('beep');
console.log('vars', vars);
return context.streamFile('beep', '#');
})
.then(function (result) {
return context.setVariable('RECOGNITION_RESULT', 'I\'m your father, Luc');
})
.then(function (result) {
return context.end();
});
})
.fail(console.log);
};
var agi = new AGIServer(handler);
agi.start(3000);
\ No newline at end of file
var agi = new AGIServer(handler, {debug: true});
agi.start(3007);
\ No newline at end of file
......@@ -330,6 +330,7 @@ module.exports = [
}
},
{
default: '#',
prepare: function (value) {
return '"' + value + '"';
}
......
......@@ -4,63 +4,53 @@ var state = require('./state');
var Q = require('q');
var commands = require('./command');
//base context
var Context = function (stream, debug) {
EventEmitter.call(this);
this.debug = debug;
this.stream = new Readable();
//this.stream = stream;
this.stream.setEncoding('utf8');
this.stream.wrap(stream);
this.state = state.init;
this.msg = "";
this.variables = {};
this.pending = null;
var self = this;
this.stream.on('readable', function () {
//always keep the 'leftover' part of the message
//always keep the 'leftover' part of the message
self.msg = self.read();
});
//this.msg = this.read();
this.variables = {};
this.pending = null;
this.stream.on('error', this.emit.bind(this, 'error'));
this.stream.on('close', this.emit.bind(this, 'close'));
};
require('util').inherits(Context, EventEmitter);
Context.prototype.read = function() {
var buffer = this.stream.read();
Context.prototype.read = function () {
var buffer = this.stream.read();
if (!buffer) return this.msg;
this.msg += buffer; //.toString('utf8');
this.msg += buffer;
if (this.state === state.init) {
if(this.msg.indexOf('\n\n') < 0) return this.msg; //we don't have whole message
if (this.msg.indexOf('\n\n') < 0) return this.msg; //we don't have whole message
this.readVariables(this.msg);
} else if (this.state === state.waiting) {
if(this.msg.indexOf('\n') < 0) return this.msg; //we don't have whole message
if (this.msg.indexOf('\n') < 0) return this.msg; //we don't have whole message
this.readResponse(this.msg);
}
return "";
return '';
};
Context.prototype.readVariables = function (msg) {
var lines = msg.split('\n');
/*
for(var i = 0; i < lines.length; i++) {
var line = lines[i];
var split = line.split(':')
var name = split[0];
var value = split[1];
this.variables[name] = (value||'').trim();
}*/
var lines = msg.split('\n');
lines.map(function (line) {
var split = line.split(':');
......@@ -71,33 +61,30 @@ Context.prototype.readVariables = function (msg) {
this.emit('variables', this.variables);
this.setState(state.waiting);
//return "";
};
Context.prototype.readResponse = function (msg) {
var lines = msg.split('\n');
/*
for(var i = 0; i < lines.length; i++) {
this.readResponseLine(lines[i]);
}
*/
lines.map(function (line) {
this.readResponseLine(line);
}, this);
//return "";
};
Context.prototype.readResponseLine = function (line) {
if (!line) return;
var parsed = /^(\d{3})(?: result=)(.*)/.exec(line);
//var parsed = /^(\d{3})(?: result=)(.*)/.exec(line);
var parsed = /^(\d{3})(?: result=)([^(]*)(?:\((.*)\))?/.exec(line);
if (!parsed) {
return this.emit('hangup');
}
var response = {
code: parseInt(parsed[1]),
result: parsed[2]
result: parsed[2].trim(),
};
//our last command had a pending callback
......@@ -109,11 +96,11 @@ Context.prototype.readResponseLine = function (line) {
this.emit('response', response);
}
Context.prototype.setState = function(state) {
Context.prototype.setState = function (state) {
this.state = state;
};
Context.prototype.send = function(msg, cb) {
Context.prototype.send = function (msg, cb) {
this.pending = cb;
this.stream.write(msg);
};
......@@ -146,16 +133,14 @@ Context.prototype.onEvent = function (event) {
return defer.promise;
};
Context.prototype.dial = function (num, timeout, params) {
return this.exec('Dial', num + ',' + timeout + ',' + params);
};
//additional agi commands
commands.map(function (command) {
var str = '';
Context.prototype[command.name] = function () {
if (command.params > 0) {
var args = [].slice.call(arguments, 0, command.params);
str = command.command + " " + prepareArgs(args, command.paramRules).join(" ");
str = command.command + " " + prepareArgs(args, command.paramRules, command.params).join(" ");
} else {
str = command.command;
}
......@@ -163,16 +148,35 @@ commands.map(function (command) {
};
});
var prepareArgs = function (args, argsRules) {
var q;
if (argsRules) {
q = args.map(function (arg, i) {
return (argsRules[i]) ? argsRules[i].prepare(arg) : arg;
var prepareArgs = function (args, argsRules, count) {
var q, argsP = [];
if (argsRules && count) {
args = args.map(function (arg){
return arg.toString();
});
for (var i = 0; i < count; i++) {
argsP[i] = (args[i]) ?
args[i] :
((argsRules[i] && argsRules[i].default) ?
argsRules[i].default :
'');
}
q = argsP.map(function (arg, i) {
return (argsRules[i] && argsRules[i].prepare) ? argsRules[i].prepare(arg) : arg;
});
} else {
q = args;
}
return q;
}
};
//sugar commands
Context.prototype.dial = function (target, timeout, params) {
return this.exec('Dial', target + ',' + timeout + ',' + params);
};
module.exports = Context;
\ No newline at end of file
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