Built motion from commit (unavailable).|2.3.4
[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 _0x8ab5=['finally','[Fax][Message][NoUpdate2]','\x20hangup\x20sendFile\x20','faxpages','faxstatus','SUCCESS','SENDING\x20FAILED:\x20','faxstatusstring','SENDING\x20FAILED','execOriginate','[Fax][execOriginate][success]','Fax\x20received','[Fax][execOriginate][failure]','checkTrunk','FaxAccountId','Trunk','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','%s/%s.tif','dir','XMF-PATH','Action','variablesAction','ext','isTrunkReachable','isNil','SIP/%s','reachable','unmonitored','[Fax][checkTrunk][Trunk\x20not\x20work]','\x20Trunk\x20is\x20not\x20active','[Fax][checkTrunk][Empty\x20Fax]','[Fax][checkTrunk][Empty\x20path]','Empty\x20path','Is\x20Valid','exports','util','lodash','bluebird','uuid','path','tiff2pdf','./util/gs','../../../config/logger','fax','./channel','./util/util','./action','../rpc/faxAccount','../rpc/attachment','../rpc/faxMessage','channels','faxAccounts','trunks','actions','newchannel','syncNewchannel','bind','varset','syncVarSet','newstate','newexten','syncNewExten','syncFaxStatus','syncReceiveFax','hangup','syncHangup','syncOriginateResponse','prototype','context','from-voip-provider','hasOwnProperty','exten','uniqueid','name','info','\x20syncNewchannel\x20','channel','variable','value','syncNewState','channelstate','channelstatedesc','operation','status','localstationid','filename','\x20syncFaxStatus\x20','remotestationid','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','\x20result\x20conversion\x20file\x20','message','create','parse','.pdf','error','\x20receiveFax\x20','stringify','merge','pick','reason','AttachmentId','SENDING\x20FAILED\x20','logger','catch','[Fax][Message][Error][NoUpdate1]','updateStatus','update','then','[Fax][Message][NoUpdate1]'];(function(_0x20d057,_0x1f77a9){var _0x1694df=function(_0x436b50){while(--_0x436b50){_0x20d057['push'](_0x20d057['shift']());}};_0x1694df(++_0x1f77a9);}(_0x8ab5,0x98));var _0x58ab=function(_0x5c87b8,_0x7d5e66){_0x5c87b8=_0x5c87b8-0x0;var _0xe21122=_0x8ab5[_0x5c87b8];return _0xe21122;};'use strict';var util=require(_0x58ab('0x0'));var _=require(_0x58ab('0x1'));var BPromise=require(_0x58ab('0x2'));var uuid=require(_0x58ab('0x3'));var moment=require('moment');var path=require(_0x58ab('0x4'));var tiff2pdf=require(_0x58ab('0x5'));var gs=require(_0x58ab('0x6'));var logger=require(_0x58ab('0x7'))(_0x58ab('0x8'));var ami=require('../ami');var rpcDb=require('./rpcDb');var Channel=require(_0x58ab('0x9'));var ut=require(_0x58ab('0xa'));var Action=require(_0x58ab('0xb'));var faxAccount=require(_0x58ab('0xc'));var attachment=require(_0x58ab('0xd'));var faxMessage=require(_0x58ab('0xe'));function Fax(_0x2e4f6e){this[_0x58ab('0xf')]={};this['faxAccounts']=_0x2e4f6e[_0x58ab('0x10')];this['trunks']=_0x2e4f6e[_0x58ab('0x11')];this[_0x58ab('0x12')]={};ami['on'](_0x58ab('0x13'),this[_0x58ab('0x14')][_0x58ab('0x15')](this));ami['on'](_0x58ab('0x16'),this[_0x58ab('0x17')][_0x58ab('0x15')](this));ami['on'](_0x58ab('0x18'),this['syncNewState'][_0x58ab('0x15')](this));ami['on'](_0x58ab('0x19'),this[_0x58ab('0x1a')][_0x58ab('0x15')](this));ami['on']('faxstatus',this[_0x58ab('0x1b')][_0x58ab('0x15')](this));ami['on']('receivefax',this[_0x58ab('0x1c')][_0x58ab('0x15')](this));ami['on'](_0x58ab('0x1d'),this[_0x58ab('0x1e')]['bind'](this));ami['on']('originateresponse',this[_0x58ab('0x1f')][_0x58ab('0x15')](this));}Fax[_0x58ab('0x20')][_0x58ab('0x14')]=function(_0x1f0573){if(_0x1f0573[_0x58ab('0x21')]===_0x58ab('0x22')){for(var _0x2fb286 in this[_0x58ab('0x10')]){if(this[_0x58ab('0x10')][_0x58ab('0x23')](_0x2fb286)){if(_0x1f0573[_0x58ab('0x24')]===this[_0x58ab('0x10')][_0x2fb286]['localstationid']){this['channels'][_0x1f0573[_0x58ab('0x25')]]={};this['channels'][_0x1f0573[_0x58ab('0x25')]]['channel']=_0x1f0573['channel'];this[_0x58ab('0xf')][_0x1f0573[_0x58ab('0x25')]]['id']=this[_0x58ab('0x10')][_0x2fb286]['id'];this[_0x58ab('0xf')][_0x1f0573[_0x58ab('0x25')]][_0x58ab('0x26')]=this[_0x58ab('0x10')][_0x2fb286][_0x58ab('0x26')];logger[_0x58ab('0x27')](_0x1f0573[_0x58ab('0x25')],_0x58ab('0x28'));}}}}if(this[_0x58ab('0x12')][_0x1f0573[_0x58ab('0x25')]]){this[_0x58ab('0xf')][_0x1f0573[_0x58ab('0x25')]]={};this['channels'][_0x1f0573[_0x58ab('0x25')]][_0x58ab('0x29')]=_0x1f0573[_0x58ab('0x29')];logger['info'](_0x1f0573['uniqueid'],_0x58ab('0x28'));}};Fax[_0x58ab('0x20')][_0x58ab('0x17')]=function(_0x14ac9a){if(this[_0x58ab('0xf')][_0x14ac9a[_0x58ab('0x25')]]){for(var _0x194bb7 in _0x14ac9a['variable']){if(_0x14ac9a[_0x58ab('0x2a')]['hasOwnProperty'](_0x194bb7)){this['channels'][_0x14ac9a[_0x58ab('0x25')]][_0x194bb7]=_0x14ac9a[_0x58ab('0x2b')];}}}};Fax[_0x58ab('0x20')][_0x58ab('0x2c')]=function(_0x242e30){if(this[_0x58ab('0xf')][_0x242e30[_0x58ab('0x25')]]){this[_0x58ab('0xf')][_0x242e30[_0x58ab('0x25')]][_0x58ab('0x2d')]=_0x242e30[_0x58ab('0x2d')];this[_0x58ab('0xf')][_0x242e30[_0x58ab('0x25')]][_0x58ab('0x2e')]=_0x242e30['channelstatedesc'];}};Fax[_0x58ab('0x20')]['syncNewExten']=function(_0x338ee0){if(this[_0x58ab('0xf')][_0x338ee0[_0x58ab('0x25')]]){for(var _0x379377 in _0x338ee0['variable']){if(_0x338ee0[_0x58ab('0x2a')][_0x58ab('0x23')](_0x379377)){this[_0x58ab('0xf')][_0x338ee0['uniqueid']][_0x379377]=_0x338ee0[_0x58ab('0x2b')];}}}};Fax[_0x58ab('0x20')][_0x58ab('0x1b')]=function(_0xfcba55){if(this[_0x58ab('0xf')][_0xfcba55[_0x58ab('0x25')]]){this[_0x58ab('0xf')][_0xfcba55['uniqueid']]['operation']=_0xfcba55[_0x58ab('0x2f')];this[_0x58ab('0xf')][_0xfcba55[_0x58ab('0x25')]][_0x58ab('0x30')]=_0xfcba55[_0x58ab('0x30')];this['channels'][_0xfcba55[_0x58ab('0x25')]][_0x58ab('0x31')]=_0xfcba55[_0x58ab('0x31')];this[_0x58ab('0xf')][_0xfcba55[_0x58ab('0x25')]][_0x58ab('0x32')]=_0xfcba55[_0x58ab('0x32')];logger[_0x58ab('0x27')](_0xfcba55['uniqueid'],_0x58ab('0x33'));}};Fax['prototype']['syncReceiveFax']=function(_0x5479e0){var _0x5a6adc=this;if(this[_0x58ab('0xf')][_0x5479e0[_0x58ab('0x25')]]){var _0x2c8cb4=this[_0x58ab('0xf')][_0x5479e0[_0x58ab('0x25')]]['id'];this[_0x58ab('0xf')][_0x5479e0[_0x58ab('0x25')]][_0x58ab('0x34')]=_0x5479e0[_0x58ab('0x34')];this[_0x58ab('0xf')][_0x5479e0[_0x58ab('0x25')]][_0x58ab('0x35')]=_0x5479e0[_0x58ab('0x35')];this[_0x58ab('0xf')][_0x5479e0[_0x58ab('0x25')]][_0x58ab('0x31')]=_0x5479e0[_0x58ab('0x31')];this[_0x58ab('0xf')][_0x5479e0['uniqueid']][_0x58ab('0x36')]=_0x5479e0[_0x58ab('0x36')];this[_0x58ab('0xf')][_0x5479e0[_0x58ab('0x25')]][_0x58ab('0x37')]=_0x5479e0[_0x58ab('0x37')];this[_0x58ab('0xf')][_0x5479e0[_0x58ab('0x25')]][_0x58ab('0x32')]=_0x5479e0['filename'];this[_0x58ab('0xf')][_0x5479e0[_0x58ab('0x25')]][_0x58ab('0x38')]=_0x5479e0[_0x58ab('0x38')];if(this[_0x58ab('0xf')][_0x5479e0[_0x58ab('0x25')]][_0x58ab('0x35')]>0x0){tiff2pdf(_0x5479e0[_0x58ab('0x32')],_0x58ab('0x39'),function(_0x3ce97d){logger[_0x58ab('0x27')](_0x5479e0[_0x58ab('0x25')],_0x58ab('0x3a'),_0x3ce97d[_0x58ab('0x3b')]);attachment[_0x58ab('0x3c')]({'name':path[_0x58ab('0x3d')](_0x5479e0[_0x58ab('0x32')])['name'],'basename':path['parse'](_0x5479e0[_0x58ab('0x32')])[_0x58ab('0x26')]+'.pdf','type':'application/pdf'})['then'](function(_0x512d90){faxAccount['notify'](_0x2c8cb4,{'from':_0x5479e0['calleridnum'],'mapKey':_0x58ab('0x8'),'body':path['parse'](_0x5479e0[_0x58ab('0x32')])[_0x58ab('0x26')]+_0x58ab('0x3e'),'AttachmentId':_0x512d90['id']})['catch'](function(_0x1c63f4){logger['error'](_0x5479e0[_0x58ab('0x25')],'\x20notify\x20',JSON['stringify'](_0x1c63f4));});})['catch'](function(_0x4f3712){logger[_0x58ab('0x3f')](_0x5479e0['uniqueid'],_0x58ab('0x40'),JSON[_0x58ab('0x41')](_0x4f3712));});});}logger['info'](_0x5479e0['uniqueid'],_0x58ab('0x40'));}};Fax[_0x58ab('0x20')]['syncOriginateResponse']=function(_0x36b963){if(this['actions'][_0x36b963[_0x58ab('0x25')]]){_[_0x58ab('0x42')](this[_0x58ab('0xf')][_0x36b963[_0x58ab('0x25')]],_[_0x58ab('0x43')](_0x36b963,['response',_0x58ab('0x44')]));}};function updateStatus(_0x83ece5){if(_0x83ece5['AttachmentId']){faxMessage['update'](_0x83ece5[_0x58ab('0x45')],{'failMessage':_0x58ab('0x46')})['then'](function(_0x57a2c2){ut[_0x58ab('0x47')]('[Fax][Message][Error][Updated]',_0x57a2c2);})[_0x58ab('0x48')](function(_0x42a808){ut['logger'](_0x58ab('0x49'),_0x42a808);});}else{ut[_0x58ab('0x47')]('[Fax][Message][Error][NoUpdate2]');}}Fax[_0x58ab('0x20')][_0x58ab('0x4a')]=function(_0x33839d,_0x30e8fd,_0x499717){var _0x4f2196=this;var _0x6c0ef7={};if(_0x30e8fd){_0x6c0ef7={'read':_0x30e8fd};}else{_0x6c0ef7={'failMessage':_0x499717};}if(this[_0x58ab('0x12')][_0x33839d][_0x58ab('0x45')]){faxMessage[_0x58ab('0x4b')](this[_0x58ab('0x12')][_0x33839d]['AttachmentId'],_0x6c0ef7)[_0x58ab('0x4c')](function(_0x9c2a72){ut['logger']('[Fax][Message][Updated]',_0x9c2a72);})[_0x58ab('0x48')](function(_0x553670){ut[_0x58ab('0x47')](_0x58ab('0x4d'),_0x553670);})[_0x58ab('0x4e')](function(){delete _0x4f2196[_0x58ab('0xf')][_0x33839d];delete _0x4f2196[_0x58ab('0x12')][_0x33839d];});}else{ut[_0x58ab('0x47')](_0x58ab('0x4f'));delete this['channels'][_0x33839d];delete this[_0x58ab('0x12')][_0x33839d];}};Fax[_0x58ab('0x20')][_0x58ab('0x1e')]=function(_0x5b6745){var _0x57e632=this;if(this[_0x58ab('0xf')][_0x5b6745['uniqueid']]){if(this[_0x58ab('0x12')][_0x5b6745['uniqueid']]){this[_0x58ab('0xf')][_0x5b6745[_0x58ab('0x25')]]['id']=this['actions'][_0x5b6745[_0x58ab('0x25')]]['id'];this['channels'][_0x5b6745[_0x58ab('0x25')]][_0x58ab('0x26')]=this[_0x58ab('0x12')][_0x5b6745[_0x58ab('0x25')]]['name'];}logger[_0x58ab('0x27')](_0x5b6745['uniqueid'],this[_0x58ab('0x12')][_0x5b6745[_0x58ab('0x25')]]?_0x58ab('0x50'):'\x20hangup\x20faxRiceive\x20',JSON[_0x58ab('0x41')](this['channels'][_0x5b6745[_0x58ab('0x25')]]));if(this[_0x58ab('0x12')][_0x5b6745['uniqueid']]){if(this[_0x58ab('0xf')][_0x5b6745[_0x58ab('0x25')]][_0x58ab('0x51')]>0x0&&this[_0x58ab('0xf')][_0x5b6745[_0x58ab('0x25')]][_0x58ab('0x52')]===_0x58ab('0x53')){this[_0x58ab('0x4a')](_0x5b6745[_0x58ab('0x25')],!![]);}else{this[_0x58ab('0x4a')](_0x5b6745[_0x58ab('0x25')],![],this[_0x58ab('0xf')][_0x5b6745[_0x58ab('0x25')]]['faxstatusstring']?_0x58ab('0x54')+this[_0x58ab('0xf')][_0x5b6745['uniqueid']][_0x58ab('0x55')]:_0x58ab('0x56'));}}}};Fax[_0x58ab('0x20')]['send']=function(_0x2ff803){var _0x3d2155=this;return new BPromise(function(_0x48bd39,_0x1e238d){_0x3d2155[_0x58ab('0x57')](_0x2ff803,_0x3d2155[_0x58ab('0x10')][_0x2ff803['FaxAccountId']])['then'](function(_0x4875bc){ut['logger'](_0x58ab('0x58'),_0x4875bc);_0x48bd39(ut[_0x58ab('0x3b')](_0x58ab('0x59')));})[_0x58ab('0x48')](function(_0x16a412){ut[_0x58ab('0x47')](_0x58ab('0x5a'),_0x16a412);updateStatus(_0x2ff803);_0x1e238d(ut[_0x58ab('0x3b')]('Fax\x20failed'));});});};Fax[_0x58ab('0x20')]['checkAfterSend']=function(_0x36bdc4){var _0x3f1f6e=this;return new BPromise(function(_0x422f47,_0x1dd09c){var _0x460081=_0x3f1f6e[_0x58ab('0x5b')](_0x3f1f6e[_0x58ab('0x10')][_0x36bdc4[_0x58ab('0x5c')]][_0x58ab('0x5d')],_0x36bdc4);if(_0x460081['isValid']){_0x422f47(ut[_0x58ab('0x3b')]('Trunk\x20Reachable'));}else{_0x1dd09c(ut[_0x58ab('0x3b')](_0x460081[_0x58ab('0x3b')]));}});};function originate(_0x5781f7,_0x380ab5,_0x444cdb){return new BPromise(function(_0x13025e,_0xe69d07){if(_0x444cdb){var _0x5d1d6e=util[_0x58ab('0x5e')](_0x58ab('0x5f'),path[_0x58ab('0x3d')](_0x5781f7[_0x58ab('0x4')])['name'],_0x5781f7['path']);gs[_0x58ab('0x60')](_0x5d1d6e)['then'](function(){_0x5781f7[_0x58ab('0x4')]=util[_0x58ab('0x5e')](_0x58ab('0x61'),path[_0x58ab('0x3d')](_0x5781f7['path'])[_0x58ab('0x62')],path[_0x58ab('0x3d')](_0x5781f7[_0x58ab('0x4')])[_0x58ab('0x26')]);_0x380ab5[_0x58ab('0x63')]=_0x5781f7['path'];ut[_0x58ab('0x47')]('Convert\x20pdf\x20to\x20tif','');ami[_0x58ab('0x64')](ut[_0x58ab('0x65')](_0x5781f7,_0x380ab5))['then'](function(_0x6c4786){_0x13025e(_0x6c4786);})['catch'](function(_0x2d8992){_0xe69d07(_0x2d8992);});})[_0x58ab('0x48')](function(_0x11683d){_0xe69d07(_0x11683d);});}else{ami['Action'](ut['variablesAction'](_0x5781f7,_0x380ab5))[_0x58ab('0x4c')](function(_0x276816){_0x13025e(_0x276816);})[_0x58ab('0x48')](function(_0x1cf8bf){_0xe69d07(_0x1cf8bf);});}});}Fax[_0x58ab('0x20')][_0x58ab('0x57')]=function(_0x75a15b,_0x238494){var _0x220d1a=this;return new BPromise(function(_0x409081,_0x57bae6){var _0x549708=uuid['v4']();var _0x4fd56b=ut['addVariables'](_0x75a15b,_0x238494);_0x220d1a[_0x58ab('0x12')][_0x549708]=new Action(_0x549708,_0x238494,_0x75a15b);var _0x6335e7=_0x220d1a[_0x58ab('0x12')][_0x549708];if(path['parse'](_0x75a15b[_0x58ab('0x4')])[_0x58ab('0x66')]==='.pdf'){originate(_0x6335e7,_0x4fd56b,!![])['then'](function(_0x2e53ba){_0x409081(_0x2e53ba);})[_0x58ab('0x48')](function(_0x19c9b1){_0x57bae6(_0x19c9b1);});}else{originate(_0x6335e7,_0x4fd56b,![])[_0x58ab('0x4c')](function(_0x4a79db){_0x409081(_0x4a79db);})[_0x58ab('0x48')](function(_0x55eea6){_0x57bae6(_0x55eea6);});}});};Fax[_0x58ab('0x20')][_0x58ab('0x67')]=function(_0x29b2ee){return!_['isNil'](_0x29b2ee)&&!_[_0x58ab('0x68')](_0x29b2ee[_0x58ab('0x26')])&&!_[_0x58ab('0x68')](_0x29b2ee['active'])&&_0x29b2ee['active']&&!_[_0x58ab('0x68')](this[_0x58ab('0x11')][util[_0x58ab('0x5e')]('SIP/%s',_0x29b2ee[_0x58ab('0x26')])])&&!_[_0x58ab('0x68')](this[_0x58ab('0x11')][util[_0x58ab('0x5e')](_0x58ab('0x69'),_0x29b2ee[_0x58ab('0x26')])]['status'])&&(this[_0x58ab('0x11')][util[_0x58ab('0x5e')]('SIP/%s',_0x29b2ee['name'])][_0x58ab('0x30')]===_0x58ab('0x6a')||this[_0x58ab('0x11')][util[_0x58ab('0x5e')]('SIP/%s',_0x29b2ee[_0x58ab('0x26')])][_0x58ab('0x30')]===_0x58ab('0x6b'));};Fax[_0x58ab('0x20')][_0x58ab('0x5b')]=function(_0x4cee22,_0xec90cc){if(!this[_0x58ab('0x67')](_0x4cee22)){ut[_0x58ab('0x47')](_0x58ab('0x6c'),_['isNil'](_0x4cee22)?'':_0x4cee22['name']+_0x58ab('0x6d'));return{'isValid':![],'message':'Trunk\x20not\x20working'};}if(_[_0x58ab('0x68')](_0xec90cc[_0x58ab('0x8')])){ut[_0x58ab('0x47')](_0x58ab('0x6e'),JSON['stringify'](_0xec90cc));return{'isValid':![],'message':'Empty\x20Fax'};}if(_[_0x58ab('0x68')](_0xec90cc[_0x58ab('0x4')])){ut['logger'](_0x58ab('0x6f'),JSON['stringify'](_0xec90cc));return{'isValid':![],'message':_0x58ab('0x70')};}return{'isValid':!![],'message':_0x58ab('0x71')};};module[_0x58ab('0x72')]=Fax;