79dff0c7341ca4354f5cd407d1839e9a6a422307
[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 _0x7fb0=['Attachments','Data','gmail','yahoo','imap.mail.yahoo.com','127.0.0.1','randomstring','util','../../../config/logger','email','value','address','generate','@xcally.com','name','split','length','slice','Anonymous','pop','isEmpty','anonymous','html','replace','includes','<img','attachments','push','filename','_SRC','_IMG','body','textAsHtml','message','originCc','attach','type','format','%s-%s','now','join','root','server/files/attachments/','writeFileSync','contentType','parser','from','date','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','messageId','parseBuffer','first','content','BodyHTML','<p>Empty\x20message</p>'];(function(_0x229f31,_0x860846){var _0x134adf=function(_0x391f95){while(--_0x391f95){_0x229f31['push'](_0x229f31['shift']());}};_0x134adf(++_0x860846);}(_0x7fb0,0xee));var _0x07fb=function(_0x467800,_0x17ae09){_0x467800=_0x467800-0x0;var _0x43e9bb=_0x7fb0[_0x467800];return _0x43e9bb;};'use strict';var _=require('lodash');var moment=require('moment');var random=require(_0x07fb('0x0'));var fs=require('fs');var path=require('path');var util=require(_0x07fb('0x1'));var tnef=require('node-tnef');var logger=require(_0x07fb('0x2'))(_0x07fb('0x3'));var config=require('../../../config/environment');function getAddress(_0x2b495d){var _0x58ca75=_['head'](_0x2b495d[_0x07fb('0x4')]);return _0x58ca75&&_0x58ca75[_0x07fb('0x5')]?_0x58ca75['address']:'anonymous'+random[_0x07fb('0x6')](0x6)+_0x07fb('0x7');}function getFirstName(_0x445c8d){var _0x49bc98=_['head'](_0x445c8d[_0x07fb('0x4')]);if(_0x49bc98&&_0x49bc98[_0x07fb('0x8')]){var _0x17f4ca=_0x49bc98[_0x07fb('0x8')][_0x07fb('0x9')](/\s+/);return _0x17f4ca[_0x07fb('0xa')]?_0x17f4ca[_0x07fb('0xb')](0x0,-0x1)['join']('\x20'):_0x49bc98[_0x07fb('0x8')];}return _0x07fb('0xc');}function getLastName(_0x2d96c7){var _0x794c2c=_['head'](_0x2d96c7[_0x07fb('0x4')]);if(_0x794c2c&&_0x794c2c['name']){var _0x3a3b47=_0x794c2c[_0x07fb('0x8')][_0x07fb('0x9')](/\s+/);return _0x3a3b47['length']>0x1?_0x3a3b47[_0x07fb('0xd')]():'';}return'';}function getMails(_0x400004,_0x2130c6){var _0x2666b4='';for(var _0x6d277e=0x0;_0x6d277e<_0x2130c6[_0x07fb('0x4')][_0x07fb('0xa')];_0x6d277e+=0x1){if(_0x2130c6[_0x07fb('0x4')][_0x6d277e]['address']!==_0x400004){if(!_[_0x07fb('0xe')](_0x2666b4)){_0x2666b4+=',';}_0x2666b4+=_0x2130c6['value'][_0x6d277e][_0x07fb('0x8')]||_0x2130c6[_0x07fb('0x4')][_0x6d277e][_0x07fb('0x5')]||'';_0x2666b4+='\x20<';_0x2666b4+=_0x2130c6['value'][_0x6d277e][_0x07fb('0x5')]?_0x2130c6[_0x07fb('0x4')][_0x6d277e][_0x07fb('0x5')]:_0x07fb('0xf')+random[_0x07fb('0x6')](0x6)+_0x07fb('0x7');_0x2666b4+='>';}}return _0x2666b4;}function parseEmail(_0x483092,_0x13732d,_0x3cdfa4){if(_0x13732d[_0x07fb('0x10')]){var _0x5cee18=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x5cee18['test'](_0x13732d[_0x07fb('0x10')])){_0x13732d[_0x07fb('0x10')]=_0x13732d['html'][_0x07fb('0x11')](_0x5cee18,'');}if(_0x13732d['html'][_0x07fb('0x12')](_0x07fb('0x13'))){var _0x54a530=[];_0x13732d[_0x07fb('0x14')]['forEach'](_0x1e462c=>{_0x54a530[_0x07fb('0x15')](_0x1e462c[_0x07fb('0x16')]);});var _0x3d57b0=/<img.*? _SRC(.*?)[^\>]+>/g;var _0x1b777b=/src=\"data:image\/([a-zA-Z]*);base64,([^\"]*)\"/g;var _0x184387=_0x13732d['html'][_0x07fb('0x11')](_0x1b777b,_0x07fb('0x17'));var _0x3e4610=_0x184387[_0x07fb('0x11')](_0x3d57b0,_0x07fb('0x18'));for(let _0x570f59=0x0;_0x570f59<_0x54a530[_0x07fb('0xa')];_0x570f59++){var _0x58b4d4=_0x3e4610['replace']('_IMG','<span\x20style=font-weight:bold>\x20[Attachment:\x20'+_0x54a530[_0x570f59]+'\x20]</span>');_0x3e4610=_0x58b4d4;}_0x13732d[_0x07fb('0x10')]=_0x3e4610;}_0x3cdfa4[_0x07fb('0x19')]=_0x13732d['html'];}else{_0x3cdfa4[_0x07fb('0x19')]=_0x13732d[_0x07fb('0x1a')];}if(_0x3cdfa4[_0x07fb('0x19')]){_0x3cdfa4[_0x07fb('0x19')]=_0x3cdfa4[_0x07fb('0x19')][_0x07fb('0x11')](/<a\s+/gi,'<a\x20target=\x22_blank\x22\x20');}if(_0x13732d['to']){_0x3cdfa4['message']['originTo']=getMails(null,_0x13732d['to']);var _0xa5b461=getMails(_0x483092[_0x07fb('0x3')],_0x13732d['to']);if(!_[_0x07fb('0xe')](_0xa5b461)){_0x3cdfa4[_0x07fb('0x1b')]['cc']+=_0xa5b461;}}if(_0x13732d['cc']){_0x3cdfa4[_0x07fb('0x1b')][_0x07fb('0x1c')]=getMails(null,_0x13732d['cc']);var _0x77865b=getMails(_0x483092[_0x07fb('0x3')],_0x13732d['cc']);if(!_['isEmpty'](_0x77865b)){if(!_['isEmpty'](_0x3cdfa4['message']['cc'])){_0x3cdfa4['message']['cc']+=',';}_0x3cdfa4[_0x07fb('0x1b')]['cc']+=_0x77865b;}}if(!_[_0x07fb('0x12')](_0x3cdfa4[_0x07fb('0x1b')]['originTo'],_0x483092[_0x07fb('0x3')])&&!_[_0x07fb('0x12')](_0x3cdfa4['message'][_0x07fb('0x1c')],_0x483092[_0x07fb('0x3')])){_0x3cdfa4[_0x07fb('0x1b')]['bcc']=_0x483092[_0x07fb('0x3')];}if(_0x13732d['attachments']&&_0x13732d['attachments'][_0x07fb('0xa')]){_0x3cdfa4[_0x07fb('0x1b')][_0x07fb('0x1d')]=_0x13732d[_0x07fb('0x14')]['length'];for(var _0xb9ceca=0x0,_0x3341ba,_0x3dc7c0,_0x4d0335;_0xb9ceca<_0x13732d[_0x07fb('0x14')][_0x07fb('0xa')];_0xb9ceca+=0x1){if(_0x13732d['attachments'][_0xb9ceca][_0x07fb('0x1e')]==='attachment'){_0x4d0335=_0x13732d[_0x07fb('0x14')][_0xb9ceca][_0x07fb('0x16')]||'attachment-'+_0xb9ceca;_0x3341ba=util[_0x07fb('0x1f')](_0x07fb('0x20'),Date[_0x07fb('0x21')](),_0x4d0335);_0x3dc7c0=path[_0x07fb('0x22')](config[_0x07fb('0x23')],_0x07fb('0x24'),_0x3341ba);fs[_0x07fb('0x25')](_0x3dc7c0,_0x13732d[_0x07fb('0x14')][_0xb9ceca]['content']);_0x3cdfa4[_0x07fb('0x1b')][_0x07fb('0x14')][_0x07fb('0x15')]({'name':_0x4d0335,'basename':_0x3341ba,'type':_0x13732d[_0x07fb('0x14')][_0xb9ceca][_0x07fb('0x26')],'filepath':_0x3dc7c0});}}}return _0x3cdfa4;}exports[_0x07fb('0x27')]=function(_0x6f73c4,_0x478daa,_0x4be3f8){var _0x50bb23={'from':getAddress(_0x478daa['from']),'firstName':getFirstName(_0x478daa[_0x07fb('0x28')]),'lastName':getLastName(_0x478daa[_0x07fb('0x28')]),'mapKey':_0x07fb('0x3'),'message':{'from':getMails(_0x6f73c4['email'],_0x478daa[_0x07fb('0x28')]),'to':_0x6f73c4[_0x07fb('0x3')],'cc':'','subject':_0x478daa['subject']||'','messageId':_0x478daa['messageId'],'inReplyTo':_0x478daa['inReplyTo'],'sentAt':moment(_0x478daa[_0x07fb('0x29')])[_0x07fb('0x1f')](_0x07fb('0x2a')),'attach':![],'attachments':[]}};if(!_0x478daa[_0x07fb('0x10')]&&!_0x478daa[_0x07fb('0x1a')]){var _0x5795e9=_[_0x07fb('0x2b')](_0x478daa['attachments'],{'contentType':_0x07fb('0x2c')});if(_0x5795e9['length']){logger['info'](util[_0x07fb('0x1f')](_0x07fb('0x2d'),_0x478daa[_0x07fb('0x2e')]));tnef[_0x07fb('0x2f')](_[_0x07fb('0x30')](_0x5795e9)[_0x07fb('0x31')],function(_0x469e14,_0x5d8696){if(_0x5d8696){_0x478daa[_0x07fb('0x10')]=_0x5d8696[_0x07fb('0x32')]||_0x07fb('0x33');if(_0x5d8696[_0x07fb('0x34')]){for(var _0x206b3c=0x0;_0x206b3c<_0x5d8696[_0x07fb('0x34')][_0x07fb('0xa')];_0x206b3c++){var _0x2c8499=_0x5d8696[_0x07fb('0x34')][_0x206b3c];_0x50bb23[_0x07fb('0x1b')][_0x07fb('0x14')][_0x07fb('0x15')]({'filename':_0x2c8499['Title'],'content':new Buffer(_0x2c8499[_0x07fb('0x35')])});}}}_0x4be3f8(parseEmail(_0x6f73c4,_0x478daa,_0x50bb23));});}else{_0x478daa[_0x07fb('0x10')]=_0x07fb('0x33');_0x4be3f8(parseEmail(_0x6f73c4,_0x478daa,_0x50bb23));}}else{_0x4be3f8(parseEmail(_0x6f73c4,_0x478daa,_0x50bb23));}};exports['getHost']=function(_0x25b1ae){switch(_0x25b1ae){case _0x07fb('0x36'):return'imap.gmail.com';case _0x07fb('0x37'):return _0x07fb('0x38');case'hotmail':return'imap-mail.outlook.com';case'outlook365':return'outlook.office365.com';}return _0x07fb('0x39');};