Built motion from commit 9d0783e6.|2.6.13
[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 _0xffc2=['../rpc/faxAccount','../rpc/attachment','../rpc/faxMessage','faxAccounts','trunks','actions','newchannel','bind','varset','syncFaxStatus','syncReceiveFax','hangup','syncHangup','originateresponse','syncNewchannel','hasOwnProperty','localstationid','uniqueid','channels','channel','name','info','\x20syncNewchannel\x20','prototype','syncVarSet','variable','value','syncNewState','channelstate','channelstatedesc','syncNewExten','operation','filename','\x20syncFaxStatus\x20','remotestationid','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','\x20result\x20conversion\x20file\x20','message','create','parse','application/pdf','notify','fax','.pdf','catch','error','\x20notify\x20','stringify','\x20receiveFax\x20','syncOriginateResponse','merge','pick','reason','AttachmentId','update','SENDING\x20FAILED\x20','then','logger','[Fax][Message][Error][Updated]','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','updateStatus','[Fax][Message][Updated]','[Fax][Message][NoUpdate1]','finally','[Fax][Message][NoUpdate2]','\x20hangup\x20faxRiceive\x20','faxpages','faxstatus','SUCCESS','faxstatusstring','SENDING\x20FAILED','execOriginate','FaxAccountId','[Fax][execOriginate][success]','Fax\x20received','checkAfterSend','checkTrunk','Trunk\x20Reachable','format','%s/%s.tif','dir','XMF-PATH','Convert\x20pdf\x20to\x20tif','Action','variablesAction','isNil','active','SIP/%s','status','reachable','unmonitored','isTrunkReachable','[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','Is\x20Valid','lodash','bluebird','uuid','moment','path','tiff2pdf','./util/gs','../../../config/logger','../ami','./rpcDb','./channel','./util/util'];(function(_0x4a53d,_0x58a95e){var _0x27410e=function(_0x38fc90){while(--_0x38fc90){_0x4a53d['push'](_0x4a53d['shift']());}};_0x27410e(++_0x58a95e);}(_0xffc2,0xde));var _0x2ffc=function(_0x1476fb,_0x467d01){_0x1476fb=_0x1476fb-0x0;var _0x44f57e=_0xffc2[_0x1476fb];return _0x44f57e;};'use strict';var util=require('util');var _=require(_0x2ffc('0x0'));var BPromise=require(_0x2ffc('0x1'));var uuid=require(_0x2ffc('0x2'));var moment=require(_0x2ffc('0x3'));var path=require(_0x2ffc('0x4'));var tiff2pdf=require(_0x2ffc('0x5'));var gs=require(_0x2ffc('0x6'));var logger=require(_0x2ffc('0x7'))('fax');var ami=require(_0x2ffc('0x8'));var rpcDb=require(_0x2ffc('0x9'));var Channel=require(_0x2ffc('0xa'));var ut=require(_0x2ffc('0xb'));var Action=require('./action');var faxAccount=require(_0x2ffc('0xc'));var attachment=require(_0x2ffc('0xd'));var faxMessage=require(_0x2ffc('0xe'));function Fax(_0x4e266c){this['channels']={};this['faxAccounts']=_0x4e266c[_0x2ffc('0xf')];this['trunks']=_0x4e266c[_0x2ffc('0x10')];this[_0x2ffc('0x11')]={};ami['on'](_0x2ffc('0x12'),this['syncNewchannel'][_0x2ffc('0x13')](this));ami['on'](_0x2ffc('0x14'),this['syncVarSet']['bind'](this));ami['on']('newstate',this['syncNewState'][_0x2ffc('0x13')](this));ami['on']('newexten',this['syncNewExten'][_0x2ffc('0x13')](this));ami['on']('faxstatus',this[_0x2ffc('0x15')][_0x2ffc('0x13')](this));ami['on']('receivefax',this[_0x2ffc('0x16')][_0x2ffc('0x13')](this));ami['on'](_0x2ffc('0x17'),this[_0x2ffc('0x18')][_0x2ffc('0x13')](this));ami['on'](_0x2ffc('0x19'),this['syncOriginateResponse'][_0x2ffc('0x13')](this));}Fax['prototype'][_0x2ffc('0x1a')]=function(_0x331a59){if(_0x331a59['context']==='from-voip-provider'){for(var _0x3e3460 in this[_0x2ffc('0xf')]){if(this['faxAccounts'][_0x2ffc('0x1b')](_0x3e3460)){if(_0x331a59['exten']===this[_0x2ffc('0xf')][_0x3e3460][_0x2ffc('0x1c')]){this['channels'][_0x331a59[_0x2ffc('0x1d')]]={};this[_0x2ffc('0x1e')][_0x331a59[_0x2ffc('0x1d')]][_0x2ffc('0x1f')]=_0x331a59['channel'];this['channels'][_0x331a59['uniqueid']]['id']=this[_0x2ffc('0xf')][_0x3e3460]['id'];this[_0x2ffc('0x1e')][_0x331a59[_0x2ffc('0x1d')]][_0x2ffc('0x20')]=this[_0x2ffc('0xf')][_0x3e3460][_0x2ffc('0x20')];logger[_0x2ffc('0x21')](_0x331a59[_0x2ffc('0x1d')],_0x2ffc('0x22'));}}}}if(this[_0x2ffc('0x11')][_0x331a59['uniqueid']]){this[_0x2ffc('0x1e')][_0x331a59[_0x2ffc('0x1d')]]={};this[_0x2ffc('0x1e')][_0x331a59[_0x2ffc('0x1d')]]['channel']=_0x331a59[_0x2ffc('0x1f')];logger[_0x2ffc('0x21')](_0x331a59[_0x2ffc('0x1d')],'\x20syncNewchannel\x20');}};Fax[_0x2ffc('0x23')][_0x2ffc('0x24')]=function(_0x1fc90e){if(this[_0x2ffc('0x1e')][_0x1fc90e['uniqueid']]){for(var _0x13413e in _0x1fc90e[_0x2ffc('0x25')]){if(_0x1fc90e[_0x2ffc('0x25')]['hasOwnProperty'](_0x13413e)){this[_0x2ffc('0x1e')][_0x1fc90e[_0x2ffc('0x1d')]][_0x13413e]=_0x1fc90e[_0x2ffc('0x26')];}}}};Fax[_0x2ffc('0x23')][_0x2ffc('0x27')]=function(_0x484f37){if(this[_0x2ffc('0x1e')][_0x484f37['uniqueid']]){this[_0x2ffc('0x1e')][_0x484f37[_0x2ffc('0x1d')]][_0x2ffc('0x28')]=_0x484f37[_0x2ffc('0x28')];this[_0x2ffc('0x1e')][_0x484f37['uniqueid']]['channelstatedesc']=_0x484f37[_0x2ffc('0x29')];}};Fax[_0x2ffc('0x23')][_0x2ffc('0x2a')]=function(_0xce878e){if(this['channels'][_0xce878e[_0x2ffc('0x1d')]]){for(var _0x5dd38f in _0xce878e[_0x2ffc('0x25')]){if(_0xce878e[_0x2ffc('0x25')]['hasOwnProperty'](_0x5dd38f)){this[_0x2ffc('0x1e')][_0xce878e[_0x2ffc('0x1d')]][_0x5dd38f]=_0xce878e['value'];}}}};Fax[_0x2ffc('0x23')]['syncFaxStatus']=function(_0xe0c714){if(this[_0x2ffc('0x1e')][_0xe0c714['uniqueid']]){this['channels'][_0xe0c714[_0x2ffc('0x1d')]][_0x2ffc('0x2b')]=_0xe0c714[_0x2ffc('0x2b')];this[_0x2ffc('0x1e')][_0xe0c714[_0x2ffc('0x1d')]]['status']=_0xe0c714['status'];this[_0x2ffc('0x1e')][_0xe0c714[_0x2ffc('0x1d')]]['localstationid']=_0xe0c714[_0x2ffc('0x1c')];this[_0x2ffc('0x1e')][_0xe0c714['uniqueid']][_0x2ffc('0x2c')]=_0xe0c714[_0x2ffc('0x2c')];logger[_0x2ffc('0x21')](_0xe0c714[_0x2ffc('0x1d')],_0x2ffc('0x2d'));}};Fax[_0x2ffc('0x23')][_0x2ffc('0x16')]=function(_0x51e7c2){var _0x11e0e0=this;if(this[_0x2ffc('0x1e')][_0x51e7c2['uniqueid']]){var _0x368c95=this['channels'][_0x51e7c2[_0x2ffc('0x1d')]]['id'];this[_0x2ffc('0x1e')][_0x51e7c2[_0x2ffc('0x1d')]][_0x2ffc('0x2e')]=_0x51e7c2[_0x2ffc('0x2e')];this[_0x2ffc('0x1e')][_0x51e7c2[_0x2ffc('0x1d')]][_0x2ffc('0x2f')]=_0x51e7c2[_0x2ffc('0x2f')];this[_0x2ffc('0x1e')][_0x51e7c2[_0x2ffc('0x1d')]]['localstationid']=_0x51e7c2[_0x2ffc('0x1c')];this[_0x2ffc('0x1e')][_0x51e7c2[_0x2ffc('0x1d')]][_0x2ffc('0x30')]=_0x51e7c2[_0x2ffc('0x30')];this[_0x2ffc('0x1e')][_0x51e7c2[_0x2ffc('0x1d')]]['transferrate']=_0x51e7c2[_0x2ffc('0x31')];this[_0x2ffc('0x1e')][_0x51e7c2[_0x2ffc('0x1d')]]['filename']=_0x51e7c2[_0x2ffc('0x2c')];this['channels'][_0x51e7c2[_0x2ffc('0x1d')]]['calleridnum']=_0x51e7c2[_0x2ffc('0x32')];if(this[_0x2ffc('0x1e')][_0x51e7c2[_0x2ffc('0x1d')]][_0x2ffc('0x2f')]>0x0){tiff2pdf(_0x51e7c2[_0x2ffc('0x2c')],_0x2ffc('0x33'),function(_0x5e26e6){logger[_0x2ffc('0x21')](_0x51e7c2['uniqueid'],_0x2ffc('0x34'),_0x5e26e6[_0x2ffc('0x35')]);attachment[_0x2ffc('0x36')]({'name':path[_0x2ffc('0x37')](_0x51e7c2[_0x2ffc('0x2c')])['name'],'basename':path['parse'](_0x51e7c2['filename'])[_0x2ffc('0x20')]+'.pdf','type':_0x2ffc('0x38')})['then'](function(_0x8f877a){faxAccount[_0x2ffc('0x39')](_0x368c95,{'from':_0x51e7c2[_0x2ffc('0x32')],'mapKey':_0x2ffc('0x3a'),'body':path[_0x2ffc('0x37')](_0x51e7c2[_0x2ffc('0x2c')])[_0x2ffc('0x20')]+_0x2ffc('0x3b'),'AttachmentId':_0x8f877a['id']})[_0x2ffc('0x3c')](function(_0x434e4a){logger[_0x2ffc('0x3d')](_0x51e7c2[_0x2ffc('0x1d')],_0x2ffc('0x3e'),JSON[_0x2ffc('0x3f')](_0x434e4a));});})[_0x2ffc('0x3c')](function(_0x745bba){logger['error'](_0x51e7c2[_0x2ffc('0x1d')],_0x2ffc('0x40'),JSON[_0x2ffc('0x3f')](_0x745bba));});});}logger[_0x2ffc('0x21')](_0x51e7c2['uniqueid'],_0x2ffc('0x40'));}};Fax[_0x2ffc('0x23')][_0x2ffc('0x41')]=function(_0x2c8ffb){if(this[_0x2ffc('0x11')][_0x2c8ffb[_0x2ffc('0x1d')]]){_[_0x2ffc('0x42')](this[_0x2ffc('0x1e')][_0x2c8ffb[_0x2ffc('0x1d')]],_[_0x2ffc('0x43')](_0x2c8ffb,['response',_0x2ffc('0x44')]));}};function updateStatus(_0x8f4cde){if(_0x8f4cde[_0x2ffc('0x45')]){faxMessage[_0x2ffc('0x46')](_0x8f4cde[_0x2ffc('0x45')],{'failMessage':_0x2ffc('0x47')})[_0x2ffc('0x48')](function(_0x31374e){ut[_0x2ffc('0x49')](_0x2ffc('0x4a'),_0x31374e);})[_0x2ffc('0x3c')](function(_0x30138d){ut[_0x2ffc('0x49')](_0x2ffc('0x4b'),_0x30138d);});}else{ut[_0x2ffc('0x49')](_0x2ffc('0x4c'));}}Fax[_0x2ffc('0x23')][_0x2ffc('0x4d')]=function(_0x58957a,_0x163a8b,_0x2ed789){var _0x222994=this;var _0xd4f469={};if(_0x163a8b){_0xd4f469={'read':_0x163a8b};}else{_0xd4f469={'failMessage':_0x2ed789};}if(this[_0x2ffc('0x11')][_0x58957a][_0x2ffc('0x45')]){faxMessage['update'](this[_0x2ffc('0x11')][_0x58957a][_0x2ffc('0x45')],_0xd4f469)['then'](function(_0x27410d){ut[_0x2ffc('0x49')](_0x2ffc('0x4e'),_0x27410d);})['catch'](function(_0x19bf2d){ut['logger'](_0x2ffc('0x4f'),_0x19bf2d);})[_0x2ffc('0x50')](function(){delete _0x222994[_0x2ffc('0x1e')][_0x58957a];delete _0x222994[_0x2ffc('0x11')][_0x58957a];});}else{ut[_0x2ffc('0x49')](_0x2ffc('0x51'));delete this[_0x2ffc('0x1e')][_0x58957a];delete this[_0x2ffc('0x11')][_0x58957a];}};Fax[_0x2ffc('0x23')]['syncHangup']=function(_0x512412){var _0x5e5e6f=this;if(this[_0x2ffc('0x1e')][_0x512412['uniqueid']]){if(this[_0x2ffc('0x11')][_0x512412[_0x2ffc('0x1d')]]){this['channels'][_0x512412[_0x2ffc('0x1d')]]['id']=this[_0x2ffc('0x11')][_0x512412[_0x2ffc('0x1d')]]['id'];this[_0x2ffc('0x1e')][_0x512412[_0x2ffc('0x1d')]][_0x2ffc('0x20')]=this[_0x2ffc('0x11')][_0x512412['uniqueid']][_0x2ffc('0x20')];}logger[_0x2ffc('0x21')](_0x512412['uniqueid'],this[_0x2ffc('0x11')][_0x512412[_0x2ffc('0x1d')]]?'\x20hangup\x20sendFile\x20':_0x2ffc('0x52'),JSON[_0x2ffc('0x3f')](this[_0x2ffc('0x1e')][_0x512412[_0x2ffc('0x1d')]]));if(this[_0x2ffc('0x11')][_0x512412['uniqueid']]){if(this[_0x2ffc('0x1e')][_0x512412[_0x2ffc('0x1d')]][_0x2ffc('0x53')]>0x0&&this[_0x2ffc('0x1e')][_0x512412[_0x2ffc('0x1d')]][_0x2ffc('0x54')]===_0x2ffc('0x55')){this[_0x2ffc('0x4d')](_0x512412[_0x2ffc('0x1d')],!![]);}else{this[_0x2ffc('0x4d')](_0x512412[_0x2ffc('0x1d')],![],this[_0x2ffc('0x1e')][_0x512412[_0x2ffc('0x1d')]][_0x2ffc('0x56')]?'SENDING\x20FAILED:\x20'+this[_0x2ffc('0x1e')][_0x512412['uniqueid']][_0x2ffc('0x56')]:_0x2ffc('0x57'));}}}};Fax[_0x2ffc('0x23')]['send']=function(_0x5a5c45){var _0x45ff1c=this;return new BPromise(function(_0x454fc5,_0x4c9ba9){_0x45ff1c[_0x2ffc('0x58')](_0x5a5c45,_0x45ff1c['faxAccounts'][_0x5a5c45[_0x2ffc('0x59')]])['then'](function(_0x1a05ae){ut[_0x2ffc('0x49')](_0x2ffc('0x5a'),_0x1a05ae);_0x454fc5(ut[_0x2ffc('0x35')](_0x2ffc('0x5b')));})[_0x2ffc('0x3c')](function(_0x5e9a77){ut['logger']('[Fax][execOriginate][failure]',_0x5e9a77);updateStatus(_0x5a5c45);_0x4c9ba9(ut[_0x2ffc('0x35')]('Fax\x20failed'));});});};Fax[_0x2ffc('0x23')][_0x2ffc('0x5c')]=function(_0x22a81c){var _0x18feae=this;return new BPromise(function(_0x185459,_0x5dcd95){var _0x3d000b=_0x18feae[_0x2ffc('0x5d')](_0x18feae['faxAccounts'][_0x22a81c['FaxAccountId']]['Trunk'],_0x22a81c);if(_0x3d000b['isValid']){_0x185459(ut[_0x2ffc('0x35')](_0x2ffc('0x5e')));}else{_0x5dcd95(ut[_0x2ffc('0x35')](_0x3d000b['message']));}});};function originate(_0xbae885,_0xb3c44d,_0x645514){return new BPromise(function(_0x2e2a3b,_0x8e9aff){if(_0x645514){var _0x19caa0=util['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[_0x2ffc('0x37')](_0xbae885[_0x2ffc('0x4')])[_0x2ffc('0x20')],_0xbae885[_0x2ffc('0x4')]);gs['execute'](_0x19caa0)['then'](function(){_0xbae885[_0x2ffc('0x4')]=util[_0x2ffc('0x5f')](_0x2ffc('0x60'),path[_0x2ffc('0x37')](_0xbae885[_0x2ffc('0x4')])[_0x2ffc('0x61')],path[_0x2ffc('0x37')](_0xbae885['path'])[_0x2ffc('0x20')]);_0xb3c44d[_0x2ffc('0x62')]=_0xbae885['path'];ut[_0x2ffc('0x49')](_0x2ffc('0x63'),'');ami[_0x2ffc('0x64')](ut[_0x2ffc('0x65')](_0xbae885,_0xb3c44d))[_0x2ffc('0x48')](function(_0x257219){_0x2e2a3b(_0x257219);})[_0x2ffc('0x3c')](function(_0x1bd9f8){_0x8e9aff(_0x1bd9f8);});})['catch'](function(_0x4811f0){_0x8e9aff(_0x4811f0);});}else{ami[_0x2ffc('0x64')](ut[_0x2ffc('0x65')](_0xbae885,_0xb3c44d))[_0x2ffc('0x48')](function(_0x28a771){_0x2e2a3b(_0x28a771);})[_0x2ffc('0x3c')](function(_0x46163f){_0x8e9aff(_0x46163f);});}});}Fax[_0x2ffc('0x23')][_0x2ffc('0x58')]=function(_0x23b248,_0x222880){var _0x3bc316=this;return new BPromise(function(_0x1eed3c,_0x1e00b2){var _0x2511a1=uuid['v4']();var _0x149ce3=ut['addVariables'](_0x23b248,_0x222880);_0x3bc316[_0x2ffc('0x11')][_0x2511a1]=new Action(_0x2511a1,_0x222880,_0x23b248);var _0x41f333=_0x3bc316['actions'][_0x2511a1];if(path['parse'](_0x23b248[_0x2ffc('0x4')])['ext']===_0x2ffc('0x3b')){originate(_0x41f333,_0x149ce3,!![])[_0x2ffc('0x48')](function(_0xf79243){_0x1eed3c(_0xf79243);})[_0x2ffc('0x3c')](function(_0x55726c){_0x1e00b2(_0x55726c);});}else{originate(_0x41f333,_0x149ce3,![])[_0x2ffc('0x48')](function(_0xacc286){_0x1eed3c(_0xacc286);})[_0x2ffc('0x3c')](function(_0x31b611){_0x1e00b2(_0x31b611);});}});};Fax[_0x2ffc('0x23')]['isTrunkReachable']=function(_0x3bb153){return!_[_0x2ffc('0x66')](_0x3bb153)&&!_['isNil'](_0x3bb153[_0x2ffc('0x20')])&&!_[_0x2ffc('0x66')](_0x3bb153['active'])&&_0x3bb153[_0x2ffc('0x67')]&&!_[_0x2ffc('0x66')](this[_0x2ffc('0x10')][util[_0x2ffc('0x5f')](_0x2ffc('0x68'),_0x3bb153['name'])])&&!_[_0x2ffc('0x66')](this['trunks'][util['format'](_0x2ffc('0x68'),_0x3bb153[_0x2ffc('0x20')])][_0x2ffc('0x69')])&&(this[_0x2ffc('0x10')][util[_0x2ffc('0x5f')](_0x2ffc('0x68'),_0x3bb153['name'])][_0x2ffc('0x69')]===_0x2ffc('0x6a')||this[_0x2ffc('0x10')][util[_0x2ffc('0x5f')]('SIP/%s',_0x3bb153[_0x2ffc('0x20')])]['status']===_0x2ffc('0x6b'));};Fax[_0x2ffc('0x23')][_0x2ffc('0x5d')]=function(_0x586e1a,_0x3fd511){if(!this[_0x2ffc('0x6c')](_0x586e1a)){ut['logger'](_0x2ffc('0x6d'),_[_0x2ffc('0x66')](_0x586e1a)?'':_0x586e1a['name']+_0x2ffc('0x6e'));return{'isValid':![],'message':_0x2ffc('0x6f')};}if(_['isNil'](_0x3fd511[_0x2ffc('0x3a')])){ut[_0x2ffc('0x49')](_0x2ffc('0x70'),JSON[_0x2ffc('0x3f')](_0x3fd511));return{'isValid':![],'message':_0x2ffc('0x71')};}if(_['isNil'](_0x3fd511['path'])){ut['logger'](_0x2ffc('0x72'),JSON[_0x2ffc('0x3f')](_0x3fd511));return{'isValid':![],'message':_0x2ffc('0x73')};}return{'isValid':!![],'message':_0x2ffc('0x74')};};module['exports']=Fax;