Built motion from commit b5996064.|2.6.21
[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 _0x3056=['includes','originCc','bcc','attach','type','%s-%s','now','root','server/files/attachments/','writeFileSync','content','contentType','headerLines','Auto-Submitted:\x20auto-replied','X-Auto-Response-Suppress:\x20All','Precedence:\x20auto_reply','line','isAutoreply','parser','from','subject','inReplyTo','date','format','YYYY-MM-DD\x20HH:mm:ss','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','generateXOAuth2Token','user=','auth=Bearer\x20','utf-8','base64','moment','randomstring','path','node-tnef','../../../config/logger','email','../../../config/environment','value','address','anonymous','head','name','substring','indexOf','length','slice','join','Anonymous','split','pop','isEmpty','generate','html','test','replace','<img','attachments','forEach','push','filename','_SRC','<span\x20style=font-weight:bold>\x20[Attachment:\x20','\x20]</span>','body','textAsHtml','message','originTo'];(function(_0x53ccf2,_0x13cb67){var _0x5a2964=function(_0x3b0514){while(--_0x3b0514){_0x53ccf2['push'](_0x53ccf2['shift']());}};_0x5a2964(++_0x13cb67);}(_0x3056,0x1a3));var _0x6305=function(_0xcd98f7,_0x58859d){_0xcd98f7=_0xcd98f7-0x0;var _0x171bb3=_0x3056[_0xcd98f7];return _0x171bb3;};'use strict';var _=require('lodash');var moment=require(_0x6305('0x0'));var random=require(_0x6305('0x1'));var fs=require('fs');var path=require(_0x6305('0x2'));var util=require('util');var tnef=require(_0x6305('0x3'));var logger=require(_0x6305('0x4'))(_0x6305('0x5'));var config=require(_0x6305('0x6'));function getAddress(_0x36ae0f){var _0x13d4af=_['head'](_0x36ae0f[_0x6305('0x7')]);return _0x13d4af&&_0x13d4af[_0x6305('0x8')]?_0x13d4af['address']:_0x6305('0x9')+random['generate'](0x6)+'@xcally.com';}function getFirstName(_0x5570ee){var _0x2b28c9=_[_0x6305('0xa')](_0x5570ee[_0x6305('0x7')]);if(_0x2b28c9&&_0x2b28c9[_0x6305('0xb')]){if(_0x2b28c9[_0x6305('0xb')]==_0x2b28c9[_0x6305('0x8')]){return _0x2b28c9[_0x6305('0xb')][_0x6305('0xc')](0x0,_0x2b28c9['name'][_0x6305('0xd')]('@'));}var _0x500efd=_0x2b28c9['name']['split'](/\s+/);return _0x500efd[_0x6305('0xe')]>0x1?_0x500efd[_0x6305('0xf')](0x0,-0x1)[_0x6305('0x10')]('\x20'):_0x2b28c9[_0x6305('0xb')];}return _0x6305('0x11');}function getLastName(_0x34c7a0){var _0x30ee97=_[_0x6305('0xa')](_0x34c7a0[_0x6305('0x7')]);if(_0x30ee97&&_0x30ee97[_0x6305('0xb')]){var _0x23d8bb=_0x30ee97[_0x6305('0xb')][_0x6305('0x12')](/\s+/);return _0x23d8bb[_0x6305('0xe')]>0x1?_0x23d8bb[_0x6305('0x13')]():'';}return'';}function getMails(_0x1c559c,_0x29ee0e){var _0x5c0fe7='';for(var _0x439b8f=0x0;_0x439b8f<_0x29ee0e[_0x6305('0x7')][_0x6305('0xe')];_0x439b8f+=0x1){if(_0x29ee0e[_0x6305('0x7')][_0x439b8f][_0x6305('0x8')]!==_0x1c559c){if(!_[_0x6305('0x14')](_0x5c0fe7)){_0x5c0fe7+=',';}_0x5c0fe7+=_0x29ee0e[_0x6305('0x7')][_0x439b8f][_0x6305('0xb')]||_0x29ee0e['value'][_0x439b8f][_0x6305('0x8')]||'';_0x5c0fe7+='\x20<';_0x5c0fe7+=_0x29ee0e[_0x6305('0x7')][_0x439b8f][_0x6305('0x8')]?_0x29ee0e[_0x6305('0x7')][_0x439b8f][_0x6305('0x8')]:_0x6305('0x9')+random[_0x6305('0x15')](0x6)+'@xcally.com';_0x5c0fe7+='>';}}return _0x5c0fe7;}function parseEmail(_0x58d460,_0x171667,_0x3e3389){if(_0x171667[_0x6305('0x16')]){var _0x115ec4=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x115ec4[_0x6305('0x17')](_0x171667[_0x6305('0x16')])){_0x171667[_0x6305('0x16')]=_0x171667[_0x6305('0x16')][_0x6305('0x18')](_0x115ec4,'');}if(_0x171667[_0x6305('0x16')]['includes'](_0x6305('0x19'))){var _0x5a0302=[];_0x171667[_0x6305('0x1a')][_0x6305('0x1b')](_0x3c90c2=>{_0x5a0302[_0x6305('0x1c')](_0x3c90c2[_0x6305('0x1d')]);});var _0x469d08=/<img.*? _SRC(.*?)[^>]+>/g;var _0x288db7=/src="data:image\/([a-zA-Z]*);base64,([^"]*)"/g;var _0x10314e=_0x171667[_0x6305('0x16')]['replace'](_0x288db7,_0x6305('0x1e'));var _0x296df5=_0x10314e[_0x6305('0x18')](_0x469d08,'_IMG');for(let _0x1d500b=0x0;_0x1d500b<_0x5a0302[_0x6305('0xe')];_0x1d500b++){var _0x26ce6c=_0x296df5[_0x6305('0x18')]('_IMG',_0x6305('0x1f')+_0x5a0302[_0x1d500b]+_0x6305('0x20'));_0x296df5=_0x26ce6c;}_0x171667[_0x6305('0x16')]=_0x296df5;}_0x3e3389['body']=_0x171667[_0x6305('0x16')];}else{_0x3e3389[_0x6305('0x21')]=_0x171667[_0x6305('0x22')];}if(_0x3e3389['body']){_0x3e3389[_0x6305('0x21')]=_0x3e3389[_0x6305('0x21')][_0x6305('0x18')](/<a\s+/gi,'<a\x20target=\x22_blank\x22\x20');}if(_0x171667['to']){_0x3e3389[_0x6305('0x23')][_0x6305('0x24')]=getMails(null,_0x171667['to']);var _0x32a61e=getMails(_0x58d460[_0x6305('0x5')],_0x171667['to']);if(!_[_0x6305('0x14')](_0x32a61e)){_0x3e3389[_0x6305('0x23')]['cc']+=_0x32a61e;}}if(_0x171667['cc']){_0x3e3389[_0x6305('0x23')]['originCc']=getMails(null,_0x171667['cc']);var _0x5aed0a=getMails(_0x58d460[_0x6305('0x5')],_0x171667['cc']);if(!_[_0x6305('0x14')](_0x5aed0a)){if(!_[_0x6305('0x14')](_0x3e3389[_0x6305('0x23')]['cc'])){_0x3e3389[_0x6305('0x23')]['cc']+=',';}_0x3e3389[_0x6305('0x23')]['cc']+=_0x5aed0a;}}if(!_['includes'](_0x3e3389[_0x6305('0x23')]['originTo'],_0x58d460[_0x6305('0x5')])&&!_[_0x6305('0x25')](_0x3e3389['message'][_0x6305('0x26')],_0x58d460[_0x6305('0x5')])){_0x3e3389[_0x6305('0x23')][_0x6305('0x27')]=_0x58d460[_0x6305('0x5')];}if(_0x171667['attachments']&&_0x171667[_0x6305('0x1a')][_0x6305('0xe')]){_0x3e3389[_0x6305('0x23')][_0x6305('0x28')]=_0x171667['attachments'][_0x6305('0xe')];for(var _0x1ae614=0x0,_0x59399a,_0x44def5,_0x52c269;_0x1ae614<_0x171667['attachments']['length'];_0x1ae614+=0x1){if(_0x171667['attachments'][_0x1ae614][_0x6305('0x29')]==='attachment'){_0x52c269=_0x171667[_0x6305('0x1a')][_0x1ae614][_0x6305('0x1d')]||'attachment-'+_0x1ae614;_0x59399a=util['format'](_0x6305('0x2a'),Date[_0x6305('0x2b')](),_0x52c269);_0x44def5=path[_0x6305('0x10')](config[_0x6305('0x2c')],_0x6305('0x2d'),_0x59399a);fs[_0x6305('0x2e')](_0x44def5,_0x171667[_0x6305('0x1a')][_0x1ae614][_0x6305('0x2f')]);_0x3e3389['message'][_0x6305('0x1a')][_0x6305('0x1c')]({'name':_0x52c269,'basename':_0x59399a,'type':_0x171667[_0x6305('0x1a')][_0x1ae614][_0x6305('0x30')],'filepath':_0x44def5});}}}if(_0x171667['headerLines']&&_0x171667[_0x6305('0x31')]['length']>0x0){var _0x145c51=[_0x6305('0x32'),_0x6305('0x33'),_0x6305('0x34')];for(var _0x44b822=0x0;_0x44b822<_0x171667[_0x6305('0x31')][_0x6305('0xe')];_0x44b822++){var _0x35a408=_[_0x6305('0xd')](_0x145c51,_0x171667[_0x6305('0x31')][_0x44b822][_0x6305('0x35')]);if(_0x35a408>=0x0){_0x3e3389[_0x6305('0x36')]=!![];}}}return _0x3e3389;}exports[_0x6305('0x37')]=function(_0x486c28,_0x1bf178,_0x2d2eb7){var _0x8bd459={'from':getAddress(_0x1bf178['from']),'firstName':getFirstName(_0x1bf178['from']),'lastName':getLastName(_0x1bf178['from']),'mapKey':'email','message':{'from':getMails(_0x486c28['email'],_0x1bf178[_0x6305('0x38')]),'to':_0x486c28[_0x6305('0x5')],'cc':'','subject':_0x1bf178[_0x6305('0x39')]||'','messageId':_0x1bf178['messageId'],'inReplyTo':_0x1bf178[_0x6305('0x3a')],'sentAt':moment(_0x1bf178[_0x6305('0x3b')])[_0x6305('0x3c')](_0x6305('0x3d')),'attach':![],'attachments':[]}};if(!_0x1bf178[_0x6305('0x16')]&&!_0x1bf178[_0x6305('0x22')]){var _0x37043e=_['filter'](_0x1bf178[_0x6305('0x1a')],{'contentType':_0x6305('0x3e')});if(_0x37043e[_0x6305('0xe')]){logger[_0x6305('0x3f')](util['format'](_0x6305('0x40'),_0x1bf178['messageId']));tnef[_0x6305('0x41')](_[_0x6305('0x42')](_0x37043e)[_0x6305('0x2f')],function(_0x498a96,_0xc70955){if(_0xc70955){_0x1bf178[_0x6305('0x16')]=_0xc70955[_0x6305('0x43')]||_0x6305('0x44');if(_0xc70955[_0x6305('0x45')]){for(var _0x48be5b=0x0;_0x48be5b<_0xc70955['Attachments'][_0x6305('0xe')];_0x48be5b++){var _0xa76432=_0xc70955[_0x6305('0x45')][_0x48be5b];_0x8bd459[_0x6305('0x23')][_0x6305('0x1a')][_0x6305('0x1c')]({'filename':_0xa76432[_0x6305('0x46')],'content':Buffer[_0x6305('0x38')](_0xa76432['Data'])});}}}_0x2d2eb7(parseEmail(_0x486c28,_0x1bf178,_0x8bd459));});}else{_0x1bf178[_0x6305('0x16')]=_0x6305('0x44');_0x2d2eb7(parseEmail(_0x486c28,_0x1bf178,_0x8bd459));}}else{_0x2d2eb7(parseEmail(_0x486c28,_0x1bf178,_0x8bd459));}};exports[_0x6305('0x47')]=function(_0x8ca11b,_0x28c887){var _0x4b8151=[_0x6305('0x48')+(_0x28c887||''),_0x6305('0x49')+_0x8ca11b,'',''];return Buffer[_0x6305('0x38')](_0x4b8151[_0x6305('0x10')]('\ 1'),_0x6305('0x4a'))['toString'](_0x6305('0x4b'));};