c34c3d749f7506235f8f6f92e08842dbdce527a8
[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 _0xf243=['actions','merge','pick','response','AttachmentId','update','logger','[Fax][Message][Error][Updated]','[Fax][Message][Error][NoUpdate1]','[Fax][Message][Error][NoUpdate2]','updateStatus','[Fax][Message][Updated]','finally','[Fax][Message][NoUpdate2]','\x20hangup\x20sendFile\x20','\x20hangup\x20faxRiceive\x20','SUCCESS','faxstatusstring','SENDING\x20FAILED:\x20','SENDING\x20FAILED','execOriginate','FaxAccountId','[Fax][execOriginate][success]','Fax\x20received','[Fax][execOriginate][failure]','Fax\x20failed','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','XMF-PATH','variablesAction','Action','addVariables','ext','isTrunkReachable','isNil','active','SIP/%s','reachable','[Fax][checkTrunk][Trunk\x20not\x20working\x20correctly]','\x20Trunk\x20is\x20not\x20active','Trunk\x20not\x20working\x20correctly','Empty\x20Fax','[Fax][checkTrunk][Empty\x20path]','Empty\x20path','Is\x20Valid','exports','util','lodash','bluebird','uuid','moment','path','tiff2pdf','./util/gs','../../../config/logger','fax','../ami','./rpcDb','./channel','./util/util','../rpc/faxAccount','../rpc/attachment','../rpc/faxMessage','channels','faxAccounts','trunks','syncNewchannel','bind','syncVarSet','newstate','syncNewState','newexten','faxstatus','syncFaxStatus','receivefax','syncReceiveFax','hangup','syncHangup','syncOriginateResponse','from-voip-provider','exten','localstationid','uniqueid','channel','name','info','hasOwnProperty','value','channelstate','channelstatedesc','prototype','syncNewExten','variable','status','filename','\x20syncFaxStatus\x20','remotestationid','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','message','create','parse','application/pdf','then','notify','.pdf','error','\x20notify\x20','catch','\x20receiveFax\x20','stringify'];(function(_0x516ac4,_0x8b2375){var _0x35c675=function(_0x3c0972){while(--_0x3c0972){_0x516ac4['push'](_0x516ac4['shift']());}};_0x35c675(++_0x8b2375);}(_0xf243,0x121));var _0x3f24=function(_0x571c8e,_0x18546d){_0x571c8e=_0x571c8e-0x0;var _0x1fe343=_0xf243[_0x571c8e];return _0x1fe343;};'use strict';var util=require(_0x3f24('0x0'));var _=require(_0x3f24('0x1'));var BPromise=require(_0x3f24('0x2'));var uuid=require(_0x3f24('0x3'));var moment=require(_0x3f24('0x4'));var path=require(_0x3f24('0x5'));var tiff2pdf=require(_0x3f24('0x6'));var gs=require(_0x3f24('0x7'));var logger=require(_0x3f24('0x8'))(_0x3f24('0x9'));var ami=require(_0x3f24('0xa'));var rpcDb=require(_0x3f24('0xb'));var Channel=require(_0x3f24('0xc'));var ut=require(_0x3f24('0xd'));var Action=require('./action');var faxAccount=require(_0x3f24('0xe'));var attachment=require(_0x3f24('0xf'));var faxMessage=require(_0x3f24('0x10'));function Fax(_0x5cc4bd){this[_0x3f24('0x11')]={};this['faxAccounts']=_0x5cc4bd[_0x3f24('0x12')];this['trunks']=_0x5cc4bd[_0x3f24('0x13')];this['actions']={};ami['on']('newchannel',this[_0x3f24('0x14')][_0x3f24('0x15')](this));ami['on']('varset',this[_0x3f24('0x16')]['bind'](this));ami['on'](_0x3f24('0x17'),this[_0x3f24('0x18')][_0x3f24('0x15')](this));ami['on'](_0x3f24('0x19'),this['syncNewExten'][_0x3f24('0x15')](this));ami['on'](_0x3f24('0x1a'),this[_0x3f24('0x1b')][_0x3f24('0x15')](this));ami['on'](_0x3f24('0x1c'),this[_0x3f24('0x1d')][_0x3f24('0x15')](this));ami['on'](_0x3f24('0x1e'),this[_0x3f24('0x1f')][_0x3f24('0x15')](this));ami['on']('originateresponse',this[_0x3f24('0x20')][_0x3f24('0x15')](this));}Fax['prototype'][_0x3f24('0x14')]=function(_0x23a887){if(_0x23a887['context']===_0x3f24('0x21')){for(var _0xd91c33 in this[_0x3f24('0x12')]){if(this[_0x3f24('0x12')]['hasOwnProperty'](_0xd91c33)){if(_0x23a887[_0x3f24('0x22')]===this[_0x3f24('0x12')][_0xd91c33][_0x3f24('0x23')]){this[_0x3f24('0x11')][_0x23a887[_0x3f24('0x24')]]={};this[_0x3f24('0x11')][_0x23a887['uniqueid']][_0x3f24('0x25')]=_0x23a887[_0x3f24('0x25')];this[_0x3f24('0x11')][_0x23a887['uniqueid']]['id']=this[_0x3f24('0x12')][_0xd91c33]['id'];this[_0x3f24('0x11')][_0x23a887['uniqueid']][_0x3f24('0x26')]=this['faxAccounts'][_0xd91c33][_0x3f24('0x26')];logger[_0x3f24('0x27')](_0x23a887[_0x3f24('0x24')],'\x20syncNewchannel\x20');}}}}if(this['actions'][_0x23a887[_0x3f24('0x24')]]){this['channels'][_0x23a887[_0x3f24('0x24')]]={};this['channels'][_0x23a887[_0x3f24('0x24')]]['channel']=_0x23a887['channel'];logger[_0x3f24('0x27')](_0x23a887[_0x3f24('0x24')],'\x20syncNewchannel\x20');}};Fax['prototype'][_0x3f24('0x16')]=function(_0x651432){if(this[_0x3f24('0x11')][_0x651432[_0x3f24('0x24')]]){for(var _0x4b0dbd in _0x651432['variable']){if(_0x651432['variable'][_0x3f24('0x28')](_0x4b0dbd)){this[_0x3f24('0x11')][_0x651432[_0x3f24('0x24')]][_0x4b0dbd]=_0x651432[_0x3f24('0x29')];}}}};Fax['prototype'][_0x3f24('0x18')]=function(_0x2a9a29){if(this[_0x3f24('0x11')][_0x2a9a29[_0x3f24('0x24')]]){this[_0x3f24('0x11')][_0x2a9a29[_0x3f24('0x24')]]['channelstate']=_0x2a9a29[_0x3f24('0x2a')];this[_0x3f24('0x11')][_0x2a9a29[_0x3f24('0x24')]][_0x3f24('0x2b')]=_0x2a9a29[_0x3f24('0x2b')];}};Fax[_0x3f24('0x2c')][_0x3f24('0x2d')]=function(_0x4f2dc1){if(this[_0x3f24('0x11')][_0x4f2dc1[_0x3f24('0x24')]]){for(var _0x47b235 in _0x4f2dc1['variable']){if(_0x4f2dc1[_0x3f24('0x2e')][_0x3f24('0x28')](_0x47b235)){this[_0x3f24('0x11')][_0x4f2dc1[_0x3f24('0x24')]][_0x47b235]=_0x4f2dc1[_0x3f24('0x29')];}}}};Fax[_0x3f24('0x2c')]['syncFaxStatus']=function(_0x41eb44){if(this[_0x3f24('0x11')][_0x41eb44['uniqueid']]){this[_0x3f24('0x11')][_0x41eb44[_0x3f24('0x24')]]['operation']=_0x41eb44['operation'];this['channels'][_0x41eb44[_0x3f24('0x24')]][_0x3f24('0x2f')]=_0x41eb44[_0x3f24('0x2f')];this[_0x3f24('0x11')][_0x41eb44[_0x3f24('0x24')]][_0x3f24('0x23')]=_0x41eb44[_0x3f24('0x23')];this[_0x3f24('0x11')][_0x41eb44['uniqueid']][_0x3f24('0x30')]=_0x41eb44[_0x3f24('0x30')];logger[_0x3f24('0x27')](_0x41eb44[_0x3f24('0x24')],_0x3f24('0x31'));}};Fax[_0x3f24('0x2c')][_0x3f24('0x1d')]=function(_0x269edc){var _0x738172=this;if(this['channels'][_0x269edc[_0x3f24('0x24')]]){var _0x236ab9=this[_0x3f24('0x11')][_0x269edc[_0x3f24('0x24')]]['id'];this['channels'][_0x269edc[_0x3f24('0x24')]][_0x3f24('0x32')]=_0x269edc[_0x3f24('0x32')];this[_0x3f24('0x11')][_0x269edc[_0x3f24('0x24')]][_0x3f24('0x33')]=_0x269edc[_0x3f24('0x33')];this['channels'][_0x269edc[_0x3f24('0x24')]][_0x3f24('0x23')]=_0x269edc[_0x3f24('0x23')];this['channels'][_0x269edc[_0x3f24('0x24')]][_0x3f24('0x34')]=_0x269edc[_0x3f24('0x34')];this[_0x3f24('0x11')][_0x269edc[_0x3f24('0x24')]]['transferrate']=_0x269edc[_0x3f24('0x35')];this['channels'][_0x269edc['uniqueid']][_0x3f24('0x30')]=_0x269edc[_0x3f24('0x30')];this['channels'][_0x269edc[_0x3f24('0x24')]][_0x3f24('0x36')]=_0x269edc['calleridnum'];if(this[_0x3f24('0x11')][_0x269edc[_0x3f24('0x24')]][_0x3f24('0x33')]>0x0){tiff2pdf(_0x269edc[_0x3f24('0x30')],_0x3f24('0x37'),function(_0x5c311b){logger[_0x3f24('0x27')](_0x269edc[_0x3f24('0x24')],'\x20result\x20conversion\x20file\x20',_0x5c311b[_0x3f24('0x38')]);attachment[_0x3f24('0x39')]({'name':path[_0x3f24('0x3a')](_0x269edc[_0x3f24('0x30')])[_0x3f24('0x26')],'basename':path['parse'](_0x269edc[_0x3f24('0x30')])[_0x3f24('0x26')]+'.pdf','type':_0x3f24('0x3b')})[_0x3f24('0x3c')](function(_0x5a8097){faxAccount[_0x3f24('0x3d')](_0x236ab9,{'from':_0x269edc['calleridnum'],'mapKey':_0x3f24('0x9'),'body':path[_0x3f24('0x3a')](_0x269edc[_0x3f24('0x30')])[_0x3f24('0x26')]+_0x3f24('0x3e'),'AttachmentId':_0x5a8097['id']})['catch'](function(_0xf9aab5){logger[_0x3f24('0x3f')](_0x269edc[_0x3f24('0x24')],_0x3f24('0x40'),JSON['stringify'](_0xf9aab5));});})[_0x3f24('0x41')](function(_0x2eb548){logger['error'](_0x269edc[_0x3f24('0x24')],_0x3f24('0x42'),JSON[_0x3f24('0x43')](_0x2eb548));});});}logger[_0x3f24('0x27')](_0x269edc[_0x3f24('0x24')],_0x3f24('0x42'));}};Fax['prototype'][_0x3f24('0x20')]=function(_0x1d94d4){if(this[_0x3f24('0x44')][_0x1d94d4[_0x3f24('0x24')]]){_[_0x3f24('0x45')](this[_0x3f24('0x11')][_0x1d94d4[_0x3f24('0x24')]],_[_0x3f24('0x46')](_0x1d94d4,[_0x3f24('0x47'),'reason']));}};function updateStatus(_0x4059ee){if(_0x4059ee[_0x3f24('0x48')]){faxMessage[_0x3f24('0x49')](_0x4059ee[_0x3f24('0x48')],{'failMessage':'SENDING\x20FAILED\x20'})['then'](function(_0x314911){ut[_0x3f24('0x4a')](_0x3f24('0x4b'),_0x314911);})[_0x3f24('0x41')](function(_0x50eaea){ut[_0x3f24('0x4a')](_0x3f24('0x4c'),_0x50eaea);});}else{ut[_0x3f24('0x4a')](_0x3f24('0x4d'));}}Fax[_0x3f24('0x2c')][_0x3f24('0x4e')]=function(_0x4cef35,_0x12c3ee,_0x42fbed){var _0x392095=this;var _0x3cd6b4={};if(_0x12c3ee){_0x3cd6b4={'read':_0x12c3ee};}else{_0x3cd6b4={'failMessage':_0x42fbed};}if(this['actions'][_0x4cef35][_0x3f24('0x48')]){faxMessage[_0x3f24('0x49')](this[_0x3f24('0x44')][_0x4cef35]['AttachmentId'],_0x3cd6b4)[_0x3f24('0x3c')](function(_0x8a3a7d){ut[_0x3f24('0x4a')](_0x3f24('0x4f'),_0x8a3a7d);})[_0x3f24('0x41')](function(_0x10ed81){ut[_0x3f24('0x4a')]('[Fax][Message][NoUpdate1]',_0x10ed81);})[_0x3f24('0x50')](function(){delete _0x392095[_0x3f24('0x11')][_0x4cef35];delete _0x392095['actions'][_0x4cef35];});}else{ut[_0x3f24('0x4a')](_0x3f24('0x51'));delete this[_0x3f24('0x11')][_0x4cef35];delete this['actions'][_0x4cef35];}};Fax['prototype'][_0x3f24('0x1f')]=function(_0x189840){var _0x15362c=this;if(this[_0x3f24('0x11')][_0x189840[_0x3f24('0x24')]]){if(this[_0x3f24('0x44')][_0x189840[_0x3f24('0x24')]]){this[_0x3f24('0x11')][_0x189840[_0x3f24('0x24')]]['id']=this[_0x3f24('0x44')][_0x189840[_0x3f24('0x24')]]['id'];this[_0x3f24('0x11')][_0x189840[_0x3f24('0x24')]]['name']=this['actions'][_0x189840[_0x3f24('0x24')]]['name'];}logger['info'](_0x189840[_0x3f24('0x24')],this[_0x3f24('0x44')][_0x189840[_0x3f24('0x24')]]?_0x3f24('0x52'):_0x3f24('0x53'),JSON[_0x3f24('0x43')](this['channels'][_0x189840[_0x3f24('0x24')]]));if(this[_0x3f24('0x44')][_0x189840['uniqueid']]){if(this['channels'][_0x189840[_0x3f24('0x24')]]['faxpages']>0x0&&this[_0x3f24('0x11')][_0x189840[_0x3f24('0x24')]][_0x3f24('0x1a')]===_0x3f24('0x54')){this[_0x3f24('0x4e')](_0x189840[_0x3f24('0x24')],!![]);}else{this['updateStatus'](_0x189840[_0x3f24('0x24')],![],this['channels'][_0x189840['uniqueid']][_0x3f24('0x55')]?_0x3f24('0x56')+this['channels'][_0x189840[_0x3f24('0x24')]][_0x3f24('0x55')]:_0x3f24('0x57'));}}}};Fax[_0x3f24('0x2c')]['send']=function(_0x2eac5c){var _0x208b57=this;return new BPromise(function(_0x1892c8,_0x1ebffa){_0x208b57[_0x3f24('0x58')](_0x2eac5c,_0x208b57[_0x3f24('0x12')][_0x2eac5c[_0x3f24('0x59')]])['then'](function(_0x491b68){ut[_0x3f24('0x4a')](_0x3f24('0x5a'),_0x491b68);_0x1892c8(ut['message'](_0x3f24('0x5b')));})[_0x3f24('0x41')](function(_0x583f6d){ut[_0x3f24('0x4a')](_0x3f24('0x5c'),_0x583f6d);updateStatus(_0x2eac5c);_0x1ebffa(ut[_0x3f24('0x38')](_0x3f24('0x5d')));});});};Fax['prototype']['checkAfterSend']=function(_0x25ac9d){var _0x278932=this;return new BPromise(function(_0x23b6b5,_0xb6c3fc){var _0x442bc5=_0x278932[_0x3f24('0x5e')](_0x278932[_0x3f24('0x12')][_0x25ac9d[_0x3f24('0x59')]][_0x3f24('0x5f')],_0x25ac9d);if(_0x442bc5[_0x3f24('0x60')]){_0x23b6b5(ut['message'](_0x3f24('0x61')));}else{_0xb6c3fc(ut[_0x3f24('0x38')](_0x442bc5[_0x3f24('0x38')]));}});};function originate(_0x37e578,_0xd05d2a,_0xfd5937){return new BPromise(function(_0x1d829c,_0x272e77){if(_0xfd5937){var _0x4f9f42=util[_0x3f24('0x62')](_0x3f24('0x63'),path[_0x3f24('0x3a')](_0x37e578[_0x3f24('0x5')])[_0x3f24('0x26')],_0x37e578['path']);gs[_0x3f24('0x64')](_0x4f9f42)[_0x3f24('0x3c')](function(){_0x37e578[_0x3f24('0x5')]=util[_0x3f24('0x62')]('%s/%s.tif',path['parse'](_0x37e578[_0x3f24('0x5')])['dir'],path[_0x3f24('0x3a')](_0x37e578[_0x3f24('0x5')])[_0x3f24('0x26')]);_0xd05d2a[_0x3f24('0x65')]=_0x37e578[_0x3f24('0x5')];ut['logger']('Convert\x20pdf\x20to\x20tif','');ami['Action'](ut[_0x3f24('0x66')](_0x37e578,_0xd05d2a))['then'](function(_0x2d94d8){_0x1d829c(_0x2d94d8);})['catch'](function(_0x4cefb8){_0x272e77(_0x4cefb8);});})[_0x3f24('0x41')](function(_0x1bbd6a){_0x272e77(_0x1bbd6a);});}else{ami[_0x3f24('0x67')](ut['variablesAction'](_0x37e578,_0xd05d2a))[_0x3f24('0x3c')](function(_0x39ecc0){_0x1d829c(_0x39ecc0);})[_0x3f24('0x41')](function(_0x4c004a){_0x272e77(_0x4c004a);});}});}Fax[_0x3f24('0x2c')][_0x3f24('0x58')]=function(_0x2fce16,_0x21cb3d){var _0x1c1b27=this;return new BPromise(function(_0xe903a2,_0x21ecd9){var _0x2a04a1=uuid['v4']();var _0xc806c5=ut[_0x3f24('0x68')](_0x2fce16,_0x21cb3d);_0x1c1b27[_0x3f24('0x44')][_0x2a04a1]=new Action(_0x2a04a1,_0x21cb3d,_0x2fce16);var _0x5d6946=_0x1c1b27[_0x3f24('0x44')][_0x2a04a1];if(path[_0x3f24('0x3a')](_0x2fce16[_0x3f24('0x5')])[_0x3f24('0x69')]===_0x3f24('0x3e')){originate(_0x5d6946,_0xc806c5,!![])['then'](function(_0x11dff8){_0xe903a2(_0x11dff8);})[_0x3f24('0x41')](function(_0x4ed7e5){_0x21ecd9(_0x4ed7e5);});}else{originate(_0x5d6946,_0xc806c5,![])[_0x3f24('0x3c')](function(_0x38ca1c){_0xe903a2(_0x38ca1c);})[_0x3f24('0x41')](function(_0x50712a){_0x21ecd9(_0x50712a);});}});};Fax[_0x3f24('0x2c')][_0x3f24('0x6a')]=function(_0x51e08d){return!_[_0x3f24('0x6b')](_0x51e08d)&&!_[_0x3f24('0x6b')](_0x51e08d[_0x3f24('0x26')])&&!_['isNil'](_0x51e08d[_0x3f24('0x6c')])&&_0x51e08d[_0x3f24('0x6c')]&&!_['isNil'](this[_0x3f24('0x13')][util[_0x3f24('0x62')]('SIP/%s',_0x51e08d[_0x3f24('0x26')])])&&!_['isNil'](this[_0x3f24('0x13')][util['format'](_0x3f24('0x6d'),_0x51e08d[_0x3f24('0x26')])]['status'])&&(this['trunks'][util[_0x3f24('0x62')](_0x3f24('0x6d'),_0x51e08d[_0x3f24('0x26')])][_0x3f24('0x2f')]===_0x3f24('0x6e')||this[_0x3f24('0x13')][util['format'](_0x3f24('0x6d'),_0x51e08d[_0x3f24('0x26')])]['status']==='unmonitored');};Fax[_0x3f24('0x2c')][_0x3f24('0x5e')]=function(_0x6f593b,_0x519ead){if(!this[_0x3f24('0x6a')](_0x6f593b)){ut[_0x3f24('0x4a')](_0x3f24('0x6f'),_[_0x3f24('0x6b')](_0x6f593b)?'':_0x6f593b['name']+_0x3f24('0x70'));return{'isValid':![],'message':_0x3f24('0x71')};}if(_[_0x3f24('0x6b')](_0x519ead['fax'])){ut[_0x3f24('0x4a')]('[Fax][checkTrunk][Empty\x20Fax]',JSON[_0x3f24('0x43')](_0x519ead));return{'isValid':![],'message':_0x3f24('0x72')};}if(_[_0x3f24('0x6b')](_0x519ead[_0x3f24('0x5')])){ut[_0x3f24('0x4a')](_0x3f24('0x73'),JSON['stringify'](_0x519ead));return{'isValid':![],'message':_0x3f24('0x74')};}return{'isValid':!![],'message':_0x3f24('0x75')};};module[_0x3f24('0x76')]=Fax;