556d511dde6fc55552eb1f9dea1937bad465f376
[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 _0xc8ec=['send','isNil','Smtp','verify','exports','lodash','moment','bluebird','../../../config/environment','../../../config/logger','email','redis','defaults','localhost','./imap','./smtp','../rpc/mailAccount','start\x20email\x20channel','mailAccounts','imapAccounts','smtp','prototype','emit','name','key','active','Imap','messageStatus','total','stringify','md5','mailAccount:update','refresh','hasOwnProperty','provider','custom','service','bind','handleImapEmail','end','handleImapEnd','handleImapError','disabled','handleImapBox','status','connected','messages','info','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName','notify','then','Mail\x20accepted\x20from\x20routing','catch','error','lastSync','sendMail'];(function(_0x397e5f,_0x454c8f){var _0x5302b8=function(_0x1a29ef){while(--_0x1a29ef){_0x397e5f['push'](_0x397e5f['shift']());}};_0x5302b8(++_0x454c8f);}(_0xc8ec,0x127));var _0xcc8e=function(_0x4b5c38,_0x44e4ca){_0x4b5c38=_0x4b5c38-0x0;var _0x522619=_0xc8ec[_0x4b5c38];return _0x522619;};'use strict';var _=require(_0xcc8e('0x0'));var moment=require(_0xcc8e('0x1'));var BPromise=require(_0xcc8e('0x2'));var md5=require('md5');var Redis=require('ioredis');var config=require(_0xcc8e('0x3'));var logger=require(_0xcc8e('0x4'))(_0xcc8e('0x5'));config[_0xcc8e('0x6')]=_[_0xcc8e('0x7')](config[_0xcc8e('0x6')],{'host':_0xcc8e('0x8'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0xcc8e('0x6')]));var EmailImap=require(_0xcc8e('0x9'));var EmailSmtp=require(_0xcc8e('0xa'));var mailAccount=require(_0xcc8e('0xb'));var md5Accounts={};function EmailChannel(_0x44cf39){logger['info'](_0xcc8e('0xc'));this[_0xcc8e('0xd')]=_0x44cf39[_0xcc8e('0xd')];this[_0xcc8e('0xe')]={};this[_0xcc8e('0xf')]=new EmailSmtp();}EmailChannel[_0xcc8e('0x10')][_0xcc8e('0x11')]=function(_0x2afc85){var _0x399579=this[_0xcc8e('0xd')][_0x2afc85];var _0x105033={'id':_0x399579['id'],'name':_0x399579[_0xcc8e('0x12')],'key':_0x399579[_0xcc8e('0x13')],'active':_0x399579[_0xcc8e('0x14')],'Imap':{'status':_0x399579[_0xcc8e('0x15')]['status'],'messageStatus':_0x399579[_0xcc8e('0x15')][_0xcc8e('0x16')],'total':_0x399579[_0xcc8e('0x15')][_0xcc8e('0x17')]}};if(_['isNil'](md5Accounts[_0x2afc85])){md5Accounts[_0x2afc85]={};}var _0x14d76c=md5(JSON[_0xcc8e('0x18')](_0x105033));if(md5Accounts[_0x2afc85]['md5']!==_0x14d76c){md5Accounts[_0x2afc85][_0xcc8e('0x19')]=_0x14d76c;io['emit'](_0xcc8e('0x1a'),_0x105033);}};EmailChannel[_0xcc8e('0x10')][_0xcc8e('0x1b')]=function(_0x17b980){for(var _0x52f27f in _0x17b980){if(_0x17b980[_0xcc8e('0x1c')](_0x52f27f)){if(_0x17b980[_0x52f27f][_0xcc8e('0x15')]&&_0x17b980[_0x52f27f][_0xcc8e('0x14')]){var _0x1488f3=_0x17b980[_0x52f27f]['Imap'];if(!_0x1488f3['service']&&_0x17b980[_0x52f27f][_0xcc8e('0x1d')]!==_0xcc8e('0x1e'))_0x1488f3[_0xcc8e('0x1f')]=_0x17b980[_0x52f27f][_0xcc8e('0x1d')];if(!this[_0xcc8e('0xe')]['hasOwnProperty'](_0x1488f3['id'])){this[_0xcc8e('0xe')][_0x1488f3['id']]=new EmailImap(_0x1488f3,_0x17b980[_0x52f27f]['email']);this[_0xcc8e('0xe')][_0x1488f3['id']]['on']('box',this['handleImapBox'][_0xcc8e('0x20')](this,_0x52f27f));this['imapAccounts'][_0x1488f3['id']]['on']('email',this[_0xcc8e('0x21')][_0xcc8e('0x20')](this,_0x52f27f));this[_0xcc8e('0xe')][_0x1488f3['id']]['on'](_0xcc8e('0x22'),this[_0xcc8e('0x23')]['bind'](this,_0x52f27f));this[_0xcc8e('0xe')][_0x1488f3['id']]['on']('error',this[_0xcc8e('0x24')]['bind'](this,_0x52f27f));}}else{if(_['isNil'](this[_0xcc8e('0xd')][_0x52f27f][_0xcc8e('0x15')])){this[_0xcc8e('0xd')][_0x52f27f][_0xcc8e('0x15')]={};}this[_0xcc8e('0xd')][_0x52f27f][_0xcc8e('0x15')]['status']=_0xcc8e('0x25');this[_0xcc8e('0xd')][_0x52f27f][_0xcc8e('0x15')]['messageStatus']='';this[_0xcc8e('0x11')](_0x52f27f);}}}};EmailChannel[_0xcc8e('0x10')][_0xcc8e('0x26')]=function(_0x23704c,_0x3905e8){if(this[_0xcc8e('0xd')][_0x23704c]){this[_0xcc8e('0xd')][_0x23704c]['Imap'][_0xcc8e('0x27')]=_0xcc8e('0x28');this[_0xcc8e('0xd')][_0x23704c]['Imap'][_0xcc8e('0x16')]=_0x3905e8[_0xcc8e('0x12')];if(_0x3905e8[_0xcc8e('0x29')]){this[_0xcc8e('0xd')][_0x23704c]['Imap'][_0xcc8e('0x17')]=_0x3905e8[_0xcc8e('0x29')][_0xcc8e('0x17')];}return this[_0xcc8e('0x11')](_0x23704c);}};EmailChannel[_0xcc8e('0x10')][_0xcc8e('0x21')]=function(_0x31c8d1,_0x520be6){logger[_0xcc8e('0x2a')](_0xcc8e('0x2b'),_0x520be6[_0xcc8e('0x2c')],_0x520be6[_0xcc8e('0x2d')],_0x520be6['from'],_0x31c8d1);if(this[_0xcc8e('0xd')][_0x31c8d1]){return mailAccount[_0xcc8e('0x2e')](_0x31c8d1,_0x520be6)[_0xcc8e('0x2f')](function(_0x227e43){if(_0x227e43){logger[_0xcc8e('0x2a')](_0xcc8e('0x30'));}})[_0xcc8e('0x31')](function(_0x3322fe){logger[_0xcc8e('0x32')]('Mail\x20not\x20accepted\x20from\x20routing\x20%s',JSON[_0xcc8e('0x18')](_0x3322fe));});}};EmailChannel[_0xcc8e('0x10')]['handleImapEnd']=function(_0xa0926d){if(this[_0xcc8e('0xd')][_0xa0926d]&&this[_0xcc8e('0xd')][_0xa0926d][_0xcc8e('0x15')]){if(this[_0xcc8e('0xe')][this[_0xcc8e('0xd')][_0xa0926d][_0xcc8e('0x15')]['id']]){delete this[_0xcc8e('0xe')][this[_0xcc8e('0xd')][_0xa0926d][_0xcc8e('0x15')]['id']];}this['mailAccounts'][_0xa0926d][_0xcc8e('0x15')][_0xcc8e('0x33')]=moment();this[_0xcc8e('0x11')](_0xa0926d);}};EmailChannel[_0xcc8e('0x10')][_0xcc8e('0x24')]=function(_0x16741c,_0x58bfb1){if(this[_0xcc8e('0xd')][_0x16741c]){this['mailAccounts'][_0x16741c]['Imap'][_0xcc8e('0x27')]=_0xcc8e('0x32');this['mailAccounts'][_0x16741c][_0xcc8e('0x15')]['messageStatus']=_0x58bfb1;return this['emit'](_0x16741c);}};EmailChannel[_0xcc8e('0x10')][_0xcc8e('0x34')]=function(_0x3332da,_0x209260){return this['smtp'][_0xcc8e('0x35')](_0x3332da,_0x209260);};EmailChannel['prototype']['verifySmtp']=function(_0x4b6a70){var _0x52edb5=this;if(this[_0xcc8e('0xd')][_0x4b6a70['id']]){if(_[_0xcc8e('0x36')](this[_0xcc8e('0xd')][_0x4b6a70['id']][_0xcc8e('0x37')])){this[_0xcc8e('0xd')][_0x4b6a70['id']][_0xcc8e('0x37')]={};}this['mailAccounts'][_0x4b6a70['id']][_0xcc8e('0x37')][_0xcc8e('0x33')]=moment();return new BPromise(function(_0x202f50,_0x35fb7c){return _0x52edb5[_0xcc8e('0xf')][_0xcc8e('0x38')](_0x4b6a70)[_0xcc8e('0x2f')](function(_0x345786){_0x52edb5[_0xcc8e('0xd')][_0x4b6a70['id']][_0xcc8e('0x37')][_0xcc8e('0x27')]='verified';_0x52edb5[_0xcc8e('0xd')][_0x4b6a70['id']][_0xcc8e('0x37')]['messageStatus']='';_0x52edb5[_0xcc8e('0x11')](_0x4b6a70['id']);_0x202f50(_0x345786);})['catch'](function(_0x102742){_0x52edb5['mailAccounts'][_0x4b6a70['id']]['Smtp'][_0xcc8e('0x27')]=_0xcc8e('0x32');_0x52edb5[_0xcc8e('0xd')][_0x4b6a70['id']][_0xcc8e('0x37')][_0xcc8e('0x16')]=_0x102742;_0x52edb5['emit'](_0x4b6a70['id']);_0x35fb7c(_0x102742);});});}return this[_0xcc8e('0xf')][_0xcc8e('0x38')](_0x4b6a70);};module[_0xcc8e('0x39')]=EmailChannel;