Built motion from commit (unavailable).|2.6.0
[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 _0x5ac3=['checkAfterSend','FaxAccountId','Trunk','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','path','execute','%s/%s.tif','XMF-PATH','Convert\x20pdf\x20to\x20tif','Action','variablesAction','addVariables','ext','isTrunkReachable','isNil','active','SIP/%s','unmonitored','\x20Trunk\x20is\x20not\x20active','Trunk\x20not\x20working','Empty\x20Fax','[Fax][checkTrunk][Empty\x20path]','Empty\x20path','exports','lodash','bluebird','./util/gs','../../../config/logger','fax','./rpcDb','./channel','./util/util','../rpc/faxAccount','../rpc/attachment','../rpc/faxMessage','channels','faxAccounts','trunks','actions','newchannel','syncNewchannel','varset','syncVarSet','bind','newstate','syncNewState','newexten','syncNewExten','faxstatus','syncFaxStatus','receivefax','syncReceiveFax','hangup','syncHangup','originateresponse','syncOriginateResponse','prototype','context','from-voip-provider','hasOwnProperty','exten','localstationid','uniqueid','channel','name','\x20syncNewchannel\x20','info','variable','channelstate','channelstatedesc','value','operation','status','filename','\x20syncFaxStatus\x20','pagestransferred','resolution','transferrate','calleridnum','/var/opt/motion2/server/files/attachments/','\x20result\x20conversion\x20file\x20','parse','.pdf','notify','catch','\x20notify\x20','stringify','error','\x20receiveFax\x20','merge','reason','AttachmentId','update','SENDING\x20FAILED\x20','then','[Fax][Message][Error][Updated]','logger','[Fax][Message][Error][NoUpdate2]','[Fax][Message][Updated]','finally','[Fax][Message][NoUpdate2]','\x20hangup\x20sendFile\x20','faxpages','SUCCESS','updateStatus','faxstatusstring','SENDING\x20FAILED','send','execOriginate','[Fax][execOriginate][success]','message','Fax\x20received','[Fax][execOriginate][failure]','Fax\x20failed'];(function(_0x1526e1,_0x5ed5a5){var _0x249a1e=function(_0x4a7c12){while(--_0x4a7c12){_0x1526e1['push'](_0x1526e1['shift']());}};_0x249a1e(++_0x5ed5a5);}(_0x5ac3,0x176));var _0x35ac=function(_0x58c601,_0x18c000){_0x58c601=_0x58c601-0x0;var _0x3c166b=_0x5ac3[_0x58c601];return _0x3c166b;};'use strict';var util=require('util');var _=require(_0x35ac('0x0'));var BPromise=require(_0x35ac('0x1'));var uuid=require('uuid');var moment=require('moment');var path=require('path');var tiff2pdf=require('tiff2pdf');var gs=require(_0x35ac('0x2'));var logger=require(_0x35ac('0x3'))(_0x35ac('0x4'));var ami=require('../ami');var rpcDb=require(_0x35ac('0x5'));var Channel=require(_0x35ac('0x6'));var ut=require(_0x35ac('0x7'));var Action=require('./action');var faxAccount=require(_0x35ac('0x8'));var attachment=require(_0x35ac('0x9'));var faxMessage=require(_0x35ac('0xa'));function Fax(_0x18c0b3){this[_0x35ac('0xb')]={};this[_0x35ac('0xc')]=_0x18c0b3[_0x35ac('0xc')];this[_0x35ac('0xd')]=_0x18c0b3[_0x35ac('0xd')];this[_0x35ac('0xe')]={};ami['on'](_0x35ac('0xf'),this[_0x35ac('0x10')]['bind'](this));ami['on'](_0x35ac('0x11'),this[_0x35ac('0x12')][_0x35ac('0x13')](this));ami['on'](_0x35ac('0x14'),this[_0x35ac('0x15')][_0x35ac('0x13')](this));ami['on'](_0x35ac('0x16'),this[_0x35ac('0x17')][_0x35ac('0x13')](this));ami['on'](_0x35ac('0x18'),this[_0x35ac('0x19')]['bind'](this));ami['on'](_0x35ac('0x1a'),this[_0x35ac('0x1b')][_0x35ac('0x13')](this));ami['on'](_0x35ac('0x1c'),this[_0x35ac('0x1d')][_0x35ac('0x13')](this));ami['on'](_0x35ac('0x1e'),this[_0x35ac('0x1f')]['bind'](this));}Fax[_0x35ac('0x20')][_0x35ac('0x10')]=function(_0x4a168a){if(_0x4a168a[_0x35ac('0x21')]===_0x35ac('0x22')){for(var _0x27c603 in this[_0x35ac('0xc')]){if(this[_0x35ac('0xc')][_0x35ac('0x23')](_0x27c603)){if(_0x4a168a[_0x35ac('0x24')]===this[_0x35ac('0xc')][_0x27c603][_0x35ac('0x25')]){this[_0x35ac('0xb')][_0x4a168a[_0x35ac('0x26')]]={};this[_0x35ac('0xb')][_0x4a168a[_0x35ac('0x26')]][_0x35ac('0x27')]=_0x4a168a[_0x35ac('0x27')];this[_0x35ac('0xb')][_0x4a168a[_0x35ac('0x26')]]['id']=this[_0x35ac('0xc')][_0x27c603]['id'];this[_0x35ac('0xb')][_0x4a168a['uniqueid']]['name']=this[_0x35ac('0xc')][_0x27c603][_0x35ac('0x28')];logger['info'](_0x4a168a[_0x35ac('0x26')],_0x35ac('0x29'));}}}}if(this[_0x35ac('0xe')][_0x4a168a[_0x35ac('0x26')]]){this[_0x35ac('0xb')][_0x4a168a[_0x35ac('0x26')]]={};this['channels'][_0x4a168a[_0x35ac('0x26')]][_0x35ac('0x27')]=_0x4a168a['channel'];logger[_0x35ac('0x2a')](_0x4a168a['uniqueid'],_0x35ac('0x29'));}};Fax[_0x35ac('0x20')][_0x35ac('0x12')]=function(_0x4672b3){if(this[_0x35ac('0xb')][_0x4672b3[_0x35ac('0x26')]]){for(var _0x56abe9 in _0x4672b3[_0x35ac('0x2b')]){if(_0x4672b3[_0x35ac('0x2b')][_0x35ac('0x23')](_0x56abe9)){this[_0x35ac('0xb')][_0x4672b3[_0x35ac('0x26')]][_0x56abe9]=_0x4672b3['value'];}}}};Fax[_0x35ac('0x20')]['syncNewState']=function(_0x2fd807){if(this[_0x35ac('0xb')][_0x2fd807[_0x35ac('0x26')]]){this[_0x35ac('0xb')][_0x2fd807[_0x35ac('0x26')]][_0x35ac('0x2c')]=_0x2fd807[_0x35ac('0x2c')];this[_0x35ac('0xb')][_0x2fd807[_0x35ac('0x26')]][_0x35ac('0x2d')]=_0x2fd807['channelstatedesc'];}};Fax[_0x35ac('0x20')]['syncNewExten']=function(_0x17e838){if(this[_0x35ac('0xb')][_0x17e838[_0x35ac('0x26')]]){for(var _0x5e243e in _0x17e838[_0x35ac('0x2b')]){if(_0x17e838[_0x35ac('0x2b')][_0x35ac('0x23')](_0x5e243e)){this['channels'][_0x17e838[_0x35ac('0x26')]][_0x5e243e]=_0x17e838[_0x35ac('0x2e')];}}}};Fax[_0x35ac('0x20')][_0x35ac('0x19')]=function(_0x246715){if(this[_0x35ac('0xb')][_0x246715[_0x35ac('0x26')]]){this[_0x35ac('0xb')][_0x246715['uniqueid']][_0x35ac('0x2f')]=_0x246715['operation'];this[_0x35ac('0xb')][_0x246715[_0x35ac('0x26')]][_0x35ac('0x30')]=_0x246715[_0x35ac('0x30')];this[_0x35ac('0xb')][_0x246715[_0x35ac('0x26')]][_0x35ac('0x25')]=_0x246715[_0x35ac('0x25')];this[_0x35ac('0xb')][_0x246715[_0x35ac('0x26')]][_0x35ac('0x31')]=_0x246715['filename'];logger[_0x35ac('0x2a')](_0x246715['uniqueid'],_0x35ac('0x32'));}};Fax[_0x35ac('0x20')][_0x35ac('0x1b')]=function(_0x4b48fe){var _0x5ab556=this;if(this['channels'][_0x4b48fe[_0x35ac('0x26')]]){var _0x26e165=this[_0x35ac('0xb')][_0x4b48fe['uniqueid']]['id'];this[_0x35ac('0xb')][_0x4b48fe[_0x35ac('0x26')]]['remotestationid']=_0x4b48fe['remotestationid'];this[_0x35ac('0xb')][_0x4b48fe[_0x35ac('0x26')]][_0x35ac('0x33')]=_0x4b48fe[_0x35ac('0x33')];this[_0x35ac('0xb')][_0x4b48fe[_0x35ac('0x26')]][_0x35ac('0x25')]=_0x4b48fe[_0x35ac('0x25')];this[_0x35ac('0xb')][_0x4b48fe[_0x35ac('0x26')]][_0x35ac('0x34')]=_0x4b48fe[_0x35ac('0x34')];this[_0x35ac('0xb')][_0x4b48fe[_0x35ac('0x26')]]['transferrate']=_0x4b48fe[_0x35ac('0x35')];this['channels'][_0x4b48fe[_0x35ac('0x26')]]['filename']=_0x4b48fe[_0x35ac('0x31')];this[_0x35ac('0xb')][_0x4b48fe[_0x35ac('0x26')]][_0x35ac('0x36')]=_0x4b48fe[_0x35ac('0x36')];if(this[_0x35ac('0xb')][_0x4b48fe[_0x35ac('0x26')]][_0x35ac('0x33')]>0x0){tiff2pdf(_0x4b48fe[_0x35ac('0x31')],_0x35ac('0x37'),function(_0x586f15){logger[_0x35ac('0x2a')](_0x4b48fe[_0x35ac('0x26')],_0x35ac('0x38'),_0x586f15['message']);attachment['create']({'name':path[_0x35ac('0x39')](_0x4b48fe[_0x35ac('0x31')])[_0x35ac('0x28')],'basename':path[_0x35ac('0x39')](_0x4b48fe[_0x35ac('0x31')])['name']+_0x35ac('0x3a'),'type':'application/pdf'})['then'](function(_0x29231a){faxAccount[_0x35ac('0x3b')](_0x26e165,{'from':_0x4b48fe[_0x35ac('0x36')],'mapKey':_0x35ac('0x4'),'body':path['parse'](_0x4b48fe['filename'])['name']+'.pdf','AttachmentId':_0x29231a['id']})[_0x35ac('0x3c')](function(_0x503b65){logger['error'](_0x4b48fe[_0x35ac('0x26')],_0x35ac('0x3d'),JSON[_0x35ac('0x3e')](_0x503b65));});})['catch'](function(_0x3fa647){logger[_0x35ac('0x3f')](_0x4b48fe[_0x35ac('0x26')],_0x35ac('0x40'),JSON[_0x35ac('0x3e')](_0x3fa647));});});}logger['info'](_0x4b48fe[_0x35ac('0x26')],'\x20receiveFax\x20');}};Fax[_0x35ac('0x20')]['syncOriginateResponse']=function(_0x5b3c6e){if(this[_0x35ac('0xe')][_0x5b3c6e['uniqueid']]){_[_0x35ac('0x41')](this[_0x35ac('0xb')][_0x5b3c6e[_0x35ac('0x26')]],_['pick'](_0x5b3c6e,['response',_0x35ac('0x42')]));}};function updateStatus(_0x4a27c6){if(_0x4a27c6[_0x35ac('0x43')]){faxMessage[_0x35ac('0x44')](_0x4a27c6[_0x35ac('0x43')],{'failMessage':_0x35ac('0x45')})[_0x35ac('0x46')](function(_0x518e9d){ut['logger'](_0x35ac('0x47'),_0x518e9d);})[_0x35ac('0x3c')](function(_0x104bed){ut['logger']('[Fax][Message][Error][NoUpdate1]',_0x104bed);});}else{ut[_0x35ac('0x48')](_0x35ac('0x49'));}}Fax[_0x35ac('0x20')]['updateStatus']=function(_0x3da02f,_0x48ee1c,_0x51232e){var _0x15d773=this;var _0x32db86={};if(_0x48ee1c){_0x32db86={'read':_0x48ee1c};}else{_0x32db86={'failMessage':_0x51232e};}if(this[_0x35ac('0xe')][_0x3da02f][_0x35ac('0x43')]){faxMessage[_0x35ac('0x44')](this[_0x35ac('0xe')][_0x3da02f][_0x35ac('0x43')],_0x32db86)['then'](function(_0x136c54){ut[_0x35ac('0x48')](_0x35ac('0x4a'),_0x136c54);})[_0x35ac('0x3c')](function(_0x319611){ut[_0x35ac('0x48')]('[Fax][Message][NoUpdate1]',_0x319611);})[_0x35ac('0x4b')](function(){delete _0x15d773['channels'][_0x3da02f];delete _0x15d773[_0x35ac('0xe')][_0x3da02f];});}else{ut[_0x35ac('0x48')](_0x35ac('0x4c'));delete this[_0x35ac('0xb')][_0x3da02f];delete this['actions'][_0x3da02f];}};Fax[_0x35ac('0x20')][_0x35ac('0x1d')]=function(_0x11ee46){var _0x570742=this;if(this[_0x35ac('0xb')][_0x11ee46[_0x35ac('0x26')]]){if(this[_0x35ac('0xe')][_0x11ee46['uniqueid']]){this[_0x35ac('0xb')][_0x11ee46[_0x35ac('0x26')]]['id']=this[_0x35ac('0xe')][_0x11ee46[_0x35ac('0x26')]]['id'];this[_0x35ac('0xb')][_0x11ee46[_0x35ac('0x26')]][_0x35ac('0x28')]=this['actions'][_0x11ee46[_0x35ac('0x26')]][_0x35ac('0x28')];}logger[_0x35ac('0x2a')](_0x11ee46[_0x35ac('0x26')],this['actions'][_0x11ee46['uniqueid']]?_0x35ac('0x4d'):'\x20hangup\x20faxRiceive\x20',JSON[_0x35ac('0x3e')](this[_0x35ac('0xb')][_0x11ee46[_0x35ac('0x26')]]));if(this[_0x35ac('0xe')][_0x11ee46[_0x35ac('0x26')]]){if(this[_0x35ac('0xb')][_0x11ee46['uniqueid']][_0x35ac('0x4e')]>0x0&&this[_0x35ac('0xb')][_0x11ee46['uniqueid']]['faxstatus']===_0x35ac('0x4f')){this[_0x35ac('0x50')](_0x11ee46['uniqueid'],!![]);}else{this['updateStatus'](_0x11ee46[_0x35ac('0x26')],![],this[_0x35ac('0xb')][_0x11ee46[_0x35ac('0x26')]][_0x35ac('0x51')]?'SENDING\x20FAILED:\x20'+this[_0x35ac('0xb')][_0x11ee46[_0x35ac('0x26')]][_0x35ac('0x51')]:_0x35ac('0x52'));}}}};Fax[_0x35ac('0x20')][_0x35ac('0x53')]=function(_0x3b5b3c){var _0x52bc17=this;return new BPromise(function(_0x509c1c,_0x540eb0){_0x52bc17[_0x35ac('0x54')](_0x3b5b3c,_0x52bc17[_0x35ac('0xc')][_0x3b5b3c['FaxAccountId']])[_0x35ac('0x46')](function(_0x5e7538){ut['logger'](_0x35ac('0x55'),_0x5e7538);_0x509c1c(ut[_0x35ac('0x56')](_0x35ac('0x57')));})[_0x35ac('0x3c')](function(_0x5c6d4e){ut[_0x35ac('0x48')](_0x35ac('0x58'),_0x5c6d4e);updateStatus(_0x3b5b3c);_0x540eb0(ut[_0x35ac('0x56')](_0x35ac('0x59')));});});};Fax['prototype'][_0x35ac('0x5a')]=function(_0xd68bfb){var _0xb09a3=this;return new BPromise(function(_0x382be2,_0x56c637){var _0x4e00f2=_0xb09a3['checkTrunk'](_0xb09a3['faxAccounts'][_0xd68bfb[_0x35ac('0x5b')]][_0x35ac('0x5c')],_0xd68bfb);if(_0x4e00f2['isValid']){_0x382be2(ut['message'](_0x35ac('0x5d')));}else{_0x56c637(ut['message'](_0x4e00f2[_0x35ac('0x56')]));}});};function originate(_0xef8ff8,_0x2f5042,_0x127ef2){return new BPromise(function(_0x499205,_0x1950cc){if(_0x127ef2){var _0x58b492=util[_0x35ac('0x5e')](_0x35ac('0x5f'),path['parse'](_0xef8ff8['path'])[_0x35ac('0x28')],_0xef8ff8[_0x35ac('0x60')]);gs[_0x35ac('0x61')](_0x58b492)[_0x35ac('0x46')](function(){_0xef8ff8[_0x35ac('0x60')]=util[_0x35ac('0x5e')](_0x35ac('0x62'),path[_0x35ac('0x39')](_0xef8ff8[_0x35ac('0x60')])['dir'],path[_0x35ac('0x39')](_0xef8ff8[_0x35ac('0x60')])['name']);_0x2f5042[_0x35ac('0x63')]=_0xef8ff8[_0x35ac('0x60')];ut[_0x35ac('0x48')](_0x35ac('0x64'),'');ami[_0x35ac('0x65')](ut[_0x35ac('0x66')](_0xef8ff8,_0x2f5042))[_0x35ac('0x46')](function(_0x5a8cee){_0x499205(_0x5a8cee);})[_0x35ac('0x3c')](function(_0x5d6c6f){_0x1950cc(_0x5d6c6f);});})[_0x35ac('0x3c')](function(_0x18eb05){_0x1950cc(_0x18eb05);});}else{ami[_0x35ac('0x65')](ut['variablesAction'](_0xef8ff8,_0x2f5042))[_0x35ac('0x46')](function(_0x592522){_0x499205(_0x592522);})[_0x35ac('0x3c')](function(_0x4e221c){_0x1950cc(_0x4e221c);});}});}Fax[_0x35ac('0x20')][_0x35ac('0x54')]=function(_0x2dfc32,_0x32c7d3){var _0x30b471=this;return new BPromise(function(_0xd3b54f,_0x2c5e06){var _0x437cf0=uuid['v4']();var _0x14d040=ut[_0x35ac('0x67')](_0x2dfc32,_0x32c7d3);_0x30b471['actions'][_0x437cf0]=new Action(_0x437cf0,_0x32c7d3,_0x2dfc32);var _0x20584d=_0x30b471[_0x35ac('0xe')][_0x437cf0];if(path['parse'](_0x2dfc32[_0x35ac('0x60')])[_0x35ac('0x68')]===_0x35ac('0x3a')){originate(_0x20584d,_0x14d040,!![])[_0x35ac('0x46')](function(_0x1b2955){_0xd3b54f(_0x1b2955);})[_0x35ac('0x3c')](function(_0x55df0d){_0x2c5e06(_0x55df0d);});}else{originate(_0x20584d,_0x14d040,![])['then'](function(_0x1eb96b){_0xd3b54f(_0x1eb96b);})[_0x35ac('0x3c')](function(_0x216a46){_0x2c5e06(_0x216a46);});}});};Fax[_0x35ac('0x20')][_0x35ac('0x69')]=function(_0x5b2bb3){return!_[_0x35ac('0x6a')](_0x5b2bb3)&&!_['isNil'](_0x5b2bb3[_0x35ac('0x28')])&&!_['isNil'](_0x5b2bb3['active'])&&_0x5b2bb3[_0x35ac('0x6b')]&&!_[_0x35ac('0x6a')](this[_0x35ac('0xd')][util['format'](_0x35ac('0x6c'),_0x5b2bb3['name'])])&&!_[_0x35ac('0x6a')](this[_0x35ac('0xd')][util['format']('SIP/%s',_0x5b2bb3[_0x35ac('0x28')])][_0x35ac('0x30')])&&(this[_0x35ac('0xd')][util[_0x35ac('0x5e')](_0x35ac('0x6c'),_0x5b2bb3[_0x35ac('0x28')])][_0x35ac('0x30')]==='reachable'||this[_0x35ac('0xd')][util[_0x35ac('0x5e')](_0x35ac('0x6c'),_0x5b2bb3[_0x35ac('0x28')])][_0x35ac('0x30')]===_0x35ac('0x6d'));};Fax[_0x35ac('0x20')]['checkTrunk']=function(_0x22367e,_0x513755){if(!this[_0x35ac('0x69')](_0x22367e)){ut['logger']('[Fax][checkTrunk][Trunk\x20not\x20work]',_[_0x35ac('0x6a')](_0x22367e)?'':_0x22367e[_0x35ac('0x28')]+_0x35ac('0x6e'));return{'isValid':![],'message':_0x35ac('0x6f')};}if(_['isNil'](_0x513755['fax'])){ut[_0x35ac('0x48')]('[Fax][checkTrunk][Empty\x20Fax]',JSON[_0x35ac('0x3e')](_0x513755));return{'isValid':![],'message':_0x35ac('0x70')};}if(_['isNil'](_0x513755[_0x35ac('0x60')])){ut[_0x35ac('0x48')](_0x35ac('0x71'),JSON[_0x35ac('0x3e')](_0x513755));return{'isValid':![],'message':_0x35ac('0x72')};}return{'isValid':!![],'message':'Is\x20Valid'};};module[_0x35ac('0x73')]=Fax;