Built motion from commit (unavailable).|2.4.5
[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 _0x6f7f=['variables','stringify','[START]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','callerid','close','timeout','evtHangup','hangup','sync','production','start','joinAt','mxCell','[START\x20TRAVERSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','traverseBFS','clear','agicommand','gotoc','gotoc\x20block\x20stop\x20traverse\x20BFS','context','extension','priority','result','response','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','isUndefined','Project\x20%s\x20not\x20published','continueAt','getLicense','voice','callysquare','[LICENSE]\x20CHANNELS:\x20UNLIMITED','[LICENSE]\x20VOICE\x20IS\x20DISABLED','[LICENSE]','./server','listening','lodash','syncho','util','moment','../../config/environment','agi','./rpc','./mailRpc','./graph','127.0.0.1','nextTick','error','log','info','format','[LISTENING]\x20Cally\x20Square\x20listening\x20%s:%d','port','message','[%s]\x20%s','[ERROR]\x20%s','[CLOSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','arg_1','calleridname','dnid','uniqueid','stack','[HANGUP]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','mxGraphModel','root','hasOwnProperty','retry','maxRetry','addVertex','isArray','length','source','target','addEdge','then','createSquareReport','indexOf','agi_','substring','inspect','debug'];(function(_0x1e63cd,_0x58d3e5){var _0x16ed2d=function(_0x4f2f34){while(--_0x4f2f34){_0x1e63cd['push'](_0x1e63cd['shift']());}};_0x16ed2d(++_0x58d3e5);}(_0x6f7f,0x177));var _0xf6f7=function(_0x4b6303,_0x17e331){_0x4b6303=_0x4b6303-0x0;var _0x29210d=_0x6f7f[_0x4b6303];return _0x29210d;};'use strict';var parser=require('xml2json');var _=require(_0xf6f7('0x0'));var syncho=require(_0xf6f7('0x1'));var util=require(_0xf6f7('0x2'));var moment=require(_0xf6f7('0x3'));var config=require(_0xf6f7('0x4'));var logger=require('../../config/logger')(_0xf6f7('0x5'));var utilLicense=require('../../config/license/util');var rpc=require(_0xf6f7('0x6'));var mailRpc=require(_0xf6f7('0x7'));var Graph=require(_0xf6f7('0x8'));var Vertices=require('./vertices');var AGI_CODE_SUCCESS=0xc8;var AGI_RESULT_ERROR=-0x1;config[_0xf6f7('0x5')]=_['defaults'](config[_0xf6f7('0x5')],{'ip':_0xf6f7('0x9'),'port':0x11dd});function connectionHandler(_0xe86f3a){process[_0xf6f7('0xa')](function(){syncho(function(){try{setChannelVariables(_0xe86f3a);agiHandler(_0xe86f3a);}catch(_0x2bf166){logger[_0xf6f7('0xb')](_0x2bf166['stack']);}});});}function listeningHandler(){console[_0xf6f7('0xc')]('Cally\x20Square\x20server\x20listening\x20on\x20port\x20%d!',config['agi']['port']);logger[_0xf6f7('0xd')](util[_0xf6f7('0xe')](_0xf6f7('0xf'),config[_0xf6f7('0x5')]['ip'],config[_0xf6f7('0x5')][_0xf6f7('0x10')]));}function errorHandler(_0x4def48){if(_0x4def48&&_0x4def48['name']&&_0x4def48[_0xf6f7('0x11')]){logger[_0xf6f7('0xb')](util[_0xf6f7('0xe')](_0xf6f7('0x12'),_0x4def48['name'],_0x4def48['message']));}else{logger[_0xf6f7('0xb')](util['format'](_0xf6f7('0x13'),_0x4def48['stack']));}}function closeHandler(){logger[_0xf6f7('0xd')](util[_0xf6f7('0xe')](_0xf6f7('0x14'),this[_0xf6f7('0x15')],this[_0xf6f7('0x16')],this['callerid'],this[_0xf6f7('0x17')],this[_0xf6f7('0x18')]));try{createSquareReport(this);}catch(_0x566589){logger[_0xf6f7('0xb')](_0x566589[_0xf6f7('0x19')]);}}function timeoutHandler(){logger[_0xf6f7('0xd')](util[_0xf6f7('0xe')]('[TIMEOUT]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',this[_0xf6f7('0x15')],this[_0xf6f7('0x16')],this['callerid'],this[_0xf6f7('0x17')],this[_0xf6f7('0x18')]));}function hangupHandler(){logger[_0xf6f7('0xd')](util[_0xf6f7('0xe')](_0xf6f7('0x1a'),this[_0xf6f7('0x15')],this[_0xf6f7('0x16')],this['callerid'],this[_0xf6f7('0x17')],this['uniqueid']));this['evtHangup']=!![];}function getRoot(_0x66e5e4){return parser['toJson'](_0x66e5e4,{'object':!![]})[_0xf6f7('0x1b')][_0xf6f7('0x1c')];}function addVertex(_0x50b550,_0x3ce38d,_0x5ec44f){_0x3ce38d['agicommand']=_0x5ec44f;if(_0x3ce38d[_0xf6f7('0x1d')](_0xf6f7('0x1e'))){_0x3ce38d[_0xf6f7('0x1f')]=parseInt(_0x3ce38d[_0xf6f7('0x1e')],0xa);}_0x50b550[_0xf6f7('0x20')](_['omit'](_0x3ce38d,'mxCell'));}function initGraph(_0x3fecc1,_0x2620a4){var _0x277023=new Graph();for(var _0x1de269 in _0x3fecc1){if(_0x3fecc1[_0xf6f7('0x1d')](_0x1de269)){if(_[_0xf6f7('0x21')](_0x3fecc1[_0x1de269])){for(var _0x4b6e82=0x0;_0x4b6e82<_0x3fecc1[_0x1de269][_0xf6f7('0x22')];_0x4b6e82+=0x1){addVertex(_0x277023,_0x3fecc1[_0x1de269][_0x4b6e82],_0x1de269);}}else{addVertex(_0x277023,_0x3fecc1[_0x1de269],_0x1de269);}}}for(var _0x2c545c=0x0;_0x2c545c<_0x2620a4[_0xf6f7('0x22')];_0x2c545c+=0x1){if(_0x2620a4[_0x2c545c][_0xf6f7('0x23')]&&_0x2620a4[_0x2c545c][_0xf6f7('0x24')]){_0x277023[_0xf6f7('0x25')](_0x2620a4[_0x2c545c][_0xf6f7('0x23')],_0x2620a4[_0x2c545c]['target'],_0x2620a4[_0x2c545c]['value']);}}return _0x277023;}function getSquareProject(_0x4bd86c,_0x3bd11b){rpc['getSquareProject'](_0x4bd86c)[_0xf6f7('0x26')](function(_0x35b586){_0x3bd11b(null,_0x35b586);})['catch'](function(_0x50ef5b){_0x3bd11b(null,_0x50ef5b);});}function createSquareReport(_0x3b85db,_0x26610f){rpc[_0xf6f7('0x27')](_0x3b85db)['then'](function(_0x2fcb7c){if(_0x26610f){_0x26610f(null,_0x2fcb7c);}})['catch'](function(_0x34b223){if(_0x26610f){_0x26610f(null,_0x34b223);}});}function setChannelVariables(_0x2b0faf){for(var _0x958646 in _0x2b0faf){if(_0x2b0faf[_0xf6f7('0x1d')](_0x958646)){if(_0x958646[_0xf6f7('0x28')](_0xf6f7('0x29'))===0x0){_0x2b0faf[_0x958646[_0xf6f7('0x2a')](0x4)]=_0x2b0faf[_0x958646];delete _0x2b0faf[_0x958646];}}}}function getLogFromObject(_0x257af4){return util[_0xf6f7('0x2b')](_0x257af4,{'showHidden':![],'depth':null});}function agiHandler(_0x174730){var _0xd755e6,_0x4589c4,_0x5883e3,_0x3fbdb6,_0x55e336;logger[_0xf6f7('0x2c')](_0xf6f7('0x2d'),JSON[_0xf6f7('0x2e')](_0x174730,null,0x2));logger[_0xf6f7('0xd')](util[_0xf6f7('0xe')](_0xf6f7('0x2f'),_0x174730[_0xf6f7('0x15')],_0x174730[_0xf6f7('0x16')],_0x174730[_0xf6f7('0x30')],_0x174730['dnid'],_0x174730['uniqueid']));_0x174730['on'](_0xf6f7('0xb'),errorHandler);_0x174730['on'](_0xf6f7('0x31'),closeHandler);_0x174730['on'](_0xf6f7('0x32'),timeoutHandler);_0x174730[_0xf6f7('0x33')]=![];_0x174730['on'](_0xf6f7('0x34'),hangupHandler);var _0x41975f=getSquareProject[_0xf6f7('0x35')](null,_0x174730[_0xf6f7('0x15')]);if(_0x41975f){if(_0x41975f[_0xf6f7('0x36')]){_0xd755e6=getRoot(_0x41975f[_0xf6f7('0x36')]);if(_0xd755e6&&_0xd755e6[_0xf6f7('0x37')]){logger[_0xf6f7('0x2c')](_0xf6f7('0x1c'),JSON[_0xf6f7('0x2e')](_0xd755e6,null,0x2));_0x174730[_0xf6f7('0x38')]=moment()[_0xf6f7('0xe')]('YYYY-MM-DD\x20HH:mm:ss');_0x4589c4=initGraph(_['omit'](_0xd755e6,_0xf6f7('0x39')),_0xd755e6[_0xf6f7('0x39')]);_0x5883e3=new Vertices(_0x174730,rpc,mailRpc);logger[_0xf6f7('0xd')](util[_0xf6f7('0xe')](_0xf6f7('0x3a'),_0x174730[_0xf6f7('0x15')],_0x174730['calleridname'],_0x174730[_0xf6f7('0x30')],_0x174730[_0xf6f7('0x17')],_0x174730[_0xf6f7('0x18')]));_0x4589c4[_0xf6f7('0x3b')](_0xd755e6['start']['id'],function(_0x489c39){try{_0x489c39=_0x5883e3[_0xf6f7('0x3c')](_0x489c39);logger['debug']('vertex',JSON[_0xf6f7('0x2e')](_0x489c39,null,0x2));if(_0x489c39[_0xf6f7('0x3d')]===_0xf6f7('0x3e')){logger['info'](_0xf6f7('0x3f'));_0x174730[_0xf6f7('0x40')]=_0x489c39[_0xf6f7('0x40')]||_0x174730[_0xf6f7('0x40')];_0x174730['extension']=_0x489c39[_0xf6f7('0x41')];_0x174730[_0xf6f7('0x42')]=_0x489c39['priority']?_0x489c39['priority']-0x1:0x1;return null;}else if(_0x489c39&&_0x5883e3[_0x489c39[_0xf6f7('0x3d')]]){_0x55e336=_0x5883e3[_0x489c39['agicommand']](_0x489c39);logger[_0xf6f7('0xd')]('response',JSON['stringify'](_0x55e336,null,0x2));if(_0x55e336&&_0x55e336['code']===AGI_CODE_SUCCESS&&_0x55e336[_0xf6f7('0x43')]!==AGI_RESULT_ERROR&&!_0x174730[_0xf6f7('0x33')]){logger[_0xf6f7('0x2c')](_0xf6f7('0x44'),getLogFromObject(_0x55e336));return _['isUndefined'](_0x55e336[_0xf6f7('0x43')])?null:_0x55e336['result'][_0xf6f7('0x45')]();}else{logger['error']('response',getLogFromObject(_0x55e336));return null;}}else{logger[_0xf6f7('0xb')](_0xf6f7('0x46'));return null;}}catch(_0xff8a0f){logger['error'](_0xff8a0f[_0xf6f7('0x19')]);return null;}});if(_0xd755e6[_0xf6f7('0x47')]){logger['info'](util['format'](_0xf6f7('0x48'),_0x174730['arg_1'],_0x174730[_0xf6f7('0x16')],_0x174730['callerid'],_0x174730['dnid'],_0x174730[_0xf6f7('0x18')]));_0x4589c4['traverseBFS'](_0xd755e6[_0xf6f7('0x47')]['id'],function(_0x59b0fd){try{_0x59b0fd=_0x5883e3[_0xf6f7('0x3c')](_0x59b0fd);logger['debug']('vertex',JSON[_0xf6f7('0x2e')](_0x59b0fd,null,0x2));if(_0x59b0fd&&_0x5883e3[_0x59b0fd[_0xf6f7('0x3d')]]){_0x55e336=_0x5883e3[_0x59b0fd[_0xf6f7('0x3d')]](_0x59b0fd);if(_0x55e336){logger[_0xf6f7('0x2c')](_0xf6f7('0x44'),getLogFromObject(_0x55e336));return _[_0xf6f7('0x49')](_0x55e336[_0xf6f7('0x43')])?null:_0x55e336[_0xf6f7('0x43')][_0xf6f7('0x45')]();}else{logger[_0xf6f7('0xb')](_0xf6f7('0x44'),getLogFromObject(_0x55e336));return null;}}else{logger[_0xf6f7('0xb')]('agicommand\x20not\x20found');return null;}}catch(_0x4003b7){logger['error'](_0x4003b7[_0xf6f7('0x19')]);return null;}});}logger[_0xf6f7('0xd')](util[_0xf6f7('0xe')]('[END]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',_0x174730[_0xf6f7('0x15')],_0x174730[_0xf6f7('0x16')],_0x174730[_0xf6f7('0x30')],_0x174730[_0xf6f7('0x17')],_0x174730[_0xf6f7('0x18')]));logger['info'](util[_0xf6f7('0xe')]('[GOTO]\x20context:\x20%s\x20extension:\x20%s\x20priority:\x20%s',_0x174730[_0xf6f7('0x40')],_0x174730[_0xf6f7('0x41')],parseInt(_0x174730[_0xf6f7('0x42')],0xa)+0x1));_0x174730['continueAt'](_0x174730['context'],_0x174730[_0xf6f7('0x41')],parseInt(_0x174730[_0xf6f7('0x42')],0xa)+0x1);_0x174730[_0xf6f7('0x31')]();}}else{logger['error'](util[_0xf6f7('0xe')](_0xf6f7('0x4a'),_0x174730[_0xf6f7('0x15')]));_0x174730[_0xf6f7('0x4b')](_0x174730[_0xf6f7('0x40')],_0x174730[_0xf6f7('0x41')],parseInt(_0x174730['priority'],0xa)+0x1);_0x174730[_0xf6f7('0x31')]();}}else{logger[_0xf6f7('0xb')](util['format']('Project\x20%s\x20not\x20found',_0x174730[_0xf6f7('0x15')]));_0x174730[_0xf6f7('0x4b')](_0x174730['context'],_0x174730[_0xf6f7('0x41')],parseInt(_0x174730['priority'],0xa)+0x1);_0x174730[_0xf6f7('0x31')]();}}function main(){var _0x41f083;return utilLicense[_0xf6f7('0x4c')]()[_0xf6f7('0x26')](function(_0x12ed5a){if(_0x12ed5a){if(_0x12ed5a[_0xf6f7('0x4d')]){if(_0x12ed5a[_0xf6f7('0x4e')]&&_0x12ed5a[_0xf6f7('0x4e')]>0x0){logger['info'](util['format']('[LICENSE]\x20CHANNELS:\x20%s',_0x12ed5a[_0xf6f7('0x4e')]));_0x41f083=_0x12ed5a[_0xf6f7('0x4e')];}else{logger[_0xf6f7('0xd')](_0xf6f7('0x4f'));}}else{logger['info'](_0xf6f7('0x50'));_0x41f083=-0x1;}}})['catch'](function(_0x117ec1){logger[_0xf6f7('0xb')](_0xf6f7('0x51'),_0x117ec1[_0xf6f7('0x19')]);})['finally'](function(){var _0x1e43c4=require(_0xf6f7('0x52'))(config[_0xf6f7('0x5')][_0xf6f7('0x10')],config[_0xf6f7('0x5')]['ip'],_0x41f083);_0x1e43c4['on'](_0xf6f7('0xb'),errorHandler);_0x1e43c4['on'](_0xf6f7('0x53'),listeningHandler);_0x1e43c4['on']('connection',connectionHandler);_0x1e43c4['on']('close',closeHandler);});}main();