Built motion from commit 753c950e.|2.5.42
[motion2.git] / server / services / agi / index.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0xe285=['message','error','[ERROR]\x20%s','[CLOSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','arg_1','uniqueid','[TIMEOUT]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','calleridname','callerid','[HANGUP]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','toJson','mxGraphModel','root','hasOwnProperty','retry','maxRetry','addVertex','omit','mxCell','isArray','length','target','source','value','catch','createSquareReport','then','indexOf','agi_','inspect','debug','variables','stringify','[START]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','dnid','evtHangup','hangup','production','joinAt','YYYY-MM-DD\x20HH:mm:ss','[START\x20TRAVERSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','traverseBFS','start','clear','vertex','agicommand','gotoc\x20block\x20stop\x20traverse\x20BFS','context','extension','priority','response','result','isUndefined','toString','finally','[FINALLY\x20TRAVERSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','agicommand\x20not\x20found','[END]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','[GOTO]\x20context:\x20%s\x20extension:\x20%s\x20priority:\x20%s','continueAt','close','Project\x20%s\x20not\x20published','Project\x20%s\x20not\x20found','getLicense','voice','callysquare','[LICENSE]\x20CHANNELS:\x20UNLIMITED','[LICENSE]\x20VOICE\x20IS\x20DISABLED','listening','connection','lodash','syncho','util','moment','../../config/environment','../../config/logger','../../config/license/util','./mailRpc','./vertices','agi','127.0.0.1','nextTick','stack','log','Cally\x20Square\x20server\x20listening\x20on\x20port\x20%d!','info','format','[LISTENING]\x20Cally\x20Square\x20listening\x20%s:%d','port','name'];(function(_0x38bc08,_0x39ab88){var _0x8110e4=function(_0x2216db){while(--_0x2216db){_0x38bc08['push'](_0x38bc08['shift']());}};_0x8110e4(++_0x39ab88);}(_0xe285,0xfa));var _0x5e28=function(_0x5608a3,_0x5b33c5){_0x5608a3=_0x5608a3-0x0;var _0x1ed50a=_0xe285[_0x5608a3];return _0x1ed50a;};'use strict';var parser=require('xml2json');var _=require(_0x5e28('0x0'));var syncho=require(_0x5e28('0x1'));var util=require(_0x5e28('0x2'));var moment=require(_0x5e28('0x3'));var config=require(_0x5e28('0x4'));var logger=require(_0x5e28('0x5'))('agi');var utilLicense=require(_0x5e28('0x6'));var rpc=require('./rpc');var mailRpc=require(_0x5e28('0x7'));var Graph=require('./graph');var Vertices=require(_0x5e28('0x8'));var AGI_CODE_SUCCESS=0xc8;var AGI_RESULT_ERROR=-0x1;config[_0x5e28('0x9')]=_['defaults'](config[_0x5e28('0x9')],{'ip':_0x5e28('0xa'),'port':0x11dd});function connectionHandler(_0x29fe1b){process[_0x5e28('0xb')](function(){syncho(function(){try{setChannelVariables(_0x29fe1b);agiHandler(_0x29fe1b);}catch(_0x571af7){logger['error'](_0x571af7[_0x5e28('0xc')]);}});});}function listeningHandler(){console[_0x5e28('0xd')](_0x5e28('0xe'),config[_0x5e28('0x9')]['port']);logger[_0x5e28('0xf')](util[_0x5e28('0x10')](_0x5e28('0x11'),config['agi']['ip'],config['agi'][_0x5e28('0x12')]));}function errorHandler(_0x4f6aab){if(_0x4f6aab&&_0x4f6aab[_0x5e28('0x13')]&&_0x4f6aab[_0x5e28('0x14')]){logger[_0x5e28('0x15')](util['format']('[%s]\x20%s',_0x4f6aab['name'],_0x4f6aab[_0x5e28('0x14')]));}else{logger[_0x5e28('0x15')](util[_0x5e28('0x10')](_0x5e28('0x16'),_0x4f6aab[_0x5e28('0xc')]));}}function closeHandler(){logger[_0x5e28('0xf')](util[_0x5e28('0x10')](_0x5e28('0x17'),this[_0x5e28('0x18')],this['calleridname'],this['callerid'],this['dnid'],this[_0x5e28('0x19')]));try{createSquareReport(this);}catch(_0x375ba5){logger['error'](_0x375ba5[_0x5e28('0xc')]);}}function timeoutHandler(){logger[_0x5e28('0xf')](util[_0x5e28('0x10')](_0x5e28('0x1a'),this[_0x5e28('0x18')],this[_0x5e28('0x1b')],this[_0x5e28('0x1c')],this['dnid'],this[_0x5e28('0x19')]));}function hangupHandler(){logger[_0x5e28('0xf')](util['format'](_0x5e28('0x1d'),this[_0x5e28('0x18')],this[_0x5e28('0x1b')],this[_0x5e28('0x1c')],this['dnid'],this[_0x5e28('0x19')]));this['evtHangup']=!![];}function getRoot(_0x84c9a9){return parser[_0x5e28('0x1e')](_0x84c9a9,{'object':!![]})[_0x5e28('0x1f')][_0x5e28('0x20')];}function addVertex(_0x57df83,_0x3126f2,_0x242404){_0x3126f2['agicommand']=_0x242404;if(_0x3126f2[_0x5e28('0x21')](_0x5e28('0x22'))){_0x3126f2[_0x5e28('0x23')]=parseInt(_0x3126f2[_0x5e28('0x22')],0xa);}_0x57df83[_0x5e28('0x24')](_[_0x5e28('0x25')](_0x3126f2,_0x5e28('0x26')));}function initGraph(_0x3fad59,_0x35f13a){var _0x3fe0a0=new Graph();for(var _0x225e10 in _0x3fad59){if(_0x3fad59[_0x5e28('0x21')](_0x225e10)){if(_[_0x5e28('0x27')](_0x3fad59[_0x225e10])){for(var _0x6082c2=0x0;_0x6082c2<_0x3fad59[_0x225e10][_0x5e28('0x28')];_0x6082c2+=0x1){addVertex(_0x3fe0a0,_0x3fad59[_0x225e10][_0x6082c2],_0x225e10);}}else{addVertex(_0x3fe0a0,_0x3fad59[_0x225e10],_0x225e10);}}}for(var _0x3ea1fb=0x0;_0x3ea1fb<_0x35f13a['length'];_0x3ea1fb+=0x1){if(_0x35f13a[_0x3ea1fb]['source']&&_0x35f13a[_0x3ea1fb][_0x5e28('0x29')]){_0x3fe0a0['addEdge'](_0x35f13a[_0x3ea1fb][_0x5e28('0x2a')],_0x35f13a[_0x3ea1fb][_0x5e28('0x29')],_0x35f13a[_0x3ea1fb][_0x5e28('0x2b')]);}}return _0x3fe0a0;}function getSquareProject(_0x42b7be,_0x1470be){rpc['getSquareProject'](_0x42b7be)['then'](function(_0x43f2b0){_0x1470be(null,_0x43f2b0);})[_0x5e28('0x2c')](function(_0x1dce37){_0x1470be(null,_0x1dce37);});}function createSquareReport(_0x314f0e,_0x3b279c){rpc[_0x5e28('0x2d')](_0x314f0e)[_0x5e28('0x2e')](function(_0x469b2c){if(_0x3b279c){_0x3b279c(null,_0x469b2c);}})[_0x5e28('0x2c')](function(_0x3f5037){if(_0x3b279c){_0x3b279c(null,_0x3f5037);}});}function setChannelVariables(_0x2512ca){for(var _0x279b11 in _0x2512ca){if(_0x2512ca[_0x5e28('0x21')](_0x279b11)){if(_0x279b11[_0x5e28('0x2f')](_0x5e28('0x30'))===0x0){_0x2512ca[_0x279b11['substring'](0x4)]=_0x2512ca[_0x279b11];delete _0x2512ca[_0x279b11];}}}}function getLogFromObject(_0x227794){return util[_0x5e28('0x31')](_0x227794,{'showHidden':![],'depth':null});}function agiHandler(_0x106010){var _0x4916ac,_0x249508,_0x26f9b2,_0xbaa297,_0x52220f;logger[_0x5e28('0x32')](_0x5e28('0x33'),JSON[_0x5e28('0x34')](_0x106010,null,0x2));logger[_0x5e28('0xf')](util['format'](_0x5e28('0x35'),_0x106010[_0x5e28('0x18')],_0x106010['calleridname'],_0x106010[_0x5e28('0x1c')],_0x106010[_0x5e28('0x36')],_0x106010[_0x5e28('0x19')]));_0x106010['on']('error',errorHandler);_0x106010['on']('close',closeHandler);_0x106010['on']('timeout',timeoutHandler);_0x106010[_0x5e28('0x37')]=![];_0x106010['on'](_0x5e28('0x38'),hangupHandler);var _0x34ea4e=getSquareProject['sync'](null,_0x106010[_0x5e28('0x18')]);if(_0x34ea4e){if(_0x34ea4e[_0x5e28('0x39')]){_0x4916ac=getRoot(_0x34ea4e[_0x5e28('0x39')]);if(_0x4916ac&&_0x4916ac['start']){logger[_0x5e28('0x32')](_0x5e28('0x20'),JSON['stringify'](_0x4916ac,null,0x2));_0x106010[_0x5e28('0x3a')]=moment()[_0x5e28('0x10')](_0x5e28('0x3b'));_0x249508=initGraph(_[_0x5e28('0x25')](_0x4916ac,_0x5e28('0x26')),_0x4916ac[_0x5e28('0x26')]);_0x26f9b2=new Vertices(_0x106010,rpc,mailRpc);logger[_0x5e28('0xf')](util['format'](_0x5e28('0x3c'),_0x106010[_0x5e28('0x18')],_0x106010['calleridname'],_0x106010['callerid'],_0x106010[_0x5e28('0x36')],_0x106010[_0x5e28('0x19')]));_0x249508[_0x5e28('0x3d')](_0x4916ac[_0x5e28('0x3e')]['id'],function(_0x4aba43){try{_0x4aba43=_0x26f9b2[_0x5e28('0x3f')](_0x4aba43);logger[_0x5e28('0x32')](_0x5e28('0x40'),JSON['stringify'](_0x4aba43,null,0x2));if(_0x4aba43[_0x5e28('0x41')]==='gotoc'){logger[_0x5e28('0xf')](_0x5e28('0x42'));_0x106010[_0x5e28('0x43')]=_0x4aba43[_0x5e28('0x43')]||_0x106010['context'];_0x106010[_0x5e28('0x44')]=_0x4aba43['extension'];_0x106010[_0x5e28('0x45')]=_0x4aba43[_0x5e28('0x45')]?_0x4aba43[_0x5e28('0x45')]-0x1:0x1;return null;}else if(_0x4aba43&&_0x26f9b2[_0x4aba43[_0x5e28('0x41')]]){_0x52220f=_0x26f9b2[_0x4aba43[_0x5e28('0x41')]](_0x4aba43);logger[_0x5e28('0xf')](_0x5e28('0x46'),JSON['stringify'](_0x52220f,null,0x2));if(_0x52220f&&_0x52220f['code']===AGI_CODE_SUCCESS&&_0x52220f[_0x5e28('0x47')]!==AGI_RESULT_ERROR&&!_0x106010[_0x5e28('0x37')]){logger['debug']('response',getLogFromObject(_0x52220f));return _[_0x5e28('0x48')](_0x52220f[_0x5e28('0x47')])?null:_0x52220f[_0x5e28('0x47')][_0x5e28('0x49')]();}else{logger['error'](_0x5e28('0x46'),getLogFromObject(_0x52220f));return null;}}else{logger[_0x5e28('0x15')]('agicommand\x20not\x20found');return null;}}catch(_0x4cb990){logger['error'](_0x4cb990[_0x5e28('0xc')]);return null;}});if(_0x4916ac[_0x5e28('0x4a')]){logger[_0x5e28('0xf')](util[_0x5e28('0x10')](_0x5e28('0x4b'),_0x106010['arg_1'],_0x106010[_0x5e28('0x1b')],_0x106010['callerid'],_0x106010['dnid'],_0x106010[_0x5e28('0x19')]));_0x249508[_0x5e28('0x3d')](_0x4916ac[_0x5e28('0x4a')]['id'],function(_0x4749e6){try{_0x4749e6=_0x26f9b2[_0x5e28('0x3f')](_0x4749e6);logger[_0x5e28('0x32')](_0x5e28('0x40'),JSON[_0x5e28('0x34')](_0x4749e6,null,0x2));if(_0x4749e6&&_0x26f9b2[_0x4749e6[_0x5e28('0x41')]]){_0x52220f=_0x26f9b2[_0x4749e6[_0x5e28('0x41')]](_0x4749e6);if(_0x52220f){logger[_0x5e28('0x32')](_0x5e28('0x46'),getLogFromObject(_0x52220f));return _[_0x5e28('0x48')](_0x52220f[_0x5e28('0x47')])?null:_0x52220f['result']['toString']();}else{logger[_0x5e28('0x15')](_0x5e28('0x46'),getLogFromObject(_0x52220f));return null;}}else{logger[_0x5e28('0x15')](_0x5e28('0x4c'));return null;}}catch(_0x587f06){logger[_0x5e28('0x15')](_0x587f06[_0x5e28('0xc')]);return null;}});}logger[_0x5e28('0xf')](util[_0x5e28('0x10')](_0x5e28('0x4d'),_0x106010[_0x5e28('0x18')],_0x106010['calleridname'],_0x106010['callerid'],_0x106010['dnid'],_0x106010['uniqueid']));logger[_0x5e28('0xf')](util[_0x5e28('0x10')](_0x5e28('0x4e'),_0x106010['context'],_0x106010[_0x5e28('0x44')],parseInt(_0x106010[_0x5e28('0x45')],0xa)+0x1));_0x106010[_0x5e28('0x4f')](_0x106010[_0x5e28('0x43')],_0x106010[_0x5e28('0x44')],parseInt(_0x106010[_0x5e28('0x45')],0xa)+0x1);_0x106010[_0x5e28('0x50')]();}}else{logger['error'](util[_0x5e28('0x10')](_0x5e28('0x51'),_0x106010['arg_1']));_0x106010[_0x5e28('0x4f')](_0x106010[_0x5e28('0x43')],_0x106010[_0x5e28('0x44')],parseInt(_0x106010['priority'],0xa)+0x1);_0x106010[_0x5e28('0x50')]();}}else{logger['error'](util['format'](_0x5e28('0x52'),_0x106010['arg_1']));_0x106010[_0x5e28('0x4f')](_0x106010[_0x5e28('0x43')],_0x106010[_0x5e28('0x44')],parseInt(_0x106010['priority'],0xa)+0x1);_0x106010[_0x5e28('0x50')]();}}function main(){var _0x3a4b00;return utilLicense[_0x5e28('0x53')]()[_0x5e28('0x2e')](function(_0xd06a19){if(_0xd06a19){if(_0xd06a19[_0x5e28('0x54')]){if(_0xd06a19[_0x5e28('0x55')]&&_0xd06a19[_0x5e28('0x55')]>0x0){logger[_0x5e28('0xf')](util['format']('[LICENSE]\x20CHANNELS:\x20%s',_0xd06a19[_0x5e28('0x55')]));_0x3a4b00=_0xd06a19[_0x5e28('0x55')];}else{logger[_0x5e28('0xf')](_0x5e28('0x56'));}}else{logger[_0x5e28('0xf')](_0x5e28('0x57'));_0x3a4b00=-0x1;}}})['catch'](function(_0x5e04c3){logger[_0x5e28('0x15')]('[LICENSE]',_0x5e04c3[_0x5e28('0xc')]);})[_0x5e28('0x4a')](function(){var _0x1f854d=require('./server')(config[_0x5e28('0x9')][_0x5e28('0x12')],config[_0x5e28('0x9')]['ip'],_0x3a4b00);_0x1f854d['on'](_0x5e28('0x15'),errorHandler);_0x1f854d['on'](_0x5e28('0x58'),listeningHandler);_0x1f854d['on'](_0x5e28('0x59'),connectionHandler);_0x1f854d['on']('close',closeHandler);});}main();