78e8e0850ffde68a9dc7f25636da60c560aca6dd
[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 _0xf397=['stack','log','port','format','name','error','[%s]\x20%s','message','[CLOSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','arg_1','calleridname','callerid','dnid','uniqueid','info','[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','toJson','mxGraphModel','root','hasOwnProperty','maxRetry','retry','addVertex','mxCell','isArray','length','target','addEdge','value','getSquareProject','then','catch','createSquareReport','indexOf','agi_','substring','inspect','debug','variables','timeout','evtHangup','hangup','production','stringify','joinAt','YYYY-MM-DD\x20HH:mm:ss','omit','[START\x20TRAVERSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','traverseBFS','start','agicommand','gotoc','gotoc\x20block\x20stop\x20traverse\x20BFS','context','extension','priority','code','response','isUndefined','result','agicommand\x20not\x20found','[FINALLY\x20TRAVERSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','clear','toString','[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','close','Project\x20%s\x20not\x20published','continueAt','getLicense','callysquare','[LICENSE]\x20CHANNELS:\x20%s','[LICENSE]\x20CHANNELS:\x20UNLIMITED','[LICENSE]','finally','./server','listening','connection','xml2json','lodash','syncho','util','agi','../../config/license/util','./rpc','./graph','./vertices','defaults','nextTick'];(function(_0x4799a9,_0x32505){var _0x39cfb4=function(_0x43d4c5){while(--_0x43d4c5){_0x4799a9['push'](_0x4799a9['shift']());}};_0x39cfb4(++_0x32505);}(_0xf397,0x1b7));var _0x7f39=function(_0x379567,_0x1d56fd){_0x379567=_0x379567-0x0;var _0x125fd6=_0xf397[_0x379567];return _0x125fd6;};'use strict';var parser=require(_0x7f39('0x0'));var _=require(_0x7f39('0x1'));var syncho=require(_0x7f39('0x2'));var util=require(_0x7f39('0x3'));var moment=require('moment');var config=require('../../config/environment');var logger=require('../../config/logger')(_0x7f39('0x4'));var utilLicense=require(_0x7f39('0x5'));var rpc=require(_0x7f39('0x6'));var mailRpc=require('./mailRpc');var Graph=require(_0x7f39('0x7'));var Vertices=require(_0x7f39('0x8'));var AGI_CODE_SUCCESS=0xc8;var AGI_RESULT_ERROR=-0x1;config[_0x7f39('0x4')]=_[_0x7f39('0x9')](config[_0x7f39('0x4')],{'ip':'127.0.0.1','port':0x11dd});function connectionHandler(_0x28e861){process[_0x7f39('0xa')](function(){syncho(function(){try{setChannelVariables(_0x28e861);agiHandler(_0x28e861);}catch(_0x3713a6){logger['error'](_0x3713a6[_0x7f39('0xb')]);}});});}function listeningHandler(){console[_0x7f39('0xc')]('Cally\x20Square\x20server\x20listening\x20on\x20port\x20%d!',config['agi'][_0x7f39('0xd')]);logger['info'](util[_0x7f39('0xe')]('[LISTENING]\x20Cally\x20Square\x20listening\x20%s:%d',config[_0x7f39('0x4')]['ip'],config[_0x7f39('0x4')][_0x7f39('0xd')]));}function errorHandler(_0x4b95ca){if(_0x4b95ca&&_0x4b95ca[_0x7f39('0xf')]&&_0x4b95ca['message']){logger[_0x7f39('0x10')](util[_0x7f39('0xe')](_0x7f39('0x11'),_0x4b95ca[_0x7f39('0xf')],_0x4b95ca[_0x7f39('0x12')]));}else{logger['error'](util[_0x7f39('0xe')]('[ERROR]\x20%s',_0x4b95ca[_0x7f39('0xb')]));}}function closeHandler(){logger['info'](util[_0x7f39('0xe')](_0x7f39('0x13'),this[_0x7f39('0x14')],this[_0x7f39('0x15')],this[_0x7f39('0x16')],this[_0x7f39('0x17')],this[_0x7f39('0x18')]));try{createSquareReport(this);}catch(_0x4e47bc){logger[_0x7f39('0x10')](_0x4e47bc[_0x7f39('0xb')]);}}function timeoutHandler(){logger[_0x7f39('0x19')](util['format'](_0x7f39('0x1a'),this[_0x7f39('0x14')],this[_0x7f39('0x15')],this[_0x7f39('0x16')],this[_0x7f39('0x17')],this[_0x7f39('0x18')]));}function hangupHandler(){logger[_0x7f39('0x19')](util[_0x7f39('0xe')](_0x7f39('0x1b'),this[_0x7f39('0x14')],this[_0x7f39('0x15')],this['callerid'],this['dnid'],this['uniqueid']));this['evtHangup']=!![];}function getRoot(_0x65771d){return parser[_0x7f39('0x1c')](_0x65771d,{'object':!![]})[_0x7f39('0x1d')][_0x7f39('0x1e')];}function addVertex(_0x458903,_0x4811ff,_0x1cd4f2){_0x4811ff['agicommand']=_0x1cd4f2;if(_0x4811ff[_0x7f39('0x1f')]('retry')){_0x4811ff[_0x7f39('0x20')]=parseInt(_0x4811ff[_0x7f39('0x21')],0xa);}_0x458903[_0x7f39('0x22')](_['omit'](_0x4811ff,_0x7f39('0x23')));}function initGraph(_0x1d11b6,_0x58e4a6){var _0x35e347=new Graph();for(var _0x1b0243 in _0x1d11b6){if(_0x1d11b6[_0x7f39('0x1f')](_0x1b0243)){if(_[_0x7f39('0x24')](_0x1d11b6[_0x1b0243])){for(var _0x3dc2e1=0x0;_0x3dc2e1<_0x1d11b6[_0x1b0243]['length'];_0x3dc2e1+=0x1){addVertex(_0x35e347,_0x1d11b6[_0x1b0243][_0x3dc2e1],_0x1b0243);}}else{addVertex(_0x35e347,_0x1d11b6[_0x1b0243],_0x1b0243);}}}for(var _0x4d1445=0x0;_0x4d1445<_0x58e4a6[_0x7f39('0x25')];_0x4d1445+=0x1){if(_0x58e4a6[_0x4d1445]['source']&&_0x58e4a6[_0x4d1445][_0x7f39('0x26')]){_0x35e347[_0x7f39('0x27')](_0x58e4a6[_0x4d1445]['source'],_0x58e4a6[_0x4d1445][_0x7f39('0x26')],_0x58e4a6[_0x4d1445][_0x7f39('0x28')]);}}return _0x35e347;}function getSquareProject(_0x113bd2,_0x22881a){rpc[_0x7f39('0x29')](_0x113bd2)[_0x7f39('0x2a')](function(_0x1ab39e){_0x22881a(null,_0x1ab39e);})[_0x7f39('0x2b')](function(_0x74a482){_0x22881a(null,_0x74a482);});}function createSquareReport(_0x59239e,_0x3330d8){rpc[_0x7f39('0x2c')](_0x59239e)[_0x7f39('0x2a')](function(_0x3d3d88){if(_0x3330d8){_0x3330d8(null,_0x3d3d88);}})[_0x7f39('0x2b')](function(_0x53ed5b){if(_0x3330d8){_0x3330d8(null,_0x53ed5b);}});}function setChannelVariables(_0x3694aa){for(var _0x5031a7 in _0x3694aa){if(_0x3694aa[_0x7f39('0x1f')](_0x5031a7)){if(_0x5031a7[_0x7f39('0x2d')](_0x7f39('0x2e'))===0x0){_0x3694aa[_0x5031a7[_0x7f39('0x2f')](0x4)]=_0x3694aa[_0x5031a7];delete _0x3694aa[_0x5031a7];}}}}function getLogFromObject(_0x426e91){return util[_0x7f39('0x30')](_0x426e91,{'showHidden':![],'depth':null});}function agiHandler(_0x1bd3c6){var _0x29249d,_0x37b709,_0x5ee1ab,_0x3edca8,_0x35018d;logger[_0x7f39('0x31')](_0x7f39('0x32'),JSON['stringify'](_0x1bd3c6,null,0x2));logger[_0x7f39('0x19')](util[_0x7f39('0xe')]('[START]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',_0x1bd3c6['arg_1'],_0x1bd3c6['calleridname'],_0x1bd3c6[_0x7f39('0x16')],_0x1bd3c6['dnid'],_0x1bd3c6[_0x7f39('0x18')]));_0x1bd3c6['on'](_0x7f39('0x10'),errorHandler);_0x1bd3c6['on']('close',closeHandler);_0x1bd3c6['on'](_0x7f39('0x33'),timeoutHandler);_0x1bd3c6[_0x7f39('0x34')]=![];_0x1bd3c6['on'](_0x7f39('0x35'),hangupHandler);var _0x17029e=getSquareProject['sync'](null,_0x1bd3c6[_0x7f39('0x14')]);if(_0x17029e){if(_0x17029e[_0x7f39('0x36')]){_0x29249d=getRoot(_0x17029e['production']);if(_0x29249d&&_0x29249d['start']){logger[_0x7f39('0x31')]('root',JSON[_0x7f39('0x37')](_0x29249d,null,0x2));_0x1bd3c6[_0x7f39('0x38')]=moment()[_0x7f39('0xe')](_0x7f39('0x39'));_0x37b709=initGraph(_[_0x7f39('0x3a')](_0x29249d,_0x7f39('0x23')),_0x29249d[_0x7f39('0x23')]);_0x5ee1ab=new Vertices(_0x1bd3c6,rpc,mailRpc);logger['info'](util[_0x7f39('0xe')](_0x7f39('0x3b'),_0x1bd3c6[_0x7f39('0x14')],_0x1bd3c6[_0x7f39('0x15')],_0x1bd3c6['callerid'],_0x1bd3c6[_0x7f39('0x17')],_0x1bd3c6[_0x7f39('0x18')]));_0x37b709[_0x7f39('0x3c')](_0x29249d[_0x7f39('0x3d')]['id'],function(_0x5f568d){try{_0x5f568d=_0x5ee1ab['clear'](_0x5f568d);logger[_0x7f39('0x31')]('vertex',JSON[_0x7f39('0x37')](_0x5f568d,null,0x2));if(_0x5f568d[_0x7f39('0x3e')]===_0x7f39('0x3f')){logger[_0x7f39('0x19')](_0x7f39('0x40'));_0x1bd3c6['context']=_0x5f568d[_0x7f39('0x41')]||_0x1bd3c6['context'];_0x1bd3c6['extension']=_0x5f568d[_0x7f39('0x42')];_0x1bd3c6[_0x7f39('0x43')]=_0x5f568d[_0x7f39('0x43')]?_0x5f568d[_0x7f39('0x43')]-0x1:0x1;return null;}else if(_0x5f568d&&_0x5ee1ab[_0x5f568d[_0x7f39('0x3e')]]){_0x35018d=_0x5ee1ab[_0x5f568d[_0x7f39('0x3e')]](_0x5f568d);logger[_0x7f39('0x19')]('response',JSON[_0x7f39('0x37')](_0x35018d,null,0x2));if(_0x35018d&&_0x35018d[_0x7f39('0x44')]===AGI_CODE_SUCCESS&&_0x35018d['result']!==AGI_RESULT_ERROR&&!_0x1bd3c6[_0x7f39('0x34')]){logger[_0x7f39('0x31')](_0x7f39('0x45'),getLogFromObject(_0x35018d));return _[_0x7f39('0x46')](_0x35018d[_0x7f39('0x47')])?null:_0x35018d['result']['toString']();}else{logger[_0x7f39('0x10')]('response',getLogFromObject(_0x35018d));return null;}}else{logger[_0x7f39('0x10')](_0x7f39('0x48'));return null;}}catch(_0x10eb88){logger[_0x7f39('0x10')](_0x10eb88[_0x7f39('0xb')]);return null;}});if(_0x29249d['finally']){logger[_0x7f39('0x19')](util[_0x7f39('0xe')](_0x7f39('0x49'),_0x1bd3c6[_0x7f39('0x14')],_0x1bd3c6[_0x7f39('0x15')],_0x1bd3c6[_0x7f39('0x16')],_0x1bd3c6[_0x7f39('0x17')],_0x1bd3c6['uniqueid']));_0x37b709[_0x7f39('0x3c')](_0x29249d['finally']['id'],function(_0x46a33b){try{_0x46a33b=_0x5ee1ab[_0x7f39('0x4a')](_0x46a33b);logger[_0x7f39('0x31')]('vertex',JSON[_0x7f39('0x37')](_0x46a33b,null,0x2));if(_0x46a33b&&_0x5ee1ab[_0x46a33b[_0x7f39('0x3e')]]){_0x35018d=_0x5ee1ab[_0x46a33b[_0x7f39('0x3e')]](_0x46a33b);if(_0x35018d){logger[_0x7f39('0x31')]('response',getLogFromObject(_0x35018d));return _['isUndefined'](_0x35018d[_0x7f39('0x47')])?null:_0x35018d['result'][_0x7f39('0x4b')]();}else{logger[_0x7f39('0x10')]('response',getLogFromObject(_0x35018d));return null;}}else{logger[_0x7f39('0x10')](_0x7f39('0x48'));return null;}}catch(_0x34bcbe){logger[_0x7f39('0x10')](_0x34bcbe['stack']);return null;}});}logger[_0x7f39('0x19')](util[_0x7f39('0xe')](_0x7f39('0x4c'),_0x1bd3c6[_0x7f39('0x14')],_0x1bd3c6[_0x7f39('0x15')],_0x1bd3c6['callerid'],_0x1bd3c6[_0x7f39('0x17')],_0x1bd3c6[_0x7f39('0x18')]));logger['info'](util[_0x7f39('0xe')](_0x7f39('0x4d'),_0x1bd3c6[_0x7f39('0x41')],_0x1bd3c6['extension'],parseInt(_0x1bd3c6['priority'],0xa)+0x1));_0x1bd3c6['continueAt'](_0x1bd3c6[_0x7f39('0x41')],_0x1bd3c6['extension'],parseInt(_0x1bd3c6['priority'],0xa)+0x1);_0x1bd3c6[_0x7f39('0x4e')]();}}else{logger[_0x7f39('0x10')](util[_0x7f39('0xe')](_0x7f39('0x4f'),_0x1bd3c6[_0x7f39('0x14')]));_0x1bd3c6[_0x7f39('0x50')](_0x1bd3c6[_0x7f39('0x41')],_0x1bd3c6[_0x7f39('0x42')],parseInt(_0x1bd3c6[_0x7f39('0x43')],0xa)+0x1);_0x1bd3c6[_0x7f39('0x4e')]();}}else{logger['error'](util[_0x7f39('0xe')]('Project\x20%s\x20not\x20found',_0x1bd3c6[_0x7f39('0x14')]));_0x1bd3c6[_0x7f39('0x50')](_0x1bd3c6[_0x7f39('0x41')],_0x1bd3c6['extension'],parseInt(_0x1bd3c6[_0x7f39('0x43')],0xa)+0x1);_0x1bd3c6[_0x7f39('0x4e')]();}}function main(){var _0x2d71ad;return utilLicense[_0x7f39('0x51')]()['then'](function(_0x56fbd1){if(_0x56fbd1){if(_0x56fbd1['voice']){if(_0x56fbd1[_0x7f39('0x52')]&&_0x56fbd1[_0x7f39('0x52')]>0x0){logger[_0x7f39('0x19')](util['format'](_0x7f39('0x53'),_0x56fbd1[_0x7f39('0x52')]));_0x2d71ad=_0x56fbd1[_0x7f39('0x52')];}else{logger['info'](_0x7f39('0x54'));}}else{logger[_0x7f39('0x19')]('[LICENSE]\x20VOICE\x20IS\x20DISABLED');_0x2d71ad=-0x1;}}})['catch'](function(_0x3520c0){logger[_0x7f39('0x10')](_0x7f39('0x55'),_0x3520c0[_0x7f39('0xb')]);})[_0x7f39('0x56')](function(){var _0xf1e5fd=require(_0x7f39('0x57'))(config['agi'][_0x7f39('0xd')],config[_0x7f39('0x4')]['ip'],_0x2d71ad);_0xf1e5fd['on'](_0x7f39('0x10'),errorHandler);_0xf1e5fd['on'](_0x7f39('0x58'),listeningHandler);_0xf1e5fd['on'](_0x7f39('0x59'),connectionHandler);_0xf1e5fd['on'](_0x7f39('0x4e'),closeHandler);});}main();