Built motion from commit (unavailable).|2.5.3
[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 _0x25e6=['messageId','inReplyTo','date','format','YYYY-MM-DD\x20HH:mm:ss','textAsHtml','filter','application/ms-tnef','info','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','parseBuffer','first','BodyHTML','Attachments','Title','Data','<p>Empty\x20message</p>','gmail','imap.gmail.com','yahoo','imap.mail.yahoo.com','hotmail','imap-mail.outlook.com','outlook365','outlook.office365.com','lodash','moment','path','util','node-tnef','../../../config/logger','email','value','address','@xcally.com','name','length','slice','join','Anonymous','head','split','pop','isEmpty','anonymous','generate','html','test','body','message','originTo','originCc','includes','bcc','attachments','attach','type','attachment','filename','%s-%s','now','server/files/attachments/','writeFileSync','content','push','from','subject'];(function(_0x3826ad,_0x49a30d){var _0x17b900=function(_0x38474f){while(--_0x38474f){_0x3826ad['push'](_0x3826ad['shift']());}};_0x17b900(++_0x49a30d);}(_0x25e6,0xe2));var _0x625e=function(_0x4b4d1d,_0x35ff57){_0x4b4d1d=_0x4b4d1d-0x0;var _0x36b98c=_0x25e6[_0x4b4d1d];return _0x36b98c;};'use strict';var _=require(_0x625e('0x0'));var moment=require(_0x625e('0x1'));var random=require('randomstring');var fs=require('fs');var path=require(_0x625e('0x2'));var util=require(_0x625e('0x3'));var tnef=require(_0x625e('0x4'));var logger=require(_0x625e('0x5'))(_0x625e('0x6'));var config=require('../../../config/environment');function getAddress(_0x2c7657){var _0x106a8e=_['head'](_0x2c7657[_0x625e('0x7')]);return _0x106a8e&&_0x106a8e[_0x625e('0x8')]?_0x106a8e[_0x625e('0x8')]:'anonymous'+random['generate'](0x6)+_0x625e('0x9');}function getFirstName(_0x1050b9){var _0x1618c9=_['head'](_0x1050b9[_0x625e('0x7')]);if(_0x1618c9&&_0x1618c9[_0x625e('0xa')]){var _0xce2de3=_0x1618c9[_0x625e('0xa')]['split'](/\s+/);return _0xce2de3[_0x625e('0xb')]?_0xce2de3[_0x625e('0xc')](0x0,-0x1)[_0x625e('0xd')]('\x20'):_0x1618c9[_0x625e('0xa')];}return _0x625e('0xe');}function getLastName(_0x2480ff){var _0x52cce7=_[_0x625e('0xf')](_0x2480ff[_0x625e('0x7')]);if(_0x52cce7&&_0x52cce7[_0x625e('0xa')]){var _0x34fc53=_0x52cce7[_0x625e('0xa')][_0x625e('0x10')](/\s+/);return _0x34fc53[_0x625e('0xb')]>0x1?_0x34fc53[_0x625e('0x11')]():'';}return'';}function getMails(_0x2065b1,_0x359c79){var _0x30f478='';for(var _0x133591=0x0;_0x133591<_0x359c79[_0x625e('0x7')][_0x625e('0xb')];_0x133591+=0x1){if(_0x359c79[_0x625e('0x7')][_0x133591]['address']!==_0x2065b1){if(!_[_0x625e('0x12')](_0x30f478)){_0x30f478+=',';}_0x30f478+=_0x359c79[_0x625e('0x7')][_0x133591][_0x625e('0xa')]||_0x359c79[_0x625e('0x7')][_0x133591][_0x625e('0x8')]||'';_0x30f478+='\x20<';_0x30f478+=_0x359c79[_0x625e('0x7')][_0x133591][_0x625e('0x8')]?_0x359c79[_0x625e('0x7')][_0x133591][_0x625e('0x8')]:_0x625e('0x13')+random[_0x625e('0x14')](0x6)+_0x625e('0x9');_0x30f478+='>';}}return _0x30f478;}function parseEmail(_0x13f423,_0xf40cc2,_0x3f9d59){if(_0xf40cc2[_0x625e('0x15')]){var _0x4076e8=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x4076e8[_0x625e('0x16')](_0xf40cc2[_0x625e('0x15')])){_0xf40cc2[_0x625e('0x15')]=_0xf40cc2['html']['replace'](_0x4076e8,'');}_0x3f9d59[_0x625e('0x17')]=_0xf40cc2['html'];}else{_0x3f9d59[_0x625e('0x17')]=_0xf40cc2['textAsHtml'];}if(_0x3f9d59[_0x625e('0x17')]){_0x3f9d59[_0x625e('0x17')]=_0x3f9d59[_0x625e('0x17')]['replace'](/<a\s+/gi,'<a\x20target=\x22_blank\x22\x20');}if(_0xf40cc2['to']){_0x3f9d59[_0x625e('0x18')][_0x625e('0x19')]=getMails(null,_0xf40cc2['to']);var _0x5686de=getMails(_0x13f423['email'],_0xf40cc2['to']);if(!_['isEmpty'](_0x5686de)){_0x3f9d59[_0x625e('0x18')]['cc']+=_0x5686de;}}if(_0xf40cc2['cc']){_0x3f9d59[_0x625e('0x18')][_0x625e('0x1a')]=getMails(null,_0xf40cc2['cc']);var _0x56b284=getMails(_0x13f423[_0x625e('0x6')],_0xf40cc2['cc']);if(!_[_0x625e('0x12')](_0x56b284)){if(!_[_0x625e('0x12')](_0x3f9d59[_0x625e('0x18')]['cc'])){_0x3f9d59[_0x625e('0x18')]['cc']+=',';}_0x3f9d59[_0x625e('0x18')]['cc']+=_0x56b284;}}if(!_[_0x625e('0x1b')](_0x3f9d59[_0x625e('0x18')][_0x625e('0x19')],_0x13f423['email'])&&!_['includes'](_0x3f9d59['message'][_0x625e('0x1a')],_0x13f423['email'])){_0x3f9d59['message'][_0x625e('0x1c')]=_0x13f423[_0x625e('0x6')];}if(_0xf40cc2[_0x625e('0x1d')]&&_0xf40cc2[_0x625e('0x1d')][_0x625e('0xb')]){_0x3f9d59[_0x625e('0x18')][_0x625e('0x1e')]=_0xf40cc2[_0x625e('0x1d')]['length'];for(var _0x2cf2fe=0x0,_0x317a63,_0x3b5bd1,_0x21a0f2;_0x2cf2fe<_0xf40cc2[_0x625e('0x1d')]['length'];_0x2cf2fe+=0x1){if(_0xf40cc2[_0x625e('0x1d')][_0x2cf2fe][_0x625e('0x1f')]===_0x625e('0x20')){_0x21a0f2=_0xf40cc2[_0x625e('0x1d')][_0x2cf2fe][_0x625e('0x21')]||'attachment-'+_0x2cf2fe;_0x317a63=util['format'](_0x625e('0x22'),Date[_0x625e('0x23')](),_0x21a0f2);_0x3b5bd1=path[_0x625e('0xd')](config['root'],_0x625e('0x24'),_0x317a63);fs[_0x625e('0x25')](_0x3b5bd1,_0xf40cc2[_0x625e('0x1d')][_0x2cf2fe][_0x625e('0x26')]);_0x3f9d59[_0x625e('0x18')]['attachments'][_0x625e('0x27')]({'name':_0x21a0f2,'basename':_0x317a63,'type':_0xf40cc2[_0x625e('0x1d')][_0x2cf2fe]['contentType'],'filepath':_0x3b5bd1});}}}return _0x3f9d59;}exports['parser']=function(_0xadcb97,_0x2d5860,_0x28ac77){var _0x9eae7d={'from':getAddress(_0x2d5860['from']),'firstName':getFirstName(_0x2d5860[_0x625e('0x28')]),'lastName':getLastName(_0x2d5860[_0x625e('0x28')]),'mapKey':_0x625e('0x6'),'message':{'from':getMails(_0xadcb97[_0x625e('0x6')],_0x2d5860[_0x625e('0x28')]),'to':_0xadcb97[_0x625e('0x6')],'cc':'','subject':_0x2d5860[_0x625e('0x29')]||'','messageId':_0x2d5860[_0x625e('0x2a')],'inReplyTo':_0x2d5860[_0x625e('0x2b')],'sentAt':moment(_0x2d5860[_0x625e('0x2c')])[_0x625e('0x2d')](_0x625e('0x2e')),'attach':![],'attachments':[]}};if(!_0x2d5860[_0x625e('0x15')]&&!_0x2d5860[_0x625e('0x2f')]){var _0x3a50b0=_[_0x625e('0x30')](_0x2d5860[_0x625e('0x1d')],{'contentType':_0x625e('0x31')});if(_0x3a50b0[_0x625e('0xb')]){logger[_0x625e('0x32')](util[_0x625e('0x2d')](_0x625e('0x33'),_0x2d5860['messageId']));tnef[_0x625e('0x34')](_[_0x625e('0x35')](_0x3a50b0)[_0x625e('0x26')],function(_0x41fa45,_0x58c76f){if(_0x58c76f){_0x2d5860[_0x625e('0x15')]=_0x58c76f[_0x625e('0x36')]||'<p>Empty\x20message</p>';if(_0x58c76f[_0x625e('0x37')]){for(var _0x4e1861=0x0;_0x4e1861<_0x58c76f['Attachments']['length'];_0x4e1861++){var _0x17397d=_0x58c76f[_0x625e('0x37')][_0x4e1861];_0x9eae7d[_0x625e('0x18')]['attachments']['push']({'filename':_0x17397d[_0x625e('0x38')],'content':new Buffer(_0x17397d[_0x625e('0x39')])});}}}_0x28ac77(parseEmail(_0xadcb97,_0x2d5860,_0x9eae7d));});}else{_0x2d5860['html']=_0x625e('0x3a');_0x28ac77(parseEmail(_0xadcb97,_0x2d5860,_0x9eae7d));}}else{_0x28ac77(parseEmail(_0xadcb97,_0x2d5860,_0x9eae7d));}};exports['getHost']=function(_0x585ce7){switch(_0x585ce7){case _0x625e('0x3b'):return _0x625e('0x3c');case _0x625e('0x3d'):return _0x625e('0x3e');case _0x625e('0x3f'):return _0x625e('0x40');case _0x625e('0x41'):return _0x625e('0x42');}return'127.0.0.1';};