88310470af0e4399533cf6cb80e4f9e9d957274e
[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 _0xfa92=['timeout','evtHangup','hangup','find','handleNotFound','production','handleUnpublished','initialize','getLicense','voice','callysquare','[LICENSE]\x20VOICE\x20IS\x20DISABLED','getAll','[LICENSE]','finally','./server','connection','../../config/environment','agi','./rpc','./project','./events','EventEmitter','defaults','127.0.0.1','projectUpdate','error','stack','deleted','splice','log','Cally\x20Square\x20server\x20listening\x20on\x20port\x20%d!','info','format','[LISTENING]\x20Cally\x20Square\x20listening\x20%s:%d','port','[%s]\x20%s','name','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','then','catch','hasOwnProperty','indexOf','agi_','substring','debug','stringify','[START]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','close'];(function(_0x2851d5,_0x375267){var _0x341757=function(_0xd0f49d){while(--_0xd0f49d){_0x2851d5['push'](_0x2851d5['shift']());}};_0x341757(++_0x375267);}(_0xfa92,0xb9));var _0x2fa9=function(_0xed4b05,_0x8dc6de){_0xed4b05=_0xed4b05-0x0;var _0x5dfa3e=_0xfa92[_0xed4b05];return _0x5dfa3e;};'use strict';var _=require('lodash');var syncho=require('syncho');var util=require('util');var config=require(_0x2fa9('0x0'));var logger=require('../../config/logger')(_0x2fa9('0x1'));var utilLicense=require('../../config/license/util');var rpc=require(_0x2fa9('0x2'));var Project=require(_0x2fa9('0x3'));var AGIEmitter=require(_0x2fa9('0x4'))[_0x2fa9('0x5')];var projects=[];config[_0x2fa9('0x1')]=_[_0x2fa9('0x6')](config['agi'],{'ip':_0x2fa9('0x7'),'port':0x11dd});AGIEmitter['on'](_0x2fa9('0x8'),projectUpdateHandler);function connectionHandler(_0x1710fd){process['nextTick'](function(){syncho(function(){try{setChannelVariables(_0x1710fd);agiHandler(_0x1710fd);}catch(_0x1c474f){logger[_0x2fa9('0x9')](_0x1c474f[_0x2fa9('0xa')]);}});});}function projectUpdateHandler(_0x51ffae){try{var _0x615c6=_['findIndex'](projects,['id',_0x51ffae['id']]);if(_0x615c6>-0x1){if(_0x51ffae[_0x2fa9('0xb')]===!![]){projects[_0x2fa9('0xc')](_0x615c6,0x1);}else{projects[_0x615c6]=_0x51ffae;}}else{projects['push'](_0x51ffae);}}catch(_0x5dd8db){logger[_0x2fa9('0x9')](_0x5dd8db[_0x2fa9('0xa')]);}}function listeningHandler(){console[_0x2fa9('0xd')](_0x2fa9('0xe'),config[_0x2fa9('0x1')]['port']);logger[_0x2fa9('0xf')](util[_0x2fa9('0x10')](_0x2fa9('0x11'),config[_0x2fa9('0x1')]['ip'],config[_0x2fa9('0x1')][_0x2fa9('0x12')]));}function errorHandler(_0x154e87){if(_0x154e87&&_0x154e87['name']&&_0x154e87['message']){logger['error'](util['format'](_0x2fa9('0x13'),_0x154e87[_0x2fa9('0x14')],_0x154e87[_0x2fa9('0x15')]));}else{logger[_0x2fa9('0x9')](util[_0x2fa9('0x10')](_0x2fa9('0x16'),_0x154e87[_0x2fa9('0xa')]));}}function closeHandler(){logger['info'](util[_0x2fa9('0x10')](_0x2fa9('0x17'),this[_0x2fa9('0x18')],this[_0x2fa9('0x19')],this[_0x2fa9('0x1a')],this[_0x2fa9('0x1b')],this['uniqueid']));try{createSquareReport(this);}catch(_0x366b01){logger[_0x2fa9('0x9')](_0x366b01[_0x2fa9('0xa')]);}}function timeoutHandler(){logger[_0x2fa9('0xf')](util[_0x2fa9('0x10')]('[TIMEOUT]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',this[_0x2fa9('0x18')],this['calleridname'],this['callerid'],this[_0x2fa9('0x1b')],this['uniqueid']));}function hangupHandler(){logger['info'](util['format']('[HANGUP]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',this[_0x2fa9('0x18')],this[_0x2fa9('0x19')],this[_0x2fa9('0x1a')],this['dnid'],this[_0x2fa9('0x1c')]));this['evtHangup']=!![];}function createSquareReport(_0x31b3c9,_0x204631){rpc['createSquareReport'](_0x31b3c9)[_0x2fa9('0x1d')](function(_0x5b4a02){if(_0x204631){_0x204631(null,_0x5b4a02);}})[_0x2fa9('0x1e')](function(_0x27dff0){if(_0x204631){_0x204631(null,_0x27dff0);}});}function setChannelVariables(_0x469ccb){for(var _0x296838 in _0x469ccb){if(_0x469ccb[_0x2fa9('0x1f')](_0x296838)){if(_0x296838[_0x2fa9('0x20')](_0x2fa9('0x21'))===0x0){_0x469ccb[_0x296838[_0x2fa9('0x22')](0x4)]=_0x469ccb[_0x296838];delete _0x469ccb[_0x296838];}}}}function agiHandler(_0x38c857){logger[_0x2fa9('0x23')]('variables',JSON[_0x2fa9('0x24')](_0x38c857,null,0x2));logger[_0x2fa9('0xf')](util[_0x2fa9('0x10')](_0x2fa9('0x25'),_0x38c857[_0x2fa9('0x18')],_0x38c857[_0x2fa9('0x19')],_0x38c857[_0x2fa9('0x1a')],_0x38c857['dnid'],_0x38c857[_0x2fa9('0x1c')]));_0x38c857['on'](_0x2fa9('0x9'),errorHandler);_0x38c857['on'](_0x2fa9('0x26'),closeHandler);_0x38c857['on'](_0x2fa9('0x27'),timeoutHandler);_0x38c857[_0x2fa9('0x28')]=![];_0x38c857['on'](_0x2fa9('0x29'),hangupHandler);var _0x1440dd=_[_0x2fa9('0x2a')](projects,[_0x2fa9('0x14'),_0x38c857['arg_1']]);if(!_0x1440dd)return Project[_0x2fa9('0x2b')](_0x38c857);if(!_0x1440dd[_0x2fa9('0x2c')])return Project[_0x2fa9('0x2d')](_0x38c857);return Project[_0x2fa9('0x2e')](_0x1440dd,_0x38c857);}function main(){var _0x26b214;return utilLicense[_0x2fa9('0x2f')]()[_0x2fa9('0x1d')](function(_0x21abfc){if(!_0x21abfc)return;if(_0x21abfc[_0x2fa9('0x30')]){if(_0x21abfc[_0x2fa9('0x31')]&&_0x21abfc[_0x2fa9('0x31')]>0x0){logger[_0x2fa9('0xf')](util['format']('[LICENSE]\x20CHANNELS:\x20%s',_0x21abfc[_0x2fa9('0x31')]));_0x26b214=_0x21abfc[_0x2fa9('0x31')];}else{logger[_0x2fa9('0xf')]('[LICENSE]\x20CHANNELS:\x20UNLIMITED');}}else{logger['info'](_0x2fa9('0x32'));_0x26b214=-0x1;}})['then'](function(){return Project[_0x2fa9('0x33')]();})['then'](function(_0x4335ce){projects=_0x4335ce;return;})[_0x2fa9('0x1e')](function(_0x20cfcd){logger[_0x2fa9('0x9')](_0x2fa9('0x34'),_0x20cfcd[_0x2fa9('0xa')]);})[_0x2fa9('0x35')](function(){var _0x2b3ae8=require(_0x2fa9('0x36'))(config['agi'][_0x2fa9('0x12')],config[_0x2fa9('0x1')]['ip'],_0x26b214);_0x2b3ae8['on']('error',errorHandler);_0x2b3ae8['on']('listening',listeningHandler);_0x2b3ae8['on'](_0x2fa9('0x37'),connectionHandler);_0x2b3ae8['on'](_0x2fa9('0x26'),closeHandler);});}main();