Built motion from commit 3d6333b4.|2.6.31
[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 _0x9baf=['syncNewExten','syncFaxStatus','receivefax','syncReceiveFax','originateresponse','syncOriginateResponse','prototype','context','from-voip-provider','hasOwnProperty','exten','uniqueid','channels','channel','name','info','\x20syncNewchannel\x20','variable','value','channelstate','channelstatedesc','operation','status','localstationid','filename','\x20syncFaxStatus\x20','remotestationid','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','\x20result\x20conversion\x20file\x20','message','create','parse','.pdf','application/pdf','then','notify','fax','catch','error','\x20notify\x20','stringify','\x20receiveFax\x20','merge','pick','response','reason','AttachmentId','update','SENDING\x20FAILED\x20','logger','updateStatus','[Fax][Message][Updated]','[Fax][Message][NoUpdate1]','finally','[Fax][Message][NoUpdate2]','deleteChannelAfterTimeout','syncHangup','\x20hangup\x20sendFile\x20','\x20hangup\x20faxRiceive\x20','faxpages','faxstatus','SUCCESS','faxstatusstring','SENDING\x20FAILED:\x20','SENDING\x20FAILED','execOriginate','FaxAccountId','[Fax][execOriginate][success]','Fax\x20received','[Fax][execOriginate][failure]','Fax\x20failed','checkAfterSend','checkTrunk','Trunk','isValid','Trunk\x20Reachable','execute','format','%s/%s.tif','Action','variablesAction','addVariables','active','SIP/%s','isNil','reachable','[Fax][checkTrunk][Trunk\x20not\x20working\x20correctly]','Trunk\x20not\x20working\x20correctly','[Fax][checkTrunk][Empty\x20Fax]','Empty\x20Fax','Empty\x20path','Is\x20Valid','exports','util','bluebird','uuid','moment','path','tiff2pdf','./util/gs','../../../config/logger','../ami','./rpcDb','./channel','./util/util','./action','../rpc/faxAccount','faxAccounts','trunks','actions','syncNewchannel','bind','varset','syncVarSet','newstate','syncNewState','newexten'];(function(_0x25662e,_0x4cec68){var _0x59b7bc=function(_0xafb159){while(--_0xafb159){_0x25662e['push'](_0x25662e['shift']());}};_0x59b7bc(++_0x4cec68);}(_0x9baf,0x1cc));var _0xf9ba=function(_0x452618,_0x391bfb){_0x452618=_0x452618-0x0;var _0x3a4ae7=_0x9baf[_0x452618];return _0x3a4ae7;};'use strict';var util=require(_0xf9ba('0x0'));var _=require('lodash');var BPromise=require(_0xf9ba('0x1'));var uuid=require(_0xf9ba('0x2'));var moment=require(_0xf9ba('0x3'));var path=require(_0xf9ba('0x4'));var tiff2pdf=require(_0xf9ba('0x5'));var gs=require(_0xf9ba('0x6'));var logger=require(_0xf9ba('0x7'))('fax');var ami=require(_0xf9ba('0x8'));var rpcDb=require(_0xf9ba('0x9'));var Channel=require(_0xf9ba('0xa'));var ut=require(_0xf9ba('0xb'));var Action=require(_0xf9ba('0xc'));var faxAccount=require(_0xf9ba('0xd'));var attachment=require('../rpc/attachment');var faxMessage=require('../rpc/faxMessage');function Fax(_0x20a1a0){this['channels']={};this[_0xf9ba('0xe')]=_0x20a1a0[_0xf9ba('0xe')];this[_0xf9ba('0xf')]=_0x20a1a0[_0xf9ba('0xf')];this[_0xf9ba('0x10')]={};ami['on']('newchannel',this[_0xf9ba('0x11')][_0xf9ba('0x12')](this));ami['on'](_0xf9ba('0x13'),this[_0xf9ba('0x14')][_0xf9ba('0x12')](this));ami['on'](_0xf9ba('0x15'),this[_0xf9ba('0x16')][_0xf9ba('0x12')](this));ami['on'](_0xf9ba('0x17'),this[_0xf9ba('0x18')][_0xf9ba('0x12')](this));ami['on']('faxstatus',this[_0xf9ba('0x19')][_0xf9ba('0x12')](this));ami['on'](_0xf9ba('0x1a'),this[_0xf9ba('0x1b')]['bind'](this));ami['on']('hangup',this['syncHangup']['bind'](this));ami['on'](_0xf9ba('0x1c'),this[_0xf9ba('0x1d')]['bind'](this));}Fax[_0xf9ba('0x1e')][_0xf9ba('0x11')]=function(_0x4274e7){if(_0x4274e7[_0xf9ba('0x1f')]===_0xf9ba('0x20')){for(var _0x2cac08 in this[_0xf9ba('0xe')]){if(this['faxAccounts'][_0xf9ba('0x21')](_0x2cac08)){if(_0x4274e7[_0xf9ba('0x22')]===this[_0xf9ba('0xe')][_0x2cac08]['localstationid']){this['channels'][_0x4274e7[_0xf9ba('0x23')]]={};this[_0xf9ba('0x24')][_0x4274e7['uniqueid']][_0xf9ba('0x25')]=_0x4274e7['channel'];this[_0xf9ba('0x24')][_0x4274e7[_0xf9ba('0x23')]]['id']=this[_0xf9ba('0xe')][_0x2cac08]['id'];this['channels'][_0x4274e7[_0xf9ba('0x23')]][_0xf9ba('0x26')]=this[_0xf9ba('0xe')][_0x2cac08][_0xf9ba('0x26')];logger[_0xf9ba('0x27')](_0x4274e7[_0xf9ba('0x23')],'\x20syncNewchannel\x20');}}}}if(this['actions'][_0x4274e7[_0xf9ba('0x23')]]){this[_0xf9ba('0x24')][_0x4274e7[_0xf9ba('0x23')]]={};this[_0xf9ba('0x24')][_0x4274e7[_0xf9ba('0x23')]][_0xf9ba('0x25')]=_0x4274e7['channel'];logger[_0xf9ba('0x27')](_0x4274e7[_0xf9ba('0x23')],_0xf9ba('0x28'));}};Fax[_0xf9ba('0x1e')][_0xf9ba('0x14')]=function(_0x213074){if(this[_0xf9ba('0x24')][_0x213074[_0xf9ba('0x23')]]){for(var _0x23e37b in _0x213074[_0xf9ba('0x29')]){if(_0x213074[_0xf9ba('0x29')][_0xf9ba('0x21')](_0x23e37b)){this[_0xf9ba('0x24')][_0x213074[_0xf9ba('0x23')]][_0x23e37b]=_0x213074[_0xf9ba('0x2a')];}}}};Fax[_0xf9ba('0x1e')][_0xf9ba('0x16')]=function(_0x34da72){if(this[_0xf9ba('0x24')][_0x34da72['uniqueid']]){this['channels'][_0x34da72[_0xf9ba('0x23')]][_0xf9ba('0x2b')]=_0x34da72[_0xf9ba('0x2b')];this['channels'][_0x34da72[_0xf9ba('0x23')]][_0xf9ba('0x2c')]=_0x34da72[_0xf9ba('0x2c')];}};Fax[_0xf9ba('0x1e')][_0xf9ba('0x18')]=function(_0x5e523a){if(this[_0xf9ba('0x24')][_0x5e523a[_0xf9ba('0x23')]]){for(var _0x243632 in _0x5e523a[_0xf9ba('0x29')]){if(_0x5e523a['variable'][_0xf9ba('0x21')](_0x243632)){this[_0xf9ba('0x24')][_0x5e523a[_0xf9ba('0x23')]][_0x243632]=_0x5e523a[_0xf9ba('0x2a')];}}}};Fax[_0xf9ba('0x1e')][_0xf9ba('0x19')]=function(_0x4de029){if(this[_0xf9ba('0x24')][_0x4de029[_0xf9ba('0x23')]]){this[_0xf9ba('0x24')][_0x4de029[_0xf9ba('0x23')]][_0xf9ba('0x2d')]=_0x4de029[_0xf9ba('0x2d')];this[_0xf9ba('0x24')][_0x4de029[_0xf9ba('0x23')]][_0xf9ba('0x2e')]=_0x4de029[_0xf9ba('0x2e')];this[_0xf9ba('0x24')][_0x4de029[_0xf9ba('0x23')]][_0xf9ba('0x2f')]=_0x4de029[_0xf9ba('0x2f')];this[_0xf9ba('0x24')][_0x4de029[_0xf9ba('0x23')]][_0xf9ba('0x30')]=_0x4de029[_0xf9ba('0x30')];logger[_0xf9ba('0x27')](_0x4de029[_0xf9ba('0x23')],_0xf9ba('0x31'));}};Fax[_0xf9ba('0x1e')][_0xf9ba('0x1b')]=function(_0x10524f){var _0x10c5d9=this;if(this[_0xf9ba('0x24')][_0x10524f[_0xf9ba('0x23')]]){var _0x1768c8=this[_0xf9ba('0x24')][_0x10524f[_0xf9ba('0x23')]]['id'];this[_0xf9ba('0x24')][_0x10524f[_0xf9ba('0x23')]]['remotestationid']=_0x10524f[_0xf9ba('0x32')];this[_0xf9ba('0x24')][_0x10524f[_0xf9ba('0x23')]][_0xf9ba('0x33')]=_0x10524f[_0xf9ba('0x33')];this[_0xf9ba('0x24')][_0x10524f[_0xf9ba('0x23')]]['localstationid']=_0x10524f[_0xf9ba('0x2f')];this[_0xf9ba('0x24')][_0x10524f[_0xf9ba('0x23')]][_0xf9ba('0x34')]=_0x10524f[_0xf9ba('0x34')];this[_0xf9ba('0x24')][_0x10524f[_0xf9ba('0x23')]][_0xf9ba('0x35')]=_0x10524f[_0xf9ba('0x35')];this[_0xf9ba('0x24')][_0x10524f[_0xf9ba('0x23')]][_0xf9ba('0x30')]=_0x10524f[_0xf9ba('0x30')];this[_0xf9ba('0x24')][_0x10524f[_0xf9ba('0x23')]][_0xf9ba('0x36')]=_0x10524f['calleridnum'];if(this[_0xf9ba('0x24')][_0x10524f[_0xf9ba('0x23')]][_0xf9ba('0x33')]>0x0){tiff2pdf(_0x10524f['filename'],_0xf9ba('0x37'),function(_0x30659b){logger[_0xf9ba('0x27')](_0x10524f[_0xf9ba('0x23')],_0xf9ba('0x38'),_0x30659b[_0xf9ba('0x39')]);attachment[_0xf9ba('0x3a')]({'name':path[_0xf9ba('0x3b')](_0x10524f['filename'])[_0xf9ba('0x26')],'basename':path[_0xf9ba('0x3b')](_0x10524f[_0xf9ba('0x30')])[_0xf9ba('0x26')]+_0xf9ba('0x3c'),'type':_0xf9ba('0x3d')})[_0xf9ba('0x3e')](function(_0x3c0e3a){faxAccount[_0xf9ba('0x3f')](_0x1768c8,{'from':_0x10524f[_0xf9ba('0x36')],'mapKey':_0xf9ba('0x40'),'body':path[_0xf9ba('0x3b')](_0x10524f[_0xf9ba('0x30')])['name']+'.pdf','AttachmentId':_0x3c0e3a['id']})[_0xf9ba('0x41')](function(_0x483e77){logger[_0xf9ba('0x42')](_0x10524f[_0xf9ba('0x23')],_0xf9ba('0x43'),JSON[_0xf9ba('0x44')](_0x483e77));});})[_0xf9ba('0x41')](function(_0x37cd31){logger[_0xf9ba('0x42')](_0x10524f[_0xf9ba('0x23')],_0xf9ba('0x45'),JSON[_0xf9ba('0x44')](_0x37cd31));});});}logger[_0xf9ba('0x27')](_0x10524f['uniqueid'],'\x20receiveFax\x20');}};Fax[_0xf9ba('0x1e')][_0xf9ba('0x1d')]=function(_0x31e0d6){if(this['actions'][_0x31e0d6['uniqueid']]){_[_0xf9ba('0x46')](this['channels'][_0x31e0d6[_0xf9ba('0x23')]],_[_0xf9ba('0x47')](_0x31e0d6,[_0xf9ba('0x48'),_0xf9ba('0x49')]));}};function updateStatus(_0x4914f8){if(_0x4914f8[_0xf9ba('0x4a')]){faxMessage[_0xf9ba('0x4b')](_0x4914f8['AttachmentId'],{'failMessage':_0xf9ba('0x4c')})['then'](function(_0x1a5f85){ut['logger']('[Fax][Message][Error][Updated]',_0x1a5f85);})['catch'](function(_0x45c7d3){ut[_0xf9ba('0x4d')]('[Fax][Message][Error][NoUpdate1]',_0x45c7d3);});}else{ut[_0xf9ba('0x4d')]('[Fax][Message][Error][NoUpdate2]');}}Fax[_0xf9ba('0x1e')][_0xf9ba('0x4e')]=function(_0xaaa559,_0x275271,_0x246c30){var _0x3e4440=this;var _0x4da646={};if(_0x275271){_0x4da646={'read':_0x275271};}else{_0x4da646={'failMessage':_0x246c30};}if(this[_0xf9ba('0x10')][_0xaaa559][_0xf9ba('0x4a')]){faxMessage[_0xf9ba('0x4b')](this[_0xf9ba('0x10')][_0xaaa559][_0xf9ba('0x4a')],_0x4da646)[_0xf9ba('0x3e')](function(_0x50f83e){ut['logger'](_0xf9ba('0x4f'),_0x50f83e);})[_0xf9ba('0x41')](function(_0x36bf71){ut['logger'](_0xf9ba('0x50'),_0x36bf71);})[_0xf9ba('0x51')](function(){delete _0x3e4440[_0xf9ba('0x24')][_0xaaa559];delete _0x3e4440[_0xf9ba('0x10')][_0xaaa559];});}else{ut[_0xf9ba('0x4d')](_0xf9ba('0x52'));this[_0xf9ba('0x53')](_0xaaa559);delete this[_0xf9ba('0x10')][_0xaaa559];}};Fax[_0xf9ba('0x1e')][_0xf9ba('0x54')]=function(_0x55f123){var _0x3e5caa=this;if(this[_0xf9ba('0x24')][_0x55f123[_0xf9ba('0x23')]]){if(this[_0xf9ba('0x10')][_0x55f123['uniqueid']]){this[_0xf9ba('0x24')][_0x55f123['uniqueid']]['id']=this[_0xf9ba('0x10')][_0x55f123[_0xf9ba('0x23')]]['id'];this[_0xf9ba('0x24')][_0x55f123['uniqueid']][_0xf9ba('0x26')]=this[_0xf9ba('0x10')][_0x55f123[_0xf9ba('0x23')]]['name'];}logger[_0xf9ba('0x27')](_0x55f123[_0xf9ba('0x23')],this[_0xf9ba('0x10')][_0x55f123[_0xf9ba('0x23')]]?_0xf9ba('0x55'):_0xf9ba('0x56'),JSON[_0xf9ba('0x44')](this[_0xf9ba('0x24')][_0x55f123[_0xf9ba('0x23')]]));if(this[_0xf9ba('0x10')][_0x55f123[_0xf9ba('0x23')]]){if(this[_0xf9ba('0x24')][_0x55f123[_0xf9ba('0x23')]][_0xf9ba('0x57')]>0x0&&this[_0xf9ba('0x24')][_0x55f123[_0xf9ba('0x23')]][_0xf9ba('0x58')]===_0xf9ba('0x59')){this[_0xf9ba('0x4e')](_0x55f123[_0xf9ba('0x23')],!![]);}else{this[_0xf9ba('0x4e')](_0x55f123[_0xf9ba('0x23')],![],this['channels'][_0x55f123[_0xf9ba('0x23')]][_0xf9ba('0x5a')]?_0xf9ba('0x5b')+this['channels'][_0x55f123[_0xf9ba('0x23')]][_0xf9ba('0x5a')]:_0xf9ba('0x5c'));}}}};Fax[_0xf9ba('0x1e')]['send']=function(_0x295044){var _0x306b84=this;return new BPromise(function(_0x4ecec9,_0x51cdd0){_0x306b84[_0xf9ba('0x5d')](_0x295044,_0x306b84[_0xf9ba('0xe')][_0x295044[_0xf9ba('0x5e')]])[_0xf9ba('0x3e')](function(_0x36ce37){ut[_0xf9ba('0x4d')](_0xf9ba('0x5f'),_0x36ce37);_0x4ecec9(ut[_0xf9ba('0x39')](_0xf9ba('0x60')));})['catch'](function(_0x5d77a3){ut[_0xf9ba('0x4d')](_0xf9ba('0x61'),_0x5d77a3);updateStatus(_0x295044);_0x51cdd0(ut['message'](_0xf9ba('0x62')));});});};Fax['prototype'][_0xf9ba('0x63')]=function(_0x490a22){var _0x45742a=this;return new BPromise(function(_0x328b82,_0x426443){var _0x35550f=_0x45742a[_0xf9ba('0x64')](_0x45742a[_0xf9ba('0xe')][_0x490a22['FaxAccountId']][_0xf9ba('0x65')],_0x490a22);if(_0x35550f[_0xf9ba('0x66')]){_0x328b82(ut[_0xf9ba('0x39')](_0xf9ba('0x67')));}else{_0x426443(ut[_0xf9ba('0x39')](_0x35550f[_0xf9ba('0x39')]));}});};function originate(_0x1f357b,_0x5cf45c,_0x3d3a1b){return new BPromise(function(_0x3bce6c,_0x54f06c){if(_0x3d3a1b){var _0x1c4d6b=util['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[_0xf9ba('0x3b')](_0x1f357b[_0xf9ba('0x4')])['name'],_0x1f357b['path']);gs[_0xf9ba('0x68')](_0x1c4d6b)['then'](function(){_0x1f357b[_0xf9ba('0x4')]=util[_0xf9ba('0x69')](_0xf9ba('0x6a'),path[_0xf9ba('0x3b')](_0x1f357b[_0xf9ba('0x4')])['dir'],path['parse'](_0x1f357b[_0xf9ba('0x4')])[_0xf9ba('0x26')]);_0x5cf45c['XMF-PATH']=_0x1f357b[_0xf9ba('0x4')];ut[_0xf9ba('0x4d')]('Convert\x20pdf\x20to\x20tif','');ami[_0xf9ba('0x6b')](ut[_0xf9ba('0x6c')](_0x1f357b,_0x5cf45c))[_0xf9ba('0x3e')](function(_0x3c0f89){_0x3bce6c(_0x3c0f89);})[_0xf9ba('0x41')](function(_0x4e1ae6){_0x54f06c(_0x4e1ae6);});})['catch'](function(_0x8eb7ba){_0x54f06c(_0x8eb7ba);});}else{ami[_0xf9ba('0x6b')](ut['variablesAction'](_0x1f357b,_0x5cf45c))[_0xf9ba('0x3e')](function(_0x5ca2e8){_0x3bce6c(_0x5ca2e8);})[_0xf9ba('0x41')](function(_0x48a191){_0x54f06c(_0x48a191);});}});}Fax['prototype'][_0xf9ba('0x5d')]=function(_0x3651e7,_0x1ca2c9){var _0x857df=this;return new BPromise(function(_0x54b3b8,_0x11f173){var _0x478651=uuid['v4']();var _0x2824c3=ut[_0xf9ba('0x6d')](_0x3651e7,_0x1ca2c9);_0x857df[_0xf9ba('0x10')][_0x478651]=new Action(_0x478651,_0x1ca2c9,_0x3651e7);var _0x50e818=_0x857df['actions'][_0x478651];if(path[_0xf9ba('0x3b')](_0x3651e7[_0xf9ba('0x4')])['ext']===_0xf9ba('0x3c')){originate(_0x50e818,_0x2824c3,!![])[_0xf9ba('0x3e')](function(_0x99e321){_0x54b3b8(_0x99e321);})['catch'](function(_0x5e41f8){_0x11f173(_0x5e41f8);});}else{originate(_0x50e818,_0x2824c3,![])['then'](function(_0x1501e2){_0x54b3b8(_0x1501e2);})[_0xf9ba('0x41')](function(_0xef2356){_0x11f173(_0xef2356);});}});};Fax['prototype']['isTrunkReachable']=function(_0xf6d410){return!_['isNil'](_0xf6d410)&&!_['isNil'](_0xf6d410[_0xf9ba('0x26')])&&!_['isNil'](_0xf6d410[_0xf9ba('0x6e')])&&_0xf6d410[_0xf9ba('0x6e')]&&!_['isNil'](this[_0xf9ba('0xf')][util[_0xf9ba('0x69')](_0xf9ba('0x6f'),_0xf6d410[_0xf9ba('0x26')])])&&!_[_0xf9ba('0x70')](this[_0xf9ba('0xf')][util[_0xf9ba('0x69')](_0xf9ba('0x6f'),_0xf6d410[_0xf9ba('0x26')])]['status'])&&(this[_0xf9ba('0xf')][util[_0xf9ba('0x69')]('SIP/%s',_0xf6d410[_0xf9ba('0x26')])][_0xf9ba('0x2e')]===_0xf9ba('0x71')||this[_0xf9ba('0xf')][util[_0xf9ba('0x69')](_0xf9ba('0x6f'),_0xf6d410['name'])][_0xf9ba('0x2e')]==='unmonitored');};Fax['prototype']['checkTrunk']=function(_0x4fccdd,_0x334bcb){if(!this['isTrunkReachable'](_0x4fccdd)){ut[_0xf9ba('0x4d')](_0xf9ba('0x72'),_[_0xf9ba('0x70')](_0x4fccdd)?'':_0x4fccdd[_0xf9ba('0x26')]+'\x20Trunk\x20is\x20not\x20active');return{'isValid':![],'message':_0xf9ba('0x73')};}if(_['isNil'](_0x334bcb[_0xf9ba('0x40')])){ut[_0xf9ba('0x4d')](_0xf9ba('0x74'),JSON[_0xf9ba('0x44')](_0x334bcb));return{'isValid':![],'message':_0xf9ba('0x75')};}if(_[_0xf9ba('0x70')](_0x334bcb[_0xf9ba('0x4')])){ut[_0xf9ba('0x4d')]('[Fax][checkTrunk][Empty\x20path]',JSON[_0xf9ba('0x44')](_0x334bcb));return{'isValid':![],'message':_0xf9ba('0x76')};}return{'isValid':!![],'message':_0xf9ba('0x77')};};Fax[_0xf9ba('0x1e')][_0xf9ba('0x53')]=function(_0x59a9f5,_0x2de24a=0x2710){if(this[_0xf9ba('0x24')][_0x59a9f5]){setTimeout(()=>{delete this['channels'][_0x59a9f5];},_0x2de24a);}};module[_0xf9ba('0x78')]=Fax;