Built motion from commit (unavailable).|2.1.0
[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 _0xcf98=['end','handleImapEnd','error','handleImapError','status','messageStatus','prototype','connected','messages','total','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','lastSync','send','verifySmtp','isNil','Smtp','verify','verified','exports','moment','bluebird','md5','ioredis','../../../config/environment','redis','defaults','localhost','socket.io-emitter','./imap','./smtp','../rpc/mailAccount','info','mailAccounts','smtp','emit','name','key','Imap','stringify','mailAccount:update','refresh','hasOwnProperty','active','imapAccounts','email','box','handleImapBox','bind','handleImapEmail'];(function(_0x3622a6,_0x58798b){var _0xc32e8e=function(_0x1aa271){while(--_0x1aa271){_0x3622a6['push'](_0x3622a6['shift']());}};_0xc32e8e(++_0x58798b);}(_0xcf98,0x8d));var _0x8cf9=function(_0x4bcfd5,_0x3cb1bd){_0x4bcfd5=_0x4bcfd5-0x0;var _0x345543=_0xcf98[_0x4bcfd5];return _0x345543;};'use strict';var _=require('lodash');var moment=require(_0x8cf9('0x0'));var BPromise=require(_0x8cf9('0x1'));var md5=require(_0x8cf9('0x2'));var Redis=require(_0x8cf9('0x3'));var config=require(_0x8cf9('0x4'));var logger=require('../../../config/logger')('email');config[_0x8cf9('0x5')]=_[_0x8cf9('0x6')](config[_0x8cf9('0x5')],{'host':_0x8cf9('0x7'),'port':0x18eb});var io=require(_0x8cf9('0x8'))(new Redis(config[_0x8cf9('0x5')]));var EmailImap=require(_0x8cf9('0x9'));var EmailSmtp=require(_0x8cf9('0xa'));var mailAccount=require(_0x8cf9('0xb'));var md5Accounts={};function EmailChannel(_0x136309){logger[_0x8cf9('0xc')]('start\x20email\x20channel');this[_0x8cf9('0xd')]=_0x136309[_0x8cf9('0xd')];this['imapAccounts']={};this[_0x8cf9('0xe')]=new EmailSmtp();}EmailChannel['prototype'][_0x8cf9('0xf')]=function(_0x432742){var _0x1af7e4=this[_0x8cf9('0xd')][_0x432742];var _0x236fbd={'id':_0x1af7e4['id'],'name':_0x1af7e4[_0x8cf9('0x10')],'key':_0x1af7e4[_0x8cf9('0x11')],'active':_0x1af7e4['active'],'Imap':{'status':_0x1af7e4[_0x8cf9('0x12')]['status'],'messageStatus':_0x1af7e4['Imap']['messageStatus'],'total':_0x1af7e4[_0x8cf9('0x12')]['total']}};if(_['isNil'](md5Accounts[_0x432742])){md5Accounts[_0x432742]={};}var _0x41b825=md5(JSON[_0x8cf9('0x13')](_0x236fbd));if(md5Accounts[_0x432742][_0x8cf9('0x2')]!==_0x41b825){md5Accounts[_0x432742]['md5']=_0x41b825;io[_0x8cf9('0xf')](_0x8cf9('0x14'),_0x236fbd);}};EmailChannel['prototype'][_0x8cf9('0x15')]=function(_0xc13eeb){for(var _0x2417d8 in _0xc13eeb){if(_0xc13eeb[_0x8cf9('0x16')](_0x2417d8)){if(_0xc13eeb[_0x2417d8][_0x8cf9('0x12')]&&_0xc13eeb[_0x2417d8][_0x8cf9('0x17')]){var _0x21f397=_0xc13eeb[_0x2417d8][_0x8cf9('0x12')];if(!this[_0x8cf9('0x18')][_0x8cf9('0x16')](_0x21f397['id'])){this[_0x8cf9('0x18')][_0x21f397['id']]=new EmailImap(_0x21f397,_0xc13eeb[_0x2417d8][_0x8cf9('0x19')]);this[_0x8cf9('0x18')][_0x21f397['id']]['on'](_0x8cf9('0x1a'),this[_0x8cf9('0x1b')][_0x8cf9('0x1c')](this,_0x2417d8));this['imapAccounts'][_0x21f397['id']]['on']('email',this[_0x8cf9('0x1d')]['bind'](this,_0x2417d8));this[_0x8cf9('0x18')][_0x21f397['id']]['on'](_0x8cf9('0x1e'),this[_0x8cf9('0x1f')][_0x8cf9('0x1c')](this,_0x2417d8));this[_0x8cf9('0x18')][_0x21f397['id']]['on'](_0x8cf9('0x20'),this[_0x8cf9('0x21')][_0x8cf9('0x1c')](this,_0x2417d8));}}else{if(_['isNil'](this[_0x8cf9('0xd')][_0x2417d8]['Imap'])){this['mailAccounts'][_0x2417d8][_0x8cf9('0x12')]={};}this[_0x8cf9('0xd')][_0x2417d8]['Imap'][_0x8cf9('0x22')]='disabled';this['mailAccounts'][_0x2417d8][_0x8cf9('0x12')][_0x8cf9('0x23')]='';this['emit'](_0x2417d8);}}}};EmailChannel[_0x8cf9('0x24')][_0x8cf9('0x1b')]=function(_0x170414,_0x36dbcc){if(this[_0x8cf9('0xd')][_0x170414]){this['mailAccounts'][_0x170414]['Imap'][_0x8cf9('0x22')]=_0x8cf9('0x25');this[_0x8cf9('0xd')][_0x170414][_0x8cf9('0x12')][_0x8cf9('0x23')]=_0x36dbcc[_0x8cf9('0x10')];if(_0x36dbcc[_0x8cf9('0x26')]){this[_0x8cf9('0xd')][_0x170414][_0x8cf9('0x12')]['total']=_0x36dbcc['messages'][_0x8cf9('0x27')];}return this['emit'](_0x170414);}};EmailChannel[_0x8cf9('0x24')][_0x8cf9('0x1d')]=function(_0x2bbd81,_0x443ca1){logger['info'](_0x8cf9('0x28'),_0x443ca1[_0x8cf9('0x29')],_0x443ca1[_0x8cf9('0x2a')],_0x443ca1[_0x8cf9('0x2b')],_0x2bbd81);if(this[_0x8cf9('0xd')][_0x2bbd81]){return mailAccount[_0x8cf9('0x2c')](_0x2bbd81,_0x443ca1)[_0x8cf9('0x2d')](function(_0x52631c){if(_0x52631c){logger['info'](_0x8cf9('0x2e'));}})[_0x8cf9('0x2f')](function(_0x289b5b){logger[_0x8cf9('0x20')](_0x8cf9('0x30'),JSON[_0x8cf9('0x13')](_0x289b5b));});}};EmailChannel['prototype'][_0x8cf9('0x1f')]=function(_0x43057b){if(this[_0x8cf9('0xd')][_0x43057b]&&this[_0x8cf9('0xd')][_0x43057b][_0x8cf9('0x12')]){if(this[_0x8cf9('0x18')][this[_0x8cf9('0xd')][_0x43057b][_0x8cf9('0x12')]['id']]){delete this['imapAccounts'][this[_0x8cf9('0xd')][_0x43057b][_0x8cf9('0x12')]['id']];}this[_0x8cf9('0xd')][_0x43057b]['Imap'][_0x8cf9('0x31')]=moment();this['emit'](_0x43057b);}};EmailChannel[_0x8cf9('0x24')]['handleImapError']=function(_0x25870e,_0x5a577d){if(this[_0x8cf9('0xd')][_0x25870e]){this['mailAccounts'][_0x25870e][_0x8cf9('0x12')]['status']='error';this[_0x8cf9('0xd')][_0x25870e]['Imap'][_0x8cf9('0x23')]=_0x5a577d;return this['emit'](_0x25870e);}};EmailChannel[_0x8cf9('0x24')]['sendMail']=function(_0x236a22,_0x4542d5){return this[_0x8cf9('0xe')][_0x8cf9('0x32')](_0x236a22,_0x4542d5);};EmailChannel[_0x8cf9('0x24')][_0x8cf9('0x33')]=function(_0x2e2a03){var _0x1f4c52=this;if(this[_0x8cf9('0xd')][_0x2e2a03['id']]){if(_[_0x8cf9('0x34')](this[_0x8cf9('0xd')][_0x2e2a03['id']][_0x8cf9('0x35')])){this[_0x8cf9('0xd')][_0x2e2a03['id']][_0x8cf9('0x35')]={};}this['mailAccounts'][_0x2e2a03['id']][_0x8cf9('0x35')][_0x8cf9('0x31')]=moment();return new BPromise(function(_0x7b9a5a,_0x13fde7){return _0x1f4c52[_0x8cf9('0xe')][_0x8cf9('0x36')](_0x2e2a03)[_0x8cf9('0x2d')](function(_0x5d4c08){_0x1f4c52[_0x8cf9('0xd')][_0x2e2a03['id']][_0x8cf9('0x35')][_0x8cf9('0x22')]=_0x8cf9('0x37');_0x1f4c52['mailAccounts'][_0x2e2a03['id']][_0x8cf9('0x35')][_0x8cf9('0x23')]='';_0x1f4c52[_0x8cf9('0xf')](_0x2e2a03['id']);_0x7b9a5a(_0x5d4c08);})[_0x8cf9('0x2f')](function(_0x40361e){_0x1f4c52[_0x8cf9('0xd')][_0x2e2a03['id']][_0x8cf9('0x35')]['status']=_0x8cf9('0x20');_0x1f4c52[_0x8cf9('0xd')][_0x2e2a03['id']][_0x8cf9('0x35')][_0x8cf9('0x23')]=_0x40361e;_0x1f4c52['emit'](_0x2e2a03['id']);_0x13fde7(_0x40361e);});});}return this['smtp'][_0x8cf9('0x36')](_0x2e2a03);};module[_0x8cf9('0x38')]=EmailChannel;