57c7ff28954304edfad353fde79517d216df53e8
[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 _0x7c5d=['variables','close','hangup','handleNotFound','production','initialize','voice','callysquare','[LICENSE]\x20CHANNELS:\x20%s','[LICENSE]\x20CHANNELS:\x20UNLIMITED','[LICENSE]\x20VOICE\x20IS\x20DISABLED','[LICENSE]','finally','connection','lodash','syncho','util','../../config/environment','../../config/logger','../../config/license/util','./rpc','./project','EventEmitter','agi','defaults','127.0.0.1','error','stack','findIndex','deleted','splice','push','log','port','info','format','[LISTENING]\x20Cally\x20Square\x20listening\x20%s:%d','message','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','uniqueid','[TIMEOUT]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s','callerid','dnid','evtHangup','createSquareReport','then','indexOf','agi_','substring','debug'];(function(_0x3e5086,_0x48f4a2){var _0xfeb102=function(_0x5a945f){while(--_0x5a945f){_0x3e5086['push'](_0x3e5086['shift']());}};_0xfeb102(++_0x48f4a2);}(_0x7c5d,0xe6));var _0xd7c5=function(_0x2566c6,_0x5ef6e2){_0x2566c6=_0x2566c6-0x0;var _0x23dac4=_0x7c5d[_0x2566c6];return _0x23dac4;};'use strict';var _=require(_0xd7c5('0x0'));var syncho=require(_0xd7c5('0x1'));var util=require(_0xd7c5('0x2'));var config=require(_0xd7c5('0x3'));var logger=require(_0xd7c5('0x4'))('agi');var utilLicense=require(_0xd7c5('0x5'));var rpc=require(_0xd7c5('0x6'));var Project=require(_0xd7c5('0x7'));var AGIEmitter=require('./events')[_0xd7c5('0x8')];var projects=[];config[_0xd7c5('0x9')]=_[_0xd7c5('0xa')](config[_0xd7c5('0x9')],{'ip':_0xd7c5('0xb'),'port':0x11dd});AGIEmitter['on']('projectUpdate',projectUpdateHandler);function connectionHandler(_0x40c66b){process['nextTick'](function(){syncho(function(){try{setChannelVariables(_0x40c66b);agiHandler(_0x40c66b);}catch(_0x2ca4f7){logger[_0xd7c5('0xc')](_0x2ca4f7[_0xd7c5('0xd')]);}});});}function projectUpdateHandler(_0x2e6c53){try{var _0x241cb2=_[_0xd7c5('0xe')](projects,['id',_0x2e6c53['id']]);if(_0x241cb2>-0x1){if(_0x2e6c53[_0xd7c5('0xf')]===!![]){projects[_0xd7c5('0x10')](_0x241cb2,0x1);}else{projects[_0x241cb2]=_0x2e6c53;}}else{projects[_0xd7c5('0x11')](_0x2e6c53);}}catch(_0x49b210){logger[_0xd7c5('0xc')](_0x49b210[_0xd7c5('0xd')]);}}function listeningHandler(){var _0x59639f=config['env']==='production'||![];console[_0xd7c5('0x12')]('Cally\x20Square\x20server\x20listening\x20on\x20port\x20%d!',config[_0xd7c5('0x9')][_0xd7c5('0x13')]);logger[_0xd7c5('0x14')](util[_0xd7c5('0x15')](_0xd7c5('0x16'),_0x59639f?config[_0xd7c5('0x9')]['ip']:'host.docker.internal',config[_0xd7c5('0x9')][_0xd7c5('0x13')]));}function errorHandler(_0x212bb){if(_0x212bb&&_0x212bb['name']&&_0x212bb[_0xd7c5('0x17')]){logger[_0xd7c5('0xc')](util['format']('[%s]\x20%s',_0x212bb[_0xd7c5('0x18')],_0x212bb[_0xd7c5('0x17')]));}else{logger[_0xd7c5('0xc')](util['format'](_0xd7c5('0x19'),_0x212bb[_0xd7c5('0xd')]));}}function closeHandler(){logger[_0xd7c5('0x14')](util[_0xd7c5('0x15')](_0xd7c5('0x1a'),this[_0xd7c5('0x1b')],this[_0xd7c5('0x1c')],this['callerid'],this['dnid'],this[_0xd7c5('0x1d')]));try{createSquareReport(this);}catch(_0x535a5a){logger[_0xd7c5('0xc')](_0x535a5a[_0xd7c5('0xd')]);}}function timeoutHandler(){logger['info'](util[_0xd7c5('0x15')](_0xd7c5('0x1e'),this['arg_1'],this[_0xd7c5('0x1c')],this[_0xd7c5('0x1f')],this[_0xd7c5('0x20')],this[_0xd7c5('0x1d')]));}function hangupHandler(){logger[_0xd7c5('0x14')](util[_0xd7c5('0x15')]('[HANGUP]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',this[_0xd7c5('0x1b')],this[_0xd7c5('0x1c')],this[_0xd7c5('0x1f')],this['dnid'],this['uniqueid']));this[_0xd7c5('0x21')]=!![];}function createSquareReport(_0x32c746,_0x246eff){rpc[_0xd7c5('0x22')](_0x32c746)[_0xd7c5('0x23')](function(_0x355043){if(_0x246eff){_0x246eff(null,_0x355043);}})['catch'](function(_0x4592cf){if(_0x246eff){_0x246eff(null,_0x4592cf);}});}function setChannelVariables(_0x803fe9){for(var _0x353153 in _0x803fe9){if(_0x803fe9['hasOwnProperty'](_0x353153)){if(_0x353153[_0xd7c5('0x24')](_0xd7c5('0x25'))===0x0){_0x803fe9[_0x353153[_0xd7c5('0x26')](0x4)]=_0x803fe9[_0x353153];delete _0x803fe9[_0x353153];}}}}function agiHandler(_0x250ee5){logger[_0xd7c5('0x27')](_0xd7c5('0x28'),JSON['stringify'](_0x250ee5,null,0x2));logger[_0xd7c5('0x14')](util[_0xd7c5('0x15')]('[START]\x20%s\x20project:\x20from\x20\x22%s\x22\x20<%s>\x20to\x20extension\x20%s\x20with\x20uniqueid\x20%s',_0x250ee5['arg_1'],_0x250ee5['calleridname'],_0x250ee5['callerid'],_0x250ee5[_0xd7c5('0x20')],_0x250ee5[_0xd7c5('0x1d')]));_0x250ee5['on'](_0xd7c5('0xc'),errorHandler);_0x250ee5['on'](_0xd7c5('0x29'),closeHandler);_0x250ee5['on']('timeout',timeoutHandler);_0x250ee5[_0xd7c5('0x21')]=![];_0x250ee5['on'](_0xd7c5('0x2a'),hangupHandler);var _0x1f14f4=_['find'](projects,[_0xd7c5('0x18'),_0x250ee5[_0xd7c5('0x1b')]]);if(!_0x1f14f4)return Project[_0xd7c5('0x2b')](_0x250ee5);if(!_0x1f14f4[_0xd7c5('0x2c')])return Project['handleUnpublished'](_0x250ee5);return Project[_0xd7c5('0x2d')](_0x1f14f4,_0x250ee5);}function main(){var _0x52f41a;return utilLicense['getLicense']()[_0xd7c5('0x23')](function(_0x41a399){if(!_0x41a399)return;if(_0x41a399[_0xd7c5('0x2e')]){if(_0x41a399['callysquare']&&_0x41a399[_0xd7c5('0x2f')]>0x0){logger[_0xd7c5('0x14')](util[_0xd7c5('0x15')](_0xd7c5('0x30'),_0x41a399[_0xd7c5('0x2f')]));_0x52f41a=_0x41a399[_0xd7c5('0x2f')];}else{logger[_0xd7c5('0x14')](_0xd7c5('0x31'));}}else{logger[_0xd7c5('0x14')](_0xd7c5('0x32'));_0x52f41a=-0x1;}})[_0xd7c5('0x23')](function(){return Project['getAll']();})[_0xd7c5('0x23')](function(_0x1910c9){projects=_0x1910c9;return;})['catch'](function(_0x63fb2d){logger[_0xd7c5('0xc')](_0xd7c5('0x33'),_0x63fb2d[_0xd7c5('0xd')]);})[_0xd7c5('0x34')](function(){var _0x10a2d8=require('./server')(config[_0xd7c5('0x9')][_0xd7c5('0x13')],config[_0xd7c5('0x9')]['ip'],_0x52f41a);_0x10a2d8['on'](_0xd7c5('0xc'),errorHandler);_0x10a2d8['on']('listening',listeningHandler);_0x10a2d8['on'](_0xd7c5('0x35'),connectionHandler);_0x10a2d8['on'](_0xd7c5('0x29'),closeHandler);});}main();