252b835790fe80d527b0a240d80cb561ca0a3460
[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 _0xda0f=['format','YYYY-MM-DD\x20HH:mm:ss','filter','application/ms-tnef','info','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','parseBuffer','BodyHTML','<p>Empty\x20message</p>','Attachments','push','Title','Data','imap.gmail.com','yahoo','imap.mail.yahoo.com','imap-mail.outlook.com','127.0.0.1','lodash','moment','path','node-tnef','../../../config/environment','value','address','anonymous','generate','@xcally.com','head','name','split','slice','join','Anonymous','length','pop','isEmpty','html','body','textAsHtml','replace','<a\x20target=\x22_blank\x22\x20','message','originTo','email','includes','originCc','attach','attachments','type','filename','attachment-','root','server/files/attachments/','writeFileSync','content','contentType','parser','from','subject','messageId','inReplyTo','date'];(function(_0x583f43,_0x5dd404){var _0x4d235f=function(_0x51d129){while(--_0x51d129){_0x583f43['push'](_0x583f43['shift']());}};_0x4d235f(++_0x5dd404);}(_0xda0f,0x1cb));var _0xfda0=function(_0x1d0343,_0x472470){_0x1d0343=_0x1d0343-0x0;var _0xeba613=_0xda0f[_0x1d0343];return _0xeba613;};'use strict';var _=require(_0xfda0('0x0'));var moment=require(_0xfda0('0x1'));var random=require('randomstring');var fs=require('fs');var path=require(_0xfda0('0x2'));var util=require('util');var tnef=require(_0xfda0('0x3'));var logger=require('../../../config/logger')('email');var config=require(_0xfda0('0x4'));function getAddress(_0xfefea7){var _0x213f96=_['head'](_0xfefea7[_0xfda0('0x5')]);return _0x213f96&&_0x213f96[_0xfda0('0x6')]?_0x213f96[_0xfda0('0x6')]:_0xfda0('0x7')+random[_0xfda0('0x8')](0x6)+_0xfda0('0x9');}function getFirstName(_0x13f428){var _0x3458eb=_[_0xfda0('0xa')](_0x13f428[_0xfda0('0x5')]);if(_0x3458eb&&_0x3458eb[_0xfda0('0xb')]){var _0x118299=_0x3458eb['name'][_0xfda0('0xc')](/\s+/);return _0x118299['length']?_0x118299[_0xfda0('0xd')](0x0,-0x1)[_0xfda0('0xe')]('\x20'):_0x3458eb[_0xfda0('0xb')];}return _0xfda0('0xf');}function getLastName(_0x561d96){var _0x5ef8e1=_['head'](_0x561d96['value']);if(_0x5ef8e1&&_0x5ef8e1[_0xfda0('0xb')]){var _0x17591d=_0x5ef8e1[_0xfda0('0xb')][_0xfda0('0xc')](/\s+/);return _0x17591d[_0xfda0('0x10')]>0x1?_0x17591d[_0xfda0('0x11')]():'';}return'';}function getMails(_0x24632d,_0x94e8f5){var _0x4021cc='';for(var _0x3b8d69=0x0;_0x3b8d69<_0x94e8f5[_0xfda0('0x5')]['length'];_0x3b8d69+=0x1){if(_0x94e8f5[_0xfda0('0x5')][_0x3b8d69][_0xfda0('0x6')]!==_0x24632d){if(!_[_0xfda0('0x12')](_0x4021cc)){_0x4021cc+=',';}_0x4021cc+=_0x94e8f5[_0xfda0('0x5')][_0x3b8d69][_0xfda0('0xb')]||_0x94e8f5[_0xfda0('0x5')][_0x3b8d69][_0xfda0('0x6')]||'';_0x4021cc+='\x20<';_0x4021cc+=_0x94e8f5[_0xfda0('0x5')][_0x3b8d69][_0xfda0('0x6')]?_0x94e8f5[_0xfda0('0x5')][_0x3b8d69][_0xfda0('0x6')]:_0xfda0('0x7')+random[_0xfda0('0x8')](0x6)+_0xfda0('0x9');_0x4021cc+='>';}}return _0x4021cc;}function parseEmail(_0x1d438f,_0x39ef23,_0x58f731){if(_0x39ef23[_0xfda0('0x13')]){var _0x5338ac=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x5338ac['test'](_0x39ef23['html'])){_0x39ef23[_0xfda0('0x13')]=_0x39ef23['html']['replace'](_0x5338ac,'');}_0x58f731['body']=_0x39ef23[_0xfda0('0x13')];}else{_0x58f731[_0xfda0('0x14')]=_0x39ef23[_0xfda0('0x15')];}if(_0x58f731['body']){_0x58f731[_0xfda0('0x14')]=_0x58f731[_0xfda0('0x14')][_0xfda0('0x16')](/<a\s+/gi,_0xfda0('0x17'));}if(_0x39ef23['to']){_0x58f731[_0xfda0('0x18')][_0xfda0('0x19')]=getMails(null,_0x39ef23['to']);var _0x5c6d63=getMails(_0x1d438f[_0xfda0('0x1a')],_0x39ef23['to']);if(!_[_0xfda0('0x12')](_0x5c6d63)){_0x58f731[_0xfda0('0x18')]['cc']+=_0x5c6d63;}}if(_0x39ef23['cc']){_0x58f731[_0xfda0('0x18')]['originCc']=getMails(null,_0x39ef23['cc']);var _0x33f6c3=getMails(_0x1d438f[_0xfda0('0x1a')],_0x39ef23['cc']);if(!_[_0xfda0('0x12')](_0x33f6c3)){if(!_['isEmpty'](_0x58f731[_0xfda0('0x18')]['cc'])){_0x58f731[_0xfda0('0x18')]['cc']+=',';}_0x58f731[_0xfda0('0x18')]['cc']+=_0x33f6c3;}}if(!_[_0xfda0('0x1b')](_0x58f731[_0xfda0('0x18')][_0xfda0('0x19')],_0x1d438f[_0xfda0('0x1a')])&&!_[_0xfda0('0x1b')](_0x58f731[_0xfda0('0x18')][_0xfda0('0x1c')],_0x1d438f[_0xfda0('0x1a')])){_0x58f731['message']['bcc']=_0x1d438f[_0xfda0('0x1a')];}if(_0x39ef23['attachments']&&_0x39ef23['attachments'][_0xfda0('0x10')]){_0x58f731['message'][_0xfda0('0x1d')]=_0x39ef23[_0xfda0('0x1e')][_0xfda0('0x10')];for(var _0x234fe0=0x0,_0x4c7947,_0x5b6348,_0x465c94;_0x234fe0<_0x39ef23['attachments'][_0xfda0('0x10')];_0x234fe0+=0x1){if(_0x39ef23['attachments'][_0x234fe0][_0xfda0('0x1f')]==='attachment'){_0x465c94=_0x39ef23['attachments'][_0x234fe0][_0xfda0('0x20')]||_0xfda0('0x21')+_0x234fe0;_0x4c7947=util['format']('%s-%s',Date['now'](),_0x465c94);_0x5b6348=path[_0xfda0('0xe')](config[_0xfda0('0x22')],_0xfda0('0x23'),_0x4c7947);fs[_0xfda0('0x24')](_0x5b6348,_0x39ef23[_0xfda0('0x1e')][_0x234fe0][_0xfda0('0x25')]);_0x58f731[_0xfda0('0x18')][_0xfda0('0x1e')]['push']({'name':_0x465c94,'basename':_0x4c7947,'type':_0x39ef23[_0xfda0('0x1e')][_0x234fe0][_0xfda0('0x26')],'filepath':_0x5b6348});}}}return _0x58f731;}exports[_0xfda0('0x27')]=function(_0x473dec,_0x407a54,_0x15444a){var _0x52e0a3={'from':getAddress(_0x407a54['from']),'firstName':getFirstName(_0x407a54[_0xfda0('0x28')]),'lastName':getLastName(_0x407a54[_0xfda0('0x28')]),'mapKey':_0xfda0('0x1a'),'message':{'from':getMails(_0x473dec[_0xfda0('0x1a')],_0x407a54[_0xfda0('0x28')]),'to':_0x473dec[_0xfda0('0x1a')],'cc':'','subject':_0x407a54[_0xfda0('0x29')]||'','messageId':_0x407a54[_0xfda0('0x2a')],'inReplyTo':_0x407a54[_0xfda0('0x2b')],'sentAt':moment(_0x407a54[_0xfda0('0x2c')])[_0xfda0('0x2d')](_0xfda0('0x2e')),'attach':![],'attachments':[]}};if(!_0x407a54['html']&&!_0x407a54[_0xfda0('0x15')]){var _0x437538=_[_0xfda0('0x2f')](_0x407a54[_0xfda0('0x1e')],{'contentType':_0xfda0('0x30')});if(_0x437538[_0xfda0('0x10')]){logger[_0xfda0('0x31')](util[_0xfda0('0x2d')](_0xfda0('0x32'),_0x407a54[_0xfda0('0x2a')]));tnef[_0xfda0('0x33')](_['first'](_0x437538)[_0xfda0('0x25')],function(_0x2847cf,_0x144816){if(_0x144816){_0x407a54[_0xfda0('0x13')]=_0x144816[_0xfda0('0x34')]||_0xfda0('0x35');if(_0x144816[_0xfda0('0x36')]){for(var _0x5d4efc=0x0;_0x5d4efc<_0x144816[_0xfda0('0x36')][_0xfda0('0x10')];_0x5d4efc++){var _0x3591be=_0x144816[_0xfda0('0x36')][_0x5d4efc];_0x52e0a3[_0xfda0('0x18')][_0xfda0('0x1e')][_0xfda0('0x37')]({'filename':_0x3591be[_0xfda0('0x38')],'content':new Buffer(_0x3591be[_0xfda0('0x39')])});}}}_0x15444a(parseEmail(_0x473dec,_0x407a54,_0x52e0a3));});}else{_0x407a54[_0xfda0('0x13')]=_0xfda0('0x35');_0x15444a(parseEmail(_0x473dec,_0x407a54,_0x52e0a3));}}else{_0x15444a(parseEmail(_0x473dec,_0x407a54,_0x52e0a3));}};exports['getHost']=function(_0x1fe613){switch(_0x1fe613){case'gmail':return _0xfda0('0x3a');case _0xfda0('0x3b'):return _0xfda0('0x3c');case'hotmail':return _0xfda0('0x3d');case'outlook365':return'outlook.office365.com';}return _0xfda0('0x3e');};