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 _0x35f6=['body','textAsHtml','<a\x20target=\x22_blank\x22\x20','message','originTo','originCc','includes','attachments','type','attachment','filename','attachment-','format','%s-%s','now','root','server/files/attachments/','push','contentType','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>','Title','Data','getHost','gmail','imap.gmail.com','yahoo','imap.mail.yahoo.com','hotmail','lodash','moment','randomstring','path','node-tnef','../../../config/logger','email','../../../config/environment','address','anonymous','generate','value','name','split','join','Anonymous','head','length','isEmpty','html','replace'];(function(_0x29f2f4,_0xf1c507){var _0x7c76cf=function(_0x5ea59f){while(--_0x5ea59f){_0x29f2f4['push'](_0x29f2f4['shift']());}};_0x7c76cf(++_0xf1c507);}(_0x35f6,0x19d));var _0x635f=function(_0x386910,_0x748feb){_0x386910=_0x386910-0x0;var _0x199d22=_0x35f6[_0x386910];return _0x199d22;};'use strict';var _=require(_0x635f('0x0'));var moment=require(_0x635f('0x1'));var random=require(_0x635f('0x2'));var fs=require('fs');var path=require(_0x635f('0x3'));var util=require('util');var tnef=require(_0x635f('0x4'));var logger=require(_0x635f('0x5'))(_0x635f('0x6'));var config=require(_0x635f('0x7'));function getAddress(_0x4b8589){var _0xae23b4=_['head'](_0x4b8589['value']);return _0xae23b4&&_0xae23b4['address']?_0xae23b4[_0x635f('0x8')]:_0x635f('0x9')+random[_0x635f('0xa')](0x6)+'@xcally.com';}function getFirstName(_0x34951d){var _0x42b0a8=_['head'](_0x34951d[_0x635f('0xb')]);if(_0x42b0a8&&_0x42b0a8[_0x635f('0xc')]){var _0x3e8c25=_0x42b0a8[_0x635f('0xc')][_0x635f('0xd')](/\s+/);return _0x3e8c25['length']?_0x3e8c25['slice'](0x0,-0x1)[_0x635f('0xe')]('\x20'):_0x42b0a8[_0x635f('0xc')];}return _0x635f('0xf');}function getLastName(_0x3fdda4){var _0x5c8615=_[_0x635f('0x10')](_0x3fdda4['value']);if(_0x5c8615&&_0x5c8615[_0x635f('0xc')]){var _0x25f3ee=_0x5c8615['name'][_0x635f('0xd')](/\s+/);return _0x25f3ee[_0x635f('0x11')]>0x1?_0x25f3ee['pop']():'';}return'';}function getMails(_0x374856,_0x1638a5){var _0x1076ac='';for(var _0x5027e6=0x0;_0x5027e6<_0x1638a5[_0x635f('0xb')][_0x635f('0x11')];_0x5027e6+=0x1){if(_0x1638a5['value'][_0x5027e6]['address']!==_0x374856){if(!_[_0x635f('0x12')](_0x1076ac)){_0x1076ac+=',';}_0x1076ac+=_0x1638a5[_0x635f('0xb')][_0x5027e6][_0x635f('0xc')]||_0x1638a5[_0x635f('0xb')][_0x5027e6][_0x635f('0x8')]||'';_0x1076ac+='\x20<';_0x1076ac+=_0x1638a5['value'][_0x5027e6][_0x635f('0x8')]?_0x1638a5['value'][_0x5027e6]['address']:_0x635f('0x9')+random[_0x635f('0xa')](0x6)+'@xcally.com';_0x1076ac+='>';}}return _0x1076ac;}function parseEmail(_0x104e3c,_0x45e3e2,_0x12a1ec){if(_0x45e3e2[_0x635f('0x13')]){var _0x54e499=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x54e499['test'](_0x45e3e2[_0x635f('0x13')])){_0x45e3e2[_0x635f('0x13')]=_0x45e3e2[_0x635f('0x13')][_0x635f('0x14')](_0x54e499,'');}_0x12a1ec[_0x635f('0x15')]=_0x45e3e2[_0x635f('0x13')];}else{_0x12a1ec['body']=_0x45e3e2[_0x635f('0x16')];}if(_0x12a1ec[_0x635f('0x15')]){_0x12a1ec[_0x635f('0x15')]=_0x12a1ec[_0x635f('0x15')]['replace'](/<a\s+/gi,_0x635f('0x17'));}if(_0x45e3e2['to']){_0x12a1ec[_0x635f('0x18')][_0x635f('0x19')]=getMails(null,_0x45e3e2['to']);var _0x3879b3=getMails(_0x104e3c['email'],_0x45e3e2['to']);if(!_['isEmpty'](_0x3879b3)){_0x12a1ec['message']['cc']+=_0x3879b3;}}if(_0x45e3e2['cc']){_0x12a1ec[_0x635f('0x18')][_0x635f('0x1a')]=getMails(null,_0x45e3e2['cc']);var _0x5eaea3=getMails(_0x104e3c[_0x635f('0x6')],_0x45e3e2['cc']);if(!_['isEmpty'](_0x5eaea3)){if(!_['isEmpty'](_0x12a1ec[_0x635f('0x18')]['cc'])){_0x12a1ec[_0x635f('0x18')]['cc']+=',';}_0x12a1ec['message']['cc']+=_0x5eaea3;}}if(!_[_0x635f('0x1b')](_0x12a1ec[_0x635f('0x18')]['originTo'],_0x104e3c[_0x635f('0x6')])&&!_[_0x635f('0x1b')](_0x12a1ec[_0x635f('0x18')][_0x635f('0x1a')],_0x104e3c['email'])){_0x12a1ec[_0x635f('0x18')]['bcc']=_0x104e3c[_0x635f('0x6')];}if(_0x45e3e2[_0x635f('0x1c')]&&_0x45e3e2[_0x635f('0x1c')][_0x635f('0x11')]){_0x12a1ec['message']['attach']=_0x45e3e2[_0x635f('0x1c')][_0x635f('0x11')];for(var _0x2cc9db=0x0,_0x5ba06d,_0x295a19,_0x4cf608;_0x2cc9db<_0x45e3e2[_0x635f('0x1c')][_0x635f('0x11')];_0x2cc9db+=0x1){if(_0x45e3e2[_0x635f('0x1c')][_0x2cc9db][_0x635f('0x1d')]===_0x635f('0x1e')){_0x4cf608=_0x45e3e2[_0x635f('0x1c')][_0x2cc9db][_0x635f('0x1f')]||_0x635f('0x20')+_0x2cc9db;_0x5ba06d=util[_0x635f('0x21')](_0x635f('0x22'),Date[_0x635f('0x23')](),_0x4cf608);_0x295a19=path[_0x635f('0xe')](config[_0x635f('0x24')],_0x635f('0x25'),_0x5ba06d);fs['writeFileSync'](_0x295a19,_0x45e3e2[_0x635f('0x1c')][_0x2cc9db]['content']);_0x12a1ec[_0x635f('0x18')][_0x635f('0x1c')][_0x635f('0x26')]({'name':_0x4cf608,'basename':_0x5ba06d,'type':_0x45e3e2[_0x635f('0x1c')][_0x2cc9db][_0x635f('0x27')],'filepath':_0x295a19});}}}return _0x12a1ec;}exports['parser']=function(_0x44037c,_0x5e47c9,_0x217ace){var _0x960162={'from':getAddress(_0x5e47c9[_0x635f('0x28')]),'firstName':getFirstName(_0x5e47c9['from']),'lastName':getLastName(_0x5e47c9[_0x635f('0x28')]),'mapKey':_0x635f('0x6'),'message':{'from':getMails(_0x44037c['email'],_0x5e47c9[_0x635f('0x28')]),'to':_0x44037c[_0x635f('0x6')],'cc':'','subject':_0x5e47c9[_0x635f('0x29')]||'','messageId':_0x5e47c9[_0x635f('0x2a')],'inReplyTo':_0x5e47c9[_0x635f('0x2b')],'sentAt':moment(_0x5e47c9['date'])[_0x635f('0x21')](_0x635f('0x2c')),'attach':![],'attachments':[]}};if(!_0x5e47c9[_0x635f('0x13')]&&!_0x5e47c9[_0x635f('0x16')]){var _0x4270ac=_[_0x635f('0x2d')](_0x5e47c9[_0x635f('0x1c')],{'contentType':_0x635f('0x2e')});if(_0x4270ac[_0x635f('0x11')]){logger[_0x635f('0x2f')](util[_0x635f('0x21')](_0x635f('0x30'),_0x5e47c9[_0x635f('0x2a')]));tnef[_0x635f('0x31')](_[_0x635f('0x32')](_0x4270ac)[_0x635f('0x33')],function(_0x449ddf,_0x17802b){if(_0x17802b){_0x5e47c9[_0x635f('0x13')]=_0x17802b[_0x635f('0x34')]||_0x635f('0x35');if(_0x17802b['Attachments']){for(var _0x336a6e=0x0;_0x336a6e<_0x17802b['Attachments'][_0x635f('0x11')];_0x336a6e++){var _0x3ef709=_0x17802b['Attachments'][_0x336a6e];_0x960162['message'][_0x635f('0x1c')][_0x635f('0x26')]({'filename':_0x3ef709[_0x635f('0x36')],'content':new Buffer(_0x3ef709[_0x635f('0x37')])});}}}_0x217ace(parseEmail(_0x44037c,_0x5e47c9,_0x960162));});}else{_0x5e47c9['html']=_0x635f('0x35');_0x217ace(parseEmail(_0x44037c,_0x5e47c9,_0x960162));}}else{_0x217ace(parseEmail(_0x44037c,_0x5e47c9,_0x960162));}};exports[_0x635f('0x38')]=function(_0x5f0dd6){switch(_0x5f0dd6){case _0x635f('0x39'):return _0x635f('0x3a');case _0x635f('0x3b'):return _0x635f('0x3c');case _0x635f('0x3d'):return'imap-mail.outlook.com';case'outlook365':return'outlook.office365.com';}return'127.0.0.1';};