Built motion from commit 863c3375.|2.6.28
[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 _0x90f7=['Data','user=','auth=Bearer\x20','utf-8','toString','base64','lodash','moment','randomstring','path','node-tnef','email','../../../config/environment','head','value','address','anonymous','generate','@xcally.com','name','substring','indexOf','split','length','slice','Anonymous','pop','isEmpty','test','html','replace','includes','<img','attachments','forEach','_SRC','_IMG','<span\x20style=font-weight:bold>\x20[Attachment:\x20','textAsHtml','body','<a\x20target=\x22_blank\x22\x20','message','originTo','originCc','bcc','attach','type','filename','attachment-','format','%s-%s','join','server/files/attachments/','writeFileSync','content','push','contentType','headerLines','Auto-Submitted:\x20auto-replied','Precedence:\x20auto_reply','line','isAutoreply','parser','from','messageId','inReplyTo','YYYY-MM-DD\x20HH:mm:ss','filter','info','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','BodyHTML','<p>Empty\x20message</p>','Attachments','Title'];(function(_0x2dcfa7,_0x2e95d8){var _0x63b478=function(_0x32e8a2){while(--_0x32e8a2){_0x2dcfa7['push'](_0x2dcfa7['shift']());}};_0x63b478(++_0x2e95d8);}(_0x90f7,0x9a));var _0x790f=function(_0x276232,_0x20b6b8){_0x276232=_0x276232-0x0;var _0x427024=_0x90f7[_0x276232];return _0x427024;};'use strict';var _=require(_0x790f('0x0'));var moment=require(_0x790f('0x1'));var random=require(_0x790f('0x2'));var fs=require('fs');var path=require(_0x790f('0x3'));var util=require('util');var tnef=require(_0x790f('0x4'));var logger=require('../../../config/logger')(_0x790f('0x5'));var config=require(_0x790f('0x6'));function getAddress(_0x43e49b){var _0x312654=_[_0x790f('0x7')](_0x43e49b[_0x790f('0x8')]);return _0x312654&&_0x312654['address']?_0x312654[_0x790f('0x9')]:_0x790f('0xa')+random[_0x790f('0xb')](0x6)+_0x790f('0xc');}function getFirstName(_0x2c09fc){var _0x10d0c4=_[_0x790f('0x7')](_0x2c09fc[_0x790f('0x8')]);if(_0x10d0c4&&_0x10d0c4[_0x790f('0xd')]){if(_0x10d0c4[_0x790f('0xd')]==_0x10d0c4[_0x790f('0x9')]){return _0x10d0c4[_0x790f('0xd')][_0x790f('0xe')](0x0,_0x10d0c4['name'][_0x790f('0xf')]('@'));}var _0x971b63=_0x10d0c4[_0x790f('0xd')][_0x790f('0x10')](/\s+/);return _0x971b63[_0x790f('0x11')]>0x1?_0x971b63[_0x790f('0x12')](0x0,-0x1)['join']('\x20'):_0x10d0c4[_0x790f('0xd')];}return _0x790f('0x13');}function getLastName(_0x2a0d69){var _0x23d7fb=_[_0x790f('0x7')](_0x2a0d69[_0x790f('0x8')]);if(_0x23d7fb&&_0x23d7fb[_0x790f('0xd')]){var _0x553920=_0x23d7fb[_0x790f('0xd')]['split'](/\s+/);return _0x553920[_0x790f('0x11')]>0x1?_0x553920[_0x790f('0x14')]():'';}return'';}function getMails(_0xfbb459,_0x2b5559){var _0x30cf73='';for(var _0xb01670=0x0;_0xb01670<_0x2b5559[_0x790f('0x8')]['length'];_0xb01670+=0x1){if(_0x2b5559[_0x790f('0x8')][_0xb01670][_0x790f('0x9')]!==_0xfbb459){if(!_[_0x790f('0x15')](_0x30cf73)){_0x30cf73+=',';}_0x30cf73+=_0x2b5559[_0x790f('0x8')][_0xb01670][_0x790f('0xd')]||_0x2b5559[_0x790f('0x8')][_0xb01670][_0x790f('0x9')]||'';_0x30cf73+='\x20<';_0x30cf73+=_0x2b5559[_0x790f('0x8')][_0xb01670][_0x790f('0x9')]?_0x2b5559['value'][_0xb01670][_0x790f('0x9')]:'anonymous'+random[_0x790f('0xb')](0x6)+'@xcally.com';_0x30cf73+='>';}}return _0x30cf73;}function parseEmail(_0x305e83,_0x13d4c4,_0x5aa5b9){if(_0x13d4c4['html']){var _0x4c9ba1=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x4c9ba1[_0x790f('0x16')](_0x13d4c4[_0x790f('0x17')])){_0x13d4c4[_0x790f('0x17')]=_0x13d4c4[_0x790f('0x17')][_0x790f('0x18')](_0x4c9ba1,'');}if(_0x13d4c4[_0x790f('0x17')][_0x790f('0x19')](_0x790f('0x1a'))){var _0x5cba8f=[];_0x13d4c4[_0x790f('0x1b')][_0x790f('0x1c')](_0x5cddac=>{_0x5cba8f['push'](_0x5cddac['filename']);});var _0x2bacbb=/<img.*? _SRC(.*?)[^>]+>/g;var _0x472807=/src="data:image\/([a-zA-Z]*);base64,([^"]*)"/g;var _0x338f9d=_0x13d4c4['html'][_0x790f('0x18')](_0x472807,_0x790f('0x1d'));var _0x21b88d=_0x338f9d[_0x790f('0x18')](_0x2bacbb,_0x790f('0x1e'));for(let _0x446a88=0x0;_0x446a88<_0x5cba8f[_0x790f('0x11')];_0x446a88++){var _0x16162c=_0x21b88d[_0x790f('0x18')](_0x790f('0x1e'),_0x790f('0x1f')+_0x5cba8f[_0x446a88]+'\x20]</span>');_0x21b88d=_0x16162c;}_0x13d4c4[_0x790f('0x17')]=_0x21b88d;}_0x5aa5b9['body']=_0x13d4c4['html'];}else{_0x5aa5b9['body']=_0x13d4c4[_0x790f('0x20')];}if(_0x5aa5b9[_0x790f('0x21')]){_0x5aa5b9[_0x790f('0x21')]=_0x5aa5b9['body'][_0x790f('0x18')](/<a\s+/gi,_0x790f('0x22'));}if(_0x13d4c4['to']){_0x5aa5b9[_0x790f('0x23')][_0x790f('0x24')]=getMails(null,_0x13d4c4['to']);var _0x231346=getMails(_0x305e83[_0x790f('0x5')],_0x13d4c4['to']);if(!_['isEmpty'](_0x231346)){_0x5aa5b9['message']['cc']+=_0x231346;}}if(_0x13d4c4['cc']){_0x5aa5b9[_0x790f('0x23')][_0x790f('0x25')]=getMails(null,_0x13d4c4['cc']);var _0x27ab7a=getMails(_0x305e83['email'],_0x13d4c4['cc']);if(!_['isEmpty'](_0x27ab7a)){if(!_[_0x790f('0x15')](_0x5aa5b9['message']['cc'])){_0x5aa5b9[_0x790f('0x23')]['cc']+=',';}_0x5aa5b9[_0x790f('0x23')]['cc']+=_0x27ab7a;}}if(!_['includes'](_0x5aa5b9[_0x790f('0x23')][_0x790f('0x24')],_0x305e83[_0x790f('0x5')])&&!_['includes'](_0x5aa5b9[_0x790f('0x23')][_0x790f('0x25')],_0x305e83[_0x790f('0x5')])){_0x5aa5b9[_0x790f('0x23')][_0x790f('0x26')]=_0x305e83[_0x790f('0x5')];}if(_0x13d4c4[_0x790f('0x1b')]&&_0x13d4c4[_0x790f('0x1b')]['length']){_0x5aa5b9['message'][_0x790f('0x27')]=_0x13d4c4[_0x790f('0x1b')][_0x790f('0x11')];for(var _0x33f250=0x0,_0x1cab6b,_0x4265f9,_0x2e371d;_0x33f250<_0x13d4c4['attachments'][_0x790f('0x11')];_0x33f250+=0x1){if(_0x13d4c4[_0x790f('0x1b')][_0x33f250][_0x790f('0x28')]==='attachment'){_0x2e371d=_0x13d4c4[_0x790f('0x1b')][_0x33f250][_0x790f('0x29')]||_0x790f('0x2a')+_0x33f250;_0x1cab6b=util[_0x790f('0x2b')](_0x790f('0x2c'),Date['now'](),_0x2e371d);_0x4265f9=path[_0x790f('0x2d')](config['root'],_0x790f('0x2e'),_0x1cab6b);fs[_0x790f('0x2f')](_0x4265f9,_0x13d4c4[_0x790f('0x1b')][_0x33f250][_0x790f('0x30')]);_0x5aa5b9[_0x790f('0x23')]['attachments'][_0x790f('0x31')]({'name':_0x2e371d,'basename':_0x1cab6b,'type':_0x13d4c4['attachments'][_0x33f250][_0x790f('0x32')],'filepath':_0x4265f9});}}}if(_0x13d4c4[_0x790f('0x33')]&&_0x13d4c4[_0x790f('0x33')][_0x790f('0x11')]>0x0){var _0x98d6b1=[_0x790f('0x34'),'X-Auto-Response-Suppress:\x20All',_0x790f('0x35')];for(var _0x266f16=0x0;_0x266f16<_0x13d4c4[_0x790f('0x33')][_0x790f('0x11')];_0x266f16++){var _0x2ccd13=_[_0x790f('0xf')](_0x98d6b1,_0x13d4c4['headerLines'][_0x266f16][_0x790f('0x36')]);if(_0x2ccd13>=0x0){_0x5aa5b9[_0x790f('0x37')]=!![];}}}return _0x5aa5b9;}exports[_0x790f('0x38')]=function(_0x5f153a,_0x5a62ea,_0x7690f5){var _0x7b5f81={'from':getAddress(_0x5a62ea[_0x790f('0x39')]),'firstName':getFirstName(_0x5a62ea['from']),'lastName':getLastName(_0x5a62ea[_0x790f('0x39')]),'mapKey':_0x790f('0x5'),'message':{'from':getMails(_0x5f153a[_0x790f('0x5')],_0x5a62ea[_0x790f('0x39')]),'to':_0x5f153a['email'],'cc':'','subject':_0x5a62ea['subject']||'','messageId':_0x5a62ea[_0x790f('0x3a')],'inReplyTo':_0x5a62ea[_0x790f('0x3b')],'sentAt':moment(_0x5a62ea['date'])[_0x790f('0x2b')](_0x790f('0x3c')),'attach':![],'attachments':[]}};if(!_0x5a62ea[_0x790f('0x17')]&&!_0x5a62ea[_0x790f('0x20')]){var _0x36a85b=_[_0x790f('0x3d')](_0x5a62ea[_0x790f('0x1b')],{'contentType':'application/ms-tnef'});if(_0x36a85b[_0x790f('0x11')]){logger[_0x790f('0x3e')](util[_0x790f('0x2b')](_0x790f('0x3f'),_0x5a62ea['messageId']));tnef['parseBuffer'](_['first'](_0x36a85b)[_0x790f('0x30')],function(_0x42f51f,_0x3bc2fb){if(_0x3bc2fb){_0x5a62ea[_0x790f('0x17')]=_0x3bc2fb[_0x790f('0x40')]||_0x790f('0x41');if(_0x3bc2fb[_0x790f('0x42')]){for(var _0x3d8ae4=0x0;_0x3d8ae4<_0x3bc2fb['Attachments'][_0x790f('0x11')];_0x3d8ae4++){var _0x1e930c=_0x3bc2fb[_0x790f('0x42')][_0x3d8ae4];_0x7b5f81[_0x790f('0x23')][_0x790f('0x1b')]['push']({'filename':_0x1e930c[_0x790f('0x43')],'content':Buffer['from'](_0x1e930c[_0x790f('0x44')])});}}}_0x7690f5(parseEmail(_0x5f153a,_0x5a62ea,_0x7b5f81));});}else{_0x5a62ea[_0x790f('0x17')]=_0x790f('0x41');_0x7690f5(parseEmail(_0x5f153a,_0x5a62ea,_0x7b5f81));}}else{_0x7690f5(parseEmail(_0x5f153a,_0x5a62ea,_0x7b5f81));}};exports['generateXOAuth2Token']=function(_0xc02ed3,_0x387b2d){var _0x3236a1=[_0x790f('0x45')+(_0x387b2d||''),_0x790f('0x46')+_0xc02ed3,'',''];return Buffer['from'](_0x3236a1[_0x790f('0x2d')]('\ 1'),_0x790f('0x47'))[_0x790f('0x48')](_0x790f('0x49'));};