5ca2d7f9e0a52dc1cf5ac0f253d609169f7f1b22
[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 _0x06cf=['./util/gs','../../../config/logger','fax','./rpcDb','./channel','../rpc/faxAccount','../rpc/attachment','../rpc/faxMessage','faxAccounts','trunks','actions','syncNewchannel','syncVarSet','bind','newstate','syncNewState','newexten','faxstatus','syncFaxStatus','syncReceiveFax','hangup','syncHangup','originateresponse','syncOriginateResponse','prototype','context','hasOwnProperty','exten','channels','uniqueid','channel','name','info','\x20syncNewchannel\x20','variable','value','channelstate','channelstatedesc','operation','status','localstationid','filename','\x20syncFaxStatus\x20','remotestationid','resolution','transferrate','calleridnum','pagestransferred','\x20result\x20conversion\x20file\x20','create','parse','then','notify','.pdf','catch','error','\x20notify\x20','stringify','\x20receiveFax\x20','merge','pick','reason','AttachmentId','update','SENDING\x20FAILED\x20','logger','[Fax][Message][Error][Updated]','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','[Fax][Message][Updated]','\x20hangup\x20sendFile\x20','\x20hangup\x20faxRiceive\x20','faxpages','SUCCESS','updateStatus','faxstatusstring','SENDING\x20FAILED:\x20','SENDING\x20FAILED','send','execOriginate','message','Fax\x20failed','checkAfterSend','checkTrunk','Trunk','format','execute','%s/%s.tif','dir','XMF-PATH','Convert\x20pdf\x20to\x20tif','Action','variablesAction','addVariables','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','exports','util','lodash','bluebird','moment','path','tiff2pdf'];(function(_0x59ec7a,_0x1eb345){var _0x8535ed=function(_0x5bca0b){while(--_0x5bca0b){_0x59ec7a['push'](_0x59ec7a['shift']());}};_0x8535ed(++_0x1eb345);}(_0x06cf,0xda));var _0xf06c=function(_0x399884,_0x3f3433){_0x399884=_0x399884-0x0;var _0x29c025=_0x06cf[_0x399884];return _0x29c025;};'use strict';var util=require(_0xf06c('0x0'));var _=require(_0xf06c('0x1'));var BPromise=require(_0xf06c('0x2'));var uuid=require('uuid');var moment=require(_0xf06c('0x3'));var path=require(_0xf06c('0x4'));var tiff2pdf=require(_0xf06c('0x5'));var gs=require(_0xf06c('0x6'));var logger=require(_0xf06c('0x7'))(_0xf06c('0x8'));var ami=require('../ami');var rpcDb=require(_0xf06c('0x9'));var Channel=require(_0xf06c('0xa'));var ut=require('./util/util');var Action=require('./action');var faxAccount=require(_0xf06c('0xb'));var attachment=require(_0xf06c('0xc'));var faxMessage=require(_0xf06c('0xd'));function Fax(_0x27905e){this['channels']={};this[_0xf06c('0xe')]=_0x27905e['faxAccounts'];this[_0xf06c('0xf')]=_0x27905e[_0xf06c('0xf')];this[_0xf06c('0x10')]={};ami['on']('newchannel',this[_0xf06c('0x11')]['bind'](this));ami['on']('varset',this[_0xf06c('0x12')][_0xf06c('0x13')](this));ami['on'](_0xf06c('0x14'),this[_0xf06c('0x15')]['bind'](this));ami['on'](_0xf06c('0x16'),this['syncNewExten'][_0xf06c('0x13')](this));ami['on'](_0xf06c('0x17'),this[_0xf06c('0x18')][_0xf06c('0x13')](this));ami['on']('receivefax',this[_0xf06c('0x19')]['bind'](this));ami['on'](_0xf06c('0x1a'),this[_0xf06c('0x1b')][_0xf06c('0x13')](this));ami['on'](_0xf06c('0x1c'),this[_0xf06c('0x1d')][_0xf06c('0x13')](this));}Fax[_0xf06c('0x1e')]['syncNewchannel']=function(_0x58c99c){if(_0x58c99c[_0xf06c('0x1f')]==='from-voip-provider'){for(var _0x3220bf in this[_0xf06c('0xe')]){if(this['faxAccounts'][_0xf06c('0x20')](_0x3220bf)){if(_0x58c99c[_0xf06c('0x21')]===this['faxAccounts'][_0x3220bf]['localstationid']){this[_0xf06c('0x22')][_0x58c99c[_0xf06c('0x23')]]={};this[_0xf06c('0x22')][_0x58c99c[_0xf06c('0x23')]][_0xf06c('0x24')]=_0x58c99c['channel'];this[_0xf06c('0x22')][_0x58c99c[_0xf06c('0x23')]]['id']=this[_0xf06c('0xe')][_0x3220bf]['id'];this[_0xf06c('0x22')][_0x58c99c['uniqueid']][_0xf06c('0x25')]=this[_0xf06c('0xe')][_0x3220bf]['name'];logger[_0xf06c('0x26')](_0x58c99c[_0xf06c('0x23')],_0xf06c('0x27'));}}}}if(this[_0xf06c('0x10')][_0x58c99c['uniqueid']]){this[_0xf06c('0x22')][_0x58c99c[_0xf06c('0x23')]]={};this[_0xf06c('0x22')][_0x58c99c[_0xf06c('0x23')]][_0xf06c('0x24')]=_0x58c99c['channel'];logger['info'](_0x58c99c['uniqueid'],_0xf06c('0x27'));}};Fax['prototype']['syncVarSet']=function(_0x5813fe){if(this[_0xf06c('0x22')][_0x5813fe[_0xf06c('0x23')]]){for(var _0x419707 in _0x5813fe[_0xf06c('0x28')]){if(_0x5813fe[_0xf06c('0x28')][_0xf06c('0x20')](_0x419707)){this[_0xf06c('0x22')][_0x5813fe[_0xf06c('0x23')]][_0x419707]=_0x5813fe[_0xf06c('0x29')];}}}};Fax[_0xf06c('0x1e')][_0xf06c('0x15')]=function(_0x4ec089){if(this[_0xf06c('0x22')][_0x4ec089[_0xf06c('0x23')]]){this[_0xf06c('0x22')][_0x4ec089[_0xf06c('0x23')]][_0xf06c('0x2a')]=_0x4ec089[_0xf06c('0x2a')];this[_0xf06c('0x22')][_0x4ec089[_0xf06c('0x23')]][_0xf06c('0x2b')]=_0x4ec089['channelstatedesc'];}};Fax[_0xf06c('0x1e')]['syncNewExten']=function(_0x12c440){if(this[_0xf06c('0x22')][_0x12c440['uniqueid']]){for(var _0x4c15c8 in _0x12c440[_0xf06c('0x28')]){if(_0x12c440[_0xf06c('0x28')][_0xf06c('0x20')](_0x4c15c8)){this['channels'][_0x12c440[_0xf06c('0x23')]][_0x4c15c8]=_0x12c440[_0xf06c('0x29')];}}}};Fax[_0xf06c('0x1e')][_0xf06c('0x18')]=function(_0xbde4c){if(this[_0xf06c('0x22')][_0xbde4c[_0xf06c('0x23')]]){this['channels'][_0xbde4c[_0xf06c('0x23')]][_0xf06c('0x2c')]=_0xbde4c[_0xf06c('0x2c')];this[_0xf06c('0x22')][_0xbde4c[_0xf06c('0x23')]][_0xf06c('0x2d')]=_0xbde4c[_0xf06c('0x2d')];this['channels'][_0xbde4c[_0xf06c('0x23')]][_0xf06c('0x2e')]=_0xbde4c[_0xf06c('0x2e')];this[_0xf06c('0x22')][_0xbde4c[_0xf06c('0x23')]]['filename']=_0xbde4c[_0xf06c('0x2f')];logger[_0xf06c('0x26')](_0xbde4c['uniqueid'],_0xf06c('0x30'));}};Fax[_0xf06c('0x1e')][_0xf06c('0x19')]=function(_0x2c6014){var _0x56467d=this;if(this['channels'][_0x2c6014[_0xf06c('0x23')]]){var _0x3fecd3=this[_0xf06c('0x22')][_0x2c6014[_0xf06c('0x23')]]['id'];this[_0xf06c('0x22')][_0x2c6014['uniqueid']][_0xf06c('0x31')]=_0x2c6014['remotestationid'];this[_0xf06c('0x22')][_0x2c6014[_0xf06c('0x23')]]['pagestransferred']=_0x2c6014['pagestransferred'];this[_0xf06c('0x22')][_0x2c6014[_0xf06c('0x23')]][_0xf06c('0x2e')]=_0x2c6014[_0xf06c('0x2e')];this[_0xf06c('0x22')][_0x2c6014['uniqueid']][_0xf06c('0x32')]=_0x2c6014['resolution'];this['channels'][_0x2c6014['uniqueid']][_0xf06c('0x33')]=_0x2c6014[_0xf06c('0x33')];this[_0xf06c('0x22')][_0x2c6014['uniqueid']][_0xf06c('0x2f')]=_0x2c6014[_0xf06c('0x2f')];this[_0xf06c('0x22')][_0x2c6014['uniqueid']][_0xf06c('0x34')]=_0x2c6014[_0xf06c('0x34')];if(this[_0xf06c('0x22')][_0x2c6014['uniqueid']][_0xf06c('0x35')]>0x0){tiff2pdf(_0x2c6014[_0xf06c('0x2f')],'/var/opt/motion2/server/files/attachments/',function(_0x5c417e){logger['info'](_0x2c6014[_0xf06c('0x23')],_0xf06c('0x36'),_0x5c417e['message']);attachment[_0xf06c('0x37')]({'name':path[_0xf06c('0x38')](_0x2c6014[_0xf06c('0x2f')])[_0xf06c('0x25')],'basename':path['parse'](_0x2c6014[_0xf06c('0x2f')])[_0xf06c('0x25')]+'.pdf','type':'application/pdf'})[_0xf06c('0x39')](function(_0xd956e5){faxAccount[_0xf06c('0x3a')](_0x3fecd3,{'from':_0x2c6014[_0xf06c('0x34')],'mapKey':'fax','body':path[_0xf06c('0x38')](_0x2c6014['filename'])[_0xf06c('0x25')]+_0xf06c('0x3b'),'AttachmentId':_0xd956e5['id']})[_0xf06c('0x3c')](function(_0x352857){logger[_0xf06c('0x3d')](_0x2c6014['uniqueid'],_0xf06c('0x3e'),JSON[_0xf06c('0x3f')](_0x352857));});})['catch'](function(_0x5e75fc){logger[_0xf06c('0x3d')](_0x2c6014['uniqueid'],_0xf06c('0x40'),JSON['stringify'](_0x5e75fc));});});}logger[_0xf06c('0x26')](_0x2c6014[_0xf06c('0x23')],_0xf06c('0x40'));}};Fax[_0xf06c('0x1e')][_0xf06c('0x1d')]=function(_0x191e88){if(this['actions'][_0x191e88['uniqueid']]){_[_0xf06c('0x41')](this['channels'][_0x191e88[_0xf06c('0x23')]],_[_0xf06c('0x42')](_0x191e88,['response',_0xf06c('0x43')]));}};function updateStatus(_0x48001c){if(_0x48001c[_0xf06c('0x44')]){faxMessage[_0xf06c('0x45')](_0x48001c[_0xf06c('0x44')],{'failMessage':_0xf06c('0x46')})[_0xf06c('0x39')](function(_0x11f57b){ut[_0xf06c('0x47')](_0xf06c('0x48'),_0x11f57b);})[_0xf06c('0x3c')](function(_0x30a492){ut[_0xf06c('0x47')](_0xf06c('0x49'),_0x30a492);});}else{ut['logger'](_0xf06c('0x4a'));}}Fax[_0xf06c('0x1e')]['updateStatus']=function(_0x3148a1,_0x875aac,_0x3b157f){var _0x1049c8=this;var _0x2da1b0={};if(_0x875aac){_0x2da1b0={'read':_0x875aac};}else{_0x2da1b0={'failMessage':_0x3b157f};}if(this[_0xf06c('0x10')][_0x3148a1]['AttachmentId']){faxMessage[_0xf06c('0x45')](this['actions'][_0x3148a1][_0xf06c('0x44')],_0x2da1b0)['then'](function(_0x762af1){ut[_0xf06c('0x47')](_0xf06c('0x4b'),_0x762af1);})[_0xf06c('0x3c')](function(_0x2e4b8b){ut[_0xf06c('0x47')]('[Fax][Message][NoUpdate1]',_0x2e4b8b);})['finally'](function(){delete _0x1049c8[_0xf06c('0x22')][_0x3148a1];delete _0x1049c8[_0xf06c('0x10')][_0x3148a1];});}else{ut[_0xf06c('0x47')]('[Fax][Message][NoUpdate2]');delete this[_0xf06c('0x22')][_0x3148a1];delete this[_0xf06c('0x10')][_0x3148a1];}};Fax[_0xf06c('0x1e')][_0xf06c('0x1b')]=function(_0x165d78){var _0x5a7a38=this;if(this[_0xf06c('0x22')][_0x165d78[_0xf06c('0x23')]]){if(this[_0xf06c('0x10')][_0x165d78['uniqueid']]){this[_0xf06c('0x22')][_0x165d78[_0xf06c('0x23')]]['id']=this[_0xf06c('0x10')][_0x165d78[_0xf06c('0x23')]]['id'];this[_0xf06c('0x22')][_0x165d78['uniqueid']][_0xf06c('0x25')]=this[_0xf06c('0x10')][_0x165d78[_0xf06c('0x23')]][_0xf06c('0x25')];}logger[_0xf06c('0x26')](_0x165d78[_0xf06c('0x23')],this[_0xf06c('0x10')][_0x165d78[_0xf06c('0x23')]]?_0xf06c('0x4c'):_0xf06c('0x4d'),JSON['stringify'](this[_0xf06c('0x22')][_0x165d78[_0xf06c('0x23')]]));if(this[_0xf06c('0x10')][_0x165d78[_0xf06c('0x23')]]){if(this[_0xf06c('0x22')][_0x165d78[_0xf06c('0x23')]][_0xf06c('0x4e')]>0x0&&this[_0xf06c('0x22')][_0x165d78['uniqueid']][_0xf06c('0x17')]===_0xf06c('0x4f')){this[_0xf06c('0x50')](_0x165d78['uniqueid'],!![]);}else{this[_0xf06c('0x50')](_0x165d78[_0xf06c('0x23')],![],this[_0xf06c('0x22')][_0x165d78[_0xf06c('0x23')]][_0xf06c('0x51')]?_0xf06c('0x52')+this[_0xf06c('0x22')][_0x165d78['uniqueid']][_0xf06c('0x51')]:_0xf06c('0x53'));}}}};Fax[_0xf06c('0x1e')][_0xf06c('0x54')]=function(_0x4e905a){var _0x41cad3=this;return new BPromise(function(_0x43dd70,_0x2e63f5){_0x41cad3[_0xf06c('0x55')](_0x4e905a,_0x41cad3[_0xf06c('0xe')][_0x4e905a['FaxAccountId']])[_0xf06c('0x39')](function(_0x4839d4){ut['logger']('[Fax][execOriginate][success]',_0x4839d4);_0x43dd70(ut[_0xf06c('0x56')]('Fax\x20received'));})[_0xf06c('0x3c')](function(_0x36ce6f){ut[_0xf06c('0x47')]('[Fax][execOriginate][failure]',_0x36ce6f);updateStatus(_0x4e905a);_0x2e63f5(ut['message'](_0xf06c('0x57')));});});};Fax['prototype'][_0xf06c('0x58')]=function(_0x567252){var _0x4279de=this;return new BPromise(function(_0x1d8a7b,_0x1b5174){var _0x3a03ef=_0x4279de[_0xf06c('0x59')](_0x4279de[_0xf06c('0xe')][_0x567252['FaxAccountId']][_0xf06c('0x5a')],_0x567252);if(_0x3a03ef['isValid']){_0x1d8a7b(ut[_0xf06c('0x56')]('Trunk\x20Reachable'));}else{_0x1b5174(ut['message'](_0x3a03ef[_0xf06c('0x56')]));}});};function originate(_0x4a86fb,_0x219ef7,_0x3538a1){return new BPromise(function(_0x42ffce,_0x95b3fb){if(_0x3538a1){var _0x3bb548=util[_0xf06c('0x5b')]('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['parse'](_0x4a86fb[_0xf06c('0x4')])[_0xf06c('0x25')],_0x4a86fb['path']);gs[_0xf06c('0x5c')](_0x3bb548)[_0xf06c('0x39')](function(){_0x4a86fb['path']=util[_0xf06c('0x5b')](_0xf06c('0x5d'),path['parse'](_0x4a86fb['path'])[_0xf06c('0x5e')],path[_0xf06c('0x38')](_0x4a86fb[_0xf06c('0x4')])[_0xf06c('0x25')]);_0x219ef7[_0xf06c('0x5f')]=_0x4a86fb[_0xf06c('0x4')];ut[_0xf06c('0x47')](_0xf06c('0x60'),'');ami[_0xf06c('0x61')](ut[_0xf06c('0x62')](_0x4a86fb,_0x219ef7))['then'](function(_0x562afc){_0x42ffce(_0x562afc);})[_0xf06c('0x3c')](function(_0x244486){_0x95b3fb(_0x244486);});})['catch'](function(_0x308fdf){_0x95b3fb(_0x308fdf);});}else{ami[_0xf06c('0x61')](ut[_0xf06c('0x62')](_0x4a86fb,_0x219ef7))['then'](function(_0x530117){_0x42ffce(_0x530117);})[_0xf06c('0x3c')](function(_0x572031){_0x95b3fb(_0x572031);});}});}Fax['prototype']['execOriginate']=function(_0x2fab6a,_0x388675){var _0x3befa9=this;return new BPromise(function(_0x1d751d,_0x29408f){var _0x170129=uuid['v4']();var _0x1b62e2=ut[_0xf06c('0x63')](_0x2fab6a,_0x388675);_0x3befa9[_0xf06c('0x10')][_0x170129]=new Action(_0x170129,_0x388675,_0x2fab6a);var _0x3a0e70=_0x3befa9[_0xf06c('0x10')][_0x170129];if(path[_0xf06c('0x38')](_0x2fab6a[_0xf06c('0x4')])['ext']==='.pdf'){originate(_0x3a0e70,_0x1b62e2,!![])['then'](function(_0x58de64){_0x1d751d(_0x58de64);})[_0xf06c('0x3c')](function(_0x4a9457){_0x29408f(_0x4a9457);});}else{originate(_0x3a0e70,_0x1b62e2,![])['then'](function(_0x5ed126){_0x1d751d(_0x5ed126);})[_0xf06c('0x3c')](function(_0x559356){_0x29408f(_0x559356);});}});};Fax[_0xf06c('0x1e')][_0xf06c('0x64')]=function(_0x188666){return!_[_0xf06c('0x65')](_0x188666)&&!_['isNil'](_0x188666[_0xf06c('0x25')])&&!_[_0xf06c('0x65')](_0x188666[_0xf06c('0x66')])&&_0x188666[_0xf06c('0x66')]&&!_[_0xf06c('0x65')](this['trunks'][util['format'](_0xf06c('0x67'),_0x188666[_0xf06c('0x25')])])&&!_[_0xf06c('0x65')](this[_0xf06c('0xf')][util[_0xf06c('0x5b')](_0xf06c('0x67'),_0x188666[_0xf06c('0x25')])][_0xf06c('0x2d')])&&(this[_0xf06c('0xf')][util[_0xf06c('0x5b')]('SIP/%s',_0x188666['name'])][_0xf06c('0x2d')]===_0xf06c('0x68')||this[_0xf06c('0xf')][util[_0xf06c('0x5b')]('SIP/%s',_0x188666[_0xf06c('0x25')])]['status']===_0xf06c('0x69'));};Fax['prototype'][_0xf06c('0x59')]=function(_0x4bdb54,_0x34ae2f){if(!this[_0xf06c('0x64')](_0x4bdb54)){ut['logger'](_0xf06c('0x6a'),_['isNil'](_0x4bdb54)?'':_0x4bdb54[_0xf06c('0x25')]+_0xf06c('0x6b'));return{'isValid':![],'message':_0xf06c('0x6c')};}if(_[_0xf06c('0x65')](_0x34ae2f[_0xf06c('0x8')])){ut[_0xf06c('0x47')](_0xf06c('0x6d'),JSON[_0xf06c('0x3f')](_0x34ae2f));return{'isValid':![],'message':_0xf06c('0x6e')};}if(_['isNil'](_0x34ae2f[_0xf06c('0x4')])){ut[_0xf06c('0x47')]('[Fax][checkTrunk][Empty\x20path]',JSON[_0xf06c('0x3f')](_0x34ae2f));return{'isValid':![],'message':'Empty\x20path'};}return{'isValid':!![],'message':'Is\x20Valid'};};module[_0xf06c('0x6f')]=Fax;