9e7f79fbfcc46a1f8c40ced4ebad52dfed85b259
[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 _0x2496=['error','handleImapError','disabled','handleImapBox','name','messages','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName','from','notify','Mail\x20accepted\x20from\x20routing','catch','lastSync','send','verifySmtp','Smtp','verify','verified','exports','lodash','ioredis','../../../config/logger','email','redis','localhost','socket.io-emitter','./imap','info','mailAccounts','imapAccounts','smtp','prototype','emit','active','Imap','status','messageStatus','total','isNil','stringify','md5','hasOwnProperty','service','provider','custom','box','bind','handleImapEmail','end','handleImapEnd'];(function(_0x2f54b6,_0x3cc6cb){var _0x169f21=function(_0x4dac27){while(--_0x4dac27){_0x2f54b6['push'](_0x2f54b6['shift']());}};_0x169f21(++_0x3cc6cb);}(_0x2496,0x146));var _0x6249=function(_0x361e50,_0xc348da){_0x361e50=_0x361e50-0x0;var _0xf4ae47=_0x2496[_0x361e50];return _0xf4ae47;};'use strict';var _=require(_0x6249('0x0'));var moment=require('moment');var BPromise=require('bluebird');var md5=require('md5');var Redis=require(_0x6249('0x1'));var config=require('../../../config/environment');var logger=require(_0x6249('0x2'))(_0x6249('0x3'));config[_0x6249('0x4')]=_['defaults'](config[_0x6249('0x4')],{'host':_0x6249('0x5'),'port':0x18eb});var io=require(_0x6249('0x6'))(new Redis(config['redis']));var EmailImap=require(_0x6249('0x7'));var EmailSmtp=require('./smtp');var mailAccount=require('../rpc/mailAccount');var md5Accounts={};function EmailChannel(_0x3d3b6a){logger[_0x6249('0x8')]('start\x20email\x20channel');this['mailAccounts']=_0x3d3b6a[_0x6249('0x9')];this[_0x6249('0xa')]={};this[_0x6249('0xb')]=new EmailSmtp();}EmailChannel[_0x6249('0xc')][_0x6249('0xd')]=function(_0x381a67){var _0x542fe1=this['mailAccounts'][_0x381a67];var _0x4aa303={'id':_0x542fe1['id'],'name':_0x542fe1['name'],'key':_0x542fe1['key'],'active':_0x542fe1[_0x6249('0xe')],'Imap':{'status':_0x542fe1[_0x6249('0xf')][_0x6249('0x10')],'messageStatus':_0x542fe1['Imap'][_0x6249('0x11')],'total':_0x542fe1[_0x6249('0xf')][_0x6249('0x12')]}};if(_[_0x6249('0x13')](md5Accounts[_0x381a67])){md5Accounts[_0x381a67]={};}var _0x61ba9e=md5(JSON[_0x6249('0x14')](_0x4aa303));if(md5Accounts[_0x381a67][_0x6249('0x15')]!==_0x61ba9e){md5Accounts[_0x381a67]['md5']=_0x61ba9e;io[_0x6249('0xd')]('mailAccount:update',_0x4aa303);}};EmailChannel[_0x6249('0xc')]['refresh']=function(_0x1845d2){for(var _0x31dd6a in _0x1845d2){if(_0x1845d2[_0x6249('0x16')](_0x31dd6a)){if(_0x1845d2[_0x31dd6a][_0x6249('0xf')]&&_0x1845d2[_0x31dd6a]['active']){var _0x594ad7=_0x1845d2[_0x31dd6a][_0x6249('0xf')];if(!_0x594ad7[_0x6249('0x17')]&&_0x1845d2[_0x31dd6a][_0x6249('0x18')]!==_0x6249('0x19'))_0x594ad7['service']=_0x1845d2[_0x31dd6a][_0x6249('0x18')];if(!this[_0x6249('0xa')][_0x6249('0x16')](_0x594ad7['id'])){this['imapAccounts'][_0x594ad7['id']]=new EmailImap(_0x594ad7,_0x1845d2[_0x31dd6a][_0x6249('0x3')]);this[_0x6249('0xa')][_0x594ad7['id']]['on'](_0x6249('0x1a'),this['handleImapBox'][_0x6249('0x1b')](this,_0x31dd6a));this[_0x6249('0xa')][_0x594ad7['id']]['on'](_0x6249('0x3'),this[_0x6249('0x1c')][_0x6249('0x1b')](this,_0x31dd6a));this['imapAccounts'][_0x594ad7['id']]['on'](_0x6249('0x1d'),this[_0x6249('0x1e')][_0x6249('0x1b')](this,_0x31dd6a));this[_0x6249('0xa')][_0x594ad7['id']]['on'](_0x6249('0x1f'),this[_0x6249('0x20')][_0x6249('0x1b')](this,_0x31dd6a));}}else{if(_[_0x6249('0x13')](this['mailAccounts'][_0x31dd6a][_0x6249('0xf')])){this[_0x6249('0x9')][_0x31dd6a]['Imap']={};}this['mailAccounts'][_0x31dd6a][_0x6249('0xf')][_0x6249('0x10')]=_0x6249('0x21');this[_0x6249('0x9')][_0x31dd6a][_0x6249('0xf')][_0x6249('0x11')]='';this[_0x6249('0xd')](_0x31dd6a);}}}};EmailChannel['prototype'][_0x6249('0x22')]=function(_0x2d443d,_0x4fd022){if(this[_0x6249('0x9')][_0x2d443d]){this[_0x6249('0x9')][_0x2d443d][_0x6249('0xf')][_0x6249('0x10')]='connected';this[_0x6249('0x9')][_0x2d443d][_0x6249('0xf')][_0x6249('0x11')]=_0x4fd022[_0x6249('0x23')];if(_0x4fd022['messages']){this[_0x6249('0x9')][_0x2d443d][_0x6249('0xf')][_0x6249('0x12')]=_0x4fd022[_0x6249('0x24')]['total'];}return this[_0x6249('0xd')](_0x2d443d);}};EmailChannel[_0x6249('0xc')][_0x6249('0x1c')]=function(_0x333ade,_0x41e7cb){logger[_0x6249('0x8')](_0x6249('0x25'),_0x41e7cb[_0x6249('0x26')],_0x41e7cb[_0x6249('0x27')],_0x41e7cb[_0x6249('0x28')],_0x333ade);if(this[_0x6249('0x9')][_0x333ade]){return mailAccount[_0x6249('0x29')](_0x333ade,_0x41e7cb)['then'](function(_0x5ede4d){if(_0x5ede4d){logger[_0x6249('0x8')](_0x6249('0x2a'));}})[_0x6249('0x2b')](function(_0x31db7b){logger[_0x6249('0x1f')]('Mail\x20not\x20accepted\x20from\x20routing\x20%s',JSON['stringify'](_0x31db7b));});}};EmailChannel[_0x6249('0xc')]['handleImapEnd']=function(_0x50726d){if(this[_0x6249('0x9')][_0x50726d]&&this[_0x6249('0x9')][_0x50726d][_0x6249('0xf')]){if(this[_0x6249('0xa')][this[_0x6249('0x9')][_0x50726d][_0x6249('0xf')]['id']]){delete this[_0x6249('0xa')][this['mailAccounts'][_0x50726d][_0x6249('0xf')]['id']];}this[_0x6249('0x9')][_0x50726d][_0x6249('0xf')][_0x6249('0x2c')]=moment();this[_0x6249('0xd')](_0x50726d);}};EmailChannel[_0x6249('0xc')][_0x6249('0x20')]=function(_0x11a26e,_0x4c310a){if(this[_0x6249('0x9')][_0x11a26e]){this[_0x6249('0x9')][_0x11a26e]['Imap'][_0x6249('0x10')]='error';this['mailAccounts'][_0x11a26e]['Imap'][_0x6249('0x11')]=_0x4c310a;return this[_0x6249('0xd')](_0x11a26e);}};EmailChannel['prototype']['sendMail']=function(_0x54a27d,_0x451fe2){return this[_0x6249('0xb')][_0x6249('0x2d')](_0x54a27d,_0x451fe2);};EmailChannel['prototype'][_0x6249('0x2e')]=function(_0x504be8){var _0x45fab4=this;if(this[_0x6249('0x9')][_0x504be8['id']]){if(_['isNil'](this[_0x6249('0x9')][_0x504be8['id']][_0x6249('0x2f')])){this['mailAccounts'][_0x504be8['id']]['Smtp']={};}this[_0x6249('0x9')][_0x504be8['id']][_0x6249('0x2f')]['lastSync']=moment();return new BPromise(function(_0x3de313,_0x1dbe7d){return _0x45fab4['smtp'][_0x6249('0x30')](_0x504be8)['then'](function(_0x133d8d){_0x45fab4[_0x6249('0x9')][_0x504be8['id']][_0x6249('0x2f')][_0x6249('0x10')]=_0x6249('0x31');_0x45fab4[_0x6249('0x9')][_0x504be8['id']][_0x6249('0x2f')][_0x6249('0x11')]='';_0x45fab4[_0x6249('0xd')](_0x504be8['id']);_0x3de313(_0x133d8d);})['catch'](function(_0x3bee87){_0x45fab4[_0x6249('0x9')][_0x504be8['id']][_0x6249('0x2f')][_0x6249('0x10')]='error';_0x45fab4[_0x6249('0x9')][_0x504be8['id']]['Smtp']['messageStatus']=_0x3bee87;_0x45fab4[_0x6249('0xd')](_0x504be8['id']);_0x1dbe7d(_0x3bee87);});});}return this[_0x6249('0xb')][_0x6249('0x30')](_0x504be8);};module[_0x6249('0x32')]=EmailChannel;