99e401d14b77227895a41269786d785d3e90c03a
[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 _0x0ab5=['stringify','faxstatusstring','SENDING\x20FAILED','send','execOriginate','[Fax][execOriginate][success]','[Fax][execOriginate][failure]','Fax\x20failed','checkAfterSend','checkTrunk','isValid','Trunk\x20Reachable','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','execute','dir','XMF-PATH','Action','variablesAction','addVariables','ext','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','exports','util','lodash','bluebird','moment','path','tiff2pdf','fax','../ami','./rpcDb','./util/util','./action','../rpc/faxAccount','../rpc/attachment','../rpc/faxMessage','channels','faxAccounts','trunks','actions','newchannel','bind','varset','syncVarSet','newstate','syncNewState','syncNewExten','faxstatus','receivefax','syncReceiveFax','hangup','originateresponse','prototype','syncNewchannel','from-voip-provider','hasOwnProperty','localstationid','uniqueid','channel','name','info','\x20syncNewchannel\x20','variable','value','channelstate','channelstatedesc','syncFaxStatus','operation','status','filename','\x20syncFaxStatus\x20','remotestationid','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','message','create','parse','.pdf','application/pdf','then','catch','\x20notify\x20','error','\x20receiveFax\x20','syncOriginateResponse','pick','response','reason','AttachmentId','update','SENDING\x20FAILED\x20','logger','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','updateStatus','finally','[Fax][Message][NoUpdate2]','syncHangup','\x20hangup\x20sendFile\x20','\x20hangup\x20faxRiceive\x20'];(function(_0xb511c4,_0x5532ba){var _0x5b539d=function(_0xbc61d8){while(--_0xbc61d8){_0xb511c4['push'](_0xb511c4['shift']());}};_0x5b539d(++_0x5532ba);}(_0x0ab5,0x97));var _0x50ab=function(_0x446186,_0x116e31){_0x446186=_0x446186-0x0;var _0x63b564=_0x0ab5[_0x446186];return _0x63b564;};'use strict';var util=require(_0x50ab('0x0'));var _=require(_0x50ab('0x1'));var BPromise=require(_0x50ab('0x2'));var uuid=require('uuid');var moment=require(_0x50ab('0x3'));var path=require(_0x50ab('0x4'));var tiff2pdf=require(_0x50ab('0x5'));var gs=require('./util/gs');var logger=require('../../../config/logger')(_0x50ab('0x6'));var ami=require(_0x50ab('0x7'));var rpcDb=require(_0x50ab('0x8'));var Channel=require('./channel');var ut=require(_0x50ab('0x9'));var Action=require(_0x50ab('0xa'));var faxAccount=require(_0x50ab('0xb'));var attachment=require(_0x50ab('0xc'));var faxMessage=require(_0x50ab('0xd'));function Fax(_0x4f4353){this[_0x50ab('0xe')]={};this[_0x50ab('0xf')]=_0x4f4353['faxAccounts'];this[_0x50ab('0x10')]=_0x4f4353['trunks'];this[_0x50ab('0x11')]={};ami['on'](_0x50ab('0x12'),this['syncNewchannel'][_0x50ab('0x13')](this));ami['on'](_0x50ab('0x14'),this[_0x50ab('0x15')][_0x50ab('0x13')](this));ami['on'](_0x50ab('0x16'),this[_0x50ab('0x17')]['bind'](this));ami['on']('newexten',this[_0x50ab('0x18')]['bind'](this));ami['on'](_0x50ab('0x19'),this['syncFaxStatus'][_0x50ab('0x13')](this));ami['on'](_0x50ab('0x1a'),this[_0x50ab('0x1b')][_0x50ab('0x13')](this));ami['on'](_0x50ab('0x1c'),this['syncHangup']['bind'](this));ami['on'](_0x50ab('0x1d'),this['syncOriginateResponse']['bind'](this));}Fax[_0x50ab('0x1e')][_0x50ab('0x1f')]=function(_0x597e7d){if(_0x597e7d['context']===_0x50ab('0x20')){for(var _0x4f2051 in this[_0x50ab('0xf')]){if(this[_0x50ab('0xf')][_0x50ab('0x21')](_0x4f2051)){if(_0x597e7d['exten']===this[_0x50ab('0xf')][_0x4f2051][_0x50ab('0x22')]){this[_0x50ab('0xe')][_0x597e7d[_0x50ab('0x23')]]={};this[_0x50ab('0xe')][_0x597e7d['uniqueid']][_0x50ab('0x24')]=_0x597e7d[_0x50ab('0x24')];this[_0x50ab('0xe')][_0x597e7d['uniqueid']]['id']=this[_0x50ab('0xf')][_0x4f2051]['id'];this['channels'][_0x597e7d[_0x50ab('0x23')]][_0x50ab('0x25')]=this['faxAccounts'][_0x4f2051]['name'];logger[_0x50ab('0x26')](_0x597e7d['uniqueid'],_0x50ab('0x27'));}}}}if(this[_0x50ab('0x11')][_0x597e7d[_0x50ab('0x23')]]){this[_0x50ab('0xe')][_0x597e7d[_0x50ab('0x23')]]={};this[_0x50ab('0xe')][_0x597e7d[_0x50ab('0x23')]][_0x50ab('0x24')]=_0x597e7d[_0x50ab('0x24')];logger[_0x50ab('0x26')](_0x597e7d[_0x50ab('0x23')],_0x50ab('0x27'));}};Fax['prototype'][_0x50ab('0x15')]=function(_0xd81226){if(this[_0x50ab('0xe')][_0xd81226[_0x50ab('0x23')]]){for(var _0x48656a in _0xd81226[_0x50ab('0x28')]){if(_0xd81226[_0x50ab('0x28')][_0x50ab('0x21')](_0x48656a)){this['channels'][_0xd81226[_0x50ab('0x23')]][_0x48656a]=_0xd81226[_0x50ab('0x29')];}}}};Fax[_0x50ab('0x1e')][_0x50ab('0x17')]=function(_0x4006b1){if(this['channels'][_0x4006b1['uniqueid']]){this[_0x50ab('0xe')][_0x4006b1[_0x50ab('0x23')]][_0x50ab('0x2a')]=_0x4006b1[_0x50ab('0x2a')];this[_0x50ab('0xe')][_0x4006b1[_0x50ab('0x23')]][_0x50ab('0x2b')]=_0x4006b1[_0x50ab('0x2b')];}};Fax[_0x50ab('0x1e')][_0x50ab('0x18')]=function(_0x1fe77b){if(this['channels'][_0x1fe77b['uniqueid']]){for(var _0x2b581b in _0x1fe77b['variable']){if(_0x1fe77b[_0x50ab('0x28')]['hasOwnProperty'](_0x2b581b)){this['channels'][_0x1fe77b['uniqueid']][_0x2b581b]=_0x1fe77b['value'];}}}};Fax[_0x50ab('0x1e')][_0x50ab('0x2c')]=function(_0x1b4155){if(this[_0x50ab('0xe')][_0x1b4155[_0x50ab('0x23')]]){this[_0x50ab('0xe')][_0x1b4155[_0x50ab('0x23')]]['operation']=_0x1b4155[_0x50ab('0x2d')];this['channels'][_0x1b4155['uniqueid']]['status']=_0x1b4155[_0x50ab('0x2e')];this[_0x50ab('0xe')][_0x1b4155[_0x50ab('0x23')]][_0x50ab('0x22')]=_0x1b4155[_0x50ab('0x22')];this[_0x50ab('0xe')][_0x1b4155['uniqueid']][_0x50ab('0x2f')]=_0x1b4155[_0x50ab('0x2f')];logger[_0x50ab('0x26')](_0x1b4155['uniqueid'],_0x50ab('0x30'));}};Fax[_0x50ab('0x1e')][_0x50ab('0x1b')]=function(_0x260a34){var _0x589959=this;if(this[_0x50ab('0xe')][_0x260a34[_0x50ab('0x23')]]){var _0x4c9f6d=this[_0x50ab('0xe')][_0x260a34[_0x50ab('0x23')]]['id'];this['channels'][_0x260a34[_0x50ab('0x23')]]['remotestationid']=_0x260a34[_0x50ab('0x31')];this[_0x50ab('0xe')][_0x260a34['uniqueid']][_0x50ab('0x32')]=_0x260a34[_0x50ab('0x32')];this[_0x50ab('0xe')][_0x260a34[_0x50ab('0x23')]]['localstationid']=_0x260a34[_0x50ab('0x22')];this[_0x50ab('0xe')][_0x260a34[_0x50ab('0x23')]][_0x50ab('0x33')]=_0x260a34[_0x50ab('0x33')];this['channels'][_0x260a34[_0x50ab('0x23')]][_0x50ab('0x34')]=_0x260a34[_0x50ab('0x34')];this['channels'][_0x260a34['uniqueid']][_0x50ab('0x2f')]=_0x260a34[_0x50ab('0x2f')];this[_0x50ab('0xe')][_0x260a34[_0x50ab('0x23')]][_0x50ab('0x35')]=_0x260a34[_0x50ab('0x35')];if(this['channels'][_0x260a34['uniqueid']][_0x50ab('0x32')]>0x0){tiff2pdf(_0x260a34['filename'],_0x50ab('0x36'),function(_0x156fa3){logger['info'](_0x260a34['uniqueid'],'\x20result\x20conversion\x20file\x20',_0x156fa3[_0x50ab('0x37')]);attachment[_0x50ab('0x38')]({'name':path[_0x50ab('0x39')](_0x260a34[_0x50ab('0x2f')])[_0x50ab('0x25')],'basename':path[_0x50ab('0x39')](_0x260a34['filename'])[_0x50ab('0x25')]+_0x50ab('0x3a'),'type':_0x50ab('0x3b')})[_0x50ab('0x3c')](function(_0x3f8446){faxAccount['notify'](_0x4c9f6d,{'from':_0x260a34[_0x50ab('0x35')],'mapKey':_0x50ab('0x6'),'body':path['parse'](_0x260a34['filename'])[_0x50ab('0x25')]+_0x50ab('0x3a'),'AttachmentId':_0x3f8446['id']})[_0x50ab('0x3d')](function(_0x432f8d){logger['error'](_0x260a34[_0x50ab('0x23')],_0x50ab('0x3e'),JSON['stringify'](_0x432f8d));});})[_0x50ab('0x3d')](function(_0x37c9e1){logger[_0x50ab('0x3f')](_0x260a34[_0x50ab('0x23')],_0x50ab('0x40'),JSON['stringify'](_0x37c9e1));});});}logger[_0x50ab('0x26')](_0x260a34[_0x50ab('0x23')],_0x50ab('0x40'));}};Fax['prototype'][_0x50ab('0x41')]=function(_0x5afcbb){if(this[_0x50ab('0x11')][_0x5afcbb[_0x50ab('0x23')]]){_['merge'](this[_0x50ab('0xe')][_0x5afcbb[_0x50ab('0x23')]],_[_0x50ab('0x42')](_0x5afcbb,[_0x50ab('0x43'),_0x50ab('0x44')]));}};function updateStatus(_0x57c8a0){if(_0x57c8a0[_0x50ab('0x45')]){faxMessage[_0x50ab('0x46')](_0x57c8a0['AttachmentId'],{'failMessage':_0x50ab('0x47')})[_0x50ab('0x3c')](function(_0x140054){ut[_0x50ab('0x48')]('[Fax][Message][Error][Updated]',_0x140054);})[_0x50ab('0x3d')](function(_0x4d158c){ut['logger'](_0x50ab('0x49'),_0x4d158c);});}else{ut[_0x50ab('0x48')](_0x50ab('0x4a'));}}Fax[_0x50ab('0x1e')][_0x50ab('0x4b')]=function(_0x212a9b,_0x52062d,_0x57ffa2){var _0x59bb9e=this;var _0x1523d7={};if(_0x52062d){_0x1523d7={'read':_0x52062d};}else{_0x1523d7={'failMessage':_0x57ffa2};}if(this[_0x50ab('0x11')][_0x212a9b]['AttachmentId']){faxMessage[_0x50ab('0x46')](this[_0x50ab('0x11')][_0x212a9b][_0x50ab('0x45')],_0x1523d7)[_0x50ab('0x3c')](function(_0x23c5b8){ut['logger']('[Fax][Message][Updated]',_0x23c5b8);})[_0x50ab('0x3d')](function(_0x5107dd){ut['logger']('[Fax][Message][NoUpdate1]',_0x5107dd);})[_0x50ab('0x4c')](function(){delete _0x59bb9e[_0x50ab('0xe')][_0x212a9b];delete _0x59bb9e['actions'][_0x212a9b];});}else{ut['logger'](_0x50ab('0x4d'));delete this['channels'][_0x212a9b];delete this[_0x50ab('0x11')][_0x212a9b];}};Fax[_0x50ab('0x1e')][_0x50ab('0x4e')]=function(_0x7e00d7){var _0x24b63f=this;if(this['channels'][_0x7e00d7['uniqueid']]){if(this[_0x50ab('0x11')][_0x7e00d7[_0x50ab('0x23')]]){this[_0x50ab('0xe')][_0x7e00d7[_0x50ab('0x23')]]['id']=this[_0x50ab('0x11')][_0x7e00d7[_0x50ab('0x23')]]['id'];this[_0x50ab('0xe')][_0x7e00d7[_0x50ab('0x23')]]['name']=this['actions'][_0x7e00d7[_0x50ab('0x23')]][_0x50ab('0x25')];}logger['info'](_0x7e00d7[_0x50ab('0x23')],this[_0x50ab('0x11')][_0x7e00d7[_0x50ab('0x23')]]?_0x50ab('0x4f'):_0x50ab('0x50'),JSON[_0x50ab('0x51')](this[_0x50ab('0xe')][_0x7e00d7[_0x50ab('0x23')]]));if(this['actions'][_0x7e00d7[_0x50ab('0x23')]]){if(this[_0x50ab('0xe')][_0x7e00d7[_0x50ab('0x23')]]['faxpages']>0x0&&this[_0x50ab('0xe')][_0x7e00d7[_0x50ab('0x23')]][_0x50ab('0x19')]==='SUCCESS'){this[_0x50ab('0x4b')](_0x7e00d7['uniqueid'],!![]);}else{this[_0x50ab('0x4b')](_0x7e00d7['uniqueid'],![],this[_0x50ab('0xe')][_0x7e00d7[_0x50ab('0x23')]][_0x50ab('0x52')]?'SENDING\x20FAILED:\x20'+this[_0x50ab('0xe')][_0x7e00d7['uniqueid']][_0x50ab('0x52')]:_0x50ab('0x53'));}}}};Fax[_0x50ab('0x1e')][_0x50ab('0x54')]=function(_0x5c415a){var _0x3ff679=this;return new BPromise(function(_0x31f6d0,_0x166600){_0x3ff679[_0x50ab('0x55')](_0x5c415a,_0x3ff679[_0x50ab('0xf')][_0x5c415a['FaxAccountId']])[_0x50ab('0x3c')](function(_0x16de40){ut['logger'](_0x50ab('0x56'),_0x16de40);_0x31f6d0(ut[_0x50ab('0x37')]('Fax\x20received'));})[_0x50ab('0x3d')](function(_0x35c79b){ut[_0x50ab('0x48')](_0x50ab('0x57'),_0x35c79b);updateStatus(_0x5c415a);_0x166600(ut[_0x50ab('0x37')](_0x50ab('0x58')));});});};Fax[_0x50ab('0x1e')][_0x50ab('0x59')]=function(_0x4735d9){var _0x7e0ad=this;return new BPromise(function(_0x1d9669,_0x211a81){var _0x5a2b2=_0x7e0ad[_0x50ab('0x5a')](_0x7e0ad[_0x50ab('0xf')][_0x4735d9['FaxAccountId']]['Trunk'],_0x4735d9);if(_0x5a2b2[_0x50ab('0x5b')]){_0x1d9669(ut[_0x50ab('0x37')](_0x50ab('0x5c')));}else{_0x211a81(ut[_0x50ab('0x37')](_0x5a2b2[_0x50ab('0x37')]));}});};function originate(_0x26334d,_0x1a9bef,_0x11ab7e){return new BPromise(function(_0x1058a0,_0x1cd670){if(_0x11ab7e){var _0x2a92e1=util[_0x50ab('0x5d')](_0x50ab('0x5e'),path[_0x50ab('0x39')](_0x26334d['path'])[_0x50ab('0x25')],_0x26334d[_0x50ab('0x4')]);gs[_0x50ab('0x5f')](_0x2a92e1)['then'](function(){_0x26334d['path']=util[_0x50ab('0x5d')]('%s/%s.tif',path[_0x50ab('0x39')](_0x26334d['path'])[_0x50ab('0x60')],path[_0x50ab('0x39')](_0x26334d[_0x50ab('0x4')])[_0x50ab('0x25')]);_0x1a9bef[_0x50ab('0x61')]=_0x26334d[_0x50ab('0x4')];ut[_0x50ab('0x48')]('Convert\x20pdf\x20to\x20tif','');ami[_0x50ab('0x62')](ut[_0x50ab('0x63')](_0x26334d,_0x1a9bef))[_0x50ab('0x3c')](function(_0x4892e8){_0x1058a0(_0x4892e8);})[_0x50ab('0x3d')](function(_0x2c8abc){_0x1cd670(_0x2c8abc);});})['catch'](function(_0x4a409c){_0x1cd670(_0x4a409c);});}else{ami['Action'](ut[_0x50ab('0x63')](_0x26334d,_0x1a9bef))[_0x50ab('0x3c')](function(_0x5e3b2c){_0x1058a0(_0x5e3b2c);})[_0x50ab('0x3d')](function(_0x170082){_0x1cd670(_0x170082);});}});}Fax[_0x50ab('0x1e')]['execOriginate']=function(_0x52a1e4,_0x374722){var _0x14594d=this;return new BPromise(function(_0x14a2a6,_0x189788){var _0x5bf411=uuid['v4']();var _0x3f974c=ut[_0x50ab('0x64')](_0x52a1e4,_0x374722);_0x14594d[_0x50ab('0x11')][_0x5bf411]=new Action(_0x5bf411,_0x374722,_0x52a1e4);var _0x2d6810=_0x14594d[_0x50ab('0x11')][_0x5bf411];if(path[_0x50ab('0x39')](_0x52a1e4['path'])[_0x50ab('0x65')]===_0x50ab('0x3a')){originate(_0x2d6810,_0x3f974c,!![])[_0x50ab('0x3c')](function(_0x29aa24){_0x14a2a6(_0x29aa24);})['catch'](function(_0x1db27c){_0x189788(_0x1db27c);});}else{originate(_0x2d6810,_0x3f974c,![])[_0x50ab('0x3c')](function(_0x40da41){_0x14a2a6(_0x40da41);})[_0x50ab('0x3d')](function(_0x55ef99){_0x189788(_0x55ef99);});}});};Fax[_0x50ab('0x1e')][_0x50ab('0x66')]=function(_0x1ee59d){return!_[_0x50ab('0x67')](_0x1ee59d)&&!_[_0x50ab('0x67')](_0x1ee59d['name'])&&!_[_0x50ab('0x67')](_0x1ee59d[_0x50ab('0x68')])&&_0x1ee59d[_0x50ab('0x68')]&&!_[_0x50ab('0x67')](this[_0x50ab('0x10')][util[_0x50ab('0x5d')](_0x50ab('0x69'),_0x1ee59d[_0x50ab('0x25')])])&&!_[_0x50ab('0x67')](this[_0x50ab('0x10')][util['format'](_0x50ab('0x69'),_0x1ee59d['name'])]['status'])&&(this[_0x50ab('0x10')][util['format']('SIP/%s',_0x1ee59d[_0x50ab('0x25')])][_0x50ab('0x2e')]===_0x50ab('0x6a')||this['trunks'][util[_0x50ab('0x5d')](_0x50ab('0x69'),_0x1ee59d[_0x50ab('0x25')])][_0x50ab('0x2e')]===_0x50ab('0x6b'));};Fax[_0x50ab('0x1e')][_0x50ab('0x5a')]=function(_0x5a2712,_0x3c94aa){if(!this['isTrunkReachable'](_0x5a2712)){ut['logger'](_0x50ab('0x6c'),_['isNil'](_0x5a2712)?'':_0x5a2712[_0x50ab('0x25')]+_0x50ab('0x6d'));return{'isValid':![],'message':_0x50ab('0x6e')};}if(_[_0x50ab('0x67')](_0x3c94aa[_0x50ab('0x6')])){ut[_0x50ab('0x48')](_0x50ab('0x6f'),JSON['stringify'](_0x3c94aa));return{'isValid':![],'message':_0x50ab('0x70')};}if(_[_0x50ab('0x67')](_0x3c94aa[_0x50ab('0x4')])){ut[_0x50ab('0x48')](_0x50ab('0x71'),JSON['stringify'](_0x3c94aa));return{'isValid':![],'message':_0x50ab('0x72')};}return{'isValid':!![],'message':'Is\x20Valid'};};module[_0x50ab('0x73')]=Fax;