Built motion from commit 775e2a94.|2.6.3
[motion2.git] / server / services / email / lib / email.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 _0xd3b7=['end','handleImapEnd','error','handleImapError','disabled','messageStatus','connected','messages','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName','notify','then','Mail\x20accepted\x20from\x20routing','Mail\x20not\x20accepted\x20from\x20routing\x20%s','sendMail','send','Smtp','lastSync','verify','verified','exports','lodash','moment','ioredis','../../../config/logger','email','redis','defaults','localhost','socket.io-emitter','./smtp','../rpc/mailAccount','info','mailAccounts','imapAccounts','smtp','prototype','emit','name','key','active','Imap','status','total','isNil','stringify','md5','mailAccount:update','refresh','hasOwnProperty','service','provider','handleImapBox','bind','handleImapEmail'];(function(_0x5aca71,_0x583dfa){var _0x375f79=function(_0x3b2d5e){while(--_0x3b2d5e){_0x5aca71['push'](_0x5aca71['shift']());}};_0x375f79(++_0x583dfa);}(_0xd3b7,0x166));var _0x7d3b=function(_0x3432a8,_0x1f76f5){_0x3432a8=_0x3432a8-0x0;var _0x20c0ec=_0xd3b7[_0x3432a8];return _0x20c0ec;};'use strict';var _=require(_0x7d3b('0x0'));var moment=require(_0x7d3b('0x1'));var BPromise=require('bluebird');var md5=require('md5');var Redis=require(_0x7d3b('0x2'));var config=require('../../../config/environment');var logger=require(_0x7d3b('0x3'))(_0x7d3b('0x4'));config[_0x7d3b('0x5')]=_[_0x7d3b('0x6')](config['redis'],{'host':_0x7d3b('0x7'),'port':0x18eb});var io=require(_0x7d3b('0x8'))(new Redis(config[_0x7d3b('0x5')]));var EmailImap=require('./imap');var EmailSmtp=require(_0x7d3b('0x9'));var mailAccount=require(_0x7d3b('0xa'));var md5Accounts={};function EmailChannel(_0x283671){logger[_0x7d3b('0xb')]('start\x20email\x20channel');this[_0x7d3b('0xc')]=_0x283671[_0x7d3b('0xc')];this[_0x7d3b('0xd')]={};this[_0x7d3b('0xe')]=new EmailSmtp();}EmailChannel[_0x7d3b('0xf')][_0x7d3b('0x10')]=function(_0x578c08){var _0x173bf9=this['mailAccounts'][_0x578c08];var _0x24eb7d={'id':_0x173bf9['id'],'name':_0x173bf9[_0x7d3b('0x11')],'key':_0x173bf9[_0x7d3b('0x12')],'active':_0x173bf9[_0x7d3b('0x13')],'Imap':{'status':_0x173bf9[_0x7d3b('0x14')][_0x7d3b('0x15')],'messageStatus':_0x173bf9[_0x7d3b('0x14')]['messageStatus'],'total':_0x173bf9[_0x7d3b('0x14')][_0x7d3b('0x16')]}};if(_[_0x7d3b('0x17')](md5Accounts[_0x578c08])){md5Accounts[_0x578c08]={};}var _0x17beeb=md5(JSON[_0x7d3b('0x18')](_0x24eb7d));if(md5Accounts[_0x578c08][_0x7d3b('0x19')]!==_0x17beeb){md5Accounts[_0x578c08]['md5']=_0x17beeb;io[_0x7d3b('0x10')](_0x7d3b('0x1a'),_0x24eb7d);}};EmailChannel['prototype'][_0x7d3b('0x1b')]=function(_0x3924f0){for(var _0x145e14 in _0x3924f0){if(_0x3924f0[_0x7d3b('0x1c')](_0x145e14)){if(_0x3924f0[_0x145e14]['Imap']&&_0x3924f0[_0x145e14][_0x7d3b('0x13')]){var _0x2287dd=_0x3924f0[_0x145e14][_0x7d3b('0x14')];if(!_0x2287dd[_0x7d3b('0x1d')]&&_0x3924f0[_0x145e14][_0x7d3b('0x1e')]!=='custom')_0x2287dd['service']=_0x3924f0[_0x145e14][_0x7d3b('0x1e')];if(!this['imapAccounts'][_0x7d3b('0x1c')](_0x2287dd['id'])){this[_0x7d3b('0xd')][_0x2287dd['id']]=new EmailImap(_0x2287dd,_0x3924f0[_0x145e14][_0x7d3b('0x4')]);this[_0x7d3b('0xd')][_0x2287dd['id']]['on']('box',this[_0x7d3b('0x1f')][_0x7d3b('0x20')](this,_0x145e14));this['imapAccounts'][_0x2287dd['id']]['on'](_0x7d3b('0x4'),this[_0x7d3b('0x21')][_0x7d3b('0x20')](this,_0x145e14));this[_0x7d3b('0xd')][_0x2287dd['id']]['on'](_0x7d3b('0x22'),this[_0x7d3b('0x23')][_0x7d3b('0x20')](this,_0x145e14));this[_0x7d3b('0xd')][_0x2287dd['id']]['on'](_0x7d3b('0x24'),this[_0x7d3b('0x25')]['bind'](this,_0x145e14));}}else{if(_[_0x7d3b('0x17')](this[_0x7d3b('0xc')][_0x145e14][_0x7d3b('0x14')])){this[_0x7d3b('0xc')][_0x145e14]['Imap']={};}this[_0x7d3b('0xc')][_0x145e14]['Imap']['status']=_0x7d3b('0x26');this['mailAccounts'][_0x145e14][_0x7d3b('0x14')][_0x7d3b('0x27')]='';this[_0x7d3b('0x10')](_0x145e14);}}}};EmailChannel[_0x7d3b('0xf')][_0x7d3b('0x1f')]=function(_0x27a52b,_0x47468a){if(this[_0x7d3b('0xc')][_0x27a52b]){this[_0x7d3b('0xc')][_0x27a52b][_0x7d3b('0x14')][_0x7d3b('0x15')]=_0x7d3b('0x28');this[_0x7d3b('0xc')][_0x27a52b][_0x7d3b('0x14')][_0x7d3b('0x27')]=_0x47468a[_0x7d3b('0x11')];if(_0x47468a[_0x7d3b('0x29')]){this[_0x7d3b('0xc')][_0x27a52b]['Imap']['total']=_0x47468a[_0x7d3b('0x29')][_0x7d3b('0x16')];}return this[_0x7d3b('0x10')](_0x27a52b);}};EmailChannel[_0x7d3b('0xf')][_0x7d3b('0x21')]=function(_0x279950,_0x59444e){logger[_0x7d3b('0xb')](_0x7d3b('0x2a'),_0x59444e[_0x7d3b('0x2b')],_0x59444e[_0x7d3b('0x2c')],_0x59444e['from'],_0x279950);if(this[_0x7d3b('0xc')][_0x279950]){return mailAccount[_0x7d3b('0x2d')](_0x279950,_0x59444e)[_0x7d3b('0x2e')](function(_0x70313){if(_0x70313){logger[_0x7d3b('0xb')](_0x7d3b('0x2f'));}})['catch'](function(_0x5780ab){logger['error'](_0x7d3b('0x30'),JSON['stringify'](_0x5780ab));});}};EmailChannel[_0x7d3b('0xf')][_0x7d3b('0x23')]=function(_0x936e01){if(this[_0x7d3b('0xc')][_0x936e01]&&this[_0x7d3b('0xc')][_0x936e01][_0x7d3b('0x14')]){if(this['imapAccounts'][this['mailAccounts'][_0x936e01]['Imap']['id']]){delete this[_0x7d3b('0xd')][this[_0x7d3b('0xc')][_0x936e01][_0x7d3b('0x14')]['id']];}this[_0x7d3b('0xc')][_0x936e01][_0x7d3b('0x14')]['lastSync']=moment();this[_0x7d3b('0x10')](_0x936e01);}};EmailChannel[_0x7d3b('0xf')][_0x7d3b('0x25')]=function(_0x1d97e1,_0x365238){if(this[_0x7d3b('0xc')][_0x1d97e1]){this[_0x7d3b('0xc')][_0x1d97e1][_0x7d3b('0x14')][_0x7d3b('0x15')]=_0x7d3b('0x24');this[_0x7d3b('0xc')][_0x1d97e1][_0x7d3b('0x14')][_0x7d3b('0x27')]=_0x365238;return this[_0x7d3b('0x10')](_0x1d97e1);}};EmailChannel[_0x7d3b('0xf')][_0x7d3b('0x31')]=function(_0xf47d56,_0x3f3f7e){return this['smtp'][_0x7d3b('0x32')](_0xf47d56,_0x3f3f7e);};EmailChannel[_0x7d3b('0xf')]['verifySmtp']=function(_0x5bb37){var _0x30b0d9=this;if(this['mailAccounts'][_0x5bb37['id']]){if(_['isNil'](this[_0x7d3b('0xc')][_0x5bb37['id']][_0x7d3b('0x33')])){this['mailAccounts'][_0x5bb37['id']][_0x7d3b('0x33')]={};}this[_0x7d3b('0xc')][_0x5bb37['id']]['Smtp'][_0x7d3b('0x34')]=moment();return new BPromise(function(_0x5ea6b7,_0x48420c){return _0x30b0d9[_0x7d3b('0xe')][_0x7d3b('0x35')](_0x5bb37)['then'](function(_0x5cf1a5){_0x30b0d9[_0x7d3b('0xc')][_0x5bb37['id']][_0x7d3b('0x33')][_0x7d3b('0x15')]=_0x7d3b('0x36');_0x30b0d9['mailAccounts'][_0x5bb37['id']]['Smtp']['messageStatus']='';_0x30b0d9[_0x7d3b('0x10')](_0x5bb37['id']);_0x5ea6b7(_0x5cf1a5);})['catch'](function(_0x327d7c){_0x30b0d9['mailAccounts'][_0x5bb37['id']]['Smtp'][_0x7d3b('0x15')]=_0x7d3b('0x24');_0x30b0d9['mailAccounts'][_0x5bb37['id']][_0x7d3b('0x33')][_0x7d3b('0x27')]=_0x327d7c;_0x30b0d9[_0x7d3b('0x10')](_0x5bb37['id']);_0x48420c(_0x327d7c);});});}return this[_0x7d3b('0xe')][_0x7d3b('0x35')](_0x5bb37);};module[_0x7d3b('0x37')]=EmailChannel;