e03068a32389f4f54150b477f27d246a151f5aae
[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 _0xa71e=['handleImapEmail','firstName','lastName','from','notify','then','catch','Mail\x20not\x20accepted\x20from\x20routing\x20%s','lastSync','error','send','verifySmtp','Smtp','verify','verified','exports','lodash','moment','bluebird','md5','ioredis','../../../config/environment','../../../config/logger','defaults','redis','localhost','socket.io-emitter','./imap','info','start\x20email\x20channel','mailAccounts','imapAccounts','smtp','prototype','emit','key','active','Imap','status','messageStatus','isNil','stringify','mailAccount:update','service','provider','custom','hasOwnProperty','email','box','handleImapBox','bind','end','handleImapEnd','handleImapError','disabled','connected','name','messages','total'];(function(_0x455959,_0x27e796){var _0x1e54c2=function(_0x30a0a7){while(--_0x30a0a7){_0x455959['push'](_0x455959['shift']());}};_0x1e54c2(++_0x27e796);}(_0xa71e,0x1e8));var _0xea71=function(_0x17823f,_0xffd807){_0x17823f=_0x17823f-0x0;var _0x51fc21=_0xa71e[_0x17823f];return _0x51fc21;};'use strict';var _=require(_0xea71('0x0'));var moment=require(_0xea71('0x1'));var BPromise=require(_0xea71('0x2'));var md5=require(_0xea71('0x3'));var Redis=require(_0xea71('0x4'));var config=require(_0xea71('0x5'));var logger=require(_0xea71('0x6'))('email');config['redis']=_[_0xea71('0x7')](config[_0xea71('0x8')],{'host':_0xea71('0x9'),'port':0x18eb});var io=require(_0xea71('0xa'))(new Redis(config[_0xea71('0x8')]));var EmailImap=require(_0xea71('0xb'));var EmailSmtp=require('./smtp');var mailAccount=require('../rpc/mailAccount');var md5Accounts={};function EmailChannel(_0x41fcfa){logger[_0xea71('0xc')](_0xea71('0xd'));this[_0xea71('0xe')]=_0x41fcfa[_0xea71('0xe')];this[_0xea71('0xf')]={};this[_0xea71('0x10')]=new EmailSmtp();}EmailChannel[_0xea71('0x11')][_0xea71('0x12')]=function(_0x56cd97){var _0x179070=this[_0xea71('0xe')][_0x56cd97];var _0x1fb309={'id':_0x179070['id'],'name':_0x179070['name'],'key':_0x179070[_0xea71('0x13')],'active':_0x179070[_0xea71('0x14')],'Imap':{'status':_0x179070[_0xea71('0x15')][_0xea71('0x16')],'messageStatus':_0x179070[_0xea71('0x15')][_0xea71('0x17')],'total':_0x179070[_0xea71('0x15')]['total']}};if(_[_0xea71('0x18')](md5Accounts[_0x56cd97])){md5Accounts[_0x56cd97]={};}var _0x1a7599=md5(JSON[_0xea71('0x19')](_0x1fb309));if(md5Accounts[_0x56cd97]['md5']!==_0x1a7599){md5Accounts[_0x56cd97]['md5']=_0x1a7599;io[_0xea71('0x12')](_0xea71('0x1a'),_0x1fb309);}};EmailChannel[_0xea71('0x11')]['refresh']=function(_0x3a0596){for(var _0x4ed2c1 in _0x3a0596){if(_0x3a0596['hasOwnProperty'](_0x4ed2c1)){if(_0x3a0596[_0x4ed2c1][_0xea71('0x15')]&&_0x3a0596[_0x4ed2c1][_0xea71('0x14')]){var _0x5a9004=_0x3a0596[_0x4ed2c1][_0xea71('0x15')];if(!_0x5a9004[_0xea71('0x1b')]&&_0x3a0596[_0x4ed2c1][_0xea71('0x1c')]!==_0xea71('0x1d'))_0x5a9004[_0xea71('0x1b')]=_0x3a0596[_0x4ed2c1]['provider'];if(!this[_0xea71('0xf')][_0xea71('0x1e')](_0x5a9004['id'])){this['imapAccounts'][_0x5a9004['id']]=new EmailImap(_0x5a9004,_0x3a0596[_0x4ed2c1][_0xea71('0x1f')]);this[_0xea71('0xf')][_0x5a9004['id']]['on'](_0xea71('0x20'),this[_0xea71('0x21')][_0xea71('0x22')](this,_0x4ed2c1));this['imapAccounts'][_0x5a9004['id']]['on'](_0xea71('0x1f'),this['handleImapEmail']['bind'](this,_0x4ed2c1));this[_0xea71('0xf')][_0x5a9004['id']]['on'](_0xea71('0x23'),this[_0xea71('0x24')][_0xea71('0x22')](this,_0x4ed2c1));this[_0xea71('0xf')][_0x5a9004['id']]['on']('error',this[_0xea71('0x25')][_0xea71('0x22')](this,_0x4ed2c1));}}else{if(_['isNil'](this[_0xea71('0xe')][_0x4ed2c1]['Imap'])){this[_0xea71('0xe')][_0x4ed2c1][_0xea71('0x15')]={};}this[_0xea71('0xe')][_0x4ed2c1]['Imap'][_0xea71('0x16')]=_0xea71('0x26');this[_0xea71('0xe')][_0x4ed2c1][_0xea71('0x15')]['messageStatus']='';this[_0xea71('0x12')](_0x4ed2c1);}}}};EmailChannel['prototype']['handleImapBox']=function(_0x401119,_0x25558e){if(this[_0xea71('0xe')][_0x401119]){this[_0xea71('0xe')][_0x401119][_0xea71('0x15')][_0xea71('0x16')]=_0xea71('0x27');this[_0xea71('0xe')][_0x401119][_0xea71('0x15')][_0xea71('0x17')]=_0x25558e[_0xea71('0x28')];if(_0x25558e[_0xea71('0x29')]){this['mailAccounts'][_0x401119][_0xea71('0x15')][_0xea71('0x2a')]=_0x25558e['messages'][_0xea71('0x2a')];}return this[_0xea71('0x12')](_0x401119);}};EmailChannel[_0xea71('0x11')][_0xea71('0x2b')]=function(_0x45a13a,_0x4c6d48){logger[_0xea71('0xc')]('New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s',_0x4c6d48[_0xea71('0x2c')],_0x4c6d48[_0xea71('0x2d')],_0x4c6d48[_0xea71('0x2e')],_0x45a13a);if(this['mailAccounts'][_0x45a13a]){return mailAccount[_0xea71('0x2f')](_0x45a13a,_0x4c6d48)[_0xea71('0x30')](function(_0x2973c5){if(_0x2973c5){logger[_0xea71('0xc')]('Mail\x20accepted\x20from\x20routing');}})[_0xea71('0x31')](function(_0x36eeb1){logger['error'](_0xea71('0x32'),JSON[_0xea71('0x19')](_0x36eeb1));});}};EmailChannel[_0xea71('0x11')]['handleImapEnd']=function(_0x9271bb){if(this[_0xea71('0xe')][_0x9271bb]&&this[_0xea71('0xe')][_0x9271bb][_0xea71('0x15')]){if(this['imapAccounts'][this[_0xea71('0xe')][_0x9271bb]['Imap']['id']]){delete this[_0xea71('0xf')][this[_0xea71('0xe')][_0x9271bb][_0xea71('0x15')]['id']];}this[_0xea71('0xe')][_0x9271bb][_0xea71('0x15')][_0xea71('0x33')]=moment();this[_0xea71('0x12')](_0x9271bb);}};EmailChannel[_0xea71('0x11')][_0xea71('0x25')]=function(_0x3a9865,_0xbfd5ee){if(this[_0xea71('0xe')][_0x3a9865]){this[_0xea71('0xe')][_0x3a9865][_0xea71('0x15')][_0xea71('0x16')]=_0xea71('0x34');this[_0xea71('0xe')][_0x3a9865]['Imap'][_0xea71('0x17')]=_0xbfd5ee;return this['emit'](_0x3a9865);}};EmailChannel[_0xea71('0x11')]['sendMail']=function(_0xf8c91b,_0x29f902){return this['smtp'][_0xea71('0x35')](_0xf8c91b,_0x29f902);};EmailChannel[_0xea71('0x11')][_0xea71('0x36')]=function(_0x3356d1){var _0x4d1afc=this;if(this[_0xea71('0xe')][_0x3356d1['id']]){if(_[_0xea71('0x18')](this[_0xea71('0xe')][_0x3356d1['id']]['Smtp'])){this[_0xea71('0xe')][_0x3356d1['id']][_0xea71('0x37')]={};}this[_0xea71('0xe')][_0x3356d1['id']][_0xea71('0x37')][_0xea71('0x33')]=moment();return new BPromise(function(_0x326b46,_0x2f9500){return _0x4d1afc[_0xea71('0x10')][_0xea71('0x38')](_0x3356d1)[_0xea71('0x30')](function(_0x476dee){_0x4d1afc[_0xea71('0xe')][_0x3356d1['id']][_0xea71('0x37')][_0xea71('0x16')]=_0xea71('0x39');_0x4d1afc[_0xea71('0xe')][_0x3356d1['id']][_0xea71('0x37')]['messageStatus']='';_0x4d1afc[_0xea71('0x12')](_0x3356d1['id']);_0x326b46(_0x476dee);})[_0xea71('0x31')](function(_0x486861){_0x4d1afc['mailAccounts'][_0x3356d1['id']][_0xea71('0x37')][_0xea71('0x16')]=_0xea71('0x34');_0x4d1afc[_0xea71('0xe')][_0x3356d1['id']][_0xea71('0x37')][_0xea71('0x17')]=_0x486861;_0x4d1afc[_0xea71('0x12')](_0x3356d1['id']);_0x2f9500(_0x486861);});});}return this[_0xea71('0x10')][_0xea71('0x38')](_0x3356d1);};module[_0xea71('0x3a')]=EmailChannel;