Built motion from commit dbf3a7e2.|2.6.30
[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 _0xf8f7=['attachment-','format','%s-%s','now','root','server/files/attachments/','content','contentType','headerLines','Auto-Submitted:\x20auto-replied','X-Auto-Response-Suppress:\x20All','Precedence:\x20auto_reply','line','isAutoreply','parser','from','inReplyTo','YYYY-MM-DD\x20HH:mm:ss','textAsHtml','filter','info','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','messageId','parseBuffer','first','BodyHTML','<p>Empty\x20message</p>','Attachments','Title','Data','auth=Bearer\x20','toString','base64','moment','randomstring','util','node-tnef','email','head','value','address','anonymous','generate','@xcally.com','name','indexOf','split','length','slice','join','Anonymous','pop','isEmpty','html','replace','includes','<img','push','filename','_SRC','<span\x20style=font-weight:bold>\x20[Attachment:\x20','\x20]</span>','body','<a\x20target=\x22_blank\x22\x20','message','originTo','originCc','attachments','type','attachment'];(function(_0x347287,_0x2321b5){var _0x5ff41c=function(_0x814359){while(--_0x814359){_0x347287['push'](_0x347287['shift']());}};_0x5ff41c(++_0x2321b5);}(_0xf8f7,0x17f));var _0x7f8f=function(_0xf7ea9c,_0x5a6daf){_0xf7ea9c=_0xf7ea9c-0x0;var _0x5e159f=_0xf8f7[_0xf7ea9c];return _0x5e159f;};'use strict';var _=require('lodash');var moment=require(_0x7f8f('0x0'));var random=require(_0x7f8f('0x1'));var fs=require('fs');var path=require('path');var util=require(_0x7f8f('0x2'));var tnef=require(_0x7f8f('0x3'));var logger=require('../../../config/logger')(_0x7f8f('0x4'));var config=require('../../../config/environment');function getAddress(_0x2c4c9c){var _0x1c5c52=_[_0x7f8f('0x5')](_0x2c4c9c[_0x7f8f('0x6')]);return _0x1c5c52&&_0x1c5c52['address']?_0x1c5c52[_0x7f8f('0x7')]:_0x7f8f('0x8')+random[_0x7f8f('0x9')](0x6)+_0x7f8f('0xa');}function getFirstName(_0x5dfc53){var _0x252ee3=_[_0x7f8f('0x5')](_0x5dfc53['value']);if(_0x252ee3&&_0x252ee3['name']){if(_0x252ee3['name']==_0x252ee3['address']){return _0x252ee3[_0x7f8f('0xb')]['substring'](0x0,_0x252ee3[_0x7f8f('0xb')][_0x7f8f('0xc')]('@'));}var _0x27281e=_0x252ee3[_0x7f8f('0xb')][_0x7f8f('0xd')](/\s+/);return _0x27281e[_0x7f8f('0xe')]>0x1?_0x27281e[_0x7f8f('0xf')](0x0,-0x1)[_0x7f8f('0x10')]('\x20'):_0x252ee3[_0x7f8f('0xb')];}return _0x7f8f('0x11');}function getLastName(_0x57409a){var _0x25f1ba=_[_0x7f8f('0x5')](_0x57409a['value']);if(_0x25f1ba&&_0x25f1ba['name']){var _0x5ba403=_0x25f1ba[_0x7f8f('0xb')][_0x7f8f('0xd')](/\s+/);return _0x5ba403[_0x7f8f('0xe')]>0x1?_0x5ba403[_0x7f8f('0x12')]():'';}return'';}function getMails(_0x541bf8,_0xb1662){var _0xa7791a='';for(var _0x36a4d7=0x0;_0x36a4d7<_0xb1662[_0x7f8f('0x6')][_0x7f8f('0xe')];_0x36a4d7+=0x1){if(_0xb1662[_0x7f8f('0x6')][_0x36a4d7][_0x7f8f('0x7')]!==_0x541bf8){if(!_[_0x7f8f('0x13')](_0xa7791a)){_0xa7791a+=',';}_0xa7791a+=_0xb1662[_0x7f8f('0x6')][_0x36a4d7]['name']||_0xb1662[_0x7f8f('0x6')][_0x36a4d7]['address']||'';_0xa7791a+='\x20<';_0xa7791a+=_0xb1662[_0x7f8f('0x6')][_0x36a4d7][_0x7f8f('0x7')]?_0xb1662[_0x7f8f('0x6')][_0x36a4d7][_0x7f8f('0x7')]:'anonymous'+random[_0x7f8f('0x9')](0x6)+_0x7f8f('0xa');_0xa7791a+='>';}}return _0xa7791a;}function parseEmail(_0x503bce,_0x2e0819,_0x1210cd){if(_0x2e0819[_0x7f8f('0x14')]){var _0x471460=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x471460['test'](_0x2e0819[_0x7f8f('0x14')])){_0x2e0819[_0x7f8f('0x14')]=_0x2e0819[_0x7f8f('0x14')][_0x7f8f('0x15')](_0x471460,'');}if(_0x2e0819[_0x7f8f('0x14')][_0x7f8f('0x16')](_0x7f8f('0x17'))){var _0x598f7d=[];_0x2e0819['attachments']['forEach'](_0x104665=>{_0x598f7d[_0x7f8f('0x18')](_0x104665[_0x7f8f('0x19')]);});var _0x120ca8=/<img.*? _SRC(.*?)[^>]+>/g;var _0x29c32f=/src="data:image\/([a-zA-Z]*);base64,([^"]*)"/g;var _0x39a90a=_0x2e0819[_0x7f8f('0x14')][_0x7f8f('0x15')](_0x29c32f,_0x7f8f('0x1a'));var _0x1721a3=_0x39a90a['replace'](_0x120ca8,'_IMG');for(let _0x13d2e1=0x0;_0x13d2e1<_0x598f7d[_0x7f8f('0xe')];_0x13d2e1++){var _0xc11efa=_0x1721a3[_0x7f8f('0x15')]('_IMG',_0x7f8f('0x1b')+_0x598f7d[_0x13d2e1]+_0x7f8f('0x1c'));_0x1721a3=_0xc11efa;}_0x2e0819[_0x7f8f('0x14')]=_0x1721a3;}_0x1210cd['body']=_0x2e0819[_0x7f8f('0x14')];}else{_0x1210cd[_0x7f8f('0x1d')]=_0x2e0819['textAsHtml'];}if(_0x1210cd['body']){_0x1210cd['body']=_0x1210cd[_0x7f8f('0x1d')][_0x7f8f('0x15')](/<a\s+/gi,_0x7f8f('0x1e'));}if(_0x2e0819['to']){_0x1210cd[_0x7f8f('0x1f')][_0x7f8f('0x20')]=getMails(null,_0x2e0819['to']);var _0x1e407e=getMails(_0x503bce[_0x7f8f('0x4')],_0x2e0819['to']);if(!_[_0x7f8f('0x13')](_0x1e407e)){_0x1210cd[_0x7f8f('0x1f')]['cc']+=_0x1e407e;}}if(_0x2e0819['cc']){_0x1210cd[_0x7f8f('0x1f')][_0x7f8f('0x21')]=getMails(null,_0x2e0819['cc']);var _0x1cf04c=getMails(_0x503bce['email'],_0x2e0819['cc']);if(!_[_0x7f8f('0x13')](_0x1cf04c)){if(!_[_0x7f8f('0x13')](_0x1210cd[_0x7f8f('0x1f')]['cc'])){_0x1210cd[_0x7f8f('0x1f')]['cc']+=',';}_0x1210cd[_0x7f8f('0x1f')]['cc']+=_0x1cf04c;}}if(!_[_0x7f8f('0x16')](_0x1210cd[_0x7f8f('0x1f')]['originTo'],_0x503bce[_0x7f8f('0x4')])&&!_['includes'](_0x1210cd[_0x7f8f('0x1f')][_0x7f8f('0x21')],_0x503bce[_0x7f8f('0x4')])){_0x1210cd[_0x7f8f('0x1f')]['bcc']=_0x503bce['email'];}if(_0x2e0819['attachments']&&_0x2e0819[_0x7f8f('0x22')][_0x7f8f('0xe')]){_0x1210cd[_0x7f8f('0x1f')]['attach']=_0x2e0819[_0x7f8f('0x22')][_0x7f8f('0xe')];for(var _0x20569b=0x0,_0x5d1038,_0x18a69e,_0x5641fb;_0x20569b<_0x2e0819['attachments'][_0x7f8f('0xe')];_0x20569b+=0x1){if(_0x2e0819[_0x7f8f('0x22')][_0x20569b][_0x7f8f('0x23')]===_0x7f8f('0x24')){_0x5641fb=_0x2e0819[_0x7f8f('0x22')][_0x20569b][_0x7f8f('0x19')]||_0x7f8f('0x25')+_0x20569b;_0x5d1038=util[_0x7f8f('0x26')](_0x7f8f('0x27'),Date[_0x7f8f('0x28')](),_0x5641fb);_0x18a69e=path['join'](config[_0x7f8f('0x29')],_0x7f8f('0x2a'),_0x5d1038);fs['writeFileSync'](_0x18a69e,_0x2e0819[_0x7f8f('0x22')][_0x20569b][_0x7f8f('0x2b')]);_0x1210cd[_0x7f8f('0x1f')][_0x7f8f('0x22')]['push']({'name':_0x5641fb,'basename':_0x5d1038,'type':_0x2e0819[_0x7f8f('0x22')][_0x20569b][_0x7f8f('0x2c')],'filepath':_0x18a69e});}}}if(_0x2e0819[_0x7f8f('0x2d')]&&_0x2e0819[_0x7f8f('0x2d')][_0x7f8f('0xe')]>0x0){var _0x328dae=[_0x7f8f('0x2e'),_0x7f8f('0x2f'),_0x7f8f('0x30')];for(var _0x48cf67=0x0;_0x48cf67<_0x2e0819[_0x7f8f('0x2d')][_0x7f8f('0xe')];_0x48cf67++){var _0x1cfc0e=_[_0x7f8f('0xc')](_0x328dae,_0x2e0819[_0x7f8f('0x2d')][_0x48cf67][_0x7f8f('0x31')]);if(_0x1cfc0e>=0x0){_0x1210cd[_0x7f8f('0x32')]=!![];}}}return _0x1210cd;}exports[_0x7f8f('0x33')]=function(_0x55f8a3,_0x309e86,_0x34f616){var _0x2be7d9={'from':getAddress(_0x309e86[_0x7f8f('0x34')]),'firstName':getFirstName(_0x309e86[_0x7f8f('0x34')]),'lastName':getLastName(_0x309e86[_0x7f8f('0x34')]),'mapKey':_0x7f8f('0x4'),'message':{'from':getMails(_0x55f8a3[_0x7f8f('0x4')],_0x309e86['from']),'to':_0x55f8a3[_0x7f8f('0x4')],'cc':'','subject':_0x309e86['subject']||'','messageId':_0x309e86['messageId'],'inReplyTo':_0x309e86[_0x7f8f('0x35')],'sentAt':moment(_0x309e86['date'])['format'](_0x7f8f('0x36')),'attach':![],'attachments':[]}};if(!_0x309e86['html']&&!_0x309e86[_0x7f8f('0x37')]){var _0x5d8d37=_[_0x7f8f('0x38')](_0x309e86[_0x7f8f('0x22')],{'contentType':'application/ms-tnef'});if(_0x5d8d37['length']){logger[_0x7f8f('0x39')](util['format'](_0x7f8f('0x3a'),_0x309e86[_0x7f8f('0x3b')]));tnef[_0x7f8f('0x3c')](_[_0x7f8f('0x3d')](_0x5d8d37)[_0x7f8f('0x2b')],function(_0x571717,_0x168284){if(_0x168284){_0x309e86[_0x7f8f('0x14')]=_0x168284[_0x7f8f('0x3e')]||_0x7f8f('0x3f');if(_0x168284[_0x7f8f('0x40')]){for(var _0x166c3d=0x0;_0x166c3d<_0x168284['Attachments']['length'];_0x166c3d++){var _0x1334e2=_0x168284[_0x7f8f('0x40')][_0x166c3d];_0x2be7d9[_0x7f8f('0x1f')][_0x7f8f('0x22')][_0x7f8f('0x18')]({'filename':_0x1334e2[_0x7f8f('0x41')],'content':Buffer[_0x7f8f('0x34')](_0x1334e2[_0x7f8f('0x42')])});}}}_0x34f616(parseEmail(_0x55f8a3,_0x309e86,_0x2be7d9));});}else{_0x309e86[_0x7f8f('0x14')]=_0x7f8f('0x3f');_0x34f616(parseEmail(_0x55f8a3,_0x309e86,_0x2be7d9));}}else{_0x34f616(parseEmail(_0x55f8a3,_0x309e86,_0x2be7d9));}};exports['generateXOAuth2Token']=function(_0x39a5ab,_0x1caa1c){var _0x17e6e3=['user='+(_0x1caa1c||''),_0x7f8f('0x43')+_0x39a5ab,'',''];return Buffer[_0x7f8f('0x34')](_0x17e6e3[_0x7f8f('0x10')]('\ 1'),'utf-8')[_0x7f8f('0x44')](_0x7f8f('0x45'));};