4af5bb88cbcb4b6d0cf04cd422ca108038c55d5b
[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 _0x7ecc=['filename','replace','_SRC','_IMG','\x20]</span>','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','message','originTo','originCc','includes','bcc','attachment','format','now','root','writeFileSync','content','contentType','parser','from','subject','messageId','inReplyTo','YYYY-MM-DD\x20HH:mm:ss','filter','application/ms-tnef','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','parseBuffer','BodyHTML','<p>Empty\x20message</p>','Attachments','Title','getHost','gmail','imap.mail.yahoo.com','hotmail','imap-mail.outlook.com','outlook.office365.com','127.0.0.1','moment','util','node-tnef','email','../../../config/environment','value','address','anonymous','@xcally.com','head','name','split','length','slice','join','pop','isEmpty','generate','test','html','<img','attachments','forEach','push'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0x7ecc,0xec));var _0xc7ec=function(_0x14c95c,_0x63f180){_0x14c95c=_0x14c95c-0x0;var _0x566209=_0x7ecc[_0x14c95c];return _0x566209;};'use strict';var _=require('lodash');var moment=require(_0xc7ec('0x0'));var random=require('randomstring');var fs=require('fs');var path=require('path');var util=require(_0xc7ec('0x1'));var tnef=require(_0xc7ec('0x2'));var logger=require('../../../config/logger')(_0xc7ec('0x3'));var config=require(_0xc7ec('0x4'));function getAddress(_0x4ad64d){var _0xc0d34d=_['head'](_0x4ad64d[_0xc7ec('0x5')]);return _0xc0d34d&&_0xc0d34d[_0xc7ec('0x6')]?_0xc0d34d[_0xc7ec('0x6')]:_0xc7ec('0x7')+random['generate'](0x6)+_0xc7ec('0x8');}function getFirstName(_0x5e01ca){var _0x3d0f3c=_[_0xc7ec('0x9')](_0x5e01ca[_0xc7ec('0x5')]);if(_0x3d0f3c&&_0x3d0f3c[_0xc7ec('0xa')]){var _0x41e73e=_0x3d0f3c[_0xc7ec('0xa')][_0xc7ec('0xb')](/\s+/);return _0x41e73e[_0xc7ec('0xc')]?_0x41e73e[_0xc7ec('0xd')](0x0,-0x1)[_0xc7ec('0xe')]('\x20'):_0x3d0f3c[_0xc7ec('0xa')];}return'Anonymous';}function getLastName(_0x5856fa){var _0x1e2273=_[_0xc7ec('0x9')](_0x5856fa[_0xc7ec('0x5')]);if(_0x1e2273&&_0x1e2273[_0xc7ec('0xa')]){var _0x56c54d=_0x1e2273[_0xc7ec('0xa')][_0xc7ec('0xb')](/\s+/);return _0x56c54d[_0xc7ec('0xc')]>0x1?_0x56c54d[_0xc7ec('0xf')]():'';}return'';}function getMails(_0x4079af,_0x4c5f48){var _0x53de13='';for(var _0x27f9ee=0x0;_0x27f9ee<_0x4c5f48[_0xc7ec('0x5')][_0xc7ec('0xc')];_0x27f9ee+=0x1){if(_0x4c5f48[_0xc7ec('0x5')][_0x27f9ee]['address']!==_0x4079af){if(!_[_0xc7ec('0x10')](_0x53de13)){_0x53de13+=',';}_0x53de13+=_0x4c5f48['value'][_0x27f9ee][_0xc7ec('0xa')]||_0x4c5f48['value'][_0x27f9ee]['address']||'';_0x53de13+='\x20<';_0x53de13+=_0x4c5f48[_0xc7ec('0x5')][_0x27f9ee][_0xc7ec('0x6')]?_0x4c5f48[_0xc7ec('0x5')][_0x27f9ee][_0xc7ec('0x6')]:_0xc7ec('0x7')+random[_0xc7ec('0x11')](0x6)+'@xcally.com';_0x53de13+='>';}}return _0x53de13;}function parseEmail(_0xe34f0,_0x537acf,_0x5edde1){if(_0x537acf['html']){var _0xb223bb=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0xb223bb[_0xc7ec('0x12')](_0x537acf[_0xc7ec('0x13')])){_0x537acf['html']=_0x537acf['html']['replace'](_0xb223bb,'');}if(_0x537acf[_0xc7ec('0x13')]['includes'](_0xc7ec('0x14'))){var _0x1b24ca=[];_0x537acf[_0xc7ec('0x15')][_0xc7ec('0x16')](_0x4e5c13=>{_0x1b24ca[_0xc7ec('0x17')](_0x4e5c13[_0xc7ec('0x18')]);});var _0x567ea6=/<img.*? _SRC(.*?)[^\>]+>/g;var _0xf80e43=/src=\"data:image\/([a-zA-Z]*);base64,([^\"]*)\"/g;var _0x57b8d6=_0x537acf[_0xc7ec('0x13')][_0xc7ec('0x19')](_0xf80e43,_0xc7ec('0x1a'));var _0x4a913e=_0x57b8d6[_0xc7ec('0x19')](_0x567ea6,_0xc7ec('0x1b'));for(let _0xe9dd08=0x0;_0xe9dd08<_0x1b24ca[_0xc7ec('0xc')];_0xe9dd08++){var _0x4af078=_0x4a913e['replace']('_IMG','<span\x20style=font-weight:bold>\x20[Attachment:\x20'+_0x1b24ca[_0xe9dd08]+_0xc7ec('0x1c'));_0x4a913e=_0x4af078;}_0x537acf[_0xc7ec('0x13')]=_0x4a913e;}_0x5edde1[_0xc7ec('0x1d')]=_0x537acf[_0xc7ec('0x13')];}else{_0x5edde1[_0xc7ec('0x1d')]=_0x537acf[_0xc7ec('0x1e')];}if(_0x5edde1['body']){_0x5edde1[_0xc7ec('0x1d')]=_0x5edde1[_0xc7ec('0x1d')][_0xc7ec('0x19')](/<a\s+/gi,_0xc7ec('0x1f'));}if(_0x537acf['to']){_0x5edde1[_0xc7ec('0x20')][_0xc7ec('0x21')]=getMails(null,_0x537acf['to']);var _0x4b5176=getMails(_0xe34f0[_0xc7ec('0x3')],_0x537acf['to']);if(!_['isEmpty'](_0x4b5176)){_0x5edde1[_0xc7ec('0x20')]['cc']+=_0x4b5176;}}if(_0x537acf['cc']){_0x5edde1[_0xc7ec('0x20')][_0xc7ec('0x22')]=getMails(null,_0x537acf['cc']);var _0x819eac=getMails(_0xe34f0['email'],_0x537acf['cc']);if(!_[_0xc7ec('0x10')](_0x819eac)){if(!_['isEmpty'](_0x5edde1['message']['cc'])){_0x5edde1[_0xc7ec('0x20')]['cc']+=',';}_0x5edde1[_0xc7ec('0x20')]['cc']+=_0x819eac;}}if(!_[_0xc7ec('0x23')](_0x5edde1[_0xc7ec('0x20')]['originTo'],_0xe34f0[_0xc7ec('0x3')])&&!_[_0xc7ec('0x23')](_0x5edde1[_0xc7ec('0x20')][_0xc7ec('0x22')],_0xe34f0[_0xc7ec('0x3')])){_0x5edde1[_0xc7ec('0x20')][_0xc7ec('0x24')]=_0xe34f0[_0xc7ec('0x3')];}if(_0x537acf['attachments']&&_0x537acf['attachments']['length']){_0x5edde1[_0xc7ec('0x20')]['attach']=_0x537acf[_0xc7ec('0x15')][_0xc7ec('0xc')];for(var _0x1b78c9=0x0,_0x23e7bc,_0x566c9d,_0x2e0873;_0x1b78c9<_0x537acf['attachments'][_0xc7ec('0xc')];_0x1b78c9+=0x1){if(_0x537acf[_0xc7ec('0x15')][_0x1b78c9]['type']===_0xc7ec('0x25')){_0x2e0873=_0x537acf[_0xc7ec('0x15')][_0x1b78c9][_0xc7ec('0x18')]||'attachment-'+_0x1b78c9;_0x23e7bc=util[_0xc7ec('0x26')]('%s-%s',Date[_0xc7ec('0x27')](),_0x2e0873);_0x566c9d=path[_0xc7ec('0xe')](config[_0xc7ec('0x28')],'server/files/attachments/',_0x23e7bc);fs[_0xc7ec('0x29')](_0x566c9d,_0x537acf[_0xc7ec('0x15')][_0x1b78c9][_0xc7ec('0x2a')]);_0x5edde1[_0xc7ec('0x20')]['attachments'][_0xc7ec('0x17')]({'name':_0x2e0873,'basename':_0x23e7bc,'type':_0x537acf['attachments'][_0x1b78c9][_0xc7ec('0x2b')],'filepath':_0x566c9d});}}}return _0x5edde1;}exports[_0xc7ec('0x2c')]=function(_0x109634,_0x419f35,_0x3211b8){var _0x2aacb2={'from':getAddress(_0x419f35[_0xc7ec('0x2d')]),'firstName':getFirstName(_0x419f35[_0xc7ec('0x2d')]),'lastName':getLastName(_0x419f35[_0xc7ec('0x2d')]),'mapKey':_0xc7ec('0x3'),'message':{'from':getMails(_0x109634[_0xc7ec('0x3')],_0x419f35[_0xc7ec('0x2d')]),'to':_0x109634[_0xc7ec('0x3')],'cc':'','subject':_0x419f35[_0xc7ec('0x2e')]||'','messageId':_0x419f35[_0xc7ec('0x2f')],'inReplyTo':_0x419f35[_0xc7ec('0x30')],'sentAt':moment(_0x419f35['date'])[_0xc7ec('0x26')](_0xc7ec('0x31')),'attach':![],'attachments':[]}};if(!_0x419f35[_0xc7ec('0x13')]&&!_0x419f35[_0xc7ec('0x1e')]){var _0x5b9624=_[_0xc7ec('0x32')](_0x419f35[_0xc7ec('0x15')],{'contentType':_0xc7ec('0x33')});if(_0x5b9624[_0xc7ec('0xc')]){logger['info'](util['format'](_0xc7ec('0x34'),_0x419f35[_0xc7ec('0x2f')]));tnef[_0xc7ec('0x35')](_['first'](_0x5b9624)[_0xc7ec('0x2a')],function(_0x35d41c,_0x4b3002){if(_0x4b3002){_0x419f35[_0xc7ec('0x13')]=_0x4b3002[_0xc7ec('0x36')]||_0xc7ec('0x37');if(_0x4b3002[_0xc7ec('0x38')]){for(var _0x33f282=0x0;_0x33f282<_0x4b3002[_0xc7ec('0x38')][_0xc7ec('0xc')];_0x33f282++){var _0x2de63a=_0x4b3002[_0xc7ec('0x38')][_0x33f282];_0x2aacb2['message']['attachments']['push']({'filename':_0x2de63a[_0xc7ec('0x39')],'content':new Buffer(_0x2de63a['Data'])});}}}_0x3211b8(parseEmail(_0x109634,_0x419f35,_0x2aacb2));});}else{_0x419f35[_0xc7ec('0x13')]='<p>Empty\x20message</p>';_0x3211b8(parseEmail(_0x109634,_0x419f35,_0x2aacb2));}}else{_0x3211b8(parseEmail(_0x109634,_0x419f35,_0x2aacb2));}};exports[_0xc7ec('0x3a')]=function(_0x1202f7){switch(_0x1202f7){case _0xc7ec('0x3b'):return'imap.gmail.com';case'yahoo':return _0xc7ec('0x3c');case _0xc7ec('0x3d'):return _0xc7ec('0x3e');case'outlook365':return _0xc7ec('0x3f');}return _0xc7ec('0x40');};