Built motion from commit (unavailable).|2.5.0
[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 _0x3465=['replace','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','message','originTo','isEmpty','includes','originCc','attachments','attach','attachment','filename','attachment-','format','now','server/files/attachments/','content','push','contentType','from','subject','messageId','inReplyTo','date','YYYY-MM-DD\x20HH:mm:ss','filter','application/ms-tnef','info','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','first','BodyHTML','<p>Empty\x20message</p>','Attachments','Title','getHost','gmail','imap.gmail.com','yahoo','imap.mail.yahoo.com','hotmail','imap-mail.outlook.com','outlook365','outlook.office365.com','lodash','moment','randomstring','path','util','node-tnef','../../../config/logger','email','../../../config/environment','head','value','address','anonymous','generate','@xcally.com','name','split','length','slice','join','Anonymous','pop','html','test'];(function(_0x4a209a,_0x575ff9){var _0x4b3c20=function(_0x1a50c1){while(--_0x1a50c1){_0x4a209a['push'](_0x4a209a['shift']());}};_0x4b3c20(++_0x575ff9);}(_0x3465,0x70));var _0x5346=function(_0x528fd9,_0x4de3c4){_0x528fd9=_0x528fd9-0x0;var _0x59b2aa=_0x3465[_0x528fd9];return _0x59b2aa;};'use strict';var _=require(_0x5346('0x0'));var moment=require(_0x5346('0x1'));var random=require(_0x5346('0x2'));var fs=require('fs');var path=require(_0x5346('0x3'));var util=require(_0x5346('0x4'));var tnef=require(_0x5346('0x5'));var logger=require(_0x5346('0x6'))(_0x5346('0x7'));var config=require(_0x5346('0x8'));function getAddress(_0x48319b){var _0x3033c2=_[_0x5346('0x9')](_0x48319b[_0x5346('0xa')]);return _0x3033c2&&_0x3033c2[_0x5346('0xb')]?_0x3033c2['address']:_0x5346('0xc')+random[_0x5346('0xd')](0x6)+_0x5346('0xe');}function getFirstName(_0x16c470){var _0x32f5e0=_[_0x5346('0x9')](_0x16c470[_0x5346('0xa')]);if(_0x32f5e0&&_0x32f5e0[_0x5346('0xf')]){var _0x2607e2=_0x32f5e0[_0x5346('0xf')][_0x5346('0x10')](/\s+/);return _0x2607e2[_0x5346('0x11')]?_0x2607e2[_0x5346('0x12')](0x0,-0x1)[_0x5346('0x13')]('\x20'):_0x32f5e0[_0x5346('0xf')];}return _0x5346('0x14');}function getLastName(_0x2d6b57){var _0x4ef512=_['head'](_0x2d6b57[_0x5346('0xa')]);if(_0x4ef512&&_0x4ef512[_0x5346('0xf')]){var _0x48c9a9=_0x4ef512['name'][_0x5346('0x10')](/\s+/);return _0x48c9a9[_0x5346('0x11')]>0x1?_0x48c9a9[_0x5346('0x15')]():'';}return'';}function getMails(_0x33c318,_0x208d27){var _0x187ac7='';for(var _0x42ccfc=0x0;_0x42ccfc<_0x208d27['value']['length'];_0x42ccfc+=0x1){if(_0x208d27[_0x5346('0xa')][_0x42ccfc][_0x5346('0xb')]!==_0x33c318){if(!_['isEmpty'](_0x187ac7)){_0x187ac7+=',';}_0x187ac7+=_0x208d27[_0x5346('0xa')][_0x42ccfc][_0x5346('0xf')]||_0x208d27[_0x5346('0xa')][_0x42ccfc]['address']||'';_0x187ac7+='\x20<';_0x187ac7+=_0x208d27[_0x5346('0xa')][_0x42ccfc]['address']?_0x208d27[_0x5346('0xa')][_0x42ccfc]['address']:_0x5346('0xc')+random[_0x5346('0xd')](0x6)+_0x5346('0xe');_0x187ac7+='>';}}return _0x187ac7;}function parseEmail(_0x6ac087,_0x42ca4d,_0x5775fe){if(_0x42ca4d[_0x5346('0x16')]){var _0x26755d=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x26755d[_0x5346('0x17')](_0x42ca4d['html'])){_0x42ca4d[_0x5346('0x16')]=_0x42ca4d[_0x5346('0x16')][_0x5346('0x18')](_0x26755d,'');}_0x5775fe[_0x5346('0x19')]=_0x42ca4d[_0x5346('0x16')];}else{_0x5775fe[_0x5346('0x19')]=_0x42ca4d[_0x5346('0x1a')];}if(_0x5775fe[_0x5346('0x19')]){_0x5775fe[_0x5346('0x19')]=_0x5775fe['body'][_0x5346('0x18')](/<a\s+/gi,_0x5346('0x1b'));}if(_0x42ca4d['to']){_0x5775fe[_0x5346('0x1c')][_0x5346('0x1d')]=getMails(null,_0x42ca4d['to']);var _0x51868a=getMails(_0x6ac087[_0x5346('0x7')],_0x42ca4d['to']);if(!_[_0x5346('0x1e')](_0x51868a)){_0x5775fe[_0x5346('0x1c')]['cc']+=_0x51868a;}}if(_0x42ca4d['cc']){_0x5775fe[_0x5346('0x1c')]['originCc']=getMails(null,_0x42ca4d['cc']);var _0xbd992b=getMails(_0x6ac087[_0x5346('0x7')],_0x42ca4d['cc']);if(!_[_0x5346('0x1e')](_0xbd992b)){if(!_['isEmpty'](_0x5775fe[_0x5346('0x1c')]['cc'])){_0x5775fe[_0x5346('0x1c')]['cc']+=',';}_0x5775fe[_0x5346('0x1c')]['cc']+=_0xbd992b;}}if(!_[_0x5346('0x1f')](_0x5775fe[_0x5346('0x1c')][_0x5346('0x1d')],_0x6ac087[_0x5346('0x7')])&&!_[_0x5346('0x1f')](_0x5775fe[_0x5346('0x1c')][_0x5346('0x20')],_0x6ac087[_0x5346('0x7')])){_0x5775fe[_0x5346('0x1c')]['bcc']=_0x6ac087[_0x5346('0x7')];}if(_0x42ca4d[_0x5346('0x21')]&&_0x42ca4d[_0x5346('0x21')]['length']){_0x5775fe[_0x5346('0x1c')][_0x5346('0x22')]=_0x42ca4d['attachments'][_0x5346('0x11')];for(var _0x133b43=0x0,_0x1c4e56,_0x513e84,_0x1cf1af;_0x133b43<_0x42ca4d[_0x5346('0x21')][_0x5346('0x11')];_0x133b43+=0x1){if(_0x42ca4d[_0x5346('0x21')][_0x133b43]['type']===_0x5346('0x23')){_0x1cf1af=_0x42ca4d[_0x5346('0x21')][_0x133b43][_0x5346('0x24')]||_0x5346('0x25')+_0x133b43;_0x1c4e56=util[_0x5346('0x26')]('%s-%s',Date[_0x5346('0x27')](),_0x1cf1af);_0x513e84=path[_0x5346('0x13')](config['root'],_0x5346('0x28'),_0x1c4e56);fs['writeFileSync'](_0x513e84,_0x42ca4d[_0x5346('0x21')][_0x133b43][_0x5346('0x29')]);_0x5775fe[_0x5346('0x1c')][_0x5346('0x21')][_0x5346('0x2a')]({'name':_0x1cf1af,'basename':_0x1c4e56,'type':_0x42ca4d[_0x5346('0x21')][_0x133b43][_0x5346('0x2b')],'filepath':_0x513e84});}}}return _0x5775fe;}exports['parser']=function(_0x31bde1,_0xdb0de3,_0xe14e8f){var _0x4296b4={'from':getAddress(_0xdb0de3[_0x5346('0x2c')]),'firstName':getFirstName(_0xdb0de3[_0x5346('0x2c')]),'lastName':getLastName(_0xdb0de3['from']),'mapKey':_0x5346('0x7'),'message':{'from':getMails(_0x31bde1[_0x5346('0x7')],_0xdb0de3['from']),'to':_0x31bde1[_0x5346('0x7')],'cc':'','subject':_0xdb0de3[_0x5346('0x2d')]||'','messageId':_0xdb0de3[_0x5346('0x2e')],'inReplyTo':_0xdb0de3[_0x5346('0x2f')],'sentAt':moment(_0xdb0de3[_0x5346('0x30')])[_0x5346('0x26')](_0x5346('0x31')),'attach':![],'attachments':[]}};if(!_0xdb0de3['html']&&!_0xdb0de3[_0x5346('0x1a')]){var _0x98720=_[_0x5346('0x32')](_0xdb0de3['attachments'],{'contentType':_0x5346('0x33')});if(_0x98720['length']){logger[_0x5346('0x34')](util[_0x5346('0x26')](_0x5346('0x35'),_0xdb0de3['messageId']));tnef['parseBuffer'](_[_0x5346('0x36')](_0x98720)['content'],function(_0x33a1a0,_0x1e67c4){if(_0x1e67c4){_0xdb0de3[_0x5346('0x16')]=_0x1e67c4[_0x5346('0x37')]||_0x5346('0x38');if(_0x1e67c4['Attachments']){for(var _0x557f06=0x0;_0x557f06<_0x1e67c4['Attachments'][_0x5346('0x11')];_0x557f06++){var _0xef1368=_0x1e67c4[_0x5346('0x39')][_0x557f06];_0x4296b4[_0x5346('0x1c')][_0x5346('0x21')][_0x5346('0x2a')]({'filename':_0xef1368[_0x5346('0x3a')],'content':new Buffer(_0xef1368['Data'])});}}}_0xe14e8f(parseEmail(_0x31bde1,_0xdb0de3,_0x4296b4));});}else{_0xdb0de3[_0x5346('0x16')]=_0x5346('0x38');_0xe14e8f(parseEmail(_0x31bde1,_0xdb0de3,_0x4296b4));}}else{_0xe14e8f(parseEmail(_0x31bde1,_0xdb0de3,_0x4296b4));}};exports[_0x5346('0x3b')]=function(_0x1c2c5e){switch(_0x1c2c5e){case _0x5346('0x3c'):return _0x5346('0x3d');case _0x5346('0x3e'):return _0x5346('0x3f');case _0x5346('0x40'):return _0x5346('0x41');case _0x5346('0x42'):return _0x5346('0x43');}return'127.0.0.1';};