Built motion from commit 3d6333b4.|2.6.31
[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 _0x6230=['attachment-','format','%s-%s','now','root','server/files/attachments/','writeFileSync','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','application/ms-tnef','parseBuffer','first','BodyHTML','Attachments','Title','Data','<p>Empty\x20message</p>','auth=Bearer\x20','utf-8','toString','base64','randomstring','util','node-tnef','../../../config/logger','../../../config/environment','head','value','address','anonymous','@xcally.com','name','substring','indexOf','length','slice','join','Anonymous','pop','generate','test','html','replace','<img','forEach','push','filename','_SRC','_IMG','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','originTo','email','isEmpty','message','originCc','includes','bcc','attachments','attach','attachment'];(function(_0xe73080,_0x24c56d){var _0x12cbc4=function(_0x1377e2){while(--_0x1377e2){_0xe73080['push'](_0xe73080['shift']());}};_0x12cbc4(++_0x24c56d);}(_0x6230,0x19f));var _0x0623=function(_0x43762c,_0x48ff0f){_0x43762c=_0x43762c-0x0;var _0x4a85e0=_0x6230[_0x43762c];return _0x4a85e0;};'use strict';var _=require('lodash');var moment=require('moment');var random=require(_0x0623('0x0'));var fs=require('fs');var path=require('path');var util=require(_0x0623('0x1'));var tnef=require(_0x0623('0x2'));var logger=require(_0x0623('0x3'))('email');var config=require(_0x0623('0x4'));function getAddress(_0x1d36a0){var _0x12a328=_[_0x0623('0x5')](_0x1d36a0[_0x0623('0x6')]);return _0x12a328&&_0x12a328[_0x0623('0x7')]?_0x12a328[_0x0623('0x7')]:_0x0623('0x8')+random['generate'](0x6)+_0x0623('0x9');}function getFirstName(_0x567cdf){var _0x1aeb84=_[_0x0623('0x5')](_0x567cdf[_0x0623('0x6')]);if(_0x1aeb84&&_0x1aeb84[_0x0623('0xa')]){if(_0x1aeb84[_0x0623('0xa')]==_0x1aeb84[_0x0623('0x7')]){return _0x1aeb84['name'][_0x0623('0xb')](0x0,_0x1aeb84[_0x0623('0xa')][_0x0623('0xc')]('@'));}var _0x1648f0=_0x1aeb84[_0x0623('0xa')]['split'](/\s+/);return _0x1648f0[_0x0623('0xd')]>0x1?_0x1648f0[_0x0623('0xe')](0x0,-0x1)[_0x0623('0xf')]('\x20'):_0x1aeb84[_0x0623('0xa')];}return _0x0623('0x10');}function getLastName(_0x14eacf){var _0xba13d1=_[_0x0623('0x5')](_0x14eacf[_0x0623('0x6')]);if(_0xba13d1&&_0xba13d1[_0x0623('0xa')]){var _0x4c05ea=_0xba13d1[_0x0623('0xa')]['split'](/\s+/);return _0x4c05ea[_0x0623('0xd')]>0x1?_0x4c05ea[_0x0623('0x11')]():'';}return'';}function getMails(_0xf8eed7,_0x5112ce){var _0x175bd4='';for(var _0x2ffabd=0x0;_0x2ffabd<_0x5112ce[_0x0623('0x6')][_0x0623('0xd')];_0x2ffabd+=0x1){if(_0x5112ce[_0x0623('0x6')][_0x2ffabd][_0x0623('0x7')]!==_0xf8eed7){if(!_['isEmpty'](_0x175bd4)){_0x175bd4+=',';}_0x175bd4+=_0x5112ce[_0x0623('0x6')][_0x2ffabd][_0x0623('0xa')]||_0x5112ce['value'][_0x2ffabd][_0x0623('0x7')]||'';_0x175bd4+='\x20<';_0x175bd4+=_0x5112ce[_0x0623('0x6')][_0x2ffabd]['address']?_0x5112ce[_0x0623('0x6')][_0x2ffabd][_0x0623('0x7')]:_0x0623('0x8')+random[_0x0623('0x12')](0x6)+_0x0623('0x9');_0x175bd4+='>';}}return _0x175bd4;}function parseEmail(_0x13995d,_0xf3c04c,_0x2e03d3){if(_0xf3c04c['html']){var _0x5315df=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x5315df[_0x0623('0x13')](_0xf3c04c['html'])){_0xf3c04c[_0x0623('0x14')]=_0xf3c04c[_0x0623('0x14')][_0x0623('0x15')](_0x5315df,'');}if(_0xf3c04c[_0x0623('0x14')]['includes'](_0x0623('0x16'))){var _0x6a41ef=[];_0xf3c04c['attachments'][_0x0623('0x17')](_0x136a1d=>{_0x6a41ef[_0x0623('0x18')](_0x136a1d[_0x0623('0x19')]);});var _0x1f77d0=/<img.*? _SRC(.*?)[^>]+>/g;var _0x46724a=/src="data:image\/([a-zA-Z]*);base64,([^"]*)"/g;var _0xc6950e=_0xf3c04c[_0x0623('0x14')][_0x0623('0x15')](_0x46724a,_0x0623('0x1a'));var _0x576d82=_0xc6950e[_0x0623('0x15')](_0x1f77d0,'_IMG');for(let _0x56a546=0x0;_0x56a546<_0x6a41ef[_0x0623('0xd')];_0x56a546++){var _0x45cc6c=_0x576d82[_0x0623('0x15')](_0x0623('0x1b'),'<span\x20style=font-weight:bold>\x20[Attachment:\x20'+_0x6a41ef[_0x56a546]+'\x20]</span>');_0x576d82=_0x45cc6c;}_0xf3c04c['html']=_0x576d82;}_0x2e03d3[_0x0623('0x1c')]=_0xf3c04c[_0x0623('0x14')];}else{_0x2e03d3[_0x0623('0x1c')]=_0xf3c04c[_0x0623('0x1d')];}if(_0x2e03d3[_0x0623('0x1c')]){_0x2e03d3[_0x0623('0x1c')]=_0x2e03d3['body'][_0x0623('0x15')](/<a\s+/gi,_0x0623('0x1e'));}if(_0xf3c04c['to']){_0x2e03d3['message'][_0x0623('0x1f')]=getMails(null,_0xf3c04c['to']);var _0x1e779f=getMails(_0x13995d[_0x0623('0x20')],_0xf3c04c['to']);if(!_[_0x0623('0x21')](_0x1e779f)){_0x2e03d3['message']['cc']+=_0x1e779f;}}if(_0xf3c04c['cc']){_0x2e03d3[_0x0623('0x22')][_0x0623('0x23')]=getMails(null,_0xf3c04c['cc']);var _0x387585=getMails(_0x13995d[_0x0623('0x20')],_0xf3c04c['cc']);if(!_[_0x0623('0x21')](_0x387585)){if(!_[_0x0623('0x21')](_0x2e03d3[_0x0623('0x22')]['cc'])){_0x2e03d3[_0x0623('0x22')]['cc']+=',';}_0x2e03d3[_0x0623('0x22')]['cc']+=_0x387585;}}if(!_[_0x0623('0x24')](_0x2e03d3[_0x0623('0x22')][_0x0623('0x1f')],_0x13995d['email'])&&!_[_0x0623('0x24')](_0x2e03d3[_0x0623('0x22')][_0x0623('0x23')],_0x13995d[_0x0623('0x20')])){_0x2e03d3[_0x0623('0x22')][_0x0623('0x25')]=_0x13995d[_0x0623('0x20')];}if(_0xf3c04c[_0x0623('0x26')]&&_0xf3c04c['attachments'][_0x0623('0xd')]){_0x2e03d3['message'][_0x0623('0x27')]=_0xf3c04c[_0x0623('0x26')][_0x0623('0xd')];for(var _0xfe328f=0x0,_0x41aa6c,_0x30566d,_0x46ea84;_0xfe328f<_0xf3c04c[_0x0623('0x26')][_0x0623('0xd')];_0xfe328f+=0x1){if(_0xf3c04c[_0x0623('0x26')][_0xfe328f]['type']===_0x0623('0x28')){_0x46ea84=_0xf3c04c[_0x0623('0x26')][_0xfe328f][_0x0623('0x19')]||_0x0623('0x29')+_0xfe328f;_0x41aa6c=util[_0x0623('0x2a')](_0x0623('0x2b'),Date[_0x0623('0x2c')](),_0x46ea84);_0x30566d=path[_0x0623('0xf')](config[_0x0623('0x2d')],_0x0623('0x2e'),_0x41aa6c);fs[_0x0623('0x2f')](_0x30566d,_0xf3c04c[_0x0623('0x26')][_0xfe328f][_0x0623('0x30')]);_0x2e03d3['message'][_0x0623('0x26')][_0x0623('0x18')]({'name':_0x46ea84,'basename':_0x41aa6c,'type':_0xf3c04c[_0x0623('0x26')][_0xfe328f][_0x0623('0x31')],'filepath':_0x30566d});}}}if(_0xf3c04c[_0x0623('0x32')]&&_0xf3c04c[_0x0623('0x32')]['length']>0x0){var _0x202e61=[_0x0623('0x33'),_0x0623('0x34'),_0x0623('0x35')];for(var _0x509cba=0x0;_0x509cba<_0xf3c04c[_0x0623('0x32')][_0x0623('0xd')];_0x509cba++){var _0x136fef=_[_0x0623('0xc')](_0x202e61,_0xf3c04c['headerLines'][_0x509cba][_0x0623('0x36')]);if(_0x136fef>=0x0){_0x2e03d3[_0x0623('0x37')]=!![];}}}return _0x2e03d3;}exports[_0x0623('0x38')]=function(_0x25b1e3,_0x487d58,_0x1fb657){var _0x4b4201={'from':getAddress(_0x487d58[_0x0623('0x39')]),'firstName':getFirstName(_0x487d58[_0x0623('0x39')]),'lastName':getLastName(_0x487d58[_0x0623('0x39')]),'mapKey':_0x0623('0x20'),'message':{'from':getMails(_0x25b1e3['email'],_0x487d58['from']),'to':_0x25b1e3[_0x0623('0x20')],'cc':'','subject':_0x487d58[_0x0623('0x3a')]||'','messageId':_0x487d58[_0x0623('0x3b')],'inReplyTo':_0x487d58[_0x0623('0x3c')],'sentAt':moment(_0x487d58[_0x0623('0x3d')])['format'](_0x0623('0x3e')),'attach':![],'attachments':[]}};if(!_0x487d58[_0x0623('0x14')]&&!_0x487d58[_0x0623('0x1d')]){var _0x4f4265=_[_0x0623('0x3f')](_0x487d58['attachments'],{'contentType':_0x0623('0x40')});if(_0x4f4265[_0x0623('0xd')]){logger['info'](util['format']('Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system',_0x487d58['messageId']));tnef[_0x0623('0x41')](_[_0x0623('0x42')](_0x4f4265)[_0x0623('0x30')],function(_0x22a097,_0x11af38){if(_0x11af38){_0x487d58[_0x0623('0x14')]=_0x11af38[_0x0623('0x43')]||'<p>Empty\x20message</p>';if(_0x11af38[_0x0623('0x44')]){for(var _0x2c9352=0x0;_0x2c9352<_0x11af38[_0x0623('0x44')]['length'];_0x2c9352++){var _0x2bf7b7=_0x11af38[_0x0623('0x44')][_0x2c9352];_0x4b4201['message']['attachments'][_0x0623('0x18')]({'filename':_0x2bf7b7[_0x0623('0x45')],'content':Buffer[_0x0623('0x39')](_0x2bf7b7[_0x0623('0x46')])});}}}_0x1fb657(parseEmail(_0x25b1e3,_0x487d58,_0x4b4201));});}else{_0x487d58[_0x0623('0x14')]=_0x0623('0x47');_0x1fb657(parseEmail(_0x25b1e3,_0x487d58,_0x4b4201));}}else{_0x1fb657(parseEmail(_0x25b1e3,_0x487d58,_0x4b4201));}};exports['generateXOAuth2Token']=function(_0x3a4c41,_0x26bcd5){var _0x5bd6d4=['user='+(_0x26bcd5||''),_0x0623('0x48')+_0x3a4c41,'',''];return Buffer[_0x0623('0x39')](_0x5bd6d4[_0x0623('0xf')]('\ 1'),_0x0623('0x49'))[_0x0623('0x4a')](_0x0623('0x4b'));};