Built motion from commit (unavailable).|2.6.0
[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 _0x1601=['test','replace','includes','<img','attachments','push','filename','_SRC','_IMG','<span\x20style=font-weight:bold>\x20[Attachment:\x20','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','originTo','originCc','email','message','attach','type','attachment','format','%s-%s','now','root','server/files/attachments/','writeFileSync','contentType','parser','from','subject','messageId','inReplyTo','date','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','first','BodyHTML','<p>Empty\x20message</p>','Attachments','Title','Data','getHost','gmail','imap.gmail.com','yahoo','imap.mail.yahoo.com','outlook365','outlook.office365.com','127.0.0.1','moment','path','node-tnef','../../../config/environment','address','anonymous','@xcally.com','head','value','name','split','length','slice','join','Anonymous','pop','isEmpty','html'];(function(_0x5303e1,_0x284a21){var _0x22221b=function(_0x574177){while(--_0x574177){_0x5303e1['push'](_0x5303e1['shift']());}};_0x22221b(++_0x284a21);}(_0x1601,0x151));var _0x1160=function(_0x57cb4f,_0x541f57){_0x57cb4f=_0x57cb4f-0x0;var _0x29bb45=_0x1601[_0x57cb4f];return _0x29bb45;};'use strict';var _=require('lodash');var moment=require(_0x1160('0x0'));var random=require('randomstring');var fs=require('fs');var path=require(_0x1160('0x1'));var util=require('util');var tnef=require(_0x1160('0x2'));var logger=require('../../../config/logger')('email');var config=require(_0x1160('0x3'));function getAddress(_0xae5d8a){var _0x317035=_['head'](_0xae5d8a['value']);return _0x317035&&_0x317035[_0x1160('0x4')]?_0x317035[_0x1160('0x4')]:_0x1160('0x5')+random['generate'](0x6)+_0x1160('0x6');}function getFirstName(_0x526f5c){var _0xc905d4=_[_0x1160('0x7')](_0x526f5c[_0x1160('0x8')]);if(_0xc905d4&&_0xc905d4[_0x1160('0x9')]){var _0x536fbe=_0xc905d4[_0x1160('0x9')][_0x1160('0xa')](/\s+/);return _0x536fbe[_0x1160('0xb')]?_0x536fbe[_0x1160('0xc')](0x0,-0x1)[_0x1160('0xd')]('\x20'):_0xc905d4['name'];}return _0x1160('0xe');}function getLastName(_0x59e137){var _0x3b14d1=_[_0x1160('0x7')](_0x59e137[_0x1160('0x8')]);if(_0x3b14d1&&_0x3b14d1[_0x1160('0x9')]){var _0x21e716=_0x3b14d1['name'][_0x1160('0xa')](/\s+/);return _0x21e716[_0x1160('0xb')]>0x1?_0x21e716[_0x1160('0xf')]():'';}return'';}function getMails(_0x1c28db,_0x41bc7d){var _0x211b89='';for(var _0x433818=0x0;_0x433818<_0x41bc7d[_0x1160('0x8')][_0x1160('0xb')];_0x433818+=0x1){if(_0x41bc7d[_0x1160('0x8')][_0x433818][_0x1160('0x4')]!==_0x1c28db){if(!_[_0x1160('0x10')](_0x211b89)){_0x211b89+=',';}_0x211b89+=_0x41bc7d[_0x1160('0x8')][_0x433818][_0x1160('0x9')]||_0x41bc7d[_0x1160('0x8')][_0x433818][_0x1160('0x4')]||'';_0x211b89+='\x20<';_0x211b89+=_0x41bc7d[_0x1160('0x8')][_0x433818][_0x1160('0x4')]?_0x41bc7d['value'][_0x433818]['address']:_0x1160('0x5')+random['generate'](0x6)+'@xcally.com';_0x211b89+='>';}}return _0x211b89;}function parseEmail(_0x24bbf4,_0x7efc43,_0xe7bfcb){if(_0x7efc43[_0x1160('0x11')]){var _0x4abddd=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x4abddd[_0x1160('0x12')](_0x7efc43['html'])){_0x7efc43[_0x1160('0x11')]=_0x7efc43[_0x1160('0x11')][_0x1160('0x13')](_0x4abddd,'');}if(_0x7efc43[_0x1160('0x11')][_0x1160('0x14')](_0x1160('0x15'))){var _0x55328b=[];_0x7efc43[_0x1160('0x16')]['forEach'](_0x27cd86=>{_0x55328b[_0x1160('0x17')](_0x27cd86[_0x1160('0x18')]);});var _0x1a06d9=/<img.*? _SRC(.*?)[^\>]+>/g;var _0x32b422=/src=\"data:image\/([a-zA-Z]*);base64,([^\"]*)\"/g;var _0x4a9bfd=_0x7efc43[_0x1160('0x11')][_0x1160('0x13')](_0x32b422,_0x1160('0x19'));var _0x52d053=_0x4a9bfd['replace'](_0x1a06d9,_0x1160('0x1a'));for(let _0xaf9440=0x0;_0xaf9440<_0x55328b[_0x1160('0xb')];_0xaf9440++){var _0x4614eb=_0x52d053['replace'](_0x1160('0x1a'),_0x1160('0x1b')+_0x55328b[_0xaf9440]+'\x20]</span>');_0x52d053=_0x4614eb;}_0x7efc43[_0x1160('0x11')]=_0x52d053;}_0xe7bfcb[_0x1160('0x1c')]=_0x7efc43[_0x1160('0x11')];}else{_0xe7bfcb[_0x1160('0x1c')]=_0x7efc43[_0x1160('0x1d')];}if(_0xe7bfcb['body']){_0xe7bfcb[_0x1160('0x1c')]=_0xe7bfcb[_0x1160('0x1c')][_0x1160('0x13')](/<a\s+/gi,_0x1160('0x1e'));}if(_0x7efc43['to']){_0xe7bfcb['message'][_0x1160('0x1f')]=getMails(null,_0x7efc43['to']);var _0x1cc00e=getMails(_0x24bbf4['email'],_0x7efc43['to']);if(!_[_0x1160('0x10')](_0x1cc00e)){_0xe7bfcb['message']['cc']+=_0x1cc00e;}}if(_0x7efc43['cc']){_0xe7bfcb['message'][_0x1160('0x20')]=getMails(null,_0x7efc43['cc']);var _0x39a505=getMails(_0x24bbf4[_0x1160('0x21')],_0x7efc43['cc']);if(!_[_0x1160('0x10')](_0x39a505)){if(!_['isEmpty'](_0xe7bfcb[_0x1160('0x22')]['cc'])){_0xe7bfcb[_0x1160('0x22')]['cc']+=',';}_0xe7bfcb['message']['cc']+=_0x39a505;}}if(!_[_0x1160('0x14')](_0xe7bfcb[_0x1160('0x22')][_0x1160('0x1f')],_0x24bbf4[_0x1160('0x21')])&&!_[_0x1160('0x14')](_0xe7bfcb[_0x1160('0x22')][_0x1160('0x20')],_0x24bbf4[_0x1160('0x21')])){_0xe7bfcb[_0x1160('0x22')]['bcc']=_0x24bbf4[_0x1160('0x21')];}if(_0x7efc43[_0x1160('0x16')]&&_0x7efc43[_0x1160('0x16')]['length']){_0xe7bfcb[_0x1160('0x22')][_0x1160('0x23')]=_0x7efc43[_0x1160('0x16')][_0x1160('0xb')];for(var _0x4f5143=0x0,_0xcf20b1,_0x1e3eba,_0x582a55;_0x4f5143<_0x7efc43[_0x1160('0x16')][_0x1160('0xb')];_0x4f5143+=0x1){if(_0x7efc43[_0x1160('0x16')][_0x4f5143][_0x1160('0x24')]===_0x1160('0x25')){_0x582a55=_0x7efc43[_0x1160('0x16')][_0x4f5143][_0x1160('0x18')]||'attachment-'+_0x4f5143;_0xcf20b1=util[_0x1160('0x26')](_0x1160('0x27'),Date[_0x1160('0x28')](),_0x582a55);_0x1e3eba=path[_0x1160('0xd')](config[_0x1160('0x29')],_0x1160('0x2a'),_0xcf20b1);fs[_0x1160('0x2b')](_0x1e3eba,_0x7efc43[_0x1160('0x16')][_0x4f5143]['content']);_0xe7bfcb[_0x1160('0x22')]['attachments']['push']({'name':_0x582a55,'basename':_0xcf20b1,'type':_0x7efc43[_0x1160('0x16')][_0x4f5143][_0x1160('0x2c')],'filepath':_0x1e3eba});}}}return _0xe7bfcb;}exports[_0x1160('0x2d')]=function(_0x20ea47,_0x11607b,_0x43a4b9){var _0x2cd6d3={'from':getAddress(_0x11607b[_0x1160('0x2e')]),'firstName':getFirstName(_0x11607b['from']),'lastName':getLastName(_0x11607b[_0x1160('0x2e')]),'mapKey':_0x1160('0x21'),'message':{'from':getMails(_0x20ea47[_0x1160('0x21')],_0x11607b['from']),'to':_0x20ea47[_0x1160('0x21')],'cc':'','subject':_0x11607b[_0x1160('0x2f')]||'','messageId':_0x11607b[_0x1160('0x30')],'inReplyTo':_0x11607b[_0x1160('0x31')],'sentAt':moment(_0x11607b[_0x1160('0x32')])[_0x1160('0x26')](_0x1160('0x33')),'attach':![],'attachments':[]}};if(!_0x11607b[_0x1160('0x11')]&&!_0x11607b[_0x1160('0x1d')]){var _0x3020c1=_[_0x1160('0x34')](_0x11607b[_0x1160('0x16')],{'contentType':_0x1160('0x35')});if(_0x3020c1[_0x1160('0xb')]){logger[_0x1160('0x36')](util['format'](_0x1160('0x37'),_0x11607b[_0x1160('0x30')]));tnef[_0x1160('0x38')](_[_0x1160('0x39')](_0x3020c1)['content'],function(_0x413edf,_0x293007){if(_0x293007){_0x11607b[_0x1160('0x11')]=_0x293007[_0x1160('0x3a')]||_0x1160('0x3b');if(_0x293007[_0x1160('0x3c')]){for(var _0x472a39=0x0;_0x472a39<_0x293007[_0x1160('0x3c')][_0x1160('0xb')];_0x472a39++){var _0x16d6f9=_0x293007[_0x1160('0x3c')][_0x472a39];_0x2cd6d3[_0x1160('0x22')][_0x1160('0x16')]['push']({'filename':_0x16d6f9[_0x1160('0x3d')],'content':new Buffer(_0x16d6f9[_0x1160('0x3e')])});}}}_0x43a4b9(parseEmail(_0x20ea47,_0x11607b,_0x2cd6d3));});}else{_0x11607b[_0x1160('0x11')]='<p>Empty\x20message</p>';_0x43a4b9(parseEmail(_0x20ea47,_0x11607b,_0x2cd6d3));}}else{_0x43a4b9(parseEmail(_0x20ea47,_0x11607b,_0x2cd6d3));}};exports[_0x1160('0x3f')]=function(_0xdc8ce1){switch(_0xdc8ce1){case _0x1160('0x40'):return _0x1160('0x41');case _0x1160('0x42'):return _0x1160('0x43');case'hotmail':return'imap-mail.outlook.com';case _0x1160('0x44'):return _0x1160('0x45');}return _0x1160('0x46');};