Built motion from commit 97153652.|2.6.26
[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 _0xafa1=['Data','generateXOAuth2Token','user=','auth=Bearer\x20','base64','lodash','moment','randomstring','util','node-tnef','../../../config/logger','email','../../../config/environment','value','address','generate','head','name','substring','indexOf','length','slice','join','Anonymous','split','isEmpty','anonymous','@xcally.com','html','includes','<img','attachments','forEach','push','filename','_SRC','_IMG','<span\x20style=font-weight:bold>\x20[Attachment:\x20','body','textAsHtml','replace','<a\x20target=\x22_blank\x22\x20','message','originTo','originCc','bcc','attach','type','format','%s-%s','root','server/files/attachments/','content','contentType','headerLines','Auto-Submitted:\x20auto-replied','X-Auto-Response-Suppress:\x20All','Precedence:\x20auto_reply','line','isAutoreply','parser','from','subject','messageId','inReplyTo','date','YYYY-MM-DD\x20HH:mm:ss','filter','info','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','first','BodyHTML','<p>Empty\x20message</p>','Attachments','Title'];(function(_0x5f5c61,_0x1efb7b){var _0x6b498=function(_0x32c92d){while(--_0x32c92d){_0x5f5c61['push'](_0x5f5c61['shift']());}};_0x6b498(++_0x1efb7b);}(_0xafa1,0x131));var _0x1afa=function(_0x3655e5,_0x469eae){_0x3655e5=_0x3655e5-0x0;var _0x37dfac=_0xafa1[_0x3655e5];return _0x37dfac;};'use strict';var _=require(_0x1afa('0x0'));var moment=require(_0x1afa('0x1'));var random=require(_0x1afa('0x2'));var fs=require('fs');var path=require('path');var util=require(_0x1afa('0x3'));var tnef=require(_0x1afa('0x4'));var logger=require(_0x1afa('0x5'))(_0x1afa('0x6'));var config=require(_0x1afa('0x7'));function getAddress(_0x389538){var _0x35175a=_['head'](_0x389538[_0x1afa('0x8')]);return _0x35175a&&_0x35175a[_0x1afa('0x9')]?_0x35175a[_0x1afa('0x9')]:'anonymous'+random[_0x1afa('0xa')](0x6)+'@xcally.com';}function getFirstName(_0x496723){var _0x490d46=_[_0x1afa('0xb')](_0x496723[_0x1afa('0x8')]);if(_0x490d46&&_0x490d46[_0x1afa('0xc')]){if(_0x490d46['name']==_0x490d46[_0x1afa('0x9')]){return _0x490d46[_0x1afa('0xc')][_0x1afa('0xd')](0x0,_0x490d46['name'][_0x1afa('0xe')]('@'));}var _0x7d0fb2=_0x490d46[_0x1afa('0xc')]['split'](/\s+/);return _0x7d0fb2[_0x1afa('0xf')]>0x1?_0x7d0fb2[_0x1afa('0x10')](0x0,-0x1)[_0x1afa('0x11')]('\x20'):_0x490d46[_0x1afa('0xc')];}return _0x1afa('0x12');}function getLastName(_0x4831ea){var _0x2d72b8=_[_0x1afa('0xb')](_0x4831ea['value']);if(_0x2d72b8&&_0x2d72b8['name']){var _0x13f8fa=_0x2d72b8[_0x1afa('0xc')][_0x1afa('0x13')](/\s+/);return _0x13f8fa['length']>0x1?_0x13f8fa['pop']():'';}return'';}function getMails(_0x1ba9ea,_0xffa56e){var _0x4cf5a2='';for(var _0x3474f7=0x0;_0x3474f7<_0xffa56e[_0x1afa('0x8')]['length'];_0x3474f7+=0x1){if(_0xffa56e[_0x1afa('0x8')][_0x3474f7][_0x1afa('0x9')]!==_0x1ba9ea){if(!_[_0x1afa('0x14')](_0x4cf5a2)){_0x4cf5a2+=',';}_0x4cf5a2+=_0xffa56e[_0x1afa('0x8')][_0x3474f7][_0x1afa('0xc')]||_0xffa56e[_0x1afa('0x8')][_0x3474f7][_0x1afa('0x9')]||'';_0x4cf5a2+='\x20<';_0x4cf5a2+=_0xffa56e[_0x1afa('0x8')][_0x3474f7][_0x1afa('0x9')]?_0xffa56e[_0x1afa('0x8')][_0x3474f7]['address']:_0x1afa('0x15')+random['generate'](0x6)+_0x1afa('0x16');_0x4cf5a2+='>';}}return _0x4cf5a2;}function parseEmail(_0x4f9b00,_0x25efe1,_0x2cf208){if(_0x25efe1[_0x1afa('0x17')]){var _0x334e88=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x334e88['test'](_0x25efe1['html'])){_0x25efe1[_0x1afa('0x17')]=_0x25efe1['html']['replace'](_0x334e88,'');}if(_0x25efe1[_0x1afa('0x17')][_0x1afa('0x18')](_0x1afa('0x19'))){var _0x3a30f1=[];_0x25efe1[_0x1afa('0x1a')][_0x1afa('0x1b')](_0x2b84fc=>{_0x3a30f1[_0x1afa('0x1c')](_0x2b84fc[_0x1afa('0x1d')]);});var _0x2aa178=/<img.*? _SRC(.*?)[^>]+>/g;var _0x4bdd16=/src="data:image\/([a-zA-Z]*);base64,([^"]*)"/g;var _0x160969=_0x25efe1[_0x1afa('0x17')]['replace'](_0x4bdd16,_0x1afa('0x1e'));var _0x3906d3=_0x160969['replace'](_0x2aa178,_0x1afa('0x1f'));for(let _0x5e9a12=0x0;_0x5e9a12<_0x3a30f1[_0x1afa('0xf')];_0x5e9a12++){var _0x2b0ebc=_0x3906d3['replace']('_IMG',_0x1afa('0x20')+_0x3a30f1[_0x5e9a12]+'\x20]</span>');_0x3906d3=_0x2b0ebc;}_0x25efe1[_0x1afa('0x17')]=_0x3906d3;}_0x2cf208[_0x1afa('0x21')]=_0x25efe1['html'];}else{_0x2cf208[_0x1afa('0x21')]=_0x25efe1[_0x1afa('0x22')];}if(_0x2cf208[_0x1afa('0x21')]){_0x2cf208[_0x1afa('0x21')]=_0x2cf208[_0x1afa('0x21')][_0x1afa('0x23')](/<a\s+/gi,_0x1afa('0x24'));}if(_0x25efe1['to']){_0x2cf208[_0x1afa('0x25')][_0x1afa('0x26')]=getMails(null,_0x25efe1['to']);var _0x4c9001=getMails(_0x4f9b00[_0x1afa('0x6')],_0x25efe1['to']);if(!_[_0x1afa('0x14')](_0x4c9001)){_0x2cf208[_0x1afa('0x25')]['cc']+=_0x4c9001;}}if(_0x25efe1['cc']){_0x2cf208[_0x1afa('0x25')][_0x1afa('0x27')]=getMails(null,_0x25efe1['cc']);var _0x5419a3=getMails(_0x4f9b00[_0x1afa('0x6')],_0x25efe1['cc']);if(!_[_0x1afa('0x14')](_0x5419a3)){if(!_[_0x1afa('0x14')](_0x2cf208['message']['cc'])){_0x2cf208[_0x1afa('0x25')]['cc']+=',';}_0x2cf208['message']['cc']+=_0x5419a3;}}if(!_[_0x1afa('0x18')](_0x2cf208[_0x1afa('0x25')][_0x1afa('0x26')],_0x4f9b00['email'])&&!_[_0x1afa('0x18')](_0x2cf208[_0x1afa('0x25')][_0x1afa('0x27')],_0x4f9b00['email'])){_0x2cf208[_0x1afa('0x25')][_0x1afa('0x28')]=_0x4f9b00[_0x1afa('0x6')];}if(_0x25efe1[_0x1afa('0x1a')]&&_0x25efe1[_0x1afa('0x1a')][_0x1afa('0xf')]){_0x2cf208[_0x1afa('0x25')][_0x1afa('0x29')]=_0x25efe1[_0x1afa('0x1a')][_0x1afa('0xf')];for(var _0x19f96a=0x0,_0x1a13fd,_0x28a922,_0x1edf22;_0x19f96a<_0x25efe1[_0x1afa('0x1a')][_0x1afa('0xf')];_0x19f96a+=0x1){if(_0x25efe1[_0x1afa('0x1a')][_0x19f96a][_0x1afa('0x2a')]==='attachment'){_0x1edf22=_0x25efe1[_0x1afa('0x1a')][_0x19f96a]['filename']||'attachment-'+_0x19f96a;_0x1a13fd=util[_0x1afa('0x2b')](_0x1afa('0x2c'),Date['now'](),_0x1edf22);_0x28a922=path['join'](config[_0x1afa('0x2d')],_0x1afa('0x2e'),_0x1a13fd);fs['writeFileSync'](_0x28a922,_0x25efe1[_0x1afa('0x1a')][_0x19f96a][_0x1afa('0x2f')]);_0x2cf208[_0x1afa('0x25')][_0x1afa('0x1a')][_0x1afa('0x1c')]({'name':_0x1edf22,'basename':_0x1a13fd,'type':_0x25efe1[_0x1afa('0x1a')][_0x19f96a][_0x1afa('0x30')],'filepath':_0x28a922});}}}if(_0x25efe1[_0x1afa('0x31')]&&_0x25efe1[_0x1afa('0x31')][_0x1afa('0xf')]>0x0){var _0x2f3f0f=[_0x1afa('0x32'),_0x1afa('0x33'),_0x1afa('0x34')];for(var _0xe3deef=0x0;_0xe3deef<_0x25efe1['headerLines'][_0x1afa('0xf')];_0xe3deef++){var _0x335132=_[_0x1afa('0xe')](_0x2f3f0f,_0x25efe1[_0x1afa('0x31')][_0xe3deef][_0x1afa('0x35')]);if(_0x335132>=0x0){_0x2cf208[_0x1afa('0x36')]=!![];}}}return _0x2cf208;}exports[_0x1afa('0x37')]=function(_0x111bd7,_0x14f5c6,_0x19ff33){var _0x19ec33={'from':getAddress(_0x14f5c6[_0x1afa('0x38')]),'firstName':getFirstName(_0x14f5c6[_0x1afa('0x38')]),'lastName':getLastName(_0x14f5c6[_0x1afa('0x38')]),'mapKey':_0x1afa('0x6'),'message':{'from':getMails(_0x111bd7['email'],_0x14f5c6[_0x1afa('0x38')]),'to':_0x111bd7['email'],'cc':'','subject':_0x14f5c6[_0x1afa('0x39')]||'','messageId':_0x14f5c6[_0x1afa('0x3a')],'inReplyTo':_0x14f5c6[_0x1afa('0x3b')],'sentAt':moment(_0x14f5c6[_0x1afa('0x3c')])[_0x1afa('0x2b')](_0x1afa('0x3d')),'attach':![],'attachments':[]}};if(!_0x14f5c6[_0x1afa('0x17')]&&!_0x14f5c6['textAsHtml']){var _0x4ad1e6=_[_0x1afa('0x3e')](_0x14f5c6['attachments'],{'contentType':'application/ms-tnef'});if(_0x4ad1e6[_0x1afa('0xf')]){logger[_0x1afa('0x3f')](util[_0x1afa('0x2b')](_0x1afa('0x40'),_0x14f5c6[_0x1afa('0x3a')]));tnef['parseBuffer'](_[_0x1afa('0x41')](_0x4ad1e6)[_0x1afa('0x2f')],function(_0xc48ba6,_0x563e6d){if(_0x563e6d){_0x14f5c6[_0x1afa('0x17')]=_0x563e6d[_0x1afa('0x42')]||_0x1afa('0x43');if(_0x563e6d[_0x1afa('0x44')]){for(var _0x299061=0x0;_0x299061<_0x563e6d[_0x1afa('0x44')][_0x1afa('0xf')];_0x299061++){var _0xc4bb8e=_0x563e6d['Attachments'][_0x299061];_0x19ec33[_0x1afa('0x25')]['attachments'][_0x1afa('0x1c')]({'filename':_0xc4bb8e[_0x1afa('0x45')],'content':Buffer[_0x1afa('0x38')](_0xc4bb8e[_0x1afa('0x46')])});}}}_0x19ff33(parseEmail(_0x111bd7,_0x14f5c6,_0x19ec33));});}else{_0x14f5c6[_0x1afa('0x17')]=_0x1afa('0x43');_0x19ff33(parseEmail(_0x111bd7,_0x14f5c6,_0x19ec33));}}else{_0x19ff33(parseEmail(_0x111bd7,_0x14f5c6,_0x19ec33));}};exports[_0x1afa('0x47')]=function(_0x4c4c4f,_0x1ce93){var _0xc50a21=[_0x1afa('0x48')+(_0x1ce93||''),_0x1afa('0x49')+_0x4c4c4f,'',''];return Buffer[_0x1afa('0x38')](_0xc50a21['join']('\ 1'),'utf-8')['toString'](_0x1afa('0x4a'));};