d2aecdd14cd6220d59f1bca260bf6ddc947480d0
[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 _0x4bc3=['util','moment','../../config/logger','agi','../../config/license/util','./rpc','./mailRpc','./graph','./vertices','defaults','nextTick','error','stack','log','port','info','format','name','[%s]\x20%s','message','[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','[HANGUP]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','evtHangup','toJson','agicommand','hasOwnProperty','retry','maxRetry','addVertex','omit','mxCell','isArray','length','target','addEdge','source','value','getSquareProject','then','catch','createSquareReport','indexOf','agi_','substring','debug','variables','stringify','close','timeout','sync','production','start','root','joinAt','YYYY-MM-DD\x20HH:mm:ss','[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','result','isUndefined','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','[END]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','continueAt','Project\x20%s\x20not\x20found','getLicense','voice','callysquare','[LICENSE]\x20CHANNELS:\x20%s','[LICENSE]\x20CHANNELS:\x20UNLIMITED','[LICENSE]\x20VOICE\x20IS\x20DISABLED','[LICENSE]','./server','listening','connection','xml2json','lodash'];(function(_0xa464c9,_0x7c86d8){var _0x3d73c4=function(_0x5c2056){while(--_0x5c2056){_0xa464c9['push'](_0xa464c9['shift']());}};_0x3d73c4(++_0x7c86d8);}(_0x4bc3,0x16e));var _0x34bc=function(_0x68c979,_0x431cff){_0x68c979=_0x68c979-0x0;var _0x4f0001=_0x4bc3[_0x68c979];return _0x4f0001;};'use strict';var parser=require(_0x34bc('0x0'));var _=require(_0x34bc('0x1'));var syncho=require('syncho');var util=require(_0x34bc('0x2'));var moment=require(_0x34bc('0x3'));var config=require('../../config/environment');var logger=require(_0x34bc('0x4'))(_0x34bc('0x5'));var utilLicense=require(_0x34bc('0x6'));var rpc=require(_0x34bc('0x7'));var mailRpc=require(_0x34bc('0x8'));var Graph=require(_0x34bc('0x9'));var Vertices=require(_0x34bc('0xa'));var AGI_CODE_SUCCESS=0xc8;var AGI_RESULT_ERROR=-0x1;config['agi']=_[_0x34bc('0xb')](config['agi'],{'ip':'127.0.0.1','port':0x11dd});function connectionHandler(_0x18a01c){process[_0x34bc('0xc')](function(){syncho(function(){try{setChannelVariables(_0x18a01c);agiHandler(_0x18a01c);}catch(_0x1f72d8){logger[_0x34bc('0xd')](_0x1f72d8[_0x34bc('0xe')]);}});});}function listeningHandler(){console[_0x34bc('0xf')]('Cally\x20Square\x20server\x20listening\x20on\x20port\x20%d!',config['agi'][_0x34bc('0x10')]);logger[_0x34bc('0x11')](util[_0x34bc('0x12')]('[LISTENING]\x20Cally\x20Square\x20listening\x20%s:%d',config[_0x34bc('0x5')]['ip'],config['agi'][_0x34bc('0x10')]));}function errorHandler(_0x124b87){if(_0x124b87&&_0x124b87[_0x34bc('0x13')]&&_0x124b87['message']){logger[_0x34bc('0xd')](util['format'](_0x34bc('0x14'),_0x124b87[_0x34bc('0x13')],_0x124b87[_0x34bc('0x15')]));}else{logger['error'](util['format'](_0x34bc('0x16'),_0x124b87['stack']));}}function closeHandler(){logger[_0x34bc('0x11')](util[_0x34bc('0x12')](_0x34bc('0x17'),this[_0x34bc('0x18')],this[_0x34bc('0x19')],this[_0x34bc('0x1a')],this['dnid'],this['uniqueid']));try{createSquareReport(this);}catch(_0x5a5895){logger[_0x34bc('0xd')](_0x5a5895[_0x34bc('0xe')]);}}function timeoutHandler(){logger[_0x34bc('0x11')](util[_0x34bc('0x12')]('[TIMEOUT]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',this[_0x34bc('0x18')],this[_0x34bc('0x19')],this['callerid'],this[_0x34bc('0x1b')],this[_0x34bc('0x1c')]));}function hangupHandler(){logger['info'](util[_0x34bc('0x12')](_0x34bc('0x1d'),this[_0x34bc('0x18')],this[_0x34bc('0x19')],this[_0x34bc('0x1a')],this[_0x34bc('0x1b')],this['uniqueid']));this[_0x34bc('0x1e')]=!![];}function getRoot(_0x3c9939){return parser[_0x34bc('0x1f')](_0x3c9939,{'object':!![]})['mxGraphModel']['root'];}function addVertex(_0x358d51,_0x1b820a,_0x4263de){_0x1b820a[_0x34bc('0x20')]=_0x4263de;if(_0x1b820a[_0x34bc('0x21')](_0x34bc('0x22'))){_0x1b820a[_0x34bc('0x23')]=parseInt(_0x1b820a[_0x34bc('0x22')],0xa);}_0x358d51[_0x34bc('0x24')](_[_0x34bc('0x25')](_0x1b820a,_0x34bc('0x26')));}function initGraph(_0x1edcb0,_0x4fd589){var _0x43e527=new Graph();for(var _0x48ec13 in _0x1edcb0){if(_0x1edcb0['hasOwnProperty'](_0x48ec13)){if(_[_0x34bc('0x27')](_0x1edcb0[_0x48ec13])){for(var _0x5332c8=0x0;_0x5332c8<_0x1edcb0[_0x48ec13][_0x34bc('0x28')];_0x5332c8+=0x1){addVertex(_0x43e527,_0x1edcb0[_0x48ec13][_0x5332c8],_0x48ec13);}}else{addVertex(_0x43e527,_0x1edcb0[_0x48ec13],_0x48ec13);}}}for(var _0x446b58=0x0;_0x446b58<_0x4fd589[_0x34bc('0x28')];_0x446b58+=0x1){if(_0x4fd589[_0x446b58]['source']&&_0x4fd589[_0x446b58][_0x34bc('0x29')]){_0x43e527[_0x34bc('0x2a')](_0x4fd589[_0x446b58][_0x34bc('0x2b')],_0x4fd589[_0x446b58][_0x34bc('0x29')],_0x4fd589[_0x446b58][_0x34bc('0x2c')]);}}return _0x43e527;}function getSquareProject(_0x33bf46,_0x520fd4){rpc[_0x34bc('0x2d')](_0x33bf46)[_0x34bc('0x2e')](function(_0x22aacc){_0x520fd4(null,_0x22aacc);})[_0x34bc('0x2f')](function(_0x2241b4){_0x520fd4(null,_0x2241b4);});}function createSquareReport(_0xd92957,_0x5b37de){rpc[_0x34bc('0x30')](_0xd92957)['then'](function(_0x530748){if(_0x5b37de){_0x5b37de(null,_0x530748);}})[_0x34bc('0x2f')](function(_0x540aa3){if(_0x5b37de){_0x5b37de(null,_0x540aa3);}});}function setChannelVariables(_0x1e8445){for(var _0x2ffd4b in _0x1e8445){if(_0x1e8445[_0x34bc('0x21')](_0x2ffd4b)){if(_0x2ffd4b[_0x34bc('0x31')](_0x34bc('0x32'))===0x0){_0x1e8445[_0x2ffd4b[_0x34bc('0x33')](0x4)]=_0x1e8445[_0x2ffd4b];delete _0x1e8445[_0x2ffd4b];}}}}function getLogFromObject(_0x2f0ce5){return util['inspect'](_0x2f0ce5,{'showHidden':![],'depth':null});}function agiHandler(_0xbd59f9){var _0x3a5a74,_0xc0a30e,_0x3224c7,_0x360126,_0x5c7e56;logger[_0x34bc('0x34')](_0x34bc('0x35'),JSON[_0x34bc('0x36')](_0xbd59f9,null,0x2));logger[_0x34bc('0x11')](util[_0x34bc('0x12')]('[START]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',_0xbd59f9['arg_1'],_0xbd59f9[_0x34bc('0x19')],_0xbd59f9[_0x34bc('0x1a')],_0xbd59f9[_0x34bc('0x1b')],_0xbd59f9[_0x34bc('0x1c')]));_0xbd59f9['on']('error',errorHandler);_0xbd59f9['on'](_0x34bc('0x37'),closeHandler);_0xbd59f9['on'](_0x34bc('0x38'),timeoutHandler);_0xbd59f9[_0x34bc('0x1e')]=![];_0xbd59f9['on']('hangup',hangupHandler);var _0x37cd30=getSquareProject[_0x34bc('0x39')](null,_0xbd59f9[_0x34bc('0x18')]);if(_0x37cd30){if(_0x37cd30[_0x34bc('0x3a')]){_0x3a5a74=getRoot(_0x37cd30['production']);if(_0x3a5a74&&_0x3a5a74[_0x34bc('0x3b')]){logger[_0x34bc('0x34')](_0x34bc('0x3c'),JSON['stringify'](_0x3a5a74,null,0x2));_0xbd59f9[_0x34bc('0x3d')]=moment()[_0x34bc('0x12')](_0x34bc('0x3e'));_0xc0a30e=initGraph(_[_0x34bc('0x25')](_0x3a5a74,_0x34bc('0x26')),_0x3a5a74['mxCell']);_0x3224c7=new Vertices(_0xbd59f9,rpc,mailRpc);logger[_0x34bc('0x11')](util[_0x34bc('0x12')](_0x34bc('0x3f'),_0xbd59f9[_0x34bc('0x18')],_0xbd59f9[_0x34bc('0x19')],_0xbd59f9['callerid'],_0xbd59f9[_0x34bc('0x1b')],_0xbd59f9['uniqueid']));_0xc0a30e[_0x34bc('0x40')](_0x3a5a74[_0x34bc('0x3b')]['id'],function(_0xfaa92d){try{_0xfaa92d=_0x3224c7[_0x34bc('0x41')](_0xfaa92d);logger['debug'](_0x34bc('0x42'),JSON[_0x34bc('0x36')](_0xfaa92d,null,0x2));if(_0xfaa92d['agicommand']===_0x34bc('0x43')){logger[_0x34bc('0x11')](_0x34bc('0x44'));_0xbd59f9['context']=_0xfaa92d[_0x34bc('0x45')]||_0xbd59f9[_0x34bc('0x45')];_0xbd59f9[_0x34bc('0x46')]=_0xfaa92d[_0x34bc('0x46')];_0xbd59f9[_0x34bc('0x47')]=_0xfaa92d[_0x34bc('0x47')]?_0xfaa92d[_0x34bc('0x47')]-0x1:0x1;return null;}else if(_0xfaa92d&&_0x3224c7[_0xfaa92d[_0x34bc('0x20')]]){_0x5c7e56=_0x3224c7[_0xfaa92d[_0x34bc('0x20')]](_0xfaa92d);logger[_0x34bc('0x11')](_0x34bc('0x48'),JSON['stringify'](_0x5c7e56,null,0x2));if(_0x5c7e56&&_0x5c7e56['code']===AGI_CODE_SUCCESS&&_0x5c7e56[_0x34bc('0x49')]!==AGI_RESULT_ERROR&&!_0xbd59f9[_0x34bc('0x1e')]){logger[_0x34bc('0x34')](_0x34bc('0x48'),getLogFromObject(_0x5c7e56));return _[_0x34bc('0x4a')](_0x5c7e56[_0x34bc('0x49')])?null:_0x5c7e56['result'][_0x34bc('0x4b')]();}else{logger['error'](_0x34bc('0x48'),getLogFromObject(_0x5c7e56));return null;}}else{logger[_0x34bc('0xd')](_0x34bc('0x4c'));return null;}}catch(_0x4ba4eb){logger[_0x34bc('0xd')](_0x4ba4eb[_0x34bc('0xe')]);return null;}});if(_0x3a5a74[_0x34bc('0x4d')]){logger[_0x34bc('0x11')](util[_0x34bc('0x12')](_0x34bc('0x4e'),_0xbd59f9[_0x34bc('0x18')],_0xbd59f9[_0x34bc('0x19')],_0xbd59f9[_0x34bc('0x1a')],_0xbd59f9[_0x34bc('0x1b')],_0xbd59f9[_0x34bc('0x1c')]));_0xc0a30e[_0x34bc('0x40')](_0x3a5a74['finally']['id'],function(_0x56494f){try{_0x56494f=_0x3224c7[_0x34bc('0x41')](_0x56494f);logger[_0x34bc('0x34')](_0x34bc('0x42'),JSON['stringify'](_0x56494f,null,0x2));if(_0x56494f&&_0x3224c7[_0x56494f['agicommand']]){_0x5c7e56=_0x3224c7[_0x56494f[_0x34bc('0x20')]](_0x56494f);if(_0x5c7e56){logger[_0x34bc('0x34')](_0x34bc('0x48'),getLogFromObject(_0x5c7e56));return _['isUndefined'](_0x5c7e56[_0x34bc('0x49')])?null:_0x5c7e56[_0x34bc('0x49')]['toString']();}else{logger[_0x34bc('0xd')]('response',getLogFromObject(_0x5c7e56));return null;}}else{logger[_0x34bc('0xd')]('agicommand\x20not\x20found');return null;}}catch(_0x281d6f){logger[_0x34bc('0xd')](_0x281d6f[_0x34bc('0xe')]);return null;}});}logger[_0x34bc('0x11')](util['format'](_0x34bc('0x4f'),_0xbd59f9[_0x34bc('0x18')],_0xbd59f9[_0x34bc('0x19')],_0xbd59f9[_0x34bc('0x1a')],_0xbd59f9[_0x34bc('0x1b')],_0xbd59f9[_0x34bc('0x1c')]));logger[_0x34bc('0x11')](util[_0x34bc('0x12')]('[GOTO]\x20context:\x20%s\x20extension:\x20%s\x20priority:\x20%s',_0xbd59f9['context'],_0xbd59f9['extension'],parseInt(_0xbd59f9[_0x34bc('0x47')],0xa)+0x1));_0xbd59f9[_0x34bc('0x50')](_0xbd59f9['context'],_0xbd59f9[_0x34bc('0x46')],parseInt(_0xbd59f9[_0x34bc('0x47')],0xa)+0x1);_0xbd59f9[_0x34bc('0x37')]();}}else{logger[_0x34bc('0xd')](util[_0x34bc('0x12')]('Project\x20%s\x20not\x20published',_0xbd59f9['arg_1']));_0xbd59f9['continueAt'](_0xbd59f9[_0x34bc('0x45')],_0xbd59f9[_0x34bc('0x46')],parseInt(_0xbd59f9[_0x34bc('0x47')],0xa)+0x1);_0xbd59f9[_0x34bc('0x37')]();}}else{logger[_0x34bc('0xd')](util['format'](_0x34bc('0x51'),_0xbd59f9[_0x34bc('0x18')]));_0xbd59f9[_0x34bc('0x50')](_0xbd59f9[_0x34bc('0x45')],_0xbd59f9['extension'],parseInt(_0xbd59f9[_0x34bc('0x47')],0xa)+0x1);_0xbd59f9[_0x34bc('0x37')]();}}function main(){var _0x29d7df;return utilLicense[_0x34bc('0x52')]()[_0x34bc('0x2e')](function(_0x31c40b){if(_0x31c40b){if(_0x31c40b[_0x34bc('0x53')]){if(_0x31c40b[_0x34bc('0x54')]&&_0x31c40b[_0x34bc('0x54')]>0x0){logger[_0x34bc('0x11')](util[_0x34bc('0x12')](_0x34bc('0x55'),_0x31c40b[_0x34bc('0x54')]));_0x29d7df=_0x31c40b['callysquare'];}else{logger[_0x34bc('0x11')](_0x34bc('0x56'));}}else{logger[_0x34bc('0x11')](_0x34bc('0x57'));_0x29d7df=-0x1;}}})[_0x34bc('0x2f')](function(_0x1b3704){logger[_0x34bc('0xd')](_0x34bc('0x58'),_0x1b3704[_0x34bc('0xe')]);})[_0x34bc('0x4d')](function(){var _0x11cd4c=require(_0x34bc('0x59'))(config[_0x34bc('0x5')][_0x34bc('0x10')],config['agi']['ip'],_0x29d7df);_0x11cd4c['on'](_0x34bc('0xd'),errorHandler);_0x11cd4c['on'](_0x34bc('0x5a'),listeningHandler);_0x11cd4c['on'](_0x34bc('0x5b'),connectionHandler);_0x11cd4c['on'](_0x34bc('0x37'),closeHandler);});}main();