Built motion from commit (unavailable).|2.5.6
[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 _0x20cc=['lodash','moment','randomstring','path','util','node-tnef','../../../config/logger','email','head','value','address','anonymous','@xcally.com','name','split','length','slice','Anonymous','pop','isEmpty','generate','html','test','replace','body','textAsHtml','message','originCc','includes','bcc','attachments','attach','type','filename','attachment-','format','now','join','root','writeFileSync','content','contentType','from','subject','messageId','inReplyTo','YYYY-MM-DD\x20HH:mm:ss','filter','application/ms-tnef','info','BodyHTML','<p>Empty\x20message</p>','Attachments','push','Data','gmail','yahoo','imap.mail.yahoo.com','imap-mail.outlook.com','outlook365','outlook.office365.com','127.0.0.1'];(function(_0x444283,_0x357086){var _0x322120=function(_0x5a4b5d){while(--_0x5a4b5d){_0x444283['push'](_0x444283['shift']());}};_0x322120(++_0x357086);}(_0x20cc,0xba));var _0xc20c=function(_0x15bc81,_0x4d4f77){_0x15bc81=_0x15bc81-0x0;var _0x59440e=_0x20cc[_0x15bc81];return _0x59440e;};'use strict';var _=require(_0xc20c('0x0'));var moment=require(_0xc20c('0x1'));var random=require(_0xc20c('0x2'));var fs=require('fs');var path=require(_0xc20c('0x3'));var util=require(_0xc20c('0x4'));var tnef=require(_0xc20c('0x5'));var logger=require(_0xc20c('0x6'))(_0xc20c('0x7'));var config=require('../../../config/environment');function getAddress(_0x28ff7a){var _0x4f41b1=_[_0xc20c('0x8')](_0x28ff7a[_0xc20c('0x9')]);return _0x4f41b1&&_0x4f41b1[_0xc20c('0xa')]?_0x4f41b1[_0xc20c('0xa')]:_0xc20c('0xb')+random['generate'](0x6)+_0xc20c('0xc');}function getFirstName(_0x53b122){var _0x4bde6d=_[_0xc20c('0x8')](_0x53b122['value']);if(_0x4bde6d&&_0x4bde6d[_0xc20c('0xd')]){var _0x42dc56=_0x4bde6d[_0xc20c('0xd')][_0xc20c('0xe')](/\s+/);return _0x42dc56[_0xc20c('0xf')]?_0x42dc56[_0xc20c('0x10')](0x0,-0x1)['join']('\x20'):_0x4bde6d[_0xc20c('0xd')];}return _0xc20c('0x11');}function getLastName(_0x461d05){var _0x39775b=_[_0xc20c('0x8')](_0x461d05['value']);if(_0x39775b&&_0x39775b[_0xc20c('0xd')]){var _0x2d9709=_0x39775b[_0xc20c('0xd')][_0xc20c('0xe')](/\s+/);return _0x2d9709[_0xc20c('0xf')]>0x1?_0x2d9709[_0xc20c('0x12')]():'';}return'';}function getMails(_0x1f2b0a,_0x110773){var _0x270fcd='';for(var _0x28f9a1=0x0;_0x28f9a1<_0x110773[_0xc20c('0x9')][_0xc20c('0xf')];_0x28f9a1+=0x1){if(_0x110773[_0xc20c('0x9')][_0x28f9a1][_0xc20c('0xa')]!==_0x1f2b0a){if(!_[_0xc20c('0x13')](_0x270fcd)){_0x270fcd+=',';}_0x270fcd+=_0x110773[_0xc20c('0x9')][_0x28f9a1]['name']||_0x110773[_0xc20c('0x9')][_0x28f9a1][_0xc20c('0xa')]||'';_0x270fcd+='\x20<';_0x270fcd+=_0x110773['value'][_0x28f9a1][_0xc20c('0xa')]?_0x110773[_0xc20c('0x9')][_0x28f9a1][_0xc20c('0xa')]:_0xc20c('0xb')+random[_0xc20c('0x14')](0x6)+'@xcally.com';_0x270fcd+='>';}}return _0x270fcd;}function parseEmail(_0x59612e,_0x24910b,_0x5d4626){if(_0x24910b[_0xc20c('0x15')]){var _0x57815c=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x57815c[_0xc20c('0x16')](_0x24910b[_0xc20c('0x15')])){_0x24910b[_0xc20c('0x15')]=_0x24910b['html'][_0xc20c('0x17')](_0x57815c,'');}_0x5d4626['body']=_0x24910b['html'];}else{_0x5d4626[_0xc20c('0x18')]=_0x24910b[_0xc20c('0x19')];}if(_0x5d4626['body']){_0x5d4626[_0xc20c('0x18')]=_0x5d4626['body'][_0xc20c('0x17')](/<a\s+/gi,'<a\x20target=\x22_blank\x22\x20');}if(_0x24910b['to']){_0x5d4626[_0xc20c('0x1a')]['originTo']=getMails(null,_0x24910b['to']);var _0x580720=getMails(_0x59612e['email'],_0x24910b['to']);if(!_['isEmpty'](_0x580720)){_0x5d4626[_0xc20c('0x1a')]['cc']+=_0x580720;}}if(_0x24910b['cc']){_0x5d4626[_0xc20c('0x1a')][_0xc20c('0x1b')]=getMails(null,_0x24910b['cc']);var _0x5a1bab=getMails(_0x59612e['email'],_0x24910b['cc']);if(!_[_0xc20c('0x13')](_0x5a1bab)){if(!_['isEmpty'](_0x5d4626[_0xc20c('0x1a')]['cc'])){_0x5d4626[_0xc20c('0x1a')]['cc']+=',';}_0x5d4626[_0xc20c('0x1a')]['cc']+=_0x5a1bab;}}if(!_[_0xc20c('0x1c')](_0x5d4626[_0xc20c('0x1a')]['originTo'],_0x59612e['email'])&&!_[_0xc20c('0x1c')](_0x5d4626['message'][_0xc20c('0x1b')],_0x59612e[_0xc20c('0x7')])){_0x5d4626[_0xc20c('0x1a')][_0xc20c('0x1d')]=_0x59612e[_0xc20c('0x7')];}if(_0x24910b[_0xc20c('0x1e')]&&_0x24910b[_0xc20c('0x1e')]['length']){_0x5d4626[_0xc20c('0x1a')][_0xc20c('0x1f')]=_0x24910b[_0xc20c('0x1e')]['length'];for(var _0x5125e1=0x0,_0x388a1d,_0x578111,_0x316b1c;_0x5125e1<_0x24910b[_0xc20c('0x1e')][_0xc20c('0xf')];_0x5125e1+=0x1){if(_0x24910b['attachments'][_0x5125e1][_0xc20c('0x20')]==='attachment'){_0x316b1c=_0x24910b[_0xc20c('0x1e')][_0x5125e1][_0xc20c('0x21')]||_0xc20c('0x22')+_0x5125e1;_0x388a1d=util[_0xc20c('0x23')]('%s-%s',Date[_0xc20c('0x24')](),_0x316b1c);_0x578111=path[_0xc20c('0x25')](config[_0xc20c('0x26')],'server/files/attachments/',_0x388a1d);fs[_0xc20c('0x27')](_0x578111,_0x24910b[_0xc20c('0x1e')][_0x5125e1][_0xc20c('0x28')]);_0x5d4626['message'][_0xc20c('0x1e')]['push']({'name':_0x316b1c,'basename':_0x388a1d,'type':_0x24910b[_0xc20c('0x1e')][_0x5125e1][_0xc20c('0x29')],'filepath':_0x578111});}}}return _0x5d4626;}exports['parser']=function(_0x46dbdf,_0xfe5958,_0x2c8294){var _0x58c665={'from':getAddress(_0xfe5958[_0xc20c('0x2a')]),'firstName':getFirstName(_0xfe5958[_0xc20c('0x2a')]),'lastName':getLastName(_0xfe5958[_0xc20c('0x2a')]),'mapKey':_0xc20c('0x7'),'message':{'from':getMails(_0x46dbdf[_0xc20c('0x7')],_0xfe5958[_0xc20c('0x2a')]),'to':_0x46dbdf['email'],'cc':'','subject':_0xfe5958[_0xc20c('0x2b')]||'','messageId':_0xfe5958[_0xc20c('0x2c')],'inReplyTo':_0xfe5958[_0xc20c('0x2d')],'sentAt':moment(_0xfe5958['date'])[_0xc20c('0x23')](_0xc20c('0x2e')),'attach':![],'attachments':[]}};if(!_0xfe5958[_0xc20c('0x15')]&&!_0xfe5958[_0xc20c('0x19')]){var _0x4b2c47=_[_0xc20c('0x2f')](_0xfe5958[_0xc20c('0x1e')],{'contentType':_0xc20c('0x30')});if(_0x4b2c47[_0xc20c('0xf')]){logger[_0xc20c('0x31')](util[_0xc20c('0x23')]('Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system',_0xfe5958[_0xc20c('0x2c')]));tnef['parseBuffer'](_['first'](_0x4b2c47)[_0xc20c('0x28')],function(_0x368929,_0x80042d){if(_0x80042d){_0xfe5958[_0xc20c('0x15')]=_0x80042d[_0xc20c('0x32')]||_0xc20c('0x33');if(_0x80042d['Attachments']){for(var _0x1dc961=0x0;_0x1dc961<_0x80042d[_0xc20c('0x34')]['length'];_0x1dc961++){var _0x9d2ac1=_0x80042d[_0xc20c('0x34')][_0x1dc961];_0x58c665[_0xc20c('0x1a')][_0xc20c('0x1e')][_0xc20c('0x35')]({'filename':_0x9d2ac1['Title'],'content':new Buffer(_0x9d2ac1[_0xc20c('0x36')])});}}}_0x2c8294(parseEmail(_0x46dbdf,_0xfe5958,_0x58c665));});}else{_0xfe5958[_0xc20c('0x15')]=_0xc20c('0x33');_0x2c8294(parseEmail(_0x46dbdf,_0xfe5958,_0x58c665));}}else{_0x2c8294(parseEmail(_0x46dbdf,_0xfe5958,_0x58c665));}};exports['getHost']=function(_0x1ad570){switch(_0x1ad570){case _0xc20c('0x37'):return'imap.gmail.com';case _0xc20c('0x38'):return _0xc20c('0x39');case'hotmail':return _0xc20c('0x3a');case _0xc20c('0x3b'):return _0xc20c('0x3c');}return _0xc20c('0x3d');};