edbd3072eeb124cc6e2a580f86308a2e67f51bab
[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 _0xdf0b=['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','evtHangup','toJson','agicommand','hasOwnProperty','maxRetry','retry','omit','mxCell','isArray','length','source','addEdge','getSquareProject','then','catch','createSquareReport','agi_','substring','inspect','debug','variables','[START]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','close','timeout','hangup','sync','production','start','root','stringify','joinAt','[START\x20TRAVERSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','traverseBFS','clear','vertex','gotoc\x20block\x20stop\x20traverse\x20BFS','context','extension','priority','code','result','response','isUndefined','agicommand\x20not\x20found','finally','[FINALLY\x20TRAVERSE]\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','Project\x20%s\x20not\x20published','Project\x20%s\x20not\x20found','voice','callysquare','[LICENSE]\x20CHANNELS:\x20%s','[LICENSE]\x20VOICE\x20IS\x20DISABLED','[LICENSE]','./server','xml2json','lodash','syncho','util','moment','../../config/environment','../../config/logger','../../config/license/util','./rpc','./mailRpc','defaults','agi','127.0.0.1','nextTick','error','port','info','message','format','[%s]\x20%s','name','[ERROR]\x20%s','stack','[CLOSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','arg_1','calleridname','callerid'];(function(_0x14e4c4,_0x3a4da5){var _0x47420b=function(_0x2f120d){while(--_0x2f120d){_0x14e4c4['push'](_0x14e4c4['shift']());}};_0x47420b(++_0x3a4da5);}(_0xdf0b,0x1ef));var _0xbdf0=function(_0x5a65a4,_0x500f11){_0x5a65a4=_0x5a65a4-0x0;var _0x552746=_0xdf0b[_0x5a65a4];return _0x552746;};'use strict';var parser=require(_0xbdf0('0x0'));var _=require(_0xbdf0('0x1'));var syncho=require(_0xbdf0('0x2'));var util=require(_0xbdf0('0x3'));var moment=require(_0xbdf0('0x4'));var config=require(_0xbdf0('0x5'));var logger=require(_0xbdf0('0x6'))('agi');var utilLicense=require(_0xbdf0('0x7'));var rpc=require(_0xbdf0('0x8'));var mailRpc=require(_0xbdf0('0x9'));var Graph=require('./graph');var Vertices=require('./vertices');var AGI_CODE_SUCCESS=0xc8;var AGI_RESULT_ERROR=-0x1;config['agi']=_[_0xbdf0('0xa')](config[_0xbdf0('0xb')],{'ip':_0xbdf0('0xc'),'port':0x11dd});function connectionHandler(_0x23585f){process[_0xbdf0('0xd')](function(){syncho(function(){try{setChannelVariables(_0x23585f);agiHandler(_0x23585f);}catch(_0x3b06cd){logger[_0xbdf0('0xe')](_0x3b06cd['stack']);}});});}function listeningHandler(){console['log']('Cally\x20Square\x20server\x20listening\x20on\x20port\x20%d!',config['agi'][_0xbdf0('0xf')]);logger[_0xbdf0('0x10')](util['format']('[LISTENING]\x20Cally\x20Square\x20listening\x20%s:%d',config[_0xbdf0('0xb')]['ip'],config[_0xbdf0('0xb')][_0xbdf0('0xf')]));}function errorHandler(_0xa24ff8){if(_0xa24ff8&&_0xa24ff8['name']&&_0xa24ff8[_0xbdf0('0x11')]){logger['error'](util[_0xbdf0('0x12')](_0xbdf0('0x13'),_0xa24ff8[_0xbdf0('0x14')],_0xa24ff8[_0xbdf0('0x11')]));}else{logger['error'](util[_0xbdf0('0x12')](_0xbdf0('0x15'),_0xa24ff8[_0xbdf0('0x16')]));}}function closeHandler(){logger[_0xbdf0('0x10')](util[_0xbdf0('0x12')](_0xbdf0('0x17'),this[_0xbdf0('0x18')],this[_0xbdf0('0x19')],this[_0xbdf0('0x1a')],this[_0xbdf0('0x1b')],this[_0xbdf0('0x1c')]));try{createSquareReport(this);}catch(_0x53f9d5){logger[_0xbdf0('0xe')](_0x53f9d5[_0xbdf0('0x16')]);}}function timeoutHandler(){logger[_0xbdf0('0x10')](util[_0xbdf0('0x12')](_0xbdf0('0x1d'),this['arg_1'],this[_0xbdf0('0x19')],this[_0xbdf0('0x1a')],this['dnid'],this[_0xbdf0('0x1c')]));}function hangupHandler(){logger[_0xbdf0('0x10')](util[_0xbdf0('0x12')](_0xbdf0('0x1e'),this['arg_1'],this['calleridname'],this['callerid'],this[_0xbdf0('0x1b')],this[_0xbdf0('0x1c')]));this[_0xbdf0('0x1f')]=!![];}function getRoot(_0x52c606){return parser[_0xbdf0('0x20')](_0x52c606,{'object':!![]})['mxGraphModel']['root'];}function addVertex(_0x189669,_0x37da7e,_0x4bb1bf){_0x37da7e[_0xbdf0('0x21')]=_0x4bb1bf;if(_0x37da7e[_0xbdf0('0x22')]('retry')){_0x37da7e[_0xbdf0('0x23')]=parseInt(_0x37da7e[_0xbdf0('0x24')],0xa);}_0x189669['addVertex'](_[_0xbdf0('0x25')](_0x37da7e,_0xbdf0('0x26')));}function initGraph(_0x2f2827,_0x2486b0){var _0xedbc10=new Graph();for(var _0x5c9519 in _0x2f2827){if(_0x2f2827['hasOwnProperty'](_0x5c9519)){if(_[_0xbdf0('0x27')](_0x2f2827[_0x5c9519])){for(var _0x51e3ad=0x0;_0x51e3ad<_0x2f2827[_0x5c9519]['length'];_0x51e3ad+=0x1){addVertex(_0xedbc10,_0x2f2827[_0x5c9519][_0x51e3ad],_0x5c9519);}}else{addVertex(_0xedbc10,_0x2f2827[_0x5c9519],_0x5c9519);}}}for(var _0x19e131=0x0;_0x19e131<_0x2486b0[_0xbdf0('0x28')];_0x19e131+=0x1){if(_0x2486b0[_0x19e131][_0xbdf0('0x29')]&&_0x2486b0[_0x19e131]['target']){_0xedbc10[_0xbdf0('0x2a')](_0x2486b0[_0x19e131][_0xbdf0('0x29')],_0x2486b0[_0x19e131]['target'],_0x2486b0[_0x19e131]['value']);}}return _0xedbc10;}function getSquareProject(_0x492dd0,_0x2cbb47){rpc[_0xbdf0('0x2b')](_0x492dd0)[_0xbdf0('0x2c')](function(_0x22c3e6){_0x2cbb47(null,_0x22c3e6);})[_0xbdf0('0x2d')](function(_0x1009f4){_0x2cbb47(null,_0x1009f4);});}function createSquareReport(_0x19509e,_0x2fefcd){rpc[_0xbdf0('0x2e')](_0x19509e)['then'](function(_0x289639){if(_0x2fefcd){_0x2fefcd(null,_0x289639);}})[_0xbdf0('0x2d')](function(_0x1b801f){if(_0x2fefcd){_0x2fefcd(null,_0x1b801f);}});}function setChannelVariables(_0x419f7f){for(var _0x38952c in _0x419f7f){if(_0x419f7f[_0xbdf0('0x22')](_0x38952c)){if(_0x38952c['indexOf'](_0xbdf0('0x2f'))===0x0){_0x419f7f[_0x38952c[_0xbdf0('0x30')](0x4)]=_0x419f7f[_0x38952c];delete _0x419f7f[_0x38952c];}}}}function getLogFromObject(_0x55415e){return util[_0xbdf0('0x31')](_0x55415e,{'showHidden':![],'depth':null});}function agiHandler(_0x4bd320){var _0x3c3abc,_0x11c3fc,_0x413fa5,_0x39b325,_0x1a70cb;logger[_0xbdf0('0x32')](_0xbdf0('0x33'),JSON['stringify'](_0x4bd320,null,0x2));logger['info'](util['format'](_0xbdf0('0x34'),_0x4bd320[_0xbdf0('0x18')],_0x4bd320[_0xbdf0('0x19')],_0x4bd320[_0xbdf0('0x1a')],_0x4bd320[_0xbdf0('0x1b')],_0x4bd320[_0xbdf0('0x1c')]));_0x4bd320['on'](_0xbdf0('0xe'),errorHandler);_0x4bd320['on'](_0xbdf0('0x35'),closeHandler);_0x4bd320['on'](_0xbdf0('0x36'),timeoutHandler);_0x4bd320[_0xbdf0('0x1f')]=![];_0x4bd320['on'](_0xbdf0('0x37'),hangupHandler);var _0x247b90=getSquareProject[_0xbdf0('0x38')](null,_0x4bd320['arg_1']);if(_0x247b90){if(_0x247b90[_0xbdf0('0x39')]){_0x3c3abc=getRoot(_0x247b90[_0xbdf0('0x39')]);if(_0x3c3abc&&_0x3c3abc[_0xbdf0('0x3a')]){logger['debug'](_0xbdf0('0x3b'),JSON[_0xbdf0('0x3c')](_0x3c3abc,null,0x2));_0x4bd320[_0xbdf0('0x3d')]=moment()['format']('YYYY-MM-DD\x20HH:mm:ss');_0x11c3fc=initGraph(_['omit'](_0x3c3abc,_0xbdf0('0x26')),_0x3c3abc[_0xbdf0('0x26')]);_0x413fa5=new Vertices(_0x4bd320,rpc,mailRpc);logger[_0xbdf0('0x10')](util[_0xbdf0('0x12')](_0xbdf0('0x3e'),_0x4bd320[_0xbdf0('0x18')],_0x4bd320[_0xbdf0('0x19')],_0x4bd320[_0xbdf0('0x1a')],_0x4bd320[_0xbdf0('0x1b')],_0x4bd320[_0xbdf0('0x1c')]));_0x11c3fc[_0xbdf0('0x3f')](_0x3c3abc[_0xbdf0('0x3a')]['id'],function(_0x514aea){try{_0x514aea=_0x413fa5[_0xbdf0('0x40')](_0x514aea);logger[_0xbdf0('0x32')](_0xbdf0('0x41'),JSON[_0xbdf0('0x3c')](_0x514aea,null,0x2));if(_0x514aea['agicommand']==='gotoc'){logger[_0xbdf0('0x10')](_0xbdf0('0x42'));_0x4bd320[_0xbdf0('0x43')]=_0x514aea[_0xbdf0('0x43')]||_0x4bd320[_0xbdf0('0x43')];_0x4bd320[_0xbdf0('0x44')]=_0x514aea['extension'];_0x4bd320['priority']=_0x514aea[_0xbdf0('0x45')]?_0x514aea['priority']-0x1:0x1;return null;}else if(_0x514aea&&_0x413fa5[_0x514aea[_0xbdf0('0x21')]]){_0x1a70cb=_0x413fa5[_0x514aea[_0xbdf0('0x21')]](_0x514aea);logger[_0xbdf0('0x10')]('response',JSON['stringify'](_0x1a70cb,null,0x2));if(_0x1a70cb&&_0x1a70cb[_0xbdf0('0x46')]===AGI_CODE_SUCCESS&&_0x1a70cb[_0xbdf0('0x47')]!==AGI_RESULT_ERROR&&!_0x4bd320['evtHangup']){logger['debug'](_0xbdf0('0x48'),getLogFromObject(_0x1a70cb));return _[_0xbdf0('0x49')](_0x1a70cb[_0xbdf0('0x47')])?null:_0x1a70cb['result']['toString']();}else{logger[_0xbdf0('0xe')](_0xbdf0('0x48'),getLogFromObject(_0x1a70cb));return null;}}else{logger[_0xbdf0('0xe')](_0xbdf0('0x4a'));return null;}}catch(_0x2aa45e){logger['error'](_0x2aa45e[_0xbdf0('0x16')]);return null;}});if(_0x3c3abc[_0xbdf0('0x4b')]){logger[_0xbdf0('0x10')](util['format'](_0xbdf0('0x4c'),_0x4bd320[_0xbdf0('0x18')],_0x4bd320[_0xbdf0('0x19')],_0x4bd320['callerid'],_0x4bd320[_0xbdf0('0x1b')],_0x4bd320[_0xbdf0('0x1c')]));_0x11c3fc[_0xbdf0('0x3f')](_0x3c3abc['finally']['id'],function(_0x401348){try{_0x401348=_0x413fa5['clear'](_0x401348);logger[_0xbdf0('0x32')](_0xbdf0('0x41'),JSON['stringify'](_0x401348,null,0x2));if(_0x401348&&_0x413fa5[_0x401348[_0xbdf0('0x21')]]){_0x1a70cb=_0x413fa5[_0x401348['agicommand']](_0x401348);if(_0x1a70cb){logger['debug'](_0xbdf0('0x48'),getLogFromObject(_0x1a70cb));return _[_0xbdf0('0x49')](_0x1a70cb['result'])?null:_0x1a70cb[_0xbdf0('0x47')]['toString']();}else{logger[_0xbdf0('0xe')](_0xbdf0('0x48'),getLogFromObject(_0x1a70cb));return null;}}else{logger['error'](_0xbdf0('0x4a'));return null;}}catch(_0x21b9fb){logger[_0xbdf0('0xe')](_0x21b9fb[_0xbdf0('0x16')]);return null;}});}logger['info'](util[_0xbdf0('0x12')]('[END]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',_0x4bd320[_0xbdf0('0x18')],_0x4bd320[_0xbdf0('0x19')],_0x4bd320[_0xbdf0('0x1a')],_0x4bd320[_0xbdf0('0x1b')],_0x4bd320['uniqueid']));logger[_0xbdf0('0x10')](util[_0xbdf0('0x12')](_0xbdf0('0x4d'),_0x4bd320['context'],_0x4bd320['extension'],parseInt(_0x4bd320['priority'],0xa)+0x1));_0x4bd320[_0xbdf0('0x4e')](_0x4bd320[_0xbdf0('0x43')],_0x4bd320[_0xbdf0('0x44')],parseInt(_0x4bd320[_0xbdf0('0x45')],0xa)+0x1);_0x4bd320[_0xbdf0('0x35')]();}}else{logger[_0xbdf0('0xe')](util['format'](_0xbdf0('0x4f'),_0x4bd320[_0xbdf0('0x18')]));_0x4bd320[_0xbdf0('0x4e')](_0x4bd320[_0xbdf0('0x43')],_0x4bd320[_0xbdf0('0x44')],parseInt(_0x4bd320['priority'],0xa)+0x1);_0x4bd320[_0xbdf0('0x35')]();}}else{logger[_0xbdf0('0xe')](util[_0xbdf0('0x12')](_0xbdf0('0x50'),_0x4bd320[_0xbdf0('0x18')]));_0x4bd320[_0xbdf0('0x4e')](_0x4bd320['context'],_0x4bd320[_0xbdf0('0x44')],parseInt(_0x4bd320[_0xbdf0('0x45')],0xa)+0x1);_0x4bd320[_0xbdf0('0x35')]();}}function main(){var _0x1e1eb8;return utilLicense['getLicense']()[_0xbdf0('0x2c')](function(_0x32c89e){if(_0x32c89e){if(_0x32c89e[_0xbdf0('0x51')]){if(_0x32c89e['callysquare']&&_0x32c89e[_0xbdf0('0x52')]>0x0){logger[_0xbdf0('0x10')](util[_0xbdf0('0x12')](_0xbdf0('0x53'),_0x32c89e[_0xbdf0('0x52')]));_0x1e1eb8=_0x32c89e[_0xbdf0('0x52')];}else{logger[_0xbdf0('0x10')]('[LICENSE]\x20CHANNELS:\x20UNLIMITED');}}else{logger[_0xbdf0('0x10')](_0xbdf0('0x54'));_0x1e1eb8=-0x1;}}})[_0xbdf0('0x2d')](function(_0x55c9e0){logger[_0xbdf0('0xe')](_0xbdf0('0x55'),_0x55c9e0[_0xbdf0('0x16')]);})[_0xbdf0('0x4b')](function(){var _0x29bdc0=require(_0xbdf0('0x56'))(config[_0xbdf0('0xb')][_0xbdf0('0xf')],config[_0xbdf0('0xb')]['ip'],_0x1e1eb8);_0x29bdc0['on']('error',errorHandler);_0x29bdc0['on']('listening',listeningHandler);_0x29bdc0['on']('connection',connectionHandler);_0x29bdc0['on'](_0xbdf0('0x35'),closeHandler);});}main();