8642f09a55e9ba80e5c6772655be1555fdd44d74
[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 _0x5352=['create','parse','.pdf','application/pdf','then','notify','\x20notify\x20','stringify','catch','error','\x20receiveFax\x20','merge','pick','reason','AttachmentId','update','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','send','execOriginate','FaxAccountId','Fax\x20received','[Fax][execOriginate][failure]','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','variablesAction','addVariables','ext','isTrunkReachable','isNil','active','reachable','SIP/%s','unmonitored','checkTrunk','[Fax][checkTrunk][Trunk\x20not\x20working\x20correctly]','Trunk\x20not\x20working\x20correctly','fax','[Fax][checkTrunk][Empty\x20Fax]','Empty\x20Fax','Is\x20Valid','exports','util','bluebird','uuid','moment','path','tiff2pdf','./util/gs','../../../config/logger','../ami','./rpcDb','./util/util','./action','../rpc/attachment','../rpc/faxMessage','channels','faxAccounts','trunks','syncNewchannel','bind','varset','syncVarSet','newstate','syncNewState','newexten','faxstatus','syncFaxStatus','receivefax','syncReceiveFax','hangup','syncHangup','syncOriginateResponse','prototype','context','hasOwnProperty','exten','localstationid','uniqueid','channel','name','info','\x20syncNewchannel\x20','actions','variable','channelstate','channelstatedesc','operation','status','filename','\x20syncFaxStatus\x20','remotestationid','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','\x20result\x20conversion\x20file\x20','message'];(function(_0x507dd0,_0x44680f){var _0x186957=function(_0x28e894){while(--_0x28e894){_0x507dd0['push'](_0x507dd0['shift']());}};_0x186957(++_0x44680f);}(_0x5352,0x12c));var _0x2535=function(_0x1ad7fc,_0x444145){_0x1ad7fc=_0x1ad7fc-0x0;var _0x3bb433=_0x5352[_0x1ad7fc];return _0x3bb433;};'use strict';var util=require(_0x2535('0x0'));var _=require('lodash');var BPromise=require(_0x2535('0x1'));var uuid=require(_0x2535('0x2'));var moment=require(_0x2535('0x3'));var path=require(_0x2535('0x4'));var tiff2pdf=require(_0x2535('0x5'));var gs=require(_0x2535('0x6'));var logger=require(_0x2535('0x7'))('fax');var ami=require(_0x2535('0x8'));var rpcDb=require(_0x2535('0x9'));var Channel=require('./channel');var ut=require(_0x2535('0xa'));var Action=require(_0x2535('0xb'));var faxAccount=require('../rpc/faxAccount');var attachment=require(_0x2535('0xc'));var faxMessage=require(_0x2535('0xd'));function Fax(_0x3d0770){this[_0x2535('0xe')]={};this['faxAccounts']=_0x3d0770[_0x2535('0xf')];this[_0x2535('0x10')]=_0x3d0770[_0x2535('0x10')];this['actions']={};ami['on']('newchannel',this[_0x2535('0x11')][_0x2535('0x12')](this));ami['on'](_0x2535('0x13'),this[_0x2535('0x14')]['bind'](this));ami['on'](_0x2535('0x15'),this[_0x2535('0x16')][_0x2535('0x12')](this));ami['on'](_0x2535('0x17'),this['syncNewExten']['bind'](this));ami['on'](_0x2535('0x18'),this[_0x2535('0x19')][_0x2535('0x12')](this));ami['on'](_0x2535('0x1a'),this[_0x2535('0x1b')][_0x2535('0x12')](this));ami['on'](_0x2535('0x1c'),this[_0x2535('0x1d')][_0x2535('0x12')](this));ami['on']('originateresponse',this[_0x2535('0x1e')][_0x2535('0x12')](this));}Fax[_0x2535('0x1f')][_0x2535('0x11')]=function(_0x34178a){if(_0x34178a[_0x2535('0x20')]==='from-voip-provider'){for(var _0x485128 in this[_0x2535('0xf')]){if(this[_0x2535('0xf')][_0x2535('0x21')](_0x485128)){if(_0x34178a[_0x2535('0x22')]===this[_0x2535('0xf')][_0x485128][_0x2535('0x23')]){this['channels'][_0x34178a['uniqueid']]={};this[_0x2535('0xe')][_0x34178a[_0x2535('0x24')]][_0x2535('0x25')]=_0x34178a[_0x2535('0x25')];this[_0x2535('0xe')][_0x34178a[_0x2535('0x24')]]['id']=this['faxAccounts'][_0x485128]['id'];this[_0x2535('0xe')][_0x34178a['uniqueid']][_0x2535('0x26')]=this[_0x2535('0xf')][_0x485128][_0x2535('0x26')];logger[_0x2535('0x27')](_0x34178a[_0x2535('0x24')],_0x2535('0x28'));}}}}if(this[_0x2535('0x29')][_0x34178a[_0x2535('0x24')]]){this[_0x2535('0xe')][_0x34178a[_0x2535('0x24')]]={};this[_0x2535('0xe')][_0x34178a['uniqueid']][_0x2535('0x25')]=_0x34178a[_0x2535('0x25')];logger['info'](_0x34178a[_0x2535('0x24')],_0x2535('0x28'));}};Fax[_0x2535('0x1f')][_0x2535('0x14')]=function(_0xd8c849){if(this['channels'][_0xd8c849[_0x2535('0x24')]]){for(var _0x141341 in _0xd8c849[_0x2535('0x2a')]){if(_0xd8c849[_0x2535('0x2a')]['hasOwnProperty'](_0x141341)){this['channels'][_0xd8c849[_0x2535('0x24')]][_0x141341]=_0xd8c849['value'];}}}};Fax[_0x2535('0x1f')][_0x2535('0x16')]=function(_0x26b64c){if(this[_0x2535('0xe')][_0x26b64c[_0x2535('0x24')]]){this[_0x2535('0xe')][_0x26b64c[_0x2535('0x24')]][_0x2535('0x2b')]=_0x26b64c[_0x2535('0x2b')];this[_0x2535('0xe')][_0x26b64c[_0x2535('0x24')]][_0x2535('0x2c')]=_0x26b64c[_0x2535('0x2c')];}};Fax[_0x2535('0x1f')]['syncNewExten']=function(_0x3488f7){if(this[_0x2535('0xe')][_0x3488f7[_0x2535('0x24')]]){for(var _0x5ac1ca in _0x3488f7['variable']){if(_0x3488f7[_0x2535('0x2a')][_0x2535('0x21')](_0x5ac1ca)){this[_0x2535('0xe')][_0x3488f7[_0x2535('0x24')]][_0x5ac1ca]=_0x3488f7['value'];}}}};Fax[_0x2535('0x1f')]['syncFaxStatus']=function(_0x4e07a8){if(this[_0x2535('0xe')][_0x4e07a8[_0x2535('0x24')]]){this[_0x2535('0xe')][_0x4e07a8[_0x2535('0x24')]][_0x2535('0x2d')]=_0x4e07a8[_0x2535('0x2d')];this[_0x2535('0xe')][_0x4e07a8['uniqueid']][_0x2535('0x2e')]=_0x4e07a8[_0x2535('0x2e')];this[_0x2535('0xe')][_0x4e07a8[_0x2535('0x24')]][_0x2535('0x23')]=_0x4e07a8['localstationid'];this[_0x2535('0xe')][_0x4e07a8[_0x2535('0x24')]][_0x2535('0x2f')]=_0x4e07a8[_0x2535('0x2f')];logger[_0x2535('0x27')](_0x4e07a8[_0x2535('0x24')],_0x2535('0x30'));}};Fax[_0x2535('0x1f')][_0x2535('0x1b')]=function(_0xd0eb05){var _0xf99355=this;if(this[_0x2535('0xe')][_0xd0eb05['uniqueid']]){var _0xeba365=this[_0x2535('0xe')][_0xd0eb05[_0x2535('0x24')]]['id'];this[_0x2535('0xe')][_0xd0eb05[_0x2535('0x24')]][_0x2535('0x31')]=_0xd0eb05['remotestationid'];this['channels'][_0xd0eb05[_0x2535('0x24')]][_0x2535('0x32')]=_0xd0eb05[_0x2535('0x32')];this[_0x2535('0xe')][_0xd0eb05[_0x2535('0x24')]][_0x2535('0x23')]=_0xd0eb05[_0x2535('0x23')];this[_0x2535('0xe')][_0xd0eb05[_0x2535('0x24')]][_0x2535('0x33')]=_0xd0eb05['resolution'];this[_0x2535('0xe')][_0xd0eb05['uniqueid']][_0x2535('0x34')]=_0xd0eb05[_0x2535('0x34')];this[_0x2535('0xe')][_0xd0eb05[_0x2535('0x24')]][_0x2535('0x2f')]=_0xd0eb05['filename'];this[_0x2535('0xe')][_0xd0eb05[_0x2535('0x24')]]['calleridnum']=_0xd0eb05[_0x2535('0x35')];if(this[_0x2535('0xe')][_0xd0eb05[_0x2535('0x24')]][_0x2535('0x32')]>0x0){tiff2pdf(_0xd0eb05[_0x2535('0x2f')],_0x2535('0x36'),function(_0x1c0afa){logger[_0x2535('0x27')](_0xd0eb05[_0x2535('0x24')],_0x2535('0x37'),_0x1c0afa[_0x2535('0x38')]);attachment[_0x2535('0x39')]({'name':path[_0x2535('0x3a')](_0xd0eb05['filename'])[_0x2535('0x26')],'basename':path[_0x2535('0x3a')](_0xd0eb05[_0x2535('0x2f')])[_0x2535('0x26')]+_0x2535('0x3b'),'type':_0x2535('0x3c')})[_0x2535('0x3d')](function(_0x15008c){faxAccount[_0x2535('0x3e')](_0xeba365,{'from':_0xd0eb05['calleridnum'],'mapKey':'fax','body':path[_0x2535('0x3a')](_0xd0eb05[_0x2535('0x2f')])[_0x2535('0x26')]+_0x2535('0x3b'),'AttachmentId':_0x15008c['id']})['catch'](function(_0x4e7bb3){logger['error'](_0xd0eb05[_0x2535('0x24')],_0x2535('0x3f'),JSON[_0x2535('0x40')](_0x4e7bb3));});})[_0x2535('0x41')](function(_0x2b6418){logger[_0x2535('0x42')](_0xd0eb05[_0x2535('0x24')],_0x2535('0x43'),JSON[_0x2535('0x40')](_0x2b6418));});});}logger[_0x2535('0x27')](_0xd0eb05[_0x2535('0x24')],_0x2535('0x43'));}};Fax[_0x2535('0x1f')]['syncOriginateResponse']=function(_0x524cc7){if(this[_0x2535('0x29')][_0x524cc7[_0x2535('0x24')]]){_[_0x2535('0x44')](this[_0x2535('0xe')][_0x524cc7[_0x2535('0x24')]],_[_0x2535('0x45')](_0x524cc7,['response',_0x2535('0x46')]));}};function updateStatus(_0x48228e){if(_0x48228e[_0x2535('0x47')]){faxMessage[_0x2535('0x48')](_0x48228e[_0x2535('0x47')],{'failMessage':'SENDING\x20FAILED\x20'})[_0x2535('0x3d')](function(_0x5d62bc){ut['logger']('[Fax][Message][Error][Updated]',_0x5d62bc);})[_0x2535('0x41')](function(_0x1bfe87){ut[_0x2535('0x49')](_0x2535('0x4a'),_0x1bfe87);});}else{ut[_0x2535('0x49')](_0x2535('0x4b'));}}Fax[_0x2535('0x1f')]['updateStatus']=function(_0x59f909,_0x446716,_0x51dac1){var _0x36239d=this;var _0x32471e={};if(_0x446716){_0x32471e={'read':_0x446716};}else{_0x32471e={'failMessage':_0x51dac1};}if(this['actions'][_0x59f909][_0x2535('0x47')]){faxMessage[_0x2535('0x48')](this[_0x2535('0x29')][_0x59f909]['AttachmentId'],_0x32471e)[_0x2535('0x3d')](function(_0x591acc){ut[_0x2535('0x49')]('[Fax][Message][Updated]',_0x591acc);})[_0x2535('0x41')](function(_0x2d651f){ut[_0x2535('0x49')](_0x2535('0x4c'),_0x2d651f);})[_0x2535('0x4d')](function(){delete _0x36239d[_0x2535('0xe')][_0x59f909];delete _0x36239d['actions'][_0x59f909];});}else{ut[_0x2535('0x49')](_0x2535('0x4e'));delete this[_0x2535('0xe')][_0x59f909];delete this[_0x2535('0x29')][_0x59f909];}};Fax['prototype']['syncHangup']=function(_0xde66bf){var _0x14ba89=this;if(this[_0x2535('0xe')][_0xde66bf['uniqueid']]){if(this[_0x2535('0x29')][_0xde66bf[_0x2535('0x24')]]){this[_0x2535('0xe')][_0xde66bf[_0x2535('0x24')]]['id']=this[_0x2535('0x29')][_0xde66bf['uniqueid']]['id'];this['channels'][_0xde66bf[_0x2535('0x24')]][_0x2535('0x26')]=this['actions'][_0xde66bf['uniqueid']][_0x2535('0x26')];}logger['info'](_0xde66bf[_0x2535('0x24')],this[_0x2535('0x29')][_0xde66bf[_0x2535('0x24')]]?'\x20hangup\x20sendFile\x20':_0x2535('0x4f'),JSON[_0x2535('0x40')](this[_0x2535('0xe')][_0xde66bf[_0x2535('0x24')]]));if(this[_0x2535('0x29')][_0xde66bf['uniqueid']]){if(this[_0x2535('0xe')][_0xde66bf[_0x2535('0x24')]][_0x2535('0x50')]>0x0&&this['channels'][_0xde66bf[_0x2535('0x24')]][_0x2535('0x18')]===_0x2535('0x51')){this[_0x2535('0x52')](_0xde66bf[_0x2535('0x24')],!![]);}else{this[_0x2535('0x52')](_0xde66bf[_0x2535('0x24')],![],this[_0x2535('0xe')][_0xde66bf[_0x2535('0x24')]][_0x2535('0x53')]?'SENDING\x20FAILED:\x20'+this[_0x2535('0xe')][_0xde66bf[_0x2535('0x24')]][_0x2535('0x53')]:_0x2535('0x54'));}}}};Fax[_0x2535('0x1f')][_0x2535('0x55')]=function(_0x54bb16){var _0x54e964=this;return new BPromise(function(_0x18ec42,_0x5189d1){_0x54e964[_0x2535('0x56')](_0x54bb16,_0x54e964[_0x2535('0xf')][_0x54bb16[_0x2535('0x57')]])[_0x2535('0x3d')](function(_0x10ba85){ut[_0x2535('0x49')]('[Fax][execOriginate][success]',_0x10ba85);_0x18ec42(ut['message'](_0x2535('0x58')));})[_0x2535('0x41')](function(_0x25f734){ut[_0x2535('0x49')](_0x2535('0x59'),_0x25f734);updateStatus(_0x54bb16);_0x5189d1(ut[_0x2535('0x38')](_0x2535('0x5a')));});});};Fax[_0x2535('0x1f')][_0x2535('0x5b')]=function(_0x5d1ad3){var _0x127074=this;return new BPromise(function(_0x5e8f2f,_0x3cbc96){var _0x33de05=_0x127074['checkTrunk'](_0x127074['faxAccounts'][_0x5d1ad3[_0x2535('0x57')]][_0x2535('0x5c')],_0x5d1ad3);if(_0x33de05[_0x2535('0x5d')]){_0x5e8f2f(ut[_0x2535('0x38')](_0x2535('0x5e')));}else{_0x3cbc96(ut[_0x2535('0x38')](_0x33de05[_0x2535('0x38')]));}});};function originate(_0xd0578a,_0x2983e6,_0x86e9af){return new BPromise(function(_0x57ecc2,_0x9f8491){if(_0x86e9af){var _0x24bc21=util[_0x2535('0x5f')](_0x2535('0x60'),path['parse'](_0xd0578a['path'])[_0x2535('0x26')],_0xd0578a[_0x2535('0x4')]);gs[_0x2535('0x61')](_0x24bc21)[_0x2535('0x3d')](function(){_0xd0578a['path']=util['format'](_0x2535('0x62'),path[_0x2535('0x3a')](_0xd0578a['path'])[_0x2535('0x63')],path[_0x2535('0x3a')](_0xd0578a[_0x2535('0x4')])['name']);_0x2983e6[_0x2535('0x64')]=_0xd0578a[_0x2535('0x4')];ut[_0x2535('0x49')](_0x2535('0x65'),'');ami['Action'](ut[_0x2535('0x66')](_0xd0578a,_0x2983e6))['then'](function(_0x41041e){_0x57ecc2(_0x41041e);})[_0x2535('0x41')](function(_0x3344fc){_0x9f8491(_0x3344fc);});})[_0x2535('0x41')](function(_0x89191c){_0x9f8491(_0x89191c);});}else{ami['Action'](ut['variablesAction'](_0xd0578a,_0x2983e6))['then'](function(_0x4a9ad7){_0x57ecc2(_0x4a9ad7);})[_0x2535('0x41')](function(_0x172914){_0x9f8491(_0x172914);});}});}Fax[_0x2535('0x1f')]['execOriginate']=function(_0x452d23,_0x4eec8b){var _0x8f224=this;return new BPromise(function(_0x15746f,_0x196d3c){var _0x1d7b0a=uuid['v4']();var _0x1479c6=ut[_0x2535('0x67')](_0x452d23,_0x4eec8b);_0x8f224['actions'][_0x1d7b0a]=new Action(_0x1d7b0a,_0x4eec8b,_0x452d23);var _0x2534b2=_0x8f224['actions'][_0x1d7b0a];if(path[_0x2535('0x3a')](_0x452d23[_0x2535('0x4')])[_0x2535('0x68')]===_0x2535('0x3b')){originate(_0x2534b2,_0x1479c6,!![])[_0x2535('0x3d')](function(_0x451a18){_0x15746f(_0x451a18);})['catch'](function(_0x17112e){_0x196d3c(_0x17112e);});}else{originate(_0x2534b2,_0x1479c6,![])[_0x2535('0x3d')](function(_0x33412a){_0x15746f(_0x33412a);})[_0x2535('0x41')](function(_0x3b42e5){_0x196d3c(_0x3b42e5);});}});};Fax['prototype'][_0x2535('0x69')]=function(_0x2c89ac){return!_[_0x2535('0x6a')](_0x2c89ac)&&!_[_0x2535('0x6a')](_0x2c89ac[_0x2535('0x26')])&&!_[_0x2535('0x6a')](_0x2c89ac['active'])&&_0x2c89ac[_0x2535('0x6b')]&&!_['isNil'](this[_0x2535('0x10')][util['format']('SIP/%s',_0x2c89ac[_0x2535('0x26')])])&&!_['isNil'](this[_0x2535('0x10')][util['format']('SIP/%s',_0x2c89ac[_0x2535('0x26')])][_0x2535('0x2e')])&&(this[_0x2535('0x10')][util[_0x2535('0x5f')]('SIP/%s',_0x2c89ac[_0x2535('0x26')])][_0x2535('0x2e')]===_0x2535('0x6c')||this[_0x2535('0x10')][util[_0x2535('0x5f')](_0x2535('0x6d'),_0x2c89ac[_0x2535('0x26')])]['status']===_0x2535('0x6e'));};Fax[_0x2535('0x1f')][_0x2535('0x6f')]=function(_0x211b9a,_0x433d52){if(!this['isTrunkReachable'](_0x211b9a)){ut[_0x2535('0x49')](_0x2535('0x70'),_['isNil'](_0x211b9a)?'':_0x211b9a[_0x2535('0x26')]+'\x20Trunk\x20is\x20not\x20active');return{'isValid':![],'message':_0x2535('0x71')};}if(_[_0x2535('0x6a')](_0x433d52[_0x2535('0x72')])){ut['logger'](_0x2535('0x73'),JSON[_0x2535('0x40')](_0x433d52));return{'isValid':![],'message':_0x2535('0x74')};}if(_[_0x2535('0x6a')](_0x433d52[_0x2535('0x4')])){ut[_0x2535('0x49')]('[Fax][checkTrunk][Empty\x20path]',JSON[_0x2535('0x40')](_0x433d52));return{'isValid':![],'message':'Empty\x20path'};}return{'isValid':!![],'message':_0x2535('0x75')};};module[_0x2535('0x76')]=Fax;