3df23ff98c1300297fccb4c7175536f2b4352148
[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 _0xe785=['syncNewchannel','context','hasOwnProperty','localstationid','channels','uniqueid','channel','name','info','\x20syncNewchannel\x20','variable','value','channelstate','channelstatedesc','operation','status','\x20syncFaxStatus\x20','syncReceiveFax','remotestationid','pagestransferred','resolution','transferrate','filename','calleridnum','/var/opt/motion2/server/files/attachments/','message','parse','application/pdf','notify','catch','error','\x20notify\x20','stringify','\x20receiveFax\x20','merge','pick','response','reason','AttachmentId','update','SENDING\x20FAILED\x20','logger','[Fax][Message][Error][Updated]','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','[Fax][Message][Updated]','[Fax][Message][NoUpdate1]','finally','[Fax][Message][NoUpdate2]','\x20hangup\x20sendFile\x20','\x20hangup\x20faxRiceive\x20','faxpages','SUCCESS','updateStatus','faxstatusstring','SENDING\x20FAILED','send','execOriginate','FaxAccountId','then','Fax\x20received','[Fax][execOriginate][failure]','checkAfterSend','checkTrunk','isValid','format','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','path','execute','%s/%s.tif','dir','Convert\x20pdf\x20to\x20tif','Action','variablesAction','addVariables','ext','.pdf','isTrunkReachable','isNil','active','SIP/%s','reachable','\x20Trunk\x20is\x20not\x20active','Trunk\x20not\x20working','[Fax][checkTrunk][Empty\x20Fax]','Empty\x20Fax','[Fax][checkTrunk][Empty\x20path]','Empty\x20path','Is\x20Valid','exports','util','lodash','uuid','moment','tiff2pdf','./util/gs','../../../config/logger','fax','./rpcDb','./channel','./util/util','./action','../rpc/faxAccount','../rpc/attachment','faxAccounts','trunks','actions','newchannel','varset','syncVarSet','bind','newstate','syncNewState','newexten','syncNewExten','faxstatus','syncFaxStatus','hangup','syncHangup','syncOriginateResponse','prototype'];(function(_0x306ddc,_0x9c4d60){var _0x11767f=function(_0x35183e){while(--_0x35183e){_0x306ddc['push'](_0x306ddc['shift']());}};_0x11767f(++_0x9c4d60);}(_0xe785,0x14c));var _0x5e78=function(_0x41682b,_0x33b6e4){_0x41682b=_0x41682b-0x0;var _0x137693=_0xe785[_0x41682b];return _0x137693;};'use strict';var util=require(_0x5e78('0x0'));var _=require(_0x5e78('0x1'));var BPromise=require('bluebird');var uuid=require(_0x5e78('0x2'));var moment=require(_0x5e78('0x3'));var path=require('path');var tiff2pdf=require(_0x5e78('0x4'));var gs=require(_0x5e78('0x5'));var logger=require(_0x5e78('0x6'))(_0x5e78('0x7'));var ami=require('../ami');var rpcDb=require(_0x5e78('0x8'));var Channel=require(_0x5e78('0x9'));var ut=require(_0x5e78('0xa'));var Action=require(_0x5e78('0xb'));var faxAccount=require(_0x5e78('0xc'));var attachment=require(_0x5e78('0xd'));var faxMessage=require('../rpc/faxMessage');function Fax(_0x2dd5db){this['channels']={};this['faxAccounts']=_0x2dd5db[_0x5e78('0xe')];this[_0x5e78('0xf')]=_0x2dd5db[_0x5e78('0xf')];this[_0x5e78('0x10')]={};ami['on'](_0x5e78('0x11'),this['syncNewchannel']['bind'](this));ami['on'](_0x5e78('0x12'),this[_0x5e78('0x13')][_0x5e78('0x14')](this));ami['on'](_0x5e78('0x15'),this[_0x5e78('0x16')][_0x5e78('0x14')](this));ami['on'](_0x5e78('0x17'),this[_0x5e78('0x18')][_0x5e78('0x14')](this));ami['on'](_0x5e78('0x19'),this[_0x5e78('0x1a')][_0x5e78('0x14')](this));ami['on']('receivefax',this['syncReceiveFax']['bind'](this));ami['on'](_0x5e78('0x1b'),this[_0x5e78('0x1c')][_0x5e78('0x14')](this));ami['on']('originateresponse',this[_0x5e78('0x1d')][_0x5e78('0x14')](this));}Fax[_0x5e78('0x1e')][_0x5e78('0x1f')]=function(_0x5b7ff4){if(_0x5b7ff4[_0x5e78('0x20')]==='from-voip-provider'){for(var _0x44186c in this['faxAccounts']){if(this[_0x5e78('0xe')][_0x5e78('0x21')](_0x44186c)){if(_0x5b7ff4['exten']===this[_0x5e78('0xe')][_0x44186c][_0x5e78('0x22')]){this[_0x5e78('0x23')][_0x5b7ff4[_0x5e78('0x24')]]={};this['channels'][_0x5b7ff4[_0x5e78('0x24')]][_0x5e78('0x25')]=_0x5b7ff4[_0x5e78('0x25')];this[_0x5e78('0x23')][_0x5b7ff4[_0x5e78('0x24')]]['id']=this[_0x5e78('0xe')][_0x44186c]['id'];this[_0x5e78('0x23')][_0x5b7ff4[_0x5e78('0x24')]][_0x5e78('0x26')]=this[_0x5e78('0xe')][_0x44186c][_0x5e78('0x26')];logger[_0x5e78('0x27')](_0x5b7ff4['uniqueid'],_0x5e78('0x28'));}}}}if(this[_0x5e78('0x10')][_0x5b7ff4[_0x5e78('0x24')]]){this[_0x5e78('0x23')][_0x5b7ff4[_0x5e78('0x24')]]={};this[_0x5e78('0x23')][_0x5b7ff4[_0x5e78('0x24')]][_0x5e78('0x25')]=_0x5b7ff4['channel'];logger['info'](_0x5b7ff4[_0x5e78('0x24')],_0x5e78('0x28'));}};Fax['prototype']['syncVarSet']=function(_0x6f8098){if(this['channels'][_0x6f8098['uniqueid']]){for(var _0x5c3e59 in _0x6f8098[_0x5e78('0x29')]){if(_0x6f8098[_0x5e78('0x29')][_0x5e78('0x21')](_0x5c3e59)){this[_0x5e78('0x23')][_0x6f8098[_0x5e78('0x24')]][_0x5c3e59]=_0x6f8098[_0x5e78('0x2a')];}}}};Fax['prototype'][_0x5e78('0x16')]=function(_0x2d1ce9){if(this[_0x5e78('0x23')][_0x2d1ce9[_0x5e78('0x24')]]){this['channels'][_0x2d1ce9[_0x5e78('0x24')]][_0x5e78('0x2b')]=_0x2d1ce9['channelstate'];this[_0x5e78('0x23')][_0x2d1ce9['uniqueid']][_0x5e78('0x2c')]=_0x2d1ce9['channelstatedesc'];}};Fax['prototype'][_0x5e78('0x18')]=function(_0x5863e7){if(this[_0x5e78('0x23')][_0x5863e7[_0x5e78('0x24')]]){for(var _0x26a15d in _0x5863e7[_0x5e78('0x29')]){if(_0x5863e7[_0x5e78('0x29')][_0x5e78('0x21')](_0x26a15d)){this[_0x5e78('0x23')][_0x5863e7[_0x5e78('0x24')]][_0x26a15d]=_0x5863e7['value'];}}}};Fax['prototype'][_0x5e78('0x1a')]=function(_0x56aff8){if(this['channels'][_0x56aff8[_0x5e78('0x24')]]){this['channels'][_0x56aff8[_0x5e78('0x24')]][_0x5e78('0x2d')]=_0x56aff8[_0x5e78('0x2d')];this['channels'][_0x56aff8['uniqueid']][_0x5e78('0x2e')]=_0x56aff8[_0x5e78('0x2e')];this[_0x5e78('0x23')][_0x56aff8[_0x5e78('0x24')]]['localstationid']=_0x56aff8[_0x5e78('0x22')];this[_0x5e78('0x23')][_0x56aff8['uniqueid']]['filename']=_0x56aff8['filename'];logger[_0x5e78('0x27')](_0x56aff8[_0x5e78('0x24')],_0x5e78('0x2f'));}};Fax[_0x5e78('0x1e')][_0x5e78('0x30')]=function(_0xfccc71){var _0x19fc37=this;if(this['channels'][_0xfccc71[_0x5e78('0x24')]]){var _0xfd8f50=this[_0x5e78('0x23')][_0xfccc71[_0x5e78('0x24')]]['id'];this['channels'][_0xfccc71[_0x5e78('0x24')]]['remotestationid']=_0xfccc71[_0x5e78('0x31')];this[_0x5e78('0x23')][_0xfccc71[_0x5e78('0x24')]][_0x5e78('0x32')]=_0xfccc71[_0x5e78('0x32')];this[_0x5e78('0x23')][_0xfccc71['uniqueid']][_0x5e78('0x22')]=_0xfccc71[_0x5e78('0x22')];this[_0x5e78('0x23')][_0xfccc71[_0x5e78('0x24')]][_0x5e78('0x33')]=_0xfccc71['resolution'];this[_0x5e78('0x23')][_0xfccc71['uniqueid']][_0x5e78('0x34')]=_0xfccc71[_0x5e78('0x34')];this[_0x5e78('0x23')][_0xfccc71['uniqueid']][_0x5e78('0x35')]=_0xfccc71[_0x5e78('0x35')];this['channels'][_0xfccc71[_0x5e78('0x24')]][_0x5e78('0x36')]=_0xfccc71[_0x5e78('0x36')];if(this[_0x5e78('0x23')][_0xfccc71[_0x5e78('0x24')]]['pagestransferred']>0x0){tiff2pdf(_0xfccc71['filename'],_0x5e78('0x37'),function(_0x1d42c0){logger[_0x5e78('0x27')](_0xfccc71['uniqueid'],'\x20result\x20conversion\x20file\x20',_0x1d42c0[_0x5e78('0x38')]);attachment['create']({'name':path['parse'](_0xfccc71[_0x5e78('0x35')])[_0x5e78('0x26')],'basename':path[_0x5e78('0x39')](_0xfccc71[_0x5e78('0x35')])[_0x5e78('0x26')]+'.pdf','type':_0x5e78('0x3a')})['then'](function(_0x45f1f4){faxAccount[_0x5e78('0x3b')](_0xfd8f50,{'from':_0xfccc71['calleridnum'],'mapKey':'fax','body':path[_0x5e78('0x39')](_0xfccc71['filename'])['name']+'.pdf','AttachmentId':_0x45f1f4['id']})[_0x5e78('0x3c')](function(_0x4cdc4a){logger[_0x5e78('0x3d')](_0xfccc71['uniqueid'],_0x5e78('0x3e'),JSON[_0x5e78('0x3f')](_0x4cdc4a));});})[_0x5e78('0x3c')](function(_0x40130f){logger[_0x5e78('0x3d')](_0xfccc71[_0x5e78('0x24')],_0x5e78('0x40'),JSON['stringify'](_0x40130f));});});}logger[_0x5e78('0x27')](_0xfccc71[_0x5e78('0x24')],_0x5e78('0x40'));}};Fax[_0x5e78('0x1e')]['syncOriginateResponse']=function(_0x1a586d){if(this[_0x5e78('0x10')][_0x1a586d[_0x5e78('0x24')]]){_[_0x5e78('0x41')](this[_0x5e78('0x23')][_0x1a586d[_0x5e78('0x24')]],_[_0x5e78('0x42')](_0x1a586d,[_0x5e78('0x43'),_0x5e78('0x44')]));}};function updateStatus(_0x222584){if(_0x222584[_0x5e78('0x45')]){faxMessage[_0x5e78('0x46')](_0x222584[_0x5e78('0x45')],{'failMessage':_0x5e78('0x47')})['then'](function(_0x29b8e4){ut[_0x5e78('0x48')](_0x5e78('0x49'),_0x29b8e4);})[_0x5e78('0x3c')](function(_0xd50f71){ut[_0x5e78('0x48')](_0x5e78('0x4a'),_0xd50f71);});}else{ut[_0x5e78('0x48')](_0x5e78('0x4b'));}}Fax[_0x5e78('0x1e')]['updateStatus']=function(_0x58e086,_0x3ff49d,_0x199f16){var _0x5108ec=this;var _0x2405ad={};if(_0x3ff49d){_0x2405ad={'read':_0x3ff49d};}else{_0x2405ad={'failMessage':_0x199f16};}if(this[_0x5e78('0x10')][_0x58e086][_0x5e78('0x45')]){faxMessage['update'](this[_0x5e78('0x10')][_0x58e086]['AttachmentId'],_0x2405ad)['then'](function(_0x18d016){ut[_0x5e78('0x48')](_0x5e78('0x4c'),_0x18d016);})[_0x5e78('0x3c')](function(_0x6a7a1c){ut[_0x5e78('0x48')](_0x5e78('0x4d'),_0x6a7a1c);})[_0x5e78('0x4e')](function(){delete _0x5108ec[_0x5e78('0x23')][_0x58e086];delete _0x5108ec['actions'][_0x58e086];});}else{ut['logger'](_0x5e78('0x4f'));delete this[_0x5e78('0x23')][_0x58e086];delete this[_0x5e78('0x10')][_0x58e086];}};Fax['prototype'][_0x5e78('0x1c')]=function(_0x9db381){var _0x30a66f=this;if(this['channels'][_0x9db381[_0x5e78('0x24')]]){if(this[_0x5e78('0x10')][_0x9db381[_0x5e78('0x24')]]){this[_0x5e78('0x23')][_0x9db381[_0x5e78('0x24')]]['id']=this[_0x5e78('0x10')][_0x9db381[_0x5e78('0x24')]]['id'];this['channels'][_0x9db381['uniqueid']][_0x5e78('0x26')]=this['actions'][_0x9db381[_0x5e78('0x24')]][_0x5e78('0x26')];}logger[_0x5e78('0x27')](_0x9db381[_0x5e78('0x24')],this['actions'][_0x9db381[_0x5e78('0x24')]]?_0x5e78('0x50'):_0x5e78('0x51'),JSON[_0x5e78('0x3f')](this[_0x5e78('0x23')][_0x9db381[_0x5e78('0x24')]]));if(this[_0x5e78('0x10')][_0x9db381[_0x5e78('0x24')]]){if(this[_0x5e78('0x23')][_0x9db381[_0x5e78('0x24')]][_0x5e78('0x52')]>0x0&&this[_0x5e78('0x23')][_0x9db381[_0x5e78('0x24')]][_0x5e78('0x19')]===_0x5e78('0x53')){this[_0x5e78('0x54')](_0x9db381[_0x5e78('0x24')],!![]);}else{this[_0x5e78('0x54')](_0x9db381[_0x5e78('0x24')],![],this[_0x5e78('0x23')][_0x9db381[_0x5e78('0x24')]][_0x5e78('0x55')]?'SENDING\x20FAILED:\x20'+this[_0x5e78('0x23')][_0x9db381[_0x5e78('0x24')]][_0x5e78('0x55')]:_0x5e78('0x56'));}}}};Fax[_0x5e78('0x1e')][_0x5e78('0x57')]=function(_0x62b937){var _0x38bb39=this;return new BPromise(function(_0x454054,_0x34e980){_0x38bb39[_0x5e78('0x58')](_0x62b937,_0x38bb39[_0x5e78('0xe')][_0x62b937[_0x5e78('0x59')]])[_0x5e78('0x5a')](function(_0x42a6ca){ut[_0x5e78('0x48')]('[Fax][execOriginate][success]',_0x42a6ca);_0x454054(ut['message'](_0x5e78('0x5b')));})['catch'](function(_0x96b469){ut[_0x5e78('0x48')](_0x5e78('0x5c'),_0x96b469);updateStatus(_0x62b937);_0x34e980(ut[_0x5e78('0x38')]('Fax\x20failed'));});});};Fax['prototype'][_0x5e78('0x5d')]=function(_0x2e7f55){var _0x277024=this;return new BPromise(function(_0x424444,_0x4468bf){var _0x31dfb0=_0x277024[_0x5e78('0x5e')](_0x277024[_0x5e78('0xe')][_0x2e7f55[_0x5e78('0x59')]]['Trunk'],_0x2e7f55);if(_0x31dfb0[_0x5e78('0x5f')]){_0x424444(ut[_0x5e78('0x38')]('Trunk\x20Reachable'));}else{_0x4468bf(ut[_0x5e78('0x38')](_0x31dfb0[_0x5e78('0x38')]));}});};function originate(_0x18f9c3,_0x2a91c4,_0x43b459){return new BPromise(function(_0x23a714,_0x545715){if(_0x43b459){var _0x290d81=util[_0x5e78('0x60')](_0x5e78('0x61'),path['parse'](_0x18f9c3[_0x5e78('0x62')])[_0x5e78('0x26')],_0x18f9c3['path']);gs[_0x5e78('0x63')](_0x290d81)['then'](function(){_0x18f9c3[_0x5e78('0x62')]=util[_0x5e78('0x60')](_0x5e78('0x64'),path[_0x5e78('0x39')](_0x18f9c3[_0x5e78('0x62')])[_0x5e78('0x65')],path[_0x5e78('0x39')](_0x18f9c3[_0x5e78('0x62')])[_0x5e78('0x26')]);_0x2a91c4['XMF-PATH']=_0x18f9c3[_0x5e78('0x62')];ut[_0x5e78('0x48')](_0x5e78('0x66'),'');ami[_0x5e78('0x67')](ut[_0x5e78('0x68')](_0x18f9c3,_0x2a91c4))[_0x5e78('0x5a')](function(_0x5a9c35){_0x23a714(_0x5a9c35);})[_0x5e78('0x3c')](function(_0x364238){_0x545715(_0x364238);});})['catch'](function(_0x1d84df){_0x545715(_0x1d84df);});}else{ami[_0x5e78('0x67')](ut['variablesAction'](_0x18f9c3,_0x2a91c4))['then'](function(_0x1b7c76){_0x23a714(_0x1b7c76);})[_0x5e78('0x3c')](function(_0x5269d1){_0x545715(_0x5269d1);});}});}Fax['prototype'][_0x5e78('0x58')]=function(_0x5c2be0,_0x426dce){var _0x4e77f2=this;return new BPromise(function(_0x411005,_0x57aba4){var _0x2ef7fc=uuid['v4']();var _0x506901=ut[_0x5e78('0x69')](_0x5c2be0,_0x426dce);_0x4e77f2[_0x5e78('0x10')][_0x2ef7fc]=new Action(_0x2ef7fc,_0x426dce,_0x5c2be0);var _0x489c54=_0x4e77f2['actions'][_0x2ef7fc];if(path['parse'](_0x5c2be0['path'])[_0x5e78('0x6a')]===_0x5e78('0x6b')){originate(_0x489c54,_0x506901,!![])[_0x5e78('0x5a')](function(_0x1700a2){_0x411005(_0x1700a2);})[_0x5e78('0x3c')](function(_0xa154cf){_0x57aba4(_0xa154cf);});}else{originate(_0x489c54,_0x506901,![])['then'](function(_0x1ef02f){_0x411005(_0x1ef02f);})[_0x5e78('0x3c')](function(_0x4ed480){_0x57aba4(_0x4ed480);});}});};Fax[_0x5e78('0x1e')][_0x5e78('0x6c')]=function(_0x1d2f74){return!_[_0x5e78('0x6d')](_0x1d2f74)&&!_[_0x5e78('0x6d')](_0x1d2f74['name'])&&!_[_0x5e78('0x6d')](_0x1d2f74[_0x5e78('0x6e')])&&_0x1d2f74['active']&&!_['isNil'](this[_0x5e78('0xf')][util[_0x5e78('0x60')](_0x5e78('0x6f'),_0x1d2f74[_0x5e78('0x26')])])&&!_['isNil'](this[_0x5e78('0xf')][util['format'](_0x5e78('0x6f'),_0x1d2f74[_0x5e78('0x26')])][_0x5e78('0x2e')])&&(this[_0x5e78('0xf')][util['format'](_0x5e78('0x6f'),_0x1d2f74[_0x5e78('0x26')])]['status']===_0x5e78('0x70')||this[_0x5e78('0xf')][util[_0x5e78('0x60')]('SIP/%s',_0x1d2f74[_0x5e78('0x26')])]['status']==='unmonitored');};Fax[_0x5e78('0x1e')]['checkTrunk']=function(_0x1d2eb5,_0x35e465){if(!this['isTrunkReachable'](_0x1d2eb5)){ut['logger']('[Fax][checkTrunk][Trunk\x20not\x20work]',_[_0x5e78('0x6d')](_0x1d2eb5)?'':_0x1d2eb5[_0x5e78('0x26')]+_0x5e78('0x71'));return{'isValid':![],'message':_0x5e78('0x72')};}if(_[_0x5e78('0x6d')](_0x35e465[_0x5e78('0x7')])){ut[_0x5e78('0x48')](_0x5e78('0x73'),JSON['stringify'](_0x35e465));return{'isValid':![],'message':_0x5e78('0x74')};}if(_['isNil'](_0x35e465['path'])){ut[_0x5e78('0x48')](_0x5e78('0x75'),JSON[_0x5e78('0x3f')](_0x35e465));return{'isValid':![],'message':_0x5e78('0x76')};}return{'isValid':!![],'message':_0x5e78('0x77')};};module[_0x5e78('0x78')]=Fax;