a81d82e1f9fd504a8e77e2428d3c9518f9bd6279
[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 _0x1d11=['head','name','substring','length','slice','join','Anonymous','split','pop','isEmpty','generate','html','test','replace','includes','<img','attachments','_SRC','_IMG','<span\x20style=font-weight:bold>\x20[Attachment:\x20','\x20]</span>','body','textAsHtml','message','originTo','email','originCc','bcc','attach','type','filename','attachment-','format','%s-%s','root','server/files/attachments/','writeFileSync','content','push','contentType','headerLines','Auto-Submitted:\x20auto-replied','X-Auto-Response-Suppress:\x20All','Precedence:\x20auto_reply','indexOf','line','isAutoreply','parser','from','messageId','inReplyTo','date','filter','application/ms-tnef','info','first','BodyHTML','<p>Empty\x20message</p>','Attachments','Title','Data','user=','utf-8','toString','lodash','moment','path','util','node-tnef','../../../config/logger','value','address','anonymous','@xcally.com'];(function(_0x5551b0,_0x3eb7ba){var _0x2b3fb1=function(_0x1375a0){while(--_0x1375a0){_0x5551b0['push'](_0x5551b0['shift']());}};_0x2b3fb1(++_0x3eb7ba);}(_0x1d11,0x8a));var _0x11d1=function(_0x12ccae,_0xb5e7f4){_0x12ccae=_0x12ccae-0x0;var _0x3c505a=_0x1d11[_0x12ccae];return _0x3c505a;};'use strict';var _=require(_0x11d1('0x0'));var moment=require(_0x11d1('0x1'));var random=require('randomstring');var fs=require('fs');var path=require(_0x11d1('0x2'));var util=require(_0x11d1('0x3'));var tnef=require(_0x11d1('0x4'));var logger=require(_0x11d1('0x5'))('email');var config=require('../../../config/environment');function getAddress(_0x2ea7c1){var _0x3779e4=_['head'](_0x2ea7c1[_0x11d1('0x6')]);return _0x3779e4&&_0x3779e4[_0x11d1('0x7')]?_0x3779e4[_0x11d1('0x7')]:_0x11d1('0x8')+random['generate'](0x6)+_0x11d1('0x9');}function getFirstName(_0x2d617f){var _0x447650=_[_0x11d1('0xa')](_0x2d617f[_0x11d1('0x6')]);if(_0x447650&&_0x447650[_0x11d1('0xb')]){if(_0x447650[_0x11d1('0xb')]==_0x447650[_0x11d1('0x7')]){return _0x447650[_0x11d1('0xb')][_0x11d1('0xc')](0x0,_0x447650[_0x11d1('0xb')]['indexOf']('@'));}var _0x4d1b98=_0x447650[_0x11d1('0xb')]['split'](/\s+/);return _0x4d1b98[_0x11d1('0xd')]>0x1?_0x4d1b98[_0x11d1('0xe')](0x0,-0x1)[_0x11d1('0xf')]('\x20'):_0x447650['name'];}return _0x11d1('0x10');}function getLastName(_0x10a4b6){var _0x4a132=_[_0x11d1('0xa')](_0x10a4b6[_0x11d1('0x6')]);if(_0x4a132&&_0x4a132['name']){var _0x375502=_0x4a132[_0x11d1('0xb')][_0x11d1('0x11')](/\s+/);return _0x375502[_0x11d1('0xd')]>0x1?_0x375502[_0x11d1('0x12')]():'';}return'';}function getMails(_0x50366b,_0x2fd082){var _0x36f2bf='';for(var _0x4e189b=0x0;_0x4e189b<_0x2fd082[_0x11d1('0x6')]['length'];_0x4e189b+=0x1){if(_0x2fd082[_0x11d1('0x6')][_0x4e189b][_0x11d1('0x7')]!==_0x50366b){if(!_[_0x11d1('0x13')](_0x36f2bf)){_0x36f2bf+=',';}_0x36f2bf+=_0x2fd082[_0x11d1('0x6')][_0x4e189b][_0x11d1('0xb')]||_0x2fd082[_0x11d1('0x6')][_0x4e189b][_0x11d1('0x7')]||'';_0x36f2bf+='\x20<';_0x36f2bf+=_0x2fd082['value'][_0x4e189b][_0x11d1('0x7')]?_0x2fd082['value'][_0x4e189b][_0x11d1('0x7')]:_0x11d1('0x8')+random[_0x11d1('0x14')](0x6)+'@xcally.com';_0x36f2bf+='>';}}return _0x36f2bf;}function parseEmail(_0x4ffd8b,_0x52a582,_0x9828a5){if(_0x52a582[_0x11d1('0x15')]){var _0x250e56=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x250e56[_0x11d1('0x16')](_0x52a582[_0x11d1('0x15')])){_0x52a582[_0x11d1('0x15')]=_0x52a582['html'][_0x11d1('0x17')](_0x250e56,'');}if(_0x52a582[_0x11d1('0x15')][_0x11d1('0x18')](_0x11d1('0x19'))){var _0x73c1f8=[];_0x52a582[_0x11d1('0x1a')]['forEach'](_0x26c2af=>{_0x73c1f8['push'](_0x26c2af['filename']);});var _0x550382=/<img.*? _SRC(.*?)[^>]+>/g;var _0x54be3f=/src="data:image\/([a-zA-Z]*);base64,([^"]*)"/g;var _0x31bbd6=_0x52a582[_0x11d1('0x15')]['replace'](_0x54be3f,_0x11d1('0x1b'));var _0x39c9fb=_0x31bbd6[_0x11d1('0x17')](_0x550382,_0x11d1('0x1c'));for(let _0x52311c=0x0;_0x52311c<_0x73c1f8[_0x11d1('0xd')];_0x52311c++){var _0x111b1a=_0x39c9fb[_0x11d1('0x17')](_0x11d1('0x1c'),_0x11d1('0x1d')+_0x73c1f8[_0x52311c]+_0x11d1('0x1e'));_0x39c9fb=_0x111b1a;}_0x52a582['html']=_0x39c9fb;}_0x9828a5[_0x11d1('0x1f')]=_0x52a582[_0x11d1('0x15')];}else{_0x9828a5[_0x11d1('0x1f')]=_0x52a582[_0x11d1('0x20')];}if(_0x9828a5[_0x11d1('0x1f')]){_0x9828a5['body']=_0x9828a5['body']['replace'](/<a\s+/gi,'<a\x20target=\x22_blank\x22\x20');}if(_0x52a582['to']){_0x9828a5[_0x11d1('0x21')][_0x11d1('0x22')]=getMails(null,_0x52a582['to']);var _0x128e3d=getMails(_0x4ffd8b[_0x11d1('0x23')],_0x52a582['to']);if(!_[_0x11d1('0x13')](_0x128e3d)){_0x9828a5['message']['cc']+=_0x128e3d;}}if(_0x52a582['cc']){_0x9828a5[_0x11d1('0x21')][_0x11d1('0x24')]=getMails(null,_0x52a582['cc']);var _0x53e9e3=getMails(_0x4ffd8b[_0x11d1('0x23')],_0x52a582['cc']);if(!_[_0x11d1('0x13')](_0x53e9e3)){if(!_['isEmpty'](_0x9828a5[_0x11d1('0x21')]['cc'])){_0x9828a5['message']['cc']+=',';}_0x9828a5['message']['cc']+=_0x53e9e3;}}if(!_[_0x11d1('0x18')](_0x9828a5[_0x11d1('0x21')]['originTo'],_0x4ffd8b[_0x11d1('0x23')])&&!_[_0x11d1('0x18')](_0x9828a5[_0x11d1('0x21')][_0x11d1('0x24')],_0x4ffd8b['email'])){_0x9828a5[_0x11d1('0x21')][_0x11d1('0x25')]=_0x4ffd8b[_0x11d1('0x23')];}if(_0x52a582[_0x11d1('0x1a')]&&_0x52a582[_0x11d1('0x1a')][_0x11d1('0xd')]){_0x9828a5['message'][_0x11d1('0x26')]=_0x52a582[_0x11d1('0x1a')][_0x11d1('0xd')];for(var _0x533d18=0x0,_0x96e06,_0xed4442,_0x25caa6;_0x533d18<_0x52a582['attachments'][_0x11d1('0xd')];_0x533d18+=0x1){if(_0x52a582[_0x11d1('0x1a')][_0x533d18][_0x11d1('0x27')]==='attachment'){_0x25caa6=_0x52a582[_0x11d1('0x1a')][_0x533d18][_0x11d1('0x28')]||_0x11d1('0x29')+_0x533d18;_0x96e06=util[_0x11d1('0x2a')](_0x11d1('0x2b'),Date['now'](),_0x25caa6);_0xed4442=path[_0x11d1('0xf')](config[_0x11d1('0x2c')],_0x11d1('0x2d'),_0x96e06);fs[_0x11d1('0x2e')](_0xed4442,_0x52a582['attachments'][_0x533d18][_0x11d1('0x2f')]);_0x9828a5[_0x11d1('0x21')][_0x11d1('0x1a')][_0x11d1('0x30')]({'name':_0x25caa6,'basename':_0x96e06,'type':_0x52a582[_0x11d1('0x1a')][_0x533d18][_0x11d1('0x31')],'filepath':_0xed4442});}}}if(_0x52a582[_0x11d1('0x32')]&&_0x52a582['headerLines'][_0x11d1('0xd')]>0x0){var _0x1b3c56=[_0x11d1('0x33'),_0x11d1('0x34'),_0x11d1('0x35')];for(var _0x1ac697=0x0;_0x1ac697<_0x52a582[_0x11d1('0x32')]['length'];_0x1ac697++){var _0x1c4947=_[_0x11d1('0x36')](_0x1b3c56,_0x52a582['headerLines'][_0x1ac697][_0x11d1('0x37')]);if(_0x1c4947>=0x0){_0x9828a5[_0x11d1('0x38')]=!![];}}}return _0x9828a5;}exports[_0x11d1('0x39')]=function(_0x4c6324,_0x1d06ea,_0x199fb6){var _0x55dee0={'from':getAddress(_0x1d06ea[_0x11d1('0x3a')]),'firstName':getFirstName(_0x1d06ea[_0x11d1('0x3a')]),'lastName':getLastName(_0x1d06ea[_0x11d1('0x3a')]),'mapKey':_0x11d1('0x23'),'message':{'from':getMails(_0x4c6324[_0x11d1('0x23')],_0x1d06ea['from']),'to':_0x4c6324[_0x11d1('0x23')],'cc':'','subject':_0x1d06ea['subject']||'','messageId':_0x1d06ea[_0x11d1('0x3b')],'inReplyTo':_0x1d06ea[_0x11d1('0x3c')],'sentAt':moment(_0x1d06ea[_0x11d1('0x3d')])['format']('YYYY-MM-DD\x20HH:mm:ss'),'attach':![],'attachments':[]}};if(!_0x1d06ea[_0x11d1('0x15')]&&!_0x1d06ea[_0x11d1('0x20')]){var _0x22611a=_[_0x11d1('0x3e')](_0x1d06ea[_0x11d1('0x1a')],{'contentType':_0x11d1('0x3f')});if(_0x22611a[_0x11d1('0xd')]){logger[_0x11d1('0x40')](util[_0x11d1('0x2a')]('Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system',_0x1d06ea[_0x11d1('0x3b')]));tnef['parseBuffer'](_[_0x11d1('0x41')](_0x22611a)[_0x11d1('0x2f')],function(_0x591df3,_0x2e2f7b){if(_0x2e2f7b){_0x1d06ea[_0x11d1('0x15')]=_0x2e2f7b[_0x11d1('0x42')]||_0x11d1('0x43');if(_0x2e2f7b[_0x11d1('0x44')]){for(var _0x334026=0x0;_0x334026<_0x2e2f7b[_0x11d1('0x44')][_0x11d1('0xd')];_0x334026++){var _0x42b2ec=_0x2e2f7b[_0x11d1('0x44')][_0x334026];_0x55dee0[_0x11d1('0x21')][_0x11d1('0x1a')]['push']({'filename':_0x42b2ec[_0x11d1('0x45')],'content':Buffer[_0x11d1('0x3a')](_0x42b2ec[_0x11d1('0x46')])});}}}_0x199fb6(parseEmail(_0x4c6324,_0x1d06ea,_0x55dee0));});}else{_0x1d06ea[_0x11d1('0x15')]=_0x11d1('0x43');_0x199fb6(parseEmail(_0x4c6324,_0x1d06ea,_0x55dee0));}}else{_0x199fb6(parseEmail(_0x4c6324,_0x1d06ea,_0x55dee0));}};exports['generateXOAuth2Token']=function(_0x2add84,_0x2b2700){var _0xcc8d21=[_0x11d1('0x47')+(_0x2b2700||''),'auth=Bearer\x20'+_0x2add84,'',''];return Buffer[_0x11d1('0x3a')](_0xcc8d21['join']('\ 1'),_0x11d1('0x48'))[_0x11d1('0x49')]('base64');};