Built motion from commit (unavailable).|2.5.25
[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 _0x7d56=['originTo','isEmpty','originCc','includes','bcc','attach','type','attachment','filename','attachment-','format','%s-%s','now','root','server/files/attachments/','writeFileSync','from','messageId','inReplyTo','YYYY-MM-DD\x20HH:mm:ss','info','parseBuffer','first','content','BodyHTML','Attachments','Title','Data','getHost','gmail','imap.mail.yahoo.com','127.0.0.1','lodash','moment','node-tnef','../../../config/logger','email','../../../config/environment','head','value','address','anonymous','@xcally.com','name','slice','join','Anonymous','split','length','pop','html','replace','attachments','forEach','push','_SRC','_IMG','<span\x20style=font-weight:bold>\x20[Attachment:\x20','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','message'];(function(_0xa8185c,_0xc72a3){var _0x23804b=function(_0x2cc53c){while(--_0x2cc53c){_0xa8185c['push'](_0xa8185c['shift']());}};_0x23804b(++_0xc72a3);}(_0x7d56,0x156));var _0x67d5=function(_0x202434,_0x1ccc96){_0x202434=_0x202434-0x0;var _0x9f27c5=_0x7d56[_0x202434];return _0x9f27c5;};'use strict';var _=require(_0x67d5('0x0'));var moment=require(_0x67d5('0x1'));var random=require('randomstring');var fs=require('fs');var path=require('path');var util=require('util');var tnef=require(_0x67d5('0x2'));var logger=require(_0x67d5('0x3'))(_0x67d5('0x4'));var config=require(_0x67d5('0x5'));function getAddress(_0x3385a4){var _0xd47b5a=_[_0x67d5('0x6')](_0x3385a4[_0x67d5('0x7')]);return _0xd47b5a&&_0xd47b5a[_0x67d5('0x8')]?_0xd47b5a['address']:_0x67d5('0x9')+random['generate'](0x6)+_0x67d5('0xa');}function getFirstName(_0x4b8e8b){var _0x12b4f6=_['head'](_0x4b8e8b[_0x67d5('0x7')]);if(_0x12b4f6&&_0x12b4f6[_0x67d5('0xb')]){var _0x4d1b05=_0x12b4f6[_0x67d5('0xb')]['split'](/\s+/);return _0x4d1b05['length']?_0x4d1b05[_0x67d5('0xc')](0x0,-0x1)[_0x67d5('0xd')]('\x20'):_0x12b4f6[_0x67d5('0xb')];}return _0x67d5('0xe');}function getLastName(_0x429df0){var _0x5ece5e=_[_0x67d5('0x6')](_0x429df0[_0x67d5('0x7')]);if(_0x5ece5e&&_0x5ece5e['name']){var _0x3a4140=_0x5ece5e[_0x67d5('0xb')][_0x67d5('0xf')](/\s+/);return _0x3a4140[_0x67d5('0x10')]>0x1?_0x3a4140[_0x67d5('0x11')]():'';}return'';}function getMails(_0x4f8921,_0x554d6d){var _0x37003='';for(var _0x372614=0x0;_0x372614<_0x554d6d[_0x67d5('0x7')][_0x67d5('0x10')];_0x372614+=0x1){if(_0x554d6d[_0x67d5('0x7')][_0x372614][_0x67d5('0x8')]!==_0x4f8921){if(!_['isEmpty'](_0x37003)){_0x37003+=',';}_0x37003+=_0x554d6d[_0x67d5('0x7')][_0x372614][_0x67d5('0xb')]||_0x554d6d[_0x67d5('0x7')][_0x372614]['address']||'';_0x37003+='\x20<';_0x37003+=_0x554d6d[_0x67d5('0x7')][_0x372614][_0x67d5('0x8')]?_0x554d6d[_0x67d5('0x7')][_0x372614][_0x67d5('0x8')]:_0x67d5('0x9')+random['generate'](0x6)+_0x67d5('0xa');_0x37003+='>';}}return _0x37003;}function parseEmail(_0x145748,_0x9ffc68,_0x30f053){if(_0x9ffc68[_0x67d5('0x12')]){var _0x3d12fe=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x3d12fe['test'](_0x9ffc68['html'])){_0x9ffc68['html']=_0x9ffc68['html'][_0x67d5('0x13')](_0x3d12fe,'');}if(_0x9ffc68[_0x67d5('0x12')]['includes']('<img')){var _0x3e3d8c=[];_0x9ffc68[_0x67d5('0x14')][_0x67d5('0x15')](_0x10e0d2=>{_0x3e3d8c[_0x67d5('0x16')](_0x10e0d2['filename']);});var _0x304833=/<img.*? _SRC(.*?)[^\>]+>/g;var _0x2be9ec=/src=\"data:image\/([a-zA-Z]*);base64,([^\"]*)\"/g;var _0x549bdc=_0x9ffc68[_0x67d5('0x12')][_0x67d5('0x13')](_0x2be9ec,_0x67d5('0x17'));var _0xe90183=_0x549bdc['replace'](_0x304833,_0x67d5('0x18'));for(let _0x187ab4=0x0;_0x187ab4<_0x3e3d8c[_0x67d5('0x10')];_0x187ab4++){var _0x5277b4=_0xe90183[_0x67d5('0x13')](_0x67d5('0x18'),_0x67d5('0x19')+_0x3e3d8c[_0x187ab4]+'\x20]</span>');_0xe90183=_0x5277b4;}_0x9ffc68[_0x67d5('0x12')]=_0xe90183;}_0x30f053[_0x67d5('0x1a')]=_0x9ffc68[_0x67d5('0x12')];}else{_0x30f053[_0x67d5('0x1a')]=_0x9ffc68[_0x67d5('0x1b')];}if(_0x30f053[_0x67d5('0x1a')]){_0x30f053[_0x67d5('0x1a')]=_0x30f053[_0x67d5('0x1a')][_0x67d5('0x13')](/<a\s+/gi,_0x67d5('0x1c'));}if(_0x9ffc68['to']){_0x30f053[_0x67d5('0x1d')][_0x67d5('0x1e')]=getMails(null,_0x9ffc68['to']);var _0x45ec5b=getMails(_0x145748[_0x67d5('0x4')],_0x9ffc68['to']);if(!_[_0x67d5('0x1f')](_0x45ec5b)){_0x30f053[_0x67d5('0x1d')]['cc']+=_0x45ec5b;}}if(_0x9ffc68['cc']){_0x30f053['message'][_0x67d5('0x20')]=getMails(null,_0x9ffc68['cc']);var _0x36f0ae=getMails(_0x145748[_0x67d5('0x4')],_0x9ffc68['cc']);if(!_[_0x67d5('0x1f')](_0x36f0ae)){if(!_['isEmpty'](_0x30f053[_0x67d5('0x1d')]['cc'])){_0x30f053[_0x67d5('0x1d')]['cc']+=',';}_0x30f053['message']['cc']+=_0x36f0ae;}}if(!_[_0x67d5('0x21')](_0x30f053[_0x67d5('0x1d')][_0x67d5('0x1e')],_0x145748[_0x67d5('0x4')])&&!_[_0x67d5('0x21')](_0x30f053['message'][_0x67d5('0x20')],_0x145748['email'])){_0x30f053[_0x67d5('0x1d')][_0x67d5('0x22')]=_0x145748['email'];}if(_0x9ffc68[_0x67d5('0x14')]&&_0x9ffc68[_0x67d5('0x14')][_0x67d5('0x10')]){_0x30f053['message'][_0x67d5('0x23')]=_0x9ffc68['attachments']['length'];for(var _0x2e4789=0x0,_0x4a301d,_0x59579a,_0x465e37;_0x2e4789<_0x9ffc68['attachments'][_0x67d5('0x10')];_0x2e4789+=0x1){if(_0x9ffc68[_0x67d5('0x14')][_0x2e4789][_0x67d5('0x24')]===_0x67d5('0x25')){_0x465e37=_0x9ffc68[_0x67d5('0x14')][_0x2e4789][_0x67d5('0x26')]||_0x67d5('0x27')+_0x2e4789;_0x4a301d=util[_0x67d5('0x28')](_0x67d5('0x29'),Date[_0x67d5('0x2a')](),_0x465e37);_0x59579a=path[_0x67d5('0xd')](config[_0x67d5('0x2b')],_0x67d5('0x2c'),_0x4a301d);fs[_0x67d5('0x2d')](_0x59579a,_0x9ffc68['attachments'][_0x2e4789]['content']);_0x30f053['message']['attachments'][_0x67d5('0x16')]({'name':_0x465e37,'basename':_0x4a301d,'type':_0x9ffc68['attachments'][_0x2e4789]['contentType'],'filepath':_0x59579a});}}}return _0x30f053;}exports['parser']=function(_0x4f39d0,_0x1e9f80,_0x40f503){var _0x119a54={'from':getAddress(_0x1e9f80['from']),'firstName':getFirstName(_0x1e9f80[_0x67d5('0x2e')]),'lastName':getLastName(_0x1e9f80[_0x67d5('0x2e')]),'mapKey':_0x67d5('0x4'),'message':{'from':getMails(_0x4f39d0[_0x67d5('0x4')],_0x1e9f80[_0x67d5('0x2e')]),'to':_0x4f39d0[_0x67d5('0x4')],'cc':'','subject':_0x1e9f80['subject']||'','messageId':_0x1e9f80[_0x67d5('0x2f')],'inReplyTo':_0x1e9f80[_0x67d5('0x30')],'sentAt':moment(_0x1e9f80['date'])[_0x67d5('0x28')](_0x67d5('0x31')),'attach':![],'attachments':[]}};if(!_0x1e9f80[_0x67d5('0x12')]&&!_0x1e9f80[_0x67d5('0x1b')]){var _0x5954d4=_['filter'](_0x1e9f80[_0x67d5('0x14')],{'contentType':'application/ms-tnef'});if(_0x5954d4[_0x67d5('0x10')]){logger[_0x67d5('0x32')](util[_0x67d5('0x28')]('Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system',_0x1e9f80[_0x67d5('0x2f')]));tnef[_0x67d5('0x33')](_[_0x67d5('0x34')](_0x5954d4)[_0x67d5('0x35')],function(_0x1dccc8,_0x40f892){if(_0x40f892){_0x1e9f80[_0x67d5('0x12')]=_0x40f892[_0x67d5('0x36')]||'<p>Empty\x20message</p>';if(_0x40f892[_0x67d5('0x37')]){for(var _0x2f0fc2=0x0;_0x2f0fc2<_0x40f892[_0x67d5('0x37')][_0x67d5('0x10')];_0x2f0fc2++){var _0x15bb27=_0x40f892['Attachments'][_0x2f0fc2];_0x119a54[_0x67d5('0x1d')][_0x67d5('0x14')][_0x67d5('0x16')]({'filename':_0x15bb27[_0x67d5('0x38')],'content':new Buffer(_0x15bb27[_0x67d5('0x39')])});}}}_0x40f503(parseEmail(_0x4f39d0,_0x1e9f80,_0x119a54));});}else{_0x1e9f80[_0x67d5('0x12')]='<p>Empty\x20message</p>';_0x40f503(parseEmail(_0x4f39d0,_0x1e9f80,_0x119a54));}}else{_0x40f503(parseEmail(_0x4f39d0,_0x1e9f80,_0x119a54));}};exports[_0x67d5('0x3a')]=function(_0x484410){switch(_0x484410){case _0x67d5('0x3b'):return'imap.gmail.com';case'yahoo':return _0x67d5('0x3c');case'hotmail':return'imap-mail.outlook.com';case'outlook365':return'outlook.office365.com';}return _0x67d5('0x3d');};