Built motion from commit (unavailable).|2.0.75
[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 _0x8067=['execOriginate','ext','isTrunkReachable','isNil','active','trunks','SIP/%s','reachable','unmonitored','[Fax][checkTrunk][Trunk\x20not\x20work]','\x20Trunk\x20is\x20not\x20active','Trunk\x20not\x20working','[Fax][checkTrunk][Empty\x20Fax]','Empty\x20Fax','[Fax][checkTrunk][Empty\x20path]','Empty\x20path','Is\x20Valid','exports','lodash','bluebird','uuid','moment','tiff2pdf','./util/gs','../../../config/logger','fax','../ami','./rpcDb','./channel','./util/util','./action','../rpc/faxAccount','../rpc/attachment','../rpc/faxMessage','channels','faxAccounts','actions','newchannel','syncNewchannel','bind','syncVarSet','newstate','syncNewState','syncNewExten','faxstatus','syncFaxStatus','receivefax','syncReceiveFax','hangup','syncHangup','originateresponse','syncOriginateResponse','prototype','context','from-voip-provider','hasOwnProperty','exten','localstationid','uniqueid','channel','name','\x20syncNewchannel\x20','info','variable','value','channelstate','channelstatedesc','operation','status','filename','remotestationid','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','\x20result\x20conversion\x20file\x20','message','create','parse','.pdf','application/pdf','notify','error','stringify','catch','merge','pick','response','update','AttachmentId','SENDING\x20FAILED\x20','then','logger','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','updateStatus','[Fax][Message][Updated]','[Fax][Message][NoUpdate2]','\x20hangup\x20faxRiceive\x20','faxpages','SUCCESS','SENDING\x20FAILED:\x20','faxstatusstring','SENDING\x20FAILED','send','FaxAccountId','[Fax][execOriginate][success]','Fax\x20received','[Fax][execOriginate][failure]','Fax\x20failed','checkAfterSend','checkTrunk','Trunk','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','path','execute','%s/%s.tif','dir','Convert\x20pdf\x20to\x20tif','Action','variablesAction'];(function(_0x4421a0,_0x1525bc){var _0x5626d0=function(_0x56d43e){while(--_0x56d43e){_0x4421a0['push'](_0x4421a0['shift']());}};_0x5626d0(++_0x1525bc);}(_0x8067,0x10c));var _0x7806=function(_0x3ab51d,_0x2fa01d){_0x3ab51d=_0x3ab51d-0x0;var _0x29a04e=_0x8067[_0x3ab51d];return _0x29a04e;};'use strict';var util=require('util');var _=require(_0x7806('0x0'));var BPromise=require(_0x7806('0x1'));var uuid=require(_0x7806('0x2'));var moment=require(_0x7806('0x3'));var path=require('path');var tiff2pdf=require(_0x7806('0x4'));var gs=require(_0x7806('0x5'));var logger=require(_0x7806('0x6'))(_0x7806('0x7'));var ami=require(_0x7806('0x8'));var rpcDb=require(_0x7806('0x9'));var Channel=require(_0x7806('0xa'));var ut=require(_0x7806('0xb'));var Action=require(_0x7806('0xc'));var faxAccount=require(_0x7806('0xd'));var attachment=require(_0x7806('0xe'));var faxMessage=require(_0x7806('0xf'));function Fax(_0x4a5038){this[_0x7806('0x10')]={};this[_0x7806('0x11')]=_0x4a5038[_0x7806('0x11')];this['trunks']=_0x4a5038['trunks'];this[_0x7806('0x12')]={};ami['on'](_0x7806('0x13'),this[_0x7806('0x14')][_0x7806('0x15')](this));ami['on']('varset',this[_0x7806('0x16')][_0x7806('0x15')](this));ami['on'](_0x7806('0x17'),this[_0x7806('0x18')][_0x7806('0x15')](this));ami['on']('newexten',this[_0x7806('0x19')][_0x7806('0x15')](this));ami['on'](_0x7806('0x1a'),this[_0x7806('0x1b')][_0x7806('0x15')](this));ami['on'](_0x7806('0x1c'),this[_0x7806('0x1d')][_0x7806('0x15')](this));ami['on'](_0x7806('0x1e'),this[_0x7806('0x1f')][_0x7806('0x15')](this));ami['on'](_0x7806('0x20'),this[_0x7806('0x21')][_0x7806('0x15')](this));}Fax[_0x7806('0x22')][_0x7806('0x14')]=function(_0x20f130){if(_0x20f130[_0x7806('0x23')]===_0x7806('0x24')){for(var _0x9006b9 in this[_0x7806('0x11')]){if(this['faxAccounts'][_0x7806('0x25')](_0x9006b9)){if(_0x20f130[_0x7806('0x26')]===this[_0x7806('0x11')][_0x9006b9][_0x7806('0x27')]){this[_0x7806('0x10')][_0x20f130[_0x7806('0x28')]]={};this[_0x7806('0x10')][_0x20f130['uniqueid']][_0x7806('0x29')]=_0x20f130[_0x7806('0x29')];this[_0x7806('0x10')][_0x20f130[_0x7806('0x28')]]['id']=this['faxAccounts'][_0x9006b9]['id'];this[_0x7806('0x10')][_0x20f130[_0x7806('0x28')]][_0x7806('0x2a')]=this[_0x7806('0x11')][_0x9006b9][_0x7806('0x2a')];logger['info'](_0x20f130['uniqueid'],_0x7806('0x2b'));}}}}if(this[_0x7806('0x12')][_0x20f130['uniqueid']]){this['channels'][_0x20f130[_0x7806('0x28')]]={};this[_0x7806('0x10')][_0x20f130[_0x7806('0x28')]][_0x7806('0x29')]=_0x20f130[_0x7806('0x29')];logger[_0x7806('0x2c')](_0x20f130[_0x7806('0x28')],_0x7806('0x2b'));}};Fax[_0x7806('0x22')]['syncVarSet']=function(_0x3c4fa1){if(this[_0x7806('0x10')][_0x3c4fa1['uniqueid']]){for(var _0x2e521a in _0x3c4fa1[_0x7806('0x2d')]){if(_0x3c4fa1[_0x7806('0x2d')][_0x7806('0x25')](_0x2e521a)){this['channels'][_0x3c4fa1[_0x7806('0x28')]][_0x2e521a]=_0x3c4fa1[_0x7806('0x2e')];}}}};Fax['prototype'][_0x7806('0x18')]=function(_0x458924){if(this[_0x7806('0x10')][_0x458924[_0x7806('0x28')]]){this[_0x7806('0x10')][_0x458924['uniqueid']][_0x7806('0x2f')]=_0x458924['channelstate'];this[_0x7806('0x10')][_0x458924[_0x7806('0x28')]][_0x7806('0x30')]=_0x458924['channelstatedesc'];}};Fax[_0x7806('0x22')][_0x7806('0x19')]=function(_0x402009){if(this[_0x7806('0x10')][_0x402009[_0x7806('0x28')]]){for(var _0x32817a in _0x402009[_0x7806('0x2d')]){if(_0x402009[_0x7806('0x2d')][_0x7806('0x25')](_0x32817a)){this['channels'][_0x402009[_0x7806('0x28')]][_0x32817a]=_0x402009[_0x7806('0x2e')];}}}};Fax[_0x7806('0x22')][_0x7806('0x1b')]=function(_0x115889){if(this[_0x7806('0x10')][_0x115889['uniqueid']]){this[_0x7806('0x10')][_0x115889[_0x7806('0x28')]][_0x7806('0x31')]=_0x115889[_0x7806('0x31')];this['channels'][_0x115889['uniqueid']][_0x7806('0x32')]=_0x115889[_0x7806('0x32')];this[_0x7806('0x10')][_0x115889['uniqueid']][_0x7806('0x27')]=_0x115889['localstationid'];this[_0x7806('0x10')][_0x115889['uniqueid']][_0x7806('0x33')]=_0x115889[_0x7806('0x33')];logger['info'](_0x115889[_0x7806('0x28')],'\x20syncFaxStatus\x20');}};Fax['prototype'][_0x7806('0x1d')]=function(_0x7c3654){var _0x15b571=this;if(this['channels'][_0x7c3654['uniqueid']]){var _0x13e6d7=this['channels'][_0x7c3654[_0x7806('0x28')]]['id'];this[_0x7806('0x10')][_0x7c3654['uniqueid']][_0x7806('0x34')]=_0x7c3654[_0x7806('0x34')];this[_0x7806('0x10')][_0x7c3654[_0x7806('0x28')]]['pagestransferred']=_0x7c3654[_0x7806('0x35')];this[_0x7806('0x10')][_0x7c3654['uniqueid']][_0x7806('0x27')]=_0x7c3654['localstationid'];this['channels'][_0x7c3654[_0x7806('0x28')]][_0x7806('0x36')]=_0x7c3654[_0x7806('0x36')];this[_0x7806('0x10')][_0x7c3654[_0x7806('0x28')]][_0x7806('0x37')]=_0x7c3654[_0x7806('0x37')];this[_0x7806('0x10')][_0x7c3654['uniqueid']][_0x7806('0x33')]=_0x7c3654['filename'];this['channels'][_0x7c3654[_0x7806('0x28')]][_0x7806('0x38')]=_0x7c3654['calleridnum'];if(this[_0x7806('0x10')][_0x7c3654[_0x7806('0x28')]][_0x7806('0x35')]>0x0){tiff2pdf(_0x7c3654[_0x7806('0x33')],_0x7806('0x39'),function(_0x223da3){logger[_0x7806('0x2c')](_0x7c3654[_0x7806('0x28')],_0x7806('0x3a'),_0x223da3[_0x7806('0x3b')]);attachment[_0x7806('0x3c')]({'name':path['parse'](_0x7c3654['filename'])[_0x7806('0x2a')],'basename':path[_0x7806('0x3d')](_0x7c3654[_0x7806('0x33')])[_0x7806('0x2a')]+_0x7806('0x3e'),'type':_0x7806('0x3f')})['then'](function(_0x76e0d6){faxAccount[_0x7806('0x40')](_0x13e6d7,{'from':_0x7c3654[_0x7806('0x38')],'mapKey':_0x7806('0x7'),'body':path[_0x7806('0x3d')](_0x7c3654['filename'])['name']+_0x7806('0x3e'),'AttachmentId':_0x76e0d6['id']})['catch'](function(_0x267cb5){logger[_0x7806('0x41')](_0x7c3654['uniqueid'],'\x20notify\x20',JSON[_0x7806('0x42')](_0x267cb5));});})[_0x7806('0x43')](function(_0x358941){logger[_0x7806('0x41')](_0x7c3654[_0x7806('0x28')],'\x20receiveFax\x20',JSON[_0x7806('0x42')](_0x358941));});});}logger[_0x7806('0x2c')](_0x7c3654[_0x7806('0x28')],'\x20receiveFax\x20');}};Fax[_0x7806('0x22')]['syncOriginateResponse']=function(_0x41d0b4){if(this[_0x7806('0x12')][_0x41d0b4[_0x7806('0x28')]]){_[_0x7806('0x44')](this[_0x7806('0x10')][_0x41d0b4[_0x7806('0x28')]],_[_0x7806('0x45')](_0x41d0b4,[_0x7806('0x46'),'reason']));}};function updateStatus(_0x25614a){if(_0x25614a['AttachmentId']){faxMessage[_0x7806('0x47')](_0x25614a[_0x7806('0x48')],{'failMessage':_0x7806('0x49')})[_0x7806('0x4a')](function(_0x15b8f6){ut['logger']('[Fax][Message][Error][Updated]',_0x15b8f6);})[_0x7806('0x43')](function(_0xf0ebc8){ut[_0x7806('0x4b')](_0x7806('0x4c'),_0xf0ebc8);});}else{ut[_0x7806('0x4b')](_0x7806('0x4d'));}}Fax['prototype'][_0x7806('0x4e')]=function(_0x2f8e08,_0x5d282a,_0x124e42){var _0x174615=this;var _0x32b1e2={};if(_0x5d282a){_0x32b1e2={'read':_0x5d282a};}else{_0x32b1e2={'failMessage':_0x124e42};}if(this['actions'][_0x2f8e08][_0x7806('0x48')]){faxMessage[_0x7806('0x47')](this[_0x7806('0x12')][_0x2f8e08][_0x7806('0x48')],_0x32b1e2)[_0x7806('0x4a')](function(_0x2269ab){ut['logger'](_0x7806('0x4f'),_0x2269ab);})[_0x7806('0x43')](function(_0x559ab7){ut[_0x7806('0x4b')]('[Fax][Message][NoUpdate1]',_0x559ab7);})['finally'](function(){delete _0x174615[_0x7806('0x10')][_0x2f8e08];delete _0x174615[_0x7806('0x12')][_0x2f8e08];});}else{ut[_0x7806('0x4b')](_0x7806('0x50'));delete this['channels'][_0x2f8e08];delete this[_0x7806('0x12')][_0x2f8e08];}};Fax[_0x7806('0x22')][_0x7806('0x1f')]=function(_0x5e20db){var _0x4ac5c0=this;if(this['channels'][_0x5e20db[_0x7806('0x28')]]){if(this[_0x7806('0x12')][_0x5e20db[_0x7806('0x28')]]){this[_0x7806('0x10')][_0x5e20db['uniqueid']]['id']=this['actions'][_0x5e20db[_0x7806('0x28')]]['id'];this[_0x7806('0x10')][_0x5e20db[_0x7806('0x28')]][_0x7806('0x2a')]=this['actions'][_0x5e20db[_0x7806('0x28')]]['name'];}logger[_0x7806('0x2c')](_0x5e20db[_0x7806('0x28')],this[_0x7806('0x12')][_0x5e20db[_0x7806('0x28')]]?'\x20hangup\x20sendFile\x20':_0x7806('0x51'),JSON['stringify'](this[_0x7806('0x10')][_0x5e20db['uniqueid']]));if(this[_0x7806('0x12')][_0x5e20db[_0x7806('0x28')]]){if(this[_0x7806('0x10')][_0x5e20db[_0x7806('0x28')]][_0x7806('0x52')]>0x0&&this[_0x7806('0x10')][_0x5e20db[_0x7806('0x28')]][_0x7806('0x1a')]===_0x7806('0x53')){this[_0x7806('0x4e')](_0x5e20db[_0x7806('0x28')],!![]);}else{this[_0x7806('0x4e')](_0x5e20db[_0x7806('0x28')],![],this[_0x7806('0x10')][_0x5e20db['uniqueid']]['faxstatusstring']?_0x7806('0x54')+this[_0x7806('0x10')][_0x5e20db['uniqueid']][_0x7806('0x55')]:_0x7806('0x56'));}}}};Fax[_0x7806('0x22')][_0x7806('0x57')]=function(_0x111eee){var _0x49b5c5=this;return new BPromise(function(_0x1aad2b,_0xe6c921){_0x49b5c5['execOriginate'](_0x111eee,_0x49b5c5[_0x7806('0x11')][_0x111eee[_0x7806('0x58')]])[_0x7806('0x4a')](function(_0x1b558f){ut[_0x7806('0x4b')](_0x7806('0x59'),_0x1b558f);_0x1aad2b(ut[_0x7806('0x3b')](_0x7806('0x5a')));})[_0x7806('0x43')](function(_0xfb3af2){ut[_0x7806('0x4b')](_0x7806('0x5b'),_0xfb3af2);updateStatus(_0x111eee);_0xe6c921(ut['message'](_0x7806('0x5c')));});});};Fax['prototype'][_0x7806('0x5d')]=function(_0x4f7379){var _0x3a1166=this;return new BPromise(function(_0x478815,_0x5a2c6b){var _0x1942fd=_0x3a1166[_0x7806('0x5e')](_0x3a1166[_0x7806('0x11')][_0x4f7379[_0x7806('0x58')]][_0x7806('0x5f')],_0x4f7379);if(_0x1942fd[_0x7806('0x60')]){_0x478815(ut[_0x7806('0x3b')](_0x7806('0x61')));}else{_0x5a2c6b(ut[_0x7806('0x3b')](_0x1942fd[_0x7806('0x3b')]));}});};function originate(_0xe80d5f,_0x175f47,_0x88490d){return new BPromise(function(_0x2a0256,_0x31ec94){if(_0x88490d){var _0x4ce5d6=util[_0x7806('0x62')](_0x7806('0x63'),path[_0x7806('0x3d')](_0xe80d5f['path'])[_0x7806('0x2a')],_0xe80d5f[_0x7806('0x64')]);gs[_0x7806('0x65')](_0x4ce5d6)[_0x7806('0x4a')](function(){_0xe80d5f[_0x7806('0x64')]=util[_0x7806('0x62')](_0x7806('0x66'),path[_0x7806('0x3d')](_0xe80d5f[_0x7806('0x64')])[_0x7806('0x67')],path['parse'](_0xe80d5f[_0x7806('0x64')])[_0x7806('0x2a')]);_0x175f47['XMF-PATH']=_0xe80d5f[_0x7806('0x64')];ut[_0x7806('0x4b')](_0x7806('0x68'),'');ami['Action'](ut['variablesAction'](_0xe80d5f,_0x175f47))['then'](function(_0x5c1d3b){_0x2a0256(_0x5c1d3b);})['catch'](function(_0x30a0a0){_0x31ec94(_0x30a0a0);});})['catch'](function(_0x7e8e6a){_0x31ec94(_0x7e8e6a);});}else{ami[_0x7806('0x69')](ut[_0x7806('0x6a')](_0xe80d5f,_0x175f47))[_0x7806('0x4a')](function(_0x55da3d){_0x2a0256(_0x55da3d);})[_0x7806('0x43')](function(_0x561d1c){_0x31ec94(_0x561d1c);});}});}Fax[_0x7806('0x22')][_0x7806('0x6b')]=function(_0x40c63f,_0xdc8e4){var _0x443653=this;return new BPromise(function(_0x1cea08,_0xf8f744){var _0x16f07d=uuid['v4']();var _0x2f0191=ut['addVariables'](_0x40c63f,_0xdc8e4);_0x443653[_0x7806('0x12')][_0x16f07d]=new Action(_0x16f07d,_0xdc8e4,_0x40c63f);var _0x3f3c8f=_0x443653['actions'][_0x16f07d];if(path[_0x7806('0x3d')](_0x40c63f[_0x7806('0x64')])[_0x7806('0x6c')]===_0x7806('0x3e')){originate(_0x3f3c8f,_0x2f0191,!![])[_0x7806('0x4a')](function(_0x413573){_0x1cea08(_0x413573);})[_0x7806('0x43')](function(_0x51159c){_0xf8f744(_0x51159c);});}else{originate(_0x3f3c8f,_0x2f0191,![])[_0x7806('0x4a')](function(_0x87ebed){_0x1cea08(_0x87ebed);})[_0x7806('0x43')](function(_0x3333a7){_0xf8f744(_0x3333a7);});}});};Fax[_0x7806('0x22')][_0x7806('0x6d')]=function(_0x2530e6){return!_[_0x7806('0x6e')](_0x2530e6)&&!_[_0x7806('0x6e')](_0x2530e6['name'])&&!_['isNil'](_0x2530e6['active'])&&_0x2530e6[_0x7806('0x6f')]&&!_[_0x7806('0x6e')](this[_0x7806('0x70')][util[_0x7806('0x62')](_0x7806('0x71'),_0x2530e6[_0x7806('0x2a')])])&&!_[_0x7806('0x6e')](this['trunks'][util['format'](_0x7806('0x71'),_0x2530e6['name'])][_0x7806('0x32')])&&(this[_0x7806('0x70')][util[_0x7806('0x62')]('SIP/%s',_0x2530e6[_0x7806('0x2a')])]['status']===_0x7806('0x72')||this[_0x7806('0x70')][util[_0x7806('0x62')]('SIP/%s',_0x2530e6[_0x7806('0x2a')])][_0x7806('0x32')]===_0x7806('0x73'));};Fax[_0x7806('0x22')][_0x7806('0x5e')]=function(_0x5e277a,_0x2d976d){if(!this[_0x7806('0x6d')](_0x5e277a)){ut['logger'](_0x7806('0x74'),_[_0x7806('0x6e')](_0x5e277a)?'':_0x5e277a[_0x7806('0x2a')]+_0x7806('0x75'));return{'isValid':![],'message':_0x7806('0x76')};}if(_['isNil'](_0x2d976d[_0x7806('0x7')])){ut['logger'](_0x7806('0x77'),JSON[_0x7806('0x42')](_0x2d976d));return{'isValid':![],'message':_0x7806('0x78')};}if(_['isNil'](_0x2d976d[_0x7806('0x64')])){ut[_0x7806('0x4b')](_0x7806('0x79'),JSON[_0x7806('0x42')](_0x2d976d));return{'isValid':![],'message':_0x7806('0x7a')};}return{'isValid':!![],'message':_0x7806('0x7b')};};module[_0x7806('0x7c')]=Fax;