Built motion from commit (unavailable).|2.3.4
[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 _0x0348=['value','address','anonymous','generate','@xcally.com','name','split','length','join','Anonymous','isEmpty','html','replace','body','textAsHtml','originTo','message','originCc','includes','attachments','attach','type','attachment','filename','attachment-','%s-%s','root','server/files/attachments/','content','push','contentType','from','subject','messageId','inReplyTo','date','format','YYYY-MM-DD\x20HH:mm:ss','application/ms-tnef','info','parseBuffer','BodyHTML','<p>Empty\x20message</p>','Attachments','Title','Data','gmail','imap.gmail.com','yahoo','imap.mail.yahoo.com','hotmail','imap-mail.outlook.com','outlook365','outlook.office365.com','127.0.0.1','moment','path','util','../../../config/logger','email','../../../config/environment','head'];(function(_0x2b6596,_0xed330a){var _0x2f49e3=function(_0x10cc8a){while(--_0x10cc8a){_0x2b6596['push'](_0x2b6596['shift']());}};_0x2f49e3(++_0xed330a);}(_0x0348,0x1e9));var _0x8034=function(_0x141749,_0x477b04){_0x141749=_0x141749-0x0;var _0x3e6932=_0x0348[_0x141749];return _0x3e6932;};'use strict';var _=require('lodash');var moment=require(_0x8034('0x0'));var random=require('randomstring');var fs=require('fs');var path=require(_0x8034('0x1'));var util=require(_0x8034('0x2'));var tnef=require('node-tnef');var logger=require(_0x8034('0x3'))(_0x8034('0x4'));var config=require(_0x8034('0x5'));function getAddress(_0x18f41e){var _0x1500ae=_[_0x8034('0x6')](_0x18f41e[_0x8034('0x7')]);return _0x1500ae&&_0x1500ae[_0x8034('0x8')]?_0x1500ae['address']:_0x8034('0x9')+random[_0x8034('0xa')](0x6)+_0x8034('0xb');}function getFirstName(_0x3ffa94){var _0x326344=_[_0x8034('0x6')](_0x3ffa94[_0x8034('0x7')]);if(_0x326344&&_0x326344[_0x8034('0xc')]){var _0x236566=_0x326344[_0x8034('0xc')][_0x8034('0xd')](/\s+/);return _0x236566[_0x8034('0xe')]?_0x236566['slice'](0x0,-0x1)[_0x8034('0xf')]('\x20'):_0x326344['name'];}return _0x8034('0x10');}function getLastName(_0x50c214){var _0x362fee=_['head'](_0x50c214['value']);if(_0x362fee&&_0x362fee[_0x8034('0xc')]){var _0x308085=_0x362fee[_0x8034('0xc')]['split'](/\s+/);return _0x308085[_0x8034('0xe')]>0x1?_0x308085['pop']():'';}return'';}function getMails(_0x34161b,_0x4a2c7b){var _0x428809='';for(var _0x370827=0x0;_0x370827<_0x4a2c7b[_0x8034('0x7')][_0x8034('0xe')];_0x370827+=0x1){if(_0x4a2c7b['value'][_0x370827]['address']!==_0x34161b){if(!_[_0x8034('0x11')](_0x428809)){_0x428809+=',';}_0x428809+=_0x4a2c7b[_0x8034('0x7')][_0x370827][_0x8034('0xc')]||_0x4a2c7b[_0x8034('0x7')][_0x370827][_0x8034('0x8')]||'';_0x428809+='\x20<';_0x428809+=_0x4a2c7b[_0x8034('0x7')][_0x370827][_0x8034('0x8')]?_0x4a2c7b['value'][_0x370827][_0x8034('0x8')]:_0x8034('0x9')+random[_0x8034('0xa')](0x6)+_0x8034('0xb');_0x428809+='>';}}return _0x428809;}function parseEmail(_0x487179,_0x3d0284,_0x2e14b6){if(_0x3d0284[_0x8034('0x12')]){var _0x3bbc55=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x3bbc55['test'](_0x3d0284[_0x8034('0x12')])){_0x3d0284['html']=_0x3d0284[_0x8034('0x12')][_0x8034('0x13')](_0x3bbc55,'');}_0x2e14b6[_0x8034('0x14')]=_0x3d0284[_0x8034('0x12')];}else{_0x2e14b6[_0x8034('0x14')]=_0x3d0284[_0x8034('0x15')];}if(_0x2e14b6['body']){_0x2e14b6['body']=_0x2e14b6[_0x8034('0x14')][_0x8034('0x13')](/<a\s+/gi,'<a\x20target=\x22_blank\x22\x20');}if(_0x3d0284['to']){_0x2e14b6['message'][_0x8034('0x16')]=getMails(null,_0x3d0284['to']);var _0x345279=getMails(_0x487179[_0x8034('0x4')],_0x3d0284['to']);if(!_[_0x8034('0x11')](_0x345279)){_0x2e14b6['message']['cc']+=_0x345279;}}if(_0x3d0284['cc']){_0x2e14b6[_0x8034('0x17')][_0x8034('0x18')]=getMails(null,_0x3d0284['cc']);var _0x514e4b=getMails(_0x487179[_0x8034('0x4')],_0x3d0284['cc']);if(!_[_0x8034('0x11')](_0x514e4b)){if(!_[_0x8034('0x11')](_0x2e14b6['message']['cc'])){_0x2e14b6[_0x8034('0x17')]['cc']+=',';}_0x2e14b6['message']['cc']+=_0x514e4b;}}if(!_[_0x8034('0x19')](_0x2e14b6['message'][_0x8034('0x16')],_0x487179['email'])&&!_[_0x8034('0x19')](_0x2e14b6[_0x8034('0x17')]['originCc'],_0x487179[_0x8034('0x4')])){_0x2e14b6[_0x8034('0x17')]['bcc']=_0x487179[_0x8034('0x4')];}if(_0x3d0284[_0x8034('0x1a')]&&_0x3d0284[_0x8034('0x1a')][_0x8034('0xe')]){_0x2e14b6[_0x8034('0x17')][_0x8034('0x1b')]=_0x3d0284[_0x8034('0x1a')][_0x8034('0xe')];for(var _0x2fbe9d=0x0,_0x24552d,_0x3bbb06,_0x18d845;_0x2fbe9d<_0x3d0284[_0x8034('0x1a')][_0x8034('0xe')];_0x2fbe9d+=0x1){if(_0x3d0284[_0x8034('0x1a')][_0x2fbe9d][_0x8034('0x1c')]===_0x8034('0x1d')){_0x18d845=_0x3d0284[_0x8034('0x1a')][_0x2fbe9d][_0x8034('0x1e')]||_0x8034('0x1f')+_0x2fbe9d;_0x24552d=util['format'](_0x8034('0x20'),Date['now'](),_0x18d845);_0x3bbb06=path[_0x8034('0xf')](config[_0x8034('0x21')],_0x8034('0x22'),_0x24552d);fs['writeFileSync'](_0x3bbb06,_0x3d0284[_0x8034('0x1a')][_0x2fbe9d][_0x8034('0x23')]);_0x2e14b6[_0x8034('0x17')][_0x8034('0x1a')][_0x8034('0x24')]({'name':_0x18d845,'basename':_0x24552d,'type':_0x3d0284['attachments'][_0x2fbe9d][_0x8034('0x25')],'filepath':_0x3bbb06});}}}return _0x2e14b6;}exports['parser']=function(_0x54d124,_0x11ed4c,_0x5ee5b8){var _0x41f912={'from':getAddress(_0x11ed4c[_0x8034('0x26')]),'firstName':getFirstName(_0x11ed4c[_0x8034('0x26')]),'lastName':getLastName(_0x11ed4c['from']),'mapKey':_0x8034('0x4'),'message':{'from':getMails(_0x54d124[_0x8034('0x4')],_0x11ed4c['from']),'to':_0x54d124[_0x8034('0x4')],'cc':'','subject':_0x11ed4c[_0x8034('0x27')]||'','messageId':_0x11ed4c[_0x8034('0x28')],'inReplyTo':_0x11ed4c[_0x8034('0x29')],'sentAt':moment(_0x11ed4c[_0x8034('0x2a')])[_0x8034('0x2b')](_0x8034('0x2c')),'attach':![],'attachments':[]}};if(!_0x11ed4c['html']&&!_0x11ed4c[_0x8034('0x15')]){var _0x3cc109=_['filter'](_0x11ed4c[_0x8034('0x1a')],{'contentType':_0x8034('0x2d')});if(_0x3cc109[_0x8034('0xe')]){logger[_0x8034('0x2e')](util[_0x8034('0x2b')]('Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system',_0x11ed4c[_0x8034('0x28')]));tnef[_0x8034('0x2f')](_['first'](_0x3cc109)[_0x8034('0x23')],function(_0xdb1b30,_0x6fdc86){if(_0x6fdc86){_0x11ed4c['html']=_0x6fdc86[_0x8034('0x30')]||_0x8034('0x31');if(_0x6fdc86[_0x8034('0x32')]){for(var _0x4f3e7e=0x0;_0x4f3e7e<_0x6fdc86[_0x8034('0x32')][_0x8034('0xe')];_0x4f3e7e++){var _0x6a22ef=_0x6fdc86[_0x8034('0x32')][_0x4f3e7e];_0x41f912[_0x8034('0x17')]['attachments']['push']({'filename':_0x6a22ef[_0x8034('0x33')],'content':new Buffer(_0x6a22ef[_0x8034('0x34')])});}}}_0x5ee5b8(parseEmail(_0x54d124,_0x11ed4c,_0x41f912));});}else{_0x11ed4c[_0x8034('0x12')]=_0x8034('0x31');_0x5ee5b8(parseEmail(_0x54d124,_0x11ed4c,_0x41f912));}}else{_0x5ee5b8(parseEmail(_0x54d124,_0x11ed4c,_0x41f912));}};exports['getHost']=function(_0x42642c){switch(_0x42642c){case _0x8034('0x35'):return _0x8034('0x36');case _0x8034('0x37'):return _0x8034('0x38');case _0x8034('0x39'):return _0x8034('0x3a');case _0x8034('0x3b'):return _0x8034('0x3c');}return _0x8034('0x3d');};