0c7b4ad4a1b79aaffe323833594142f65873cf9d
[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 _0xbc46=['join','Anonymous','pop','isEmpty','generate','test','html','replace','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','originTo','message','originCc','includes','bcc','attachments','attachment','filename','attachment-','format','now','root','server/files/attachments/','content','push','contentType','parser','from','subject','messageId','inReplyTo','YYYY-MM-DD\x20HH:mm:ss','filter','application/ms-tnef','info','parseBuffer','<p>Empty\x20message</p>','Attachments','Title','Data','getHost','gmail','imap.gmail.com','yahoo','hotmail','imap-mail.outlook.com','outlook365','outlook.office365.com','lodash','moment','randomstring','path','../../../config/logger','email','../../../config/environment','head','value','address','anonymous','@xcally.com','name','length','slice'];(function(_0x26b308,_0x397dfc){var _0x59478d=function(_0x20b4c4){while(--_0x20b4c4){_0x26b308['push'](_0x26b308['shift']());}};_0x59478d(++_0x397dfc);}(_0xbc46,0x1b1));var _0x6bc4=function(_0x55b8f0,_0x4d0a1c){_0x55b8f0=_0x55b8f0-0x0;var _0x12ec10=_0xbc46[_0x55b8f0];return _0x12ec10;};'use strict';var _=require(_0x6bc4('0x0'));var moment=require(_0x6bc4('0x1'));var random=require(_0x6bc4('0x2'));var fs=require('fs');var path=require(_0x6bc4('0x3'));var util=require('util');var tnef=require('node-tnef');var logger=require(_0x6bc4('0x4'))(_0x6bc4('0x5'));var config=require(_0x6bc4('0x6'));function getAddress(_0x56b6da){var _0x17d3e4=_[_0x6bc4('0x7')](_0x56b6da[_0x6bc4('0x8')]);return _0x17d3e4&&_0x17d3e4[_0x6bc4('0x9')]?_0x17d3e4[_0x6bc4('0x9')]:_0x6bc4('0xa')+random['generate'](0x6)+_0x6bc4('0xb');}function getFirstName(_0x4872d4){var _0x58b22a=_[_0x6bc4('0x7')](_0x4872d4[_0x6bc4('0x8')]);if(_0x58b22a&&_0x58b22a[_0x6bc4('0xc')]){var _0x24746a=_0x58b22a['name']['split'](/\s+/);return _0x24746a[_0x6bc4('0xd')]?_0x24746a[_0x6bc4('0xe')](0x0,-0x1)[_0x6bc4('0xf')]('\x20'):_0x58b22a[_0x6bc4('0xc')];}return _0x6bc4('0x10');}function getLastName(_0x5ed468){var _0x46236c=_[_0x6bc4('0x7')](_0x5ed468[_0x6bc4('0x8')]);if(_0x46236c&&_0x46236c['name']){var _0x3e434c=_0x46236c[_0x6bc4('0xc')]['split'](/\s+/);return _0x3e434c[_0x6bc4('0xd')]>0x1?_0x3e434c[_0x6bc4('0x11')]():'';}return'';}function getMails(_0x52d7f2,_0x474bf7){var _0x2c582c='';for(var _0x57d5b7=0x0;_0x57d5b7<_0x474bf7[_0x6bc4('0x8')][_0x6bc4('0xd')];_0x57d5b7+=0x1){if(_0x474bf7[_0x6bc4('0x8')][_0x57d5b7][_0x6bc4('0x9')]!==_0x52d7f2){if(!_[_0x6bc4('0x12')](_0x2c582c)){_0x2c582c+=',';}_0x2c582c+=_0x474bf7[_0x6bc4('0x8')][_0x57d5b7]['name']||_0x474bf7[_0x6bc4('0x8')][_0x57d5b7][_0x6bc4('0x9')]||'';_0x2c582c+='\x20<';_0x2c582c+=_0x474bf7[_0x6bc4('0x8')][_0x57d5b7]['address']?_0x474bf7[_0x6bc4('0x8')][_0x57d5b7]['address']:_0x6bc4('0xa')+random[_0x6bc4('0x13')](0x6)+_0x6bc4('0xb');_0x2c582c+='>';}}return _0x2c582c;}function parseEmail(_0x3c54d3,_0x31332a,_0x49acc8){if(_0x31332a['html']){var _0x24692a=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x24692a[_0x6bc4('0x14')](_0x31332a[_0x6bc4('0x15')])){_0x31332a[_0x6bc4('0x15')]=_0x31332a[_0x6bc4('0x15')][_0x6bc4('0x16')](_0x24692a,'');}_0x49acc8[_0x6bc4('0x17')]=_0x31332a[_0x6bc4('0x15')];}else{_0x49acc8[_0x6bc4('0x17')]=_0x31332a[_0x6bc4('0x18')];}if(_0x49acc8['body']){_0x49acc8[_0x6bc4('0x17')]=_0x49acc8['body'][_0x6bc4('0x16')](/<a\s+/gi,_0x6bc4('0x19'));}if(_0x31332a['to']){_0x49acc8['message'][_0x6bc4('0x1a')]=getMails(null,_0x31332a['to']);var _0x405472=getMails(_0x3c54d3[_0x6bc4('0x5')],_0x31332a['to']);if(!_['isEmpty'](_0x405472)){_0x49acc8['message']['cc']+=_0x405472;}}if(_0x31332a['cc']){_0x49acc8[_0x6bc4('0x1b')][_0x6bc4('0x1c')]=getMails(null,_0x31332a['cc']);var _0x12b126=getMails(_0x3c54d3[_0x6bc4('0x5')],_0x31332a['cc']);if(!_[_0x6bc4('0x12')](_0x12b126)){if(!_[_0x6bc4('0x12')](_0x49acc8[_0x6bc4('0x1b')]['cc'])){_0x49acc8[_0x6bc4('0x1b')]['cc']+=',';}_0x49acc8['message']['cc']+=_0x12b126;}}if(!_[_0x6bc4('0x1d')](_0x49acc8['message'][_0x6bc4('0x1a')],_0x3c54d3[_0x6bc4('0x5')])&&!_[_0x6bc4('0x1d')](_0x49acc8[_0x6bc4('0x1b')][_0x6bc4('0x1c')],_0x3c54d3[_0x6bc4('0x5')])){_0x49acc8[_0x6bc4('0x1b')][_0x6bc4('0x1e')]=_0x3c54d3[_0x6bc4('0x5')];}if(_0x31332a['attachments']&&_0x31332a[_0x6bc4('0x1f')][_0x6bc4('0xd')]){_0x49acc8[_0x6bc4('0x1b')]['attach']=_0x31332a[_0x6bc4('0x1f')]['length'];for(var _0x16e18f=0x0,_0x451ed2,_0x1d6ab2,_0x220b53;_0x16e18f<_0x31332a['attachments']['length'];_0x16e18f+=0x1){if(_0x31332a['attachments'][_0x16e18f]['type']===_0x6bc4('0x20')){_0x220b53=_0x31332a['attachments'][_0x16e18f][_0x6bc4('0x21')]||_0x6bc4('0x22')+_0x16e18f;_0x451ed2=util[_0x6bc4('0x23')]('%s-%s',Date[_0x6bc4('0x24')](),_0x220b53);_0x1d6ab2=path[_0x6bc4('0xf')](config[_0x6bc4('0x25')],_0x6bc4('0x26'),_0x451ed2);fs['writeFileSync'](_0x1d6ab2,_0x31332a[_0x6bc4('0x1f')][_0x16e18f][_0x6bc4('0x27')]);_0x49acc8['message'][_0x6bc4('0x1f')][_0x6bc4('0x28')]({'name':_0x220b53,'basename':_0x451ed2,'type':_0x31332a[_0x6bc4('0x1f')][_0x16e18f][_0x6bc4('0x29')],'filepath':_0x1d6ab2});}}}return _0x49acc8;}exports[_0x6bc4('0x2a')]=function(_0x45a4cf,_0x377e28,_0x28d4e6){var _0x4a16fc={'from':getAddress(_0x377e28[_0x6bc4('0x2b')]),'firstName':getFirstName(_0x377e28[_0x6bc4('0x2b')]),'lastName':getLastName(_0x377e28[_0x6bc4('0x2b')]),'mapKey':_0x6bc4('0x5'),'message':{'from':getMails(_0x45a4cf['email'],_0x377e28[_0x6bc4('0x2b')]),'to':_0x45a4cf['email'],'cc':'','subject':_0x377e28[_0x6bc4('0x2c')]||'','messageId':_0x377e28[_0x6bc4('0x2d')],'inReplyTo':_0x377e28[_0x6bc4('0x2e')],'sentAt':moment(_0x377e28['date'])['format'](_0x6bc4('0x2f')),'attach':![],'attachments':[]}};if(!_0x377e28[_0x6bc4('0x15')]&&!_0x377e28[_0x6bc4('0x18')]){var _0x47faf8=_[_0x6bc4('0x30')](_0x377e28[_0x6bc4('0x1f')],{'contentType':_0x6bc4('0x31')});if(_0x47faf8[_0x6bc4('0xd')]){logger[_0x6bc4('0x32')](util[_0x6bc4('0x23')]('Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system',_0x377e28[_0x6bc4('0x2d')]));tnef[_0x6bc4('0x33')](_['first'](_0x47faf8)[_0x6bc4('0x27')],function(_0x2723d5,_0x109136){if(_0x109136){_0x377e28[_0x6bc4('0x15')]=_0x109136['BodyHTML']||_0x6bc4('0x34');if(_0x109136['Attachments']){for(var _0x31d02f=0x0;_0x31d02f<_0x109136[_0x6bc4('0x35')][_0x6bc4('0xd')];_0x31d02f++){var _0x4c274e=_0x109136[_0x6bc4('0x35')][_0x31d02f];_0x4a16fc[_0x6bc4('0x1b')][_0x6bc4('0x1f')][_0x6bc4('0x28')]({'filename':_0x4c274e[_0x6bc4('0x36')],'content':new Buffer(_0x4c274e[_0x6bc4('0x37')])});}}}_0x28d4e6(parseEmail(_0x45a4cf,_0x377e28,_0x4a16fc));});}else{_0x377e28[_0x6bc4('0x15')]=_0x6bc4('0x34');_0x28d4e6(parseEmail(_0x45a4cf,_0x377e28,_0x4a16fc));}}else{_0x28d4e6(parseEmail(_0x45a4cf,_0x377e28,_0x4a16fc));}};exports[_0x6bc4('0x38')]=function(_0x192500){switch(_0x192500){case _0x6bc4('0x39'):return _0x6bc4('0x3a');case _0x6bc4('0x3b'):return'imap.mail.yahoo.com';case _0x6bc4('0x3c'):return _0x6bc4('0x3d');case _0x6bc4('0x3e'):return _0x6bc4('0x3f');}return'127.0.0.1';};