395e90f1df67b9b5445723f7a4eae1fc97b2e900
[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 _0xd517=['inReplyTo','filter','first','BodyHTML','<p>Empty\x20message</p>','Attachments','push','getHost','gmail','imap.gmail.com','yahoo','imap.mail.yahoo.com','imap-mail.outlook.com','outlook365','outlook.office365.com','moment','path','util','node-tnef','../../../config/logger','head','value','address','anonymous','generate','split','length','slice','Anonymous','name','pop','@xcally.com','html','test','replace','textAsHtml','body','<a\x20target=\x22_blank\x22\x20','message','email','isEmpty','originCc','includes','originTo','attachments','attach','type','attachment','format','%s-%s','now','join','server/files/attachments/','writeFileSync','content','contentType','parser','from','messageId'];(function(_0x13b0a3,_0x41b97a){var _0x6d318e=function(_0x564d58){while(--_0x564d58){_0x13b0a3['push'](_0x13b0a3['shift']());}};_0x6d318e(++_0x41b97a);}(_0xd517,0xfb));var _0x7d51=function(_0x450bf9,_0x1895de){_0x450bf9=_0x450bf9-0x0;var _0x268350=_0xd517[_0x450bf9];return _0x268350;};'use strict';var _=require('lodash');var moment=require(_0x7d51('0x0'));var random=require('randomstring');var fs=require('fs');var path=require(_0x7d51('0x1'));var util=require(_0x7d51('0x2'));var tnef=require(_0x7d51('0x3'));var logger=require(_0x7d51('0x4'))('email');var config=require('../../../config/environment');function getAddress(_0x16c841){var _0x422548=_[_0x7d51('0x5')](_0x16c841[_0x7d51('0x6')]);return _0x422548&&_0x422548[_0x7d51('0x7')]?_0x422548[_0x7d51('0x7')]:_0x7d51('0x8')+random[_0x7d51('0x9')](0x6)+'@xcally.com';}function getFirstName(_0x8f2f1){var _0x2ab49f=_['head'](_0x8f2f1[_0x7d51('0x6')]);if(_0x2ab49f&&_0x2ab49f['name']){var _0x4191fb=_0x2ab49f['name'][_0x7d51('0xa')](/\s+/);return _0x4191fb[_0x7d51('0xb')]?_0x4191fb[_0x7d51('0xc')](0x0,-0x1)['join']('\x20'):_0x2ab49f['name'];}return _0x7d51('0xd');}function getLastName(_0x576d8f){var _0xca79b2=_[_0x7d51('0x5')](_0x576d8f[_0x7d51('0x6')]);if(_0xca79b2&&_0xca79b2[_0x7d51('0xe')]){var _0xdbcd1d=_0xca79b2[_0x7d51('0xe')][_0x7d51('0xa')](/\s+/);return _0xdbcd1d[_0x7d51('0xb')]>0x1?_0xdbcd1d[_0x7d51('0xf')]():'';}return'';}function getMails(_0x2530c3,_0x16db1e){var _0xa1f3c3='';for(var _0x1f966d=0x0;_0x1f966d<_0x16db1e[_0x7d51('0x6')][_0x7d51('0xb')];_0x1f966d+=0x1){if(_0x16db1e[_0x7d51('0x6')][_0x1f966d]['address']!==_0x2530c3){if(!_['isEmpty'](_0xa1f3c3)){_0xa1f3c3+=',';}_0xa1f3c3+=_0x16db1e[_0x7d51('0x6')][_0x1f966d][_0x7d51('0xe')]||_0x16db1e['value'][_0x1f966d][_0x7d51('0x7')]||'';_0xa1f3c3+='\x20<';_0xa1f3c3+=_0x16db1e[_0x7d51('0x6')][_0x1f966d][_0x7d51('0x7')]?_0x16db1e[_0x7d51('0x6')][_0x1f966d][_0x7d51('0x7')]:'anonymous'+random[_0x7d51('0x9')](0x6)+_0x7d51('0x10');_0xa1f3c3+='>';}}return _0xa1f3c3;}function parseEmail(_0x41d7d2,_0x1c8a22,_0x32b491){if(_0x1c8a22[_0x7d51('0x11')]){var _0x43fe9d=/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi;while(_0x43fe9d[_0x7d51('0x12')](_0x1c8a22['html'])){_0x1c8a22[_0x7d51('0x11')]=_0x1c8a22['html'][_0x7d51('0x13')](_0x43fe9d,'');}_0x32b491['body']=_0x1c8a22['html'];}else{_0x32b491['body']=_0x1c8a22[_0x7d51('0x14')];}if(_0x32b491['body']){_0x32b491[_0x7d51('0x15')]=_0x32b491['body']['replace'](/<a\s+/gi,_0x7d51('0x16'));}if(_0x1c8a22['to']){_0x32b491[_0x7d51('0x17')]['originTo']=getMails(null,_0x1c8a22['to']);var _0x4a622a=getMails(_0x41d7d2[_0x7d51('0x18')],_0x1c8a22['to']);if(!_[_0x7d51('0x19')](_0x4a622a)){_0x32b491['message']['cc']+=_0x4a622a;}}if(_0x1c8a22['cc']){_0x32b491[_0x7d51('0x17')][_0x7d51('0x1a')]=getMails(null,_0x1c8a22['cc']);var _0x299744=getMails(_0x41d7d2[_0x7d51('0x18')],_0x1c8a22['cc']);if(!_[_0x7d51('0x19')](_0x299744)){if(!_['isEmpty'](_0x32b491['message']['cc'])){_0x32b491[_0x7d51('0x17')]['cc']+=',';}_0x32b491[_0x7d51('0x17')]['cc']+=_0x299744;}}if(!_[_0x7d51('0x1b')](_0x32b491['message'][_0x7d51('0x1c')],_0x41d7d2['email'])&&!_[_0x7d51('0x1b')](_0x32b491[_0x7d51('0x17')]['originCc'],_0x41d7d2[_0x7d51('0x18')])){_0x32b491[_0x7d51('0x17')]['bcc']=_0x41d7d2['email'];}if(_0x1c8a22[_0x7d51('0x1d')]&&_0x1c8a22[_0x7d51('0x1d')][_0x7d51('0xb')]){_0x32b491['message'][_0x7d51('0x1e')]=_0x1c8a22[_0x7d51('0x1d')][_0x7d51('0xb')];for(var _0x4bd940=0x0,_0x87e5df,_0x22fd52,_0x5c5924;_0x4bd940<_0x1c8a22[_0x7d51('0x1d')]['length'];_0x4bd940+=0x1){if(_0x1c8a22[_0x7d51('0x1d')][_0x4bd940][_0x7d51('0x1f')]===_0x7d51('0x20')){_0x5c5924=_0x1c8a22[_0x7d51('0x1d')][_0x4bd940]['filename']||'attachment-'+_0x4bd940;_0x87e5df=util[_0x7d51('0x21')](_0x7d51('0x22'),Date[_0x7d51('0x23')](),_0x5c5924);_0x22fd52=path[_0x7d51('0x24')](config['root'],_0x7d51('0x25'),_0x87e5df);fs[_0x7d51('0x26')](_0x22fd52,_0x1c8a22[_0x7d51('0x1d')][_0x4bd940][_0x7d51('0x27')]);_0x32b491[_0x7d51('0x17')][_0x7d51('0x1d')]['push']({'name':_0x5c5924,'basename':_0x87e5df,'type':_0x1c8a22[_0x7d51('0x1d')][_0x4bd940][_0x7d51('0x28')],'filepath':_0x22fd52});}}}return _0x32b491;}exports[_0x7d51('0x29')]=function(_0x2d8809,_0x50cffa,_0x41742c){var _0xd763c1={'from':getAddress(_0x50cffa[_0x7d51('0x2a')]),'firstName':getFirstName(_0x50cffa['from']),'lastName':getLastName(_0x50cffa[_0x7d51('0x2a')]),'mapKey':_0x7d51('0x18'),'message':{'from':getMails(_0x2d8809[_0x7d51('0x18')],_0x50cffa[_0x7d51('0x2a')]),'to':_0x2d8809['email'],'cc':'','subject':_0x50cffa['subject']||'','messageId':_0x50cffa[_0x7d51('0x2b')],'inReplyTo':_0x50cffa[_0x7d51('0x2c')],'sentAt':moment(_0x50cffa['date'])[_0x7d51('0x21')]('YYYY-MM-DD\x20HH:mm:ss'),'attach':![],'attachments':[]}};if(!_0x50cffa[_0x7d51('0x11')]&&!_0x50cffa[_0x7d51('0x14')]){var _0xae2f0a=_[_0x7d51('0x2d')](_0x50cffa[_0x7d51('0x1d')],{'contentType':'application/ms-tnef'});if(_0xae2f0a[_0x7d51('0xb')]){logger['info'](util['format']('Email\x20message\x20%s\x20is\x20in\x20TNEF\x20format:\x20message\x20content\x20will\x20be\x20converted\x20by\x20the\x20system',_0x50cffa[_0x7d51('0x2b')]));tnef['parseBuffer'](_[_0x7d51('0x2e')](_0xae2f0a)[_0x7d51('0x27')],function(_0x86a992,_0x309656){if(_0x309656){_0x50cffa['html']=_0x309656[_0x7d51('0x2f')]||_0x7d51('0x30');if(_0x309656['Attachments']){for(var _0x47e49e=0x0;_0x47e49e<_0x309656[_0x7d51('0x31')]['length'];_0x47e49e++){var _0x4a90dc=_0x309656[_0x7d51('0x31')][_0x47e49e];_0xd763c1[_0x7d51('0x17')][_0x7d51('0x1d')][_0x7d51('0x32')]({'filename':_0x4a90dc['Title'],'content':new Buffer(_0x4a90dc['Data'])});}}}_0x41742c(parseEmail(_0x2d8809,_0x50cffa,_0xd763c1));});}else{_0x50cffa[_0x7d51('0x11')]='<p>Empty\x20message</p>';_0x41742c(parseEmail(_0x2d8809,_0x50cffa,_0xd763c1));}}else{_0x41742c(parseEmail(_0x2d8809,_0x50cffa,_0xd763c1));}};exports[_0x7d51('0x33')]=function(_0x2bad44){switch(_0x2bad44){case _0x7d51('0x34'):return _0x7d51('0x35');case _0x7d51('0x36'):return _0x7d51('0x37');case'hotmail':return _0x7d51('0x38');case _0x7d51('0x39'):return _0x7d51('0x3a');}return'127.0.0.1';};