Built motion from commit (unavailable).|2.5.30
[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 _0xb9fd=['format','now','join','root','writeFileSync','content','contentType','parser','from','subject','messageId','date','YYYY-MM-DD\x20HH:mm:ss','textAsHtml','application/ms-tnef','info','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','parseBuffer','first','BodyHTML','<p>Empty\x20message</p>','Attachments','Title','Data','getHost','gmail','imap.gmail.com','yahoo','hotmail','imap-mail.outlook.com','outlook365','outlook.office365.com','127.0.0.1','lodash','moment','path','util','node-tnef','../../../config/logger','email','../../../config/environment','head','value','address','anonymous','@xcally.com','name','split','length','Anonymous','pop','isEmpty','generate','test','html','replace','<img','forEach','push','_SRC','_IMG','\x20]</span>','body','<a\x20target=\x22_blank\x22\x20','originTo','message','originCc','includes','bcc','attachments','attach','type','attachment','filename'];(function(_0x1ddbba,_0xd5d49f){var _0x24eb80=function(_0x2dab26){while(--_0x2dab26){_0x1ddbba['push'](_0x1ddbba['shift']());}};_0x24eb80(++_0xd5d49f);}(_0xb9fd,0x149));var _0xdb9f=function(_0x2272b5,_0x362458){_0x2272b5=_0x2272b5-0x0;var _0x110156=_0xb9fd[_0x2272b5];return _0x110156;};'use strict';var _=require(_0xdb9f('0x0'));var moment=require(_0xdb9f('0x1'));var random=require('randomstring');var fs=require('fs');var path=require(_0xdb9f('0x2'));var util=require(_0xdb9f('0x3'));var tnef=require(_0xdb9f('0x4'));var logger=require(_0xdb9f('0x5'))(_0xdb9f('0x6'));var config=require(_0xdb9f('0x7'));function getAddress(_0x22e0f1){var _0x2ce53d=_[_0xdb9f('0x8')](_0x22e0f1[_0xdb9f('0x9')]);return _0x2ce53d&&_0x2ce53d[_0xdb9f('0xa')]?_0x2ce53d[_0xdb9f('0xa')]:_0xdb9f('0xb')+random['generate'](0x6)+_0xdb9f('0xc');}function getFirstName(_0x507587){var _0x57d686=_[_0xdb9f('0x8')](_0x507587['value']);if(_0x57d686&&_0x57d686['name']){var _0x51cf40=_0x57d686[_0xdb9f('0xd')][_0xdb9f('0xe')](/\s+/);return _0x51cf40[_0xdb9f('0xf')]?_0x51cf40['slice'](0x0,-0x1)['join']('\x20'):_0x57d686[_0xdb9f('0xd')];}return _0xdb9f('0x10');}function getLastName(_0x24d65f){var _0x3290f4=_[_0xdb9f('0x8')](_0x24d65f[_0xdb9f('0x9')]);if(_0x3290f4&&_0x3290f4['name']){var _0x11f474=_0x3290f4[_0xdb9f('0xd')][_0xdb9f('0xe')](/\s+/);return _0x11f474['length']>0x1?_0x11f474[_0xdb9f('0x11')]():'';}return'';}function getMails(_0x329e80,_0x14b41e){var _0xbd0c48='';for(var _0x4fe3ca=0x0;_0x4fe3ca<_0x14b41e[_0xdb9f('0x9')][_0xdb9f('0xf')];_0x4fe3ca+=0x1){if(_0x14b41e[_0xdb9f('0x9')][_0x4fe3ca][_0xdb9f('0xa')]!==_0x329e80){if(!_[_0xdb9f('0x12')](_0xbd0c48)){_0xbd0c48+=',';}_0xbd0c48+=_0x14b41e['value'][_0x4fe3ca][_0xdb9f('0xd')]||_0x14b41e[_0xdb9f('0x9')][_0x4fe3ca]['address']||'';_0xbd0c48+='\x20<';_0xbd0c48+=_0x14b41e[_0xdb9f('0x9')][_0x4fe3ca][_0xdb9f('0xa')]?_0x14b41e['value'][_0x4fe3ca][_0xdb9f('0xa')]:'anonymous'+random[_0xdb9f('0x13')](0x6)+'@xcally.com';_0xbd0c48+='>';}}return _0xbd0c48;}function parseEmail(_0x105c92,_0x3f02f6,_0xe8bbce){if(_0x3f02f6['html']){var _0x51f1f5=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x51f1f5[_0xdb9f('0x14')](_0x3f02f6[_0xdb9f('0x15')])){_0x3f02f6['html']=_0x3f02f6[_0xdb9f('0x15')][_0xdb9f('0x16')](_0x51f1f5,'');}if(_0x3f02f6[_0xdb9f('0x15')]['includes'](_0xdb9f('0x17'))){var _0x3d557b=[];_0x3f02f6['attachments'][_0xdb9f('0x18')](_0x260f71=>{_0x3d557b[_0xdb9f('0x19')](_0x260f71['filename']);});var _0x3b64de=/<img.*? _SRC(.*?)[^\>]+>/g;var _0x161367=/src=\"data:image\/([a-zA-Z]*);base64,([^\"]*)\"/g;var _0x13f6c0=_0x3f02f6[_0xdb9f('0x15')][_0xdb9f('0x16')](_0x161367,_0xdb9f('0x1a'));var _0x3b5f32=_0x13f6c0['replace'](_0x3b64de,_0xdb9f('0x1b'));for(let _0x2590d3=0x0;_0x2590d3<_0x3d557b[_0xdb9f('0xf')];_0x2590d3++){var _0x34d02e=_0x3b5f32[_0xdb9f('0x16')]('_IMG','<span\x20style=font-weight:bold>\x20[Attachment:\x20'+_0x3d557b[_0x2590d3]+_0xdb9f('0x1c'));_0x3b5f32=_0x34d02e;}_0x3f02f6[_0xdb9f('0x15')]=_0x3b5f32;}_0xe8bbce[_0xdb9f('0x1d')]=_0x3f02f6[_0xdb9f('0x15')];}else{_0xe8bbce[_0xdb9f('0x1d')]=_0x3f02f6['textAsHtml'];}if(_0xe8bbce[_0xdb9f('0x1d')]){_0xe8bbce[_0xdb9f('0x1d')]=_0xe8bbce[_0xdb9f('0x1d')][_0xdb9f('0x16')](/<a\s+/gi,_0xdb9f('0x1e'));}if(_0x3f02f6['to']){_0xe8bbce['message'][_0xdb9f('0x1f')]=getMails(null,_0x3f02f6['to']);var _0x2e05d7=getMails(_0x105c92[_0xdb9f('0x6')],_0x3f02f6['to']);if(!_[_0xdb9f('0x12')](_0x2e05d7)){_0xe8bbce[_0xdb9f('0x20')]['cc']+=_0x2e05d7;}}if(_0x3f02f6['cc']){_0xe8bbce[_0xdb9f('0x20')][_0xdb9f('0x21')]=getMails(null,_0x3f02f6['cc']);var _0x271692=getMails(_0x105c92[_0xdb9f('0x6')],_0x3f02f6['cc']);if(!_[_0xdb9f('0x12')](_0x271692)){if(!_['isEmpty'](_0xe8bbce[_0xdb9f('0x20')]['cc'])){_0xe8bbce[_0xdb9f('0x20')]['cc']+=',';}_0xe8bbce[_0xdb9f('0x20')]['cc']+=_0x271692;}}if(!_['includes'](_0xe8bbce[_0xdb9f('0x20')][_0xdb9f('0x1f')],_0x105c92['email'])&&!_[_0xdb9f('0x22')](_0xe8bbce[_0xdb9f('0x20')][_0xdb9f('0x21')],_0x105c92[_0xdb9f('0x6')])){_0xe8bbce[_0xdb9f('0x20')][_0xdb9f('0x23')]=_0x105c92[_0xdb9f('0x6')];}if(_0x3f02f6[_0xdb9f('0x24')]&&_0x3f02f6[_0xdb9f('0x24')][_0xdb9f('0xf')]){_0xe8bbce[_0xdb9f('0x20')][_0xdb9f('0x25')]=_0x3f02f6[_0xdb9f('0x24')][_0xdb9f('0xf')];for(var _0x685779=0x0,_0x299c11,_0x214a8d,_0x127fce;_0x685779<_0x3f02f6['attachments'][_0xdb9f('0xf')];_0x685779+=0x1){if(_0x3f02f6['attachments'][_0x685779][_0xdb9f('0x26')]===_0xdb9f('0x27')){_0x127fce=_0x3f02f6[_0xdb9f('0x24')][_0x685779][_0xdb9f('0x28')]||'attachment-'+_0x685779;_0x299c11=util[_0xdb9f('0x29')]('%s-%s',Date[_0xdb9f('0x2a')](),_0x127fce);_0x214a8d=path[_0xdb9f('0x2b')](config[_0xdb9f('0x2c')],'server/files/attachments/',_0x299c11);fs[_0xdb9f('0x2d')](_0x214a8d,_0x3f02f6[_0xdb9f('0x24')][_0x685779][_0xdb9f('0x2e')]);_0xe8bbce['message'][_0xdb9f('0x24')][_0xdb9f('0x19')]({'name':_0x127fce,'basename':_0x299c11,'type':_0x3f02f6['attachments'][_0x685779][_0xdb9f('0x2f')],'filepath':_0x214a8d});}}}return _0xe8bbce;}exports[_0xdb9f('0x30')]=function(_0x449493,_0x3feb64,_0x24b557){var _0x4572d5={'from':getAddress(_0x3feb64[_0xdb9f('0x31')]),'firstName':getFirstName(_0x3feb64['from']),'lastName':getLastName(_0x3feb64[_0xdb9f('0x31')]),'mapKey':_0xdb9f('0x6'),'message':{'from':getMails(_0x449493[_0xdb9f('0x6')],_0x3feb64['from']),'to':_0x449493[_0xdb9f('0x6')],'cc':'','subject':_0x3feb64[_0xdb9f('0x32')]||'','messageId':_0x3feb64[_0xdb9f('0x33')],'inReplyTo':_0x3feb64['inReplyTo'],'sentAt':moment(_0x3feb64[_0xdb9f('0x34')])[_0xdb9f('0x29')](_0xdb9f('0x35')),'attach':![],'attachments':[]}};if(!_0x3feb64[_0xdb9f('0x15')]&&!_0x3feb64[_0xdb9f('0x36')]){var _0x2b5c31=_['filter'](_0x3feb64[_0xdb9f('0x24')],{'contentType':_0xdb9f('0x37')});if(_0x2b5c31[_0xdb9f('0xf')]){logger[_0xdb9f('0x38')](util[_0xdb9f('0x29')](_0xdb9f('0x39'),_0x3feb64[_0xdb9f('0x33')]));tnef[_0xdb9f('0x3a')](_[_0xdb9f('0x3b')](_0x2b5c31)[_0xdb9f('0x2e')],function(_0x345704,_0x2cdbd2){if(_0x2cdbd2){_0x3feb64[_0xdb9f('0x15')]=_0x2cdbd2[_0xdb9f('0x3c')]||_0xdb9f('0x3d');if(_0x2cdbd2['Attachments']){for(var _0x5cc1ee=0x0;_0x5cc1ee<_0x2cdbd2[_0xdb9f('0x3e')][_0xdb9f('0xf')];_0x5cc1ee++){var _0x5bfd53=_0x2cdbd2['Attachments'][_0x5cc1ee];_0x4572d5[_0xdb9f('0x20')][_0xdb9f('0x24')][_0xdb9f('0x19')]({'filename':_0x5bfd53[_0xdb9f('0x3f')],'content':new Buffer(_0x5bfd53[_0xdb9f('0x40')])});}}}_0x24b557(parseEmail(_0x449493,_0x3feb64,_0x4572d5));});}else{_0x3feb64[_0xdb9f('0x15')]='<p>Empty\x20message</p>';_0x24b557(parseEmail(_0x449493,_0x3feb64,_0x4572d5));}}else{_0x24b557(parseEmail(_0x449493,_0x3feb64,_0x4572d5));}};exports[_0xdb9f('0x41')]=function(_0x4479d6){switch(_0x4479d6){case _0xdb9f('0x42'):return _0xdb9f('0x43');case _0xdb9f('0x44'):return'imap.mail.yahoo.com';case _0xdb9f('0x45'):return _0xdb9f('0x46');case _0xdb9f('0x47'):return _0xdb9f('0x48');}return _0xdb9f('0x49');};