Built motion from commit (unavailable).|2.4.18
[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 _0x4db5=['Attachments','Title','Data','gmail','yahoo','hotmail','outlook365','127.0.0.1','lodash','moment','randomstring','path','node-tnef','../../../config/logger','email','head','value','anonymous','@xcally.com','name','slice','join','Anonymous','split','length','pop','address','generate','html','test','replace','body','<a\x20target=\x22_blank\x22\x20','message','isEmpty','originCc','bcc','attachments','attach','type','attachment-','format','%s-%s','server/files/attachments/','writeFileSync','content','push','parser','from','subject','messageId','inReplyTo','textAsHtml','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>'];(function(_0xc0d362,_0x5f3a6f){var _0x3e433e=function(_0x1b0c9e){while(--_0x1b0c9e){_0xc0d362['push'](_0xc0d362['shift']());}};_0x3e433e(++_0x5f3a6f);}(_0x4db5,0xbc));var _0x54db=function(_0x29412f,_0x541395){_0x29412f=_0x29412f-0x0;var _0x3f0e2e=_0x4db5[_0x29412f];return _0x3f0e2e;};'use strict';var _=require(_0x54db('0x0'));var moment=require(_0x54db('0x1'));var random=require(_0x54db('0x2'));var fs=require('fs');var path=require(_0x54db('0x3'));var util=require('util');var tnef=require(_0x54db('0x4'));var logger=require(_0x54db('0x5'))(_0x54db('0x6'));var config=require('../../../config/environment');function getAddress(_0xa0837b){var _0x1a7bc6=_[_0x54db('0x7')](_0xa0837b[_0x54db('0x8')]);return _0x1a7bc6&&_0x1a7bc6['address']?_0x1a7bc6['address']:_0x54db('0x9')+random['generate'](0x6)+_0x54db('0xa');}function getFirstName(_0x18e3ce){var _0x58f93b=_[_0x54db('0x7')](_0x18e3ce[_0x54db('0x8')]);if(_0x58f93b&&_0x58f93b['name']){var _0x5dcb93=_0x58f93b[_0x54db('0xb')]['split'](/\s+/);return _0x5dcb93['length']?_0x5dcb93[_0x54db('0xc')](0x0,-0x1)[_0x54db('0xd')]('\x20'):_0x58f93b[_0x54db('0xb')];}return _0x54db('0xe');}function getLastName(_0x49bcc4){var _0x1411c8=_[_0x54db('0x7')](_0x49bcc4[_0x54db('0x8')]);if(_0x1411c8&&_0x1411c8[_0x54db('0xb')]){var _0x5b6fb9=_0x1411c8[_0x54db('0xb')][_0x54db('0xf')](/\s+/);return _0x5b6fb9[_0x54db('0x10')]>0x1?_0x5b6fb9[_0x54db('0x11')]():'';}return'';}function getMails(_0x35fbe7,_0x1ac960){var _0x1dbdf9='';for(var _0xb76917=0x0;_0xb76917<_0x1ac960['value'][_0x54db('0x10')];_0xb76917+=0x1){if(_0x1ac960[_0x54db('0x8')][_0xb76917][_0x54db('0x12')]!==_0x35fbe7){if(!_['isEmpty'](_0x1dbdf9)){_0x1dbdf9+=',';}_0x1dbdf9+=_0x1ac960[_0x54db('0x8')][_0xb76917][_0x54db('0xb')]||_0x1ac960[_0x54db('0x8')][_0xb76917][_0x54db('0x12')]||'';_0x1dbdf9+='\x20<';_0x1dbdf9+=_0x1ac960[_0x54db('0x8')][_0xb76917][_0x54db('0x12')]?_0x1ac960[_0x54db('0x8')][_0xb76917][_0x54db('0x12')]:_0x54db('0x9')+random[_0x54db('0x13')](0x6)+_0x54db('0xa');_0x1dbdf9+='>';}}return _0x1dbdf9;}function parseEmail(_0x356f8a,_0x2a9d24,_0x25b71e){if(_0x2a9d24[_0x54db('0x14')]){var _0x1f37a6=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x1f37a6[_0x54db('0x15')](_0x2a9d24[_0x54db('0x14')])){_0x2a9d24[_0x54db('0x14')]=_0x2a9d24[_0x54db('0x14')][_0x54db('0x16')](_0x1f37a6,'');}_0x25b71e[_0x54db('0x17')]=_0x2a9d24['html'];}else{_0x25b71e['body']=_0x2a9d24['textAsHtml'];}if(_0x25b71e[_0x54db('0x17')]){_0x25b71e['body']=_0x25b71e['body']['replace'](/<a\s+/gi,_0x54db('0x18'));}if(_0x2a9d24['to']){_0x25b71e[_0x54db('0x19')]['originTo']=getMails(null,_0x2a9d24['to']);var _0xd4c12c=getMails(_0x356f8a['email'],_0x2a9d24['to']);if(!_[_0x54db('0x1a')](_0xd4c12c)){_0x25b71e[_0x54db('0x19')]['cc']+=_0xd4c12c;}}if(_0x2a9d24['cc']){_0x25b71e[_0x54db('0x19')][_0x54db('0x1b')]=getMails(null,_0x2a9d24['cc']);var _0x3ef071=getMails(_0x356f8a['email'],_0x2a9d24['cc']);if(!_[_0x54db('0x1a')](_0x3ef071)){if(!_['isEmpty'](_0x25b71e[_0x54db('0x19')]['cc'])){_0x25b71e[_0x54db('0x19')]['cc']+=',';}_0x25b71e['message']['cc']+=_0x3ef071;}}if(!_['includes'](_0x25b71e[_0x54db('0x19')]['originTo'],_0x356f8a[_0x54db('0x6')])&&!_['includes'](_0x25b71e[_0x54db('0x19')][_0x54db('0x1b')],_0x356f8a[_0x54db('0x6')])){_0x25b71e[_0x54db('0x19')][_0x54db('0x1c')]=_0x356f8a[_0x54db('0x6')];}if(_0x2a9d24[_0x54db('0x1d')]&&_0x2a9d24['attachments'][_0x54db('0x10')]){_0x25b71e[_0x54db('0x19')][_0x54db('0x1e')]=_0x2a9d24[_0x54db('0x1d')][_0x54db('0x10')];for(var _0x4b450f=0x0,_0x4634b2,_0x11d754,_0x59f728;_0x4b450f<_0x2a9d24[_0x54db('0x1d')][_0x54db('0x10')];_0x4b450f+=0x1){if(_0x2a9d24[_0x54db('0x1d')][_0x4b450f][_0x54db('0x1f')]==='attachment'){_0x59f728=_0x2a9d24[_0x54db('0x1d')][_0x4b450f]['filename']||_0x54db('0x20')+_0x4b450f;_0x4634b2=util[_0x54db('0x21')](_0x54db('0x22'),Date['now'](),_0x59f728);_0x11d754=path[_0x54db('0xd')](config['root'],_0x54db('0x23'),_0x4634b2);fs[_0x54db('0x24')](_0x11d754,_0x2a9d24[_0x54db('0x1d')][_0x4b450f][_0x54db('0x25')]);_0x25b71e[_0x54db('0x19')]['attachments'][_0x54db('0x26')]({'name':_0x59f728,'basename':_0x4634b2,'type':_0x2a9d24[_0x54db('0x1d')][_0x4b450f]['contentType'],'filepath':_0x11d754});}}}return _0x25b71e;}exports[_0x54db('0x27')]=function(_0x14275e,_0x463b4f,_0x493a81){var _0x59f05a={'from':getAddress(_0x463b4f[_0x54db('0x28')]),'firstName':getFirstName(_0x463b4f['from']),'lastName':getLastName(_0x463b4f[_0x54db('0x28')]),'mapKey':_0x54db('0x6'),'message':{'from':getMails(_0x14275e[_0x54db('0x6')],_0x463b4f['from']),'to':_0x14275e[_0x54db('0x6')],'cc':'','subject':_0x463b4f[_0x54db('0x29')]||'','messageId':_0x463b4f[_0x54db('0x2a')],'inReplyTo':_0x463b4f[_0x54db('0x2b')],'sentAt':moment(_0x463b4f['date'])[_0x54db('0x21')]('YYYY-MM-DD\x20HH:mm:ss'),'attach':![],'attachments':[]}};if(!_0x463b4f[_0x54db('0x14')]&&!_0x463b4f[_0x54db('0x2c')]){var _0x1b79ef=_['filter'](_0x463b4f[_0x54db('0x1d')],{'contentType':_0x54db('0x2d')});if(_0x1b79ef[_0x54db('0x10')]){logger[_0x54db('0x2e')](util[_0x54db('0x21')](_0x54db('0x2f'),_0x463b4f[_0x54db('0x2a')]));tnef[_0x54db('0x30')](_[_0x54db('0x31')](_0x1b79ef)[_0x54db('0x25')],function(_0x3595cf,_0x382fd4){if(_0x382fd4){_0x463b4f[_0x54db('0x14')]=_0x382fd4[_0x54db('0x32')]||_0x54db('0x33');if(_0x382fd4['Attachments']){for(var _0x4b30df=0x0;_0x4b30df<_0x382fd4[_0x54db('0x34')][_0x54db('0x10')];_0x4b30df++){var _0x231cc6=_0x382fd4[_0x54db('0x34')][_0x4b30df];_0x59f05a[_0x54db('0x19')]['attachments'][_0x54db('0x26')]({'filename':_0x231cc6[_0x54db('0x35')],'content':new Buffer(_0x231cc6[_0x54db('0x36')])});}}}_0x493a81(parseEmail(_0x14275e,_0x463b4f,_0x59f05a));});}else{_0x463b4f['html']='<p>Empty\x20message</p>';_0x493a81(parseEmail(_0x14275e,_0x463b4f,_0x59f05a));}}else{_0x493a81(parseEmail(_0x14275e,_0x463b4f,_0x59f05a));}};exports['getHost']=function(_0x33e28f){switch(_0x33e28f){case _0x54db('0x37'):return'imap.gmail.com';case _0x54db('0x38'):return'imap.mail.yahoo.com';case _0x54db('0x39'):return'imap-mail.outlook.com';case _0x54db('0x3a'):return'outlook.office365.com';}return _0x54db('0x3b');};