a4b19986ce0958a4f51b8c1eb67852f5c83498f6
[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 _0xd46e=['<span\x20style=font-weight:bold>\x20[Attachment:\x20','\x20]</span>','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','message','originTo','email','originCc','bcc','type','attachment-','format','%s-%s','now','server/files/attachments/','writeFileSync','content','contentType','headerLines','Auto-Submitted:\x20auto-replied','X-Auto-Response-Suppress:\x20All','Precedence:\x20auto_reply','line','isAutoreply','parser','from','subject','inReplyTo','date','filter','application/ms-tnef','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','messageId','parseBuffer','first','BodyHTML','Attachments','Title','Data','<p>Empty\x20message</p>','generateXOAuth2Token','user=','auth=Bearer\x20','toString','base64','lodash','moment','path','node-tnef','../../../config/logger','../../../config/environment','head','value','address','anonymous','generate','@xcally.com','name','substring','indexOf','split','length','slice','join','Anonymous','pop','isEmpty','html','test','includes','<img','attachments','forEach','push','filename','replace','_IMG'];(function(_0x1beef3,_0x57c4b8){var _0x5af964=function(_0x36f463){while(--_0x36f463){_0x1beef3['push'](_0x1beef3['shift']());}};_0x5af964(++_0x57c4b8);}(_0xd46e,0x7c));var _0xed46=function(_0x199572,_0x33e667){_0x199572=_0x199572-0x0;var _0x13cfa3=_0xd46e[_0x199572];return _0x13cfa3;};'use strict';var _=require(_0xed46('0x0'));var moment=require(_0xed46('0x1'));var random=require('randomstring');var fs=require('fs');var path=require(_0xed46('0x2'));var util=require('util');var tnef=require(_0xed46('0x3'));var logger=require(_0xed46('0x4'))('email');var config=require(_0xed46('0x5'));function getAddress(_0xe65dd){var _0xb622ff=_[_0xed46('0x6')](_0xe65dd[_0xed46('0x7')]);return _0xb622ff&&_0xb622ff[_0xed46('0x8')]?_0xb622ff['address']:_0xed46('0x9')+random[_0xed46('0xa')](0x6)+_0xed46('0xb');}function getFirstName(_0xc97d3d){var _0x5f2fb7=_[_0xed46('0x6')](_0xc97d3d[_0xed46('0x7')]);if(_0x5f2fb7&&_0x5f2fb7[_0xed46('0xc')]){if(_0x5f2fb7[_0xed46('0xc')]==_0x5f2fb7[_0xed46('0x8')]){return _0x5f2fb7[_0xed46('0xc')][_0xed46('0xd')](0x0,_0x5f2fb7[_0xed46('0xc')][_0xed46('0xe')]('@'));}var _0xd5daa7=_0x5f2fb7[_0xed46('0xc')][_0xed46('0xf')](/\s+/);return _0xd5daa7[_0xed46('0x10')]>0x1?_0xd5daa7[_0xed46('0x11')](0x0,-0x1)[_0xed46('0x12')]('\x20'):_0x5f2fb7[_0xed46('0xc')];}return _0xed46('0x13');}function getLastName(_0x2c9163){var _0x4402d0=_[_0xed46('0x6')](_0x2c9163[_0xed46('0x7')]);if(_0x4402d0&&_0x4402d0[_0xed46('0xc')]){var _0x30a89f=_0x4402d0[_0xed46('0xc')][_0xed46('0xf')](/\s+/);return _0x30a89f['length']>0x1?_0x30a89f[_0xed46('0x14')]():'';}return'';}function getMails(_0x2b87eb,_0x5e91b6){var _0x55fbf2='';for(var _0x4d8e9f=0x0;_0x4d8e9f<_0x5e91b6[_0xed46('0x7')][_0xed46('0x10')];_0x4d8e9f+=0x1){if(_0x5e91b6[_0xed46('0x7')][_0x4d8e9f]['address']!==_0x2b87eb){if(!_[_0xed46('0x15')](_0x55fbf2)){_0x55fbf2+=',';}_0x55fbf2+=_0x5e91b6['value'][_0x4d8e9f]['name']||_0x5e91b6[_0xed46('0x7')][_0x4d8e9f][_0xed46('0x8')]||'';_0x55fbf2+='\x20<';_0x55fbf2+=_0x5e91b6[_0xed46('0x7')][_0x4d8e9f][_0xed46('0x8')]?_0x5e91b6[_0xed46('0x7')][_0x4d8e9f][_0xed46('0x8')]:_0xed46('0x9')+random[_0xed46('0xa')](0x6)+_0xed46('0xb');_0x55fbf2+='>';}}return _0x55fbf2;}function parseEmail(_0x1052e2,_0x25fc7e,_0x292b82){if(_0x25fc7e[_0xed46('0x16')]){var _0x1a8e42=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x1a8e42[_0xed46('0x17')](_0x25fc7e[_0xed46('0x16')])){_0x25fc7e[_0xed46('0x16')]=_0x25fc7e[_0xed46('0x16')]['replace'](_0x1a8e42,'');}if(_0x25fc7e[_0xed46('0x16')][_0xed46('0x18')](_0xed46('0x19'))){var _0x5e8d84=[];_0x25fc7e[_0xed46('0x1a')][_0xed46('0x1b')](_0x1715df=>{_0x5e8d84[_0xed46('0x1c')](_0x1715df[_0xed46('0x1d')]);});var _0x146fbc=/<img.*? _SRC(.*?)[^>]+>/g;var _0x1e6dcb=/src="data:image\/([a-zA-Z]*);base64,([^"]*)"/g;var _0x59bf36=_0x25fc7e[_0xed46('0x16')][_0xed46('0x1e')](_0x1e6dcb,'_SRC');var _0x5c2482=_0x59bf36[_0xed46('0x1e')](_0x146fbc,_0xed46('0x1f'));for(let _0x161da6=0x0;_0x161da6<_0x5e8d84['length'];_0x161da6++){var _0x261a32=_0x5c2482['replace'](_0xed46('0x1f'),_0xed46('0x20')+_0x5e8d84[_0x161da6]+_0xed46('0x21'));_0x5c2482=_0x261a32;}_0x25fc7e['html']=_0x5c2482;}_0x292b82[_0xed46('0x22')]=_0x25fc7e[_0xed46('0x16')];}else{_0x292b82[_0xed46('0x22')]=_0x25fc7e[_0xed46('0x23')];}if(_0x292b82['body']){_0x292b82[_0xed46('0x22')]=_0x292b82['body']['replace'](/<a\s+/gi,_0xed46('0x24'));}if(_0x25fc7e['to']){_0x292b82[_0xed46('0x25')][_0xed46('0x26')]=getMails(null,_0x25fc7e['to']);var _0x118c08=getMails(_0x1052e2[_0xed46('0x27')],_0x25fc7e['to']);if(!_['isEmpty'](_0x118c08)){_0x292b82[_0xed46('0x25')]['cc']+=_0x118c08;}}if(_0x25fc7e['cc']){_0x292b82[_0xed46('0x25')][_0xed46('0x28')]=getMails(null,_0x25fc7e['cc']);var _0x798ccd=getMails(_0x1052e2[_0xed46('0x27')],_0x25fc7e['cc']);if(!_[_0xed46('0x15')](_0x798ccd)){if(!_[_0xed46('0x15')](_0x292b82[_0xed46('0x25')]['cc'])){_0x292b82[_0xed46('0x25')]['cc']+=',';}_0x292b82['message']['cc']+=_0x798ccd;}}if(!_[_0xed46('0x18')](_0x292b82[_0xed46('0x25')][_0xed46('0x26')],_0x1052e2[_0xed46('0x27')])&&!_[_0xed46('0x18')](_0x292b82['message'][_0xed46('0x28')],_0x1052e2[_0xed46('0x27')])){_0x292b82[_0xed46('0x25')][_0xed46('0x29')]=_0x1052e2[_0xed46('0x27')];}if(_0x25fc7e[_0xed46('0x1a')]&&_0x25fc7e[_0xed46('0x1a')][_0xed46('0x10')]){_0x292b82[_0xed46('0x25')]['attach']=_0x25fc7e[_0xed46('0x1a')][_0xed46('0x10')];for(var _0x5235fb=0x0,_0x2d4930,_0x4006f7,_0x5b0da3;_0x5235fb<_0x25fc7e[_0xed46('0x1a')][_0xed46('0x10')];_0x5235fb+=0x1){if(_0x25fc7e[_0xed46('0x1a')][_0x5235fb][_0xed46('0x2a')]==='attachment'){_0x5b0da3=_0x25fc7e[_0xed46('0x1a')][_0x5235fb]['filename']||_0xed46('0x2b')+_0x5235fb;_0x2d4930=util[_0xed46('0x2c')](_0xed46('0x2d'),Date[_0xed46('0x2e')](),_0x5b0da3);_0x4006f7=path[_0xed46('0x12')](config['root'],_0xed46('0x2f'),_0x2d4930);fs[_0xed46('0x30')](_0x4006f7,_0x25fc7e[_0xed46('0x1a')][_0x5235fb][_0xed46('0x31')]);_0x292b82['message']['attachments'][_0xed46('0x1c')]({'name':_0x5b0da3,'basename':_0x2d4930,'type':_0x25fc7e[_0xed46('0x1a')][_0x5235fb][_0xed46('0x32')],'filepath':_0x4006f7});}}}if(_0x25fc7e[_0xed46('0x33')]&&_0x25fc7e[_0xed46('0x33')]['length']>0x0){var _0x4f71f2=[_0xed46('0x34'),_0xed46('0x35'),_0xed46('0x36')];for(var _0x76f36e=0x0;_0x76f36e<_0x25fc7e[_0xed46('0x33')][_0xed46('0x10')];_0x76f36e++){var _0x1de9df=_[_0xed46('0xe')](_0x4f71f2,_0x25fc7e[_0xed46('0x33')][_0x76f36e][_0xed46('0x37')]);if(_0x1de9df>=0x0){_0x292b82[_0xed46('0x38')]=!![];}}}return _0x292b82;}exports[_0xed46('0x39')]=function(_0x42d36e,_0x996e22,_0x9ec63c){var _0x345944={'from':getAddress(_0x996e22[_0xed46('0x3a')]),'firstName':getFirstName(_0x996e22[_0xed46('0x3a')]),'lastName':getLastName(_0x996e22[_0xed46('0x3a')]),'mapKey':_0xed46('0x27'),'message':{'from':getMails(_0x42d36e[_0xed46('0x27')],_0x996e22[_0xed46('0x3a')]),'to':_0x42d36e[_0xed46('0x27')],'cc':'','subject':_0x996e22[_0xed46('0x3b')]||'','messageId':_0x996e22['messageId'],'inReplyTo':_0x996e22[_0xed46('0x3c')],'sentAt':moment(_0x996e22[_0xed46('0x3d')])[_0xed46('0x2c')]('YYYY-MM-DD\x20HH:mm:ss'),'attach':![],'attachments':[]}};if(!_0x996e22['html']&&!_0x996e22['textAsHtml']){var _0x4087eb=_[_0xed46('0x3e')](_0x996e22['attachments'],{'contentType':_0xed46('0x3f')});if(_0x4087eb[_0xed46('0x10')]){logger['info'](util[_0xed46('0x2c')](_0xed46('0x40'),_0x996e22[_0xed46('0x41')]));tnef[_0xed46('0x42')](_[_0xed46('0x43')](_0x4087eb)[_0xed46('0x31')],function(_0x1c41a2,_0x2022cd){if(_0x2022cd){_0x996e22[_0xed46('0x16')]=_0x2022cd[_0xed46('0x44')]||'<p>Empty\x20message</p>';if(_0x2022cd['Attachments']){for(var _0x42fb83=0x0;_0x42fb83<_0x2022cd['Attachments']['length'];_0x42fb83++){var _0x322724=_0x2022cd[_0xed46('0x45')][_0x42fb83];_0x345944[_0xed46('0x25')][_0xed46('0x1a')][_0xed46('0x1c')]({'filename':_0x322724[_0xed46('0x46')],'content':Buffer[_0xed46('0x3a')](_0x322724[_0xed46('0x47')])});}}}_0x9ec63c(parseEmail(_0x42d36e,_0x996e22,_0x345944));});}else{_0x996e22['html']=_0xed46('0x48');_0x9ec63c(parseEmail(_0x42d36e,_0x996e22,_0x345944));}}else{_0x9ec63c(parseEmail(_0x42d36e,_0x996e22,_0x345944));}};exports[_0xed46('0x49')]=function(_0x2e9430,_0x116e44){var _0x3e01aa=[_0xed46('0x4a')+(_0x116e44||''),_0xed46('0x4b')+_0x2e9430,'',''];return Buffer[_0xed46('0x3a')](_0x3e01aa[_0xed46('0x12')]('\ 1'),'utf-8')[_0xed46('0x4c')](_0xed46('0x4d'));};