f58e713a20ebdfd4ff503bfe3ca8b1adf33200b0
[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 _0xbfdb=['fax','../ami','./channel','./util/util','./action','../rpc/faxAccount','../rpc/attachment','../rpc/faxMessage','channels','faxAccounts','trunks','actions','syncNewchannel','bind','varset','syncVarSet','newstate','syncNewState','syncNewExten','faxstatus','hangup','originateresponse','syncOriginateResponse','prototype','context','from-voip-provider','hasOwnProperty','exten','uniqueid','channel','name','info','\x20syncNewchannel\x20','variable','value','channelstate','channelstatedesc','operation','status','localstationid','filename','\x20syncFaxStatus\x20','syncReceiveFax','remotestationid','pagestransferred','resolution','transferrate','/var/opt/motion2/server/files/attachments/','\x20result\x20conversion\x20file\x20','create','parse','application/pdf','then','notify','calleridnum','.pdf','\x20notify\x20','error','stringify','\x20receiveFax\x20','merge','pick','response','AttachmentId','SENDING\x20FAILED\x20','logger','[Fax][Message][Error][Updated]','catch','[Fax][Message][Error][NoUpdate2]','[Fax][Message][Updated]','finally','[Fax][Message][NoUpdate2]','deleteChannelAfterTimeout','syncHangup','\x20hangup\x20sendFile\x20','\x20hangup\x20faxRiceive\x20','SUCCESS','updateStatus','faxstatusstring','SENDING\x20FAILED:\x20','send','execOriginate','FaxAccountId','[Fax][execOriginate][success]','message','[Fax][execOriginate][failure]','Fax\x20failed','checkAfterSend','checkTrunk','Trunk','isValid','format','execute','%s/%s.tif','XMF-PATH','Convert\x20pdf\x20to\x20tif','Action','variablesAction','ext','isTrunkReachable','isNil','active','SIP/%s','[Fax][checkTrunk][Trunk\x20not\x20working\x20correctly]','\x20Trunk\x20is\x20not\x20active','Trunk\x20not\x20working\x20correctly','Empty\x20Fax','[Fax][checkTrunk][Empty\x20path]','Empty\x20path','Is\x20Valid','exports','util','lodash','bluebird','moment','path','tiff2pdf','./util/gs','../../../config/logger'];(function(_0x484e36,_0x4482d8){var _0xafc85d=function(_0xaba9c8){while(--_0xaba9c8){_0x484e36['push'](_0x484e36['shift']());}};_0xafc85d(++_0x4482d8);}(_0xbfdb,0x1d4));var _0xbbfd=function(_0x68f6a2,_0x292f96){_0x68f6a2=_0x68f6a2-0x0;var _0x52d81e=_0xbfdb[_0x68f6a2];return _0x52d81e;};'use strict';var util=require(_0xbbfd('0x0'));var _=require(_0xbbfd('0x1'));var BPromise=require(_0xbbfd('0x2'));var uuid=require('uuid');var moment=require(_0xbbfd('0x3'));var path=require(_0xbbfd('0x4'));var tiff2pdf=require(_0xbbfd('0x5'));var gs=require(_0xbbfd('0x6'));var logger=require(_0xbbfd('0x7'))(_0xbbfd('0x8'));var ami=require(_0xbbfd('0x9'));var rpcDb=require('./rpcDb');var Channel=require(_0xbbfd('0xa'));var ut=require(_0xbbfd('0xb'));var Action=require(_0xbbfd('0xc'));var faxAccount=require(_0xbbfd('0xd'));var attachment=require(_0xbbfd('0xe'));var faxMessage=require(_0xbbfd('0xf'));function Fax(_0x69bc47){this[_0xbbfd('0x10')]={};this[_0xbbfd('0x11')]=_0x69bc47['faxAccounts'];this[_0xbbfd('0x12')]=_0x69bc47[_0xbbfd('0x12')];this[_0xbbfd('0x13')]={};ami['on']('newchannel',this[_0xbbfd('0x14')][_0xbbfd('0x15')](this));ami['on'](_0xbbfd('0x16'),this[_0xbbfd('0x17')][_0xbbfd('0x15')](this));ami['on'](_0xbbfd('0x18'),this[_0xbbfd('0x19')][_0xbbfd('0x15')](this));ami['on']('newexten',this[_0xbbfd('0x1a')]['bind'](this));ami['on'](_0xbbfd('0x1b'),this['syncFaxStatus'][_0xbbfd('0x15')](this));ami['on']('receivefax',this['syncReceiveFax']['bind'](this));ami['on'](_0xbbfd('0x1c'),this['syncHangup'][_0xbbfd('0x15')](this));ami['on'](_0xbbfd('0x1d'),this[_0xbbfd('0x1e')][_0xbbfd('0x15')](this));}Fax[_0xbbfd('0x1f')][_0xbbfd('0x14')]=function(_0xd9e8da){if(_0xd9e8da[_0xbbfd('0x20')]===_0xbbfd('0x21')){for(var _0x2642e3 in this[_0xbbfd('0x11')]){if(this[_0xbbfd('0x11')][_0xbbfd('0x22')](_0x2642e3)){if(_0xd9e8da[_0xbbfd('0x23')]===this[_0xbbfd('0x11')][_0x2642e3]['localstationid']){this[_0xbbfd('0x10')][_0xd9e8da[_0xbbfd('0x24')]]={};this[_0xbbfd('0x10')][_0xd9e8da[_0xbbfd('0x24')]][_0xbbfd('0x25')]=_0xd9e8da[_0xbbfd('0x25')];this[_0xbbfd('0x10')][_0xd9e8da[_0xbbfd('0x24')]]['id']=this['faxAccounts'][_0x2642e3]['id'];this[_0xbbfd('0x10')][_0xd9e8da[_0xbbfd('0x24')]][_0xbbfd('0x26')]=this[_0xbbfd('0x11')][_0x2642e3][_0xbbfd('0x26')];logger[_0xbbfd('0x27')](_0xd9e8da['uniqueid'],'\x20syncNewchannel\x20');}}}}if(this['actions'][_0xd9e8da[_0xbbfd('0x24')]]){this[_0xbbfd('0x10')][_0xd9e8da[_0xbbfd('0x24')]]={};this[_0xbbfd('0x10')][_0xd9e8da[_0xbbfd('0x24')]]['channel']=_0xd9e8da['channel'];logger['info'](_0xd9e8da[_0xbbfd('0x24')],_0xbbfd('0x28'));}};Fax['prototype'][_0xbbfd('0x17')]=function(_0x50125c){if(this[_0xbbfd('0x10')][_0x50125c['uniqueid']]){for(var _0x1d6378 in _0x50125c[_0xbbfd('0x29')]){if(_0x50125c['variable']['hasOwnProperty'](_0x1d6378)){this['channels'][_0x50125c[_0xbbfd('0x24')]][_0x1d6378]=_0x50125c[_0xbbfd('0x2a')];}}}};Fax[_0xbbfd('0x1f')]['syncNewState']=function(_0x1ee749){if(this['channels'][_0x1ee749[_0xbbfd('0x24')]]){this[_0xbbfd('0x10')][_0x1ee749[_0xbbfd('0x24')]][_0xbbfd('0x2b')]=_0x1ee749[_0xbbfd('0x2b')];this[_0xbbfd('0x10')][_0x1ee749[_0xbbfd('0x24')]][_0xbbfd('0x2c')]=_0x1ee749[_0xbbfd('0x2c')];}};Fax[_0xbbfd('0x1f')]['syncNewExten']=function(_0x30a0f7){if(this['channels'][_0x30a0f7[_0xbbfd('0x24')]]){for(var _0x5513b1 in _0x30a0f7['variable']){if(_0x30a0f7[_0xbbfd('0x29')][_0xbbfd('0x22')](_0x5513b1)){this[_0xbbfd('0x10')][_0x30a0f7[_0xbbfd('0x24')]][_0x5513b1]=_0x30a0f7[_0xbbfd('0x2a')];}}}};Fax[_0xbbfd('0x1f')]['syncFaxStatus']=function(_0xc22cea){if(this[_0xbbfd('0x10')][_0xc22cea[_0xbbfd('0x24')]]){this['channels'][_0xc22cea[_0xbbfd('0x24')]][_0xbbfd('0x2d')]=_0xc22cea[_0xbbfd('0x2d')];this[_0xbbfd('0x10')][_0xc22cea[_0xbbfd('0x24')]]['status']=_0xc22cea[_0xbbfd('0x2e')];this[_0xbbfd('0x10')][_0xc22cea[_0xbbfd('0x24')]][_0xbbfd('0x2f')]=_0xc22cea[_0xbbfd('0x2f')];this[_0xbbfd('0x10')][_0xc22cea[_0xbbfd('0x24')]][_0xbbfd('0x30')]=_0xc22cea[_0xbbfd('0x30')];logger['info'](_0xc22cea[_0xbbfd('0x24')],_0xbbfd('0x31'));}};Fax[_0xbbfd('0x1f')][_0xbbfd('0x32')]=function(_0x66d33c){var _0x25dc90=this;if(this['channels'][_0x66d33c[_0xbbfd('0x24')]]){var _0x55bf5e=this[_0xbbfd('0x10')][_0x66d33c[_0xbbfd('0x24')]]['id'];this[_0xbbfd('0x10')][_0x66d33c[_0xbbfd('0x24')]][_0xbbfd('0x33')]=_0x66d33c[_0xbbfd('0x33')];this[_0xbbfd('0x10')][_0x66d33c['uniqueid']][_0xbbfd('0x34')]=_0x66d33c[_0xbbfd('0x34')];this[_0xbbfd('0x10')][_0x66d33c[_0xbbfd('0x24')]][_0xbbfd('0x2f')]=_0x66d33c['localstationid'];this[_0xbbfd('0x10')][_0x66d33c[_0xbbfd('0x24')]][_0xbbfd('0x35')]=_0x66d33c[_0xbbfd('0x35')];this[_0xbbfd('0x10')][_0x66d33c['uniqueid']][_0xbbfd('0x36')]=_0x66d33c[_0xbbfd('0x36')];this[_0xbbfd('0x10')][_0x66d33c['uniqueid']][_0xbbfd('0x30')]=_0x66d33c[_0xbbfd('0x30')];this['channels'][_0x66d33c['uniqueid']]['calleridnum']=_0x66d33c['calleridnum'];if(this[_0xbbfd('0x10')][_0x66d33c['uniqueid']][_0xbbfd('0x34')]>0x0){tiff2pdf(_0x66d33c[_0xbbfd('0x30')],_0xbbfd('0x37'),function(_0x477372){logger[_0xbbfd('0x27')](_0x66d33c[_0xbbfd('0x24')],_0xbbfd('0x38'),_0x477372['message']);attachment[_0xbbfd('0x39')]({'name':path['parse'](_0x66d33c[_0xbbfd('0x30')])[_0xbbfd('0x26')],'basename':path[_0xbbfd('0x3a')](_0x66d33c[_0xbbfd('0x30')])[_0xbbfd('0x26')]+'.pdf','type':_0xbbfd('0x3b')})[_0xbbfd('0x3c')](function(_0x1d01d7){faxAccount[_0xbbfd('0x3d')](_0x55bf5e,{'from':_0x66d33c[_0xbbfd('0x3e')],'mapKey':_0xbbfd('0x8'),'body':path['parse'](_0x66d33c[_0xbbfd('0x30')])['name']+_0xbbfd('0x3f'),'AttachmentId':_0x1d01d7['id']})['catch'](function(_0x5833c3){logger['error'](_0x66d33c[_0xbbfd('0x24')],_0xbbfd('0x40'),JSON['stringify'](_0x5833c3));});})['catch'](function(_0x1652dc){logger[_0xbbfd('0x41')](_0x66d33c['uniqueid'],'\x20receiveFax\x20',JSON[_0xbbfd('0x42')](_0x1652dc));});});}logger[_0xbbfd('0x27')](_0x66d33c[_0xbbfd('0x24')],_0xbbfd('0x43'));}};Fax['prototype'][_0xbbfd('0x1e')]=function(_0x1a4dda){if(this[_0xbbfd('0x13')][_0x1a4dda[_0xbbfd('0x24')]]){_[_0xbbfd('0x44')](this[_0xbbfd('0x10')][_0x1a4dda['uniqueid']],_[_0xbbfd('0x45')](_0x1a4dda,[_0xbbfd('0x46'),'reason']));}};function updateStatus(_0x5dd742){if(_0x5dd742['AttachmentId']){faxMessage['update'](_0x5dd742[_0xbbfd('0x47')],{'failMessage':_0xbbfd('0x48')})[_0xbbfd('0x3c')](function(_0x2c25eb){ut[_0xbbfd('0x49')](_0xbbfd('0x4a'),_0x2c25eb);})[_0xbbfd('0x4b')](function(_0x5f1b91){ut[_0xbbfd('0x49')]('[Fax][Message][Error][NoUpdate1]',_0x5f1b91);});}else{ut[_0xbbfd('0x49')](_0xbbfd('0x4c'));}}Fax[_0xbbfd('0x1f')]['updateStatus']=function(_0x31f4d7,_0xdf2212,_0x36593f){var _0x30c60a=this;var _0x247538={};if(_0xdf2212){_0x247538={'read':_0xdf2212};}else{_0x247538={'failMessage':_0x36593f};}if(this[_0xbbfd('0x13')][_0x31f4d7][_0xbbfd('0x47')]){faxMessage['update'](this[_0xbbfd('0x13')][_0x31f4d7]['AttachmentId'],_0x247538)[_0xbbfd('0x3c')](function(_0x1bfc8d){ut['logger'](_0xbbfd('0x4d'),_0x1bfc8d);})[_0xbbfd('0x4b')](function(_0x5e5fac){ut[_0xbbfd('0x49')]('[Fax][Message][NoUpdate1]',_0x5e5fac);})[_0xbbfd('0x4e')](function(){delete _0x30c60a[_0xbbfd('0x10')][_0x31f4d7];delete _0x30c60a[_0xbbfd('0x13')][_0x31f4d7];});}else{ut[_0xbbfd('0x49')](_0xbbfd('0x4f'));this[_0xbbfd('0x50')](_0x31f4d7);delete this[_0xbbfd('0x13')][_0x31f4d7];}};Fax['prototype'][_0xbbfd('0x51')]=function(_0xfb0450){var _0x4aa44f=this;if(this[_0xbbfd('0x10')][_0xfb0450[_0xbbfd('0x24')]]){if(this[_0xbbfd('0x13')][_0xfb0450[_0xbbfd('0x24')]]){this['channels'][_0xfb0450[_0xbbfd('0x24')]]['id']=this[_0xbbfd('0x13')][_0xfb0450[_0xbbfd('0x24')]]['id'];this['channels'][_0xfb0450[_0xbbfd('0x24')]][_0xbbfd('0x26')]=this[_0xbbfd('0x13')][_0xfb0450[_0xbbfd('0x24')]][_0xbbfd('0x26')];}logger[_0xbbfd('0x27')](_0xfb0450[_0xbbfd('0x24')],this[_0xbbfd('0x13')][_0xfb0450['uniqueid']]?_0xbbfd('0x52'):_0xbbfd('0x53'),JSON[_0xbbfd('0x42')](this[_0xbbfd('0x10')][_0xfb0450[_0xbbfd('0x24')]]));if(this[_0xbbfd('0x13')][_0xfb0450['uniqueid']]){if(this[_0xbbfd('0x10')][_0xfb0450[_0xbbfd('0x24')]]['faxpages']>0x0&&this[_0xbbfd('0x10')][_0xfb0450[_0xbbfd('0x24')]][_0xbbfd('0x1b')]===_0xbbfd('0x54')){this[_0xbbfd('0x55')](_0xfb0450[_0xbbfd('0x24')],!![]);}else{this[_0xbbfd('0x55')](_0xfb0450['uniqueid'],![],this[_0xbbfd('0x10')][_0xfb0450[_0xbbfd('0x24')]][_0xbbfd('0x56')]?_0xbbfd('0x57')+this[_0xbbfd('0x10')][_0xfb0450['uniqueid']][_0xbbfd('0x56')]:'SENDING\x20FAILED');}}}};Fax[_0xbbfd('0x1f')][_0xbbfd('0x58')]=function(_0x4eb25b){var _0x135d51=this;return new BPromise(function(_0x21ea87,_0x102fcd){_0x135d51[_0xbbfd('0x59')](_0x4eb25b,_0x135d51[_0xbbfd('0x11')][_0x4eb25b[_0xbbfd('0x5a')]])[_0xbbfd('0x3c')](function(_0x14e6e3){ut['logger'](_0xbbfd('0x5b'),_0x14e6e3);_0x21ea87(ut[_0xbbfd('0x5c')]('Fax\x20received'));})['catch'](function(_0x409316){ut[_0xbbfd('0x49')](_0xbbfd('0x5d'),_0x409316);updateStatus(_0x4eb25b);_0x102fcd(ut[_0xbbfd('0x5c')](_0xbbfd('0x5e')));});});};Fax[_0xbbfd('0x1f')][_0xbbfd('0x5f')]=function(_0x19d824){var _0x5a4cb4=this;return new BPromise(function(_0x127441,_0x4ca031){var _0x469470=_0x5a4cb4[_0xbbfd('0x60')](_0x5a4cb4['faxAccounts'][_0x19d824[_0xbbfd('0x5a')]][_0xbbfd('0x61')],_0x19d824);if(_0x469470[_0xbbfd('0x62')]){_0x127441(ut[_0xbbfd('0x5c')]('Trunk\x20Reachable'));}else{_0x4ca031(ut[_0xbbfd('0x5c')](_0x469470[_0xbbfd('0x5c')]));}});};function originate(_0x1129a1,_0x4c3746,_0x4295e2){return new BPromise(function(_0x545415,_0x1a5488){if(_0x4295e2){var _0x18d68b=util[_0xbbfd('0x63')]('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['parse'](_0x1129a1['path'])[_0xbbfd('0x26')],_0x1129a1[_0xbbfd('0x4')]);gs[_0xbbfd('0x64')](_0x18d68b)[_0xbbfd('0x3c')](function(){_0x1129a1[_0xbbfd('0x4')]=util['format'](_0xbbfd('0x65'),path[_0xbbfd('0x3a')](_0x1129a1[_0xbbfd('0x4')])['dir'],path['parse'](_0x1129a1[_0xbbfd('0x4')])[_0xbbfd('0x26')]);_0x4c3746[_0xbbfd('0x66')]=_0x1129a1[_0xbbfd('0x4')];ut[_0xbbfd('0x49')](_0xbbfd('0x67'),'');ami['Action'](ut['variablesAction'](_0x1129a1,_0x4c3746))['then'](function(_0x2be233){_0x545415(_0x2be233);})[_0xbbfd('0x4b')](function(_0x26a34d){_0x1a5488(_0x26a34d);});})[_0xbbfd('0x4b')](function(_0x5d4cbd){_0x1a5488(_0x5d4cbd);});}else{ami[_0xbbfd('0x68')](ut[_0xbbfd('0x69')](_0x1129a1,_0x4c3746))[_0xbbfd('0x3c')](function(_0x5133b8){_0x545415(_0x5133b8);})[_0xbbfd('0x4b')](function(_0x1697fb){_0x1a5488(_0x1697fb);});}});}Fax[_0xbbfd('0x1f')][_0xbbfd('0x59')]=function(_0x307a93,_0x4f2f7f){var _0x4bc645=this;return new BPromise(function(_0x33a5f7,_0xa445e){var _0x186f32=uuid['v4']();var _0x29d923=ut['addVariables'](_0x307a93,_0x4f2f7f);_0x4bc645[_0xbbfd('0x13')][_0x186f32]=new Action(_0x186f32,_0x4f2f7f,_0x307a93);var _0x1606c7=_0x4bc645['actions'][_0x186f32];if(path[_0xbbfd('0x3a')](_0x307a93['path'])[_0xbbfd('0x6a')]===_0xbbfd('0x3f')){originate(_0x1606c7,_0x29d923,!![])[_0xbbfd('0x3c')](function(_0x45c573){_0x33a5f7(_0x45c573);})[_0xbbfd('0x4b')](function(_0x292a13){_0xa445e(_0x292a13);});}else{originate(_0x1606c7,_0x29d923,![])['then'](function(_0x2adba2){_0x33a5f7(_0x2adba2);})[_0xbbfd('0x4b')](function(_0x2bb72e){_0xa445e(_0x2bb72e);});}});};Fax[_0xbbfd('0x1f')][_0xbbfd('0x6b')]=function(_0x3877ae){return!_[_0xbbfd('0x6c')](_0x3877ae)&&!_[_0xbbfd('0x6c')](_0x3877ae[_0xbbfd('0x26')])&&!_[_0xbbfd('0x6c')](_0x3877ae[_0xbbfd('0x6d')])&&_0x3877ae[_0xbbfd('0x6d')]&&!_['isNil'](this[_0xbbfd('0x12')][util[_0xbbfd('0x63')](_0xbbfd('0x6e'),_0x3877ae['name'])])&&!_[_0xbbfd('0x6c')](this[_0xbbfd('0x12')][util[_0xbbfd('0x63')](_0xbbfd('0x6e'),_0x3877ae[_0xbbfd('0x26')])][_0xbbfd('0x2e')])&&(this[_0xbbfd('0x12')][util[_0xbbfd('0x63')](_0xbbfd('0x6e'),_0x3877ae['name'])]['status']==='reachable'||this['trunks'][util[_0xbbfd('0x63')](_0xbbfd('0x6e'),_0x3877ae[_0xbbfd('0x26')])]['status']==='unmonitored');};Fax[_0xbbfd('0x1f')]['checkTrunk']=function(_0x4e8f11,_0x14f6b6){if(!this[_0xbbfd('0x6b')](_0x4e8f11)){ut[_0xbbfd('0x49')](_0xbbfd('0x6f'),_[_0xbbfd('0x6c')](_0x4e8f11)?'':_0x4e8f11[_0xbbfd('0x26')]+_0xbbfd('0x70'));return{'isValid':![],'message':_0xbbfd('0x71')};}if(_['isNil'](_0x14f6b6[_0xbbfd('0x8')])){ut[_0xbbfd('0x49')]('[Fax][checkTrunk][Empty\x20Fax]',JSON[_0xbbfd('0x42')](_0x14f6b6));return{'isValid':![],'message':_0xbbfd('0x72')};}if(_[_0xbbfd('0x6c')](_0x14f6b6[_0xbbfd('0x4')])){ut[_0xbbfd('0x49')](_0xbbfd('0x73'),JSON[_0xbbfd('0x42')](_0x14f6b6));return{'isValid':![],'message':_0xbbfd('0x74')};}return{'isValid':!![],'message':_0xbbfd('0x75')};};Fax[_0xbbfd('0x1f')][_0xbbfd('0x50')]=function(_0x4cebb5,_0x1e027b=0x2710){if(this[_0xbbfd('0x10')][_0x4cebb5]){setTimeout(()=>{delete this[_0xbbfd('0x10')][_0x4cebb5];},_0x1e027b);}};module[_0xbbfd('0x76')]=Fax;