9899eb3d91e6dc80952a45505711967c508ca22e
[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 _0x6625=['\x20notify\x20','stringify','\x20receiveFax\x20','response','reason','AttachmentId','update','logger','[Fax][Message][Error][Updated]','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','updateStatus','[Fax][Message][Updated]','[Fax][Message][NoUpdate1]','finally','\x20hangup\x20sendFile\x20','faxpages','faxstatusstring','SENDING\x20FAILED:\x20','send','execOriginate','FaxAccountId','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','execute','dir','XMF-PATH','Convert\x20pdf\x20to\x20tif','variablesAction','addVariables','ext','isTrunkReachable','isNil','active','SIP/%s','reachable','unmonitored','[Fax][checkTrunk][Trunk\x20not\x20working\x20correctly]','\x20Trunk\x20is\x20not\x20active','Trunk\x20not\x20working\x20correctly','[Fax][checkTrunk][Empty\x20Fax]','Is\x20Valid','exports','util','lodash','bluebird','moment','path','tiff2pdf','./util/gs','../../../config/logger','fax','../ami','./channel','../rpc/faxAccount','../rpc/faxMessage','faxAccounts','trunks','newchannel','syncNewchannel','bind','varset','syncVarSet','syncNewState','newexten','syncNewExten','faxstatus','receivefax','syncReceiveFax','hangup','syncHangup','originateresponse','syncOriginateResponse','prototype','context','from-voip-provider','hasOwnProperty','exten','localstationid','channels','uniqueid','channel','name','info','\x20syncNewchannel\x20','actions','variable','value','channelstate','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'];(function(_0x40ce01,_0x1e39c8){var _0xa0f16e=function(_0x2356ad){while(--_0x2356ad){_0x40ce01['push'](_0x40ce01['shift']());}};_0xa0f16e(++_0x1e39c8);}(_0x6625,0x11b));var _0x5662=function(_0x452f2e,_0x4b90aa){_0x452f2e=_0x452f2e-0x0;var _0xba0230=_0x6625[_0x452f2e];return _0xba0230;};'use strict';var util=require(_0x5662('0x0'));var _=require(_0x5662('0x1'));var BPromise=require(_0x5662('0x2'));var uuid=require('uuid');var moment=require(_0x5662('0x3'));var path=require(_0x5662('0x4'));var tiff2pdf=require(_0x5662('0x5'));var gs=require(_0x5662('0x6'));var logger=require(_0x5662('0x7'))(_0x5662('0x8'));var ami=require(_0x5662('0x9'));var rpcDb=require('./rpcDb');var Channel=require(_0x5662('0xa'));var ut=require('./util/util');var Action=require('./action');var faxAccount=require(_0x5662('0xb'));var attachment=require('../rpc/attachment');var faxMessage=require(_0x5662('0xc'));function Fax(_0x576290){this['channels']={};this[_0x5662('0xd')]=_0x576290['faxAccounts'];this[_0x5662('0xe')]=_0x576290[_0x5662('0xe')];this['actions']={};ami['on'](_0x5662('0xf'),this[_0x5662('0x10')][_0x5662('0x11')](this));ami['on'](_0x5662('0x12'),this[_0x5662('0x13')][_0x5662('0x11')](this));ami['on']('newstate',this[_0x5662('0x14')][_0x5662('0x11')](this));ami['on'](_0x5662('0x15'),this[_0x5662('0x16')][_0x5662('0x11')](this));ami['on'](_0x5662('0x17'),this['syncFaxStatus'][_0x5662('0x11')](this));ami['on'](_0x5662('0x18'),this[_0x5662('0x19')][_0x5662('0x11')](this));ami['on'](_0x5662('0x1a'),this[_0x5662('0x1b')][_0x5662('0x11')](this));ami['on'](_0x5662('0x1c'),this[_0x5662('0x1d')][_0x5662('0x11')](this));}Fax[_0x5662('0x1e')]['syncNewchannel']=function(_0x58c84d){if(_0x58c84d[_0x5662('0x1f')]===_0x5662('0x20')){for(var _0x47f3cd in this[_0x5662('0xd')]){if(this[_0x5662('0xd')][_0x5662('0x21')](_0x47f3cd)){if(_0x58c84d[_0x5662('0x22')]===this[_0x5662('0xd')][_0x47f3cd][_0x5662('0x23')]){this[_0x5662('0x24')][_0x58c84d[_0x5662('0x25')]]={};this['channels'][_0x58c84d['uniqueid']][_0x5662('0x26')]=_0x58c84d[_0x5662('0x26')];this[_0x5662('0x24')][_0x58c84d[_0x5662('0x25')]]['id']=this[_0x5662('0xd')][_0x47f3cd]['id'];this[_0x5662('0x24')][_0x58c84d['uniqueid']]['name']=this[_0x5662('0xd')][_0x47f3cd][_0x5662('0x27')];logger[_0x5662('0x28')](_0x58c84d[_0x5662('0x25')],_0x5662('0x29'));}}}}if(this[_0x5662('0x2a')][_0x58c84d[_0x5662('0x25')]]){this[_0x5662('0x24')][_0x58c84d[_0x5662('0x25')]]={};this[_0x5662('0x24')][_0x58c84d[_0x5662('0x25')]][_0x5662('0x26')]=_0x58c84d['channel'];logger[_0x5662('0x28')](_0x58c84d[_0x5662('0x25')],_0x5662('0x29'));}};Fax[_0x5662('0x1e')][_0x5662('0x13')]=function(_0x2c833b){if(this[_0x5662('0x24')][_0x2c833b[_0x5662('0x25')]]){for(var _0x529d19 in _0x2c833b[_0x5662('0x2b')]){if(_0x2c833b[_0x5662('0x2b')][_0x5662('0x21')](_0x529d19)){this[_0x5662('0x24')][_0x2c833b['uniqueid']][_0x529d19]=_0x2c833b[_0x5662('0x2c')];}}}};Fax[_0x5662('0x1e')]['syncNewState']=function(_0x5b3e1c){if(this[_0x5662('0x24')][_0x5b3e1c[_0x5662('0x25')]]){this['channels'][_0x5b3e1c[_0x5662('0x25')]][_0x5662('0x2d')]=_0x5b3e1c[_0x5662('0x2d')];this[_0x5662('0x24')][_0x5b3e1c[_0x5662('0x25')]][_0x5662('0x2e')]=_0x5b3e1c[_0x5662('0x2e')];}};Fax[_0x5662('0x1e')]['syncNewExten']=function(_0x441846){if(this[_0x5662('0x24')][_0x441846[_0x5662('0x25')]]){for(var _0x5e39b0 in _0x441846[_0x5662('0x2b')]){if(_0x441846[_0x5662('0x2b')][_0x5662('0x21')](_0x5e39b0)){this[_0x5662('0x24')][_0x441846[_0x5662('0x25')]][_0x5e39b0]=_0x441846[_0x5662('0x2c')];}}}};Fax['prototype']['syncFaxStatus']=function(_0x1b6d14){if(this[_0x5662('0x24')][_0x1b6d14[_0x5662('0x25')]]){this[_0x5662('0x24')][_0x1b6d14['uniqueid']][_0x5662('0x2f')]=_0x1b6d14[_0x5662('0x2f')];this[_0x5662('0x24')][_0x1b6d14['uniqueid']][_0x5662('0x30')]=_0x1b6d14[_0x5662('0x30')];this[_0x5662('0x24')][_0x1b6d14[_0x5662('0x25')]]['localstationid']=_0x1b6d14[_0x5662('0x23')];this[_0x5662('0x24')][_0x1b6d14[_0x5662('0x25')]]['filename']=_0x1b6d14[_0x5662('0x31')];logger['info'](_0x1b6d14['uniqueid'],_0x5662('0x32'));}};Fax['prototype'][_0x5662('0x19')]=function(_0x329941){var _0x1714aa=this;if(this[_0x5662('0x24')][_0x329941[_0x5662('0x25')]]){var _0x2388f0=this[_0x5662('0x24')][_0x329941[_0x5662('0x25')]]['id'];this[_0x5662('0x24')][_0x329941[_0x5662('0x25')]]['remotestationid']=_0x329941[_0x5662('0x33')];this[_0x5662('0x24')][_0x329941[_0x5662('0x25')]][_0x5662('0x34')]=_0x329941[_0x5662('0x34')];this['channels'][_0x329941[_0x5662('0x25')]][_0x5662('0x23')]=_0x329941[_0x5662('0x23')];this[_0x5662('0x24')][_0x329941['uniqueid']][_0x5662('0x35')]=_0x329941[_0x5662('0x35')];this[_0x5662('0x24')][_0x329941['uniqueid']][_0x5662('0x36')]=_0x329941[_0x5662('0x36')];this[_0x5662('0x24')][_0x329941[_0x5662('0x25')]]['filename']=_0x329941[_0x5662('0x31')];this[_0x5662('0x24')][_0x329941['uniqueid']]['calleridnum']=_0x329941[_0x5662('0x37')];if(this[_0x5662('0x24')][_0x329941['uniqueid']][_0x5662('0x34')]>0x0){tiff2pdf(_0x329941['filename'],_0x5662('0x38'),function(_0x5856d0){logger[_0x5662('0x28')](_0x329941['uniqueid'],'\x20result\x20conversion\x20file\x20',_0x5856d0['message']);attachment[_0x5662('0x39')]({'name':path[_0x5662('0x3a')](_0x329941[_0x5662('0x31')])['name'],'basename':path['parse'](_0x329941['filename'])['name']+_0x5662('0x3b'),'type':_0x5662('0x3c')})[_0x5662('0x3d')](function(_0x5a2eaf){faxAccount[_0x5662('0x3e')](_0x2388f0,{'from':_0x329941[_0x5662('0x37')],'mapKey':_0x5662('0x8'),'body':path[_0x5662('0x3a')](_0x329941[_0x5662('0x31')])[_0x5662('0x27')]+_0x5662('0x3b'),'AttachmentId':_0x5a2eaf['id']})[_0x5662('0x3f')](function(_0x4410a0){logger[_0x5662('0x40')](_0x329941[_0x5662('0x25')],_0x5662('0x41'),JSON[_0x5662('0x42')](_0x4410a0));});})[_0x5662('0x3f')](function(_0x2f8e3e){logger[_0x5662('0x40')](_0x329941[_0x5662('0x25')],_0x5662('0x43'),JSON[_0x5662('0x42')](_0x2f8e3e));});});}logger['info'](_0x329941[_0x5662('0x25')],_0x5662('0x43'));}};Fax[_0x5662('0x1e')][_0x5662('0x1d')]=function(_0x53ac1a){if(this[_0x5662('0x2a')][_0x53ac1a['uniqueid']]){_['merge'](this['channels'][_0x53ac1a[_0x5662('0x25')]],_['pick'](_0x53ac1a,[_0x5662('0x44'),_0x5662('0x45')]));}};function updateStatus(_0xfa130f){if(_0xfa130f[_0x5662('0x46')]){faxMessage[_0x5662('0x47')](_0xfa130f['AttachmentId'],{'failMessage':'SENDING\x20FAILED\x20'})['then'](function(_0x33d8fb){ut[_0x5662('0x48')](_0x5662('0x49'),_0x33d8fb);})['catch'](function(_0x3933d9){ut[_0x5662('0x48')](_0x5662('0x4a'),_0x3933d9);});}else{ut['logger'](_0x5662('0x4b'));}}Fax[_0x5662('0x1e')][_0x5662('0x4c')]=function(_0x197a47,_0x21d71e,_0x15d53b){var _0x253755=this;var _0x1c3f63={};if(_0x21d71e){_0x1c3f63={'read':_0x21d71e};}else{_0x1c3f63={'failMessage':_0x15d53b};}if(this['actions'][_0x197a47][_0x5662('0x46')]){faxMessage[_0x5662('0x47')](this[_0x5662('0x2a')][_0x197a47][_0x5662('0x46')],_0x1c3f63)[_0x5662('0x3d')](function(_0x225862){ut[_0x5662('0x48')](_0x5662('0x4d'),_0x225862);})[_0x5662('0x3f')](function(_0x88d5eb){ut['logger'](_0x5662('0x4e'),_0x88d5eb);})[_0x5662('0x4f')](function(){delete _0x253755[_0x5662('0x24')][_0x197a47];delete _0x253755[_0x5662('0x2a')][_0x197a47];});}else{ut[_0x5662('0x48')]('[Fax][Message][NoUpdate2]');delete this[_0x5662('0x24')][_0x197a47];delete this['actions'][_0x197a47];}};Fax[_0x5662('0x1e')][_0x5662('0x1b')]=function(_0x38d76e){var _0x2c67b5=this;if(this['channels'][_0x38d76e[_0x5662('0x25')]]){if(this[_0x5662('0x2a')][_0x38d76e[_0x5662('0x25')]]){this[_0x5662('0x24')][_0x38d76e[_0x5662('0x25')]]['id']=this[_0x5662('0x2a')][_0x38d76e['uniqueid']]['id'];this[_0x5662('0x24')][_0x38d76e[_0x5662('0x25')]][_0x5662('0x27')]=this[_0x5662('0x2a')][_0x38d76e['uniqueid']]['name'];}logger['info'](_0x38d76e[_0x5662('0x25')],this[_0x5662('0x2a')][_0x38d76e[_0x5662('0x25')]]?_0x5662('0x50'):'\x20hangup\x20faxRiceive\x20',JSON[_0x5662('0x42')](this[_0x5662('0x24')][_0x38d76e[_0x5662('0x25')]]));if(this['actions'][_0x38d76e['uniqueid']]){if(this['channels'][_0x38d76e[_0x5662('0x25')]][_0x5662('0x51')]>0x0&&this[_0x5662('0x24')][_0x38d76e[_0x5662('0x25')]][_0x5662('0x17')]==='SUCCESS'){this['updateStatus'](_0x38d76e[_0x5662('0x25')],!![]);}else{this[_0x5662('0x4c')](_0x38d76e[_0x5662('0x25')],![],this[_0x5662('0x24')][_0x38d76e[_0x5662('0x25')]][_0x5662('0x52')]?_0x5662('0x53')+this[_0x5662('0x24')][_0x38d76e[_0x5662('0x25')]][_0x5662('0x52')]:'SENDING\x20FAILED');}}}};Fax[_0x5662('0x1e')][_0x5662('0x54')]=function(_0x156b7b){var _0x129a3a=this;return new BPromise(function(_0x2327dd,_0x4a17cd){_0x129a3a[_0x5662('0x55')](_0x156b7b,_0x129a3a['faxAccounts'][_0x156b7b[_0x5662('0x56')]])['then'](function(_0x3adbe1){ut[_0x5662('0x48')]('[Fax][execOriginate][success]',_0x3adbe1);_0x2327dd(ut[_0x5662('0x57')](_0x5662('0x58')));})[_0x5662('0x3f')](function(_0x121e9a){ut[_0x5662('0x48')]('[Fax][execOriginate][failure]',_0x121e9a);updateStatus(_0x156b7b);_0x4a17cd(ut[_0x5662('0x57')](_0x5662('0x59')));});});};Fax[_0x5662('0x1e')][_0x5662('0x5a')]=function(_0x2c983c){var _0x11c3e2=this;return new BPromise(function(_0x49408b,_0x81162b){var _0x241060=_0x11c3e2[_0x5662('0x5b')](_0x11c3e2[_0x5662('0xd')][_0x2c983c[_0x5662('0x56')]][_0x5662('0x5c')],_0x2c983c);if(_0x241060[_0x5662('0x5d')]){_0x49408b(ut[_0x5662('0x57')](_0x5662('0x5e')));}else{_0x81162b(ut['message'](_0x241060[_0x5662('0x57')]));}});};function originate(_0x12a6e8,_0x221fab,_0x46140f){return new BPromise(function(_0x5704d1,_0x4c9fc5){if(_0x46140f){var _0x704486=util[_0x5662('0x5f')](_0x5662('0x60'),path['parse'](_0x12a6e8[_0x5662('0x4')])[_0x5662('0x27')],_0x12a6e8[_0x5662('0x4')]);gs[_0x5662('0x61')](_0x704486)[_0x5662('0x3d')](function(){_0x12a6e8['path']=util[_0x5662('0x5f')]('%s/%s.tif',path[_0x5662('0x3a')](_0x12a6e8[_0x5662('0x4')])[_0x5662('0x62')],path['parse'](_0x12a6e8[_0x5662('0x4')])[_0x5662('0x27')]);_0x221fab[_0x5662('0x63')]=_0x12a6e8[_0x5662('0x4')];ut[_0x5662('0x48')](_0x5662('0x64'),'');ami['Action'](ut[_0x5662('0x65')](_0x12a6e8,_0x221fab))[_0x5662('0x3d')](function(_0x135074){_0x5704d1(_0x135074);})['catch'](function(_0x358ca6){_0x4c9fc5(_0x358ca6);});})[_0x5662('0x3f')](function(_0x172cc2){_0x4c9fc5(_0x172cc2);});}else{ami['Action'](ut[_0x5662('0x65')](_0x12a6e8,_0x221fab))[_0x5662('0x3d')](function(_0x53fb30){_0x5704d1(_0x53fb30);})[_0x5662('0x3f')](function(_0x574830){_0x4c9fc5(_0x574830);});}});}Fax['prototype'][_0x5662('0x55')]=function(_0x27ac7a,_0x4415f9){var _0x5e081=this;return new BPromise(function(_0x5ee830,_0x16624f){var _0x2175bb=uuid['v4']();var _0x12e521=ut[_0x5662('0x66')](_0x27ac7a,_0x4415f9);_0x5e081[_0x5662('0x2a')][_0x2175bb]=new Action(_0x2175bb,_0x4415f9,_0x27ac7a);var _0x593fce=_0x5e081[_0x5662('0x2a')][_0x2175bb];if(path['parse'](_0x27ac7a[_0x5662('0x4')])[_0x5662('0x67')]===_0x5662('0x3b')){originate(_0x593fce,_0x12e521,!![])[_0x5662('0x3d')](function(_0x557eae){_0x5ee830(_0x557eae);})[_0x5662('0x3f')](function(_0x1747c6){_0x16624f(_0x1747c6);});}else{originate(_0x593fce,_0x12e521,![])[_0x5662('0x3d')](function(_0x1ea0bd){_0x5ee830(_0x1ea0bd);})[_0x5662('0x3f')](function(_0x19d68f){_0x16624f(_0x19d68f);});}});};Fax['prototype'][_0x5662('0x68')]=function(_0x3cf651){return!_[_0x5662('0x69')](_0x3cf651)&&!_[_0x5662('0x69')](_0x3cf651['name'])&&!_[_0x5662('0x69')](_0x3cf651[_0x5662('0x6a')])&&_0x3cf651['active']&&!_[_0x5662('0x69')](this[_0x5662('0xe')][util[_0x5662('0x5f')](_0x5662('0x6b'),_0x3cf651[_0x5662('0x27')])])&&!_[_0x5662('0x69')](this[_0x5662('0xe')][util['format'](_0x5662('0x6b'),_0x3cf651[_0x5662('0x27')])][_0x5662('0x30')])&&(this[_0x5662('0xe')][util['format'](_0x5662('0x6b'),_0x3cf651[_0x5662('0x27')])][_0x5662('0x30')]===_0x5662('0x6c')||this['trunks'][util['format']('SIP/%s',_0x3cf651[_0x5662('0x27')])]['status']===_0x5662('0x6d'));};Fax[_0x5662('0x1e')][_0x5662('0x5b')]=function(_0x126c07,_0x2d85cf){if(!this[_0x5662('0x68')](_0x126c07)){ut['logger'](_0x5662('0x6e'),_[_0x5662('0x69')](_0x126c07)?'':_0x126c07[_0x5662('0x27')]+_0x5662('0x6f'));return{'isValid':![],'message':_0x5662('0x70')};}if(_[_0x5662('0x69')](_0x2d85cf[_0x5662('0x8')])){ut['logger'](_0x5662('0x71'),JSON[_0x5662('0x42')](_0x2d85cf));return{'isValid':![],'message':'Empty\x20Fax'};}if(_[_0x5662('0x69')](_0x2d85cf[_0x5662('0x4')])){ut[_0x5662('0x48')]('[Fax][checkTrunk][Empty\x20path]',JSON[_0x5662('0x42')](_0x2d85cf));return{'isValid':![],'message':'Empty\x20path'};}return{'isValid':!![],'message':_0x5662('0x72')};};module[_0x5662('0x73')]=Fax;