6d263b944907ab9b7a2d5ab0315e28bd168a321a
[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 _0xebda=['bind','handleImapEmail','end','handleImapEnd','error','handleImapError','isNil','connected','messages','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName','from','notify','then','Mail\x20accepted\x20from\x20routing','catch','Mail\x20not\x20accepted\x20from\x20routing\x20%s','stringify','lastSync','send','verifySmtp','Smtp','verify','verified','exports','lodash','bluebird','md5','ioredis','../../../config/logger','redis','defaults','localhost','socket.io-emitter','./smtp','../rpc/mailAccount','info','start\x20email\x20channel','mailAccounts','smtp','prototype','emit','name','active','Imap','status','messageStatus','total','mailAccount:update','hasOwnProperty','service','provider','custom','imapAccounts','email','box','handleImapBox'];(function(_0x16d411,_0x5e0970){var _0x20336d=function(_0x15d8aa){while(--_0x15d8aa){_0x16d411['push'](_0x16d411['shift']());}};_0x20336d(++_0x5e0970);}(_0xebda,0x8e));var _0xaebd=function(_0x5a79e2,_0x319b1a){_0x5a79e2=_0x5a79e2-0x0;var _0x266c45=_0xebda[_0x5a79e2];return _0x266c45;};'use strict';var _=require(_0xaebd('0x0'));var moment=require('moment');var BPromise=require(_0xaebd('0x1'));var md5=require(_0xaebd('0x2'));var Redis=require(_0xaebd('0x3'));var config=require('../../../config/environment');var logger=require(_0xaebd('0x4'))('email');config[_0xaebd('0x5')]=_[_0xaebd('0x6')](config[_0xaebd('0x5')],{'host':_0xaebd('0x7'),'port':0x18eb});var io=require(_0xaebd('0x8'))(new Redis(config[_0xaebd('0x5')]));var EmailImap=require('./imap');var EmailSmtp=require(_0xaebd('0x9'));var mailAccount=require(_0xaebd('0xa'));var md5Accounts={};function EmailChannel(_0x54ed98){logger[_0xaebd('0xb')](_0xaebd('0xc'));this['mailAccounts']=_0x54ed98[_0xaebd('0xd')];this['imapAccounts']={};this[_0xaebd('0xe')]=new EmailSmtp();}EmailChannel[_0xaebd('0xf')][_0xaebd('0x10')]=function(_0x540fb2){var _0x3fd85f=this['mailAccounts'][_0x540fb2];var _0x476c95={'id':_0x3fd85f['id'],'name':_0x3fd85f[_0xaebd('0x11')],'key':_0x3fd85f['key'],'active':_0x3fd85f[_0xaebd('0x12')],'Imap':{'status':_0x3fd85f[_0xaebd('0x13')][_0xaebd('0x14')],'messageStatus':_0x3fd85f[_0xaebd('0x13')][_0xaebd('0x15')],'total':_0x3fd85f[_0xaebd('0x13')][_0xaebd('0x16')]}};if(_['isNil'](md5Accounts[_0x540fb2])){md5Accounts[_0x540fb2]={};}var _0x1b4cc9=md5(JSON['stringify'](_0x476c95));if(md5Accounts[_0x540fb2][_0xaebd('0x2')]!==_0x1b4cc9){md5Accounts[_0x540fb2][_0xaebd('0x2')]=_0x1b4cc9;io[_0xaebd('0x10')](_0xaebd('0x17'),_0x476c95);}};EmailChannel['prototype']['refresh']=function(_0x12d1bb){for(var _0xeb9284 in _0x12d1bb){if(_0x12d1bb[_0xaebd('0x18')](_0xeb9284)){if(_0x12d1bb[_0xeb9284]['Imap']&&_0x12d1bb[_0xeb9284][_0xaebd('0x12')]){var _0x4bbbf5=_0x12d1bb[_0xeb9284][_0xaebd('0x13')];if(!_0x4bbbf5[_0xaebd('0x19')]&&_0x12d1bb[_0xeb9284][_0xaebd('0x1a')]!==_0xaebd('0x1b'))_0x4bbbf5['service']=_0x12d1bb[_0xeb9284][_0xaebd('0x1a')];if(!this[_0xaebd('0x1c')][_0xaebd('0x18')](_0x4bbbf5['id'])){this[_0xaebd('0x1c')][_0x4bbbf5['id']]=new EmailImap(_0x4bbbf5,_0x12d1bb[_0xeb9284][_0xaebd('0x1d')]);this[_0xaebd('0x1c')][_0x4bbbf5['id']]['on'](_0xaebd('0x1e'),this[_0xaebd('0x1f')][_0xaebd('0x20')](this,_0xeb9284));this[_0xaebd('0x1c')][_0x4bbbf5['id']]['on']('email',this[_0xaebd('0x21')]['bind'](this,_0xeb9284));this[_0xaebd('0x1c')][_0x4bbbf5['id']]['on'](_0xaebd('0x22'),this[_0xaebd('0x23')][_0xaebd('0x20')](this,_0xeb9284));this[_0xaebd('0x1c')][_0x4bbbf5['id']]['on'](_0xaebd('0x24'),this[_0xaebd('0x25')][_0xaebd('0x20')](this,_0xeb9284));}}else{if(_[_0xaebd('0x26')](this[_0xaebd('0xd')][_0xeb9284][_0xaebd('0x13')])){this[_0xaebd('0xd')][_0xeb9284][_0xaebd('0x13')]={};}this[_0xaebd('0xd')][_0xeb9284]['Imap'][_0xaebd('0x14')]='disabled';this[_0xaebd('0xd')][_0xeb9284][_0xaebd('0x13')]['messageStatus']='';this[_0xaebd('0x10')](_0xeb9284);}}}};EmailChannel[_0xaebd('0xf')][_0xaebd('0x1f')]=function(_0x5f48e1,_0x53fdc4){if(this[_0xaebd('0xd')][_0x5f48e1]){this['mailAccounts'][_0x5f48e1][_0xaebd('0x13')]['status']=_0xaebd('0x27');this[_0xaebd('0xd')][_0x5f48e1]['Imap']['messageStatus']=_0x53fdc4['name'];if(_0x53fdc4[_0xaebd('0x28')]){this[_0xaebd('0xd')][_0x5f48e1][_0xaebd('0x13')][_0xaebd('0x16')]=_0x53fdc4[_0xaebd('0x28')]['total'];}return this[_0xaebd('0x10')](_0x5f48e1);}};EmailChannel['prototype'][_0xaebd('0x21')]=function(_0xde8fa4,_0x520a9e){logger[_0xaebd('0xb')](_0xaebd('0x29'),_0x520a9e[_0xaebd('0x2a')],_0x520a9e[_0xaebd('0x2b')],_0x520a9e[_0xaebd('0x2c')],_0xde8fa4);if(this[_0xaebd('0xd')][_0xde8fa4]){return mailAccount[_0xaebd('0x2d')](_0xde8fa4,_0x520a9e)[_0xaebd('0x2e')](function(_0x106c44){if(_0x106c44){logger[_0xaebd('0xb')](_0xaebd('0x2f'));}})[_0xaebd('0x30')](function(_0x189259){logger[_0xaebd('0x24')](_0xaebd('0x31'),JSON[_0xaebd('0x32')](_0x189259));});}};EmailChannel[_0xaebd('0xf')][_0xaebd('0x23')]=function(_0x199b44){if(this['mailAccounts'][_0x199b44]&&this['mailAccounts'][_0x199b44][_0xaebd('0x13')]){if(this[_0xaebd('0x1c')][this['mailAccounts'][_0x199b44][_0xaebd('0x13')]['id']]){delete this['imapAccounts'][this[_0xaebd('0xd')][_0x199b44][_0xaebd('0x13')]['id']];}this[_0xaebd('0xd')][_0x199b44][_0xaebd('0x13')][_0xaebd('0x33')]=moment();this[_0xaebd('0x10')](_0x199b44);}};EmailChannel['prototype'][_0xaebd('0x25')]=function(_0x224006,_0x3146b7){if(this[_0xaebd('0xd')][_0x224006]){this[_0xaebd('0xd')][_0x224006][_0xaebd('0x13')][_0xaebd('0x14')]=_0xaebd('0x24');this[_0xaebd('0xd')][_0x224006]['Imap']['messageStatus']=_0x3146b7;return this['emit'](_0x224006);}};EmailChannel[_0xaebd('0xf')]['sendMail']=function(_0x5459bf,_0x538796){return this[_0xaebd('0xe')][_0xaebd('0x34')](_0x5459bf,_0x538796);};EmailChannel[_0xaebd('0xf')][_0xaebd('0x35')]=function(_0x7e5aa5){var _0x447a0b=this;if(this[_0xaebd('0xd')][_0x7e5aa5['id']]){if(_[_0xaebd('0x26')](this[_0xaebd('0xd')][_0x7e5aa5['id']][_0xaebd('0x36')])){this[_0xaebd('0xd')][_0x7e5aa5['id']][_0xaebd('0x36')]={};}this[_0xaebd('0xd')][_0x7e5aa5['id']][_0xaebd('0x36')][_0xaebd('0x33')]=moment();return new BPromise(function(_0x35e067,_0x22dfd4){return _0x447a0b[_0xaebd('0xe')][_0xaebd('0x37')](_0x7e5aa5)['then'](function(_0x11c821){_0x447a0b[_0xaebd('0xd')][_0x7e5aa5['id']][_0xaebd('0x36')][_0xaebd('0x14')]=_0xaebd('0x38');_0x447a0b['mailAccounts'][_0x7e5aa5['id']]['Smtp'][_0xaebd('0x15')]='';_0x447a0b[_0xaebd('0x10')](_0x7e5aa5['id']);_0x35e067(_0x11c821);})['catch'](function(_0x26e620){_0x447a0b[_0xaebd('0xd')][_0x7e5aa5['id']][_0xaebd('0x36')][_0xaebd('0x14')]='error';_0x447a0b[_0xaebd('0xd')][_0x7e5aa5['id']]['Smtp']['messageStatus']=_0x26e620;_0x447a0b['emit'](_0x7e5aa5['id']);_0x22dfd4(_0x26e620);});});}return this[_0xaebd('0xe')][_0xaebd('0x37')](_0x7e5aa5);};module[_0xaebd('0x39')]=EmailChannel;