Built motion from commit (unavailable).|2.3.1
[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 _0xe062=['info','first','BodyHTML','<p>Empty\x20message</p>','Attachments','Title','Data','getHost','gmail','imap.gmail.com','yahoo','imap-mail.outlook.com','127.0.0.1','lodash','moment','util','node-tnef','../../../config/logger','email','../../../config/environment','head','value','@xcally.com','name','split','length','slice','join','Anonymous','address','anonymous','html','test','body','<a\x20target=\x22_blank\x22\x20','message','originTo','isEmpty','originCc','includes','bcc','attachments','type','attachment','format','%s-%s','now','root','writeFileSync','content','push','parser','from','subject','messageId','inReplyTo','date','YYYY-MM-DD\x20HH:mm:ss','textAsHtml','filter','application/ms-tnef'];(function(_0x16bfc5,_0x4f9dc3){var _0x43b820=function(_0x363ced){while(--_0x363ced){_0x16bfc5['push'](_0x16bfc5['shift']());}};_0x43b820(++_0x4f9dc3);}(_0xe062,0x1b8));var _0x2e06=function(_0x5549f1,_0x56cf72){_0x5549f1=_0x5549f1-0x0;var _0x610f44=_0xe062[_0x5549f1];return _0x610f44;};'use strict';var _=require(_0x2e06('0x0'));var moment=require(_0x2e06('0x1'));var random=require('randomstring');var fs=require('fs');var path=require('path');var util=require(_0x2e06('0x2'));var tnef=require(_0x2e06('0x3'));var logger=require(_0x2e06('0x4'))(_0x2e06('0x5'));var config=require(_0x2e06('0x6'));function getAddress(_0x28e970){var _0x22aacc=_[_0x2e06('0x7')](_0x28e970[_0x2e06('0x8')]);return _0x22aacc&&_0x22aacc['address']?_0x22aacc['address']:'anonymous'+random['generate'](0x6)+_0x2e06('0x9');}function getFirstName(_0x2180bd){var _0x5103c6=_['head'](_0x2180bd[_0x2e06('0x8')]);if(_0x5103c6&&_0x5103c6[_0x2e06('0xa')]){var _0x6be39d=_0x5103c6[_0x2e06('0xa')][_0x2e06('0xb')](/\s+/);return _0x6be39d[_0x2e06('0xc')]?_0x6be39d[_0x2e06('0xd')](0x0,-0x1)[_0x2e06('0xe')]('\x20'):_0x5103c6[_0x2e06('0xa')];}return _0x2e06('0xf');}function getLastName(_0x4bdb70){var _0x2ee63d=_['head'](_0x4bdb70[_0x2e06('0x8')]);if(_0x2ee63d&&_0x2ee63d[_0x2e06('0xa')]){var _0x4c140e=_0x2ee63d['name'][_0x2e06('0xb')](/\s+/);return _0x4c140e[_0x2e06('0xc')]>0x1?_0x4c140e['pop']():'';}return'';}function getMails(_0x48d757,_0x289134){var _0x483084='';for(var _0x4935b3=0x0;_0x4935b3<_0x289134[_0x2e06('0x8')][_0x2e06('0xc')];_0x4935b3+=0x1){if(_0x289134['value'][_0x4935b3][_0x2e06('0x10')]!==_0x48d757){if(!_['isEmpty'](_0x483084)){_0x483084+=',';}_0x483084+=_0x289134[_0x2e06('0x8')][_0x4935b3]['name']||_0x289134[_0x2e06('0x8')][_0x4935b3][_0x2e06('0x10')]||'';_0x483084+='\x20<';_0x483084+=_0x289134[_0x2e06('0x8')][_0x4935b3][_0x2e06('0x10')]?_0x289134[_0x2e06('0x8')][_0x4935b3][_0x2e06('0x10')]:_0x2e06('0x11')+random['generate'](0x6)+_0x2e06('0x9');_0x483084+='>';}}return _0x483084;}function parseEmail(_0xf5317,_0x393603,_0x4091ce){if(_0x393603[_0x2e06('0x12')]){var _0x43a978=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x43a978[_0x2e06('0x13')](_0x393603[_0x2e06('0x12')])){_0x393603[_0x2e06('0x12')]=_0x393603['html']['replace'](_0x43a978,'');}_0x4091ce['body']=_0x393603['html'];}else{_0x4091ce[_0x2e06('0x14')]=_0x393603['textAsHtml'];}if(_0x4091ce[_0x2e06('0x14')]){_0x4091ce[_0x2e06('0x14')]=_0x4091ce['body']['replace'](/<a\s+/gi,_0x2e06('0x15'));}if(_0x393603['to']){_0x4091ce[_0x2e06('0x16')][_0x2e06('0x17')]=getMails(null,_0x393603['to']);var _0x171cc1=getMails(_0xf5317[_0x2e06('0x5')],_0x393603['to']);if(!_[_0x2e06('0x18')](_0x171cc1)){_0x4091ce['message']['cc']+=_0x171cc1;}}if(_0x393603['cc']){_0x4091ce['message'][_0x2e06('0x19')]=getMails(null,_0x393603['cc']);var _0x51e780=getMails(_0xf5317[_0x2e06('0x5')],_0x393603['cc']);if(!_['isEmpty'](_0x51e780)){if(!_[_0x2e06('0x18')](_0x4091ce['message']['cc'])){_0x4091ce[_0x2e06('0x16')]['cc']+=',';}_0x4091ce[_0x2e06('0x16')]['cc']+=_0x51e780;}}if(!_['includes'](_0x4091ce['message'][_0x2e06('0x17')],_0xf5317[_0x2e06('0x5')])&&!_[_0x2e06('0x1a')](_0x4091ce['message']['originCc'],_0xf5317[_0x2e06('0x5')])){_0x4091ce[_0x2e06('0x16')][_0x2e06('0x1b')]=_0xf5317['email'];}if(_0x393603[_0x2e06('0x1c')]&&_0x393603[_0x2e06('0x1c')]['length']){_0x4091ce['message']['attach']=_0x393603[_0x2e06('0x1c')][_0x2e06('0xc')];for(var _0x42e7d6=0x0,_0x2d45f4,_0x51ce30,_0x25668a;_0x42e7d6<_0x393603[_0x2e06('0x1c')][_0x2e06('0xc')];_0x42e7d6+=0x1){if(_0x393603['attachments'][_0x42e7d6][_0x2e06('0x1d')]===_0x2e06('0x1e')){_0x25668a=_0x393603[_0x2e06('0x1c')][_0x42e7d6]['filename']||'attachment-'+_0x42e7d6;_0x2d45f4=util[_0x2e06('0x1f')](_0x2e06('0x20'),Date[_0x2e06('0x21')](),_0x25668a);_0x51ce30=path[_0x2e06('0xe')](config[_0x2e06('0x22')],'server/files/attachments/',_0x2d45f4);fs[_0x2e06('0x23')](_0x51ce30,_0x393603['attachments'][_0x42e7d6][_0x2e06('0x24')]);_0x4091ce[_0x2e06('0x16')][_0x2e06('0x1c')][_0x2e06('0x25')]({'name':_0x25668a,'basename':_0x2d45f4,'type':_0x393603[_0x2e06('0x1c')][_0x42e7d6]['contentType'],'filepath':_0x51ce30});}}}return _0x4091ce;}exports[_0x2e06('0x26')]=function(_0x4e8b41,_0x593f6f,_0x319bb8){var _0x45c92e={'from':getAddress(_0x593f6f[_0x2e06('0x27')]),'firstName':getFirstName(_0x593f6f[_0x2e06('0x27')]),'lastName':getLastName(_0x593f6f[_0x2e06('0x27')]),'mapKey':_0x2e06('0x5'),'message':{'from':getMails(_0x4e8b41[_0x2e06('0x5')],_0x593f6f[_0x2e06('0x27')]),'to':_0x4e8b41['email'],'cc':'','subject':_0x593f6f[_0x2e06('0x28')]||'','messageId':_0x593f6f[_0x2e06('0x29')],'inReplyTo':_0x593f6f[_0x2e06('0x2a')],'sentAt':moment(_0x593f6f[_0x2e06('0x2b')])[_0x2e06('0x1f')](_0x2e06('0x2c')),'attach':![],'attachments':[]}};if(!_0x593f6f[_0x2e06('0x12')]&&!_0x593f6f[_0x2e06('0x2d')]){var _0x5973b0=_[_0x2e06('0x2e')](_0x593f6f[_0x2e06('0x1c')],{'contentType':_0x2e06('0x2f')});if(_0x5973b0[_0x2e06('0xc')]){logger[_0x2e06('0x30')](util['format']('Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system',_0x593f6f['messageId']));tnef['parseBuffer'](_[_0x2e06('0x31')](_0x5973b0)[_0x2e06('0x24')],function(_0x1fc389,_0x2cd56b){if(_0x2cd56b){_0x593f6f['html']=_0x2cd56b[_0x2e06('0x32')]||_0x2e06('0x33');if(_0x2cd56b[_0x2e06('0x34')]){for(var _0x186c16=0x0;_0x186c16<_0x2cd56b[_0x2e06('0x34')]['length'];_0x186c16++){var _0x2630b3=_0x2cd56b[_0x2e06('0x34')][_0x186c16];_0x45c92e['message'][_0x2e06('0x1c')][_0x2e06('0x25')]({'filename':_0x2630b3[_0x2e06('0x35')],'content':new Buffer(_0x2630b3[_0x2e06('0x36')])});}}}_0x319bb8(parseEmail(_0x4e8b41,_0x593f6f,_0x45c92e));});}else{_0x593f6f[_0x2e06('0x12')]=_0x2e06('0x33');_0x319bb8(parseEmail(_0x4e8b41,_0x593f6f,_0x45c92e));}}else{_0x319bb8(parseEmail(_0x4e8b41,_0x593f6f,_0x45c92e));}};exports[_0x2e06('0x37')]=function(_0x43f676){switch(_0x43f676){case _0x2e06('0x38'):return _0x2e06('0x39');case _0x2e06('0x3a'):return'imap.mail.yahoo.com';case'hotmail':return _0x2e06('0x3b');case'outlook365':return'outlook.office365.com';}return _0x2e06('0x3c');};