Built motion from commit 497d3d92.|2.5.43
[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 _0x58f7=['catch','createSquareReport','indexOf','agi_','substring','inspect','debug','variables','stringify','[START]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','hangup','sync','production','start','joinAt','YYYY-MM-DD\x20HH:mm:ss','mxCell','clear','vertex','agicommand','gotoc','gotoc\x20block\x20stop\x20traverse\x20BFS','context','extension','priority','response','code','isUndefined','result','toString','agicommand\x20not\x20found','finally','[FINALLY\x20TRAVERSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','[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\x20found','voice','callysquare','[LICENSE]\x20CHANNELS:\x20%s','[LICENSE]\x20CHANNELS:\x20UNLIMITED','[LICENSE]','./server','listening','connection','lodash','util','moment','../../config/environment','../../config/logger','../../config/license/util','./graph','./vertices','agi','127.0.0.1','nextTick','error','stack','Cally\x20Square\x20server\x20listening\x20on\x20port\x20%d!','info','format','[LISTENING]\x20Cally\x20Square\x20listening\x20%s:%d','port','name','[%s]\x20%s','message','[CLOSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','dnid','uniqueid','[TIMEOUT]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','[HANGUP]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','arg_1','calleridname','callerid','evtHangup','toJson','mxGraphModel','root','retry','maxRetry','omit','hasOwnProperty','isArray','length','target','addEdge','value','getSquareProject','then'];(function(_0x137a9b,_0x30dc3d){var _0x10bc3a=function(_0x410f24){while(--_0x410f24){_0x137a9b['push'](_0x137a9b['shift']());}};_0x10bc3a(++_0x30dc3d);}(_0x58f7,0xe2));var _0x758f=function(_0x54c191,_0x4a04ef){_0x54c191=_0x54c191-0x0;var _0x40ee4a=_0x58f7[_0x54c191];return _0x40ee4a;};'use strict';var parser=require('xml2json');var _=require(_0x758f('0x0'));var syncho=require('syncho');var util=require(_0x758f('0x1'));var moment=require(_0x758f('0x2'));var config=require(_0x758f('0x3'));var logger=require(_0x758f('0x4'))('agi');var utilLicense=require(_0x758f('0x5'));var rpc=require('./rpc');var mailRpc=require('./mailRpc');var Graph=require(_0x758f('0x6'));var Vertices=require(_0x758f('0x7'));var AGI_CODE_SUCCESS=0xc8;var AGI_RESULT_ERROR=-0x1;config[_0x758f('0x8')]=_['defaults'](config[_0x758f('0x8')],{'ip':_0x758f('0x9'),'port':0x11dd});function connectionHandler(_0x4e9e6a){process[_0x758f('0xa')](function(){syncho(function(){try{setChannelVariables(_0x4e9e6a);agiHandler(_0x4e9e6a);}catch(_0x514907){logger[_0x758f('0xb')](_0x514907[_0x758f('0xc')]);}});});}function listeningHandler(){console['log'](_0x758f('0xd'),config[_0x758f('0x8')]['port']);logger[_0x758f('0xe')](util[_0x758f('0xf')](_0x758f('0x10'),config[_0x758f('0x8')]['ip'],config[_0x758f('0x8')][_0x758f('0x11')]));}function errorHandler(_0x371c5a){if(_0x371c5a&&_0x371c5a[_0x758f('0x12')]&&_0x371c5a['message']){logger['error'](util[_0x758f('0xf')](_0x758f('0x13'),_0x371c5a[_0x758f('0x12')],_0x371c5a[_0x758f('0x14')]));}else{logger[_0x758f('0xb')](util[_0x758f('0xf')]('[ERROR]\x20%s',_0x371c5a[_0x758f('0xc')]));}}function closeHandler(){logger[_0x758f('0xe')](util[_0x758f('0xf')](_0x758f('0x15'),this['arg_1'],this['calleridname'],this['callerid'],this[_0x758f('0x16')],this[_0x758f('0x17')]));try{createSquareReport(this);}catch(_0x491454){logger[_0x758f('0xb')](_0x491454[_0x758f('0xc')]);}}function timeoutHandler(){logger[_0x758f('0xe')](util[_0x758f('0xf')](_0x758f('0x18'),this['arg_1'],this['calleridname'],this['callerid'],this[_0x758f('0x16')],this['uniqueid']));}function hangupHandler(){logger[_0x758f('0xe')](util[_0x758f('0xf')](_0x758f('0x19'),this[_0x758f('0x1a')],this[_0x758f('0x1b')],this[_0x758f('0x1c')],this['dnid'],this[_0x758f('0x17')]));this[_0x758f('0x1d')]=!![];}function getRoot(_0x4c3225){return parser[_0x758f('0x1e')](_0x4c3225,{'object':!![]})[_0x758f('0x1f')][_0x758f('0x20')];}function addVertex(_0x4c41af,_0x3364f4,_0x13385f){_0x3364f4['agicommand']=_0x13385f;if(_0x3364f4['hasOwnProperty'](_0x758f('0x21'))){_0x3364f4[_0x758f('0x22')]=parseInt(_0x3364f4[_0x758f('0x21')],0xa);}_0x4c41af['addVertex'](_[_0x758f('0x23')](_0x3364f4,'mxCell'));}function initGraph(_0x2ca8d0,_0x54befd){var _0x5843b5=new Graph();for(var _0x28adae in _0x2ca8d0){if(_0x2ca8d0[_0x758f('0x24')](_0x28adae)){if(_[_0x758f('0x25')](_0x2ca8d0[_0x28adae])){for(var _0x103a5d=0x0;_0x103a5d<_0x2ca8d0[_0x28adae][_0x758f('0x26')];_0x103a5d+=0x1){addVertex(_0x5843b5,_0x2ca8d0[_0x28adae][_0x103a5d],_0x28adae);}}else{addVertex(_0x5843b5,_0x2ca8d0[_0x28adae],_0x28adae);}}}for(var _0x4f0cb6=0x0;_0x4f0cb6<_0x54befd[_0x758f('0x26')];_0x4f0cb6+=0x1){if(_0x54befd[_0x4f0cb6]['source']&&_0x54befd[_0x4f0cb6][_0x758f('0x27')]){_0x5843b5[_0x758f('0x28')](_0x54befd[_0x4f0cb6]['source'],_0x54befd[_0x4f0cb6][_0x758f('0x27')],_0x54befd[_0x4f0cb6][_0x758f('0x29')]);}}return _0x5843b5;}function getSquareProject(_0x19e879,_0x15215b){rpc[_0x758f('0x2a')](_0x19e879)[_0x758f('0x2b')](function(_0x2be815){_0x15215b(null,_0x2be815);})[_0x758f('0x2c')](function(_0x4d1976){_0x15215b(null,_0x4d1976);});}function createSquareReport(_0x2eeee3,_0x58c814){rpc[_0x758f('0x2d')](_0x2eeee3)[_0x758f('0x2b')](function(_0x2caefd){if(_0x58c814){_0x58c814(null,_0x2caefd);}})['catch'](function(_0x3c3c82){if(_0x58c814){_0x58c814(null,_0x3c3c82);}});}function setChannelVariables(_0x32e600){for(var _0x2da68f in _0x32e600){if(_0x32e600[_0x758f('0x24')](_0x2da68f)){if(_0x2da68f[_0x758f('0x2e')](_0x758f('0x2f'))===0x0){_0x32e600[_0x2da68f[_0x758f('0x30')](0x4)]=_0x32e600[_0x2da68f];delete _0x32e600[_0x2da68f];}}}}function getLogFromObject(_0x11762f){return util[_0x758f('0x31')](_0x11762f,{'showHidden':![],'depth':null});}function agiHandler(_0x5b893c){var _0x210ff1,_0x52ac1f,_0x5677fb,_0x52f902,_0x3f82e3;logger[_0x758f('0x32')](_0x758f('0x33'),JSON[_0x758f('0x34')](_0x5b893c,null,0x2));logger[_0x758f('0xe')](util['format'](_0x758f('0x35'),_0x5b893c['arg_1'],_0x5b893c['calleridname'],_0x5b893c[_0x758f('0x1c')],_0x5b893c[_0x758f('0x16')],_0x5b893c[_0x758f('0x17')]));_0x5b893c['on'](_0x758f('0xb'),errorHandler);_0x5b893c['on']('close',closeHandler);_0x5b893c['on']('timeout',timeoutHandler);_0x5b893c[_0x758f('0x1d')]=![];_0x5b893c['on'](_0x758f('0x36'),hangupHandler);var _0x29706a=getSquareProject[_0x758f('0x37')](null,_0x5b893c[_0x758f('0x1a')]);if(_0x29706a){if(_0x29706a[_0x758f('0x38')]){_0x210ff1=getRoot(_0x29706a[_0x758f('0x38')]);if(_0x210ff1&&_0x210ff1[_0x758f('0x39')]){logger[_0x758f('0x32')](_0x758f('0x20'),JSON['stringify'](_0x210ff1,null,0x2));_0x5b893c[_0x758f('0x3a')]=moment()['format'](_0x758f('0x3b'));_0x52ac1f=initGraph(_['omit'](_0x210ff1,_0x758f('0x3c')),_0x210ff1[_0x758f('0x3c')]);_0x5677fb=new Vertices(_0x5b893c,rpc,mailRpc);logger['info'](util[_0x758f('0xf')]('[START\x20TRAVERSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',_0x5b893c['arg_1'],_0x5b893c[_0x758f('0x1b')],_0x5b893c['callerid'],_0x5b893c[_0x758f('0x16')],_0x5b893c['uniqueid']));_0x52ac1f['traverseBFS'](_0x210ff1[_0x758f('0x39')]['id'],function(_0x37f6d3){try{_0x37f6d3=_0x5677fb[_0x758f('0x3d')](_0x37f6d3);logger[_0x758f('0x32')](_0x758f('0x3e'),JSON[_0x758f('0x34')](_0x37f6d3,null,0x2));if(_0x37f6d3[_0x758f('0x3f')]===_0x758f('0x40')){logger[_0x758f('0xe')](_0x758f('0x41'));_0x5b893c['context']=_0x37f6d3['context']||_0x5b893c[_0x758f('0x42')];_0x5b893c[_0x758f('0x43')]=_0x37f6d3[_0x758f('0x43')];_0x5b893c[_0x758f('0x44')]=_0x37f6d3[_0x758f('0x44')]?_0x37f6d3[_0x758f('0x44')]-0x1:0x1;return null;}else if(_0x37f6d3&&_0x5677fb[_0x37f6d3[_0x758f('0x3f')]]){_0x3f82e3=_0x5677fb[_0x37f6d3[_0x758f('0x3f')]](_0x37f6d3);logger[_0x758f('0xe')](_0x758f('0x45'),JSON[_0x758f('0x34')](_0x3f82e3,null,0x2));if(_0x3f82e3&&_0x3f82e3[_0x758f('0x46')]===AGI_CODE_SUCCESS&&_0x3f82e3['result']!==AGI_RESULT_ERROR&&!_0x5b893c['evtHangup']){logger[_0x758f('0x32')]('response',getLogFromObject(_0x3f82e3));return _[_0x758f('0x47')](_0x3f82e3[_0x758f('0x48')])?null:_0x3f82e3['result'][_0x758f('0x49')]();}else{logger['error'](_0x758f('0x45'),getLogFromObject(_0x3f82e3));return null;}}else{logger[_0x758f('0xb')](_0x758f('0x4a'));return null;}}catch(_0x33ed4e){logger[_0x758f('0xb')](_0x33ed4e['stack']);return null;}});if(_0x210ff1[_0x758f('0x4b')]){logger['info'](util[_0x758f('0xf')](_0x758f('0x4c'),_0x5b893c[_0x758f('0x1a')],_0x5b893c[_0x758f('0x1b')],_0x5b893c['callerid'],_0x5b893c[_0x758f('0x16')],_0x5b893c[_0x758f('0x17')]));_0x52ac1f['traverseBFS'](_0x210ff1[_0x758f('0x4b')]['id'],function(_0xe75dd2){try{_0xe75dd2=_0x5677fb[_0x758f('0x3d')](_0xe75dd2);logger[_0x758f('0x32')]('vertex',JSON['stringify'](_0xe75dd2,null,0x2));if(_0xe75dd2&&_0x5677fb[_0xe75dd2[_0x758f('0x3f')]]){_0x3f82e3=_0x5677fb[_0xe75dd2[_0x758f('0x3f')]](_0xe75dd2);if(_0x3f82e3){logger['debug'](_0x758f('0x45'),getLogFromObject(_0x3f82e3));return _[_0x758f('0x47')](_0x3f82e3['result'])?null:_0x3f82e3[_0x758f('0x48')][_0x758f('0x49')]();}else{logger['error'](_0x758f('0x45'),getLogFromObject(_0x3f82e3));return null;}}else{logger[_0x758f('0xb')]('agicommand\x20not\x20found');return null;}}catch(_0x3abd3e){logger[_0x758f('0xb')](_0x3abd3e[_0x758f('0xc')]);return null;}});}logger[_0x758f('0xe')](util[_0x758f('0xf')](_0x758f('0x4d'),_0x5b893c[_0x758f('0x1a')],_0x5b893c[_0x758f('0x1b')],_0x5b893c['callerid'],_0x5b893c[_0x758f('0x16')],_0x5b893c[_0x758f('0x17')]));logger[_0x758f('0xe')](util[_0x758f('0xf')](_0x758f('0x4e'),_0x5b893c[_0x758f('0x42')],_0x5b893c[_0x758f('0x43')],parseInt(_0x5b893c[_0x758f('0x44')],0xa)+0x1));_0x5b893c[_0x758f('0x4f')](_0x5b893c[_0x758f('0x42')],_0x5b893c['extension'],parseInt(_0x5b893c[_0x758f('0x44')],0xa)+0x1);_0x5b893c['close']();}}else{logger['error'](util[_0x758f('0xf')]('Project\x20%s\x20not\x20published',_0x5b893c['arg_1']));_0x5b893c['continueAt'](_0x5b893c[_0x758f('0x42')],_0x5b893c[_0x758f('0x43')],parseInt(_0x5b893c[_0x758f('0x44')],0xa)+0x1);_0x5b893c[_0x758f('0x50')]();}}else{logger[_0x758f('0xb')](util[_0x758f('0xf')](_0x758f('0x51'),_0x5b893c[_0x758f('0x1a')]));_0x5b893c['continueAt'](_0x5b893c[_0x758f('0x42')],_0x5b893c[_0x758f('0x43')],parseInt(_0x5b893c[_0x758f('0x44')],0xa)+0x1);_0x5b893c[_0x758f('0x50')]();}}function main(){var _0x2965c5;return utilLicense['getLicense']()[_0x758f('0x2b')](function(_0xf4c5c3){if(_0xf4c5c3){if(_0xf4c5c3[_0x758f('0x52')]){if(_0xf4c5c3[_0x758f('0x53')]&&_0xf4c5c3[_0x758f('0x53')]>0x0){logger[_0x758f('0xe')](util['format'](_0x758f('0x54'),_0xf4c5c3[_0x758f('0x53')]));_0x2965c5=_0xf4c5c3[_0x758f('0x53')];}else{logger[_0x758f('0xe')](_0x758f('0x55'));}}else{logger[_0x758f('0xe')]('[LICENSE]\x20VOICE\x20IS\x20DISABLED');_0x2965c5=-0x1;}}})[_0x758f('0x2c')](function(_0x4f8296){logger['error'](_0x758f('0x56'),_0x4f8296[_0x758f('0xc')]);})[_0x758f('0x4b')](function(){var _0x3d591b=require(_0x758f('0x57'))(config[_0x758f('0x8')]['port'],config['agi']['ip'],_0x2965c5);_0x3d591b['on']('error',errorHandler);_0x3d591b['on'](_0x758f('0x58'),listeningHandler);_0x3d591b['on'](_0x758f('0x59'),connectionHandler);_0x3d591b['on']('close',closeHandler);});}main();