abb381222707186f1ef5ec6dda180874d66c82a8
[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 _0x384d=['<p>Empty\x20message</p>','Attachments','Title','Data','user=','utf-8','toString','base64','lodash','randomstring','path','node-tnef','../../../config/logger','email','../../../config/environment','value','address','anonymous','generate','@xcally.com','name','indexOf','split','length','slice','join','Anonymous','pop','isEmpty','html','test','includes','<img','attachments','forEach','push','replace','_SRC','_IMG','<span\x20style=font-weight:bold>\x20[Attachment:\x20','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','message','originTo','originCc','type','attachment','filename','attachment-','format','%s-%s','root','server/files/attachments/','content','contentType','headerLines','Auto-Submitted:\x20auto-replied','Precedence:\x20auto_reply','parser','from','subject','messageId','inReplyTo','date','filter','application/ms-tnef','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','parseBuffer','first','BodyHTML'];(function(_0x456fb1,_0x859a1d){var _0x4083d6=function(_0xe1d0f2){while(--_0xe1d0f2){_0x456fb1['push'](_0x456fb1['shift']());}};_0x4083d6(++_0x859a1d);}(_0x384d,0x96));var _0xd384=function(_0x237bd2,_0x592caf){_0x237bd2=_0x237bd2-0x0;var _0x49e387=_0x384d[_0x237bd2];return _0x49e387;};'use strict';var _=require(_0xd384('0x0'));var moment=require('moment');var random=require(_0xd384('0x1'));var fs=require('fs');var path=require(_0xd384('0x2'));var util=require('util');var tnef=require(_0xd384('0x3'));var logger=require(_0xd384('0x4'))(_0xd384('0x5'));var config=require(_0xd384('0x6'));function getAddress(_0x4b4d57){var _0x3d529e=_['head'](_0x4b4d57[_0xd384('0x7')]);return _0x3d529e&&_0x3d529e[_0xd384('0x8')]?_0x3d529e[_0xd384('0x8')]:_0xd384('0x9')+random[_0xd384('0xa')](0x6)+_0xd384('0xb');}function getFirstName(_0x527aa2){var _0x382c31=_['head'](_0x527aa2['value']);if(_0x382c31&&_0x382c31[_0xd384('0xc')]){if(_0x382c31['name']==_0x382c31['address']){return _0x382c31[_0xd384('0xc')]['substring'](0x0,_0x382c31['name'][_0xd384('0xd')]('@'));}var _0x3d41e7=_0x382c31[_0xd384('0xc')][_0xd384('0xe')](/\s+/);return _0x3d41e7[_0xd384('0xf')]>0x1?_0x3d41e7[_0xd384('0x10')](0x0,-0x1)[_0xd384('0x11')]('\x20'):_0x382c31[_0xd384('0xc')];}return _0xd384('0x12');}function getLastName(_0x352140){var _0x479449=_['head'](_0x352140[_0xd384('0x7')]);if(_0x479449&&_0x479449[_0xd384('0xc')]){var _0x4bfe8f=_0x479449['name'][_0xd384('0xe')](/\s+/);return _0x4bfe8f[_0xd384('0xf')]>0x1?_0x4bfe8f[_0xd384('0x13')]():'';}return'';}function getMails(_0xe9be4e,_0x5958e2){var _0x3c0e35='';for(var _0xec4c21=0x0;_0xec4c21<_0x5958e2[_0xd384('0x7')][_0xd384('0xf')];_0xec4c21+=0x1){if(_0x5958e2[_0xd384('0x7')][_0xec4c21][_0xd384('0x8')]!==_0xe9be4e){if(!_[_0xd384('0x14')](_0x3c0e35)){_0x3c0e35+=',';}_0x3c0e35+=_0x5958e2[_0xd384('0x7')][_0xec4c21][_0xd384('0xc')]||_0x5958e2[_0xd384('0x7')][_0xec4c21][_0xd384('0x8')]||'';_0x3c0e35+='\x20<';_0x3c0e35+=_0x5958e2[_0xd384('0x7')][_0xec4c21][_0xd384('0x8')]?_0x5958e2[_0xd384('0x7')][_0xec4c21][_0xd384('0x8')]:'anonymous'+random[_0xd384('0xa')](0x6)+_0xd384('0xb');_0x3c0e35+='>';}}return _0x3c0e35;}function parseEmail(_0x5590d6,_0x1e5a14,_0x4a6a9b){if(_0x1e5a14[_0xd384('0x15')]){var _0x8a63d4=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x8a63d4[_0xd384('0x16')](_0x1e5a14[_0xd384('0x15')])){_0x1e5a14[_0xd384('0x15')]=_0x1e5a14[_0xd384('0x15')]['replace'](_0x8a63d4,'');}if(_0x1e5a14[_0xd384('0x15')][_0xd384('0x17')](_0xd384('0x18'))){var _0xffd1e3=[];_0x1e5a14[_0xd384('0x19')][_0xd384('0x1a')](_0x26410d=>{_0xffd1e3[_0xd384('0x1b')](_0x26410d['filename']);});var _0x1d07c0=/<img.*? _SRC(.*?)[^>]+>/g;var _0xe1313d=/src="data:image\/([a-zA-Z]*);base64,([^"]*)"/g;var _0x63a210=_0x1e5a14['html'][_0xd384('0x1c')](_0xe1313d,_0xd384('0x1d'));var _0x5291ee=_0x63a210[_0xd384('0x1c')](_0x1d07c0,_0xd384('0x1e'));for(let _0x2a976e=0x0;_0x2a976e<_0xffd1e3[_0xd384('0xf')];_0x2a976e++){var _0x49dc47=_0x5291ee['replace']('_IMG',_0xd384('0x1f')+_0xffd1e3[_0x2a976e]+'\x20]</span>');_0x5291ee=_0x49dc47;}_0x1e5a14['html']=_0x5291ee;}_0x4a6a9b['body']=_0x1e5a14[_0xd384('0x15')];}else{_0x4a6a9b[_0xd384('0x20')]=_0x1e5a14[_0xd384('0x21')];}if(_0x4a6a9b[_0xd384('0x20')]){_0x4a6a9b[_0xd384('0x20')]=_0x4a6a9b[_0xd384('0x20')]['replace'](/<a\s+/gi,_0xd384('0x22'));}if(_0x1e5a14['to']){_0x4a6a9b[_0xd384('0x23')][_0xd384('0x24')]=getMails(null,_0x1e5a14['to']);var _0x37d575=getMails(_0x5590d6[_0xd384('0x5')],_0x1e5a14['to']);if(!_[_0xd384('0x14')](_0x37d575)){_0x4a6a9b['message']['cc']+=_0x37d575;}}if(_0x1e5a14['cc']){_0x4a6a9b[_0xd384('0x23')][_0xd384('0x25')]=getMails(null,_0x1e5a14['cc']);var _0x8a7708=getMails(_0x5590d6[_0xd384('0x5')],_0x1e5a14['cc']);if(!_[_0xd384('0x14')](_0x8a7708)){if(!_[_0xd384('0x14')](_0x4a6a9b[_0xd384('0x23')]['cc'])){_0x4a6a9b[_0xd384('0x23')]['cc']+=',';}_0x4a6a9b['message']['cc']+=_0x8a7708;}}if(!_['includes'](_0x4a6a9b['message']['originTo'],_0x5590d6['email'])&&!_[_0xd384('0x17')](_0x4a6a9b[_0xd384('0x23')][_0xd384('0x25')],_0x5590d6[_0xd384('0x5')])){_0x4a6a9b[_0xd384('0x23')]['bcc']=_0x5590d6['email'];}if(_0x1e5a14['attachments']&&_0x1e5a14[_0xd384('0x19')][_0xd384('0xf')]){_0x4a6a9b[_0xd384('0x23')]['attach']=_0x1e5a14[_0xd384('0x19')][_0xd384('0xf')];for(var _0x16c5e1=0x0,_0x2c3268,_0x8239f9,_0x2134ef;_0x16c5e1<_0x1e5a14['attachments'][_0xd384('0xf')];_0x16c5e1+=0x1){if(_0x1e5a14['attachments'][_0x16c5e1][_0xd384('0x26')]===_0xd384('0x27')){_0x2134ef=_0x1e5a14[_0xd384('0x19')][_0x16c5e1][_0xd384('0x28')]||_0xd384('0x29')+_0x16c5e1;_0x2c3268=util[_0xd384('0x2a')](_0xd384('0x2b'),Date['now'](),_0x2134ef);_0x8239f9=path['join'](config[_0xd384('0x2c')],_0xd384('0x2d'),_0x2c3268);fs['writeFileSync'](_0x8239f9,_0x1e5a14[_0xd384('0x19')][_0x16c5e1][_0xd384('0x2e')]);_0x4a6a9b[_0xd384('0x23')][_0xd384('0x19')][_0xd384('0x1b')]({'name':_0x2134ef,'basename':_0x2c3268,'type':_0x1e5a14[_0xd384('0x19')][_0x16c5e1][_0xd384('0x2f')],'filepath':_0x8239f9});}}}if(_0x1e5a14['headerLines']&&_0x1e5a14[_0xd384('0x30')][_0xd384('0xf')]>0x0){var _0x5c7527=[_0xd384('0x31'),'X-Auto-Response-Suppress:\x20All',_0xd384('0x32')];for(var _0x28c45f=0x0;_0x28c45f<_0x1e5a14['headerLines'][_0xd384('0xf')];_0x28c45f++){var _0x46ad8e=_[_0xd384('0xd')](_0x5c7527,_0x1e5a14['headerLines'][_0x28c45f]['line']);if(_0x46ad8e>=0x0){_0x4a6a9b['isAutoreply']=!![];}}}return _0x4a6a9b;}exports[_0xd384('0x33')]=function(_0x12fdbb,_0x30f900,_0x186c11){var _0x2d9dbb={'from':getAddress(_0x30f900['from']),'firstName':getFirstName(_0x30f900[_0xd384('0x34')]),'lastName':getLastName(_0x30f900[_0xd384('0x34')]),'mapKey':'email','message':{'from':getMails(_0x12fdbb['email'],_0x30f900[_0xd384('0x34')]),'to':_0x12fdbb[_0xd384('0x5')],'cc':'','subject':_0x30f900[_0xd384('0x35')]||'','messageId':_0x30f900[_0xd384('0x36')],'inReplyTo':_0x30f900[_0xd384('0x37')],'sentAt':moment(_0x30f900[_0xd384('0x38')])[_0xd384('0x2a')]('YYYY-MM-DD\x20HH:mm:ss'),'attach':![],'attachments':[]}};if(!_0x30f900['html']&&!_0x30f900[_0xd384('0x21')]){var _0x5bf037=_[_0xd384('0x39')](_0x30f900[_0xd384('0x19')],{'contentType':_0xd384('0x3a')});if(_0x5bf037[_0xd384('0xf')]){logger['info'](util[_0xd384('0x2a')](_0xd384('0x3b'),_0x30f900[_0xd384('0x36')]));tnef[_0xd384('0x3c')](_[_0xd384('0x3d')](_0x5bf037)['content'],function(_0x3dae3b,_0x348e58){if(_0x348e58){_0x30f900['html']=_0x348e58[_0xd384('0x3e')]||_0xd384('0x3f');if(_0x348e58[_0xd384('0x40')]){for(var _0x578d9a=0x0;_0x578d9a<_0x348e58[_0xd384('0x40')][_0xd384('0xf')];_0x578d9a++){var _0x59fb3f=_0x348e58[_0xd384('0x40')][_0x578d9a];_0x2d9dbb[_0xd384('0x23')][_0xd384('0x19')][_0xd384('0x1b')]({'filename':_0x59fb3f[_0xd384('0x41')],'content':Buffer['from'](_0x59fb3f[_0xd384('0x42')])});}}}_0x186c11(parseEmail(_0x12fdbb,_0x30f900,_0x2d9dbb));});}else{_0x30f900[_0xd384('0x15')]='<p>Empty\x20message</p>';_0x186c11(parseEmail(_0x12fdbb,_0x30f900,_0x2d9dbb));}}else{_0x186c11(parseEmail(_0x12fdbb,_0x30f900,_0x2d9dbb));}};exports['generateXOAuth2Token']=function(_0xdefe29,_0x269e38){var _0x59f473=[_0xd384('0x43')+(_0x269e38||''),'auth=Bearer\x20'+_0xdefe29,'',''];return Buffer[_0xd384('0x34')](_0x59f473[_0xd384('0x11')]('\ 1'),_0xd384('0x44'))[_0xd384('0x45')](_0xd384('0x46'));};