df841393e34d697217df42b8149f3bc5ef3dae60
[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 _0x408b=['mailAccount:update','refresh','hasOwnProperty','active','service','custom','provider','box','handleImapBox','bind','handleImapEmail','end','handleImapEnd','error','handleImapError','connected','messages','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName','from','notify','catch','Mail\x20not\x20accepted\x20from\x20routing\x20%s','sendMail','send','verifySmtp','Smtp','lastSync','verify','then','verified','exports','lodash','moment','bluebird','ioredis','../../../config/environment','../../../config/logger','email','redis','defaults','socket.io-emitter','./imap','./smtp','../rpc/mailAccount','info','mailAccounts','imapAccounts','smtp','prototype','name','Imap','status','messageStatus','total','isNil','stringify','md5','emit'];(function(_0x167cc1,_0x56ef5a){var _0x10bc4b=function(_0xd6a255){while(--_0xd6a255){_0x167cc1['push'](_0x167cc1['shift']());}};_0x10bc4b(++_0x56ef5a);}(_0x408b,0x99));var _0xb408=function(_0x122096,_0x60c295){_0x122096=_0x122096-0x0;var _0x3d1966=_0x408b[_0x122096];return _0x3d1966;};'use strict';var _=require(_0xb408('0x0'));var moment=require(_0xb408('0x1'));var BPromise=require(_0xb408('0x2'));var md5=require('md5');var Redis=require(_0xb408('0x3'));var config=require(_0xb408('0x4'));var logger=require(_0xb408('0x5'))(_0xb408('0x6'));config[_0xb408('0x7')]=_[_0xb408('0x8')](config[_0xb408('0x7')],{'host':'localhost','port':0x18eb});var io=require(_0xb408('0x9'))(new Redis(config[_0xb408('0x7')]));var EmailImap=require(_0xb408('0xa'));var EmailSmtp=require(_0xb408('0xb'));var mailAccount=require(_0xb408('0xc'));var md5Accounts={};function EmailChannel(_0x3d6e8a){logger[_0xb408('0xd')]('start\x20email\x20channel');this[_0xb408('0xe')]=_0x3d6e8a['mailAccounts'];this[_0xb408('0xf')]={};this[_0xb408('0x10')]=new EmailSmtp();}EmailChannel[_0xb408('0x11')]['emit']=function(_0x39f48c){var _0x25da5d=this[_0xb408('0xe')][_0x39f48c];var _0x270b2f={'id':_0x25da5d['id'],'name':_0x25da5d[_0xb408('0x12')],'key':_0x25da5d['key'],'active':_0x25da5d['active'],'Imap':{'status':_0x25da5d[_0xb408('0x13')][_0xb408('0x14')],'messageStatus':_0x25da5d[_0xb408('0x13')][_0xb408('0x15')],'total':_0x25da5d[_0xb408('0x13')][_0xb408('0x16')]}};if(_[_0xb408('0x17')](md5Accounts[_0x39f48c])){md5Accounts[_0x39f48c]={};}var _0x142097=md5(JSON[_0xb408('0x18')](_0x270b2f));if(md5Accounts[_0x39f48c][_0xb408('0x19')]!==_0x142097){md5Accounts[_0x39f48c][_0xb408('0x19')]=_0x142097;io[_0xb408('0x1a')](_0xb408('0x1b'),_0x270b2f);}};EmailChannel[_0xb408('0x11')][_0xb408('0x1c')]=function(_0x3b911d){for(var _0x1a2c79 in _0x3b911d){if(_0x3b911d[_0xb408('0x1d')](_0x1a2c79)){if(_0x3b911d[_0x1a2c79]['Imap']&&_0x3b911d[_0x1a2c79][_0xb408('0x1e')]){var _0x2b1462=_0x3b911d[_0x1a2c79][_0xb408('0x13')];if(!_0x2b1462[_0xb408('0x1f')]&&_0x3b911d[_0x1a2c79]['provider']!==_0xb408('0x20'))_0x2b1462['service']=_0x3b911d[_0x1a2c79][_0xb408('0x21')];if(!this[_0xb408('0xf')]['hasOwnProperty'](_0x2b1462['id'])){this[_0xb408('0xf')][_0x2b1462['id']]=new EmailImap(_0x2b1462,_0x3b911d[_0x1a2c79]['email']);this[_0xb408('0xf')][_0x2b1462['id']]['on'](_0xb408('0x22'),this[_0xb408('0x23')][_0xb408('0x24')](this,_0x1a2c79));this[_0xb408('0xf')][_0x2b1462['id']]['on'](_0xb408('0x6'),this[_0xb408('0x25')][_0xb408('0x24')](this,_0x1a2c79));this[_0xb408('0xf')][_0x2b1462['id']]['on'](_0xb408('0x26'),this[_0xb408('0x27')][_0xb408('0x24')](this,_0x1a2c79));this[_0xb408('0xf')][_0x2b1462['id']]['on'](_0xb408('0x28'),this[_0xb408('0x29')][_0xb408('0x24')](this,_0x1a2c79));}}else{if(_['isNil'](this['mailAccounts'][_0x1a2c79][_0xb408('0x13')])){this[_0xb408('0xe')][_0x1a2c79][_0xb408('0x13')]={};}this[_0xb408('0xe')][_0x1a2c79][_0xb408('0x13')]['status']='disabled';this[_0xb408('0xe')][_0x1a2c79]['Imap'][_0xb408('0x15')]='';this['emit'](_0x1a2c79);}}}};EmailChannel['prototype'][_0xb408('0x23')]=function(_0x382265,_0xb69b07){if(this[_0xb408('0xe')][_0x382265]){this[_0xb408('0xe')][_0x382265][_0xb408('0x13')]['status']=_0xb408('0x2a');this['mailAccounts'][_0x382265]['Imap'][_0xb408('0x15')]=_0xb69b07[_0xb408('0x12')];if(_0xb69b07[_0xb408('0x2b')]){this[_0xb408('0xe')][_0x382265][_0xb408('0x13')][_0xb408('0x16')]=_0xb69b07['messages'][_0xb408('0x16')];}return this[_0xb408('0x1a')](_0x382265);}};EmailChannel[_0xb408('0x11')][_0xb408('0x25')]=function(_0x162b6b,_0x2592d0){logger['info'](_0xb408('0x2c'),_0x2592d0[_0xb408('0x2d')],_0x2592d0[_0xb408('0x2e')],_0x2592d0[_0xb408('0x2f')],_0x162b6b);if(this[_0xb408('0xe')][_0x162b6b]){return mailAccount[_0xb408('0x30')](_0x162b6b,_0x2592d0)['then'](function(_0x44af56){if(_0x44af56){logger['info']('Mail\x20accepted\x20from\x20routing');}})[_0xb408('0x31')](function(_0x17e90f){logger[_0xb408('0x28')](_0xb408('0x32'),JSON[_0xb408('0x18')](_0x17e90f));});}};EmailChannel['prototype'][_0xb408('0x27')]=function(_0x485f5c){if(this[_0xb408('0xe')][_0x485f5c]&&this[_0xb408('0xe')][_0x485f5c][_0xb408('0x13')]){if(this[_0xb408('0xf')][this[_0xb408('0xe')][_0x485f5c][_0xb408('0x13')]['id']]){delete this[_0xb408('0xf')][this['mailAccounts'][_0x485f5c][_0xb408('0x13')]['id']];}this[_0xb408('0xe')][_0x485f5c][_0xb408('0x13')]['lastSync']=moment();this[_0xb408('0x1a')](_0x485f5c);}};EmailChannel[_0xb408('0x11')]['handleImapError']=function(_0x2b0004,_0x1eece8){if(this[_0xb408('0xe')][_0x2b0004]){this['mailAccounts'][_0x2b0004][_0xb408('0x13')][_0xb408('0x14')]=_0xb408('0x28');this[_0xb408('0xe')][_0x2b0004][_0xb408('0x13')][_0xb408('0x15')]=_0x1eece8;return this['emit'](_0x2b0004);}};EmailChannel[_0xb408('0x11')][_0xb408('0x33')]=function(_0x41606b,_0x17e1a8){return this['smtp'][_0xb408('0x34')](_0x41606b,_0x17e1a8);};EmailChannel[_0xb408('0x11')][_0xb408('0x35')]=function(_0x5d2257){var _0x3058c9=this;if(this[_0xb408('0xe')][_0x5d2257['id']]){if(_[_0xb408('0x17')](this[_0xb408('0xe')][_0x5d2257['id']][_0xb408('0x36')])){this[_0xb408('0xe')][_0x5d2257['id']][_0xb408('0x36')]={};}this[_0xb408('0xe')][_0x5d2257['id']][_0xb408('0x36')][_0xb408('0x37')]=moment();return new BPromise(function(_0x138481,_0xcfc682){return _0x3058c9[_0xb408('0x10')][_0xb408('0x38')](_0x5d2257)[_0xb408('0x39')](function(_0x5290c1){_0x3058c9['mailAccounts'][_0x5d2257['id']]['Smtp'][_0xb408('0x14')]=_0xb408('0x3a');_0x3058c9[_0xb408('0xe')][_0x5d2257['id']]['Smtp'][_0xb408('0x15')]='';_0x3058c9['emit'](_0x5d2257['id']);_0x138481(_0x5290c1);})[_0xb408('0x31')](function(_0x38eea0){_0x3058c9['mailAccounts'][_0x5d2257['id']][_0xb408('0x36')]['status']='error';_0x3058c9[_0xb408('0xe')][_0x5d2257['id']]['Smtp'][_0xb408('0x15')]=_0x38eea0;_0x3058c9[_0xb408('0x1a')](_0x5d2257['id']);_0xcfc682(_0x38eea0);});});}return this[_0xb408('0x10')][_0xb408('0x38')](_0x5d2257);};module[_0xb408('0x3b')]=EmailChannel;