Built motion from commit (unavailable).|2.4.4
[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 _0x04e0=['attachment-','format','writeFileSync','content','push','contentType','parser','from','subject','inReplyTo','date','YYYY-MM-DD\x20HH:mm:ss','application/ms-tnef','messageId','parseBuffer','first','BodyHTML','<p>Empty\x20message</p>','Attachments','Data','getHost','gmail','imap.gmail.com','yahoo','imap-mail.outlook.com','outlook365','127.0.0.1','moment','randomstring','path','node-tnef','../../../config/logger','email','../../../config/environment','head','value','address','anonymous','generate','@xcally.com','name','length','slice','join','split','pop','html','test','replace','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','originTo','message','originCc','isEmpty','includes','bcc','attachments','type','attachment'];(function(_0x46a675,_0x27b877){var _0x6e702b=function(_0x263bf2){while(--_0x263bf2){_0x46a675['push'](_0x46a675['shift']());}};_0x6e702b(++_0x27b877);}(_0x04e0,0xd2));var _0x004e=function(_0x4cfa93,_0x105f7a){_0x4cfa93=_0x4cfa93-0x0;var _0x3f707e=_0x04e0[_0x4cfa93];return _0x3f707e;};'use strict';var _=require('lodash');var moment=require(_0x004e('0x0'));var random=require(_0x004e('0x1'));var fs=require('fs');var path=require(_0x004e('0x2'));var util=require('util');var tnef=require(_0x004e('0x3'));var logger=require(_0x004e('0x4'))(_0x004e('0x5'));var config=require(_0x004e('0x6'));function getAddress(_0x8bc73c){var _0xb64b41=_[_0x004e('0x7')](_0x8bc73c[_0x004e('0x8')]);return _0xb64b41&&_0xb64b41[_0x004e('0x9')]?_0xb64b41[_0x004e('0x9')]:_0x004e('0xa')+random[_0x004e('0xb')](0x6)+_0x004e('0xc');}function getFirstName(_0x42dd9b){var _0x8eadd3=_['head'](_0x42dd9b[_0x004e('0x8')]);if(_0x8eadd3&&_0x8eadd3[_0x004e('0xd')]){var _0x4312ec=_0x8eadd3[_0x004e('0xd')]['split'](/\s+/);return _0x4312ec[_0x004e('0xe')]?_0x4312ec[_0x004e('0xf')](0x0,-0x1)[_0x004e('0x10')]('\x20'):_0x8eadd3[_0x004e('0xd')];}return'Anonymous';}function getLastName(_0xc8faa8){var _0x293748=_[_0x004e('0x7')](_0xc8faa8[_0x004e('0x8')]);if(_0x293748&&_0x293748[_0x004e('0xd')]){var _0x305cc6=_0x293748[_0x004e('0xd')][_0x004e('0x11')](/\s+/);return _0x305cc6[_0x004e('0xe')]>0x1?_0x305cc6[_0x004e('0x12')]():'';}return'';}function getMails(_0x538472,_0x327b9d){var _0x38b995='';for(var _0x24067d=0x0;_0x24067d<_0x327b9d[_0x004e('0x8')][_0x004e('0xe')];_0x24067d+=0x1){if(_0x327b9d[_0x004e('0x8')][_0x24067d][_0x004e('0x9')]!==_0x538472){if(!_['isEmpty'](_0x38b995)){_0x38b995+=',';}_0x38b995+=_0x327b9d[_0x004e('0x8')][_0x24067d]['name']||_0x327b9d[_0x004e('0x8')][_0x24067d]['address']||'';_0x38b995+='\x20<';_0x38b995+=_0x327b9d[_0x004e('0x8')][_0x24067d]['address']?_0x327b9d[_0x004e('0x8')][_0x24067d][_0x004e('0x9')]:_0x004e('0xa')+random[_0x004e('0xb')](0x6)+'@xcally.com';_0x38b995+='>';}}return _0x38b995;}function parseEmail(_0x197c8f,_0x5b9543,_0x38e233){if(_0x5b9543[_0x004e('0x13')]){var _0x267aa=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x267aa[_0x004e('0x14')](_0x5b9543['html'])){_0x5b9543[_0x004e('0x13')]=_0x5b9543[_0x004e('0x13')][_0x004e('0x15')](_0x267aa,'');}_0x38e233[_0x004e('0x16')]=_0x5b9543[_0x004e('0x13')];}else{_0x38e233[_0x004e('0x16')]=_0x5b9543[_0x004e('0x17')];}if(_0x38e233[_0x004e('0x16')]){_0x38e233['body']=_0x38e233[_0x004e('0x16')][_0x004e('0x15')](/<a\s+/gi,_0x004e('0x18'));}if(_0x5b9543['to']){_0x38e233['message'][_0x004e('0x19')]=getMails(null,_0x5b9543['to']);var _0x345151=getMails(_0x197c8f[_0x004e('0x5')],_0x5b9543['to']);if(!_['isEmpty'](_0x345151)){_0x38e233[_0x004e('0x1a')]['cc']+=_0x345151;}}if(_0x5b9543['cc']){_0x38e233[_0x004e('0x1a')][_0x004e('0x1b')]=getMails(null,_0x5b9543['cc']);var _0x4605b7=getMails(_0x197c8f[_0x004e('0x5')],_0x5b9543['cc']);if(!_['isEmpty'](_0x4605b7)){if(!_[_0x004e('0x1c')](_0x38e233[_0x004e('0x1a')]['cc'])){_0x38e233[_0x004e('0x1a')]['cc']+=',';}_0x38e233[_0x004e('0x1a')]['cc']+=_0x4605b7;}}if(!_[_0x004e('0x1d')](_0x38e233[_0x004e('0x1a')][_0x004e('0x19')],_0x197c8f[_0x004e('0x5')])&&!_['includes'](_0x38e233[_0x004e('0x1a')]['originCc'],_0x197c8f['email'])){_0x38e233[_0x004e('0x1a')][_0x004e('0x1e')]=_0x197c8f['email'];}if(_0x5b9543[_0x004e('0x1f')]&&_0x5b9543[_0x004e('0x1f')]['length']){_0x38e233['message']['attach']=_0x5b9543['attachments'][_0x004e('0xe')];for(var _0x4d2323=0x0,_0x52a438,_0x35a9ee,_0x1ad079;_0x4d2323<_0x5b9543[_0x004e('0x1f')][_0x004e('0xe')];_0x4d2323+=0x1){if(_0x5b9543['attachments'][_0x4d2323][_0x004e('0x20')]===_0x004e('0x21')){_0x1ad079=_0x5b9543[_0x004e('0x1f')][_0x4d2323]['filename']||_0x004e('0x22')+_0x4d2323;_0x52a438=util[_0x004e('0x23')]('%s-%s',Date['now'](),_0x1ad079);_0x35a9ee=path[_0x004e('0x10')](config['root'],'server/files/attachments/',_0x52a438);fs[_0x004e('0x24')](_0x35a9ee,_0x5b9543[_0x004e('0x1f')][_0x4d2323][_0x004e('0x25')]);_0x38e233[_0x004e('0x1a')][_0x004e('0x1f')][_0x004e('0x26')]({'name':_0x1ad079,'basename':_0x52a438,'type':_0x5b9543[_0x004e('0x1f')][_0x4d2323][_0x004e('0x27')],'filepath':_0x35a9ee});}}}return _0x38e233;}exports[_0x004e('0x28')]=function(_0x191ca5,_0x8ad157,_0x1a72d7){var _0x4a29ca={'from':getAddress(_0x8ad157[_0x004e('0x29')]),'firstName':getFirstName(_0x8ad157[_0x004e('0x29')]),'lastName':getLastName(_0x8ad157[_0x004e('0x29')]),'mapKey':_0x004e('0x5'),'message':{'from':getMails(_0x191ca5[_0x004e('0x5')],_0x8ad157[_0x004e('0x29')]),'to':_0x191ca5[_0x004e('0x5')],'cc':'','subject':_0x8ad157[_0x004e('0x2a')]||'','messageId':_0x8ad157['messageId'],'inReplyTo':_0x8ad157[_0x004e('0x2b')],'sentAt':moment(_0x8ad157[_0x004e('0x2c')])[_0x004e('0x23')](_0x004e('0x2d')),'attach':![],'attachments':[]}};if(!_0x8ad157[_0x004e('0x13')]&&!_0x8ad157[_0x004e('0x17')]){var _0x49f139=_['filter'](_0x8ad157[_0x004e('0x1f')],{'contentType':_0x004e('0x2e')});if(_0x49f139['length']){logger['info'](util['format']('Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system',_0x8ad157[_0x004e('0x2f')]));tnef[_0x004e('0x30')](_[_0x004e('0x31')](_0x49f139)[_0x004e('0x25')],function(_0x7821cb,_0x4b5d0){if(_0x4b5d0){_0x8ad157[_0x004e('0x13')]=_0x4b5d0[_0x004e('0x32')]||_0x004e('0x33');if(_0x4b5d0[_0x004e('0x34')]){for(var _0xf9e69d=0x0;_0xf9e69d<_0x4b5d0[_0x004e('0x34')]['length'];_0xf9e69d++){var _0x55213d=_0x4b5d0[_0x004e('0x34')][_0xf9e69d];_0x4a29ca[_0x004e('0x1a')]['attachments'][_0x004e('0x26')]({'filename':_0x55213d['Title'],'content':new Buffer(_0x55213d[_0x004e('0x35')])});}}}_0x1a72d7(parseEmail(_0x191ca5,_0x8ad157,_0x4a29ca));});}else{_0x8ad157['html']=_0x004e('0x33');_0x1a72d7(parseEmail(_0x191ca5,_0x8ad157,_0x4a29ca));}}else{_0x1a72d7(parseEmail(_0x191ca5,_0x8ad157,_0x4a29ca));}};exports[_0x004e('0x36')]=function(_0xae946a){switch(_0xae946a){case _0x004e('0x37'):return _0x004e('0x38');case _0x004e('0x39'):return'imap.mail.yahoo.com';case'hotmail':return _0x004e('0x3a');case _0x004e('0x3b'):return'outlook.office365.com';}return _0x004e('0x3c');};