Built motion from commit (unavailable).|2.5.2
[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 _0x86ee=['content','contentType','parser','from','inReplyTo','date','filter','application/ms-tnef','info','Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system','messageId','parseBuffer','first','BodyHTML','<p>Empty\x20message</p>','Attachments','Title','Data','getHost','imap.gmail.com','yahoo','hotmail','outlook.office365.com','lodash','randomstring','util','node-tnef','../../../config/logger','head','value','address','anonymous','generate','@xcally.com','split','length','slice','join','name','isEmpty','html','test','replace','body','textAsHtml','<a\x20target=\x22_blank\x22\x20','email','message','includes','originTo','attachments','attach','type','attachment','filename','attachment-','format','%s-%s','now','server/files/attachments/','writeFileSync'];(function(_0x44dfea,_0x3a370e){var _0x5087ee=function(_0x49f066){while(--_0x49f066){_0x44dfea['push'](_0x44dfea['shift']());}};_0x5087ee(++_0x3a370e);}(_0x86ee,0xce));var _0xe86e=function(_0x3c2a69,_0x4c8b41){_0x3c2a69=_0x3c2a69-0x0;var _0x596bc8=_0x86ee[_0x3c2a69];return _0x596bc8;};'use strict';var _=require(_0xe86e('0x0'));var moment=require('moment');var random=require(_0xe86e('0x1'));var fs=require('fs');var path=require('path');var util=require(_0xe86e('0x2'));var tnef=require(_0xe86e('0x3'));var logger=require(_0xe86e('0x4'))('email');var config=require('../../../config/environment');function getAddress(_0x3f1bf7){var _0x272618=_[_0xe86e('0x5')](_0x3f1bf7[_0xe86e('0x6')]);return _0x272618&&_0x272618[_0xe86e('0x7')]?_0x272618[_0xe86e('0x7')]:_0xe86e('0x8')+random[_0xe86e('0x9')](0x6)+_0xe86e('0xa');}function getFirstName(_0x5bf968){var _0x1f9447=_[_0xe86e('0x5')](_0x5bf968[_0xe86e('0x6')]);if(_0x1f9447&&_0x1f9447['name']){var _0x4d3806=_0x1f9447['name'][_0xe86e('0xb')](/\s+/);return _0x4d3806[_0xe86e('0xc')]?_0x4d3806[_0xe86e('0xd')](0x0,-0x1)[_0xe86e('0xe')]('\x20'):_0x1f9447['name'];}return'Anonymous';}function getLastName(_0x44961a){var _0x6ede5b=_['head'](_0x44961a[_0xe86e('0x6')]);if(_0x6ede5b&&_0x6ede5b[_0xe86e('0xf')]){var _0x1531ef=_0x6ede5b['name'][_0xe86e('0xb')](/\s+/);return _0x1531ef[_0xe86e('0xc')]>0x1?_0x1531ef['pop']():'';}return'';}function getMails(_0x812c1e,_0x1d6501){var _0x1d16f9='';for(var _0x615015=0x0;_0x615015<_0x1d6501[_0xe86e('0x6')][_0xe86e('0xc')];_0x615015+=0x1){if(_0x1d6501['value'][_0x615015][_0xe86e('0x7')]!==_0x812c1e){if(!_[_0xe86e('0x10')](_0x1d16f9)){_0x1d16f9+=',';}_0x1d16f9+=_0x1d6501[_0xe86e('0x6')][_0x615015]['name']||_0x1d6501[_0xe86e('0x6')][_0x615015]['address']||'';_0x1d16f9+='\x20<';_0x1d16f9+=_0x1d6501[_0xe86e('0x6')][_0x615015]['address']?_0x1d6501[_0xe86e('0x6')][_0x615015][_0xe86e('0x7')]:_0xe86e('0x8')+random[_0xe86e('0x9')](0x6)+_0xe86e('0xa');_0x1d16f9+='>';}}return _0x1d16f9;}function parseEmail(_0x50c274,_0xc9658a,_0x41e3eb){if(_0xc9658a[_0xe86e('0x11')]){var _0x269e5a=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x269e5a[_0xe86e('0x12')](_0xc9658a[_0xe86e('0x11')])){_0xc9658a[_0xe86e('0x11')]=_0xc9658a[_0xe86e('0x11')][_0xe86e('0x13')](_0x269e5a,'');}_0x41e3eb['body']=_0xc9658a[_0xe86e('0x11')];}else{_0x41e3eb[_0xe86e('0x14')]=_0xc9658a[_0xe86e('0x15')];}if(_0x41e3eb[_0xe86e('0x14')]){_0x41e3eb[_0xe86e('0x14')]=_0x41e3eb[_0xe86e('0x14')][_0xe86e('0x13')](/<a\s+/gi,_0xe86e('0x16'));}if(_0xc9658a['to']){_0x41e3eb['message']['originTo']=getMails(null,_0xc9658a['to']);var _0x336744=getMails(_0x50c274[_0xe86e('0x17')],_0xc9658a['to']);if(!_['isEmpty'](_0x336744)){_0x41e3eb[_0xe86e('0x18')]['cc']+=_0x336744;}}if(_0xc9658a['cc']){_0x41e3eb[_0xe86e('0x18')]['originCc']=getMails(null,_0xc9658a['cc']);var _0x49c749=getMails(_0x50c274[_0xe86e('0x17')],_0xc9658a['cc']);if(!_[_0xe86e('0x10')](_0x49c749)){if(!_[_0xe86e('0x10')](_0x41e3eb[_0xe86e('0x18')]['cc'])){_0x41e3eb[_0xe86e('0x18')]['cc']+=',';}_0x41e3eb[_0xe86e('0x18')]['cc']+=_0x49c749;}}if(!_[_0xe86e('0x19')](_0x41e3eb['message'][_0xe86e('0x1a')],_0x50c274[_0xe86e('0x17')])&&!_['includes'](_0x41e3eb[_0xe86e('0x18')]['originCc'],_0x50c274[_0xe86e('0x17')])){_0x41e3eb[_0xe86e('0x18')]['bcc']=_0x50c274[_0xe86e('0x17')];}if(_0xc9658a[_0xe86e('0x1b')]&&_0xc9658a[_0xe86e('0x1b')][_0xe86e('0xc')]){_0x41e3eb[_0xe86e('0x18')][_0xe86e('0x1c')]=_0xc9658a[_0xe86e('0x1b')][_0xe86e('0xc')];for(var _0x37506f=0x0,_0x263fa3,_0x53ab38,_0x38235a;_0x37506f<_0xc9658a[_0xe86e('0x1b')]['length'];_0x37506f+=0x1){if(_0xc9658a[_0xe86e('0x1b')][_0x37506f][_0xe86e('0x1d')]===_0xe86e('0x1e')){_0x38235a=_0xc9658a['attachments'][_0x37506f][_0xe86e('0x1f')]||_0xe86e('0x20')+_0x37506f;_0x263fa3=util[_0xe86e('0x21')](_0xe86e('0x22'),Date[_0xe86e('0x23')](),_0x38235a);_0x53ab38=path[_0xe86e('0xe')](config['root'],_0xe86e('0x24'),_0x263fa3);fs[_0xe86e('0x25')](_0x53ab38,_0xc9658a[_0xe86e('0x1b')][_0x37506f][_0xe86e('0x26')]);_0x41e3eb[_0xe86e('0x18')][_0xe86e('0x1b')]['push']({'name':_0x38235a,'basename':_0x263fa3,'type':_0xc9658a[_0xe86e('0x1b')][_0x37506f][_0xe86e('0x27')],'filepath':_0x53ab38});}}}return _0x41e3eb;}exports[_0xe86e('0x28')]=function(_0x46faaf,_0x47e901,_0x3e8dbe){var _0x5a7356={'from':getAddress(_0x47e901[_0xe86e('0x29')]),'firstName':getFirstName(_0x47e901[_0xe86e('0x29')]),'lastName':getLastName(_0x47e901[_0xe86e('0x29')]),'mapKey':'email','message':{'from':getMails(_0x46faaf[_0xe86e('0x17')],_0x47e901[_0xe86e('0x29')]),'to':_0x46faaf[_0xe86e('0x17')],'cc':'','subject':_0x47e901['subject']||'','messageId':_0x47e901['messageId'],'inReplyTo':_0x47e901[_0xe86e('0x2a')],'sentAt':moment(_0x47e901[_0xe86e('0x2b')])['format']('YYYY-MM-DD\x20HH:mm:ss'),'attach':![],'attachments':[]}};if(!_0x47e901[_0xe86e('0x11')]&&!_0x47e901[_0xe86e('0x15')]){var _0x31c669=_[_0xe86e('0x2c')](_0x47e901[_0xe86e('0x1b')],{'contentType':_0xe86e('0x2d')});if(_0x31c669[_0xe86e('0xc')]){logger[_0xe86e('0x2e')](util[_0xe86e('0x21')](_0xe86e('0x2f'),_0x47e901[_0xe86e('0x30')]));tnef[_0xe86e('0x31')](_[_0xe86e('0x32')](_0x31c669)[_0xe86e('0x26')],function(_0x2901bc,_0x533465){if(_0x533465){_0x47e901[_0xe86e('0x11')]=_0x533465[_0xe86e('0x33')]||_0xe86e('0x34');if(_0x533465['Attachments']){for(var _0x5f2b73=0x0;_0x5f2b73<_0x533465[_0xe86e('0x35')][_0xe86e('0xc')];_0x5f2b73++){var _0x175d43=_0x533465[_0xe86e('0x35')][_0x5f2b73];_0x5a7356[_0xe86e('0x18')]['attachments']['push']({'filename':_0x175d43[_0xe86e('0x36')],'content':new Buffer(_0x175d43[_0xe86e('0x37')])});}}}_0x3e8dbe(parseEmail(_0x46faaf,_0x47e901,_0x5a7356));});}else{_0x47e901[_0xe86e('0x11')]=_0xe86e('0x34');_0x3e8dbe(parseEmail(_0x46faaf,_0x47e901,_0x5a7356));}}else{_0x3e8dbe(parseEmail(_0x46faaf,_0x47e901,_0x5a7356));}};exports[_0xe86e('0x38')]=function(_0x1104cf){switch(_0x1104cf){case'gmail':return _0xe86e('0x39');case _0xe86e('0x3a'):return'imap.mail.yahoo.com';case _0xe86e('0x3b'):return'imap-mail.outlook.com';case'outlook365':return _0xe86e('0x3c');}return'127.0.0.1';};