f59be5f723c4b988046ce166c34a2c6908943cab
[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 _0x905a=['checkTrunk','FaxAccountId','Trunk','isValid','Trunk\x20Reachable','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','format','dir','XMF-PATH','Convert\x20pdf\x20to\x20tif','Action','variablesAction','addVariables','ext','isTrunkReachable','isNil','active','SIP/%s','reachable','unmonitored','[Fax][checkTrunk][Trunk\x20not\x20work]','\x20Trunk\x20is\x20not\x20active','Trunk\x20not\x20working','[Fax][checkTrunk][Empty\x20Fax]','[Fax][checkTrunk][Empty\x20path]','Empty\x20path','Is\x20Valid','util','lodash','bluebird','moment','path','tiff2pdf','../../../config/logger','fax','../ami','./rpcDb','./channel','./util/util','./action','../rpc/attachment','../rpc/faxMessage','channels','faxAccounts','trunks','actions','newchannel','syncNewchannel','varset','syncVarSet','bind','newstate','syncNewState','newexten','syncNewExten','faxstatus','receivefax','syncReceiveFax','hangup','syncHangup','originateresponse','syncOriginateResponse','from-voip-provider','hasOwnProperty','exten','localstationid','uniqueid','channel','name','\x20syncNewchannel\x20','prototype','variable','value','channelstate','channelstatedesc','syncFaxStatus','operation','status','filename','info','\x20syncFaxStatus\x20','remotestationid','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','\x20result\x20conversion\x20file\x20','message','create','.pdf','application/pdf','then','notify','parse','catch','error','\x20notify\x20','stringify','\x20receiveFax\x20','merge','reason','AttachmentId','SENDING\x20FAILED\x20','logger','[Fax][Message][Error][Updated]','[Fax][Message][Error][NoUpdate1]','updateStatus','update','[Fax][Message][Updated]','[Fax][Message][NoUpdate1]','finally','[Fax][Message][NoUpdate2]','\x20hangup\x20sendFile\x20','\x20hangup\x20faxRiceive\x20','faxpages','SUCCESS','faxstatusstring','SENDING\x20FAILED:\x20','execOriginate','[Fax][execOriginate][success]','[Fax][execOriginate][failure]','Fax\x20failed'];(function(_0x1ac966,_0x1d914d){var _0x47e9ff=function(_0x49f17c){while(--_0x49f17c){_0x1ac966['push'](_0x1ac966['shift']());}};_0x47e9ff(++_0x1d914d);}(_0x905a,0x190));var _0xa905=function(_0xd2e406,_0x16caa8){_0xd2e406=_0xd2e406-0x0;var _0x23e4f2=_0x905a[_0xd2e406];return _0x23e4f2;};'use strict';var util=require(_0xa905('0x0'));var _=require(_0xa905('0x1'));var BPromise=require(_0xa905('0x2'));var uuid=require('uuid');var moment=require(_0xa905('0x3'));var path=require(_0xa905('0x4'));var tiff2pdf=require(_0xa905('0x5'));var gs=require('./util/gs');var logger=require(_0xa905('0x6'))(_0xa905('0x7'));var ami=require(_0xa905('0x8'));var rpcDb=require(_0xa905('0x9'));var Channel=require(_0xa905('0xa'));var ut=require(_0xa905('0xb'));var Action=require(_0xa905('0xc'));var faxAccount=require('../rpc/faxAccount');var attachment=require(_0xa905('0xd'));var faxMessage=require(_0xa905('0xe'));function Fax(_0x16dc0a){this[_0xa905('0xf')]={};this['faxAccounts']=_0x16dc0a[_0xa905('0x10')];this['trunks']=_0x16dc0a[_0xa905('0x11')];this[_0xa905('0x12')]={};ami['on'](_0xa905('0x13'),this[_0xa905('0x14')]['bind'](this));ami['on'](_0xa905('0x15'),this[_0xa905('0x16')][_0xa905('0x17')](this));ami['on'](_0xa905('0x18'),this[_0xa905('0x19')][_0xa905('0x17')](this));ami['on'](_0xa905('0x1a'),this[_0xa905('0x1b')][_0xa905('0x17')](this));ami['on'](_0xa905('0x1c'),this['syncFaxStatus']['bind'](this));ami['on'](_0xa905('0x1d'),this[_0xa905('0x1e')][_0xa905('0x17')](this));ami['on'](_0xa905('0x1f'),this[_0xa905('0x20')]['bind'](this));ami['on'](_0xa905('0x21'),this[_0xa905('0x22')][_0xa905('0x17')](this));}Fax['prototype'][_0xa905('0x14')]=function(_0x545e0d){if(_0x545e0d['context']===_0xa905('0x23')){for(var _0x3b868f in this[_0xa905('0x10')]){if(this[_0xa905('0x10')][_0xa905('0x24')](_0x3b868f)){if(_0x545e0d[_0xa905('0x25')]===this['faxAccounts'][_0x3b868f][_0xa905('0x26')]){this[_0xa905('0xf')][_0x545e0d['uniqueid']]={};this[_0xa905('0xf')][_0x545e0d[_0xa905('0x27')]][_0xa905('0x28')]=_0x545e0d[_0xa905('0x28')];this[_0xa905('0xf')][_0x545e0d[_0xa905('0x27')]]['id']=this[_0xa905('0x10')][_0x3b868f]['id'];this['channels'][_0x545e0d[_0xa905('0x27')]]['name']=this[_0xa905('0x10')][_0x3b868f][_0xa905('0x29')];logger['info'](_0x545e0d[_0xa905('0x27')],'\x20syncNewchannel\x20');}}}}if(this['actions'][_0x545e0d[_0xa905('0x27')]]){this['channels'][_0x545e0d[_0xa905('0x27')]]={};this[_0xa905('0xf')][_0x545e0d[_0xa905('0x27')]][_0xa905('0x28')]=_0x545e0d['channel'];logger['info'](_0x545e0d[_0xa905('0x27')],_0xa905('0x2a'));}};Fax[_0xa905('0x2b')]['syncVarSet']=function(_0x1df4ab){if(this['channels'][_0x1df4ab[_0xa905('0x27')]]){for(var _0x5dc2a1 in _0x1df4ab['variable']){if(_0x1df4ab[_0xa905('0x2c')]['hasOwnProperty'](_0x5dc2a1)){this['channels'][_0x1df4ab[_0xa905('0x27')]][_0x5dc2a1]=_0x1df4ab[_0xa905('0x2d')];}}}};Fax[_0xa905('0x2b')][_0xa905('0x19')]=function(_0x56dfee){if(this[_0xa905('0xf')][_0x56dfee[_0xa905('0x27')]]){this['channels'][_0x56dfee[_0xa905('0x27')]][_0xa905('0x2e')]=_0x56dfee[_0xa905('0x2e')];this[_0xa905('0xf')][_0x56dfee[_0xa905('0x27')]][_0xa905('0x2f')]=_0x56dfee[_0xa905('0x2f')];}};Fax['prototype'][_0xa905('0x1b')]=function(_0x3d0c17){if(this[_0xa905('0xf')][_0x3d0c17[_0xa905('0x27')]]){for(var _0x550658 in _0x3d0c17['variable']){if(_0x3d0c17[_0xa905('0x2c')]['hasOwnProperty'](_0x550658)){this[_0xa905('0xf')][_0x3d0c17[_0xa905('0x27')]][_0x550658]=_0x3d0c17[_0xa905('0x2d')];}}}};Fax[_0xa905('0x2b')][_0xa905('0x30')]=function(_0x32cfb7){if(this[_0xa905('0xf')][_0x32cfb7['uniqueid']]){this[_0xa905('0xf')][_0x32cfb7[_0xa905('0x27')]][_0xa905('0x31')]=_0x32cfb7[_0xa905('0x31')];this[_0xa905('0xf')][_0x32cfb7[_0xa905('0x27')]]['status']=_0x32cfb7[_0xa905('0x32')];this['channels'][_0x32cfb7[_0xa905('0x27')]][_0xa905('0x26')]=_0x32cfb7[_0xa905('0x26')];this[_0xa905('0xf')][_0x32cfb7[_0xa905('0x27')]][_0xa905('0x33')]=_0x32cfb7['filename'];logger[_0xa905('0x34')](_0x32cfb7[_0xa905('0x27')],_0xa905('0x35'));}};Fax[_0xa905('0x2b')][_0xa905('0x1e')]=function(_0x4d9f49){var _0x2cb4ce=this;if(this[_0xa905('0xf')][_0x4d9f49[_0xa905('0x27')]]){var _0x169b70=this['channels'][_0x4d9f49[_0xa905('0x27')]]['id'];this[_0xa905('0xf')][_0x4d9f49[_0xa905('0x27')]][_0xa905('0x36')]=_0x4d9f49[_0xa905('0x36')];this[_0xa905('0xf')][_0x4d9f49[_0xa905('0x27')]][_0xa905('0x37')]=_0x4d9f49[_0xa905('0x37')];this[_0xa905('0xf')][_0x4d9f49[_0xa905('0x27')]]['localstationid']=_0x4d9f49[_0xa905('0x26')];this[_0xa905('0xf')][_0x4d9f49[_0xa905('0x27')]][_0xa905('0x38')]=_0x4d9f49[_0xa905('0x38')];this[_0xa905('0xf')][_0x4d9f49[_0xa905('0x27')]][_0xa905('0x39')]=_0x4d9f49[_0xa905('0x39')];this[_0xa905('0xf')][_0x4d9f49[_0xa905('0x27')]][_0xa905('0x33')]=_0x4d9f49[_0xa905('0x33')];this['channels'][_0x4d9f49['uniqueid']][_0xa905('0x3a')]=_0x4d9f49['calleridnum'];if(this[_0xa905('0xf')][_0x4d9f49[_0xa905('0x27')]][_0xa905('0x37')]>0x0){tiff2pdf(_0x4d9f49[_0xa905('0x33')],_0xa905('0x3b'),function(_0x81f391){logger['info'](_0x4d9f49[_0xa905('0x27')],_0xa905('0x3c'),_0x81f391[_0xa905('0x3d')]);attachment[_0xa905('0x3e')]({'name':path['parse'](_0x4d9f49[_0xa905('0x33')])[_0xa905('0x29')],'basename':path['parse'](_0x4d9f49['filename'])[_0xa905('0x29')]+_0xa905('0x3f'),'type':_0xa905('0x40')})[_0xa905('0x41')](function(_0x4e08fb){faxAccount[_0xa905('0x42')](_0x169b70,{'from':_0x4d9f49[_0xa905('0x3a')],'mapKey':'fax','body':path[_0xa905('0x43')](_0x4d9f49[_0xa905('0x33')])['name']+_0xa905('0x3f'),'AttachmentId':_0x4e08fb['id']})[_0xa905('0x44')](function(_0x5714c7){logger[_0xa905('0x45')](_0x4d9f49[_0xa905('0x27')],_0xa905('0x46'),JSON[_0xa905('0x47')](_0x5714c7));});})[_0xa905('0x44')](function(_0xe5e8ea){logger[_0xa905('0x45')](_0x4d9f49[_0xa905('0x27')],_0xa905('0x48'),JSON[_0xa905('0x47')](_0xe5e8ea));});});}logger['info'](_0x4d9f49[_0xa905('0x27')],_0xa905('0x48'));}};Fax[_0xa905('0x2b')][_0xa905('0x22')]=function(_0x1157c1){if(this[_0xa905('0x12')][_0x1157c1['uniqueid']]){_[_0xa905('0x49')](this[_0xa905('0xf')][_0x1157c1[_0xa905('0x27')]],_['pick'](_0x1157c1,['response',_0xa905('0x4a')]));}};function updateStatus(_0x5d3f8b){if(_0x5d3f8b[_0xa905('0x4b')]){faxMessage['update'](_0x5d3f8b[_0xa905('0x4b')],{'failMessage':_0xa905('0x4c')})[_0xa905('0x41')](function(_0x236ffd){ut[_0xa905('0x4d')](_0xa905('0x4e'),_0x236ffd);})[_0xa905('0x44')](function(_0x51f698){ut[_0xa905('0x4d')](_0xa905('0x4f'),_0x51f698);});}else{ut[_0xa905('0x4d')]('[Fax][Message][Error][NoUpdate2]');}}Fax[_0xa905('0x2b')][_0xa905('0x50')]=function(_0x49fc82,_0x18cfae,_0x4f37bf){var _0xd489b8=this;var _0x1201f6={};if(_0x18cfae){_0x1201f6={'read':_0x18cfae};}else{_0x1201f6={'failMessage':_0x4f37bf};}if(this[_0xa905('0x12')][_0x49fc82][_0xa905('0x4b')]){faxMessage[_0xa905('0x51')](this[_0xa905('0x12')][_0x49fc82]['AttachmentId'],_0x1201f6)[_0xa905('0x41')](function(_0x501b5c){ut[_0xa905('0x4d')](_0xa905('0x52'),_0x501b5c);})[_0xa905('0x44')](function(_0x5a5961){ut[_0xa905('0x4d')](_0xa905('0x53'),_0x5a5961);})[_0xa905('0x54')](function(){delete _0xd489b8[_0xa905('0xf')][_0x49fc82];delete _0xd489b8[_0xa905('0x12')][_0x49fc82];});}else{ut[_0xa905('0x4d')](_0xa905('0x55'));delete this[_0xa905('0xf')][_0x49fc82];delete this[_0xa905('0x12')][_0x49fc82];}};Fax[_0xa905('0x2b')]['syncHangup']=function(_0x5dbe8e){var _0x311dca=this;if(this[_0xa905('0xf')][_0x5dbe8e[_0xa905('0x27')]]){if(this[_0xa905('0x12')][_0x5dbe8e[_0xa905('0x27')]]){this[_0xa905('0xf')][_0x5dbe8e[_0xa905('0x27')]]['id']=this[_0xa905('0x12')][_0x5dbe8e[_0xa905('0x27')]]['id'];this[_0xa905('0xf')][_0x5dbe8e[_0xa905('0x27')]]['name']=this[_0xa905('0x12')][_0x5dbe8e[_0xa905('0x27')]][_0xa905('0x29')];}logger['info'](_0x5dbe8e[_0xa905('0x27')],this[_0xa905('0x12')][_0x5dbe8e['uniqueid']]?_0xa905('0x56'):_0xa905('0x57'),JSON[_0xa905('0x47')](this['channels'][_0x5dbe8e[_0xa905('0x27')]]));if(this[_0xa905('0x12')][_0x5dbe8e['uniqueid']]){if(this['channels'][_0x5dbe8e[_0xa905('0x27')]][_0xa905('0x58')]>0x0&&this[_0xa905('0xf')][_0x5dbe8e['uniqueid']]['faxstatus']===_0xa905('0x59')){this[_0xa905('0x50')](_0x5dbe8e[_0xa905('0x27')],!![]);}else{this[_0xa905('0x50')](_0x5dbe8e['uniqueid'],![],this[_0xa905('0xf')][_0x5dbe8e['uniqueid']][_0xa905('0x5a')]?_0xa905('0x5b')+this['channels'][_0x5dbe8e[_0xa905('0x27')]][_0xa905('0x5a')]:'SENDING\x20FAILED');}}}};Fax[_0xa905('0x2b')]['send']=function(_0x1b6efe){var _0x151215=this;return new BPromise(function(_0x446f3e,_0xadef6){_0x151215[_0xa905('0x5c')](_0x1b6efe,_0x151215[_0xa905('0x10')][_0x1b6efe['FaxAccountId']])['then'](function(_0xd43536){ut[_0xa905('0x4d')](_0xa905('0x5d'),_0xd43536);_0x446f3e(ut[_0xa905('0x3d')]('Fax\x20received'));})[_0xa905('0x44')](function(_0x27d4a3){ut[_0xa905('0x4d')](_0xa905('0x5e'),_0x27d4a3);updateStatus(_0x1b6efe);_0xadef6(ut[_0xa905('0x3d')](_0xa905('0x5f')));});});};Fax[_0xa905('0x2b')]['checkAfterSend']=function(_0x58ebc2){var _0x33358c=this;return new BPromise(function(_0xddbcb9,_0x4c5686){var _0x57ac87=_0x33358c[_0xa905('0x60')](_0x33358c[_0xa905('0x10')][_0x58ebc2[_0xa905('0x61')]][_0xa905('0x62')],_0x58ebc2);if(_0x57ac87[_0xa905('0x63')]){_0xddbcb9(ut[_0xa905('0x3d')](_0xa905('0x64')));}else{_0x4c5686(ut[_0xa905('0x3d')](_0x57ac87[_0xa905('0x3d')]));}});};function originate(_0x4418a7,_0x591273,_0x4f7b72){return new BPromise(function(_0x450089,_0x3e4038){if(_0x4f7b72){var _0x54eec8=util['format'](_0xa905('0x65'),path[_0xa905('0x43')](_0x4418a7[_0xa905('0x4')])[_0xa905('0x29')],_0x4418a7[_0xa905('0x4')]);gs[_0xa905('0x66')](_0x54eec8)[_0xa905('0x41')](function(){_0x4418a7[_0xa905('0x4')]=util[_0xa905('0x67')]('%s/%s.tif',path[_0xa905('0x43')](_0x4418a7[_0xa905('0x4')])[_0xa905('0x68')],path['parse'](_0x4418a7[_0xa905('0x4')])[_0xa905('0x29')]);_0x591273[_0xa905('0x69')]=_0x4418a7[_0xa905('0x4')];ut['logger'](_0xa905('0x6a'),'');ami[_0xa905('0x6b')](ut[_0xa905('0x6c')](_0x4418a7,_0x591273))[_0xa905('0x41')](function(_0xf20348){_0x450089(_0xf20348);})[_0xa905('0x44')](function(_0x4a75ab){_0x3e4038(_0x4a75ab);});})[_0xa905('0x44')](function(_0x161097){_0x3e4038(_0x161097);});}else{ami[_0xa905('0x6b')](ut[_0xa905('0x6c')](_0x4418a7,_0x591273))[_0xa905('0x41')](function(_0x2b7d3c){_0x450089(_0x2b7d3c);})['catch'](function(_0x7bac3f){_0x3e4038(_0x7bac3f);});}});}Fax[_0xa905('0x2b')][_0xa905('0x5c')]=function(_0x473f04,_0x5cf289){var _0x588ef9=this;return new BPromise(function(_0x5d735e,_0x1b5a08){var _0x45461d=uuid['v4']();var _0x288283=ut[_0xa905('0x6d')](_0x473f04,_0x5cf289);_0x588ef9[_0xa905('0x12')][_0x45461d]=new Action(_0x45461d,_0x5cf289,_0x473f04);var _0x56e56f=_0x588ef9['actions'][_0x45461d];if(path['parse'](_0x473f04[_0xa905('0x4')])[_0xa905('0x6e')]===_0xa905('0x3f')){originate(_0x56e56f,_0x288283,!![])[_0xa905('0x41')](function(_0x14c509){_0x5d735e(_0x14c509);})[_0xa905('0x44')](function(_0x2cac78){_0x1b5a08(_0x2cac78);});}else{originate(_0x56e56f,_0x288283,![])[_0xa905('0x41')](function(_0x15edee){_0x5d735e(_0x15edee);})[_0xa905('0x44')](function(_0x192030){_0x1b5a08(_0x192030);});}});};Fax[_0xa905('0x2b')][_0xa905('0x6f')]=function(_0x2a0554){return!_[_0xa905('0x70')](_0x2a0554)&&!_[_0xa905('0x70')](_0x2a0554['name'])&&!_['isNil'](_0x2a0554[_0xa905('0x71')])&&_0x2a0554[_0xa905('0x71')]&&!_[_0xa905('0x70')](this['trunks'][util[_0xa905('0x67')](_0xa905('0x72'),_0x2a0554[_0xa905('0x29')])])&&!_[_0xa905('0x70')](this[_0xa905('0x11')][util[_0xa905('0x67')](_0xa905('0x72'),_0x2a0554[_0xa905('0x29')])]['status'])&&(this['trunks'][util[_0xa905('0x67')](_0xa905('0x72'),_0x2a0554[_0xa905('0x29')])]['status']===_0xa905('0x73')||this['trunks'][util[_0xa905('0x67')]('SIP/%s',_0x2a0554[_0xa905('0x29')])][_0xa905('0x32')]===_0xa905('0x74'));};Fax['prototype'][_0xa905('0x60')]=function(_0xd8caa5,_0x17ae26){if(!this['isTrunkReachable'](_0xd8caa5)){ut['logger'](_0xa905('0x75'),_[_0xa905('0x70')](_0xd8caa5)?'':_0xd8caa5[_0xa905('0x29')]+_0xa905('0x76'));return{'isValid':![],'message':_0xa905('0x77')};}if(_[_0xa905('0x70')](_0x17ae26[_0xa905('0x7')])){ut[_0xa905('0x4d')](_0xa905('0x78'),JSON[_0xa905('0x47')](_0x17ae26));return{'isValid':![],'message':'Empty\x20Fax'};}if(_[_0xa905('0x70')](_0x17ae26[_0xa905('0x4')])){ut['logger'](_0xa905('0x79'),JSON[_0xa905('0x47')](_0x17ae26));return{'isValid':![],'message':_0xa905('0x7a')};}return{'isValid':!![],'message':_0xa905('0x7b')};};module['exports']=Fax;