c3b96cfc6cd2f1321831c33e90db54883865fdad
[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 _0x6bf9=['isNil','active','SIP/%s','[Fax][checkTrunk][Trunk\x20not\x20working\x20correctly]','\x20Trunk\x20is\x20not\x20active','Trunk\x20not\x20working\x20correctly','Empty\x20Fax','[Fax][checkTrunk][Empty\x20path]','Is\x20Valid','util','lodash','bluebird','uuid','moment','../../../config/logger','fax','../ami','./rpcDb','./channel','./util/util','../rpc/faxAccount','../rpc/faxMessage','faxAccounts','trunks','actions','newchannel','bind','newstate','syncNewState','syncNewExten','faxstatus','syncFaxStatus','syncReceiveFax','hangup','syncHangup','syncOriginateResponse','prototype','syncNewchannel','context','from-voip-provider','hasOwnProperty','localstationid','channels','uniqueid','channel','name','\x20syncNewchannel\x20','info','syncVarSet','variable','channelstatedesc','operation','status','filename','\x20syncFaxStatus\x20','remotestationid','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','create','parse','.pdf','application/pdf','then','notify','catch','error','\x20notify\x20','stringify','\x20receiveFax\x20','pick','response','AttachmentId','SENDING\x20FAILED\x20','logger','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','updateStatus','update','[Fax][Message][Updated]','[Fax][Message][NoUpdate1]','finally','[Fax][Message][NoUpdate2]','\x20hangup\x20sendFile\x20','\x20hangup\x20faxRiceive\x20','faxpages','SUCCESS','faxstatusstring','SENDING\x20FAILED:\x20','execOriginate','FaxAccountId','[Fax][execOriginate][success]','message','Fax\x20received','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','XMF-PATH','Convert\x20pdf\x20to\x20tif','Action','addVariables','ext','isTrunkReachable'];(function(_0x419bb9,_0x247ffa){var _0x594006=function(_0x795a74){while(--_0x795a74){_0x419bb9['push'](_0x419bb9['shift']());}};_0x594006(++_0x247ffa);}(_0x6bf9,0x1cd));var _0x96bf=function(_0xd4636c,_0x34dbf6){_0xd4636c=_0xd4636c-0x0;var _0x5ee17d=_0x6bf9[_0xd4636c];return _0x5ee17d;};'use strict';var util=require(_0x96bf('0x0'));var _=require(_0x96bf('0x1'));var BPromise=require(_0x96bf('0x2'));var uuid=require(_0x96bf('0x3'));var moment=require(_0x96bf('0x4'));var path=require('path');var tiff2pdf=require('tiff2pdf');var gs=require('./util/gs');var logger=require(_0x96bf('0x5'))(_0x96bf('0x6'));var ami=require(_0x96bf('0x7'));var rpcDb=require(_0x96bf('0x8'));var Channel=require(_0x96bf('0x9'));var ut=require(_0x96bf('0xa'));var Action=require('./action');var faxAccount=require(_0x96bf('0xb'));var attachment=require('../rpc/attachment');var faxMessage=require(_0x96bf('0xc'));function Fax(_0x100fa4){this['channels']={};this[_0x96bf('0xd')]=_0x100fa4[_0x96bf('0xd')];this[_0x96bf('0xe')]=_0x100fa4['trunks'];this[_0x96bf('0xf')]={};ami['on'](_0x96bf('0x10'),this['syncNewchannel'][_0x96bf('0x11')](this));ami['on']('varset',this['syncVarSet']['bind'](this));ami['on'](_0x96bf('0x12'),this[_0x96bf('0x13')][_0x96bf('0x11')](this));ami['on']('newexten',this[_0x96bf('0x14')][_0x96bf('0x11')](this));ami['on'](_0x96bf('0x15'),this[_0x96bf('0x16')][_0x96bf('0x11')](this));ami['on']('receivefax',this[_0x96bf('0x17')][_0x96bf('0x11')](this));ami['on'](_0x96bf('0x18'),this[_0x96bf('0x19')][_0x96bf('0x11')](this));ami['on']('originateresponse',this[_0x96bf('0x1a')]['bind'](this));}Fax[_0x96bf('0x1b')][_0x96bf('0x1c')]=function(_0x2ca19e){if(_0x2ca19e[_0x96bf('0x1d')]===_0x96bf('0x1e')){for(var _0x37ec0d in this[_0x96bf('0xd')]){if(this[_0x96bf('0xd')][_0x96bf('0x1f')](_0x37ec0d)){if(_0x2ca19e['exten']===this['faxAccounts'][_0x37ec0d][_0x96bf('0x20')]){this[_0x96bf('0x21')][_0x2ca19e[_0x96bf('0x22')]]={};this['channels'][_0x2ca19e[_0x96bf('0x22')]][_0x96bf('0x23')]=_0x2ca19e[_0x96bf('0x23')];this[_0x96bf('0x21')][_0x2ca19e[_0x96bf('0x22')]]['id']=this[_0x96bf('0xd')][_0x37ec0d]['id'];this['channels'][_0x2ca19e['uniqueid']][_0x96bf('0x24')]=this[_0x96bf('0xd')][_0x37ec0d][_0x96bf('0x24')];logger['info'](_0x2ca19e[_0x96bf('0x22')],_0x96bf('0x25'));}}}}if(this[_0x96bf('0xf')][_0x2ca19e[_0x96bf('0x22')]]){this[_0x96bf('0x21')][_0x2ca19e[_0x96bf('0x22')]]={};this[_0x96bf('0x21')][_0x2ca19e['uniqueid']]['channel']=_0x2ca19e[_0x96bf('0x23')];logger[_0x96bf('0x26')](_0x2ca19e[_0x96bf('0x22')],_0x96bf('0x25'));}};Fax[_0x96bf('0x1b')][_0x96bf('0x27')]=function(_0x4152f6){if(this[_0x96bf('0x21')][_0x4152f6[_0x96bf('0x22')]]){for(var _0x3e6bfc in _0x4152f6[_0x96bf('0x28')]){if(_0x4152f6[_0x96bf('0x28')][_0x96bf('0x1f')](_0x3e6bfc)){this[_0x96bf('0x21')][_0x4152f6['uniqueid']][_0x3e6bfc]=_0x4152f6['value'];}}}};Fax['prototype'][_0x96bf('0x13')]=function(_0xe05730){if(this[_0x96bf('0x21')][_0xe05730['uniqueid']]){this[_0x96bf('0x21')][_0xe05730[_0x96bf('0x22')]]['channelstate']=_0xe05730['channelstate'];this[_0x96bf('0x21')][_0xe05730[_0x96bf('0x22')]][_0x96bf('0x29')]=_0xe05730[_0x96bf('0x29')];}};Fax[_0x96bf('0x1b')]['syncNewExten']=function(_0x20e7c4){if(this[_0x96bf('0x21')][_0x20e7c4['uniqueid']]){for(var _0x35b2f6 in _0x20e7c4['variable']){if(_0x20e7c4[_0x96bf('0x28')][_0x96bf('0x1f')](_0x35b2f6)){this[_0x96bf('0x21')][_0x20e7c4['uniqueid']][_0x35b2f6]=_0x20e7c4['value'];}}}};Fax['prototype'][_0x96bf('0x16')]=function(_0x126eeb){if(this[_0x96bf('0x21')][_0x126eeb[_0x96bf('0x22')]]){this[_0x96bf('0x21')][_0x126eeb[_0x96bf('0x22')]][_0x96bf('0x2a')]=_0x126eeb[_0x96bf('0x2a')];this[_0x96bf('0x21')][_0x126eeb[_0x96bf('0x22')]]['status']=_0x126eeb[_0x96bf('0x2b')];this[_0x96bf('0x21')][_0x126eeb[_0x96bf('0x22')]]['localstationid']=_0x126eeb[_0x96bf('0x20')];this[_0x96bf('0x21')][_0x126eeb[_0x96bf('0x22')]][_0x96bf('0x2c')]=_0x126eeb['filename'];logger[_0x96bf('0x26')](_0x126eeb['uniqueid'],_0x96bf('0x2d'));}};Fax['prototype'][_0x96bf('0x17')]=function(_0x4595d5){var _0x4646c7=this;if(this[_0x96bf('0x21')][_0x4595d5[_0x96bf('0x22')]]){var _0x1cbeec=this[_0x96bf('0x21')][_0x4595d5[_0x96bf('0x22')]]['id'];this[_0x96bf('0x21')][_0x4595d5['uniqueid']][_0x96bf('0x2e')]=_0x4595d5['remotestationid'];this[_0x96bf('0x21')][_0x4595d5['uniqueid']]['pagestransferred']=_0x4595d5[_0x96bf('0x2f')];this[_0x96bf('0x21')][_0x4595d5[_0x96bf('0x22')]][_0x96bf('0x20')]=_0x4595d5['localstationid'];this[_0x96bf('0x21')][_0x4595d5[_0x96bf('0x22')]]['resolution']=_0x4595d5[_0x96bf('0x30')];this['channels'][_0x4595d5[_0x96bf('0x22')]]['transferrate']=_0x4595d5[_0x96bf('0x31')];this[_0x96bf('0x21')][_0x4595d5[_0x96bf('0x22')]][_0x96bf('0x2c')]=_0x4595d5['filename'];this[_0x96bf('0x21')][_0x4595d5[_0x96bf('0x22')]][_0x96bf('0x32')]=_0x4595d5[_0x96bf('0x32')];if(this[_0x96bf('0x21')][_0x4595d5[_0x96bf('0x22')]]['pagestransferred']>0x0){tiff2pdf(_0x4595d5[_0x96bf('0x2c')],_0x96bf('0x33'),function(_0x888bb){logger[_0x96bf('0x26')](_0x4595d5[_0x96bf('0x22')],'\x20result\x20conversion\x20file\x20',_0x888bb['message']);attachment[_0x96bf('0x34')]({'name':path[_0x96bf('0x35')](_0x4595d5[_0x96bf('0x2c')])[_0x96bf('0x24')],'basename':path[_0x96bf('0x35')](_0x4595d5[_0x96bf('0x2c')])[_0x96bf('0x24')]+_0x96bf('0x36'),'type':_0x96bf('0x37')})[_0x96bf('0x38')](function(_0x54546b){faxAccount[_0x96bf('0x39')](_0x1cbeec,{'from':_0x4595d5[_0x96bf('0x32')],'mapKey':_0x96bf('0x6'),'body':path[_0x96bf('0x35')](_0x4595d5[_0x96bf('0x2c')])[_0x96bf('0x24')]+_0x96bf('0x36'),'AttachmentId':_0x54546b['id']})[_0x96bf('0x3a')](function(_0x4d11b7){logger[_0x96bf('0x3b')](_0x4595d5['uniqueid'],_0x96bf('0x3c'),JSON['stringify'](_0x4d11b7));});})[_0x96bf('0x3a')](function(_0x5286de){logger[_0x96bf('0x3b')](_0x4595d5[_0x96bf('0x22')],'\x20receiveFax\x20',JSON[_0x96bf('0x3d')](_0x5286de));});});}logger[_0x96bf('0x26')](_0x4595d5[_0x96bf('0x22')],_0x96bf('0x3e'));}};Fax[_0x96bf('0x1b')][_0x96bf('0x1a')]=function(_0x3149c3){if(this[_0x96bf('0xf')][_0x3149c3[_0x96bf('0x22')]]){_['merge'](this[_0x96bf('0x21')][_0x3149c3[_0x96bf('0x22')]],_[_0x96bf('0x3f')](_0x3149c3,[_0x96bf('0x40'),'reason']));}};function updateStatus(_0x3ae7c1){if(_0x3ae7c1[_0x96bf('0x41')]){faxMessage['update'](_0x3ae7c1[_0x96bf('0x41')],{'failMessage':_0x96bf('0x42')})[_0x96bf('0x38')](function(_0x54e2a6){ut[_0x96bf('0x43')]('[Fax][Message][Error][Updated]',_0x54e2a6);})[_0x96bf('0x3a')](function(_0x5c4391){ut['logger'](_0x96bf('0x44'),_0x5c4391);});}else{ut[_0x96bf('0x43')](_0x96bf('0x45'));}}Fax[_0x96bf('0x1b')][_0x96bf('0x46')]=function(_0x58a65d,_0x4b5297,_0x3afb30){var _0x1b44b8=this;var _0x315571={};if(_0x4b5297){_0x315571={'read':_0x4b5297};}else{_0x315571={'failMessage':_0x3afb30};}if(this[_0x96bf('0xf')][_0x58a65d][_0x96bf('0x41')]){faxMessage[_0x96bf('0x47')](this[_0x96bf('0xf')][_0x58a65d][_0x96bf('0x41')],_0x315571)['then'](function(_0x2d1c6a){ut[_0x96bf('0x43')](_0x96bf('0x48'),_0x2d1c6a);})[_0x96bf('0x3a')](function(_0x102f35){ut[_0x96bf('0x43')](_0x96bf('0x49'),_0x102f35);})[_0x96bf('0x4a')](function(){delete _0x1b44b8['channels'][_0x58a65d];delete _0x1b44b8[_0x96bf('0xf')][_0x58a65d];});}else{ut[_0x96bf('0x43')](_0x96bf('0x4b'));delete this[_0x96bf('0x21')][_0x58a65d];delete this['actions'][_0x58a65d];}};Fax[_0x96bf('0x1b')][_0x96bf('0x19')]=function(_0x27ccb6){var _0x1a05c1=this;if(this[_0x96bf('0x21')][_0x27ccb6[_0x96bf('0x22')]]){if(this[_0x96bf('0xf')][_0x27ccb6[_0x96bf('0x22')]]){this['channels'][_0x27ccb6[_0x96bf('0x22')]]['id']=this[_0x96bf('0xf')][_0x27ccb6[_0x96bf('0x22')]]['id'];this[_0x96bf('0x21')][_0x27ccb6[_0x96bf('0x22')]][_0x96bf('0x24')]=this[_0x96bf('0xf')][_0x27ccb6[_0x96bf('0x22')]][_0x96bf('0x24')];}logger[_0x96bf('0x26')](_0x27ccb6['uniqueid'],this[_0x96bf('0xf')][_0x27ccb6[_0x96bf('0x22')]]?_0x96bf('0x4c'):_0x96bf('0x4d'),JSON[_0x96bf('0x3d')](this[_0x96bf('0x21')][_0x27ccb6[_0x96bf('0x22')]]));if(this[_0x96bf('0xf')][_0x27ccb6[_0x96bf('0x22')]]){if(this[_0x96bf('0x21')][_0x27ccb6[_0x96bf('0x22')]][_0x96bf('0x4e')]>0x0&&this[_0x96bf('0x21')][_0x27ccb6[_0x96bf('0x22')]]['faxstatus']===_0x96bf('0x4f')){this[_0x96bf('0x46')](_0x27ccb6['uniqueid'],!![]);}else{this[_0x96bf('0x46')](_0x27ccb6['uniqueid'],![],this[_0x96bf('0x21')][_0x27ccb6[_0x96bf('0x22')]][_0x96bf('0x50')]?_0x96bf('0x51')+this['channels'][_0x27ccb6[_0x96bf('0x22')]]['faxstatusstring']:'SENDING\x20FAILED');}}}};Fax[_0x96bf('0x1b')]['send']=function(_0x312ccb){var _0x17a9a8=this;return new BPromise(function(_0x2050bd,_0x58fbfd){_0x17a9a8[_0x96bf('0x52')](_0x312ccb,_0x17a9a8[_0x96bf('0xd')][_0x312ccb[_0x96bf('0x53')]])['then'](function(_0xb2e120){ut[_0x96bf('0x43')](_0x96bf('0x54'),_0xb2e120);_0x2050bd(ut[_0x96bf('0x55')](_0x96bf('0x56')));})['catch'](function(_0x39367e){ut[_0x96bf('0x43')]('[Fax][execOriginate][failure]',_0x39367e);updateStatus(_0x312ccb);_0x58fbfd(ut[_0x96bf('0x55')](_0x96bf('0x57')));});});};Fax[_0x96bf('0x1b')][_0x96bf('0x58')]=function(_0x48be11){var _0x31b3d0=this;return new BPromise(function(_0x42b55a,_0x15340e){var _0x3a0b09=_0x31b3d0[_0x96bf('0x59')](_0x31b3d0[_0x96bf('0xd')][_0x48be11[_0x96bf('0x53')]][_0x96bf('0x5a')],_0x48be11);if(_0x3a0b09[_0x96bf('0x5b')]){_0x42b55a(ut['message'](_0x96bf('0x5c')));}else{_0x15340e(ut[_0x96bf('0x55')](_0x3a0b09[_0x96bf('0x55')]));}});};function originate(_0x5f240c,_0x34db8a,_0xa0076){return new BPromise(function(_0x15ce03,_0x2b326b){if(_0xa0076){var _0x585eb3=util[_0x96bf('0x5d')](_0x96bf('0x5e'),path['parse'](_0x5f240c['path'])[_0x96bf('0x24')],_0x5f240c[_0x96bf('0x5f')]);gs[_0x96bf('0x60')](_0x585eb3)[_0x96bf('0x38')](function(){_0x5f240c[_0x96bf('0x5f')]=util['format'](_0x96bf('0x61'),path[_0x96bf('0x35')](_0x5f240c[_0x96bf('0x5f')])['dir'],path['parse'](_0x5f240c['path'])['name']);_0x34db8a[_0x96bf('0x62')]=_0x5f240c[_0x96bf('0x5f')];ut[_0x96bf('0x43')](_0x96bf('0x63'),'');ami[_0x96bf('0x64')](ut['variablesAction'](_0x5f240c,_0x34db8a))[_0x96bf('0x38')](function(_0x146e10){_0x15ce03(_0x146e10);})[_0x96bf('0x3a')](function(_0x5cde67){_0x2b326b(_0x5cde67);});})[_0x96bf('0x3a')](function(_0x122c61){_0x2b326b(_0x122c61);});}else{ami[_0x96bf('0x64')](ut['variablesAction'](_0x5f240c,_0x34db8a))[_0x96bf('0x38')](function(_0xd20219){_0x15ce03(_0xd20219);})[_0x96bf('0x3a')](function(_0x325c73){_0x2b326b(_0x325c73);});}});}Fax[_0x96bf('0x1b')][_0x96bf('0x52')]=function(_0x5dca6f,_0x462e5d){var _0x2b3150=this;return new BPromise(function(_0x4dffea,_0xef1ab5){var _0x2740b2=uuid['v4']();var _0x7cec90=ut[_0x96bf('0x65')](_0x5dca6f,_0x462e5d);_0x2b3150[_0x96bf('0xf')][_0x2740b2]=new Action(_0x2740b2,_0x462e5d,_0x5dca6f);var _0x5b52f3=_0x2b3150[_0x96bf('0xf')][_0x2740b2];if(path[_0x96bf('0x35')](_0x5dca6f[_0x96bf('0x5f')])[_0x96bf('0x66')]==='.pdf'){originate(_0x5b52f3,_0x7cec90,!![])[_0x96bf('0x38')](function(_0xfd918c){_0x4dffea(_0xfd918c);})[_0x96bf('0x3a')](function(_0x29983a){_0xef1ab5(_0x29983a);});}else{originate(_0x5b52f3,_0x7cec90,![])[_0x96bf('0x38')](function(_0x2a786b){_0x4dffea(_0x2a786b);})[_0x96bf('0x3a')](function(_0x2ff4f5){_0xef1ab5(_0x2ff4f5);});}});};Fax[_0x96bf('0x1b')][_0x96bf('0x67')]=function(_0x2015c0){return!_[_0x96bf('0x68')](_0x2015c0)&&!_['isNil'](_0x2015c0[_0x96bf('0x24')])&&!_[_0x96bf('0x68')](_0x2015c0[_0x96bf('0x69')])&&_0x2015c0[_0x96bf('0x69')]&&!_[_0x96bf('0x68')](this[_0x96bf('0xe')][util[_0x96bf('0x5d')](_0x96bf('0x6a'),_0x2015c0[_0x96bf('0x24')])])&&!_[_0x96bf('0x68')](this[_0x96bf('0xe')][util[_0x96bf('0x5d')]('SIP/%s',_0x2015c0['name'])][_0x96bf('0x2b')])&&(this[_0x96bf('0xe')][util[_0x96bf('0x5d')]('SIP/%s',_0x2015c0['name'])][_0x96bf('0x2b')]==='reachable'||this['trunks'][util[_0x96bf('0x5d')](_0x96bf('0x6a'),_0x2015c0[_0x96bf('0x24')])][_0x96bf('0x2b')]==='unmonitored');};Fax[_0x96bf('0x1b')][_0x96bf('0x59')]=function(_0x377d0d,_0xcb8539){if(!this[_0x96bf('0x67')](_0x377d0d)){ut['logger'](_0x96bf('0x6b'),_[_0x96bf('0x68')](_0x377d0d)?'':_0x377d0d[_0x96bf('0x24')]+_0x96bf('0x6c'));return{'isValid':![],'message':_0x96bf('0x6d')};}if(_[_0x96bf('0x68')](_0xcb8539['fax'])){ut['logger']('[Fax][checkTrunk][Empty\x20Fax]',JSON['stringify'](_0xcb8539));return{'isValid':![],'message':_0x96bf('0x6e')};}if(_[_0x96bf('0x68')](_0xcb8539['path'])){ut[_0x96bf('0x43')](_0x96bf('0x6f'),JSON[_0x96bf('0x3d')](_0xcb8539));return{'isValid':![],'message':'Empty\x20path'};}return{'isValid':!![],'message':_0x96bf('0x70')};};module['exports']=Fax;