Built motion from commit 00bf23f6.|2.6.16
[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 _0xf7c3=['\x20]</span>','body','textAsHtml','message','originTo','originCc','isEmpty','bcc','attach','attachment','attachment-','now','server/files/attachments/','writeFileSync','content','contentType','headerLines','Auto-Submitted:\x20auto-replied','X-Auto-Response-Suppress:\x20All','Precedence:\x20auto_reply','line','isAutoreply','from','messageId','inReplyTo','date','format','YYYY-MM-DD\x20HH:mm:ss','filter','application/ms-tnef','info','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','parseBuffer','BodyHTML','<p>Empty\x20message</p>','Attachments','Title','Data','auth=Bearer\x20','utf-8','lodash','moment','randomstring','util','node-tnef','../../../config/logger','email','head','value','address','anonymous','generate','@xcally.com','name','substring','indexOf','split','length','slice','join','pop','html','replace','includes','<img','attachments','forEach','push','filename','_SRC','_IMG','<span\x20style=font-weight:bold>\x20[Attachment:\x20'];(function(_0x5e78f6,_0x56d814){var _0x20fe50=function(_0x236bd0){while(--_0x236bd0){_0x5e78f6['push'](_0x5e78f6['shift']());}};_0x20fe50(++_0x56d814);}(_0xf7c3,0x70));var _0x3f7c=function(_0x3ac5fe,_0x4b44af){_0x3ac5fe=_0x3ac5fe-0x0;var _0x1e7151=_0xf7c3[_0x3ac5fe];return _0x1e7151;};'use strict';var _=require(_0x3f7c('0x0'));var moment=require(_0x3f7c('0x1'));var random=require(_0x3f7c('0x2'));var fs=require('fs');var path=require('path');var util=require(_0x3f7c('0x3'));var tnef=require(_0x3f7c('0x4'));var logger=require(_0x3f7c('0x5'))(_0x3f7c('0x6'));var config=require('../../../config/environment');function getAddress(_0x41d4ed){var _0x3a04a9=_[_0x3f7c('0x7')](_0x41d4ed[_0x3f7c('0x8')]);return _0x3a04a9&&_0x3a04a9[_0x3f7c('0x9')]?_0x3a04a9[_0x3f7c('0x9')]:_0x3f7c('0xa')+random[_0x3f7c('0xb')](0x6)+_0x3f7c('0xc');}function getFirstName(_0x22d097){var _0x537725=_[_0x3f7c('0x7')](_0x22d097['value']);if(_0x537725&&_0x537725[_0x3f7c('0xd')]){if(_0x537725[_0x3f7c('0xd')]==_0x537725['address']){return _0x537725[_0x3f7c('0xd')][_0x3f7c('0xe')](0x0,_0x537725[_0x3f7c('0xd')][_0x3f7c('0xf')]('@'));}var _0x240ae7=_0x537725['name'][_0x3f7c('0x10')](/\s+/);return _0x240ae7[_0x3f7c('0x11')]>0x1?_0x240ae7[_0x3f7c('0x12')](0x0,-0x1)[_0x3f7c('0x13')]('\x20'):_0x537725[_0x3f7c('0xd')];}return'Anonymous';}function getLastName(_0x5bd920){var _0x40bc15=_[_0x3f7c('0x7')](_0x5bd920[_0x3f7c('0x8')]);if(_0x40bc15&&_0x40bc15[_0x3f7c('0xd')]){var _0xe58068=_0x40bc15[_0x3f7c('0xd')]['split'](/\s+/);return _0xe58068[_0x3f7c('0x11')]>0x1?_0xe58068[_0x3f7c('0x14')]():'';}return'';}function getMails(_0x339756,_0x21eab9){var _0x2e65b3='';for(var _0x32ab13=0x0;_0x32ab13<_0x21eab9['value'][_0x3f7c('0x11')];_0x32ab13+=0x1){if(_0x21eab9[_0x3f7c('0x8')][_0x32ab13][_0x3f7c('0x9')]!==_0x339756){if(!_['isEmpty'](_0x2e65b3)){_0x2e65b3+=',';}_0x2e65b3+=_0x21eab9['value'][_0x32ab13]['name']||_0x21eab9[_0x3f7c('0x8')][_0x32ab13]['address']||'';_0x2e65b3+='\x20<';_0x2e65b3+=_0x21eab9[_0x3f7c('0x8')][_0x32ab13][_0x3f7c('0x9')]?_0x21eab9[_0x3f7c('0x8')][_0x32ab13][_0x3f7c('0x9')]:'anonymous'+random['generate'](0x6)+_0x3f7c('0xc');_0x2e65b3+='>';}}return _0x2e65b3;}function parseEmail(_0x2d4f1d,_0x48520d,_0x339c9d){if(_0x48520d['html']){var _0x2065cc=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x2065cc['test'](_0x48520d[_0x3f7c('0x15')])){_0x48520d[_0x3f7c('0x15')]=_0x48520d[_0x3f7c('0x15')][_0x3f7c('0x16')](_0x2065cc,'');}if(_0x48520d[_0x3f7c('0x15')][_0x3f7c('0x17')](_0x3f7c('0x18'))){var _0x3a70c9=[];_0x48520d[_0x3f7c('0x19')][_0x3f7c('0x1a')](_0x34f75d=>{_0x3a70c9[_0x3f7c('0x1b')](_0x34f75d[_0x3f7c('0x1c')]);});var _0x10a842=/<img.*? _SRC(.*?)[^>]+>/g;var _0x3cce36=/src="data:image\/([a-zA-Z]*);base64,([^"]*)"/g;var _0x50354f=_0x48520d[_0x3f7c('0x15')][_0x3f7c('0x16')](_0x3cce36,_0x3f7c('0x1d'));var _0x2b0fd9=_0x50354f['replace'](_0x10a842,_0x3f7c('0x1e'));for(let _0x186778=0x0;_0x186778<_0x3a70c9[_0x3f7c('0x11')];_0x186778++){var _0x5a65de=_0x2b0fd9[_0x3f7c('0x16')](_0x3f7c('0x1e'),_0x3f7c('0x1f')+_0x3a70c9[_0x186778]+_0x3f7c('0x20'));_0x2b0fd9=_0x5a65de;}_0x48520d[_0x3f7c('0x15')]=_0x2b0fd9;}_0x339c9d[_0x3f7c('0x21')]=_0x48520d[_0x3f7c('0x15')];}else{_0x339c9d[_0x3f7c('0x21')]=_0x48520d[_0x3f7c('0x22')];}if(_0x339c9d['body']){_0x339c9d[_0x3f7c('0x21')]=_0x339c9d[_0x3f7c('0x21')][_0x3f7c('0x16')](/<a\s+/gi,'<a\x20target=\x22_blank\x22\x20');}if(_0x48520d['to']){_0x339c9d[_0x3f7c('0x23')][_0x3f7c('0x24')]=getMails(null,_0x48520d['to']);var _0x3a309b=getMails(_0x2d4f1d[_0x3f7c('0x6')],_0x48520d['to']);if(!_['isEmpty'](_0x3a309b)){_0x339c9d[_0x3f7c('0x23')]['cc']+=_0x3a309b;}}if(_0x48520d['cc']){_0x339c9d[_0x3f7c('0x23')][_0x3f7c('0x25')]=getMails(null,_0x48520d['cc']);var _0x25908b=getMails(_0x2d4f1d[_0x3f7c('0x6')],_0x48520d['cc']);if(!_[_0x3f7c('0x26')](_0x25908b)){if(!_[_0x3f7c('0x26')](_0x339c9d['message']['cc'])){_0x339c9d[_0x3f7c('0x23')]['cc']+=',';}_0x339c9d[_0x3f7c('0x23')]['cc']+=_0x25908b;}}if(!_['includes'](_0x339c9d[_0x3f7c('0x23')][_0x3f7c('0x24')],_0x2d4f1d['email'])&&!_[_0x3f7c('0x17')](_0x339c9d[_0x3f7c('0x23')][_0x3f7c('0x25')],_0x2d4f1d[_0x3f7c('0x6')])){_0x339c9d['message'][_0x3f7c('0x27')]=_0x2d4f1d[_0x3f7c('0x6')];}if(_0x48520d['attachments']&&_0x48520d[_0x3f7c('0x19')][_0x3f7c('0x11')]){_0x339c9d[_0x3f7c('0x23')][_0x3f7c('0x28')]=_0x48520d[_0x3f7c('0x19')][_0x3f7c('0x11')];for(var _0x2933ab=0x0,_0x4fefb7,_0x1043f6,_0x1d6378;_0x2933ab<_0x48520d[_0x3f7c('0x19')][_0x3f7c('0x11')];_0x2933ab+=0x1){if(_0x48520d[_0x3f7c('0x19')][_0x2933ab]['type']===_0x3f7c('0x29')){_0x1d6378=_0x48520d['attachments'][_0x2933ab][_0x3f7c('0x1c')]||_0x3f7c('0x2a')+_0x2933ab;_0x4fefb7=util['format']('%s-%s',Date[_0x3f7c('0x2b')](),_0x1d6378);_0x1043f6=path[_0x3f7c('0x13')](config['root'],_0x3f7c('0x2c'),_0x4fefb7);fs[_0x3f7c('0x2d')](_0x1043f6,_0x48520d['attachments'][_0x2933ab][_0x3f7c('0x2e')]);_0x339c9d['message']['attachments'][_0x3f7c('0x1b')]({'name':_0x1d6378,'basename':_0x4fefb7,'type':_0x48520d[_0x3f7c('0x19')][_0x2933ab][_0x3f7c('0x2f')],'filepath':_0x1043f6});}}}if(_0x48520d[_0x3f7c('0x30')]&&_0x48520d[_0x3f7c('0x30')][_0x3f7c('0x11')]>0x0){var _0x52ee8a=[_0x3f7c('0x31'),_0x3f7c('0x32'),_0x3f7c('0x33')];for(var _0x2e13a3=0x0;_0x2e13a3<_0x48520d[_0x3f7c('0x30')][_0x3f7c('0x11')];_0x2e13a3++){var _0x2d0251=_[_0x3f7c('0xf')](_0x52ee8a,_0x48520d['headerLines'][_0x2e13a3][_0x3f7c('0x34')]);if(_0x2d0251>=0x0){_0x339c9d[_0x3f7c('0x35')]=!![];}}}return _0x339c9d;}exports['parser']=function(_0xaeb562,_0x4cec53,_0x3e7692){var _0x2333d4={'from':getAddress(_0x4cec53[_0x3f7c('0x36')]),'firstName':getFirstName(_0x4cec53[_0x3f7c('0x36')]),'lastName':getLastName(_0x4cec53[_0x3f7c('0x36')]),'mapKey':'email','message':{'from':getMails(_0xaeb562[_0x3f7c('0x6')],_0x4cec53[_0x3f7c('0x36')]),'to':_0xaeb562['email'],'cc':'','subject':_0x4cec53['subject']||'','messageId':_0x4cec53[_0x3f7c('0x37')],'inReplyTo':_0x4cec53[_0x3f7c('0x38')],'sentAt':moment(_0x4cec53[_0x3f7c('0x39')])[_0x3f7c('0x3a')](_0x3f7c('0x3b')),'attach':![],'attachments':[]}};if(!_0x4cec53[_0x3f7c('0x15')]&&!_0x4cec53[_0x3f7c('0x22')]){var _0x55d37c=_[_0x3f7c('0x3c')](_0x4cec53[_0x3f7c('0x19')],{'contentType':_0x3f7c('0x3d')});if(_0x55d37c[_0x3f7c('0x11')]){logger[_0x3f7c('0x3e')](util[_0x3f7c('0x3a')](_0x3f7c('0x3f'),_0x4cec53[_0x3f7c('0x37')]));tnef[_0x3f7c('0x40')](_['first'](_0x55d37c)[_0x3f7c('0x2e')],function(_0x549c92,_0x246ad9){if(_0x246ad9){_0x4cec53[_0x3f7c('0x15')]=_0x246ad9[_0x3f7c('0x41')]||_0x3f7c('0x42');if(_0x246ad9['Attachments']){for(var _0x428cfb=0x0;_0x428cfb<_0x246ad9['Attachments'][_0x3f7c('0x11')];_0x428cfb++){var _0x558fa6=_0x246ad9[_0x3f7c('0x43')][_0x428cfb];_0x2333d4['message'][_0x3f7c('0x19')]['push']({'filename':_0x558fa6[_0x3f7c('0x44')],'content':Buffer[_0x3f7c('0x36')](_0x558fa6[_0x3f7c('0x45')])});}}}_0x3e7692(parseEmail(_0xaeb562,_0x4cec53,_0x2333d4));});}else{_0x4cec53[_0x3f7c('0x15')]='<p>Empty\x20message</p>';_0x3e7692(parseEmail(_0xaeb562,_0x4cec53,_0x2333d4));}}else{_0x3e7692(parseEmail(_0xaeb562,_0x4cec53,_0x2333d4));}};exports['generateXOAuth2Token']=function(_0x741b77,_0x139cc1){var _0x5ba006=['user='+(_0x139cc1||''),_0x3f7c('0x46')+_0x741b77,'',''];return Buffer[_0x3f7c('0x36')](_0x5ba006[_0x3f7c('0x13')]('\ 1'),_0x3f7c('0x47'))['toString']('base64');};