aef89d66d3402f9b9ff952eb09fe902e30f2e1ad
[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 _0x5548=['path','tiff2pdf','./util/gs','../../../config/logger','fax','../ami','./rpcDb','./action','../rpc/faxAccount','../rpc/attachment','../rpc/faxMessage','channels','faxAccounts','trunks','newchannel','varset','syncVarSet','bind','newstate','newexten','syncNewExten','faxstatus','syncFaxStatus','syncReceiveFax','hangup','syncHangup','originateresponse','syncOriginateResponse','prototype','syncNewchannel','context','from-voip-provider','hasOwnProperty','exten','localstationid','uniqueid','channel','name','\x20syncNewchannel\x20','info','variable','value','syncNewState','channelstate','channelstatedesc','operation','status','filename','remotestationid','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','create','parse','application/pdf','then','.pdf','catch','error','stringify','\x20receiveFax\x20','response','reason','AttachmentId','update','SENDING\x20FAILED\x20','logger','[Fax][Message][Error][Updated]','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','updateStatus','[Fax][Message][NoUpdate1]','finally','actions','\x20hangup\x20faxRiceive\x20','faxpages','SUCCESS','faxstatusstring','SENDING\x20FAILED:\x20','SENDING\x20FAILED','send','execOriginate','FaxAccountId','[Fax][execOriginate][success]','[Fax][execOriginate][failure]','Fax\x20failed','checkAfterSend','checkTrunk','isValid','message','Trunk\x20Reachable','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','execute','format','%s/%s.tif','dir','XMF-PATH','Convert\x20pdf\x20to\x20tif','Action','variablesAction','addVariables','isTrunkReachable','isNil','active','SIP/%s','\x20Trunk\x20is\x20not\x20active','Trunk\x20not\x20working\x20correctly','Empty\x20Fax','Empty\x20path','Is\x20Valid','util','bluebird','uuid','moment'];(function(_0x5c376d,_0x1309d7){var _0x5f0980=function(_0x5dff99){while(--_0x5dff99){_0x5c376d['push'](_0x5c376d['shift']());}};_0x5f0980(++_0x1309d7);}(_0x5548,0x70));var _0x8554=function(_0x5b4a57,_0x256746){_0x5b4a57=_0x5b4a57-0x0;var _0x350ead=_0x5548[_0x5b4a57];return _0x350ead;};'use strict';var util=require(_0x8554('0x0'));var _=require('lodash');var BPromise=require(_0x8554('0x1'));var uuid=require(_0x8554('0x2'));var moment=require(_0x8554('0x3'));var path=require(_0x8554('0x4'));var tiff2pdf=require(_0x8554('0x5'));var gs=require(_0x8554('0x6'));var logger=require(_0x8554('0x7'))(_0x8554('0x8'));var ami=require(_0x8554('0x9'));var rpcDb=require(_0x8554('0xa'));var Channel=require('./channel');var ut=require('./util/util');var Action=require(_0x8554('0xb'));var faxAccount=require(_0x8554('0xc'));var attachment=require(_0x8554('0xd'));var faxMessage=require(_0x8554('0xe'));function Fax(_0x28fe21){this[_0x8554('0xf')]={};this[_0x8554('0x10')]=_0x28fe21[_0x8554('0x10')];this['trunks']=_0x28fe21[_0x8554('0x11')];this['actions']={};ami['on'](_0x8554('0x12'),this['syncNewchannel']['bind'](this));ami['on'](_0x8554('0x13'),this[_0x8554('0x14')][_0x8554('0x15')](this));ami['on'](_0x8554('0x16'),this['syncNewState']['bind'](this));ami['on'](_0x8554('0x17'),this[_0x8554('0x18')][_0x8554('0x15')](this));ami['on'](_0x8554('0x19'),this[_0x8554('0x1a')]['bind'](this));ami['on']('receivefax',this[_0x8554('0x1b')]['bind'](this));ami['on'](_0x8554('0x1c'),this[_0x8554('0x1d')]['bind'](this));ami['on'](_0x8554('0x1e'),this[_0x8554('0x1f')][_0x8554('0x15')](this));}Fax[_0x8554('0x20')][_0x8554('0x21')]=function(_0x473eb4){if(_0x473eb4[_0x8554('0x22')]===_0x8554('0x23')){for(var _0x176d6b in this[_0x8554('0x10')]){if(this['faxAccounts'][_0x8554('0x24')](_0x176d6b)){if(_0x473eb4[_0x8554('0x25')]===this['faxAccounts'][_0x176d6b][_0x8554('0x26')]){this['channels'][_0x473eb4[_0x8554('0x27')]]={};this[_0x8554('0xf')][_0x473eb4['uniqueid']][_0x8554('0x28')]=_0x473eb4[_0x8554('0x28')];this[_0x8554('0xf')][_0x473eb4[_0x8554('0x27')]]['id']=this[_0x8554('0x10')][_0x176d6b]['id'];this[_0x8554('0xf')][_0x473eb4[_0x8554('0x27')]][_0x8554('0x29')]=this[_0x8554('0x10')][_0x176d6b][_0x8554('0x29')];logger['info'](_0x473eb4[_0x8554('0x27')],_0x8554('0x2a'));}}}}if(this['actions'][_0x473eb4[_0x8554('0x27')]]){this['channels'][_0x473eb4['uniqueid']]={};this[_0x8554('0xf')][_0x473eb4[_0x8554('0x27')]][_0x8554('0x28')]=_0x473eb4['channel'];logger[_0x8554('0x2b')](_0x473eb4[_0x8554('0x27')],'\x20syncNewchannel\x20');}};Fax[_0x8554('0x20')]['syncVarSet']=function(_0x4c49b7){if(this[_0x8554('0xf')][_0x4c49b7[_0x8554('0x27')]]){for(var _0x9b5b02 in _0x4c49b7[_0x8554('0x2c')]){if(_0x4c49b7['variable'][_0x8554('0x24')](_0x9b5b02)){this['channels'][_0x4c49b7['uniqueid']][_0x9b5b02]=_0x4c49b7[_0x8554('0x2d')];}}}};Fax['prototype'][_0x8554('0x2e')]=function(_0x1bddf3){if(this[_0x8554('0xf')][_0x1bddf3[_0x8554('0x27')]]){this['channels'][_0x1bddf3[_0x8554('0x27')]][_0x8554('0x2f')]=_0x1bddf3[_0x8554('0x2f')];this[_0x8554('0xf')][_0x1bddf3[_0x8554('0x27')]][_0x8554('0x30')]=_0x1bddf3['channelstatedesc'];}};Fax[_0x8554('0x20')][_0x8554('0x18')]=function(_0x1ac2fa){if(this[_0x8554('0xf')][_0x1ac2fa[_0x8554('0x27')]]){for(var _0x5194af in _0x1ac2fa['variable']){if(_0x1ac2fa[_0x8554('0x2c')]['hasOwnProperty'](_0x5194af)){this[_0x8554('0xf')][_0x1ac2fa[_0x8554('0x27')]][_0x5194af]=_0x1ac2fa[_0x8554('0x2d')];}}}};Fax[_0x8554('0x20')][_0x8554('0x1a')]=function(_0x181a77){if(this[_0x8554('0xf')][_0x181a77[_0x8554('0x27')]]){this[_0x8554('0xf')][_0x181a77[_0x8554('0x27')]]['operation']=_0x181a77[_0x8554('0x31')];this[_0x8554('0xf')][_0x181a77[_0x8554('0x27')]][_0x8554('0x32')]=_0x181a77['status'];this[_0x8554('0xf')][_0x181a77[_0x8554('0x27')]][_0x8554('0x26')]=_0x181a77[_0x8554('0x26')];this[_0x8554('0xf')][_0x181a77[_0x8554('0x27')]][_0x8554('0x33')]=_0x181a77[_0x8554('0x33')];logger[_0x8554('0x2b')](_0x181a77['uniqueid'],'\x20syncFaxStatus\x20');}};Fax['prototype'][_0x8554('0x1b')]=function(_0x21d527){var _0x28c795=this;if(this['channels'][_0x21d527[_0x8554('0x27')]]){var _0x1471f0=this[_0x8554('0xf')][_0x21d527['uniqueid']]['id'];this[_0x8554('0xf')][_0x21d527[_0x8554('0x27')]][_0x8554('0x34')]=_0x21d527[_0x8554('0x34')];this['channels'][_0x21d527[_0x8554('0x27')]][_0x8554('0x35')]=_0x21d527[_0x8554('0x35')];this[_0x8554('0xf')][_0x21d527[_0x8554('0x27')]][_0x8554('0x26')]=_0x21d527[_0x8554('0x26')];this[_0x8554('0xf')][_0x21d527[_0x8554('0x27')]]['resolution']=_0x21d527[_0x8554('0x36')];this['channels'][_0x21d527['uniqueid']][_0x8554('0x37')]=_0x21d527['transferrate'];this[_0x8554('0xf')][_0x21d527[_0x8554('0x27')]][_0x8554('0x33')]=_0x21d527[_0x8554('0x33')];this['channels'][_0x21d527[_0x8554('0x27')]]['calleridnum']=_0x21d527[_0x8554('0x38')];if(this['channels'][_0x21d527['uniqueid']]['pagestransferred']>0x0){tiff2pdf(_0x21d527[_0x8554('0x33')],_0x8554('0x39'),function(_0x16eee9){logger[_0x8554('0x2b')](_0x21d527[_0x8554('0x27')],'\x20result\x20conversion\x20file\x20',_0x16eee9['message']);attachment[_0x8554('0x3a')]({'name':path['parse'](_0x21d527['filename'])[_0x8554('0x29')],'basename':path[_0x8554('0x3b')](_0x21d527[_0x8554('0x33')])[_0x8554('0x29')]+'.pdf','type':_0x8554('0x3c')})[_0x8554('0x3d')](function(_0x907117){faxAccount['notify'](_0x1471f0,{'from':_0x21d527[_0x8554('0x38')],'mapKey':'fax','body':path[_0x8554('0x3b')](_0x21d527[_0x8554('0x33')])[_0x8554('0x29')]+_0x8554('0x3e'),'AttachmentId':_0x907117['id']})[_0x8554('0x3f')](function(_0x3407bf){logger[_0x8554('0x40')](_0x21d527[_0x8554('0x27')],'\x20notify\x20',JSON[_0x8554('0x41')](_0x3407bf));});})[_0x8554('0x3f')](function(_0x57571a){logger['error'](_0x21d527[_0x8554('0x27')],_0x8554('0x42'),JSON[_0x8554('0x41')](_0x57571a));});});}logger['info'](_0x21d527[_0x8554('0x27')],_0x8554('0x42'));}};Fax[_0x8554('0x20')][_0x8554('0x1f')]=function(_0x4a4181){if(this['actions'][_0x4a4181[_0x8554('0x27')]]){_['merge'](this[_0x8554('0xf')][_0x4a4181['uniqueid']],_['pick'](_0x4a4181,[_0x8554('0x43'),_0x8554('0x44')]));}};function updateStatus(_0x3d0cbd){if(_0x3d0cbd[_0x8554('0x45')]){faxMessage[_0x8554('0x46')](_0x3d0cbd[_0x8554('0x45')],{'failMessage':_0x8554('0x47')})[_0x8554('0x3d')](function(_0x567630){ut[_0x8554('0x48')](_0x8554('0x49'),_0x567630);})[_0x8554('0x3f')](function(_0xac8512){ut[_0x8554('0x48')](_0x8554('0x4a'),_0xac8512);});}else{ut[_0x8554('0x48')](_0x8554('0x4b'));}}Fax[_0x8554('0x20')][_0x8554('0x4c')]=function(_0x3f476d,_0x3093cd,_0x38fbda){var _0x327189=this;var _0x36e49a={};if(_0x3093cd){_0x36e49a={'read':_0x3093cd};}else{_0x36e49a={'failMessage':_0x38fbda};}if(this['actions'][_0x3f476d][_0x8554('0x45')]){faxMessage['update'](this['actions'][_0x3f476d][_0x8554('0x45')],_0x36e49a)[_0x8554('0x3d')](function(_0x336f24){ut[_0x8554('0x48')]('[Fax][Message][Updated]',_0x336f24);})['catch'](function(_0x138742){ut[_0x8554('0x48')](_0x8554('0x4d'),_0x138742);})[_0x8554('0x4e')](function(){delete _0x327189['channels'][_0x3f476d];delete _0x327189[_0x8554('0x4f')][_0x3f476d];});}else{ut[_0x8554('0x48')]('[Fax][Message][NoUpdate2]');delete this[_0x8554('0xf')][_0x3f476d];delete this[_0x8554('0x4f')][_0x3f476d];}};Fax['prototype'][_0x8554('0x1d')]=function(_0x1ff127){var _0x358c37=this;if(this['channels'][_0x1ff127['uniqueid']]){if(this[_0x8554('0x4f')][_0x1ff127['uniqueid']]){this['channels'][_0x1ff127[_0x8554('0x27')]]['id']=this[_0x8554('0x4f')][_0x1ff127[_0x8554('0x27')]]['id'];this[_0x8554('0xf')][_0x1ff127[_0x8554('0x27')]][_0x8554('0x29')]=this[_0x8554('0x4f')][_0x1ff127[_0x8554('0x27')]]['name'];}logger[_0x8554('0x2b')](_0x1ff127[_0x8554('0x27')],this[_0x8554('0x4f')][_0x1ff127['uniqueid']]?'\x20hangup\x20sendFile\x20':_0x8554('0x50'),JSON[_0x8554('0x41')](this[_0x8554('0xf')][_0x1ff127['uniqueid']]));if(this[_0x8554('0x4f')][_0x1ff127['uniqueid']]){if(this[_0x8554('0xf')][_0x1ff127[_0x8554('0x27')]][_0x8554('0x51')]>0x0&&this[_0x8554('0xf')][_0x1ff127[_0x8554('0x27')]][_0x8554('0x19')]===_0x8554('0x52')){this['updateStatus'](_0x1ff127[_0x8554('0x27')],!![]);}else{this[_0x8554('0x4c')](_0x1ff127[_0x8554('0x27')],![],this[_0x8554('0xf')][_0x1ff127[_0x8554('0x27')]][_0x8554('0x53')]?_0x8554('0x54')+this['channels'][_0x1ff127[_0x8554('0x27')]][_0x8554('0x53')]:_0x8554('0x55'));}}}};Fax[_0x8554('0x20')][_0x8554('0x56')]=function(_0x98f9a6){var _0x3c0b1c=this;return new BPromise(function(_0x5b3661,_0x4bf777){_0x3c0b1c[_0x8554('0x57')](_0x98f9a6,_0x3c0b1c[_0x8554('0x10')][_0x98f9a6[_0x8554('0x58')]])[_0x8554('0x3d')](function(_0x59a3bc){ut['logger'](_0x8554('0x59'),_0x59a3bc);_0x5b3661(ut['message']('Fax\x20received'));})[_0x8554('0x3f')](function(_0x3d90ea){ut['logger'](_0x8554('0x5a'),_0x3d90ea);updateStatus(_0x98f9a6);_0x4bf777(ut['message'](_0x8554('0x5b')));});});};Fax[_0x8554('0x20')][_0x8554('0x5c')]=function(_0x20cbad){var _0x52a902=this;return new BPromise(function(_0x5972b0,_0x41f6c5){var _0x7a1a3e=_0x52a902[_0x8554('0x5d')](_0x52a902[_0x8554('0x10')][_0x20cbad['FaxAccountId']]['Trunk'],_0x20cbad);if(_0x7a1a3e[_0x8554('0x5e')]){_0x5972b0(ut[_0x8554('0x5f')](_0x8554('0x60')));}else{_0x41f6c5(ut[_0x8554('0x5f')](_0x7a1a3e[_0x8554('0x5f')]));}});};function originate(_0x1a00cc,_0x124a24,_0x216f65){return new BPromise(function(_0x33a18d,_0x4f30af){if(_0x216f65){var _0x156913=util['format'](_0x8554('0x61'),path[_0x8554('0x3b')](_0x1a00cc['path'])[_0x8554('0x29')],_0x1a00cc[_0x8554('0x4')]);gs[_0x8554('0x62')](_0x156913)[_0x8554('0x3d')](function(){_0x1a00cc['path']=util[_0x8554('0x63')](_0x8554('0x64'),path[_0x8554('0x3b')](_0x1a00cc[_0x8554('0x4')])[_0x8554('0x65')],path[_0x8554('0x3b')](_0x1a00cc['path'])[_0x8554('0x29')]);_0x124a24[_0x8554('0x66')]=_0x1a00cc[_0x8554('0x4')];ut[_0x8554('0x48')](_0x8554('0x67'),'');ami[_0x8554('0x68')](ut['variablesAction'](_0x1a00cc,_0x124a24))[_0x8554('0x3d')](function(_0x5a3c07){_0x33a18d(_0x5a3c07);})[_0x8554('0x3f')](function(_0x453344){_0x4f30af(_0x453344);});})['catch'](function(_0xbabd43){_0x4f30af(_0xbabd43);});}else{ami[_0x8554('0x68')](ut[_0x8554('0x69')](_0x1a00cc,_0x124a24))[_0x8554('0x3d')](function(_0x5413ff){_0x33a18d(_0x5413ff);})[_0x8554('0x3f')](function(_0x4ebbfb){_0x4f30af(_0x4ebbfb);});}});}Fax[_0x8554('0x20')]['execOriginate']=function(_0x47c644,_0x1a9f1e){var _0x5a9668=this;return new BPromise(function(_0xbcc17e,_0x16538b){var _0x359f16=uuid['v4']();var _0x444d85=ut[_0x8554('0x6a')](_0x47c644,_0x1a9f1e);_0x5a9668[_0x8554('0x4f')][_0x359f16]=new Action(_0x359f16,_0x1a9f1e,_0x47c644);var _0x5573cb=_0x5a9668[_0x8554('0x4f')][_0x359f16];if(path[_0x8554('0x3b')](_0x47c644[_0x8554('0x4')])['ext']===_0x8554('0x3e')){originate(_0x5573cb,_0x444d85,!![])[_0x8554('0x3d')](function(_0x53d15d){_0xbcc17e(_0x53d15d);})[_0x8554('0x3f')](function(_0x22b908){_0x16538b(_0x22b908);});}else{originate(_0x5573cb,_0x444d85,![])['then'](function(_0x18580b){_0xbcc17e(_0x18580b);})[_0x8554('0x3f')](function(_0x3aff78){_0x16538b(_0x3aff78);});}});};Fax[_0x8554('0x20')][_0x8554('0x6b')]=function(_0x5c818d){return!_[_0x8554('0x6c')](_0x5c818d)&&!_[_0x8554('0x6c')](_0x5c818d[_0x8554('0x29')])&&!_[_0x8554('0x6c')](_0x5c818d['active'])&&_0x5c818d[_0x8554('0x6d')]&&!_[_0x8554('0x6c')](this[_0x8554('0x11')][util[_0x8554('0x63')](_0x8554('0x6e'),_0x5c818d[_0x8554('0x29')])])&&!_[_0x8554('0x6c')](this[_0x8554('0x11')][util['format']('SIP/%s',_0x5c818d[_0x8554('0x29')])]['status'])&&(this[_0x8554('0x11')][util[_0x8554('0x63')](_0x8554('0x6e'),_0x5c818d[_0x8554('0x29')])][_0x8554('0x32')]==='reachable'||this['trunks'][util['format']('SIP/%s',_0x5c818d[_0x8554('0x29')])]['status']==='unmonitored');};Fax[_0x8554('0x20')][_0x8554('0x5d')]=function(_0x4179d4,_0x3df107){if(!this['isTrunkReachable'](_0x4179d4)){ut[_0x8554('0x48')]('[Fax][checkTrunk][Trunk\x20not\x20working\x20correctly]',_['isNil'](_0x4179d4)?'':_0x4179d4[_0x8554('0x29')]+_0x8554('0x6f'));return{'isValid':![],'message':_0x8554('0x70')};}if(_['isNil'](_0x3df107['fax'])){ut[_0x8554('0x48')]('[Fax][checkTrunk][Empty\x20Fax]',JSON[_0x8554('0x41')](_0x3df107));return{'isValid':![],'message':_0x8554('0x71')};}if(_['isNil'](_0x3df107[_0x8554('0x4')])){ut[_0x8554('0x48')]('[Fax][checkTrunk][Empty\x20path]',JSON[_0x8554('0x41')](_0x3df107));return{'isValid':![],'message':_0x8554('0x72')};}return{'isValid':!![],'message':_0x8554('0x73')};};module['exports']=Fax;