6d2be6a4a0cf9a9621fdbf3b4a65ca88f883a005
[motion2.git] / server / services / ami / fax / 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 _0x19c4=['channelstate','channelstatedesc','value','operation','status','filename','remotestationid','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','\x20result\x20conversion\x20file\x20','message','create','parse','name','.pdf','application/pdf','notify','catch','\x20notify\x20','error','\x20receiveFax\x20','stringify','merge','pick','reason','update','AttachmentId','SENDING\x20FAILED\x20','then','[Fax][Message][Error][Updated]','logger','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','updateStatus','[Fax][Message][Updated]','[Fax][Message][NoUpdate1]','finally','[Fax][Message][NoUpdate2]','deleteChannelAfterTimeout','\x20hangup\x20sendFile\x20','\x20hangup\x20faxRiceive\x20','faxpages','SUCCESS','faxstatusstring','SENDING\x20FAILED','send','FaxAccountId','[Fax][execOriginate][success]','Fax\x20received','[Fax][execOriginate][failure]','Fax\x20failed','checkAfterSend','checkTrunk','Trunk','isValid','gs\x20-q\x20-dNOPAUSE\x20-dBATCH\x20-sDEVICE=tiffg4\x20-sPAPERSIZE=letter\x20-sOutputFile=/var/opt/motion2/server/files/attachments/%s.tif\x20\x20%s\x20-c\x20quit','format','%s/%s.tif','XMF-PATH','Convert\x20pdf\x20to\x20tif','Action','variablesAction','execOriginate','addVariables','isTrunkReachable','isNil','active','SIP/%s','reachable','unmonitored','[Fax][checkTrunk][Trunk\x20not\x20working\x20correctly]','\x20Trunk\x20is\x20not\x20active','Trunk\x20not\x20working\x20correctly','[Fax][checkTrunk][Empty\x20Fax]','Empty\x20Fax','[Fax][checkTrunk][Empty\x20path]','Empty\x20path','util','lodash','uuid','moment','path','tiff2pdf','../../../config/logger','fax','./util/util','./action','../rpc/faxAccount','../rpc/faxMessage','channels','faxAccounts','trunks','actions','bind','varset','syncVarSet','newstate','syncNewState','newexten','syncNewExten','faxstatus','syncFaxStatus','receivefax','syncReceiveFax','syncHangup','originateresponse','syncOriginateResponse','prototype','syncNewchannel','context','from-voip-provider','hasOwnProperty','exten','localstationid','uniqueid','channel','info','\x20syncNewchannel\x20','variable'];(function(_0x27799b,_0xc3152b){var _0x522e82=function(_0x5cc404){while(--_0x5cc404){_0x27799b['push'](_0x27799b['shift']());}};_0x522e82(++_0xc3152b);}(_0x19c4,0x144));var _0x419c=function(_0x1fd675,_0x165dfe){_0x1fd675=_0x1fd675-0x0;var _0x16b554=_0x19c4[_0x1fd675];return _0x16b554;};'use strict';var util=require(_0x419c('0x0'));var _=require(_0x419c('0x1'));var BPromise=require('bluebird');var uuid=require(_0x419c('0x2'));var moment=require(_0x419c('0x3'));var path=require(_0x419c('0x4'));var tiff2pdf=require(_0x419c('0x5'));var gs=require('./util/gs');var logger=require(_0x419c('0x6'))(_0x419c('0x7'));var ami=require('../ami');var rpcDb=require('./rpcDb');var Channel=require('./channel');var ut=require(_0x419c('0x8'));var Action=require(_0x419c('0x9'));var faxAccount=require(_0x419c('0xa'));var attachment=require('../rpc/attachment');var faxMessage=require(_0x419c('0xb'));function Fax(_0x5ed6a9){this[_0x419c('0xc')]={};this[_0x419c('0xd')]=_0x5ed6a9['faxAccounts'];this['trunks']=_0x5ed6a9[_0x419c('0xe')];this[_0x419c('0xf')]={};ami['on']('newchannel',this['syncNewchannel'][_0x419c('0x10')](this));ami['on'](_0x419c('0x11'),this[_0x419c('0x12')]['bind'](this));ami['on'](_0x419c('0x13'),this[_0x419c('0x14')][_0x419c('0x10')](this));ami['on'](_0x419c('0x15'),this[_0x419c('0x16')][_0x419c('0x10')](this));ami['on'](_0x419c('0x17'),this[_0x419c('0x18')]['bind'](this));ami['on'](_0x419c('0x19'),this[_0x419c('0x1a')][_0x419c('0x10')](this));ami['on']('hangup',this[_0x419c('0x1b')][_0x419c('0x10')](this));ami['on'](_0x419c('0x1c'),this[_0x419c('0x1d')][_0x419c('0x10')](this));}Fax[_0x419c('0x1e')][_0x419c('0x1f')]=function(_0x33c8a7){if(_0x33c8a7[_0x419c('0x20')]===_0x419c('0x21')){for(var _0x49dac8 in this['faxAccounts']){if(this[_0x419c('0xd')][_0x419c('0x22')](_0x49dac8)){if(_0x33c8a7[_0x419c('0x23')]===this[_0x419c('0xd')][_0x49dac8][_0x419c('0x24')]){this[_0x419c('0xc')][_0x33c8a7[_0x419c('0x25')]]={};this['channels'][_0x33c8a7['uniqueid']][_0x419c('0x26')]=_0x33c8a7[_0x419c('0x26')];this[_0x419c('0xc')][_0x33c8a7[_0x419c('0x25')]]['id']=this[_0x419c('0xd')][_0x49dac8]['id'];this[_0x419c('0xc')][_0x33c8a7[_0x419c('0x25')]]['name']=this[_0x419c('0xd')][_0x49dac8]['name'];logger[_0x419c('0x27')](_0x33c8a7['uniqueid'],'\x20syncNewchannel\x20');}}}}if(this[_0x419c('0xf')][_0x33c8a7[_0x419c('0x25')]]){this[_0x419c('0xc')][_0x33c8a7[_0x419c('0x25')]]={};this[_0x419c('0xc')][_0x33c8a7[_0x419c('0x25')]][_0x419c('0x26')]=_0x33c8a7['channel'];logger[_0x419c('0x27')](_0x33c8a7[_0x419c('0x25')],_0x419c('0x28'));}};Fax[_0x419c('0x1e')]['syncVarSet']=function(_0x3b464d){if(this[_0x419c('0xc')][_0x3b464d[_0x419c('0x25')]]){for(var _0x7ac591 in _0x3b464d[_0x419c('0x29')]){if(_0x3b464d[_0x419c('0x29')]['hasOwnProperty'](_0x7ac591)){this[_0x419c('0xc')][_0x3b464d[_0x419c('0x25')]][_0x7ac591]=_0x3b464d['value'];}}}};Fax['prototype'][_0x419c('0x14')]=function(_0x5cd6b4){if(this[_0x419c('0xc')][_0x5cd6b4[_0x419c('0x25')]]){this[_0x419c('0xc')][_0x5cd6b4[_0x419c('0x25')]][_0x419c('0x2a')]=_0x5cd6b4[_0x419c('0x2a')];this[_0x419c('0xc')][_0x5cd6b4['uniqueid']][_0x419c('0x2b')]=_0x5cd6b4['channelstatedesc'];}};Fax[_0x419c('0x1e')]['syncNewExten']=function(_0x5e0985){if(this['channels'][_0x5e0985['uniqueid']]){for(var _0x752573 in _0x5e0985['variable']){if(_0x5e0985[_0x419c('0x29')][_0x419c('0x22')](_0x752573)){this[_0x419c('0xc')][_0x5e0985[_0x419c('0x25')]][_0x752573]=_0x5e0985[_0x419c('0x2c')];}}}};Fax[_0x419c('0x1e')][_0x419c('0x18')]=function(_0x52a111){if(this['channels'][_0x52a111['uniqueid']]){this[_0x419c('0xc')][_0x52a111[_0x419c('0x25')]][_0x419c('0x2d')]=_0x52a111[_0x419c('0x2d')];this[_0x419c('0xc')][_0x52a111[_0x419c('0x25')]][_0x419c('0x2e')]=_0x52a111[_0x419c('0x2e')];this[_0x419c('0xc')][_0x52a111[_0x419c('0x25')]]['localstationid']=_0x52a111[_0x419c('0x24')];this[_0x419c('0xc')][_0x52a111['uniqueid']][_0x419c('0x2f')]=_0x52a111[_0x419c('0x2f')];logger[_0x419c('0x27')](_0x52a111[_0x419c('0x25')],'\x20syncFaxStatus\x20');}};Fax['prototype']['syncReceiveFax']=function(_0x244e2b){var _0xe98320=this;if(this[_0x419c('0xc')][_0x244e2b['uniqueid']]){var _0x40ea80=this['channels'][_0x244e2b[_0x419c('0x25')]]['id'];this[_0x419c('0xc')][_0x244e2b[_0x419c('0x25')]][_0x419c('0x30')]=_0x244e2b[_0x419c('0x30')];this[_0x419c('0xc')][_0x244e2b[_0x419c('0x25')]][_0x419c('0x31')]=_0x244e2b[_0x419c('0x31')];this[_0x419c('0xc')][_0x244e2b[_0x419c('0x25')]]['localstationid']=_0x244e2b[_0x419c('0x24')];this['channels'][_0x244e2b[_0x419c('0x25')]]['resolution']=_0x244e2b[_0x419c('0x32')];this[_0x419c('0xc')][_0x244e2b[_0x419c('0x25')]][_0x419c('0x33')]=_0x244e2b[_0x419c('0x33')];this[_0x419c('0xc')][_0x244e2b[_0x419c('0x25')]][_0x419c('0x2f')]=_0x244e2b[_0x419c('0x2f')];this['channels'][_0x244e2b[_0x419c('0x25')]][_0x419c('0x34')]=_0x244e2b[_0x419c('0x34')];if(this[_0x419c('0xc')][_0x244e2b['uniqueid']][_0x419c('0x31')]>0x0){tiff2pdf(_0x244e2b[_0x419c('0x2f')],_0x419c('0x35'),function(_0x32e479){logger[_0x419c('0x27')](_0x244e2b['uniqueid'],_0x419c('0x36'),_0x32e479[_0x419c('0x37')]);attachment[_0x419c('0x38')]({'name':path[_0x419c('0x39')](_0x244e2b[_0x419c('0x2f')])['name'],'basename':path[_0x419c('0x39')](_0x244e2b['filename'])[_0x419c('0x3a')]+_0x419c('0x3b'),'type':_0x419c('0x3c')})['then'](function(_0x503ab7){faxAccount[_0x419c('0x3d')](_0x40ea80,{'from':_0x244e2b['calleridnum'],'mapKey':_0x419c('0x7'),'body':path[_0x419c('0x39')](_0x244e2b['filename'])['name']+_0x419c('0x3b'),'AttachmentId':_0x503ab7['id']})[_0x419c('0x3e')](function(_0x1b59a2){logger['error'](_0x244e2b['uniqueid'],_0x419c('0x3f'),JSON['stringify'](_0x1b59a2));});})['catch'](function(_0x326370){logger[_0x419c('0x40')](_0x244e2b['uniqueid'],_0x419c('0x41'),JSON[_0x419c('0x42')](_0x326370));});});}logger[_0x419c('0x27')](_0x244e2b[_0x419c('0x25')],'\x20receiveFax\x20');}};Fax[_0x419c('0x1e')][_0x419c('0x1d')]=function(_0x128a18){if(this[_0x419c('0xf')][_0x128a18['uniqueid']]){_[_0x419c('0x43')](this[_0x419c('0xc')][_0x128a18[_0x419c('0x25')]],_[_0x419c('0x44')](_0x128a18,['response',_0x419c('0x45')]));}};function updateStatus(_0x49b5e4){if(_0x49b5e4['AttachmentId']){faxMessage[_0x419c('0x46')](_0x49b5e4[_0x419c('0x47')],{'failMessage':_0x419c('0x48')})[_0x419c('0x49')](function(_0x59efa8){ut['logger'](_0x419c('0x4a'),_0x59efa8);})[_0x419c('0x3e')](function(_0x376fc3){ut[_0x419c('0x4b')](_0x419c('0x4c'),_0x376fc3);});}else{ut[_0x419c('0x4b')](_0x419c('0x4d'));}}Fax[_0x419c('0x1e')][_0x419c('0x4e')]=function(_0x109fb6,_0x462b9b,_0x5d74a4){var _0x595d17=this;var _0x547525={};if(_0x462b9b){_0x547525={'read':_0x462b9b};}else{_0x547525={'failMessage':_0x5d74a4};}if(this['actions'][_0x109fb6]['AttachmentId']){faxMessage[_0x419c('0x46')](this['actions'][_0x109fb6][_0x419c('0x47')],_0x547525)[_0x419c('0x49')](function(_0x38797a){ut[_0x419c('0x4b')](_0x419c('0x4f'),_0x38797a);})[_0x419c('0x3e')](function(_0x14ee04){ut['logger'](_0x419c('0x50'),_0x14ee04);})[_0x419c('0x51')](function(){delete _0x595d17[_0x419c('0xc')][_0x109fb6];delete _0x595d17[_0x419c('0xf')][_0x109fb6];});}else{ut['logger'](_0x419c('0x52'));this[_0x419c('0x53')](_0x109fb6);delete this[_0x419c('0xf')][_0x109fb6];}};Fax[_0x419c('0x1e')]['syncHangup']=function(_0x2657e8){var _0x5df71a=this;if(this[_0x419c('0xc')][_0x2657e8[_0x419c('0x25')]]){if(this[_0x419c('0xf')][_0x2657e8[_0x419c('0x25')]]){this[_0x419c('0xc')][_0x2657e8[_0x419c('0x25')]]['id']=this[_0x419c('0xf')][_0x2657e8[_0x419c('0x25')]]['id'];this[_0x419c('0xc')][_0x2657e8[_0x419c('0x25')]]['name']=this[_0x419c('0xf')][_0x2657e8[_0x419c('0x25')]][_0x419c('0x3a')];}logger['info'](_0x2657e8['uniqueid'],this[_0x419c('0xf')][_0x2657e8[_0x419c('0x25')]]?_0x419c('0x54'):_0x419c('0x55'),JSON[_0x419c('0x42')](this[_0x419c('0xc')][_0x2657e8[_0x419c('0x25')]]));if(this[_0x419c('0xf')][_0x2657e8[_0x419c('0x25')]]){if(this['channels'][_0x2657e8[_0x419c('0x25')]][_0x419c('0x56')]>0x0&&this[_0x419c('0xc')][_0x2657e8[_0x419c('0x25')]][_0x419c('0x17')]===_0x419c('0x57')){this['updateStatus'](_0x2657e8[_0x419c('0x25')],!![]);}else{this[_0x419c('0x4e')](_0x2657e8[_0x419c('0x25')],![],this[_0x419c('0xc')][_0x2657e8[_0x419c('0x25')]][_0x419c('0x58')]?'SENDING\x20FAILED:\x20'+this['channels'][_0x2657e8[_0x419c('0x25')]]['faxstatusstring']:_0x419c('0x59'));}}}};Fax[_0x419c('0x1e')][_0x419c('0x5a')]=function(_0x5835e7){var _0x494170=this;return new BPromise(function(_0x5dc4bc,_0x570a0a){_0x494170['execOriginate'](_0x5835e7,_0x494170[_0x419c('0xd')][_0x5835e7[_0x419c('0x5b')]])[_0x419c('0x49')](function(_0xcbb3c0){ut[_0x419c('0x4b')](_0x419c('0x5c'),_0xcbb3c0);_0x5dc4bc(ut['message'](_0x419c('0x5d')));})[_0x419c('0x3e')](function(_0x518d90){ut[_0x419c('0x4b')](_0x419c('0x5e'),_0x518d90);updateStatus(_0x5835e7);_0x570a0a(ut[_0x419c('0x37')](_0x419c('0x5f')));});});};Fax[_0x419c('0x1e')][_0x419c('0x60')]=function(_0x153a77){var _0x3913de=this;return new BPromise(function(_0x5f00ab,_0x172a42){var _0x5c5f17=_0x3913de[_0x419c('0x61')](_0x3913de[_0x419c('0xd')][_0x153a77[_0x419c('0x5b')]][_0x419c('0x62')],_0x153a77);if(_0x5c5f17[_0x419c('0x63')]){_0x5f00ab(ut[_0x419c('0x37')]('Trunk\x20Reachable'));}else{_0x172a42(ut[_0x419c('0x37')](_0x5c5f17[_0x419c('0x37')]));}});};function originate(_0x5e343d,_0x2da6f0,_0xfbfa35){return new BPromise(function(_0x279099,_0x362890){if(_0xfbfa35){var _0x1de1ef=util['format'](_0x419c('0x64'),path[_0x419c('0x39')](_0x5e343d[_0x419c('0x4')])[_0x419c('0x3a')],_0x5e343d[_0x419c('0x4')]);gs['execute'](_0x1de1ef)[_0x419c('0x49')](function(){_0x5e343d[_0x419c('0x4')]=util[_0x419c('0x65')](_0x419c('0x66'),path['parse'](_0x5e343d['path'])['dir'],path[_0x419c('0x39')](_0x5e343d[_0x419c('0x4')])[_0x419c('0x3a')]);_0x2da6f0[_0x419c('0x67')]=_0x5e343d[_0x419c('0x4')];ut[_0x419c('0x4b')](_0x419c('0x68'),'');ami[_0x419c('0x69')](ut['variablesAction'](_0x5e343d,_0x2da6f0))[_0x419c('0x49')](function(_0x2ffcf6){_0x279099(_0x2ffcf6);})[_0x419c('0x3e')](function(_0x5d4816){_0x362890(_0x5d4816);});})['catch'](function(_0x2f3b4d){_0x362890(_0x2f3b4d);});}else{ami[_0x419c('0x69')](ut[_0x419c('0x6a')](_0x5e343d,_0x2da6f0))['then'](function(_0x4b086b){_0x279099(_0x4b086b);})['catch'](function(_0x3c620e){_0x362890(_0x3c620e);});}});}Fax['prototype'][_0x419c('0x6b')]=function(_0x31630d,_0x58bb8c){var _0x18b5e8=this;return new BPromise(function(_0x3ca09d,_0x53286b){var _0x546b59=uuid['v4']();var _0x26a8ca=ut[_0x419c('0x6c')](_0x31630d,_0x58bb8c);_0x18b5e8[_0x419c('0xf')][_0x546b59]=new Action(_0x546b59,_0x58bb8c,_0x31630d);var _0x3442f0=_0x18b5e8['actions'][_0x546b59];if(path[_0x419c('0x39')](_0x31630d[_0x419c('0x4')])['ext']===_0x419c('0x3b')){originate(_0x3442f0,_0x26a8ca,!![])[_0x419c('0x49')](function(_0x5dcbb0){_0x3ca09d(_0x5dcbb0);})[_0x419c('0x3e')](function(_0xba1d7a){_0x53286b(_0xba1d7a);});}else{originate(_0x3442f0,_0x26a8ca,![])[_0x419c('0x49')](function(_0x3af800){_0x3ca09d(_0x3af800);})['catch'](function(_0x1dd7ec){_0x53286b(_0x1dd7ec);});}});};Fax[_0x419c('0x1e')][_0x419c('0x6d')]=function(_0x2e720d){return!_[_0x419c('0x6e')](_0x2e720d)&&!_[_0x419c('0x6e')](_0x2e720d['name'])&&!_['isNil'](_0x2e720d[_0x419c('0x6f')])&&_0x2e720d[_0x419c('0x6f')]&&!_['isNil'](this[_0x419c('0xe')][util[_0x419c('0x65')](_0x419c('0x70'),_0x2e720d[_0x419c('0x3a')])])&&!_['isNil'](this[_0x419c('0xe')][util[_0x419c('0x65')](_0x419c('0x70'),_0x2e720d[_0x419c('0x3a')])]['status'])&&(this[_0x419c('0xe')][util['format'](_0x419c('0x70'),_0x2e720d[_0x419c('0x3a')])][_0x419c('0x2e')]===_0x419c('0x71')||this['trunks'][util[_0x419c('0x65')](_0x419c('0x70'),_0x2e720d[_0x419c('0x3a')])]['status']===_0x419c('0x72'));};Fax[_0x419c('0x1e')][_0x419c('0x61')]=function(_0x5b42fe,_0x1c800d){if(!this[_0x419c('0x6d')](_0x5b42fe)){ut['logger'](_0x419c('0x73'),_[_0x419c('0x6e')](_0x5b42fe)?'':_0x5b42fe[_0x419c('0x3a')]+_0x419c('0x74'));return{'isValid':![],'message':_0x419c('0x75')};}if(_[_0x419c('0x6e')](_0x1c800d[_0x419c('0x7')])){ut[_0x419c('0x4b')](_0x419c('0x76'),JSON[_0x419c('0x42')](_0x1c800d));return{'isValid':![],'message':_0x419c('0x77')};}if(_[_0x419c('0x6e')](_0x1c800d[_0x419c('0x4')])){ut[_0x419c('0x4b')](_0x419c('0x78'),JSON[_0x419c('0x42')](_0x1c800d));return{'isValid':![],'message':_0x419c('0x79')};}return{'isValid':!![],'message':'Is\x20Valid'};};Fax[_0x419c('0x1e')][_0x419c('0x53')]=function(_0xe04c08,_0x3f7a40=0x2710){if(this[_0x419c('0xc')][_0xe04c08]){setTimeout(()=>{delete this[_0x419c('0xc')][_0xe04c08];},_0x3f7a40);}};module['exports']=Fax;