89baf4262709d7bb8a4c2c01337823237f1022b9
[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 _0x020f=['email','includes','originTo','originCc','bcc','attachments','attach','type','filename','attachment-','format','%s-%s','root','server/files/attachments/','writeFileSync','push','contentType','parser','from','subject','messageId','inReplyTo','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','parseBuffer','first','content','BodyHTML','<p>Empty\x20message</p>','Attachments','Title','Data','gmail','yahoo','imap.mail.yahoo.com','hotmail','outlook365','outlook.office365.com','127.0.0.1','randomstring','path','util','../../../config/logger','../../../config/environment','head','value','address','generate','@xcally.com','name','split','slice','join','Anonymous','length','pop','isEmpty','anonymous','html','replace','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','message'];(function(_0x39a708,_0x2973e4){var _0x597d37=function(_0x448ed9){while(--_0x448ed9){_0x39a708['push'](_0x39a708['shift']());}};_0x597d37(++_0x2973e4);}(_0x020f,0x1bc));var _0xf020=function(_0x192c75,_0x3d1f06){_0x192c75=_0x192c75-0x0;var _0x2b0b93=_0x020f[_0x192c75];return _0x2b0b93;};'use strict';var _=require('lodash');var moment=require('moment');var random=require(_0xf020('0x0'));var fs=require('fs');var path=require(_0xf020('0x1'));var util=require(_0xf020('0x2'));var tnef=require('node-tnef');var logger=require(_0xf020('0x3'))('email');var config=require(_0xf020('0x4'));function getAddress(_0x166726){var _0x4e77f5=_[_0xf020('0x5')](_0x166726[_0xf020('0x6')]);return _0x4e77f5&&_0x4e77f5[_0xf020('0x7')]?_0x4e77f5[_0xf020('0x7')]:'anonymous'+random[_0xf020('0x8')](0x6)+_0xf020('0x9');}function getFirstName(_0x8d0371){var _0x1e16e0=_['head'](_0x8d0371[_0xf020('0x6')]);if(_0x1e16e0&&_0x1e16e0[_0xf020('0xa')]){var _0x2f177c=_0x1e16e0['name'][_0xf020('0xb')](/\s+/);return _0x2f177c['length']?_0x2f177c[_0xf020('0xc')](0x0,-0x1)[_0xf020('0xd')]('\x20'):_0x1e16e0['name'];}return _0xf020('0xe');}function getLastName(_0xf09324){var _0x20295f=_[_0xf020('0x5')](_0xf09324['value']);if(_0x20295f&&_0x20295f[_0xf020('0xa')]){var _0x3268c6=_0x20295f[_0xf020('0xa')]['split'](/\s+/);return _0x3268c6[_0xf020('0xf')]>0x1?_0x3268c6[_0xf020('0x10')]():'';}return'';}function getMails(_0x159cb9,_0x38dd81){var _0x558b50='';for(var _0x56685a=0x0;_0x56685a<_0x38dd81['value'][_0xf020('0xf')];_0x56685a+=0x1){if(_0x38dd81[_0xf020('0x6')][_0x56685a][_0xf020('0x7')]!==_0x159cb9){if(!_[_0xf020('0x11')](_0x558b50)){_0x558b50+=',';}_0x558b50+=_0x38dd81[_0xf020('0x6')][_0x56685a][_0xf020('0xa')]||_0x38dd81[_0xf020('0x6')][_0x56685a][_0xf020('0x7')]||'';_0x558b50+='\x20<';_0x558b50+=_0x38dd81[_0xf020('0x6')][_0x56685a][_0xf020('0x7')]?_0x38dd81[_0xf020('0x6')][_0x56685a][_0xf020('0x7')]:_0xf020('0x12')+random[_0xf020('0x8')](0x6)+_0xf020('0x9');_0x558b50+='>';}}return _0x558b50;}function parseEmail(_0x233410,_0x40dcc1,_0xb4c9b8){if(_0x40dcc1['html']){var _0x2406c7=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x2406c7['test'](_0x40dcc1[_0xf020('0x13')])){_0x40dcc1['html']=_0x40dcc1[_0xf020('0x13')][_0xf020('0x14')](_0x2406c7,'');}_0xb4c9b8[_0xf020('0x15')]=_0x40dcc1[_0xf020('0x13')];}else{_0xb4c9b8[_0xf020('0x15')]=_0x40dcc1[_0xf020('0x16')];}if(_0xb4c9b8['body']){_0xb4c9b8[_0xf020('0x15')]=_0xb4c9b8[_0xf020('0x15')][_0xf020('0x14')](/<a\s+/gi,_0xf020('0x17'));}if(_0x40dcc1['to']){_0xb4c9b8['message']['originTo']=getMails(null,_0x40dcc1['to']);var _0x26e2ab=getMails(_0x233410['email'],_0x40dcc1['to']);if(!_[_0xf020('0x11')](_0x26e2ab)){_0xb4c9b8[_0xf020('0x18')]['cc']+=_0x26e2ab;}}if(_0x40dcc1['cc']){_0xb4c9b8[_0xf020('0x18')]['originCc']=getMails(null,_0x40dcc1['cc']);var _0x24ea3e=getMails(_0x233410[_0xf020('0x19')],_0x40dcc1['cc']);if(!_[_0xf020('0x11')](_0x24ea3e)){if(!_[_0xf020('0x11')](_0xb4c9b8[_0xf020('0x18')]['cc'])){_0xb4c9b8[_0xf020('0x18')]['cc']+=',';}_0xb4c9b8[_0xf020('0x18')]['cc']+=_0x24ea3e;}}if(!_[_0xf020('0x1a')](_0xb4c9b8['message'][_0xf020('0x1b')],_0x233410['email'])&&!_[_0xf020('0x1a')](_0xb4c9b8[_0xf020('0x18')][_0xf020('0x1c')],_0x233410[_0xf020('0x19')])){_0xb4c9b8[_0xf020('0x18')][_0xf020('0x1d')]=_0x233410[_0xf020('0x19')];}if(_0x40dcc1[_0xf020('0x1e')]&&_0x40dcc1[_0xf020('0x1e')][_0xf020('0xf')]){_0xb4c9b8['message'][_0xf020('0x1f')]=_0x40dcc1[_0xf020('0x1e')][_0xf020('0xf')];for(var _0x418697=0x0,_0x2241d0,_0x560a4a,_0x352426;_0x418697<_0x40dcc1['attachments']['length'];_0x418697+=0x1){if(_0x40dcc1[_0xf020('0x1e')][_0x418697][_0xf020('0x20')]==='attachment'){_0x352426=_0x40dcc1['attachments'][_0x418697][_0xf020('0x21')]||_0xf020('0x22')+_0x418697;_0x2241d0=util[_0xf020('0x23')](_0xf020('0x24'),Date['now'](),_0x352426);_0x560a4a=path['join'](config[_0xf020('0x25')],_0xf020('0x26'),_0x2241d0);fs[_0xf020('0x27')](_0x560a4a,_0x40dcc1[_0xf020('0x1e')][_0x418697]['content']);_0xb4c9b8[_0xf020('0x18')][_0xf020('0x1e')][_0xf020('0x28')]({'name':_0x352426,'basename':_0x2241d0,'type':_0x40dcc1[_0xf020('0x1e')][_0x418697][_0xf020('0x29')],'filepath':_0x560a4a});}}}return _0xb4c9b8;}exports[_0xf020('0x2a')]=function(_0x53814e,_0x3ba6e1,_0x49dc04){var _0x2c4287={'from':getAddress(_0x3ba6e1[_0xf020('0x2b')]),'firstName':getFirstName(_0x3ba6e1[_0xf020('0x2b')]),'lastName':getLastName(_0x3ba6e1[_0xf020('0x2b')]),'mapKey':'email','message':{'from':getMails(_0x53814e['email'],_0x3ba6e1[_0xf020('0x2b')]),'to':_0x53814e[_0xf020('0x19')],'cc':'','subject':_0x3ba6e1[_0xf020('0x2c')]||'','messageId':_0x3ba6e1[_0xf020('0x2d')],'inReplyTo':_0x3ba6e1[_0xf020('0x2e')],'sentAt':moment(_0x3ba6e1['date'])[_0xf020('0x23')](_0xf020('0x2f')),'attach':![],'attachments':[]}};if(!_0x3ba6e1[_0xf020('0x13')]&&!_0x3ba6e1[_0xf020('0x16')]){var _0x2dc8c5=_[_0xf020('0x30')](_0x3ba6e1[_0xf020('0x1e')],{'contentType':_0xf020('0x31')});if(_0x2dc8c5[_0xf020('0xf')]){logger[_0xf020('0x32')](util[_0xf020('0x23')](_0xf020('0x33'),_0x3ba6e1[_0xf020('0x2d')]));tnef[_0xf020('0x34')](_[_0xf020('0x35')](_0x2dc8c5)[_0xf020('0x36')],function(_0x377ea5,_0x573ec5){if(_0x573ec5){_0x3ba6e1['html']=_0x573ec5[_0xf020('0x37')]||_0xf020('0x38');if(_0x573ec5[_0xf020('0x39')]){for(var _0x2a5801=0x0;_0x2a5801<_0x573ec5['Attachments'][_0xf020('0xf')];_0x2a5801++){var _0x8cd0f=_0x573ec5[_0xf020('0x39')][_0x2a5801];_0x2c4287[_0xf020('0x18')][_0xf020('0x1e')][_0xf020('0x28')]({'filename':_0x8cd0f[_0xf020('0x3a')],'content':new Buffer(_0x8cd0f[_0xf020('0x3b')])});}}}_0x49dc04(parseEmail(_0x53814e,_0x3ba6e1,_0x2c4287));});}else{_0x3ba6e1[_0xf020('0x13')]='<p>Empty\x20message</p>';_0x49dc04(parseEmail(_0x53814e,_0x3ba6e1,_0x2c4287));}}else{_0x49dc04(parseEmail(_0x53814e,_0x3ba6e1,_0x2c4287));}};exports['getHost']=function(_0x9f5aa4){switch(_0x9f5aa4){case _0xf020('0x3c'):return'imap.gmail.com';case _0xf020('0x3d'):return _0xf020('0x3e');case _0xf020('0x3f'):return'imap-mail.outlook.com';case _0xf020('0x40'):return _0xf020('0x41');}return _0xf020('0x42');};