Built motion from commit 58b0471d.|2.6.29
[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 _0x82c8=['error','Mail\x20not\x20accepted\x20from\x20routing\x20%s','lastSync','send','verifySmtp','isNil','Smtp','verified','verify','exports','moment','md5','ioredis','../../../config/environment','../../../config/logger','email','redis','defaults','localhost','socket.io-emitter','./imap','./smtp','../rpc/mailAccount','info','mailAccounts','smtp','prototype','emit','name','key','active','Imap','status','messageStatus','total','stringify','mailAccount:update','refresh','hasOwnProperty','provider','custom','imapAccounts','handleImapBox','handleImapEmail','bind','end','handleImapEnd','handleImapError','disabled','messages','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName','then','Mail\x20accepted\x20from\x20routing','catch'];(function(_0x2a3efe,_0x42c0f5){var _0x45f773=function(_0x2835ef){while(--_0x2835ef){_0x2a3efe['push'](_0x2a3efe['shift']());}};_0x45f773(++_0x42c0f5);}(_0x82c8,0x122));var _0x882c=function(_0x54af6a,_0x48fe6e){_0x54af6a=_0x54af6a-0x0;var _0x2bdb2b=_0x82c8[_0x54af6a];return _0x2bdb2b;};'use strict';var _=require('lodash');var moment=require(_0x882c('0x0'));var BPromise=require('bluebird');var md5=require(_0x882c('0x1'));var Redis=require(_0x882c('0x2'));var config=require(_0x882c('0x3'));var logger=require(_0x882c('0x4'))(_0x882c('0x5'));config[_0x882c('0x6')]=_[_0x882c('0x7')](config[_0x882c('0x6')],{'host':_0x882c('0x8'),'port':0x18eb});var io=require(_0x882c('0x9'))(new Redis(config['redis']));var EmailImap=require(_0x882c('0xa'));var EmailSmtp=require(_0x882c('0xb'));var mailAccount=require(_0x882c('0xc'));var md5Accounts={};function EmailChannel(_0x2d00d6){logger[_0x882c('0xd')]('start\x20email\x20channel');this[_0x882c('0xe')]=_0x2d00d6[_0x882c('0xe')];this['imapAccounts']={};this[_0x882c('0xf')]=new EmailSmtp();}EmailChannel[_0x882c('0x10')][_0x882c('0x11')]=function(_0x37f792){var _0x15bcac=this[_0x882c('0xe')][_0x37f792];var _0x47c740={'id':_0x15bcac['id'],'name':_0x15bcac[_0x882c('0x12')],'key':_0x15bcac[_0x882c('0x13')],'active':_0x15bcac[_0x882c('0x14')],'Imap':{'status':_0x15bcac[_0x882c('0x15')][_0x882c('0x16')],'messageStatus':_0x15bcac[_0x882c('0x15')][_0x882c('0x17')],'total':_0x15bcac[_0x882c('0x15')][_0x882c('0x18')]}};if(_['isNil'](md5Accounts[_0x37f792])){md5Accounts[_0x37f792]={};}var _0x2b0682=md5(JSON[_0x882c('0x19')](_0x47c740));if(md5Accounts[_0x37f792][_0x882c('0x1')]!==_0x2b0682){md5Accounts[_0x37f792][_0x882c('0x1')]=_0x2b0682;io[_0x882c('0x11')](_0x882c('0x1a'),_0x47c740);}};EmailChannel[_0x882c('0x10')][_0x882c('0x1b')]=function(_0x411999){for(var _0x5a1923 in _0x411999){if(_0x411999[_0x882c('0x1c')](_0x5a1923)){if(_0x411999[_0x5a1923]['Imap']&&_0x411999[_0x5a1923]['active']){var _0x33fc64=_0x411999[_0x5a1923]['Imap'];if(!_0x33fc64['service']&&_0x411999[_0x5a1923][_0x882c('0x1d')]!==_0x882c('0x1e'))_0x33fc64['service']=_0x411999[_0x5a1923]['provider'];if(!this[_0x882c('0x1f')]['hasOwnProperty'](_0x33fc64['id'])){this['imapAccounts'][_0x33fc64['id']]=new EmailImap(_0x33fc64,_0x411999[_0x5a1923][_0x882c('0x5')]);this[_0x882c('0x1f')][_0x33fc64['id']]['on']('box',this[_0x882c('0x20')]['bind'](this,_0x5a1923));this['imapAccounts'][_0x33fc64['id']]['on'](_0x882c('0x5'),this[_0x882c('0x21')][_0x882c('0x22')](this,_0x5a1923));this[_0x882c('0x1f')][_0x33fc64['id']]['on'](_0x882c('0x23'),this[_0x882c('0x24')]['bind'](this,_0x5a1923));this['imapAccounts'][_0x33fc64['id']]['on']('error',this[_0x882c('0x25')][_0x882c('0x22')](this,_0x5a1923));}}else{if(_['isNil'](this['mailAccounts'][_0x5a1923]['Imap'])){this[_0x882c('0xe')][_0x5a1923][_0x882c('0x15')]={};}this[_0x882c('0xe')][_0x5a1923]['Imap'][_0x882c('0x16')]=_0x882c('0x26');this[_0x882c('0xe')][_0x5a1923]['Imap'][_0x882c('0x17')]='';this[_0x882c('0x11')](_0x5a1923);}}}};EmailChannel[_0x882c('0x10')][_0x882c('0x20')]=function(_0x4f5076,_0x3fb723){if(this['mailAccounts'][_0x4f5076]){this[_0x882c('0xe')][_0x4f5076][_0x882c('0x15')][_0x882c('0x16')]='connected';this[_0x882c('0xe')][_0x4f5076][_0x882c('0x15')][_0x882c('0x17')]=_0x3fb723['name'];if(_0x3fb723['messages']){this['mailAccounts'][_0x4f5076][_0x882c('0x15')]['total']=_0x3fb723[_0x882c('0x27')][_0x882c('0x18')];}return this['emit'](_0x4f5076);}};EmailChannel[_0x882c('0x10')]['handleImapEmail']=function(_0x5397dd,_0x3a887f){logger['info'](_0x882c('0x28'),_0x3a887f[_0x882c('0x29')],_0x3a887f[_0x882c('0x2a')],_0x3a887f['from'],_0x5397dd);if(this[_0x882c('0xe')][_0x5397dd]){return mailAccount['notify'](_0x5397dd,_0x3a887f)[_0x882c('0x2b')](function(_0x31b838){if(_0x31b838){logger['info'](_0x882c('0x2c'));}})[_0x882c('0x2d')](function(_0x189171){logger[_0x882c('0x2e')](_0x882c('0x2f'),JSON[_0x882c('0x19')](_0x189171));});}};EmailChannel[_0x882c('0x10')][_0x882c('0x24')]=function(_0x51717c){if(this[_0x882c('0xe')][_0x51717c]&&this[_0x882c('0xe')][_0x51717c][_0x882c('0x15')]){if(this[_0x882c('0x1f')][this[_0x882c('0xe')][_0x51717c][_0x882c('0x15')]['id']]){delete this['imapAccounts'][this[_0x882c('0xe')][_0x51717c]['Imap']['id']];}this[_0x882c('0xe')][_0x51717c][_0x882c('0x15')][_0x882c('0x30')]=moment();this[_0x882c('0x11')](_0x51717c);}};EmailChannel['prototype'][_0x882c('0x25')]=function(_0xcc801,_0x4997ce){if(this[_0x882c('0xe')][_0xcc801]){this['mailAccounts'][_0xcc801]['Imap'][_0x882c('0x16')]=_0x882c('0x2e');this['mailAccounts'][_0xcc801][_0x882c('0x15')][_0x882c('0x17')]=_0x4997ce;return this['emit'](_0xcc801);}};EmailChannel['prototype']['sendMail']=function(_0x534dee,_0x264460){return this['smtp'][_0x882c('0x31')](_0x534dee,_0x264460);};EmailChannel[_0x882c('0x10')][_0x882c('0x32')]=function(_0x51748d){var _0x32170d=this;if(this[_0x882c('0xe')][_0x51748d['id']]){if(_[_0x882c('0x33')](this['mailAccounts'][_0x51748d['id']][_0x882c('0x34')])){this[_0x882c('0xe')][_0x51748d['id']][_0x882c('0x34')]={};}this[_0x882c('0xe')][_0x51748d['id']][_0x882c('0x34')][_0x882c('0x30')]=moment();return new BPromise(function(_0x13ec1c,_0x3a016f){return _0x32170d[_0x882c('0xf')]['verify'](_0x51748d)['then'](function(_0x5a4f73){_0x32170d['mailAccounts'][_0x51748d['id']][_0x882c('0x34')][_0x882c('0x16')]=_0x882c('0x35');_0x32170d[_0x882c('0xe')][_0x51748d['id']]['Smtp'][_0x882c('0x17')]='';_0x32170d['emit'](_0x51748d['id']);_0x13ec1c(_0x5a4f73);})[_0x882c('0x2d')](function(_0x55448f){_0x32170d[_0x882c('0xe')][_0x51748d['id']]['Smtp'][_0x882c('0x16')]=_0x882c('0x2e');_0x32170d[_0x882c('0xe')][_0x51748d['id']][_0x882c('0x34')][_0x882c('0x17')]=_0x55448f;_0x32170d[_0x882c('0x11')](_0x51748d['id']);_0x3a016f(_0x55448f);});});}return this[_0x882c('0xf')][_0x882c('0x36')](_0x51748d);};module[_0x882c('0x37')]=EmailChannel;