17174451b957488fcbe9130c12811bb9d1b118e4
[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 _0x7f85=['[START]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','close','timeout','hangup','find','handleNotFound','production','handleUnpublished','initialize','getLicense','voice','callysquare','[LICENSE]\x20CHANNELS:\x20UNLIMITED','then','[LICENSE]','finally','listening','lodash','syncho','util','../../config/environment','../../config/logger','agi','./rpc','./project','defaults','127.0.0.1','projectUpdate','nextTick','error','findIndex','port','format','[LISTENING]\x20Cally\x20Square\x20listening\x20%s:%d','name','message','[%s]\x20%s','[ERROR]\x20%s','info','[CLOSE]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','dnid','uniqueid','calleridname','callerid','arg_1','evtHangup','catch','hasOwnProperty','indexOf','agi_','substring','variables','stringify'];(function(_0x67f95e,_0x3ca797){var _0x538f5a=function(_0xc919e9){while(--_0xc919e9){_0x67f95e['push'](_0x67f95e['shift']());}};_0x538f5a(++_0x3ca797);}(_0x7f85,0xb0));var _0x57f8=function(_0x31a16a,_0x1d60e3){_0x31a16a=_0x31a16a-0x0;var _0x21c892=_0x7f85[_0x31a16a];return _0x21c892;};'use strict';var _=require(_0x57f8('0x0'));var syncho=require(_0x57f8('0x1'));var util=require(_0x57f8('0x2'));var config=require(_0x57f8('0x3'));var logger=require(_0x57f8('0x4'))(_0x57f8('0x5'));var utilLicense=require('../../config/license/util');var rpc=require(_0x57f8('0x6'));var Project=require(_0x57f8('0x7'));var AGIEmitter=require('./events')['EventEmitter'];var projects=[];config['agi']=_[_0x57f8('0x8')](config[_0x57f8('0x5')],{'ip':_0x57f8('0x9'),'port':0x11dd});AGIEmitter['on'](_0x57f8('0xa'),projectUpdateHandler);function connectionHandler(_0x597fd0){process[_0x57f8('0xb')](function(){syncho(function(){try{setChannelVariables(_0x597fd0);agiHandler(_0x597fd0);}catch(_0x386984){logger[_0x57f8('0xc')](_0x386984['stack']);}});});}function projectUpdateHandler(_0x2f8f37){try{var _0x57cacf=_[_0x57f8('0xd')](projects,['id',_0x2f8f37['id']]);if(_0x57cacf>-0x1){if(_0x2f8f37['deleted']===!![]){projects['splice'](_0x57cacf,0x1);}else{projects[_0x57cacf]=_0x2f8f37;}}else{projects['push'](_0x2f8f37);}}catch(_0x59dc4e){logger['error'](_0x59dc4e['stack']);}}function listeningHandler(){console['log']('Cally\x20Square\x20server\x20listening\x20on\x20port\x20%d!',config[_0x57f8('0x5')][_0x57f8('0xe')]);logger['info'](util[_0x57f8('0xf')](_0x57f8('0x10'),config['agi']['ip'],config[_0x57f8('0x5')][_0x57f8('0xe')]));}function errorHandler(_0x3c36ce){if(_0x3c36ce&&_0x3c36ce[_0x57f8('0x11')]&&_0x3c36ce[_0x57f8('0x12')]){logger[_0x57f8('0xc')](util[_0x57f8('0xf')](_0x57f8('0x13'),_0x3c36ce[_0x57f8('0x11')],_0x3c36ce['message']));}else{logger[_0x57f8('0xc')](util[_0x57f8('0xf')](_0x57f8('0x14'),_0x3c36ce['stack']));}}function closeHandler(){logger[_0x57f8('0x15')](util[_0x57f8('0xf')](_0x57f8('0x16'),this['arg_1'],this['calleridname'],this['callerid'],this[_0x57f8('0x17')],this[_0x57f8('0x18')]));try{createSquareReport(this);}catch(_0x53bdc0){logger['error'](_0x53bdc0['stack']);}}function timeoutHandler(){logger[_0x57f8('0x15')](util['format']('[TIMEOUT]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',this['arg_1'],this[_0x57f8('0x19')],this[_0x57f8('0x1a')],this[_0x57f8('0x17')],this[_0x57f8('0x18')]));}function hangupHandler(){logger[_0x57f8('0x15')](util['format']('[HANGUP]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',this[_0x57f8('0x1b')],this[_0x57f8('0x19')],this[_0x57f8('0x1a')],this[_0x57f8('0x17')],this[_0x57f8('0x18')]));this[_0x57f8('0x1c')]=!![];}function createSquareReport(_0x1c478f,_0x38395a){rpc['createSquareReport'](_0x1c478f)['then'](function(_0x18369b){if(_0x38395a){_0x38395a(null,_0x18369b);}})[_0x57f8('0x1d')](function(_0x26d4ad){if(_0x38395a){_0x38395a(null,_0x26d4ad);}});}function setChannelVariables(_0x4738bc){for(var _0x3889db in _0x4738bc){if(_0x4738bc[_0x57f8('0x1e')](_0x3889db)){if(_0x3889db[_0x57f8('0x1f')](_0x57f8('0x20'))===0x0){_0x4738bc[_0x3889db[_0x57f8('0x21')](0x4)]=_0x4738bc[_0x3889db];delete _0x4738bc[_0x3889db];}}}}function agiHandler(_0x2ec779){logger['debug'](_0x57f8('0x22'),JSON[_0x57f8('0x23')](_0x2ec779,null,0x2));logger['info'](util[_0x57f8('0xf')](_0x57f8('0x24'),_0x2ec779['arg_1'],_0x2ec779[_0x57f8('0x19')],_0x2ec779[_0x57f8('0x1a')],_0x2ec779['dnid'],_0x2ec779['uniqueid']));_0x2ec779['on']('error',errorHandler);_0x2ec779['on'](_0x57f8('0x25'),closeHandler);_0x2ec779['on'](_0x57f8('0x26'),timeoutHandler);_0x2ec779[_0x57f8('0x1c')]=![];_0x2ec779['on'](_0x57f8('0x27'),hangupHandler);var _0x539948=_[_0x57f8('0x28')](projects,[_0x57f8('0x11'),_0x2ec779[_0x57f8('0x1b')]]);if(!_0x539948)return Project[_0x57f8('0x29')](_0x2ec779);if(!_0x539948[_0x57f8('0x2a')])return Project[_0x57f8('0x2b')](_0x2ec779);return Project[_0x57f8('0x2c')](_0x539948,_0x2ec779);}function main(){var _0x557cc7;return utilLicense[_0x57f8('0x2d')]()['then'](function(_0x128168){if(!_0x128168)return;if(_0x128168[_0x57f8('0x2e')]){if(_0x128168[_0x57f8('0x2f')]&&_0x128168['callysquare']>0x0){logger['info'](util[_0x57f8('0xf')]('[LICENSE]\x20CHANNELS:\x20%s',_0x128168[_0x57f8('0x2f')]));_0x557cc7=_0x128168['callysquare'];}else{logger[_0x57f8('0x15')](_0x57f8('0x30'));}}else{logger[_0x57f8('0x15')]('[LICENSE]\x20VOICE\x20IS\x20DISABLED');_0x557cc7=-0x1;}})[_0x57f8('0x31')](function(){return Project['getAll']();})['then'](function(_0x4c1626){projects=_0x4c1626;return;})[_0x57f8('0x1d')](function(_0x26e506){logger[_0x57f8('0xc')](_0x57f8('0x32'),_0x26e506['stack']);})[_0x57f8('0x33')](function(){var _0x1d5798=require('./server')(config[_0x57f8('0x5')]['port'],config[_0x57f8('0x5')]['ip'],_0x557cc7);_0x1d5798['on'](_0x57f8('0xc'),errorHandler);_0x1d5798['on'](_0x57f8('0x34'),listeningHandler);_0x1d5798['on']('connection',connectionHandler);_0x1d5798['on']('close',closeHandler);});}main();