4fe62878390163c2af4ac8e56ef68da7d6b202c9
[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 _0x5aaa=['unmonitored','Trunk\x20not\x20working','fax','[Fax][checkTrunk][Empty\x20Fax]','Empty\x20Fax','Empty\x20path','exports','util','lodash','moment','path','./util/gs','../../../config/logger','../ami','./channel','./util/util','../rpc/faxAccount','../rpc/attachment','../rpc/faxMessage','faxAccounts','trunks','actions','newchannel','syncNewchannel','bind','varset','syncVarSet','newstate','syncNewState','newexten','syncNewExten','faxstatus','receivefax','originateresponse','syncOriginateResponse','prototype','context','from-voip-provider','hasOwnProperty','uniqueid','channels','channel','name','info','\x20syncNewchannel\x20','variable','value','channelstate','channelstatedesc','syncFaxStatus','operation','status','localstationid','filename','\x20syncFaxStatus\x20','syncReceiveFax','remotestationid','pagestransferred','resolution','transferrate','calleridnum','\x20result\x20conversion\x20file\x20','parse','.pdf','application/pdf','then','notify','catch','error','stringify','\x20receiveFax\x20','merge','response','reason','AttachmentId','update','SENDING\x20FAILED\x20','logger','[Fax][Message][Error][Updated]','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','updateStatus','[Fax][Message][Updated]','[Fax][Message][NoUpdate1]','finally','[Fax][Message][NoUpdate2]','\x20hangup\x20sendFile\x20','\x20hangup\x20faxRiceive\x20','faxpages','faxstatusstring','SENDING\x20FAILED:\x20','SENDING\x20FAILED','send','FaxAccountId','[Fax][execOriginate][success]','message','Fax\x20received','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','Action','execOriginate','addVariables','isTrunkReachable','isNil','active','SIP/%s'];(function(_0x30ca74,_0x32563){var _0x72aeb4=function(_0x22b859){while(--_0x22b859){_0x30ca74['push'](_0x30ca74['shift']());}};_0x72aeb4(++_0x32563);}(_0x5aaa,0xeb));var _0xa5aa=function(_0x5ddea3,_0x539a5a){_0x5ddea3=_0x5ddea3-0x0;var _0x206407=_0x5aaa[_0x5ddea3];return _0x206407;};'use strict';var util=require(_0xa5aa('0x0'));var _=require(_0xa5aa('0x1'));var BPromise=require('bluebird');var uuid=require('uuid');var moment=require(_0xa5aa('0x2'));var path=require(_0xa5aa('0x3'));var tiff2pdf=require('tiff2pdf');var gs=require(_0xa5aa('0x4'));var logger=require(_0xa5aa('0x5'))('fax');var ami=require(_0xa5aa('0x6'));var rpcDb=require('./rpcDb');var Channel=require(_0xa5aa('0x7'));var ut=require(_0xa5aa('0x8'));var Action=require('./action');var faxAccount=require(_0xa5aa('0x9'));var attachment=require(_0xa5aa('0xa'));var faxMessage=require(_0xa5aa('0xb'));function Fax(_0x16e8bf){this['channels']={};this['faxAccounts']=_0x16e8bf[_0xa5aa('0xc')];this['trunks']=_0x16e8bf[_0xa5aa('0xd')];this[_0xa5aa('0xe')]={};ami['on'](_0xa5aa('0xf'),this[_0xa5aa('0x10')][_0xa5aa('0x11')](this));ami['on'](_0xa5aa('0x12'),this[_0xa5aa('0x13')][_0xa5aa('0x11')](this));ami['on'](_0xa5aa('0x14'),this[_0xa5aa('0x15')]['bind'](this));ami['on'](_0xa5aa('0x16'),this[_0xa5aa('0x17')]['bind'](this));ami['on'](_0xa5aa('0x18'),this['syncFaxStatus'][_0xa5aa('0x11')](this));ami['on'](_0xa5aa('0x19'),this['syncReceiveFax'][_0xa5aa('0x11')](this));ami['on']('hangup',this['syncHangup']['bind'](this));ami['on'](_0xa5aa('0x1a'),this[_0xa5aa('0x1b')]['bind'](this));}Fax[_0xa5aa('0x1c')][_0xa5aa('0x10')]=function(_0x47fa3b){if(_0x47fa3b[_0xa5aa('0x1d')]===_0xa5aa('0x1e')){for(var _0x13d868 in this[_0xa5aa('0xc')]){if(this[_0xa5aa('0xc')][_0xa5aa('0x1f')](_0x13d868)){if(_0x47fa3b['exten']===this['faxAccounts'][_0x13d868]['localstationid']){this['channels'][_0x47fa3b[_0xa5aa('0x20')]]={};this[_0xa5aa('0x21')][_0x47fa3b[_0xa5aa('0x20')]]['channel']=_0x47fa3b[_0xa5aa('0x22')];this['channels'][_0x47fa3b[_0xa5aa('0x20')]]['id']=this[_0xa5aa('0xc')][_0x13d868]['id'];this['channels'][_0x47fa3b[_0xa5aa('0x20')]][_0xa5aa('0x23')]=this[_0xa5aa('0xc')][_0x13d868][_0xa5aa('0x23')];logger[_0xa5aa('0x24')](_0x47fa3b[_0xa5aa('0x20')],'\x20syncNewchannel\x20');}}}}if(this['actions'][_0x47fa3b[_0xa5aa('0x20')]]){this[_0xa5aa('0x21')][_0x47fa3b[_0xa5aa('0x20')]]={};this['channels'][_0x47fa3b[_0xa5aa('0x20')]][_0xa5aa('0x22')]=_0x47fa3b['channel'];logger['info'](_0x47fa3b['uniqueid'],_0xa5aa('0x25'));}};Fax[_0xa5aa('0x1c')]['syncVarSet']=function(_0x2a724d){if(this[_0xa5aa('0x21')][_0x2a724d[_0xa5aa('0x20')]]){for(var _0x340108 in _0x2a724d[_0xa5aa('0x26')]){if(_0x2a724d['variable']['hasOwnProperty'](_0x340108)){this[_0xa5aa('0x21')][_0x2a724d[_0xa5aa('0x20')]][_0x340108]=_0x2a724d[_0xa5aa('0x27')];}}}};Fax[_0xa5aa('0x1c')][_0xa5aa('0x15')]=function(_0x2bb0d2){if(this['channels'][_0x2bb0d2[_0xa5aa('0x20')]]){this[_0xa5aa('0x21')][_0x2bb0d2[_0xa5aa('0x20')]][_0xa5aa('0x28')]=_0x2bb0d2[_0xa5aa('0x28')];this[_0xa5aa('0x21')][_0x2bb0d2['uniqueid']]['channelstatedesc']=_0x2bb0d2[_0xa5aa('0x29')];}};Fax[_0xa5aa('0x1c')][_0xa5aa('0x17')]=function(_0x5aac22){if(this[_0xa5aa('0x21')][_0x5aac22[_0xa5aa('0x20')]]){for(var _0x269175 in _0x5aac22[_0xa5aa('0x26')]){if(_0x5aac22[_0xa5aa('0x26')][_0xa5aa('0x1f')](_0x269175)){this['channels'][_0x5aac22[_0xa5aa('0x20')]][_0x269175]=_0x5aac22[_0xa5aa('0x27')];}}}};Fax[_0xa5aa('0x1c')][_0xa5aa('0x2a')]=function(_0x3bc299){if(this[_0xa5aa('0x21')][_0x3bc299[_0xa5aa('0x20')]]){this[_0xa5aa('0x21')][_0x3bc299[_0xa5aa('0x20')]][_0xa5aa('0x2b')]=_0x3bc299[_0xa5aa('0x2b')];this['channels'][_0x3bc299['uniqueid']][_0xa5aa('0x2c')]=_0x3bc299[_0xa5aa('0x2c')];this[_0xa5aa('0x21')][_0x3bc299['uniqueid']]['localstationid']=_0x3bc299[_0xa5aa('0x2d')];this[_0xa5aa('0x21')][_0x3bc299[_0xa5aa('0x20')]][_0xa5aa('0x2e')]=_0x3bc299[_0xa5aa('0x2e')];logger['info'](_0x3bc299[_0xa5aa('0x20')],_0xa5aa('0x2f'));}};Fax[_0xa5aa('0x1c')][_0xa5aa('0x30')]=function(_0x4e771b){var _0x4d883a=this;if(this['channels'][_0x4e771b[_0xa5aa('0x20')]]){var _0x245088=this[_0xa5aa('0x21')][_0x4e771b[_0xa5aa('0x20')]]['id'];this[_0xa5aa('0x21')][_0x4e771b[_0xa5aa('0x20')]][_0xa5aa('0x31')]=_0x4e771b[_0xa5aa('0x31')];this[_0xa5aa('0x21')][_0x4e771b[_0xa5aa('0x20')]][_0xa5aa('0x32')]=_0x4e771b[_0xa5aa('0x32')];this[_0xa5aa('0x21')][_0x4e771b[_0xa5aa('0x20')]][_0xa5aa('0x2d')]=_0x4e771b[_0xa5aa('0x2d')];this[_0xa5aa('0x21')][_0x4e771b[_0xa5aa('0x20')]][_0xa5aa('0x33')]=_0x4e771b[_0xa5aa('0x33')];this[_0xa5aa('0x21')][_0x4e771b[_0xa5aa('0x20')]]['transferrate']=_0x4e771b[_0xa5aa('0x34')];this['channels'][_0x4e771b[_0xa5aa('0x20')]][_0xa5aa('0x2e')]=_0x4e771b[_0xa5aa('0x2e')];this[_0xa5aa('0x21')][_0x4e771b[_0xa5aa('0x20')]][_0xa5aa('0x35')]=_0x4e771b['calleridnum'];if(this[_0xa5aa('0x21')][_0x4e771b[_0xa5aa('0x20')]][_0xa5aa('0x32')]>0x0){tiff2pdf(_0x4e771b[_0xa5aa('0x2e')],'/var/opt/motion2/server/files/attachments/',function(_0x17a5ed){logger[_0xa5aa('0x24')](_0x4e771b[_0xa5aa('0x20')],_0xa5aa('0x36'),_0x17a5ed['message']);attachment['create']({'name':path[_0xa5aa('0x37')](_0x4e771b[_0xa5aa('0x2e')])[_0xa5aa('0x23')],'basename':path[_0xa5aa('0x37')](_0x4e771b[_0xa5aa('0x2e')])[_0xa5aa('0x23')]+_0xa5aa('0x38'),'type':_0xa5aa('0x39')})[_0xa5aa('0x3a')](function(_0x76a68e){faxAccount[_0xa5aa('0x3b')](_0x245088,{'from':_0x4e771b['calleridnum'],'mapKey':'fax','body':path[_0xa5aa('0x37')](_0x4e771b[_0xa5aa('0x2e')])[_0xa5aa('0x23')]+_0xa5aa('0x38'),'AttachmentId':_0x76a68e['id']})[_0xa5aa('0x3c')](function(_0x38dd7f){logger[_0xa5aa('0x3d')](_0x4e771b[_0xa5aa('0x20')],'\x20notify\x20',JSON[_0xa5aa('0x3e')](_0x38dd7f));});})[_0xa5aa('0x3c')](function(_0x14fe28){logger[_0xa5aa('0x3d')](_0x4e771b[_0xa5aa('0x20')],_0xa5aa('0x3f'),JSON[_0xa5aa('0x3e')](_0x14fe28));});});}logger[_0xa5aa('0x24')](_0x4e771b[_0xa5aa('0x20')],'\x20receiveFax\x20');}};Fax[_0xa5aa('0x1c')][_0xa5aa('0x1b')]=function(_0x5ec3ee){if(this[_0xa5aa('0xe')][_0x5ec3ee[_0xa5aa('0x20')]]){_[_0xa5aa('0x40')](this['channels'][_0x5ec3ee['uniqueid']],_['pick'](_0x5ec3ee,[_0xa5aa('0x41'),_0xa5aa('0x42')]));}};function updateStatus(_0x2fab53){if(_0x2fab53[_0xa5aa('0x43')]){faxMessage[_0xa5aa('0x44')](_0x2fab53[_0xa5aa('0x43')],{'failMessage':_0xa5aa('0x45')})['then'](function(_0x38198f){ut[_0xa5aa('0x46')](_0xa5aa('0x47'),_0x38198f);})[_0xa5aa('0x3c')](function(_0x39e93d){ut[_0xa5aa('0x46')](_0xa5aa('0x48'),_0x39e93d);});}else{ut[_0xa5aa('0x46')](_0xa5aa('0x49'));}}Fax[_0xa5aa('0x1c')][_0xa5aa('0x4a')]=function(_0x182de3,_0x184bef,_0x52a889){var _0xabd290=this;var _0x138dd9={};if(_0x184bef){_0x138dd9={'read':_0x184bef};}else{_0x138dd9={'failMessage':_0x52a889};}if(this[_0xa5aa('0xe')][_0x182de3][_0xa5aa('0x43')]){faxMessage[_0xa5aa('0x44')](this['actions'][_0x182de3][_0xa5aa('0x43')],_0x138dd9)[_0xa5aa('0x3a')](function(_0x33db47){ut[_0xa5aa('0x46')](_0xa5aa('0x4b'),_0x33db47);})[_0xa5aa('0x3c')](function(_0x81fb43){ut[_0xa5aa('0x46')](_0xa5aa('0x4c'),_0x81fb43);})[_0xa5aa('0x4d')](function(){delete _0xabd290[_0xa5aa('0x21')][_0x182de3];delete _0xabd290[_0xa5aa('0xe')][_0x182de3];});}else{ut[_0xa5aa('0x46')](_0xa5aa('0x4e'));delete this[_0xa5aa('0x21')][_0x182de3];delete this['actions'][_0x182de3];}};Fax[_0xa5aa('0x1c')]['syncHangup']=function(_0x263ce9){var _0x167e7e=this;if(this[_0xa5aa('0x21')][_0x263ce9[_0xa5aa('0x20')]]){if(this[_0xa5aa('0xe')][_0x263ce9[_0xa5aa('0x20')]]){this['channels'][_0x263ce9[_0xa5aa('0x20')]]['id']=this[_0xa5aa('0xe')][_0x263ce9[_0xa5aa('0x20')]]['id'];this['channels'][_0x263ce9['uniqueid']]['name']=this[_0xa5aa('0xe')][_0x263ce9[_0xa5aa('0x20')]][_0xa5aa('0x23')];}logger[_0xa5aa('0x24')](_0x263ce9[_0xa5aa('0x20')],this[_0xa5aa('0xe')][_0x263ce9[_0xa5aa('0x20')]]?_0xa5aa('0x4f'):_0xa5aa('0x50'),JSON[_0xa5aa('0x3e')](this[_0xa5aa('0x21')][_0x263ce9['uniqueid']]));if(this[_0xa5aa('0xe')][_0x263ce9[_0xa5aa('0x20')]]){if(this[_0xa5aa('0x21')][_0x263ce9['uniqueid']][_0xa5aa('0x51')]>0x0&&this['channels'][_0x263ce9[_0xa5aa('0x20')]][_0xa5aa('0x18')]==='SUCCESS'){this['updateStatus'](_0x263ce9[_0xa5aa('0x20')],!![]);}else{this[_0xa5aa('0x4a')](_0x263ce9[_0xa5aa('0x20')],![],this[_0xa5aa('0x21')][_0x263ce9[_0xa5aa('0x20')]][_0xa5aa('0x52')]?_0xa5aa('0x53')+this[_0xa5aa('0x21')][_0x263ce9['uniqueid']][_0xa5aa('0x52')]:_0xa5aa('0x54'));}}}};Fax[_0xa5aa('0x1c')][_0xa5aa('0x55')]=function(_0x2fe70b){var _0x28d393=this;return new BPromise(function(_0x3922b3,_0xa9010a){_0x28d393['execOriginate'](_0x2fe70b,_0x28d393[_0xa5aa('0xc')][_0x2fe70b[_0xa5aa('0x56')]])[_0xa5aa('0x3a')](function(_0x327418){ut['logger'](_0xa5aa('0x57'),_0x327418);_0x3922b3(ut[_0xa5aa('0x58')](_0xa5aa('0x59')));})[_0xa5aa('0x3c')](function(_0x10b332){ut[_0xa5aa('0x46')]('[Fax][execOriginate][failure]',_0x10b332);updateStatus(_0x2fe70b);_0xa9010a(ut[_0xa5aa('0x58')]('Fax\x20failed'));});});};Fax[_0xa5aa('0x1c')]['checkAfterSend']=function(_0x210048){var _0xd20818=this;return new BPromise(function(_0x425392,_0x23e8e0){var _0x39402f=_0xd20818['checkTrunk'](_0xd20818[_0xa5aa('0xc')][_0x210048[_0xa5aa('0x56')]][_0xa5aa('0x5a')],_0x210048);if(_0x39402f[_0xa5aa('0x5b')]){_0x425392(ut['message'](_0xa5aa('0x5c')));}else{_0x23e8e0(ut[_0xa5aa('0x58')](_0x39402f[_0xa5aa('0x58')]));}});};function originate(_0x2c8349,_0x1877c7,_0x1cb695){return new BPromise(function(_0x1de21a,_0x5527a4){if(_0x1cb695){var _0x3bcd4d=util[_0xa5aa('0x5d')](_0xa5aa('0x5e'),path[_0xa5aa('0x37')](_0x2c8349['path'])['name'],_0x2c8349[_0xa5aa('0x3')]);gs[_0xa5aa('0x5f')](_0x3bcd4d)['then'](function(){_0x2c8349[_0xa5aa('0x3')]=util[_0xa5aa('0x5d')]('%s/%s.tif',path['parse'](_0x2c8349[_0xa5aa('0x3')])[_0xa5aa('0x60')],path[_0xa5aa('0x37')](_0x2c8349[_0xa5aa('0x3')])[_0xa5aa('0x23')]);_0x1877c7[_0xa5aa('0x61')]=_0x2c8349[_0xa5aa('0x3')];ut['logger'](_0xa5aa('0x62'),'');ami['Action'](ut[_0xa5aa('0x63')](_0x2c8349,_0x1877c7))['then'](function(_0x29be0f){_0x1de21a(_0x29be0f);})['catch'](function(_0xc4021){_0x5527a4(_0xc4021);});})['catch'](function(_0x553776){_0x5527a4(_0x553776);});}else{ami[_0xa5aa('0x64')](ut[_0xa5aa('0x63')](_0x2c8349,_0x1877c7))[_0xa5aa('0x3a')](function(_0x10d24a){_0x1de21a(_0x10d24a);})[_0xa5aa('0x3c')](function(_0x4a308c){_0x5527a4(_0x4a308c);});}});}Fax['prototype'][_0xa5aa('0x65')]=function(_0x8851e1,_0x4dbdff){var _0x3de627=this;return new BPromise(function(_0x38143c,_0x296077){var _0x5c7ee7=uuid['v4']();var _0x4e1a06=ut[_0xa5aa('0x66')](_0x8851e1,_0x4dbdff);_0x3de627[_0xa5aa('0xe')][_0x5c7ee7]=new Action(_0x5c7ee7,_0x4dbdff,_0x8851e1);var _0x423857=_0x3de627[_0xa5aa('0xe')][_0x5c7ee7];if(path[_0xa5aa('0x37')](_0x8851e1[_0xa5aa('0x3')])['ext']===_0xa5aa('0x38')){originate(_0x423857,_0x4e1a06,!![])[_0xa5aa('0x3a')](function(_0x40d2cb){_0x38143c(_0x40d2cb);})[_0xa5aa('0x3c')](function(_0x1c578){_0x296077(_0x1c578);});}else{originate(_0x423857,_0x4e1a06,![])[_0xa5aa('0x3a')](function(_0x5e1a24){_0x38143c(_0x5e1a24);})[_0xa5aa('0x3c')](function(_0x1c680c){_0x296077(_0x1c680c);});}});};Fax[_0xa5aa('0x1c')][_0xa5aa('0x67')]=function(_0x4da591){return!_[_0xa5aa('0x68')](_0x4da591)&&!_[_0xa5aa('0x68')](_0x4da591['name'])&&!_[_0xa5aa('0x68')](_0x4da591[_0xa5aa('0x69')])&&_0x4da591[_0xa5aa('0x69')]&&!_[_0xa5aa('0x68')](this[_0xa5aa('0xd')][util[_0xa5aa('0x5d')](_0xa5aa('0x6a'),_0x4da591[_0xa5aa('0x23')])])&&!_['isNil'](this['trunks'][util[_0xa5aa('0x5d')](_0xa5aa('0x6a'),_0x4da591[_0xa5aa('0x23')])]['status'])&&(this[_0xa5aa('0xd')][util[_0xa5aa('0x5d')](_0xa5aa('0x6a'),_0x4da591[_0xa5aa('0x23')])][_0xa5aa('0x2c')]==='reachable'||this[_0xa5aa('0xd')][util[_0xa5aa('0x5d')](_0xa5aa('0x6a'),_0x4da591[_0xa5aa('0x23')])][_0xa5aa('0x2c')]===_0xa5aa('0x6b'));};Fax[_0xa5aa('0x1c')]['checkTrunk']=function(_0x410e35,_0x453db1){if(!this[_0xa5aa('0x67')](_0x410e35)){ut[_0xa5aa('0x46')]('[Fax][checkTrunk][Trunk\x20not\x20work]',_[_0xa5aa('0x68')](_0x410e35)?'':_0x410e35[_0xa5aa('0x23')]+'\x20Trunk\x20is\x20not\x20active');return{'isValid':![],'message':_0xa5aa('0x6c')};}if(_[_0xa5aa('0x68')](_0x453db1[_0xa5aa('0x6d')])){ut[_0xa5aa('0x46')](_0xa5aa('0x6e'),JSON['stringify'](_0x453db1));return{'isValid':![],'message':_0xa5aa('0x6f')};}if(_[_0xa5aa('0x68')](_0x453db1[_0xa5aa('0x3')])){ut['logger']('[Fax][checkTrunk][Empty\x20path]',JSON[_0xa5aa('0x3e')](_0x453db1));return{'isValid':![],'message':_0xa5aa('0x70')};}return{'isValid':!![],'message':'Is\x20Valid'};};module[_0xa5aa('0x71')]=Fax;