Built motion from commit da24aabd.|2.6.20
[motion2.git] / server / services / email / lib / utils.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 _0x4bba=['messageId','BodyHTML','Attachments','Title','<p>Empty\x20message</p>','generateXOAuth2Token','user=','auth=Bearer\x20','utf-8','toString','base64','lodash','moment','util','node-tnef','../../../config/logger','../../../config/environment','head','value','address','anonymous','@xcally.com','name','substring','indexOf','split','length','slice','join','Anonymous','pop','isEmpty','html','test','replace','<img','attachments','push','filename','_SRC','_IMG','<span\x20style=font-weight:bold>\x20[Attachment:\x20','\x20]</span>','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','message','originTo','email','originCc','includes','bcc','attach','type','attachment','%s-%s','now','root','writeFileSync','content','contentType','headerLines','Auto-Submitted:\x20auto-replied','X-Auto-Response-Suppress:\x20All','Precedence:\x20auto_reply','line','isAutoreply','parser','from','inReplyTo','date','format','YYYY-MM-DD\x20HH:mm:ss','application/ms-tnef','info','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system'];(function(_0x40220a,_0x22feb1){var _0x3903cc=function(_0x33a296){while(--_0x33a296){_0x40220a['push'](_0x40220a['shift']());}};_0x3903cc(++_0x22feb1);}(_0x4bba,0x1d3));var _0xa4bb=function(_0x484e36,_0x4482d8){_0x484e36=_0x484e36-0x0;var _0xafc85d=_0x4bba[_0x484e36];return _0xafc85d;};'use strict';var _=require(_0xa4bb('0x0'));var moment=require(_0xa4bb('0x1'));var random=require('randomstring');var fs=require('fs');var path=require('path');var util=require(_0xa4bb('0x2'));var tnef=require(_0xa4bb('0x3'));var logger=require(_0xa4bb('0x4'))('email');var config=require(_0xa4bb('0x5'));function getAddress(_0x554a85){var _0x505391=_[_0xa4bb('0x6')](_0x554a85[_0xa4bb('0x7')]);return _0x505391&&_0x505391[_0xa4bb('0x8')]?_0x505391[_0xa4bb('0x8')]:_0xa4bb('0x9')+random['generate'](0x6)+_0xa4bb('0xa');}function getFirstName(_0x31d802){var _0x32da14=_[_0xa4bb('0x6')](_0x31d802[_0xa4bb('0x7')]);if(_0x32da14&&_0x32da14[_0xa4bb('0xb')]){if(_0x32da14[_0xa4bb('0xb')]==_0x32da14[_0xa4bb('0x8')]){return _0x32da14[_0xa4bb('0xb')][_0xa4bb('0xc')](0x0,_0x32da14['name'][_0xa4bb('0xd')]('@'));}var _0x5ccde8=_0x32da14[_0xa4bb('0xb')][_0xa4bb('0xe')](/\s+/);return _0x5ccde8[_0xa4bb('0xf')]>0x1?_0x5ccde8[_0xa4bb('0x10')](0x0,-0x1)[_0xa4bb('0x11')]('\x20'):_0x32da14[_0xa4bb('0xb')];}return _0xa4bb('0x12');}function getLastName(_0x5b7832){var _0x5744ee=_[_0xa4bb('0x6')](_0x5b7832['value']);if(_0x5744ee&&_0x5744ee[_0xa4bb('0xb')]){var _0x466557=_0x5744ee[_0xa4bb('0xb')][_0xa4bb('0xe')](/\s+/);return _0x466557[_0xa4bb('0xf')]>0x1?_0x466557[_0xa4bb('0x13')]():'';}return'';}function getMails(_0x337716,_0x172d21){var _0xaceda4='';for(var _0x4f10e1=0x0;_0x4f10e1<_0x172d21[_0xa4bb('0x7')][_0xa4bb('0xf')];_0x4f10e1+=0x1){if(_0x172d21[_0xa4bb('0x7')][_0x4f10e1][_0xa4bb('0x8')]!==_0x337716){if(!_[_0xa4bb('0x14')](_0xaceda4)){_0xaceda4+=',';}_0xaceda4+=_0x172d21['value'][_0x4f10e1][_0xa4bb('0xb')]||_0x172d21[_0xa4bb('0x7')][_0x4f10e1]['address']||'';_0xaceda4+='\x20<';_0xaceda4+=_0x172d21[_0xa4bb('0x7')][_0x4f10e1][_0xa4bb('0x8')]?_0x172d21[_0xa4bb('0x7')][_0x4f10e1][_0xa4bb('0x8')]:_0xa4bb('0x9')+random['generate'](0x6)+_0xa4bb('0xa');_0xaceda4+='>';}}return _0xaceda4;}function parseEmail(_0xcf6ad8,_0x5719e4,_0x423ee1){if(_0x5719e4[_0xa4bb('0x15')]){var _0x16afdd=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x16afdd[_0xa4bb('0x16')](_0x5719e4[_0xa4bb('0x15')])){_0x5719e4[_0xa4bb('0x15')]=_0x5719e4[_0xa4bb('0x15')][_0xa4bb('0x17')](_0x16afdd,'');}if(_0x5719e4[_0xa4bb('0x15')]['includes'](_0xa4bb('0x18'))){var _0x5d3eaa=[];_0x5719e4[_0xa4bb('0x19')]['forEach'](_0x51926c=>{_0x5d3eaa[_0xa4bb('0x1a')](_0x51926c[_0xa4bb('0x1b')]);});var _0x3a955d=/<img.*? _SRC(.*?)[^>]+>/g;var _0x599d84=/src="data:image\/([a-zA-Z]*);base64,([^"]*)"/g;var _0x55f6f5=_0x5719e4[_0xa4bb('0x15')][_0xa4bb('0x17')](_0x599d84,_0xa4bb('0x1c'));var _0xe4f681=_0x55f6f5['replace'](_0x3a955d,_0xa4bb('0x1d'));for(let _0x150e54=0x0;_0x150e54<_0x5d3eaa[_0xa4bb('0xf')];_0x150e54++){var _0x537864=_0xe4f681['replace'](_0xa4bb('0x1d'),_0xa4bb('0x1e')+_0x5d3eaa[_0x150e54]+_0xa4bb('0x1f'));_0xe4f681=_0x537864;}_0x5719e4[_0xa4bb('0x15')]=_0xe4f681;}_0x423ee1[_0xa4bb('0x20')]=_0x5719e4[_0xa4bb('0x15')];}else{_0x423ee1['body']=_0x5719e4[_0xa4bb('0x21')];}if(_0x423ee1['body']){_0x423ee1[_0xa4bb('0x20')]=_0x423ee1[_0xa4bb('0x20')][_0xa4bb('0x17')](/<a\s+/gi,_0xa4bb('0x22'));}if(_0x5719e4['to']){_0x423ee1[_0xa4bb('0x23')][_0xa4bb('0x24')]=getMails(null,_0x5719e4['to']);var _0x58e8d6=getMails(_0xcf6ad8[_0xa4bb('0x25')],_0x5719e4['to']);if(!_[_0xa4bb('0x14')](_0x58e8d6)){_0x423ee1[_0xa4bb('0x23')]['cc']+=_0x58e8d6;}}if(_0x5719e4['cc']){_0x423ee1[_0xa4bb('0x23')][_0xa4bb('0x26')]=getMails(null,_0x5719e4['cc']);var _0x271ba0=getMails(_0xcf6ad8[_0xa4bb('0x25')],_0x5719e4['cc']);if(!_[_0xa4bb('0x14')](_0x271ba0)){if(!_['isEmpty'](_0x423ee1['message']['cc'])){_0x423ee1[_0xa4bb('0x23')]['cc']+=',';}_0x423ee1[_0xa4bb('0x23')]['cc']+=_0x271ba0;}}if(!_[_0xa4bb('0x27')](_0x423ee1[_0xa4bb('0x23')][_0xa4bb('0x24')],_0xcf6ad8[_0xa4bb('0x25')])&&!_[_0xa4bb('0x27')](_0x423ee1[_0xa4bb('0x23')][_0xa4bb('0x26')],_0xcf6ad8[_0xa4bb('0x25')])){_0x423ee1[_0xa4bb('0x23')][_0xa4bb('0x28')]=_0xcf6ad8[_0xa4bb('0x25')];}if(_0x5719e4[_0xa4bb('0x19')]&&_0x5719e4[_0xa4bb('0x19')][_0xa4bb('0xf')]){_0x423ee1[_0xa4bb('0x23')][_0xa4bb('0x29')]=_0x5719e4[_0xa4bb('0x19')][_0xa4bb('0xf')];for(var _0x54201e=0x0,_0x362ee4,_0x1d0020,_0x43cac7;_0x54201e<_0x5719e4[_0xa4bb('0x19')][_0xa4bb('0xf')];_0x54201e+=0x1){if(_0x5719e4[_0xa4bb('0x19')][_0x54201e][_0xa4bb('0x2a')]===_0xa4bb('0x2b')){_0x43cac7=_0x5719e4[_0xa4bb('0x19')][_0x54201e][_0xa4bb('0x1b')]||'attachment-'+_0x54201e;_0x362ee4=util['format'](_0xa4bb('0x2c'),Date[_0xa4bb('0x2d')](),_0x43cac7);_0x1d0020=path[_0xa4bb('0x11')](config[_0xa4bb('0x2e')],'server/files/attachments/',_0x362ee4);fs[_0xa4bb('0x2f')](_0x1d0020,_0x5719e4[_0xa4bb('0x19')][_0x54201e][_0xa4bb('0x30')]);_0x423ee1[_0xa4bb('0x23')][_0xa4bb('0x19')][_0xa4bb('0x1a')]({'name':_0x43cac7,'basename':_0x362ee4,'type':_0x5719e4[_0xa4bb('0x19')][_0x54201e][_0xa4bb('0x31')],'filepath':_0x1d0020});}}}if(_0x5719e4['headerLines']&&_0x5719e4[_0xa4bb('0x32')][_0xa4bb('0xf')]>0x0){var _0x46309c=[_0xa4bb('0x33'),_0xa4bb('0x34'),_0xa4bb('0x35')];for(var _0x4a5338=0x0;_0x4a5338<_0x5719e4[_0xa4bb('0x32')][_0xa4bb('0xf')];_0x4a5338++){var _0x38bc86=_['indexOf'](_0x46309c,_0x5719e4[_0xa4bb('0x32')][_0x4a5338][_0xa4bb('0x36')]);if(_0x38bc86>=0x0){_0x423ee1[_0xa4bb('0x37')]=!![];}}}return _0x423ee1;}exports[_0xa4bb('0x38')]=function(_0x35f820,_0x339314,_0x2b8336){var _0x440335={'from':getAddress(_0x339314['from']),'firstName':getFirstName(_0x339314['from']),'lastName':getLastName(_0x339314[_0xa4bb('0x39')]),'mapKey':_0xa4bb('0x25'),'message':{'from':getMails(_0x35f820[_0xa4bb('0x25')],_0x339314['from']),'to':_0x35f820[_0xa4bb('0x25')],'cc':'','subject':_0x339314['subject']||'','messageId':_0x339314['messageId'],'inReplyTo':_0x339314[_0xa4bb('0x3a')],'sentAt':moment(_0x339314[_0xa4bb('0x3b')])[_0xa4bb('0x3c')](_0xa4bb('0x3d')),'attach':![],'attachments':[]}};if(!_0x339314[_0xa4bb('0x15')]&&!_0x339314[_0xa4bb('0x21')]){var _0x1c8187=_['filter'](_0x339314[_0xa4bb('0x19')],{'contentType':_0xa4bb('0x3e')});if(_0x1c8187['length']){logger[_0xa4bb('0x3f')](util['format'](_0xa4bb('0x40'),_0x339314[_0xa4bb('0x41')]));tnef['parseBuffer'](_['first'](_0x1c8187)[_0xa4bb('0x30')],function(_0x2d8857,_0x1bb092){if(_0x1bb092){_0x339314[_0xa4bb('0x15')]=_0x1bb092[_0xa4bb('0x42')]||'<p>Empty\x20message</p>';if(_0x1bb092['Attachments']){for(var _0x7de44=0x0;_0x7de44<_0x1bb092[_0xa4bb('0x43')][_0xa4bb('0xf')];_0x7de44++){var _0x1424bd=_0x1bb092[_0xa4bb('0x43')][_0x7de44];_0x440335[_0xa4bb('0x23')][_0xa4bb('0x19')][_0xa4bb('0x1a')]({'filename':_0x1424bd[_0xa4bb('0x44')],'content':Buffer['from'](_0x1424bd['Data'])});}}}_0x2b8336(parseEmail(_0x35f820,_0x339314,_0x440335));});}else{_0x339314[_0xa4bb('0x15')]=_0xa4bb('0x45');_0x2b8336(parseEmail(_0x35f820,_0x339314,_0x440335));}}else{_0x2b8336(parseEmail(_0x35f820,_0x339314,_0x440335));}};exports[_0xa4bb('0x46')]=function(_0x248ac7,_0x40484b){var _0x1af54b=[_0xa4bb('0x47')+(_0x40484b||''),_0xa4bb('0x48')+_0x248ac7,'',''];return Buffer[_0xa4bb('0x39')](_0x1af54b[_0xa4bb('0x11')]('\ 1'),_0xa4bb('0x49'))[_0xa4bb('0x4a')](_0xa4bb('0x4b'));};