ebe6cb9d64dd8bfd6014f61a429bfae4f39314a4
[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 _0xa48f=['logger','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','[Fax][Message][NoUpdate1]','finally','[Fax][Message][NoUpdate2]','\x20hangup\x20faxRiceive\x20','faxpages','SUCCESS','updateStatus','faxstatusstring','SENDING\x20FAILED:\x20','SENDING\x20FAILED','send','execOriginate','FaxAccountId','[Fax][execOriginate][success]','Fax\x20received','[Fax][execOriginate][failure]','checkAfterSend','checkTrunk','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','dir','XMF-PATH','Convert\x20pdf\x20to\x20tif','Action','variablesAction','addVariables','ext','isTrunkReachable','isNil','active','SIP/%s','unmonitored','[Fax][checkTrunk][Trunk\x20not\x20working\x20correctly]','Empty\x20Fax','[Fax][checkTrunk][Empty\x20path]','exports','util','bluebird','uuid','moment','path','tiff2pdf','../../../config/logger','fax','../ami','./rpcDb','./channel','./action','../rpc/faxMessage','channels','faxAccounts','trunks','actions','newchannel','syncNewchannel','bind','varset','syncVarSet','newstate','syncNewState','newexten','faxstatus','syncFaxStatus','receivefax','hangup','syncHangup','originateresponse','prototype','context','from-voip-provider','hasOwnProperty','exten','localstationid','uniqueid','channel','name','info','\x20syncNewchannel\x20','variable','value','channelstate','channelstatedesc','syncNewExten','operation','status','filename','\x20syncFaxStatus\x20','syncReceiveFax','remotestationid','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','\x20result\x20conversion\x20file\x20','message','create','parse','.pdf','application/pdf','then','notify','catch','error','\x20notify\x20','stringify','\x20receiveFax\x20','syncOriginateResponse','pick','response','reason','AttachmentId','update','SENDING\x20FAILED\x20'];(function(_0x527882,_0x31c2e7){var _0x574bd8=function(_0x40c03c){while(--_0x40c03c){_0x527882['push'](_0x527882['shift']());}};_0x574bd8(++_0x31c2e7);}(_0xa48f,0x11a));var _0xfa48=function(_0x5d0db1,_0x5f401b){_0x5d0db1=_0x5d0db1-0x0;var _0x2ea0c0=_0xa48f[_0x5d0db1];return _0x2ea0c0;};'use strict';var util=require(_0xfa48('0x0'));var _=require('lodash');var BPromise=require(_0xfa48('0x1'));var uuid=require(_0xfa48('0x2'));var moment=require(_0xfa48('0x3'));var path=require(_0xfa48('0x4'));var tiff2pdf=require(_0xfa48('0x5'));var gs=require('./util/gs');var logger=require(_0xfa48('0x6'))(_0xfa48('0x7'));var ami=require(_0xfa48('0x8'));var rpcDb=require(_0xfa48('0x9'));var Channel=require(_0xfa48('0xa'));var ut=require('./util/util');var Action=require(_0xfa48('0xb'));var faxAccount=require('../rpc/faxAccount');var attachment=require('../rpc/attachment');var faxMessage=require(_0xfa48('0xc'));function Fax(_0x4ea197){this[_0xfa48('0xd')]={};this['faxAccounts']=_0x4ea197[_0xfa48('0xe')];this[_0xfa48('0xf')]=_0x4ea197['trunks'];this[_0xfa48('0x10')]={};ami['on'](_0xfa48('0x11'),this[_0xfa48('0x12')][_0xfa48('0x13')](this));ami['on'](_0xfa48('0x14'),this[_0xfa48('0x15')][_0xfa48('0x13')](this));ami['on'](_0xfa48('0x16'),this[_0xfa48('0x17')]['bind'](this));ami['on'](_0xfa48('0x18'),this['syncNewExten'][_0xfa48('0x13')](this));ami['on'](_0xfa48('0x19'),this[_0xfa48('0x1a')][_0xfa48('0x13')](this));ami['on'](_0xfa48('0x1b'),this['syncReceiveFax'][_0xfa48('0x13')](this));ami['on'](_0xfa48('0x1c'),this[_0xfa48('0x1d')]['bind'](this));ami['on'](_0xfa48('0x1e'),this['syncOriginateResponse'][_0xfa48('0x13')](this));}Fax[_0xfa48('0x1f')][_0xfa48('0x12')]=function(_0x5cda10){if(_0x5cda10[_0xfa48('0x20')]===_0xfa48('0x21')){for(var _0x27bdc9 in this[_0xfa48('0xe')]){if(this[_0xfa48('0xe')][_0xfa48('0x22')](_0x27bdc9)){if(_0x5cda10[_0xfa48('0x23')]===this[_0xfa48('0xe')][_0x27bdc9][_0xfa48('0x24')]){this[_0xfa48('0xd')][_0x5cda10[_0xfa48('0x25')]]={};this[_0xfa48('0xd')][_0x5cda10[_0xfa48('0x25')]][_0xfa48('0x26')]=_0x5cda10[_0xfa48('0x26')];this[_0xfa48('0xd')][_0x5cda10['uniqueid']]['id']=this['faxAccounts'][_0x27bdc9]['id'];this[_0xfa48('0xd')][_0x5cda10['uniqueid']][_0xfa48('0x27')]=this[_0xfa48('0xe')][_0x27bdc9]['name'];logger[_0xfa48('0x28')](_0x5cda10[_0xfa48('0x25')],_0xfa48('0x29'));}}}}if(this[_0xfa48('0x10')][_0x5cda10[_0xfa48('0x25')]]){this[_0xfa48('0xd')][_0x5cda10[_0xfa48('0x25')]]={};this[_0xfa48('0xd')][_0x5cda10['uniqueid']][_0xfa48('0x26')]=_0x5cda10[_0xfa48('0x26')];logger['info'](_0x5cda10[_0xfa48('0x25')],_0xfa48('0x29'));}};Fax[_0xfa48('0x1f')][_0xfa48('0x15')]=function(_0x31cf06){if(this[_0xfa48('0xd')][_0x31cf06[_0xfa48('0x25')]]){for(var _0x1ac1d0 in _0x31cf06[_0xfa48('0x2a')]){if(_0x31cf06[_0xfa48('0x2a')]['hasOwnProperty'](_0x1ac1d0)){this['channels'][_0x31cf06['uniqueid']][_0x1ac1d0]=_0x31cf06[_0xfa48('0x2b')];}}}};Fax['prototype'][_0xfa48('0x17')]=function(_0x288258){if(this['channels'][_0x288258[_0xfa48('0x25')]]){this[_0xfa48('0xd')][_0x288258['uniqueid']]['channelstate']=_0x288258[_0xfa48('0x2c')];this[_0xfa48('0xd')][_0x288258[_0xfa48('0x25')]][_0xfa48('0x2d')]=_0x288258[_0xfa48('0x2d')];}};Fax[_0xfa48('0x1f')][_0xfa48('0x2e')]=function(_0x522d13){if(this['channels'][_0x522d13[_0xfa48('0x25')]]){for(var _0x106ea6 in _0x522d13[_0xfa48('0x2a')]){if(_0x522d13[_0xfa48('0x2a')][_0xfa48('0x22')](_0x106ea6)){this[_0xfa48('0xd')][_0x522d13[_0xfa48('0x25')]][_0x106ea6]=_0x522d13[_0xfa48('0x2b')];}}}};Fax['prototype'][_0xfa48('0x1a')]=function(_0xd5a5f1){if(this['channels'][_0xd5a5f1[_0xfa48('0x25')]]){this[_0xfa48('0xd')][_0xd5a5f1[_0xfa48('0x25')]][_0xfa48('0x2f')]=_0xd5a5f1[_0xfa48('0x2f')];this[_0xfa48('0xd')][_0xd5a5f1[_0xfa48('0x25')]]['status']=_0xd5a5f1[_0xfa48('0x30')];this['channels'][_0xd5a5f1[_0xfa48('0x25')]][_0xfa48('0x24')]=_0xd5a5f1[_0xfa48('0x24')];this['channels'][_0xd5a5f1['uniqueid']][_0xfa48('0x31')]=_0xd5a5f1[_0xfa48('0x31')];logger['info'](_0xd5a5f1[_0xfa48('0x25')],_0xfa48('0x32'));}};Fax[_0xfa48('0x1f')][_0xfa48('0x33')]=function(_0x16d98b){var _0xe52592=this;if(this[_0xfa48('0xd')][_0x16d98b['uniqueid']]){var _0x2a917b=this['channels'][_0x16d98b[_0xfa48('0x25')]]['id'];this[_0xfa48('0xd')][_0x16d98b[_0xfa48('0x25')]][_0xfa48('0x34')]=_0x16d98b[_0xfa48('0x34')];this['channels'][_0x16d98b[_0xfa48('0x25')]]['pagestransferred']=_0x16d98b[_0xfa48('0x35')];this[_0xfa48('0xd')][_0x16d98b['uniqueid']][_0xfa48('0x24')]=_0x16d98b['localstationid'];this[_0xfa48('0xd')][_0x16d98b[_0xfa48('0x25')]][_0xfa48('0x36')]=_0x16d98b[_0xfa48('0x36')];this[_0xfa48('0xd')][_0x16d98b[_0xfa48('0x25')]]['transferrate']=_0x16d98b[_0xfa48('0x37')];this['channels'][_0x16d98b[_0xfa48('0x25')]][_0xfa48('0x31')]=_0x16d98b['filename'];this[_0xfa48('0xd')][_0x16d98b['uniqueid']][_0xfa48('0x38')]=_0x16d98b[_0xfa48('0x38')];if(this[_0xfa48('0xd')][_0x16d98b['uniqueid']]['pagestransferred']>0x0){tiff2pdf(_0x16d98b['filename'],_0xfa48('0x39'),function(_0x385cf8){logger[_0xfa48('0x28')](_0x16d98b[_0xfa48('0x25')],_0xfa48('0x3a'),_0x385cf8[_0xfa48('0x3b')]);attachment[_0xfa48('0x3c')]({'name':path[_0xfa48('0x3d')](_0x16d98b[_0xfa48('0x31')])['name'],'basename':path[_0xfa48('0x3d')](_0x16d98b[_0xfa48('0x31')])[_0xfa48('0x27')]+_0xfa48('0x3e'),'type':_0xfa48('0x3f')})[_0xfa48('0x40')](function(_0x25d558){faxAccount[_0xfa48('0x41')](_0x2a917b,{'from':_0x16d98b['calleridnum'],'mapKey':'fax','body':path[_0xfa48('0x3d')](_0x16d98b[_0xfa48('0x31')])[_0xfa48('0x27')]+_0xfa48('0x3e'),'AttachmentId':_0x25d558['id']})[_0xfa48('0x42')](function(_0x5b1d99){logger[_0xfa48('0x43')](_0x16d98b[_0xfa48('0x25')],_0xfa48('0x44'),JSON[_0xfa48('0x45')](_0x5b1d99));});})['catch'](function(_0x287448){logger[_0xfa48('0x43')](_0x16d98b[_0xfa48('0x25')],_0xfa48('0x46'),JSON[_0xfa48('0x45')](_0x287448));});});}logger['info'](_0x16d98b[_0xfa48('0x25')],'\x20receiveFax\x20');}};Fax['prototype'][_0xfa48('0x47')]=function(_0xb2e25b){if(this[_0xfa48('0x10')][_0xb2e25b[_0xfa48('0x25')]]){_['merge'](this['channels'][_0xb2e25b[_0xfa48('0x25')]],_[_0xfa48('0x48')](_0xb2e25b,[_0xfa48('0x49'),_0xfa48('0x4a')]));}};function updateStatus(_0x4fa617){if(_0x4fa617[_0xfa48('0x4b')]){faxMessage[_0xfa48('0x4c')](_0x4fa617[_0xfa48('0x4b')],{'failMessage':_0xfa48('0x4d')})[_0xfa48('0x40')](function(_0x178cf0){ut[_0xfa48('0x4e')]('[Fax][Message][Error][Updated]',_0x178cf0);})['catch'](function(_0x5eba2a){ut[_0xfa48('0x4e')](_0xfa48('0x4f'),_0x5eba2a);});}else{ut['logger'](_0xfa48('0x50'));}}Fax[_0xfa48('0x1f')]['updateStatus']=function(_0x49125f,_0x9587e,_0x327119){var _0x44cb6a=this;var _0x142572={};if(_0x9587e){_0x142572={'read':_0x9587e};}else{_0x142572={'failMessage':_0x327119};}if(this[_0xfa48('0x10')][_0x49125f][_0xfa48('0x4b')]){faxMessage[_0xfa48('0x4c')](this['actions'][_0x49125f]['AttachmentId'],_0x142572)['then'](function(_0x414eae){ut['logger']('[Fax][Message][Updated]',_0x414eae);})[_0xfa48('0x42')](function(_0x49f97c){ut[_0xfa48('0x4e')](_0xfa48('0x51'),_0x49f97c);})[_0xfa48('0x52')](function(){delete _0x44cb6a[_0xfa48('0xd')][_0x49125f];delete _0x44cb6a['actions'][_0x49125f];});}else{ut[_0xfa48('0x4e')](_0xfa48('0x53'));delete this[_0xfa48('0xd')][_0x49125f];delete this[_0xfa48('0x10')][_0x49125f];}};Fax[_0xfa48('0x1f')]['syncHangup']=function(_0xa19416){var _0x456413=this;if(this[_0xfa48('0xd')][_0xa19416['uniqueid']]){if(this[_0xfa48('0x10')][_0xa19416[_0xfa48('0x25')]]){this[_0xfa48('0xd')][_0xa19416[_0xfa48('0x25')]]['id']=this[_0xfa48('0x10')][_0xa19416[_0xfa48('0x25')]]['id'];this[_0xfa48('0xd')][_0xa19416[_0xfa48('0x25')]][_0xfa48('0x27')]=this[_0xfa48('0x10')][_0xa19416[_0xfa48('0x25')]][_0xfa48('0x27')];}logger[_0xfa48('0x28')](_0xa19416[_0xfa48('0x25')],this[_0xfa48('0x10')][_0xa19416[_0xfa48('0x25')]]?'\x20hangup\x20sendFile\x20':_0xfa48('0x54'),JSON[_0xfa48('0x45')](this[_0xfa48('0xd')][_0xa19416[_0xfa48('0x25')]]));if(this['actions'][_0xa19416[_0xfa48('0x25')]]){if(this['channels'][_0xa19416[_0xfa48('0x25')]][_0xfa48('0x55')]>0x0&&this[_0xfa48('0xd')][_0xa19416['uniqueid']][_0xfa48('0x19')]===_0xfa48('0x56')){this[_0xfa48('0x57')](_0xa19416[_0xfa48('0x25')],!![]);}else{this[_0xfa48('0x57')](_0xa19416['uniqueid'],![],this[_0xfa48('0xd')][_0xa19416[_0xfa48('0x25')]][_0xfa48('0x58')]?_0xfa48('0x59')+this[_0xfa48('0xd')][_0xa19416[_0xfa48('0x25')]][_0xfa48('0x58')]:_0xfa48('0x5a'));}}}};Fax[_0xfa48('0x1f')][_0xfa48('0x5b')]=function(_0x273b46){var _0x31ad48=this;return new BPromise(function(_0x3b47ff,_0x5d2867){_0x31ad48[_0xfa48('0x5c')](_0x273b46,_0x31ad48[_0xfa48('0xe')][_0x273b46[_0xfa48('0x5d')]])['then'](function(_0x40bec7){ut[_0xfa48('0x4e')](_0xfa48('0x5e'),_0x40bec7);_0x3b47ff(ut[_0xfa48('0x3b')](_0xfa48('0x5f')));})[_0xfa48('0x42')](function(_0x2b9425){ut[_0xfa48('0x4e')](_0xfa48('0x60'),_0x2b9425);updateStatus(_0x273b46);_0x5d2867(ut[_0xfa48('0x3b')]('Fax\x20failed'));});});};Fax[_0xfa48('0x1f')][_0xfa48('0x61')]=function(_0x30b0d8){var _0x2000ab=this;return new BPromise(function(_0x5429e8,_0x156742){var _0x14c635=_0x2000ab[_0xfa48('0x62')](_0x2000ab[_0xfa48('0xe')][_0x30b0d8[_0xfa48('0x5d')]]['Trunk'],_0x30b0d8);if(_0x14c635[_0xfa48('0x63')]){_0x5429e8(ut[_0xfa48('0x3b')](_0xfa48('0x64')));}else{_0x156742(ut[_0xfa48('0x3b')](_0x14c635['message']));}});};function originate(_0x35f123,_0x26f251,_0x28ec0c){return new BPromise(function(_0x1f8116,_0x8743ce){if(_0x28ec0c){var _0xaf7975=util[_0xfa48('0x65')](_0xfa48('0x66'),path[_0xfa48('0x3d')](_0x35f123[_0xfa48('0x4')])['name'],_0x35f123['path']);gs[_0xfa48('0x67')](_0xaf7975)[_0xfa48('0x40')](function(){_0x35f123[_0xfa48('0x4')]=util[_0xfa48('0x65')]('%s/%s.tif',path['parse'](_0x35f123[_0xfa48('0x4')])[_0xfa48('0x68')],path[_0xfa48('0x3d')](_0x35f123['path'])[_0xfa48('0x27')]);_0x26f251[_0xfa48('0x69')]=_0x35f123[_0xfa48('0x4')];ut['logger'](_0xfa48('0x6a'),'');ami[_0xfa48('0x6b')](ut[_0xfa48('0x6c')](_0x35f123,_0x26f251))[_0xfa48('0x40')](function(_0x4b7daf){_0x1f8116(_0x4b7daf);})['catch'](function(_0x60c8ea){_0x8743ce(_0x60c8ea);});})[_0xfa48('0x42')](function(_0x42116e){_0x8743ce(_0x42116e);});}else{ami[_0xfa48('0x6b')](ut[_0xfa48('0x6c')](_0x35f123,_0x26f251))[_0xfa48('0x40')](function(_0x468e7d){_0x1f8116(_0x468e7d);})[_0xfa48('0x42')](function(_0x34e7c5){_0x8743ce(_0x34e7c5);});}});}Fax[_0xfa48('0x1f')][_0xfa48('0x5c')]=function(_0xa19419,_0x4d6267){var _0x4b69ea=this;return new BPromise(function(_0x10e0a7,_0x40425e){var _0x22b285=uuid['v4']();var _0x5da9e8=ut[_0xfa48('0x6d')](_0xa19419,_0x4d6267);_0x4b69ea[_0xfa48('0x10')][_0x22b285]=new Action(_0x22b285,_0x4d6267,_0xa19419);var _0x508182=_0x4b69ea[_0xfa48('0x10')][_0x22b285];if(path[_0xfa48('0x3d')](_0xa19419['path'])[_0xfa48('0x6e')]===_0xfa48('0x3e')){originate(_0x508182,_0x5da9e8,!![])[_0xfa48('0x40')](function(_0x377afc){_0x10e0a7(_0x377afc);})[_0xfa48('0x42')](function(_0x100b20){_0x40425e(_0x100b20);});}else{originate(_0x508182,_0x5da9e8,![])['then'](function(_0xb5354b){_0x10e0a7(_0xb5354b);})[_0xfa48('0x42')](function(_0x59ea85){_0x40425e(_0x59ea85);});}});};Fax[_0xfa48('0x1f')][_0xfa48('0x6f')]=function(_0x43544f){return!_[_0xfa48('0x70')](_0x43544f)&&!_[_0xfa48('0x70')](_0x43544f[_0xfa48('0x27')])&&!_[_0xfa48('0x70')](_0x43544f[_0xfa48('0x71')])&&_0x43544f['active']&&!_[_0xfa48('0x70')](this[_0xfa48('0xf')][util['format'](_0xfa48('0x72'),_0x43544f[_0xfa48('0x27')])])&&!_['isNil'](this[_0xfa48('0xf')][util[_0xfa48('0x65')](_0xfa48('0x72'),_0x43544f['name'])][_0xfa48('0x30')])&&(this[_0xfa48('0xf')][util[_0xfa48('0x65')]('SIP/%s',_0x43544f[_0xfa48('0x27')])][_0xfa48('0x30')]==='reachable'||this[_0xfa48('0xf')][util[_0xfa48('0x65')](_0xfa48('0x72'),_0x43544f[_0xfa48('0x27')])][_0xfa48('0x30')]===_0xfa48('0x73'));};Fax[_0xfa48('0x1f')][_0xfa48('0x62')]=function(_0x456ddc,_0x455a6d){if(!this[_0xfa48('0x6f')](_0x456ddc)){ut['logger'](_0xfa48('0x74'),_[_0xfa48('0x70')](_0x456ddc)?'':_0x456ddc[_0xfa48('0x27')]+'\x20Trunk\x20is\x20not\x20active');return{'isValid':![],'message':'Trunk\x20not\x20working\x20correctly'};}if(_[_0xfa48('0x70')](_0x455a6d[_0xfa48('0x7')])){ut[_0xfa48('0x4e')]('[Fax][checkTrunk][Empty\x20Fax]',JSON[_0xfa48('0x45')](_0x455a6d));return{'isValid':![],'message':_0xfa48('0x75')};}if(_[_0xfa48('0x70')](_0x455a6d[_0xfa48('0x4')])){ut[_0xfa48('0x4e')](_0xfa48('0x76'),JSON[_0xfa48('0x45')](_0x455a6d));return{'isValid':![],'message':'Empty\x20path'};}return{'isValid':!![],'message':'Is\x20Valid'};};module[_0xfa48('0x77')]=Fax;