Built motion from commit 58b0471d.|2.6.29
[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 _0xb5fb=['variablesAction','addVariables','ext','isTrunkReachable','isNil','active','trunks','SIP/%s','reachable','unmonitored','checkTrunk','[Fax][checkTrunk][Trunk\x20not\x20working\x20correctly]','\x20Trunk\x20is\x20not\x20active','fax','Empty\x20Fax','[Fax][checkTrunk][Empty\x20path]','Empty\x20path','Is\x20Valid','lodash','bluebird','uuid','moment','path','../../../config/logger','./rpcDb','./channel','./action','../rpc/faxAccount','../rpc/attachment','channels','faxAccounts','newchannel','syncNewchannel','bind','varset','syncNewState','newexten','syncNewExten','receivefax','syncHangup','originateresponse','syncOriginateResponse','prototype','context','from-voip-provider','hasOwnProperty','uniqueid','channel','name','info','\x20syncNewchannel\x20','actions','syncVarSet','variable','channelstate','channelstatedesc','syncFaxStatus','operation','status','filename','remotestationid','pagestransferred','localstationid','resolution','calleridnum','/var/opt/motion2/server/files/attachments/','\x20result\x20conversion\x20file\x20','message','parse','.pdf','application/pdf','then','notify','catch','error','\x20notify\x20','stringify','\x20receiveFax\x20','pick','response','AttachmentId','update','SENDING\x20FAILED\x20','logger','[Fax][Message][Error][Updated]','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','[Fax][Message][NoUpdate1]','finally','[Fax][Message][NoUpdate2]','\x20hangup\x20sendFile\x20','faxpages','faxstatus','SUCCESS','updateStatus','faxstatusstring','SENDING\x20FAILED','send','execOriginate','FaxAccountId','[Fax][execOriginate][success]','Fax\x20received','Fax\x20failed','checkAfterSend','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','execute','%s/%s.tif','dir','XMF-PATH','Convert\x20pdf\x20to\x20tif','Action'];(function(_0x5dfee3,_0x5e3bbe){var _0x1e1785=function(_0x42b547){while(--_0x42b547){_0x5dfee3['push'](_0x5dfee3['shift']());}};_0x1e1785(++_0x5e3bbe);}(_0xb5fb,0x1de));var _0xbb5f=function(_0xb0acd8,_0x4cac86){_0xb0acd8=_0xb0acd8-0x0;var _0x491d41=_0xb5fb[_0xb0acd8];return _0x491d41;};'use strict';var util=require('util');var _=require(_0xbb5f('0x0'));var BPromise=require(_0xbb5f('0x1'));var uuid=require(_0xbb5f('0x2'));var moment=require(_0xbb5f('0x3'));var path=require(_0xbb5f('0x4'));var tiff2pdf=require('tiff2pdf');var gs=require('./util/gs');var logger=require(_0xbb5f('0x5'))('fax');var ami=require('../ami');var rpcDb=require(_0xbb5f('0x6'));var Channel=require(_0xbb5f('0x7'));var ut=require('./util/util');var Action=require(_0xbb5f('0x8'));var faxAccount=require(_0xbb5f('0x9'));var attachment=require(_0xbb5f('0xa'));var faxMessage=require('../rpc/faxMessage');function Fax(_0x56cc04){this[_0xbb5f('0xb')]={};this['faxAccounts']=_0x56cc04[_0xbb5f('0xc')];this['trunks']=_0x56cc04['trunks'];this['actions']={};ami['on'](_0xbb5f('0xd'),this[_0xbb5f('0xe')][_0xbb5f('0xf')](this));ami['on'](_0xbb5f('0x10'),this['syncVarSet'][_0xbb5f('0xf')](this));ami['on']('newstate',this[_0xbb5f('0x11')][_0xbb5f('0xf')](this));ami['on'](_0xbb5f('0x12'),this[_0xbb5f('0x13')]['bind'](this));ami['on']('faxstatus',this['syncFaxStatus'][_0xbb5f('0xf')](this));ami['on'](_0xbb5f('0x14'),this['syncReceiveFax'][_0xbb5f('0xf')](this));ami['on']('hangup',this[_0xbb5f('0x15')][_0xbb5f('0xf')](this));ami['on'](_0xbb5f('0x16'),this[_0xbb5f('0x17')]['bind'](this));}Fax[_0xbb5f('0x18')][_0xbb5f('0xe')]=function(_0x369735){if(_0x369735[_0xbb5f('0x19')]===_0xbb5f('0x1a')){for(var _0x1fdd51 in this['faxAccounts']){if(this[_0xbb5f('0xc')][_0xbb5f('0x1b')](_0x1fdd51)){if(_0x369735['exten']===this['faxAccounts'][_0x1fdd51]['localstationid']){this[_0xbb5f('0xb')][_0x369735['uniqueid']]={};this[_0xbb5f('0xb')][_0x369735[_0xbb5f('0x1c')]][_0xbb5f('0x1d')]=_0x369735[_0xbb5f('0x1d')];this[_0xbb5f('0xb')][_0x369735['uniqueid']]['id']=this[_0xbb5f('0xc')][_0x1fdd51]['id'];this[_0xbb5f('0xb')][_0x369735[_0xbb5f('0x1c')]][_0xbb5f('0x1e')]=this[_0xbb5f('0xc')][_0x1fdd51]['name'];logger[_0xbb5f('0x1f')](_0x369735[_0xbb5f('0x1c')],_0xbb5f('0x20'));}}}}if(this[_0xbb5f('0x21')][_0x369735[_0xbb5f('0x1c')]]){this[_0xbb5f('0xb')][_0x369735[_0xbb5f('0x1c')]]={};this[_0xbb5f('0xb')][_0x369735[_0xbb5f('0x1c')]][_0xbb5f('0x1d')]=_0x369735[_0xbb5f('0x1d')];logger[_0xbb5f('0x1f')](_0x369735[_0xbb5f('0x1c')],'\x20syncNewchannel\x20');}};Fax[_0xbb5f('0x18')][_0xbb5f('0x22')]=function(_0x375c43){if(this['channels'][_0x375c43[_0xbb5f('0x1c')]]){for(var _0xbb956b in _0x375c43['variable']){if(_0x375c43[_0xbb5f('0x23')]['hasOwnProperty'](_0xbb956b)){this[_0xbb5f('0xb')][_0x375c43['uniqueid']][_0xbb956b]=_0x375c43['value'];}}}};Fax[_0xbb5f('0x18')][_0xbb5f('0x11')]=function(_0x5404df){if(this[_0xbb5f('0xb')][_0x5404df[_0xbb5f('0x1c')]]){this[_0xbb5f('0xb')][_0x5404df[_0xbb5f('0x1c')]]['channelstate']=_0x5404df[_0xbb5f('0x24')];this[_0xbb5f('0xb')][_0x5404df[_0xbb5f('0x1c')]][_0xbb5f('0x25')]=_0x5404df['channelstatedesc'];}};Fax[_0xbb5f('0x18')][_0xbb5f('0x13')]=function(_0x4228dd){if(this[_0xbb5f('0xb')][_0x4228dd[_0xbb5f('0x1c')]]){for(var _0x1abc2c in _0x4228dd[_0xbb5f('0x23')]){if(_0x4228dd[_0xbb5f('0x23')][_0xbb5f('0x1b')](_0x1abc2c)){this[_0xbb5f('0xb')][_0x4228dd['uniqueid']][_0x1abc2c]=_0x4228dd['value'];}}}};Fax[_0xbb5f('0x18')][_0xbb5f('0x26')]=function(_0x2ee81f){if(this['channels'][_0x2ee81f[_0xbb5f('0x1c')]]){this[_0xbb5f('0xb')][_0x2ee81f[_0xbb5f('0x1c')]][_0xbb5f('0x27')]=_0x2ee81f[_0xbb5f('0x27')];this[_0xbb5f('0xb')][_0x2ee81f[_0xbb5f('0x1c')]][_0xbb5f('0x28')]=_0x2ee81f[_0xbb5f('0x28')];this[_0xbb5f('0xb')][_0x2ee81f['uniqueid']]['localstationid']=_0x2ee81f['localstationid'];this[_0xbb5f('0xb')][_0x2ee81f[_0xbb5f('0x1c')]][_0xbb5f('0x29')]=_0x2ee81f[_0xbb5f('0x29')];logger[_0xbb5f('0x1f')](_0x2ee81f[_0xbb5f('0x1c')],'\x20syncFaxStatus\x20');}};Fax[_0xbb5f('0x18')]['syncReceiveFax']=function(_0x500791){var _0x2342ce=this;if(this[_0xbb5f('0xb')][_0x500791['uniqueid']]){var _0x340a5c=this[_0xbb5f('0xb')][_0x500791[_0xbb5f('0x1c')]]['id'];this['channels'][_0x500791['uniqueid']][_0xbb5f('0x2a')]=_0x500791[_0xbb5f('0x2a')];this[_0xbb5f('0xb')][_0x500791[_0xbb5f('0x1c')]][_0xbb5f('0x2b')]=_0x500791['pagestransferred'];this['channels'][_0x500791[_0xbb5f('0x1c')]][_0xbb5f('0x2c')]=_0x500791[_0xbb5f('0x2c')];this[_0xbb5f('0xb')][_0x500791[_0xbb5f('0x1c')]][_0xbb5f('0x2d')]=_0x500791[_0xbb5f('0x2d')];this[_0xbb5f('0xb')][_0x500791[_0xbb5f('0x1c')]]['transferrate']=_0x500791['transferrate'];this[_0xbb5f('0xb')][_0x500791[_0xbb5f('0x1c')]][_0xbb5f('0x29')]=_0x500791[_0xbb5f('0x29')];this[_0xbb5f('0xb')][_0x500791['uniqueid']][_0xbb5f('0x2e')]=_0x500791[_0xbb5f('0x2e')];if(this['channels'][_0x500791[_0xbb5f('0x1c')]][_0xbb5f('0x2b')]>0x0){tiff2pdf(_0x500791[_0xbb5f('0x29')],_0xbb5f('0x2f'),function(_0x8d3049){logger[_0xbb5f('0x1f')](_0x500791[_0xbb5f('0x1c')],_0xbb5f('0x30'),_0x8d3049[_0xbb5f('0x31')]);attachment['create']({'name':path['parse'](_0x500791[_0xbb5f('0x29')])['name'],'basename':path[_0xbb5f('0x32')](_0x500791[_0xbb5f('0x29')])[_0xbb5f('0x1e')]+_0xbb5f('0x33'),'type':_0xbb5f('0x34')})[_0xbb5f('0x35')](function(_0x396d23){faxAccount[_0xbb5f('0x36')](_0x340a5c,{'from':_0x500791['calleridnum'],'mapKey':'fax','body':path[_0xbb5f('0x32')](_0x500791['filename'])[_0xbb5f('0x1e')]+_0xbb5f('0x33'),'AttachmentId':_0x396d23['id']})[_0xbb5f('0x37')](function(_0x44a53c){logger[_0xbb5f('0x38')](_0x500791['uniqueid'],_0xbb5f('0x39'),JSON[_0xbb5f('0x3a')](_0x44a53c));});})[_0xbb5f('0x37')](function(_0x21bcd4){logger[_0xbb5f('0x38')](_0x500791['uniqueid'],_0xbb5f('0x3b'),JSON[_0xbb5f('0x3a')](_0x21bcd4));});});}logger[_0xbb5f('0x1f')](_0x500791['uniqueid'],_0xbb5f('0x3b'));}};Fax[_0xbb5f('0x18')][_0xbb5f('0x17')]=function(_0x21b7b3){if(this[_0xbb5f('0x21')][_0x21b7b3[_0xbb5f('0x1c')]]){_['merge'](this['channels'][_0x21b7b3[_0xbb5f('0x1c')]],_[_0xbb5f('0x3c')](_0x21b7b3,[_0xbb5f('0x3d'),'reason']));}};function updateStatus(_0x4dcce7){if(_0x4dcce7[_0xbb5f('0x3e')]){faxMessage[_0xbb5f('0x3f')](_0x4dcce7['AttachmentId'],{'failMessage':_0xbb5f('0x40')})[_0xbb5f('0x35')](function(_0x1fea5b){ut[_0xbb5f('0x41')](_0xbb5f('0x42'),_0x1fea5b);})[_0xbb5f('0x37')](function(_0x44cded){ut[_0xbb5f('0x41')](_0xbb5f('0x43'),_0x44cded);});}else{ut[_0xbb5f('0x41')](_0xbb5f('0x44'));}}Fax[_0xbb5f('0x18')]['updateStatus']=function(_0x53f7fa,_0x2801a4,_0x56b7bd){var _0x516e40=this;var _0x2b2989={};if(_0x2801a4){_0x2b2989={'read':_0x2801a4};}else{_0x2b2989={'failMessage':_0x56b7bd};}if(this[_0xbb5f('0x21')][_0x53f7fa][_0xbb5f('0x3e')]){faxMessage[_0xbb5f('0x3f')](this[_0xbb5f('0x21')][_0x53f7fa]['AttachmentId'],_0x2b2989)['then'](function(_0x1fb5d9){ut[_0xbb5f('0x41')]('[Fax][Message][Updated]',_0x1fb5d9);})[_0xbb5f('0x37')](function(_0x51543e){ut[_0xbb5f('0x41')](_0xbb5f('0x45'),_0x51543e);})[_0xbb5f('0x46')](function(){delete _0x516e40['channels'][_0x53f7fa];delete _0x516e40[_0xbb5f('0x21')][_0x53f7fa];});}else{ut[_0xbb5f('0x41')](_0xbb5f('0x47'));delete this['channels'][_0x53f7fa];delete this[_0xbb5f('0x21')][_0x53f7fa];}};Fax[_0xbb5f('0x18')][_0xbb5f('0x15')]=function(_0xc487d9){var _0x212dbb=this;if(this['channels'][_0xc487d9['uniqueid']]){if(this[_0xbb5f('0x21')][_0xc487d9[_0xbb5f('0x1c')]]){this[_0xbb5f('0xb')][_0xc487d9[_0xbb5f('0x1c')]]['id']=this[_0xbb5f('0x21')][_0xc487d9['uniqueid']]['id'];this[_0xbb5f('0xb')][_0xc487d9[_0xbb5f('0x1c')]]['name']=this['actions'][_0xc487d9['uniqueid']]['name'];}logger[_0xbb5f('0x1f')](_0xc487d9[_0xbb5f('0x1c')],this['actions'][_0xc487d9[_0xbb5f('0x1c')]]?_0xbb5f('0x48'):'\x20hangup\x20faxRiceive\x20',JSON['stringify'](this[_0xbb5f('0xb')][_0xc487d9[_0xbb5f('0x1c')]]));if(this['actions'][_0xc487d9[_0xbb5f('0x1c')]]){if(this[_0xbb5f('0xb')][_0xc487d9['uniqueid']][_0xbb5f('0x49')]>0x0&&this[_0xbb5f('0xb')][_0xc487d9[_0xbb5f('0x1c')]][_0xbb5f('0x4a')]===_0xbb5f('0x4b')){this['updateStatus'](_0xc487d9[_0xbb5f('0x1c')],!![]);}else{this[_0xbb5f('0x4c')](_0xc487d9[_0xbb5f('0x1c')],![],this[_0xbb5f('0xb')][_0xc487d9[_0xbb5f('0x1c')]][_0xbb5f('0x4d')]?'SENDING\x20FAILED:\x20'+this[_0xbb5f('0xb')][_0xc487d9[_0xbb5f('0x1c')]][_0xbb5f('0x4d')]:_0xbb5f('0x4e'));}}}};Fax[_0xbb5f('0x18')][_0xbb5f('0x4f')]=function(_0xf56944){var _0x54da01=this;return new BPromise(function(_0x4cd62a,_0x7291fe){_0x54da01[_0xbb5f('0x50')](_0xf56944,_0x54da01[_0xbb5f('0xc')][_0xf56944[_0xbb5f('0x51')]])[_0xbb5f('0x35')](function(_0x29b698){ut['logger'](_0xbb5f('0x52'),_0x29b698);_0x4cd62a(ut[_0xbb5f('0x31')](_0xbb5f('0x53')));})[_0xbb5f('0x37')](function(_0x4c82f){ut[_0xbb5f('0x41')]('[Fax][execOriginate][failure]',_0x4c82f);updateStatus(_0xf56944);_0x7291fe(ut[_0xbb5f('0x31')](_0xbb5f('0x54')));});});};Fax[_0xbb5f('0x18')][_0xbb5f('0x55')]=function(_0x96fffe){var _0x232568=this;return new BPromise(function(_0x37a9d9,_0x2505b6){var _0x45165e=_0x232568['checkTrunk'](_0x232568[_0xbb5f('0xc')][_0x96fffe[_0xbb5f('0x51')]][_0xbb5f('0x56')],_0x96fffe);if(_0x45165e[_0xbb5f('0x57')]){_0x37a9d9(ut['message'](_0xbb5f('0x58')));}else{_0x2505b6(ut[_0xbb5f('0x31')](_0x45165e[_0xbb5f('0x31')]));}});};function originate(_0x7d74bd,_0x3c0241,_0x1f4790){return new BPromise(function(_0x5bf1ee,_0x30f1b9){if(_0x1f4790){var _0x324f65=util[_0xbb5f('0x59')](_0xbb5f('0x5a'),path[_0xbb5f('0x32')](_0x7d74bd[_0xbb5f('0x4')])['name'],_0x7d74bd['path']);gs[_0xbb5f('0x5b')](_0x324f65)['then'](function(){_0x7d74bd[_0xbb5f('0x4')]=util['format'](_0xbb5f('0x5c'),path['parse'](_0x7d74bd['path'])[_0xbb5f('0x5d')],path[_0xbb5f('0x32')](_0x7d74bd[_0xbb5f('0x4')])[_0xbb5f('0x1e')]);_0x3c0241[_0xbb5f('0x5e')]=_0x7d74bd[_0xbb5f('0x4')];ut['logger'](_0xbb5f('0x5f'),'');ami['Action'](ut['variablesAction'](_0x7d74bd,_0x3c0241))[_0xbb5f('0x35')](function(_0x20b9b0){_0x5bf1ee(_0x20b9b0);})[_0xbb5f('0x37')](function(_0x45fd9d){_0x30f1b9(_0x45fd9d);});})[_0xbb5f('0x37')](function(_0xb8adbf){_0x30f1b9(_0xb8adbf);});}else{ami[_0xbb5f('0x60')](ut[_0xbb5f('0x61')](_0x7d74bd,_0x3c0241))[_0xbb5f('0x35')](function(_0x1f54b5){_0x5bf1ee(_0x1f54b5);})[_0xbb5f('0x37')](function(_0x46dffb){_0x30f1b9(_0x46dffb);});}});}Fax[_0xbb5f('0x18')][_0xbb5f('0x50')]=function(_0x2c6025,_0x22f899){var _0x11abf2=this;return new BPromise(function(_0x2cad36,_0xad722e){var _0x188c95=uuid['v4']();var _0x23e62a=ut[_0xbb5f('0x62')](_0x2c6025,_0x22f899);_0x11abf2[_0xbb5f('0x21')][_0x188c95]=new Action(_0x188c95,_0x22f899,_0x2c6025);var _0x31af50=_0x11abf2[_0xbb5f('0x21')][_0x188c95];if(path[_0xbb5f('0x32')](_0x2c6025['path'])[_0xbb5f('0x63')]===_0xbb5f('0x33')){originate(_0x31af50,_0x23e62a,!![])[_0xbb5f('0x35')](function(_0x383783){_0x2cad36(_0x383783);})[_0xbb5f('0x37')](function(_0x11cb58){_0xad722e(_0x11cb58);});}else{originate(_0x31af50,_0x23e62a,![])[_0xbb5f('0x35')](function(_0x62b3f){_0x2cad36(_0x62b3f);})['catch'](function(_0x2a1af6){_0xad722e(_0x2a1af6);});}});};Fax[_0xbb5f('0x18')][_0xbb5f('0x64')]=function(_0x3dbd83){return!_[_0xbb5f('0x65')](_0x3dbd83)&&!_['isNil'](_0x3dbd83[_0xbb5f('0x1e')])&&!_['isNil'](_0x3dbd83[_0xbb5f('0x66')])&&_0x3dbd83[_0xbb5f('0x66')]&&!_[_0xbb5f('0x65')](this[_0xbb5f('0x67')][util[_0xbb5f('0x59')](_0xbb5f('0x68'),_0x3dbd83[_0xbb5f('0x1e')])])&&!_[_0xbb5f('0x65')](this[_0xbb5f('0x67')][util[_0xbb5f('0x59')](_0xbb5f('0x68'),_0x3dbd83[_0xbb5f('0x1e')])][_0xbb5f('0x28')])&&(this['trunks'][util['format'](_0xbb5f('0x68'),_0x3dbd83[_0xbb5f('0x1e')])][_0xbb5f('0x28')]===_0xbb5f('0x69')||this['trunks'][util[_0xbb5f('0x59')](_0xbb5f('0x68'),_0x3dbd83['name'])]['status']===_0xbb5f('0x6a'));};Fax['prototype'][_0xbb5f('0x6b')]=function(_0x43bd8b,_0x31036d){if(!this[_0xbb5f('0x64')](_0x43bd8b)){ut['logger'](_0xbb5f('0x6c'),_['isNil'](_0x43bd8b)?'':_0x43bd8b[_0xbb5f('0x1e')]+_0xbb5f('0x6d'));return{'isValid':![],'message':'Trunk\x20not\x20working\x20correctly'};}if(_[_0xbb5f('0x65')](_0x31036d[_0xbb5f('0x6e')])){ut[_0xbb5f('0x41')]('[Fax][checkTrunk][Empty\x20Fax]',JSON[_0xbb5f('0x3a')](_0x31036d));return{'isValid':![],'message':_0xbb5f('0x6f')};}if(_['isNil'](_0x31036d[_0xbb5f('0x4')])){ut[_0xbb5f('0x41')](_0xbb5f('0x70'),JSON['stringify'](_0x31036d));return{'isValid':![],'message':_0xbb5f('0x71')};}return{'isValid':!![],'message':_0xbb5f('0x72')};};module['exports']=Fax;