99f237d1361e99c6098206aa46bb493742046ed2
[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 _0x20f8=['format','%s-%s','now','root','server/files/attachments/','content','push','contentType','parser','from','subject','messageId','inReplyTo','date','YYYY-MM-DD\x20HH:mm:ss','filter','info','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','parseBuffer','first','Attachments','Data','getHost','gmail','hotmail','imap-mail.outlook.com','outlook365','127.0.0.1','lodash','moment','randomstring','path','util','node-tnef','../../../config/logger','email','head','value','address','anonymous','generate','split','slice','join','name','Anonymous','pop','length','isEmpty','@xcally.com','html','replace','body','message','originTo','includes','originCc','bcc','attachments','attach','type','attachment','filename','attachment-'];(function(_0x510759,_0x5a53ea){var _0x484be6=function(_0x55e5c2){while(--_0x55e5c2){_0x510759['push'](_0x510759['shift']());}};_0x484be6(++_0x5a53ea);}(_0x20f8,0x9c));var _0x820f=function(_0x23e828,_0x16f975){_0x23e828=_0x23e828-0x0;var _0x4f90df=_0x20f8[_0x23e828];return _0x4f90df;};'use strict';var _=require(_0x820f('0x0'));var moment=require(_0x820f('0x1'));var random=require(_0x820f('0x2'));var fs=require('fs');var path=require(_0x820f('0x3'));var util=require(_0x820f('0x4'));var tnef=require(_0x820f('0x5'));var logger=require(_0x820f('0x6'))(_0x820f('0x7'));var config=require('../../../config/environment');function getAddress(_0x568a43){var _0x2ddbbc=_[_0x820f('0x8')](_0x568a43[_0x820f('0x9')]);return _0x2ddbbc&&_0x2ddbbc[_0x820f('0xa')]?_0x2ddbbc['address']:_0x820f('0xb')+random[_0x820f('0xc')](0x6)+'@xcally.com';}function getFirstName(_0x230712){var _0x3e65c9=_[_0x820f('0x8')](_0x230712[_0x820f('0x9')]);if(_0x3e65c9&&_0x3e65c9['name']){var _0x435010=_0x3e65c9['name'][_0x820f('0xd')](/\s+/);return _0x435010['length']?_0x435010[_0x820f('0xe')](0x0,-0x1)[_0x820f('0xf')]('\x20'):_0x3e65c9[_0x820f('0x10')];}return _0x820f('0x11');}function getLastName(_0x298ef5){var _0x91164d=_[_0x820f('0x8')](_0x298ef5[_0x820f('0x9')]);if(_0x91164d&&_0x91164d[_0x820f('0x10')]){var _0x261aeb=_0x91164d[_0x820f('0x10')][_0x820f('0xd')](/\s+/);return _0x261aeb['length']>0x1?_0x261aeb[_0x820f('0x12')]():'';}return'';}function getMails(_0x14e263,_0x3ffe77){var _0x2d67a3='';for(var _0x5515f7=0x0;_0x5515f7<_0x3ffe77[_0x820f('0x9')][_0x820f('0x13')];_0x5515f7+=0x1){if(_0x3ffe77['value'][_0x5515f7][_0x820f('0xa')]!==_0x14e263){if(!_[_0x820f('0x14')](_0x2d67a3)){_0x2d67a3+=',';}_0x2d67a3+=_0x3ffe77['value'][_0x5515f7][_0x820f('0x10')]||_0x3ffe77[_0x820f('0x9')][_0x5515f7][_0x820f('0xa')]||'';_0x2d67a3+='\x20<';_0x2d67a3+=_0x3ffe77['value'][_0x5515f7]['address']?_0x3ffe77[_0x820f('0x9')][_0x5515f7]['address']:_0x820f('0xb')+random[_0x820f('0xc')](0x6)+_0x820f('0x15');_0x2d67a3+='>';}}return _0x2d67a3;}function parseEmail(_0x4973e5,_0x11a2b3,_0x381aa3){if(_0x11a2b3[_0x820f('0x16')]){var _0x22bfa3=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x22bfa3['test'](_0x11a2b3['html'])){_0x11a2b3[_0x820f('0x16')]=_0x11a2b3[_0x820f('0x16')][_0x820f('0x17')](_0x22bfa3,'');}_0x381aa3[_0x820f('0x18')]=_0x11a2b3['html'];}else{_0x381aa3['body']=_0x11a2b3['textAsHtml'];}if(_0x381aa3[_0x820f('0x18')]){_0x381aa3[_0x820f('0x18')]=_0x381aa3[_0x820f('0x18')]['replace'](/<a\s+/gi,'<a\x20target=\x22_blank\x22\x20');}if(_0x11a2b3['to']){_0x381aa3[_0x820f('0x19')][_0x820f('0x1a')]=getMails(null,_0x11a2b3['to']);var _0x4d4030=getMails(_0x4973e5[_0x820f('0x7')],_0x11a2b3['to']);if(!_[_0x820f('0x14')](_0x4d4030)){_0x381aa3[_0x820f('0x19')]['cc']+=_0x4d4030;}}if(_0x11a2b3['cc']){_0x381aa3['message']['originCc']=getMails(null,_0x11a2b3['cc']);var _0x21eb44=getMails(_0x4973e5[_0x820f('0x7')],_0x11a2b3['cc']);if(!_[_0x820f('0x14')](_0x21eb44)){if(!_[_0x820f('0x14')](_0x381aa3['message']['cc'])){_0x381aa3['message']['cc']+=',';}_0x381aa3[_0x820f('0x19')]['cc']+=_0x21eb44;}}if(!_[_0x820f('0x1b')](_0x381aa3['message'][_0x820f('0x1a')],_0x4973e5[_0x820f('0x7')])&&!_[_0x820f('0x1b')](_0x381aa3[_0x820f('0x19')][_0x820f('0x1c')],_0x4973e5['email'])){_0x381aa3[_0x820f('0x19')][_0x820f('0x1d')]=_0x4973e5[_0x820f('0x7')];}if(_0x11a2b3[_0x820f('0x1e')]&&_0x11a2b3[_0x820f('0x1e')][_0x820f('0x13')]){_0x381aa3[_0x820f('0x19')][_0x820f('0x1f')]=_0x11a2b3['attachments'][_0x820f('0x13')];for(var _0x31c9cc=0x0,_0x870049,_0x553bad,_0x2e861f;_0x31c9cc<_0x11a2b3[_0x820f('0x1e')][_0x820f('0x13')];_0x31c9cc+=0x1){if(_0x11a2b3[_0x820f('0x1e')][_0x31c9cc][_0x820f('0x20')]===_0x820f('0x21')){_0x2e861f=_0x11a2b3[_0x820f('0x1e')][_0x31c9cc][_0x820f('0x22')]||_0x820f('0x23')+_0x31c9cc;_0x870049=util[_0x820f('0x24')](_0x820f('0x25'),Date[_0x820f('0x26')](),_0x2e861f);_0x553bad=path[_0x820f('0xf')](config[_0x820f('0x27')],_0x820f('0x28'),_0x870049);fs['writeFileSync'](_0x553bad,_0x11a2b3[_0x820f('0x1e')][_0x31c9cc][_0x820f('0x29')]);_0x381aa3[_0x820f('0x19')][_0x820f('0x1e')][_0x820f('0x2a')]({'name':_0x2e861f,'basename':_0x870049,'type':_0x11a2b3[_0x820f('0x1e')][_0x31c9cc][_0x820f('0x2b')],'filepath':_0x553bad});}}}return _0x381aa3;}exports[_0x820f('0x2c')]=function(_0x2248ce,_0x3686d5,_0x34a11a){var _0x12606c={'from':getAddress(_0x3686d5[_0x820f('0x2d')]),'firstName':getFirstName(_0x3686d5[_0x820f('0x2d')]),'lastName':getLastName(_0x3686d5[_0x820f('0x2d')]),'mapKey':_0x820f('0x7'),'message':{'from':getMails(_0x2248ce['email'],_0x3686d5['from']),'to':_0x2248ce[_0x820f('0x7')],'cc':'','subject':_0x3686d5[_0x820f('0x2e')]||'','messageId':_0x3686d5[_0x820f('0x2f')],'inReplyTo':_0x3686d5[_0x820f('0x30')],'sentAt':moment(_0x3686d5[_0x820f('0x31')])[_0x820f('0x24')](_0x820f('0x32')),'attach':![],'attachments':[]}};if(!_0x3686d5[_0x820f('0x16')]&&!_0x3686d5['textAsHtml']){var _0x297e21=_[_0x820f('0x33')](_0x3686d5[_0x820f('0x1e')],{'contentType':'application/ms-tnef'});if(_0x297e21[_0x820f('0x13')]){logger[_0x820f('0x34')](util[_0x820f('0x24')](_0x820f('0x35'),_0x3686d5[_0x820f('0x2f')]));tnef[_0x820f('0x36')](_[_0x820f('0x37')](_0x297e21)[_0x820f('0x29')],function(_0x3e1532,_0x54dbcc){if(_0x54dbcc){_0x3686d5[_0x820f('0x16')]=_0x54dbcc['BodyHTML']||'<p>Empty\x20message</p>';if(_0x54dbcc[_0x820f('0x38')]){for(var _0x4337b1=0x0;_0x4337b1<_0x54dbcc['Attachments'][_0x820f('0x13')];_0x4337b1++){var _0x5540a7=_0x54dbcc['Attachments'][_0x4337b1];_0x12606c['message'][_0x820f('0x1e')][_0x820f('0x2a')]({'filename':_0x5540a7['Title'],'content':new Buffer(_0x5540a7[_0x820f('0x39')])});}}}_0x34a11a(parseEmail(_0x2248ce,_0x3686d5,_0x12606c));});}else{_0x3686d5[_0x820f('0x16')]='<p>Empty\x20message</p>';_0x34a11a(parseEmail(_0x2248ce,_0x3686d5,_0x12606c));}}else{_0x34a11a(parseEmail(_0x2248ce,_0x3686d5,_0x12606c));}};exports[_0x820f('0x3a')]=function(_0x514499){switch(_0x514499){case _0x820f('0x3b'):return'imap.gmail.com';case'yahoo':return'imap.mail.yahoo.com';case _0x820f('0x3c'):return _0x820f('0x3d');case _0x820f('0x3e'):return'outlook.office365.com';}return _0x820f('0x3f');};