dda30edfd75d6fef323340b6d66a2f8b18bca74d
[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 _0xdd5b=['originTo','originCc','attachments','attach','type','attachment','root','server/files/attachments/','writeFileSync','content','contentType','headerLines','X-Auto-Response-Suppress:\x20All','Precedence:\x20auto_reply','isAutoreply','parser','from','messageId','inReplyTo','date','format','filter','info','parseBuffer','first','BodyHTML','Attachments','Title','Data','<p>Empty\x20message</p>','generateXOAuth2Token','utf-8','toString','base64','lodash','moment','randomstring','path','email','../../../config/environment','head','value','address','anonymous','@xcally.com','name','substring','indexOf','split','length','slice','join','Anonymous','pop','isEmpty','generate','html','replace','includes','forEach','push','filename','<span\x20style=font-weight:bold>\x20[Attachment:\x20','\x20]</span>','body','textAsHtml','message'];(function(_0x3646b9,_0x3a06fc){var _0x5c80be=function(_0x328ea3){while(--_0x328ea3){_0x3646b9['push'](_0x3646b9['shift']());}};_0x5c80be(++_0x3a06fc);}(_0xdd5b,0xa8));var _0xbdd5=function(_0x59a7d2,_0x1a8474){_0x59a7d2=_0x59a7d2-0x0;var _0x2bd4a9=_0xdd5b[_0x59a7d2];return _0x2bd4a9;};'use strict';var _=require(_0xbdd5('0x0'));var moment=require(_0xbdd5('0x1'));var random=require(_0xbdd5('0x2'));var fs=require('fs');var path=require(_0xbdd5('0x3'));var util=require('util');var tnef=require('node-tnef');var logger=require('../../../config/logger')(_0xbdd5('0x4'));var config=require(_0xbdd5('0x5'));function getAddress(_0x3cbf2e){var _0x312bb0=_[_0xbdd5('0x6')](_0x3cbf2e[_0xbdd5('0x7')]);return _0x312bb0&&_0x312bb0[_0xbdd5('0x8')]?_0x312bb0[_0xbdd5('0x8')]:_0xbdd5('0x9')+random['generate'](0x6)+_0xbdd5('0xa');}function getFirstName(_0x30d955){var _0x4799ec=_[_0xbdd5('0x6')](_0x30d955['value']);if(_0x4799ec&&_0x4799ec[_0xbdd5('0xb')]){if(_0x4799ec[_0xbdd5('0xb')]==_0x4799ec['address']){return _0x4799ec[_0xbdd5('0xb')][_0xbdd5('0xc')](0x0,_0x4799ec[_0xbdd5('0xb')][_0xbdd5('0xd')]('@'));}var _0x19391c=_0x4799ec[_0xbdd5('0xb')][_0xbdd5('0xe')](/\s+/);return _0x19391c[_0xbdd5('0xf')]>0x1?_0x19391c[_0xbdd5('0x10')](0x0,-0x1)[_0xbdd5('0x11')]('\x20'):_0x4799ec[_0xbdd5('0xb')];}return _0xbdd5('0x12');}function getLastName(_0x43884a){var _0x50f5d9=_[_0xbdd5('0x6')](_0x43884a[_0xbdd5('0x7')]);if(_0x50f5d9&&_0x50f5d9['name']){var _0x5d1c3d=_0x50f5d9[_0xbdd5('0xb')][_0xbdd5('0xe')](/\s+/);return _0x5d1c3d[_0xbdd5('0xf')]>0x1?_0x5d1c3d[_0xbdd5('0x13')]():'';}return'';}function getMails(_0x492f26,_0x323911){var _0x4c9d79='';for(var _0x275925=0x0;_0x275925<_0x323911[_0xbdd5('0x7')][_0xbdd5('0xf')];_0x275925+=0x1){if(_0x323911[_0xbdd5('0x7')][_0x275925]['address']!==_0x492f26){if(!_[_0xbdd5('0x14')](_0x4c9d79)){_0x4c9d79+=',';}_0x4c9d79+=_0x323911[_0xbdd5('0x7')][_0x275925][_0xbdd5('0xb')]||_0x323911[_0xbdd5('0x7')][_0x275925][_0xbdd5('0x8')]||'';_0x4c9d79+='\x20<';_0x4c9d79+=_0x323911[_0xbdd5('0x7')][_0x275925][_0xbdd5('0x8')]?_0x323911[_0xbdd5('0x7')][_0x275925][_0xbdd5('0x8')]:_0xbdd5('0x9')+random[_0xbdd5('0x15')](0x6)+_0xbdd5('0xa');_0x4c9d79+='>';}}return _0x4c9d79;}function parseEmail(_0x944f22,_0x3365d9,_0x2fc366){if(_0x3365d9[_0xbdd5('0x16')]){var _0x36011e=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x36011e['test'](_0x3365d9[_0xbdd5('0x16')])){_0x3365d9[_0xbdd5('0x16')]=_0x3365d9[_0xbdd5('0x16')][_0xbdd5('0x17')](_0x36011e,'');}if(_0x3365d9[_0xbdd5('0x16')][_0xbdd5('0x18')]('<img')){var _0x225062=[];_0x3365d9['attachments'][_0xbdd5('0x19')](_0x3c6a34=>{_0x225062[_0xbdd5('0x1a')](_0x3c6a34[_0xbdd5('0x1b')]);});var _0x477ba6=/<img.*? _SRC(.*?)[^>]+>/g;var _0x1e0f71=/src="data:image\/([a-zA-Z]*);base64,([^"]*)"/g;var _0x44f14f=_0x3365d9['html'][_0xbdd5('0x17')](_0x1e0f71,'_SRC');var _0x2fceb3=_0x44f14f['replace'](_0x477ba6,'_IMG');for(let _0x7f387c=0x0;_0x7f387c<_0x225062[_0xbdd5('0xf')];_0x7f387c++){var _0x280221=_0x2fceb3[_0xbdd5('0x17')]('_IMG',_0xbdd5('0x1c')+_0x225062[_0x7f387c]+_0xbdd5('0x1d'));_0x2fceb3=_0x280221;}_0x3365d9[_0xbdd5('0x16')]=_0x2fceb3;}_0x2fc366['body']=_0x3365d9[_0xbdd5('0x16')];}else{_0x2fc366[_0xbdd5('0x1e')]=_0x3365d9[_0xbdd5('0x1f')];}if(_0x2fc366[_0xbdd5('0x1e')]){_0x2fc366[_0xbdd5('0x1e')]=_0x2fc366[_0xbdd5('0x1e')][_0xbdd5('0x17')](/<a\s+/gi,'<a\x20target=\x22_blank\x22\x20');}if(_0x3365d9['to']){_0x2fc366[_0xbdd5('0x20')][_0xbdd5('0x21')]=getMails(null,_0x3365d9['to']);var _0x25580f=getMails(_0x944f22[_0xbdd5('0x4')],_0x3365d9['to']);if(!_[_0xbdd5('0x14')](_0x25580f)){_0x2fc366[_0xbdd5('0x20')]['cc']+=_0x25580f;}}if(_0x3365d9['cc']){_0x2fc366[_0xbdd5('0x20')][_0xbdd5('0x22')]=getMails(null,_0x3365d9['cc']);var _0x566e40=getMails(_0x944f22[_0xbdd5('0x4')],_0x3365d9['cc']);if(!_[_0xbdd5('0x14')](_0x566e40)){if(!_[_0xbdd5('0x14')](_0x2fc366['message']['cc'])){_0x2fc366[_0xbdd5('0x20')]['cc']+=',';}_0x2fc366[_0xbdd5('0x20')]['cc']+=_0x566e40;}}if(!_[_0xbdd5('0x18')](_0x2fc366[_0xbdd5('0x20')][_0xbdd5('0x21')],_0x944f22[_0xbdd5('0x4')])&&!_[_0xbdd5('0x18')](_0x2fc366[_0xbdd5('0x20')][_0xbdd5('0x22')],_0x944f22[_0xbdd5('0x4')])){_0x2fc366[_0xbdd5('0x20')]['bcc']=_0x944f22['email'];}if(_0x3365d9['attachments']&&_0x3365d9[_0xbdd5('0x23')][_0xbdd5('0xf')]){_0x2fc366[_0xbdd5('0x20')][_0xbdd5('0x24')]=_0x3365d9[_0xbdd5('0x23')]['length'];for(var _0x510fec=0x0,_0x33e006,_0xd46e60,_0x255e4f;_0x510fec<_0x3365d9['attachments'][_0xbdd5('0xf')];_0x510fec+=0x1){if(_0x3365d9['attachments'][_0x510fec][_0xbdd5('0x25')]===_0xbdd5('0x26')){_0x255e4f=_0x3365d9['attachments'][_0x510fec][_0xbdd5('0x1b')]||'attachment-'+_0x510fec;_0x33e006=util['format']('%s-%s',Date['now'](),_0x255e4f);_0xd46e60=path['join'](config[_0xbdd5('0x27')],_0xbdd5('0x28'),_0x33e006);fs[_0xbdd5('0x29')](_0xd46e60,_0x3365d9[_0xbdd5('0x23')][_0x510fec][_0xbdd5('0x2a')]);_0x2fc366[_0xbdd5('0x20')][_0xbdd5('0x23')][_0xbdd5('0x1a')]({'name':_0x255e4f,'basename':_0x33e006,'type':_0x3365d9[_0xbdd5('0x23')][_0x510fec][_0xbdd5('0x2b')],'filepath':_0xd46e60});}}}if(_0x3365d9[_0xbdd5('0x2c')]&&_0x3365d9[_0xbdd5('0x2c')][_0xbdd5('0xf')]>0x0){var _0x431057=['Auto-Submitted:\x20auto-replied',_0xbdd5('0x2d'),_0xbdd5('0x2e')];for(var _0x291632=0x0;_0x291632<_0x3365d9['headerLines']['length'];_0x291632++){var _0x2c4d41=_[_0xbdd5('0xd')](_0x431057,_0x3365d9['headerLines'][_0x291632]['line']);if(_0x2c4d41>=0x0){_0x2fc366[_0xbdd5('0x2f')]=!![];}}}return _0x2fc366;}exports[_0xbdd5('0x30')]=function(_0x17daa8,_0x3210bc,_0x311536){var _0x33ed71={'from':getAddress(_0x3210bc['from']),'firstName':getFirstName(_0x3210bc[_0xbdd5('0x31')]),'lastName':getLastName(_0x3210bc['from']),'mapKey':_0xbdd5('0x4'),'message':{'from':getMails(_0x17daa8[_0xbdd5('0x4')],_0x3210bc[_0xbdd5('0x31')]),'to':_0x17daa8['email'],'cc':'','subject':_0x3210bc['subject']||'','messageId':_0x3210bc[_0xbdd5('0x32')],'inReplyTo':_0x3210bc[_0xbdd5('0x33')],'sentAt':moment(_0x3210bc[_0xbdd5('0x34')])[_0xbdd5('0x35')]('YYYY-MM-DD\x20HH:mm:ss'),'attach':![],'attachments':[]}};if(!_0x3210bc['html']&&!_0x3210bc[_0xbdd5('0x1f')]){var _0x122e4f=_[_0xbdd5('0x36')](_0x3210bc[_0xbdd5('0x23')],{'contentType':'application/ms-tnef'});if(_0x122e4f['length']){logger[_0xbdd5('0x37')](util[_0xbdd5('0x35')]('Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system',_0x3210bc[_0xbdd5('0x32')]));tnef[_0xbdd5('0x38')](_[_0xbdd5('0x39')](_0x122e4f)[_0xbdd5('0x2a')],function(_0xdfe7bb,_0xd0e6b4){if(_0xd0e6b4){_0x3210bc[_0xbdd5('0x16')]=_0xd0e6b4[_0xbdd5('0x3a')]||'<p>Empty\x20message</p>';if(_0xd0e6b4[_0xbdd5('0x3b')]){for(var _0x3a54bd=0x0;_0x3a54bd<_0xd0e6b4[_0xbdd5('0x3b')][_0xbdd5('0xf')];_0x3a54bd++){var _0x17c6a6=_0xd0e6b4[_0xbdd5('0x3b')][_0x3a54bd];_0x33ed71[_0xbdd5('0x20')][_0xbdd5('0x23')]['push']({'filename':_0x17c6a6[_0xbdd5('0x3c')],'content':Buffer[_0xbdd5('0x31')](_0x17c6a6[_0xbdd5('0x3d')])});}}}_0x311536(parseEmail(_0x17daa8,_0x3210bc,_0x33ed71));});}else{_0x3210bc[_0xbdd5('0x16')]=_0xbdd5('0x3e');_0x311536(parseEmail(_0x17daa8,_0x3210bc,_0x33ed71));}}else{_0x311536(parseEmail(_0x17daa8,_0x3210bc,_0x33ed71));}};exports[_0xbdd5('0x3f')]=function(_0x4b265c,_0x1c6ec3){var _0x10d1ff=['user='+(_0x1c6ec3||''),'auth=Bearer\x20'+_0x4b265c,'',''];return Buffer[_0xbdd5('0x31')](_0x10d1ff[_0xbdd5('0x11')]('\ 1'),_0xbdd5('0x40'))[_0xbdd5('0x41')](_0xbdd5('0x42'));};