d05122bb7619995d0e4b0effe7003c50bd7a7273
[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 _0xaf2f=['firstName','notify','then','Mail\x20accepted\x20from\x20routing','catch','Mail\x20not\x20accepted\x20from\x20routing\x20%s','lastSync','verifySmtp','Smtp','verified','exports','lodash','moment','md5','../../../config/environment','../../../config/logger','defaults','redis','localhost','socket.io-emitter','./imap','./smtp','../rpc/mailAccount','info','start\x20email\x20channel','mailAccounts','imapAccounts','smtp','prototype','name','key','Imap','total','isNil','stringify','emit','mailAccount:update','refresh','hasOwnProperty','active','service','custom','provider','email','box','handleImapBox','bind','handleImapEmail','handleImapEnd','error','handleImapError','status','messageStatus','messages'];(function(_0x522a29,_0x431f08){var _0x4d46e7=function(_0x1e1046){while(--_0x1e1046){_0x522a29['push'](_0x522a29['shift']());}};_0x4d46e7(++_0x431f08);}(_0xaf2f,0x1bb));var _0xfaf2=function(_0x9de18b,_0x3a013a){_0x9de18b=_0x9de18b-0x0;var _0x44f802=_0xaf2f[_0x9de18b];return _0x44f802;};'use strict';var _=require(_0xfaf2('0x0'));var moment=require(_0xfaf2('0x1'));var BPromise=require('bluebird');var md5=require(_0xfaf2('0x2'));var Redis=require('ioredis');var config=require(_0xfaf2('0x3'));var logger=require(_0xfaf2('0x4'))('email');config['redis']=_[_0xfaf2('0x5')](config[_0xfaf2('0x6')],{'host':_0xfaf2('0x7'),'port':0x18eb});var io=require(_0xfaf2('0x8'))(new Redis(config['redis']));var EmailImap=require(_0xfaf2('0x9'));var EmailSmtp=require(_0xfaf2('0xa'));var mailAccount=require(_0xfaf2('0xb'));var md5Accounts={};function EmailChannel(_0x5e5895){logger[_0xfaf2('0xc')](_0xfaf2('0xd'));this[_0xfaf2('0xe')]=_0x5e5895[_0xfaf2('0xe')];this[_0xfaf2('0xf')]={};this[_0xfaf2('0x10')]=new EmailSmtp();}EmailChannel[_0xfaf2('0x11')]['emit']=function(_0x11fe19){var _0x3c395f=this[_0xfaf2('0xe')][_0x11fe19];var _0x32b661={'id':_0x3c395f['id'],'name':_0x3c395f[_0xfaf2('0x12')],'key':_0x3c395f[_0xfaf2('0x13')],'active':_0x3c395f['active'],'Imap':{'status':_0x3c395f[_0xfaf2('0x14')]['status'],'messageStatus':_0x3c395f[_0xfaf2('0x14')]['messageStatus'],'total':_0x3c395f['Imap'][_0xfaf2('0x15')]}};if(_[_0xfaf2('0x16')](md5Accounts[_0x11fe19])){md5Accounts[_0x11fe19]={};}var _0x17dada=md5(JSON[_0xfaf2('0x17')](_0x32b661));if(md5Accounts[_0x11fe19][_0xfaf2('0x2')]!==_0x17dada){md5Accounts[_0x11fe19][_0xfaf2('0x2')]=_0x17dada;io[_0xfaf2('0x18')](_0xfaf2('0x19'),_0x32b661);}};EmailChannel[_0xfaf2('0x11')][_0xfaf2('0x1a')]=function(_0x54c963){for(var _0x45b489 in _0x54c963){if(_0x54c963[_0xfaf2('0x1b')](_0x45b489)){if(_0x54c963[_0x45b489][_0xfaf2('0x14')]&&_0x54c963[_0x45b489][_0xfaf2('0x1c')]){var _0xcfad75=_0x54c963[_0x45b489]['Imap'];if(!_0xcfad75[_0xfaf2('0x1d')]&&_0x54c963[_0x45b489]['provider']!==_0xfaf2('0x1e'))_0xcfad75[_0xfaf2('0x1d')]=_0x54c963[_0x45b489][_0xfaf2('0x1f')];if(!this['imapAccounts'][_0xfaf2('0x1b')](_0xcfad75['id'])){this[_0xfaf2('0xf')][_0xcfad75['id']]=new EmailImap(_0xcfad75,_0x54c963[_0x45b489][_0xfaf2('0x20')]);this['imapAccounts'][_0xcfad75['id']]['on'](_0xfaf2('0x21'),this[_0xfaf2('0x22')][_0xfaf2('0x23')](this,_0x45b489));this[_0xfaf2('0xf')][_0xcfad75['id']]['on'](_0xfaf2('0x20'),this[_0xfaf2('0x24')][_0xfaf2('0x23')](this,_0x45b489));this[_0xfaf2('0xf')][_0xcfad75['id']]['on']('end',this[_0xfaf2('0x25')]['bind'](this,_0x45b489));this['imapAccounts'][_0xcfad75['id']]['on'](_0xfaf2('0x26'),this[_0xfaf2('0x27')]['bind'](this,_0x45b489));}}else{if(_['isNil'](this['mailAccounts'][_0x45b489][_0xfaf2('0x14')])){this[_0xfaf2('0xe')][_0x45b489][_0xfaf2('0x14')]={};}this[_0xfaf2('0xe')][_0x45b489][_0xfaf2('0x14')][_0xfaf2('0x28')]='disabled';this['mailAccounts'][_0x45b489]['Imap'][_0xfaf2('0x29')]='';this[_0xfaf2('0x18')](_0x45b489);}}}};EmailChannel[_0xfaf2('0x11')][_0xfaf2('0x22')]=function(_0x82267d,_0x3df045){if(this['mailAccounts'][_0x82267d]){this[_0xfaf2('0xe')][_0x82267d]['Imap'][_0xfaf2('0x28')]='connected';this['mailAccounts'][_0x82267d][_0xfaf2('0x14')][_0xfaf2('0x29')]=_0x3df045[_0xfaf2('0x12')];if(_0x3df045[_0xfaf2('0x2a')]){this[_0xfaf2('0xe')][_0x82267d][_0xfaf2('0x14')]['total']=_0x3df045[_0xfaf2('0x2a')][_0xfaf2('0x15')];}return this['emit'](_0x82267d);}};EmailChannel[_0xfaf2('0x11')][_0xfaf2('0x24')]=function(_0x1a1dbd,_0x17e995){logger[_0xfaf2('0xc')]('New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s',_0x17e995[_0xfaf2('0x2b')],_0x17e995['lastName'],_0x17e995['from'],_0x1a1dbd);if(this[_0xfaf2('0xe')][_0x1a1dbd]){return mailAccount[_0xfaf2('0x2c')](_0x1a1dbd,_0x17e995)[_0xfaf2('0x2d')](function(_0x8e277d){if(_0x8e277d){logger['info'](_0xfaf2('0x2e'));}})[_0xfaf2('0x2f')](function(_0x52cf83){logger[_0xfaf2('0x26')](_0xfaf2('0x30'),JSON[_0xfaf2('0x17')](_0x52cf83));});}};EmailChannel[_0xfaf2('0x11')]['handleImapEnd']=function(_0xb89be2){if(this[_0xfaf2('0xe')][_0xb89be2]&&this[_0xfaf2('0xe')][_0xb89be2][_0xfaf2('0x14')]){if(this[_0xfaf2('0xf')][this['mailAccounts'][_0xb89be2]['Imap']['id']]){delete this['imapAccounts'][this[_0xfaf2('0xe')][_0xb89be2][_0xfaf2('0x14')]['id']];}this[_0xfaf2('0xe')][_0xb89be2][_0xfaf2('0x14')][_0xfaf2('0x31')]=moment();this[_0xfaf2('0x18')](_0xb89be2);}};EmailChannel[_0xfaf2('0x11')][_0xfaf2('0x27')]=function(_0x1a5848,_0x335699){if(this[_0xfaf2('0xe')][_0x1a5848]){this['mailAccounts'][_0x1a5848][_0xfaf2('0x14')][_0xfaf2('0x28')]=_0xfaf2('0x26');this[_0xfaf2('0xe')][_0x1a5848]['Imap'][_0xfaf2('0x29')]=_0x335699;return this[_0xfaf2('0x18')](_0x1a5848);}};EmailChannel[_0xfaf2('0x11')]['sendMail']=function(_0x205a40,_0xaeb489){return this[_0xfaf2('0x10')]['send'](_0x205a40,_0xaeb489);};EmailChannel[_0xfaf2('0x11')][_0xfaf2('0x32')]=function(_0x4ed7c4){var _0x2cee58=this;if(this[_0xfaf2('0xe')][_0x4ed7c4['id']]){if(_['isNil'](this[_0xfaf2('0xe')][_0x4ed7c4['id']]['Smtp'])){this[_0xfaf2('0xe')][_0x4ed7c4['id']][_0xfaf2('0x33')]={};}this[_0xfaf2('0xe')][_0x4ed7c4['id']]['Smtp']['lastSync']=moment();return new BPromise(function(_0x4b5032,_0x382480){return _0x2cee58['smtp']['verify'](_0x4ed7c4)[_0xfaf2('0x2d')](function(_0x2f8542){_0x2cee58[_0xfaf2('0xe')][_0x4ed7c4['id']]['Smtp'][_0xfaf2('0x28')]=_0xfaf2('0x34');_0x2cee58[_0xfaf2('0xe')][_0x4ed7c4['id']][_0xfaf2('0x33')][_0xfaf2('0x29')]='';_0x2cee58['emit'](_0x4ed7c4['id']);_0x4b5032(_0x2f8542);})[_0xfaf2('0x2f')](function(_0x2f0711){_0x2cee58[_0xfaf2('0xe')][_0x4ed7c4['id']][_0xfaf2('0x33')][_0xfaf2('0x28')]='error';_0x2cee58[_0xfaf2('0xe')][_0x4ed7c4['id']][_0xfaf2('0x33')][_0xfaf2('0x29')]=_0x2f0711;_0x2cee58[_0xfaf2('0x18')](_0x4ed7c4['id']);_0x382480(_0x2f0711);});});}return this[_0xfaf2('0x10')]['verify'](_0x4ed7c4);};module[_0xfaf2('0x35')]=EmailChannel;