Built motion from commit (unavailable).|2.5.26
[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 _0x7118=['Cally\x20Square\x20server\x20listening\x20on\x20port\x20%d!','info','format','[LISTENING]\x20Cally\x20Square\x20listening\x20%s:%d','port','[%s]\x20%s','name','message','[CLOSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','arg_1','calleridname','dnid','uniqueid','callerid','[HANGUP]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','evtHangup','mxGraphModel','root','agicommand','hasOwnProperty','retry','addVertex','omit','mxCell','isArray','length','source','target','addEdge','getSquareProject','catch','indexOf','agi_','substring','debug','variables','stringify','[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','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','gotoc\x20block\x20stop\x20traverse\x20BFS','context','extension','priority','response','code','isUndefined','result','toString','[FINALLY\x20TRAVERSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','finally','agicommand\x20not\x20found','[END]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','continueAt','Project\x20%s\x20not\x20published','Project\x20%s\x20not\x20found','getLicense','then','voice','callysquare','[LICENSE]\x20CHANNELS:\x20%s','[LICENSE]\x20VOICE\x20IS\x20DISABLED','[LICENSE]','./server','connection','syncho','util','../../config/environment','../../config/logger','agi','../../config/license/util','./rpc','./mailRpc','./graph','defaults','127.0.0.1','nextTick','error','stack','log'];(function(_0x250858,_0x3278ba){var _0x17771e=function(_0x3cd613){while(--_0x3cd613){_0x250858['push'](_0x250858['shift']());}};_0x17771e(++_0x3278ba);}(_0x7118,0x159));var _0x8711=function(_0xa19d0a,_0x16f2d7){_0xa19d0a=_0xa19d0a-0x0;var _0xe110d8=_0x7118[_0xa19d0a];return _0xe110d8;};'use strict';var parser=require('xml2json');var _=require('lodash');var syncho=require(_0x8711('0x0'));var util=require(_0x8711('0x1'));var moment=require('moment');var config=require(_0x8711('0x2'));var logger=require(_0x8711('0x3'))(_0x8711('0x4'));var utilLicense=require(_0x8711('0x5'));var rpc=require(_0x8711('0x6'));var mailRpc=require(_0x8711('0x7'));var Graph=require(_0x8711('0x8'));var Vertices=require('./vertices');var AGI_CODE_SUCCESS=0xc8;var AGI_RESULT_ERROR=-0x1;config[_0x8711('0x4')]=_[_0x8711('0x9')](config[_0x8711('0x4')],{'ip':_0x8711('0xa'),'port':0x11dd});function connectionHandler(_0x189349){process[_0x8711('0xb')](function(){syncho(function(){try{setChannelVariables(_0x189349);agiHandler(_0x189349);}catch(_0x7f7ca7){logger[_0x8711('0xc')](_0x7f7ca7[_0x8711('0xd')]);}});});}function listeningHandler(){console[_0x8711('0xe')](_0x8711('0xf'),config['agi']['port']);logger[_0x8711('0x10')](util[_0x8711('0x11')](_0x8711('0x12'),config['agi']['ip'],config['agi'][_0x8711('0x13')]));}function errorHandler(_0x89db9a){if(_0x89db9a&&_0x89db9a['name']&&_0x89db9a['message']){logger[_0x8711('0xc')](util[_0x8711('0x11')](_0x8711('0x14'),_0x89db9a[_0x8711('0x15')],_0x89db9a[_0x8711('0x16')]));}else{logger[_0x8711('0xc')](util[_0x8711('0x11')]('[ERROR]\x20%s',_0x89db9a[_0x8711('0xd')]));}}function closeHandler(){logger[_0x8711('0x10')](util[_0x8711('0x11')](_0x8711('0x17'),this[_0x8711('0x18')],this[_0x8711('0x19')],this['callerid'],this[_0x8711('0x1a')],this[_0x8711('0x1b')]));try{createSquareReport(this);}catch(_0x1c2cb5){logger[_0x8711('0xc')](_0x1c2cb5[_0x8711('0xd')]);}}function timeoutHandler(){logger['info'](util['format']('[TIMEOUT]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',this[_0x8711('0x18')],this[_0x8711('0x19')],this[_0x8711('0x1c')],this['dnid'],this['uniqueid']));}function hangupHandler(){logger[_0x8711('0x10')](util[_0x8711('0x11')](_0x8711('0x1d'),this[_0x8711('0x18')],this[_0x8711('0x19')],this[_0x8711('0x1c')],this[_0x8711('0x1a')],this[_0x8711('0x1b')]));this[_0x8711('0x1e')]=!![];}function getRoot(_0x41369d){return parser['toJson'](_0x41369d,{'object':!![]})[_0x8711('0x1f')][_0x8711('0x20')];}function addVertex(_0x3302b9,_0x939ef3,_0x4cf830){_0x939ef3[_0x8711('0x21')]=_0x4cf830;if(_0x939ef3[_0x8711('0x22')](_0x8711('0x23'))){_0x939ef3['maxRetry']=parseInt(_0x939ef3[_0x8711('0x23')],0xa);}_0x3302b9[_0x8711('0x24')](_[_0x8711('0x25')](_0x939ef3,_0x8711('0x26')));}function initGraph(_0x3f937b,_0x514956){var _0x494683=new Graph();for(var _0x515a74 in _0x3f937b){if(_0x3f937b[_0x8711('0x22')](_0x515a74)){if(_[_0x8711('0x27')](_0x3f937b[_0x515a74])){for(var _0x2d0901=0x0;_0x2d0901<_0x3f937b[_0x515a74][_0x8711('0x28')];_0x2d0901+=0x1){addVertex(_0x494683,_0x3f937b[_0x515a74][_0x2d0901],_0x515a74);}}else{addVertex(_0x494683,_0x3f937b[_0x515a74],_0x515a74);}}}for(var _0x12cfdc=0x0;_0x12cfdc<_0x514956[_0x8711('0x28')];_0x12cfdc+=0x1){if(_0x514956[_0x12cfdc][_0x8711('0x29')]&&_0x514956[_0x12cfdc][_0x8711('0x2a')]){_0x494683[_0x8711('0x2b')](_0x514956[_0x12cfdc]['source'],_0x514956[_0x12cfdc][_0x8711('0x2a')],_0x514956[_0x12cfdc]['value']);}}return _0x494683;}function getSquareProject(_0x28c2d5,_0xd0c671){rpc[_0x8711('0x2c')](_0x28c2d5)['then'](function(_0x440bc8){_0xd0c671(null,_0x440bc8);})['catch'](function(_0x2c4ba3){_0xd0c671(null,_0x2c4ba3);});}function createSquareReport(_0xa85687,_0x230376){rpc['createSquareReport'](_0xa85687)['then'](function(_0x465c5b){if(_0x230376){_0x230376(null,_0x465c5b);}})[_0x8711('0x2d')](function(_0x35af69){if(_0x230376){_0x230376(null,_0x35af69);}});}function setChannelVariables(_0x6ab2c7){for(var _0x1c2fae in _0x6ab2c7){if(_0x6ab2c7[_0x8711('0x22')](_0x1c2fae)){if(_0x1c2fae[_0x8711('0x2e')](_0x8711('0x2f'))===0x0){_0x6ab2c7[_0x1c2fae[_0x8711('0x30')](0x4)]=_0x6ab2c7[_0x1c2fae];delete _0x6ab2c7[_0x1c2fae];}}}}function getLogFromObject(_0x36f3bd){return util['inspect'](_0x36f3bd,{'showHidden':![],'depth':null});}function agiHandler(_0x3ede91){var _0x11c1e9,_0xa22baf,_0x285390,_0x11e2fc,_0x23a78b;logger[_0x8711('0x31')](_0x8711('0x32'),JSON[_0x8711('0x33')](_0x3ede91,null,0x2));logger[_0x8711('0x10')](util['format'](_0x8711('0x34'),_0x3ede91[_0x8711('0x18')],_0x3ede91[_0x8711('0x19')],_0x3ede91[_0x8711('0x1c')],_0x3ede91[_0x8711('0x1a')],_0x3ede91['uniqueid']));_0x3ede91['on'](_0x8711('0xc'),errorHandler);_0x3ede91['on'](_0x8711('0x35'),closeHandler);_0x3ede91['on'](_0x8711('0x36'),timeoutHandler);_0x3ede91['evtHangup']=![];_0x3ede91['on'](_0x8711('0x37'),hangupHandler);var _0x37fc30=getSquareProject[_0x8711('0x38')](null,_0x3ede91[_0x8711('0x18')]);if(_0x37fc30){if(_0x37fc30[_0x8711('0x39')]){_0x11c1e9=getRoot(_0x37fc30[_0x8711('0x39')]);if(_0x11c1e9&&_0x11c1e9[_0x8711('0x3a')]){logger['debug'](_0x8711('0x20'),JSON[_0x8711('0x33')](_0x11c1e9,null,0x2));_0x3ede91[_0x8711('0x3b')]=moment()[_0x8711('0x11')]('YYYY-MM-DD\x20HH:mm:ss');_0xa22baf=initGraph(_['omit'](_0x11c1e9,_0x8711('0x26')),_0x11c1e9[_0x8711('0x26')]);_0x285390=new Vertices(_0x3ede91,rpc,mailRpc);logger[_0x8711('0x10')](util[_0x8711('0x11')](_0x8711('0x3c'),_0x3ede91[_0x8711('0x18')],_0x3ede91[_0x8711('0x19')],_0x3ede91[_0x8711('0x1c')],_0x3ede91[_0x8711('0x1a')],_0x3ede91[_0x8711('0x1b')]));_0xa22baf[_0x8711('0x3d')](_0x11c1e9[_0x8711('0x3a')]['id'],function(_0x1efd42){try{_0x1efd42=_0x285390[_0x8711('0x3e')](_0x1efd42);logger[_0x8711('0x31')](_0x8711('0x3f'),JSON[_0x8711('0x33')](_0x1efd42,null,0x2));if(_0x1efd42[_0x8711('0x21')]===_0x8711('0x40')){logger[_0x8711('0x10')](_0x8711('0x41'));_0x3ede91['context']=_0x1efd42[_0x8711('0x42')]||_0x3ede91['context'];_0x3ede91[_0x8711('0x43')]=_0x1efd42[_0x8711('0x43')];_0x3ede91[_0x8711('0x44')]=_0x1efd42[_0x8711('0x44')]?_0x1efd42['priority']-0x1:0x1;return null;}else if(_0x1efd42&&_0x285390[_0x1efd42[_0x8711('0x21')]]){_0x23a78b=_0x285390[_0x1efd42[_0x8711('0x21')]](_0x1efd42);logger[_0x8711('0x10')](_0x8711('0x45'),JSON[_0x8711('0x33')](_0x23a78b,null,0x2));if(_0x23a78b&&_0x23a78b[_0x8711('0x46')]===AGI_CODE_SUCCESS&&_0x23a78b['result']!==AGI_RESULT_ERROR&&!_0x3ede91[_0x8711('0x1e')]){logger[_0x8711('0x31')](_0x8711('0x45'),getLogFromObject(_0x23a78b));return _[_0x8711('0x47')](_0x23a78b['result'])?null:_0x23a78b[_0x8711('0x48')][_0x8711('0x49')]();}else{logger[_0x8711('0xc')](_0x8711('0x45'),getLogFromObject(_0x23a78b));return null;}}else{logger[_0x8711('0xc')]('agicommand\x20not\x20found');return null;}}catch(_0x4a0436){logger[_0x8711('0xc')](_0x4a0436[_0x8711('0xd')]);return null;}});if(_0x11c1e9['finally']){logger[_0x8711('0x10')](util[_0x8711('0x11')](_0x8711('0x4a'),_0x3ede91[_0x8711('0x18')],_0x3ede91['calleridname'],_0x3ede91[_0x8711('0x1c')],_0x3ede91[_0x8711('0x1a')],_0x3ede91[_0x8711('0x1b')]));_0xa22baf['traverseBFS'](_0x11c1e9[_0x8711('0x4b')]['id'],function(_0x29b79e){try{_0x29b79e=_0x285390['clear'](_0x29b79e);logger[_0x8711('0x31')](_0x8711('0x3f'),JSON['stringify'](_0x29b79e,null,0x2));if(_0x29b79e&&_0x285390[_0x29b79e[_0x8711('0x21')]]){_0x23a78b=_0x285390[_0x29b79e[_0x8711('0x21')]](_0x29b79e);if(_0x23a78b){logger['debug'](_0x8711('0x45'),getLogFromObject(_0x23a78b));return _[_0x8711('0x47')](_0x23a78b[_0x8711('0x48')])?null:_0x23a78b[_0x8711('0x48')]['toString']();}else{logger[_0x8711('0xc')](_0x8711('0x45'),getLogFromObject(_0x23a78b));return null;}}else{logger['error'](_0x8711('0x4c'));return null;}}catch(_0x209813){logger['error'](_0x209813['stack']);return null;}});}logger['info'](util['format'](_0x8711('0x4d'),_0x3ede91[_0x8711('0x18')],_0x3ede91[_0x8711('0x19')],_0x3ede91[_0x8711('0x1c')],_0x3ede91[_0x8711('0x1a')],_0x3ede91[_0x8711('0x1b')]));logger[_0x8711('0x10')](util['format']('[GOTO]\x20context:\x20%s\x20extension:\x20%s\x20priority:\x20%s',_0x3ede91['context'],_0x3ede91[_0x8711('0x43')],parseInt(_0x3ede91[_0x8711('0x44')],0xa)+0x1));_0x3ede91[_0x8711('0x4e')](_0x3ede91[_0x8711('0x42')],_0x3ede91[_0x8711('0x43')],parseInt(_0x3ede91[_0x8711('0x44')],0xa)+0x1);_0x3ede91['close']();}}else{logger[_0x8711('0xc')](util[_0x8711('0x11')](_0x8711('0x4f'),_0x3ede91['arg_1']));_0x3ede91[_0x8711('0x4e')](_0x3ede91[_0x8711('0x42')],_0x3ede91[_0x8711('0x43')],parseInt(_0x3ede91[_0x8711('0x44')],0xa)+0x1);_0x3ede91[_0x8711('0x35')]();}}else{logger[_0x8711('0xc')](util[_0x8711('0x11')](_0x8711('0x50'),_0x3ede91['arg_1']));_0x3ede91[_0x8711('0x4e')](_0x3ede91['context'],_0x3ede91[_0x8711('0x43')],parseInt(_0x3ede91['priority'],0xa)+0x1);_0x3ede91['close']();}}function main(){var _0x4f6f3d;return utilLicense[_0x8711('0x51')]()[_0x8711('0x52')](function(_0x5a1468){if(_0x5a1468){if(_0x5a1468[_0x8711('0x53')]){if(_0x5a1468['callysquare']&&_0x5a1468[_0x8711('0x54')]>0x0){logger[_0x8711('0x10')](util['format'](_0x8711('0x55'),_0x5a1468['callysquare']));_0x4f6f3d=_0x5a1468[_0x8711('0x54')];}else{logger[_0x8711('0x10')]('[LICENSE]\x20CHANNELS:\x20UNLIMITED');}}else{logger['info'](_0x8711('0x56'));_0x4f6f3d=-0x1;}}})['catch'](function(_0x15c02f){logger[_0x8711('0xc')](_0x8711('0x57'),_0x15c02f[_0x8711('0xd')]);})[_0x8711('0x4b')](function(){var _0x5dfb08=require(_0x8711('0x58'))(config[_0x8711('0x4')][_0x8711('0x13')],config[_0x8711('0x4')]['ip'],_0x4f6f3d);_0x5dfb08['on'](_0x8711('0xc'),errorHandler);_0x5dfb08['on']('listening',listeningHandler);_0x5dfb08['on'](_0x8711('0x59'),connectionHandler);_0x5dfb08['on'](_0x8711('0x35'),closeHandler);});}main();