Built motion from commit (unavailable).|2.5.31
[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 _0x950a=['Project\x20%s\x20not\x20found','getLicense','voice','callysquare','[LICENSE]','./server','port','listening','connection','xml2json','lodash','syncho','util','../../config/environment','../../config/logger','agi','../../config/license/util','./mailRpc','./graph','defaults','nextTick','error','stack','log','info','[LISTENING]\x20Cally\x20Square\x20listening\x20%s:%d','message','format','name','[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','[TIMEOUT]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','callerid','[HANGUP]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','toJson','mxGraphModel','root','agicommand','hasOwnProperty','retry','maxRetry','addVertex','omit','mxCell','isArray','length','source','addEdge','value','getSquareProject','then','catch','indexOf','agi_','debug','variables','stringify','close','timeout','evtHangup','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','vertex','gotoc','gotoc\x20block\x20stop\x20traverse\x20BFS','context','extension','response','result','isUndefined','agicommand\x20not\x20found','[FINALLY\x20TRAVERSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','finally','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','priority','continueAt','Project\x20%s\x20not\x20published'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0x950a,0x111));var _0xa950=function(_0x1fcd1c,_0x31ccf7){_0x1fcd1c=_0x1fcd1c-0x0;var _0x3b158a=_0x950a[_0x1fcd1c];return _0x3b158a;};'use strict';var parser=require(_0xa950('0x0'));var _=require(_0xa950('0x1'));var syncho=require(_0xa950('0x2'));var util=require(_0xa950('0x3'));var moment=require('moment');var config=require(_0xa950('0x4'));var logger=require(_0xa950('0x5'))(_0xa950('0x6'));var utilLicense=require(_0xa950('0x7'));var rpc=require('./rpc');var mailRpc=require(_0xa950('0x8'));var Graph=require(_0xa950('0x9'));var Vertices=require('./vertices');var AGI_CODE_SUCCESS=0xc8;var AGI_RESULT_ERROR=-0x1;config[_0xa950('0x6')]=_[_0xa950('0xa')](config['agi'],{'ip':'127.0.0.1','port':0x11dd});function connectionHandler(_0x26e05c){process[_0xa950('0xb')](function(){syncho(function(){try{setChannelVariables(_0x26e05c);agiHandler(_0x26e05c);}catch(_0x7d87b2){logger[_0xa950('0xc')](_0x7d87b2[_0xa950('0xd')]);}});});}function listeningHandler(){console[_0xa950('0xe')]('Cally\x20Square\x20server\x20listening\x20on\x20port\x20%d!',config['agi']['port']);logger[_0xa950('0xf')](util['format'](_0xa950('0x10'),config[_0xa950('0x6')]['ip'],config[_0xa950('0x6')]['port']));}function errorHandler(_0xc7e9bd){if(_0xc7e9bd&&_0xc7e9bd['name']&&_0xc7e9bd[_0xa950('0x11')]){logger[_0xa950('0xc')](util[_0xa950('0x12')]('[%s]\x20%s',_0xc7e9bd[_0xa950('0x13')],_0xc7e9bd[_0xa950('0x11')]));}else{logger[_0xa950('0xc')](util['format'](_0xa950('0x14'),_0xc7e9bd[_0xa950('0xd')]));}}function closeHandler(){logger[_0xa950('0xf')](util['format'](_0xa950('0x15'),this[_0xa950('0x16')],this[_0xa950('0x17')],this['callerid'],this[_0xa950('0x18')],this[_0xa950('0x19')]));try{createSquareReport(this);}catch(_0x162586){logger[_0xa950('0xc')](_0x162586[_0xa950('0xd')]);}}function timeoutHandler(){logger[_0xa950('0xf')](util[_0xa950('0x12')](_0xa950('0x1a'),this[_0xa950('0x16')],this[_0xa950('0x17')],this[_0xa950('0x1b')],this[_0xa950('0x18')],this[_0xa950('0x19')]));}function hangupHandler(){logger['info'](util[_0xa950('0x12')](_0xa950('0x1c'),this['arg_1'],this[_0xa950('0x17')],this[_0xa950('0x1b')],this['dnid'],this[_0xa950('0x19')]));this['evtHangup']=!![];}function getRoot(_0x2eda79){return parser[_0xa950('0x1d')](_0x2eda79,{'object':!![]})[_0xa950('0x1e')][_0xa950('0x1f')];}function addVertex(_0x5617cd,_0x11b8af,_0x3f4633){_0x11b8af[_0xa950('0x20')]=_0x3f4633;if(_0x11b8af[_0xa950('0x21')](_0xa950('0x22'))){_0x11b8af[_0xa950('0x23')]=parseInt(_0x11b8af[_0xa950('0x22')],0xa);}_0x5617cd[_0xa950('0x24')](_[_0xa950('0x25')](_0x11b8af,_0xa950('0x26')));}function initGraph(_0x3c8c98,_0x26f366){var _0x43f8c8=new Graph();for(var _0x5d82d8 in _0x3c8c98){if(_0x3c8c98[_0xa950('0x21')](_0x5d82d8)){if(_[_0xa950('0x27')](_0x3c8c98[_0x5d82d8])){for(var _0x3f37f8=0x0;_0x3f37f8<_0x3c8c98[_0x5d82d8][_0xa950('0x28')];_0x3f37f8+=0x1){addVertex(_0x43f8c8,_0x3c8c98[_0x5d82d8][_0x3f37f8],_0x5d82d8);}}else{addVertex(_0x43f8c8,_0x3c8c98[_0x5d82d8],_0x5d82d8);}}}for(var _0x31c168=0x0;_0x31c168<_0x26f366[_0xa950('0x28')];_0x31c168+=0x1){if(_0x26f366[_0x31c168][_0xa950('0x29')]&&_0x26f366[_0x31c168]['target']){_0x43f8c8[_0xa950('0x2a')](_0x26f366[_0x31c168][_0xa950('0x29')],_0x26f366[_0x31c168]['target'],_0x26f366[_0x31c168][_0xa950('0x2b')]);}}return _0x43f8c8;}function getSquareProject(_0x2e7674,_0x3e5915){rpc[_0xa950('0x2c')](_0x2e7674)[_0xa950('0x2d')](function(_0x1945ca){_0x3e5915(null,_0x1945ca);})[_0xa950('0x2e')](function(_0x2d0c59){_0x3e5915(null,_0x2d0c59);});}function createSquareReport(_0x203703,_0x3c6ea8){rpc['createSquareReport'](_0x203703)[_0xa950('0x2d')](function(_0x4d3a2d){if(_0x3c6ea8){_0x3c6ea8(null,_0x4d3a2d);}})['catch'](function(_0x26ee82){if(_0x3c6ea8){_0x3c6ea8(null,_0x26ee82);}});}function setChannelVariables(_0x5db455){for(var _0x118d96 in _0x5db455){if(_0x5db455[_0xa950('0x21')](_0x118d96)){if(_0x118d96[_0xa950('0x2f')](_0xa950('0x30'))===0x0){_0x5db455[_0x118d96['substring'](0x4)]=_0x5db455[_0x118d96];delete _0x5db455[_0x118d96];}}}}function getLogFromObject(_0x107e08){return util['inspect'](_0x107e08,{'showHidden':![],'depth':null});}function agiHandler(_0x45846c){var _0x2306f8,_0x5676ad,_0x4c3e3c,_0x44c1a9,_0x1ccbed;logger[_0xa950('0x31')](_0xa950('0x32'),JSON[_0xa950('0x33')](_0x45846c,null,0x2));logger[_0xa950('0xf')](util['format']('[START]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',_0x45846c[_0xa950('0x16')],_0x45846c['calleridname'],_0x45846c[_0xa950('0x1b')],_0x45846c[_0xa950('0x18')],_0x45846c[_0xa950('0x19')]));_0x45846c['on'](_0xa950('0xc'),errorHandler);_0x45846c['on'](_0xa950('0x34'),closeHandler);_0x45846c['on'](_0xa950('0x35'),timeoutHandler);_0x45846c[_0xa950('0x36')]=![];_0x45846c['on'](_0xa950('0x37'),hangupHandler);var _0x1ba2fd=getSquareProject[_0xa950('0x38')](null,_0x45846c['arg_1']);if(_0x1ba2fd){if(_0x1ba2fd[_0xa950('0x39')]){_0x2306f8=getRoot(_0x1ba2fd['production']);if(_0x2306f8&&_0x2306f8[_0xa950('0x3a')]){logger[_0xa950('0x31')]('root',JSON['stringify'](_0x2306f8,null,0x2));_0x45846c[_0xa950('0x3b')]=moment()[_0xa950('0x12')]('YYYY-MM-DD\x20HH:mm:ss');_0x5676ad=initGraph(_[_0xa950('0x25')](_0x2306f8,_0xa950('0x26')),_0x2306f8['mxCell']);_0x4c3e3c=new Vertices(_0x45846c,rpc,mailRpc);logger[_0xa950('0xf')](util[_0xa950('0x12')](_0xa950('0x3c'),_0x45846c[_0xa950('0x16')],_0x45846c[_0xa950('0x17')],_0x45846c[_0xa950('0x1b')],_0x45846c[_0xa950('0x18')],_0x45846c[_0xa950('0x19')]));_0x5676ad['traverseBFS'](_0x2306f8[_0xa950('0x3a')]['id'],function(_0x1764b2){try{_0x1764b2=_0x4c3e3c['clear'](_0x1764b2);logger[_0xa950('0x31')](_0xa950('0x3d'),JSON[_0xa950('0x33')](_0x1764b2,null,0x2));if(_0x1764b2['agicommand']===_0xa950('0x3e')){logger[_0xa950('0xf')](_0xa950('0x3f'));_0x45846c['context']=_0x1764b2['context']||_0x45846c[_0xa950('0x40')];_0x45846c[_0xa950('0x41')]=_0x1764b2[_0xa950('0x41')];_0x45846c['priority']=_0x1764b2['priority']?_0x1764b2['priority']-0x1:0x1;return null;}else if(_0x1764b2&&_0x4c3e3c[_0x1764b2[_0xa950('0x20')]]){_0x1ccbed=_0x4c3e3c[_0x1764b2[_0xa950('0x20')]](_0x1764b2);logger[_0xa950('0xf')](_0xa950('0x42'),JSON[_0xa950('0x33')](_0x1ccbed,null,0x2));if(_0x1ccbed&&_0x1ccbed['code']===AGI_CODE_SUCCESS&&_0x1ccbed[_0xa950('0x43')]!==AGI_RESULT_ERROR&&!_0x45846c[_0xa950('0x36')]){logger['debug']('response',getLogFromObject(_0x1ccbed));return _[_0xa950('0x44')](_0x1ccbed[_0xa950('0x43')])?null:_0x1ccbed['result']['toString']();}else{logger['error']('response',getLogFromObject(_0x1ccbed));return null;}}else{logger[_0xa950('0xc')](_0xa950('0x45'));return null;}}catch(_0x5ae175){logger['error'](_0x5ae175[_0xa950('0xd')]);return null;}});if(_0x2306f8['finally']){logger[_0xa950('0xf')](util[_0xa950('0x12')](_0xa950('0x46'),_0x45846c[_0xa950('0x16')],_0x45846c[_0xa950('0x17')],_0x45846c[_0xa950('0x1b')],_0x45846c[_0xa950('0x18')],_0x45846c[_0xa950('0x19')]));_0x5676ad['traverseBFS'](_0x2306f8[_0xa950('0x47')]['id'],function(_0x4ede77){try{_0x4ede77=_0x4c3e3c[_0xa950('0x48')](_0x4ede77);logger[_0xa950('0x31')](_0xa950('0x3d'),JSON[_0xa950('0x33')](_0x4ede77,null,0x2));if(_0x4ede77&&_0x4c3e3c[_0x4ede77[_0xa950('0x20')]]){_0x1ccbed=_0x4c3e3c[_0x4ede77[_0xa950('0x20')]](_0x4ede77);if(_0x1ccbed){logger[_0xa950('0x31')](_0xa950('0x42'),getLogFromObject(_0x1ccbed));return _[_0xa950('0x44')](_0x1ccbed['result'])?null:_0x1ccbed[_0xa950('0x43')][_0xa950('0x49')]();}else{logger[_0xa950('0xc')](_0xa950('0x42'),getLogFromObject(_0x1ccbed));return null;}}else{logger[_0xa950('0xc')]('agicommand\x20not\x20found');return null;}}catch(_0x237152){logger[_0xa950('0xc')](_0x237152[_0xa950('0xd')]);return null;}});}logger[_0xa950('0xf')](util[_0xa950('0x12')](_0xa950('0x4a'),_0x45846c[_0xa950('0x16')],_0x45846c['calleridname'],_0x45846c[_0xa950('0x1b')],_0x45846c[_0xa950('0x18')],_0x45846c[_0xa950('0x19')]));logger['info'](util['format'](_0xa950('0x4b'),_0x45846c[_0xa950('0x40')],_0x45846c[_0xa950('0x41')],parseInt(_0x45846c[_0xa950('0x4c')],0xa)+0x1));_0x45846c[_0xa950('0x4d')](_0x45846c[_0xa950('0x40')],_0x45846c[_0xa950('0x41')],parseInt(_0x45846c[_0xa950('0x4c')],0xa)+0x1);_0x45846c['close']();}}else{logger[_0xa950('0xc')](util[_0xa950('0x12')](_0xa950('0x4e'),_0x45846c[_0xa950('0x16')]));_0x45846c[_0xa950('0x4d')](_0x45846c['context'],_0x45846c[_0xa950('0x41')],parseInt(_0x45846c['priority'],0xa)+0x1);_0x45846c[_0xa950('0x34')]();}}else{logger[_0xa950('0xc')](util[_0xa950('0x12')](_0xa950('0x4f'),_0x45846c[_0xa950('0x16')]));_0x45846c[_0xa950('0x4d')](_0x45846c['context'],_0x45846c['extension'],parseInt(_0x45846c['priority'],0xa)+0x1);_0x45846c[_0xa950('0x34')]();}}function main(){var _0x18dcd2;return utilLicense[_0xa950('0x50')]()[_0xa950('0x2d')](function(_0x447459){if(_0x447459){if(_0x447459[_0xa950('0x51')]){if(_0x447459[_0xa950('0x52')]&&_0x447459[_0xa950('0x52')]>0x0){logger['info'](util[_0xa950('0x12')]('[LICENSE]\x20CHANNELS:\x20%s',_0x447459[_0xa950('0x52')]));_0x18dcd2=_0x447459['callysquare'];}else{logger[_0xa950('0xf')]('[LICENSE]\x20CHANNELS:\x20UNLIMITED');}}else{logger[_0xa950('0xf')]('[LICENSE]\x20VOICE\x20IS\x20DISABLED');_0x18dcd2=-0x1;}}})[_0xa950('0x2e')](function(_0x3e536e){logger[_0xa950('0xc')](_0xa950('0x53'),_0x3e536e[_0xa950('0xd')]);})['finally'](function(){var _0x34db1c=require(_0xa950('0x54'))(config[_0xa950('0x6')][_0xa950('0x55')],config[_0xa950('0x6')]['ip'],_0x18dcd2);_0x34db1c['on']('error',errorHandler);_0x34db1c['on'](_0xa950('0x56'),listeningHandler);_0x34db1c['on'](_0xa950('0x57'),connectionHandler);_0x34db1c['on'](_0xa950('0x34'),closeHandler);});}main();