Built motion from commit e2351160.|2.5.50
[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 _0x41f0=['agicommand','hasOwnProperty','retry','maxRetry','omit','isArray','length','source','target','addEdge','getSquareProject','then','catch','createSquareReport','indexOf','inspect','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','root','joinAt','YYYY-MM-DD\x20HH:mm:ss','mxCell','traverseBFS','vertex','gotoc','gotoc\x20block\x20stop\x20traverse\x20BFS','context','extension','priority','response','code','result','toString','agicommand\x20not\x20found','finally','clear','isUndefined','[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','Project\x20%s\x20not\x20published','Project\x20%s\x20not\x20found','getLicense','callysquare','[LICENSE]\x20CHANNELS:\x20UNLIMITED','[LICENSE]\x20VOICE\x20IS\x20DISABLED','[LICENSE]','./server','listening','connection','xml2json','lodash','syncho','util','../../config/environment','../../config/logger','agi','../../config/license/util','./rpc','./mailRpc','./vertices','defaults','127.0.0.1','nextTick','stack','log','Cally\x20Square\x20server\x20listening\x20on\x20port\x20%d!','port','info','[LISTENING]\x20Cally\x20Square\x20listening\x20%s:%d','name','message','error','format','[%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','callerid','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','mxGraphModel'];(function(_0x24b1ea,_0x642a66){var _0x1eb3b0=function(_0x57c90b){while(--_0x57c90b){_0x24b1ea['push'](_0x24b1ea['shift']());}};_0x1eb3b0(++_0x642a66);}(_0x41f0,0x99));var _0x041f=function(_0x5ead01,_0x1449d9){_0x5ead01=_0x5ead01-0x0;var _0x226537=_0x41f0[_0x5ead01];return _0x226537;};'use strict';var parser=require(_0x041f('0x0'));var _=require(_0x041f('0x1'));var syncho=require(_0x041f('0x2'));var util=require(_0x041f('0x3'));var moment=require('moment');var config=require(_0x041f('0x4'));var logger=require(_0x041f('0x5'))(_0x041f('0x6'));var utilLicense=require(_0x041f('0x7'));var rpc=require(_0x041f('0x8'));var mailRpc=require(_0x041f('0x9'));var Graph=require('./graph');var Vertices=require(_0x041f('0xa'));var AGI_CODE_SUCCESS=0xc8;var AGI_RESULT_ERROR=-0x1;config['agi']=_[_0x041f('0xb')](config['agi'],{'ip':_0x041f('0xc'),'port':0x11dd});function connectionHandler(_0x1332dc){process[_0x041f('0xd')](function(){syncho(function(){try{setChannelVariables(_0x1332dc);agiHandler(_0x1332dc);}catch(_0x4792e0){logger['error'](_0x4792e0[_0x041f('0xe')]);}});});}function listeningHandler(){console[_0x041f('0xf')](_0x041f('0x10'),config['agi'][_0x041f('0x11')]);logger[_0x041f('0x12')](util['format'](_0x041f('0x13'),config[_0x041f('0x6')]['ip'],config['agi'][_0x041f('0x11')]));}function errorHandler(_0x15dabe){if(_0x15dabe&&_0x15dabe[_0x041f('0x14')]&&_0x15dabe[_0x041f('0x15')]){logger[_0x041f('0x16')](util[_0x041f('0x17')](_0x041f('0x18'),_0x15dabe[_0x041f('0x14')],_0x15dabe[_0x041f('0x15')]));}else{logger[_0x041f('0x16')](util[_0x041f('0x17')](_0x041f('0x19'),_0x15dabe[_0x041f('0xe')]));}}function closeHandler(){logger['info'](util[_0x041f('0x17')](_0x041f('0x1a'),this[_0x041f('0x1b')],this[_0x041f('0x1c')],this[_0x041f('0x1d')],this[_0x041f('0x1e')],this[_0x041f('0x1f')]));try{createSquareReport(this);}catch(_0x3f5079){logger[_0x041f('0x16')](_0x3f5079[_0x041f('0xe')]);}}function timeoutHandler(){logger[_0x041f('0x12')](util['format'](_0x041f('0x20'),this[_0x041f('0x1b')],this['calleridname'],this[_0x041f('0x1d')],this['dnid'],this['uniqueid']));}function hangupHandler(){logger['info'](util['format'](_0x041f('0x21'),this[_0x041f('0x1b')],this[_0x041f('0x1c')],this[_0x041f('0x1d')],this[_0x041f('0x1e')],this[_0x041f('0x1f')]));this[_0x041f('0x22')]=!![];}function getRoot(_0x64dec7){return parser[_0x041f('0x23')](_0x64dec7,{'object':!![]})[_0x041f('0x24')]['root'];}function addVertex(_0x5ec3ea,_0xac3d10,_0x19663b){_0xac3d10[_0x041f('0x25')]=_0x19663b;if(_0xac3d10[_0x041f('0x26')](_0x041f('0x27'))){_0xac3d10[_0x041f('0x28')]=parseInt(_0xac3d10[_0x041f('0x27')],0xa);}_0x5ec3ea['addVertex'](_[_0x041f('0x29')](_0xac3d10,'mxCell'));}function initGraph(_0x19199d,_0x20ad94){var _0x34499a=new Graph();for(var _0x104ae5 in _0x19199d){if(_0x19199d[_0x041f('0x26')](_0x104ae5)){if(_[_0x041f('0x2a')](_0x19199d[_0x104ae5])){for(var _0x1c2994=0x0;_0x1c2994<_0x19199d[_0x104ae5]['length'];_0x1c2994+=0x1){addVertex(_0x34499a,_0x19199d[_0x104ae5][_0x1c2994],_0x104ae5);}}else{addVertex(_0x34499a,_0x19199d[_0x104ae5],_0x104ae5);}}}for(var _0x861106=0x0;_0x861106<_0x20ad94[_0x041f('0x2b')];_0x861106+=0x1){if(_0x20ad94[_0x861106][_0x041f('0x2c')]&&_0x20ad94[_0x861106][_0x041f('0x2d')]){_0x34499a[_0x041f('0x2e')](_0x20ad94[_0x861106][_0x041f('0x2c')],_0x20ad94[_0x861106][_0x041f('0x2d')],_0x20ad94[_0x861106]['value']);}}return _0x34499a;}function getSquareProject(_0x309764,_0x546e54){rpc[_0x041f('0x2f')](_0x309764)[_0x041f('0x30')](function(_0x31f6e9){_0x546e54(null,_0x31f6e9);})[_0x041f('0x31')](function(_0x2eaa79){_0x546e54(null,_0x2eaa79);});}function createSquareReport(_0x32eba7,_0x5d7311){rpc[_0x041f('0x32')](_0x32eba7)[_0x041f('0x30')](function(_0x217ce7){if(_0x5d7311){_0x5d7311(null,_0x217ce7);}})[_0x041f('0x31')](function(_0x57186e){if(_0x5d7311){_0x5d7311(null,_0x57186e);}});}function setChannelVariables(_0x4678fb){for(var _0x32d71d in _0x4678fb){if(_0x4678fb[_0x041f('0x26')](_0x32d71d)){if(_0x32d71d[_0x041f('0x33')]('agi_')===0x0){_0x4678fb[_0x32d71d['substring'](0x4)]=_0x4678fb[_0x32d71d];delete _0x4678fb[_0x32d71d];}}}}function getLogFromObject(_0x4643e4){return util[_0x041f('0x34')](_0x4643e4,{'showHidden':![],'depth':null});}function agiHandler(_0x110480){var _0x1a0672,_0x945d6a,_0x5656f6,_0x43f149,_0xdf99da;logger[_0x041f('0x35')](_0x041f('0x36'),JSON[_0x041f('0x37')](_0x110480,null,0x2));logger[_0x041f('0x12')](util[_0x041f('0x17')](_0x041f('0x38'),_0x110480[_0x041f('0x1b')],_0x110480[_0x041f('0x1c')],_0x110480['callerid'],_0x110480[_0x041f('0x1e')],_0x110480[_0x041f('0x1f')]));_0x110480['on'](_0x041f('0x16'),errorHandler);_0x110480['on'](_0x041f('0x39'),closeHandler);_0x110480['on'](_0x041f('0x3a'),timeoutHandler);_0x110480[_0x041f('0x22')]=![];_0x110480['on'](_0x041f('0x3b'),hangupHandler);var _0x54125a=getSquareProject[_0x041f('0x3c')](null,_0x110480[_0x041f('0x1b')]);if(_0x54125a){if(_0x54125a[_0x041f('0x3d')]){_0x1a0672=getRoot(_0x54125a[_0x041f('0x3d')]);if(_0x1a0672&&_0x1a0672[_0x041f('0x3e')]){logger[_0x041f('0x35')](_0x041f('0x3f'),JSON[_0x041f('0x37')](_0x1a0672,null,0x2));_0x110480[_0x041f('0x40')]=moment()[_0x041f('0x17')](_0x041f('0x41'));_0x945d6a=initGraph(_['omit'](_0x1a0672,_0x041f('0x42')),_0x1a0672[_0x041f('0x42')]);_0x5656f6=new Vertices(_0x110480,rpc,mailRpc);logger['info'](util[_0x041f('0x17')]('[START\x20TRAVERSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',_0x110480[_0x041f('0x1b')],_0x110480[_0x041f('0x1c')],_0x110480[_0x041f('0x1d')],_0x110480[_0x041f('0x1e')],_0x110480['uniqueid']));_0x945d6a[_0x041f('0x43')](_0x1a0672[_0x041f('0x3e')]['id'],function(_0x17723b){try{_0x17723b=_0x5656f6['clear'](_0x17723b);logger['debug'](_0x041f('0x44'),JSON[_0x041f('0x37')](_0x17723b,null,0x2));if(_0x17723b[_0x041f('0x25')]===_0x041f('0x45')){logger['info'](_0x041f('0x46'));_0x110480[_0x041f('0x47')]=_0x17723b[_0x041f('0x47')]||_0x110480[_0x041f('0x47')];_0x110480[_0x041f('0x48')]=_0x17723b[_0x041f('0x48')];_0x110480['priority']=_0x17723b[_0x041f('0x49')]?_0x17723b['priority']-0x1:0x1;return null;}else if(_0x17723b&&_0x5656f6[_0x17723b[_0x041f('0x25')]]){_0xdf99da=_0x5656f6[_0x17723b[_0x041f('0x25')]](_0x17723b);logger[_0x041f('0x12')](_0x041f('0x4a'),JSON['stringify'](_0xdf99da,null,0x2));if(_0xdf99da&&_0xdf99da[_0x041f('0x4b')]===AGI_CODE_SUCCESS&&_0xdf99da['result']!==AGI_RESULT_ERROR&&!_0x110480[_0x041f('0x22')]){logger[_0x041f('0x35')]('response',getLogFromObject(_0xdf99da));return _['isUndefined'](_0xdf99da[_0x041f('0x4c')])?null:_0xdf99da[_0x041f('0x4c')][_0x041f('0x4d')]();}else{logger[_0x041f('0x16')](_0x041f('0x4a'),getLogFromObject(_0xdf99da));return null;}}else{logger[_0x041f('0x16')](_0x041f('0x4e'));return null;}}catch(_0x468042){logger[_0x041f('0x16')](_0x468042[_0x041f('0xe')]);return null;}});if(_0x1a0672[_0x041f('0x4f')]){logger[_0x041f('0x12')](util[_0x041f('0x17')]('[FINALLY\x20TRAVERSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',_0x110480[_0x041f('0x1b')],_0x110480[_0x041f('0x1c')],_0x110480[_0x041f('0x1d')],_0x110480['dnid'],_0x110480['uniqueid']));_0x945d6a[_0x041f('0x43')](_0x1a0672[_0x041f('0x4f')]['id'],function(_0x56b130){try{_0x56b130=_0x5656f6[_0x041f('0x50')](_0x56b130);logger['debug'](_0x041f('0x44'),JSON[_0x041f('0x37')](_0x56b130,null,0x2));if(_0x56b130&&_0x5656f6[_0x56b130[_0x041f('0x25')]]){_0xdf99da=_0x5656f6[_0x56b130[_0x041f('0x25')]](_0x56b130);if(_0xdf99da){logger[_0x041f('0x35')](_0x041f('0x4a'),getLogFromObject(_0xdf99da));return _[_0x041f('0x51')](_0xdf99da[_0x041f('0x4c')])?null:_0xdf99da[_0x041f('0x4c')][_0x041f('0x4d')]();}else{logger[_0x041f('0x16')](_0x041f('0x4a'),getLogFromObject(_0xdf99da));return null;}}else{logger['error'](_0x041f('0x4e'));return null;}}catch(_0x4fa36b){logger[_0x041f('0x16')](_0x4fa36b[_0x041f('0xe')]);return null;}});}logger['info'](util[_0x041f('0x17')](_0x041f('0x52'),_0x110480[_0x041f('0x1b')],_0x110480['calleridname'],_0x110480[_0x041f('0x1d')],_0x110480[_0x041f('0x1e')],_0x110480[_0x041f('0x1f')]));logger[_0x041f('0x12')](util[_0x041f('0x17')](_0x041f('0x53'),_0x110480[_0x041f('0x47')],_0x110480[_0x041f('0x48')],parseInt(_0x110480['priority'],0xa)+0x1));_0x110480[_0x041f('0x54')](_0x110480[_0x041f('0x47')],_0x110480[_0x041f('0x48')],parseInt(_0x110480['priority'],0xa)+0x1);_0x110480[_0x041f('0x39')]();}}else{logger[_0x041f('0x16')](util['format'](_0x041f('0x55'),_0x110480[_0x041f('0x1b')]));_0x110480[_0x041f('0x54')](_0x110480['context'],_0x110480[_0x041f('0x48')],parseInt(_0x110480[_0x041f('0x49')],0xa)+0x1);_0x110480[_0x041f('0x39')]();}}else{logger['error'](util[_0x041f('0x17')](_0x041f('0x56'),_0x110480['arg_1']));_0x110480['continueAt'](_0x110480[_0x041f('0x47')],_0x110480[_0x041f('0x48')],parseInt(_0x110480[_0x041f('0x49')],0xa)+0x1);_0x110480[_0x041f('0x39')]();}}function main(){var _0xcecebc;return utilLicense[_0x041f('0x57')]()[_0x041f('0x30')](function(_0x121ae8){if(_0x121ae8){if(_0x121ae8['voice']){if(_0x121ae8['callysquare']&&_0x121ae8['callysquare']>0x0){logger[_0x041f('0x12')](util['format']('[LICENSE]\x20CHANNELS:\x20%s',_0x121ae8[_0x041f('0x58')]));_0xcecebc=_0x121ae8[_0x041f('0x58')];}else{logger[_0x041f('0x12')](_0x041f('0x59'));}}else{logger[_0x041f('0x12')](_0x041f('0x5a'));_0xcecebc=-0x1;}}})[_0x041f('0x31')](function(_0x31e61f){logger[_0x041f('0x16')](_0x041f('0x5b'),_0x31e61f[_0x041f('0xe')]);})[_0x041f('0x4f')](function(){var _0x2d4c5c=require(_0x041f('0x5c'))(config[_0x041f('0x6')]['port'],config['agi']['ip'],_0xcecebc);_0x2d4c5c['on'](_0x041f('0x16'),errorHandler);_0x2d4c5c['on'](_0x041f('0x5d'),listeningHandler);_0x2d4c5c['on'](_0x041f('0x5e'),connectionHandler);_0x2d4c5c['on'](_0x041f('0x39'),closeHandler);});}main();