Built motion from commit d9028345.|2.5.43
[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 _0x700f=['outlook365','outlook.office365.com','127.0.0.1','generateXOAuth2Token','auth=Bearer\x20','utf-8','base64','lodash','moment','randomstring','path','../../../config/environment','head','value','address','anonymous','generate','@xcally.com','name','substring','indexOf','split','length','slice','join','pop','html','test','replace','includes','<img','attachments','forEach','push','filename','_SRC','_IMG','\x20]</span>','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','email','isEmpty','message','originTo','originCc','bcc','attach','type','attachment','attachment-','format','%s-%s','now','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','YYYY-MM-DD\x20HH:mm:ss','filter','application/ms-tnef','info','parseBuffer','first','BodyHTML','<p>Empty\x20message</p>','Attachments','Title','Data','getHost','gmail','imap.gmail.com','yahoo','hotmail','imap-mail.outlook.com'];(function(_0x210e35,_0x47aee6){var _0x231a2a=function(_0x5345f8){while(--_0x5345f8){_0x210e35['push'](_0x210e35['shift']());}};_0x231a2a(++_0x47aee6);}(_0x700f,0x1b5));var _0xf700=function(_0x367ef5,_0x1d72b8){_0x367ef5=_0x367ef5-0x0;var _0x31ff6e=_0x700f[_0x367ef5];return _0x31ff6e;};'use strict';var _=require(_0xf700('0x0'));var moment=require(_0xf700('0x1'));var random=require(_0xf700('0x2'));var fs=require('fs');var path=require(_0xf700('0x3'));var util=require('util');var tnef=require('node-tnef');var logger=require('../../../config/logger')('email');var config=require(_0xf700('0x4'));function getAddress(_0x1cf4e2){var _0x596e85=_[_0xf700('0x5')](_0x1cf4e2[_0xf700('0x6')]);return _0x596e85&&_0x596e85['address']?_0x596e85[_0xf700('0x7')]:_0xf700('0x8')+random[_0xf700('0x9')](0x6)+_0xf700('0xa');}function getFirstName(_0x574d8c){var _0x2ed151=_[_0xf700('0x5')](_0x574d8c[_0xf700('0x6')]);if(_0x2ed151&&_0x2ed151[_0xf700('0xb')]){if(_0x2ed151[_0xf700('0xb')]==_0x2ed151[_0xf700('0x7')]){return _0x2ed151['name'][_0xf700('0xc')](0x0,_0x2ed151[_0xf700('0xb')][_0xf700('0xd')]('@'));}var _0x20d142=_0x2ed151['name'][_0xf700('0xe')](/\s+/);return _0x20d142[_0xf700('0xf')]>0x1?_0x20d142[_0xf700('0x10')](0x0,-0x1)[_0xf700('0x11')]('\x20'):_0x2ed151[_0xf700('0xb')];}return'Anonymous';}function getLastName(_0x1a24ff){var _0x7f4204=_[_0xf700('0x5')](_0x1a24ff['value']);if(_0x7f4204&&_0x7f4204[_0xf700('0xb')]){var _0x54ecf8=_0x7f4204[_0xf700('0xb')]['split'](/\s+/);return _0x54ecf8['length']>0x1?_0x54ecf8[_0xf700('0x12')]():'';}return'';}function getMails(_0x4857c6,_0x1f54bb){var _0x57abe3='';for(var _0x27d0cb=0x0;_0x27d0cb<_0x1f54bb['value'][_0xf700('0xf')];_0x27d0cb+=0x1){if(_0x1f54bb[_0xf700('0x6')][_0x27d0cb][_0xf700('0x7')]!==_0x4857c6){if(!_['isEmpty'](_0x57abe3)){_0x57abe3+=',';}_0x57abe3+=_0x1f54bb[_0xf700('0x6')][_0x27d0cb][_0xf700('0xb')]||_0x1f54bb['value'][_0x27d0cb][_0xf700('0x7')]||'';_0x57abe3+='\x20<';_0x57abe3+=_0x1f54bb[_0xf700('0x6')][_0x27d0cb]['address']?_0x1f54bb[_0xf700('0x6')][_0x27d0cb][_0xf700('0x7')]:_0xf700('0x8')+random[_0xf700('0x9')](0x6)+'@xcally.com';_0x57abe3+='>';}}return _0x57abe3;}function parseEmail(_0x553d89,_0x39f6a8,_0x3bad64){if(_0x39f6a8[_0xf700('0x13')]){var _0x3204a0=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x3204a0[_0xf700('0x14')](_0x39f6a8['html'])){_0x39f6a8[_0xf700('0x13')]=_0x39f6a8['html'][_0xf700('0x15')](_0x3204a0,'');}if(_0x39f6a8[_0xf700('0x13')][_0xf700('0x16')](_0xf700('0x17'))){var _0xd5681e=[];_0x39f6a8[_0xf700('0x18')][_0xf700('0x19')](_0x23aadd=>{_0xd5681e[_0xf700('0x1a')](_0x23aadd[_0xf700('0x1b')]);});var _0x48d9c2=/<img.*? _SRC(.*?)[^>]+>/g;var _0x3d6771=/src="data:image\/([a-zA-Z]*);base64,([^"]*)"/g;var _0xe15aa8=_0x39f6a8[_0xf700('0x13')][_0xf700('0x15')](_0x3d6771,_0xf700('0x1c'));var _0x44f6db=_0xe15aa8[_0xf700('0x15')](_0x48d9c2,_0xf700('0x1d'));for(let _0x1b1506=0x0;_0x1b1506<_0xd5681e[_0xf700('0xf')];_0x1b1506++){var _0x42fd64=_0x44f6db[_0xf700('0x15')]('_IMG','<span\x20style=font-weight:bold>\x20[Attachment:\x20'+_0xd5681e[_0x1b1506]+_0xf700('0x1e'));_0x44f6db=_0x42fd64;}_0x39f6a8[_0xf700('0x13')]=_0x44f6db;}_0x3bad64[_0xf700('0x1f')]=_0x39f6a8[_0xf700('0x13')];}else{_0x3bad64[_0xf700('0x1f')]=_0x39f6a8[_0xf700('0x20')];}if(_0x3bad64[_0xf700('0x1f')]){_0x3bad64[_0xf700('0x1f')]=_0x3bad64['body'][_0xf700('0x15')](/<a\s+/gi,_0xf700('0x21'));}if(_0x39f6a8['to']){_0x3bad64['message']['originTo']=getMails(null,_0x39f6a8['to']);var _0x47cf6e=getMails(_0x553d89[_0xf700('0x22')],_0x39f6a8['to']);if(!_[_0xf700('0x23')](_0x47cf6e)){_0x3bad64[_0xf700('0x24')]['cc']+=_0x47cf6e;}}if(_0x39f6a8['cc']){_0x3bad64[_0xf700('0x24')]['originCc']=getMails(null,_0x39f6a8['cc']);var _0x53074f=getMails(_0x553d89[_0xf700('0x22')],_0x39f6a8['cc']);if(!_[_0xf700('0x23')](_0x53074f)){if(!_[_0xf700('0x23')](_0x3bad64['message']['cc'])){_0x3bad64[_0xf700('0x24')]['cc']+=',';}_0x3bad64['message']['cc']+=_0x53074f;}}if(!_['includes'](_0x3bad64[_0xf700('0x24')][_0xf700('0x25')],_0x553d89['email'])&&!_['includes'](_0x3bad64['message'][_0xf700('0x26')],_0x553d89[_0xf700('0x22')])){_0x3bad64[_0xf700('0x24')][_0xf700('0x27')]=_0x553d89[_0xf700('0x22')];}if(_0x39f6a8[_0xf700('0x18')]&&_0x39f6a8[_0xf700('0x18')][_0xf700('0xf')]){_0x3bad64['message'][_0xf700('0x28')]=_0x39f6a8[_0xf700('0x18')]['length'];for(var _0x57a97d=0x0,_0x146910,_0x39d290,_0x459827;_0x57a97d<_0x39f6a8[_0xf700('0x18')][_0xf700('0xf')];_0x57a97d+=0x1){if(_0x39f6a8[_0xf700('0x18')][_0x57a97d][_0xf700('0x29')]===_0xf700('0x2a')){_0x459827=_0x39f6a8[_0xf700('0x18')][_0x57a97d][_0xf700('0x1b')]||_0xf700('0x2b')+_0x57a97d;_0x146910=util[_0xf700('0x2c')](_0xf700('0x2d'),Date[_0xf700('0x2e')](),_0x459827);_0x39d290=path[_0xf700('0x11')](config[_0xf700('0x2f')],_0xf700('0x30'),_0x146910);fs['writeFileSync'](_0x39d290,_0x39f6a8['attachments'][_0x57a97d][_0xf700('0x31')]);_0x3bad64[_0xf700('0x24')]['attachments'][_0xf700('0x1a')]({'name':_0x459827,'basename':_0x146910,'type':_0x39f6a8[_0xf700('0x18')][_0x57a97d][_0xf700('0x32')],'filepath':_0x39d290});}}}if(_0x39f6a8[_0xf700('0x33')]&&_0x39f6a8[_0xf700('0x33')][_0xf700('0xf')]>0x0){var _0x5036f0=[_0xf700('0x34'),_0xf700('0x35'),_0xf700('0x36')];for(var _0x88856f=0x0;_0x88856f<_0x39f6a8[_0xf700('0x33')][_0xf700('0xf')];_0x88856f++){var _0x5a9e8f=_[_0xf700('0xd')](_0x5036f0,_0x39f6a8[_0xf700('0x33')][_0x88856f][_0xf700('0x37')]);if(_0x5a9e8f>=0x0){_0x3bad64[_0xf700('0x38')]=!![];}}}return _0x3bad64;}exports[_0xf700('0x39')]=function(_0x223dee,_0x5769c4,_0x59be47){var _0x3f685e={'from':getAddress(_0x5769c4[_0xf700('0x3a')]),'firstName':getFirstName(_0x5769c4[_0xf700('0x3a')]),'lastName':getLastName(_0x5769c4[_0xf700('0x3a')]),'mapKey':'email','message':{'from':getMails(_0x223dee[_0xf700('0x22')],_0x5769c4[_0xf700('0x3a')]),'to':_0x223dee[_0xf700('0x22')],'cc':'','subject':_0x5769c4[_0xf700('0x3b')]||'','messageId':_0x5769c4[_0xf700('0x3c')],'inReplyTo':_0x5769c4[_0xf700('0x3d')],'sentAt':moment(_0x5769c4['date'])[_0xf700('0x2c')](_0xf700('0x3e')),'attach':![],'attachments':[]}};if(!_0x5769c4[_0xf700('0x13')]&&!_0x5769c4[_0xf700('0x20')]){var _0x68735=_[_0xf700('0x3f')](_0x5769c4[_0xf700('0x18')],{'contentType':_0xf700('0x40')});if(_0x68735[_0xf700('0xf')]){logger[_0xf700('0x41')](util[_0xf700('0x2c')]('Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system',_0x5769c4[_0xf700('0x3c')]));tnef[_0xf700('0x42')](_[_0xf700('0x43')](_0x68735)[_0xf700('0x31')],function(_0x559f97,_0x1e90bf){if(_0x1e90bf){_0x5769c4['html']=_0x1e90bf[_0xf700('0x44')]||_0xf700('0x45');if(_0x1e90bf[_0xf700('0x46')]){for(var _0x3fd17b=0x0;_0x3fd17b<_0x1e90bf['Attachments']['length'];_0x3fd17b++){var _0x26a488=_0x1e90bf[_0xf700('0x46')][_0x3fd17b];_0x3f685e[_0xf700('0x24')][_0xf700('0x18')]['push']({'filename':_0x26a488[_0xf700('0x47')],'content':Buffer[_0xf700('0x3a')](_0x26a488[_0xf700('0x48')])});}}}_0x59be47(parseEmail(_0x223dee,_0x5769c4,_0x3f685e));});}else{_0x5769c4[_0xf700('0x13')]=_0xf700('0x45');_0x59be47(parseEmail(_0x223dee,_0x5769c4,_0x3f685e));}}else{_0x59be47(parseEmail(_0x223dee,_0x5769c4,_0x3f685e));}};exports[_0xf700('0x49')]=function(_0x228183){switch(_0x228183){case _0xf700('0x4a'):return _0xf700('0x4b');case _0xf700('0x4c'):return'imap.mail.yahoo.com';case _0xf700('0x4d'):return _0xf700('0x4e');case _0xf700('0x4f'):return _0xf700('0x50');}return _0xf700('0x51');};exports[_0xf700('0x52')]=function(_0x4da27e,_0x2a441e){var _0x455589=['user='+(_0x2a441e||''),_0xf700('0x53')+_0x4da27e,'',''];return Buffer[_0xf700('0x3a')](_0x455589[_0xf700('0x11')]('\ 1'),_0xf700('0x54'))['toString'](_0xf700('0x55'));};