Built motion from commit 10af8726.|2.6.34
[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 _0xf440=['create','parse','.pdf','application/pdf','notify','error','\x20notify\x20','stringify','catch','\x20receiveFax\x20','pick','reason','AttachmentId','update','then','logger','[Fax][Message][Error][Updated]','[Fax][Message][Error][NoUpdate2]','updateStatus','[Fax][Message][NoUpdate1]','\x20hangup\x20sendFile\x20','faxpages','SUCCESS','faxstatusstring','SENDING\x20FAILED:\x20','SENDING\x20FAILED','FaxAccountId','Fax\x20received','[Fax][execOriginate][failure]','Fax\x20failed','Trunk','isValid','Trunk\x20Reachable','path','format','%s/%s.tif','dir','variablesAction','Action','execOriginate','addVariables','ext','isNil','active','SIP/%s','reachable','checkTrunk','[Fax][checkTrunk][Trunk\x20not\x20working\x20correctly]','\x20Trunk\x20is\x20not\x20active','Trunk\x20not\x20working\x20correctly','Empty\x20Fax','[Fax][checkTrunk][Empty\x20path]','Empty\x20path','Is\x20Valid','deleteChannelAfterTimeout','exports','util','bluebird','uuid','moment','./util/gs','../../../config/logger','fax','./rpcDb','./channel','./util/util','../rpc/attachment','../rpc/faxMessage','channels','faxAccounts','trunks','actions','newchannel','syncNewchannel','varset','syncVarSet','bind','newstate','syncNewState','faxstatus','syncFaxStatus','receivefax','syncReceiveFax','hangup','syncHangup','originateresponse','syncOriginateResponse','prototype','context','from-voip-provider','hasOwnProperty','localstationid','uniqueid','channel','name','\x20syncNewchannel\x20','info','variable','value','channelstate','channelstatedesc','syncNewExten','operation','status','filename','\x20syncFaxStatus\x20','remotestationid','pagestransferred','resolution','transferrate','calleridnum','\x20result\x20conversion\x20file\x20','message'];(function(_0x3c8361,_0x23fc18){var _0x1af7bc=function(_0x4b7144){while(--_0x4b7144){_0x3c8361['push'](_0x3c8361['shift']());}};_0x1af7bc(++_0x23fc18);}(_0xf440,0x18b));var _0x0f44=function(_0x2641f9,_0x26715f){_0x2641f9=_0x2641f9-0x0;var _0x585741=_0xf440[_0x2641f9];return _0x585741;};'use strict';var util=require(_0x0f44('0x0'));var _=require('lodash');var BPromise=require(_0x0f44('0x1'));var uuid=require(_0x0f44('0x2'));var moment=require(_0x0f44('0x3'));var path=require('path');var tiff2pdf=require('tiff2pdf');var gs=require(_0x0f44('0x4'));var logger=require(_0x0f44('0x5'))(_0x0f44('0x6'));var ami=require('../ami');var rpcDb=require(_0x0f44('0x7'));var Channel=require(_0x0f44('0x8'));var ut=require(_0x0f44('0x9'));var Action=require('./action');var faxAccount=require('../rpc/faxAccount');var attachment=require(_0x0f44('0xa'));var faxMessage=require(_0x0f44('0xb'));function Fax(_0xa73050){this[_0x0f44('0xc')]={};this[_0x0f44('0xd')]=_0xa73050['faxAccounts'];this[_0x0f44('0xe')]=_0xa73050[_0x0f44('0xe')];this[_0x0f44('0xf')]={};ami['on'](_0x0f44('0x10'),this[_0x0f44('0x11')]['bind'](this));ami['on'](_0x0f44('0x12'),this[_0x0f44('0x13')][_0x0f44('0x14')](this));ami['on'](_0x0f44('0x15'),this[_0x0f44('0x16')][_0x0f44('0x14')](this));ami['on']('newexten',this['syncNewExten']['bind'](this));ami['on'](_0x0f44('0x17'),this[_0x0f44('0x18')][_0x0f44('0x14')](this));ami['on'](_0x0f44('0x19'),this[_0x0f44('0x1a')]['bind'](this));ami['on'](_0x0f44('0x1b'),this[_0x0f44('0x1c')][_0x0f44('0x14')](this));ami['on'](_0x0f44('0x1d'),this[_0x0f44('0x1e')][_0x0f44('0x14')](this));}Fax[_0x0f44('0x1f')]['syncNewchannel']=function(_0x9588e0){if(_0x9588e0[_0x0f44('0x20')]===_0x0f44('0x21')){for(var _0x2fcd8d in this[_0x0f44('0xd')]){if(this[_0x0f44('0xd')][_0x0f44('0x22')](_0x2fcd8d)){if(_0x9588e0['exten']===this[_0x0f44('0xd')][_0x2fcd8d][_0x0f44('0x23')]){this[_0x0f44('0xc')][_0x9588e0[_0x0f44('0x24')]]={};this['channels'][_0x9588e0[_0x0f44('0x24')]][_0x0f44('0x25')]=_0x9588e0[_0x0f44('0x25')];this[_0x0f44('0xc')][_0x9588e0['uniqueid']]['id']=this[_0x0f44('0xd')][_0x2fcd8d]['id'];this[_0x0f44('0xc')][_0x9588e0[_0x0f44('0x24')]][_0x0f44('0x26')]=this[_0x0f44('0xd')][_0x2fcd8d]['name'];logger['info'](_0x9588e0[_0x0f44('0x24')],_0x0f44('0x27'));}}}}if(this[_0x0f44('0xf')][_0x9588e0['uniqueid']]){this[_0x0f44('0xc')][_0x9588e0[_0x0f44('0x24')]]={};this[_0x0f44('0xc')][_0x9588e0['uniqueid']]['channel']=_0x9588e0['channel'];logger[_0x0f44('0x28')](_0x9588e0[_0x0f44('0x24')],_0x0f44('0x27'));}};Fax[_0x0f44('0x1f')][_0x0f44('0x13')]=function(_0x2887a4){if(this['channels'][_0x2887a4['uniqueid']]){for(var _0x321bad in _0x2887a4[_0x0f44('0x29')]){if(_0x2887a4[_0x0f44('0x29')]['hasOwnProperty'](_0x321bad)){this[_0x0f44('0xc')][_0x2887a4[_0x0f44('0x24')]][_0x321bad]=_0x2887a4[_0x0f44('0x2a')];}}}};Fax[_0x0f44('0x1f')][_0x0f44('0x16')]=function(_0xf00efa){if(this['channels'][_0xf00efa['uniqueid']]){this[_0x0f44('0xc')][_0xf00efa['uniqueid']][_0x0f44('0x2b')]=_0xf00efa[_0x0f44('0x2b')];this[_0x0f44('0xc')][_0xf00efa[_0x0f44('0x24')]]['channelstatedesc']=_0xf00efa[_0x0f44('0x2c')];}};Fax[_0x0f44('0x1f')][_0x0f44('0x2d')]=function(_0xa3d164){if(this[_0x0f44('0xc')][_0xa3d164['uniqueid']]){for(var _0x404d3c in _0xa3d164['variable']){if(_0xa3d164['variable'][_0x0f44('0x22')](_0x404d3c)){this['channels'][_0xa3d164['uniqueid']][_0x404d3c]=_0xa3d164[_0x0f44('0x2a')];}}}};Fax[_0x0f44('0x1f')]['syncFaxStatus']=function(_0x5507c9){if(this['channels'][_0x5507c9[_0x0f44('0x24')]]){this[_0x0f44('0xc')][_0x5507c9[_0x0f44('0x24')]][_0x0f44('0x2e')]=_0x5507c9[_0x0f44('0x2e')];this[_0x0f44('0xc')][_0x5507c9[_0x0f44('0x24')]][_0x0f44('0x2f')]=_0x5507c9[_0x0f44('0x2f')];this[_0x0f44('0xc')][_0x5507c9['uniqueid']][_0x0f44('0x23')]=_0x5507c9[_0x0f44('0x23')];this[_0x0f44('0xc')][_0x5507c9['uniqueid']][_0x0f44('0x30')]=_0x5507c9[_0x0f44('0x30')];logger[_0x0f44('0x28')](_0x5507c9[_0x0f44('0x24')],_0x0f44('0x31'));}};Fax[_0x0f44('0x1f')][_0x0f44('0x1a')]=function(_0x2cfc85){var _0x208410=this;if(this[_0x0f44('0xc')][_0x2cfc85[_0x0f44('0x24')]]){var _0x2b78f3=this['channels'][_0x2cfc85[_0x0f44('0x24')]]['id'];this['channels'][_0x2cfc85[_0x0f44('0x24')]][_0x0f44('0x32')]=_0x2cfc85['remotestationid'];this['channels'][_0x2cfc85[_0x0f44('0x24')]][_0x0f44('0x33')]=_0x2cfc85[_0x0f44('0x33')];this[_0x0f44('0xc')][_0x2cfc85['uniqueid']][_0x0f44('0x23')]=_0x2cfc85['localstationid'];this[_0x0f44('0xc')][_0x2cfc85['uniqueid']][_0x0f44('0x34')]=_0x2cfc85[_0x0f44('0x34')];this[_0x0f44('0xc')][_0x2cfc85['uniqueid']]['transferrate']=_0x2cfc85[_0x0f44('0x35')];this['channels'][_0x2cfc85['uniqueid']][_0x0f44('0x30')]=_0x2cfc85['filename'];this[_0x0f44('0xc')][_0x2cfc85[_0x0f44('0x24')]][_0x0f44('0x36')]=_0x2cfc85['calleridnum'];if(this[_0x0f44('0xc')][_0x2cfc85[_0x0f44('0x24')]][_0x0f44('0x33')]>0x0){tiff2pdf(_0x2cfc85['filename'],'/var/opt/motion2/server/files/attachments/',function(_0x4b69d4){logger[_0x0f44('0x28')](_0x2cfc85[_0x0f44('0x24')],_0x0f44('0x37'),_0x4b69d4[_0x0f44('0x38')]);attachment[_0x0f44('0x39')]({'name':path[_0x0f44('0x3a')](_0x2cfc85[_0x0f44('0x30')])['name'],'basename':path[_0x0f44('0x3a')](_0x2cfc85['filename'])[_0x0f44('0x26')]+_0x0f44('0x3b'),'type':_0x0f44('0x3c')})['then'](function(_0x2e77a9){faxAccount[_0x0f44('0x3d')](_0x2b78f3,{'from':_0x2cfc85[_0x0f44('0x36')],'mapKey':_0x0f44('0x6'),'body':path[_0x0f44('0x3a')](_0x2cfc85[_0x0f44('0x30')])['name']+_0x0f44('0x3b'),'AttachmentId':_0x2e77a9['id']})['catch'](function(_0x488782){logger[_0x0f44('0x3e')](_0x2cfc85['uniqueid'],_0x0f44('0x3f'),JSON[_0x0f44('0x40')](_0x488782));});})[_0x0f44('0x41')](function(_0x4e1bfe){logger[_0x0f44('0x3e')](_0x2cfc85[_0x0f44('0x24')],'\x20receiveFax\x20',JSON[_0x0f44('0x40')](_0x4e1bfe));});});}logger['info'](_0x2cfc85[_0x0f44('0x24')],_0x0f44('0x42'));}};Fax[_0x0f44('0x1f')][_0x0f44('0x1e')]=function(_0x169e93){if(this[_0x0f44('0xf')][_0x169e93[_0x0f44('0x24')]]){_['merge'](this[_0x0f44('0xc')][_0x169e93[_0x0f44('0x24')]],_[_0x0f44('0x43')](_0x169e93,['response',_0x0f44('0x44')]));}};function updateStatus(_0x29f531){if(_0x29f531[_0x0f44('0x45')]){faxMessage[_0x0f44('0x46')](_0x29f531[_0x0f44('0x45')],{'failMessage':'SENDING\x20FAILED\x20'})[_0x0f44('0x47')](function(_0x36bbd5){ut[_0x0f44('0x48')](_0x0f44('0x49'),_0x36bbd5);})[_0x0f44('0x41')](function(_0x1d39f3){ut[_0x0f44('0x48')]('[Fax][Message][Error][NoUpdate1]',_0x1d39f3);});}else{ut[_0x0f44('0x48')](_0x0f44('0x4a'));}}Fax['prototype'][_0x0f44('0x4b')]=function(_0x518292,_0x40cadc,_0x4789e0){var _0x49b4d1=this;var _0x45955f={};if(_0x40cadc){_0x45955f={'read':_0x40cadc};}else{_0x45955f={'failMessage':_0x4789e0};}if(this['actions'][_0x518292][_0x0f44('0x45')]){faxMessage[_0x0f44('0x46')](this[_0x0f44('0xf')][_0x518292]['AttachmentId'],_0x45955f)[_0x0f44('0x47')](function(_0x2468a8){ut['logger']('[Fax][Message][Updated]',_0x2468a8);})['catch'](function(_0x18de84){ut[_0x0f44('0x48')](_0x0f44('0x4c'),_0x18de84);})['finally'](function(){delete _0x49b4d1[_0x0f44('0xc')][_0x518292];delete _0x49b4d1[_0x0f44('0xf')][_0x518292];});}else{ut[_0x0f44('0x48')]('[Fax][Message][NoUpdate2]');this['deleteChannelAfterTimeout'](_0x518292);delete this[_0x0f44('0xf')][_0x518292];}};Fax[_0x0f44('0x1f')][_0x0f44('0x1c')]=function(_0x51f6e6){var _0x49f159=this;if(this['channels'][_0x51f6e6['uniqueid']]){if(this[_0x0f44('0xf')][_0x51f6e6['uniqueid']]){this[_0x0f44('0xc')][_0x51f6e6[_0x0f44('0x24')]]['id']=this[_0x0f44('0xf')][_0x51f6e6[_0x0f44('0x24')]]['id'];this[_0x0f44('0xc')][_0x51f6e6[_0x0f44('0x24')]]['name']=this[_0x0f44('0xf')][_0x51f6e6[_0x0f44('0x24')]][_0x0f44('0x26')];}logger[_0x0f44('0x28')](_0x51f6e6[_0x0f44('0x24')],this['actions'][_0x51f6e6[_0x0f44('0x24')]]?_0x0f44('0x4d'):'\x20hangup\x20faxRiceive\x20',JSON['stringify'](this['channels'][_0x51f6e6[_0x0f44('0x24')]]));if(this['actions'][_0x51f6e6[_0x0f44('0x24')]]){if(this[_0x0f44('0xc')][_0x51f6e6[_0x0f44('0x24')]][_0x0f44('0x4e')]>0x0&&this['channels'][_0x51f6e6[_0x0f44('0x24')]][_0x0f44('0x17')]===_0x0f44('0x4f')){this[_0x0f44('0x4b')](_0x51f6e6[_0x0f44('0x24')],!![]);}else{this[_0x0f44('0x4b')](_0x51f6e6['uniqueid'],![],this[_0x0f44('0xc')][_0x51f6e6[_0x0f44('0x24')]][_0x0f44('0x50')]?_0x0f44('0x51')+this[_0x0f44('0xc')][_0x51f6e6[_0x0f44('0x24')]][_0x0f44('0x50')]:_0x0f44('0x52'));}}}};Fax[_0x0f44('0x1f')]['send']=function(_0x58cfcb){var _0x381bd3=this;return new BPromise(function(_0x53e180,_0x197904){_0x381bd3['execOriginate'](_0x58cfcb,_0x381bd3['faxAccounts'][_0x58cfcb[_0x0f44('0x53')]])[_0x0f44('0x47')](function(_0x55d8cc){ut[_0x0f44('0x48')]('[Fax][execOriginate][success]',_0x55d8cc);_0x53e180(ut[_0x0f44('0x38')](_0x0f44('0x54')));})[_0x0f44('0x41')](function(_0x358fbf){ut[_0x0f44('0x48')](_0x0f44('0x55'),_0x358fbf);updateStatus(_0x58cfcb);_0x197904(ut[_0x0f44('0x38')](_0x0f44('0x56')));});});};Fax['prototype']['checkAfterSend']=function(_0x125b97){var _0x2d3326=this;return new BPromise(function(_0x4ada64,_0x4d8bd4){var _0x2b462c=_0x2d3326['checkTrunk'](_0x2d3326[_0x0f44('0xd')][_0x125b97[_0x0f44('0x53')]][_0x0f44('0x57')],_0x125b97);if(_0x2b462c[_0x0f44('0x58')]){_0x4ada64(ut[_0x0f44('0x38')](_0x0f44('0x59')));}else{_0x4d8bd4(ut[_0x0f44('0x38')](_0x2b462c[_0x0f44('0x38')]));}});};function originate(_0x4dc8e1,_0x35146c,_0x2d7c83){return new BPromise(function(_0x59bb5d,_0x17c512){if(_0x2d7c83){var _0x26e738=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[_0x0f44('0x3a')](_0x4dc8e1[_0x0f44('0x5a')])['name'],_0x4dc8e1[_0x0f44('0x5a')]);gs['execute'](_0x26e738)[_0x0f44('0x47')](function(){_0x4dc8e1['path']=util[_0x0f44('0x5b')](_0x0f44('0x5c'),path['parse'](_0x4dc8e1[_0x0f44('0x5a')])[_0x0f44('0x5d')],path[_0x0f44('0x3a')](_0x4dc8e1[_0x0f44('0x5a')])[_0x0f44('0x26')]);_0x35146c['XMF-PATH']=_0x4dc8e1[_0x0f44('0x5a')];ut[_0x0f44('0x48')]('Convert\x20pdf\x20to\x20tif','');ami['Action'](ut[_0x0f44('0x5e')](_0x4dc8e1,_0x35146c))[_0x0f44('0x47')](function(_0x292692){_0x59bb5d(_0x292692);})[_0x0f44('0x41')](function(_0x4e77d9){_0x17c512(_0x4e77d9);});})[_0x0f44('0x41')](function(_0x15aad8){_0x17c512(_0x15aad8);});}else{ami[_0x0f44('0x5f')](ut[_0x0f44('0x5e')](_0x4dc8e1,_0x35146c))[_0x0f44('0x47')](function(_0x98b95e){_0x59bb5d(_0x98b95e);})['catch'](function(_0x321ebe){_0x17c512(_0x321ebe);});}});}Fax['prototype'][_0x0f44('0x60')]=function(_0x3aa4cd,_0x247468){var _0x2e5a7e=this;return new BPromise(function(_0x4fe46c,_0xc3cd4){var _0x2509cb=uuid['v4']();var _0x55a0c6=ut[_0x0f44('0x61')](_0x3aa4cd,_0x247468);_0x2e5a7e['actions'][_0x2509cb]=new Action(_0x2509cb,_0x247468,_0x3aa4cd);var _0x3fa2cd=_0x2e5a7e[_0x0f44('0xf')][_0x2509cb];if(path[_0x0f44('0x3a')](_0x3aa4cd[_0x0f44('0x5a')])[_0x0f44('0x62')]===_0x0f44('0x3b')){originate(_0x3fa2cd,_0x55a0c6,!![])[_0x0f44('0x47')](function(_0x227697){_0x4fe46c(_0x227697);})['catch'](function(_0x4adbb7){_0xc3cd4(_0x4adbb7);});}else{originate(_0x3fa2cd,_0x55a0c6,![])['then'](function(_0x45de28){_0x4fe46c(_0x45de28);})[_0x0f44('0x41')](function(_0x3c1f89){_0xc3cd4(_0x3c1f89);});}});};Fax[_0x0f44('0x1f')]['isTrunkReachable']=function(_0x609e22){return!_[_0x0f44('0x63')](_0x609e22)&&!_[_0x0f44('0x63')](_0x609e22[_0x0f44('0x26')])&&!_[_0x0f44('0x63')](_0x609e22[_0x0f44('0x64')])&&_0x609e22[_0x0f44('0x64')]&&!_[_0x0f44('0x63')](this['trunks'][util['format'](_0x0f44('0x65'),_0x609e22[_0x0f44('0x26')])])&&!_['isNil'](this['trunks'][util['format'](_0x0f44('0x65'),_0x609e22[_0x0f44('0x26')])][_0x0f44('0x2f')])&&(this[_0x0f44('0xe')][util[_0x0f44('0x5b')]('SIP/%s',_0x609e22[_0x0f44('0x26')])][_0x0f44('0x2f')]===_0x0f44('0x66')||this[_0x0f44('0xe')][util[_0x0f44('0x5b')](_0x0f44('0x65'),_0x609e22[_0x0f44('0x26')])][_0x0f44('0x2f')]==='unmonitored');};Fax[_0x0f44('0x1f')][_0x0f44('0x67')]=function(_0x314093,_0x4ae7cc){if(!this['isTrunkReachable'](_0x314093)){ut['logger'](_0x0f44('0x68'),_[_0x0f44('0x63')](_0x314093)?'':_0x314093['name']+_0x0f44('0x69'));return{'isValid':![],'message':_0x0f44('0x6a')};}if(_[_0x0f44('0x63')](_0x4ae7cc[_0x0f44('0x6')])){ut[_0x0f44('0x48')]('[Fax][checkTrunk][Empty\x20Fax]',JSON['stringify'](_0x4ae7cc));return{'isValid':![],'message':_0x0f44('0x6b')};}if(_[_0x0f44('0x63')](_0x4ae7cc[_0x0f44('0x5a')])){ut['logger'](_0x0f44('0x6c'),JSON['stringify'](_0x4ae7cc));return{'isValid':![],'message':_0x0f44('0x6d')};}return{'isValid':!![],'message':_0x0f44('0x6e')};};Fax[_0x0f44('0x1f')][_0x0f44('0x6f')]=function(_0x51b7aa,_0x460b3d=0x2710){if(this[_0x0f44('0xc')][_0x51b7aa]){setTimeout(()=>{delete this[_0x0f44('0xc')][_0x51b7aa];},_0x460b3d);}};module[_0x0f44('0x70')]=Fax;