Built motion from commit (unavailable).|2.5.3
[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 _0x2c0a=['isNil','md5','mailAccount:update','refresh','hasOwnProperty','box','bind','handleImapEmail','end','error','handleImapError','connected','messageStatus','messages','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName','from','then','catch','stringify','sendMail','Smtp','lastSync','verify','verified','lodash','bluebird','ioredis','../../../config/environment','../../../config/logger','email','defaults','redis','localhost','socket.io-emitter','./imap','./smtp','../rpc/mailAccount','info','start\x20email\x20channel','mailAccounts','imapAccounts','smtp','prototype','emit','name','key','active','Imap','status','total'];(function(_0x38f788,_0x670486){var _0x3090da=function(_0x88c24d){while(--_0x88c24d){_0x38f788['push'](_0x38f788['shift']());}};_0x3090da(++_0x670486);}(_0x2c0a,0x82));var _0xa2c0=function(_0x26bf51,_0x5e261a){_0x26bf51=_0x26bf51-0x0;var _0x503415=_0x2c0a[_0x26bf51];return _0x503415;};'use strict';var _=require(_0xa2c0('0x0'));var moment=require('moment');var BPromise=require(_0xa2c0('0x1'));var md5=require('md5');var Redis=require(_0xa2c0('0x2'));var config=require(_0xa2c0('0x3'));var logger=require(_0xa2c0('0x4'))(_0xa2c0('0x5'));config['redis']=_[_0xa2c0('0x6')](config[_0xa2c0('0x7')],{'host':_0xa2c0('0x8'),'port':0x18eb});var io=require(_0xa2c0('0x9'))(new Redis(config[_0xa2c0('0x7')]));var EmailImap=require(_0xa2c0('0xa'));var EmailSmtp=require(_0xa2c0('0xb'));var mailAccount=require(_0xa2c0('0xc'));var md5Accounts={};function EmailChannel(_0x62f124){logger[_0xa2c0('0xd')](_0xa2c0('0xe'));this[_0xa2c0('0xf')]=_0x62f124[_0xa2c0('0xf')];this[_0xa2c0('0x10')]={};this[_0xa2c0('0x11')]=new EmailSmtp();}EmailChannel[_0xa2c0('0x12')][_0xa2c0('0x13')]=function(_0x8900c5){var _0x13369a=this[_0xa2c0('0xf')][_0x8900c5];var _0x50cb8c={'id':_0x13369a['id'],'name':_0x13369a[_0xa2c0('0x14')],'key':_0x13369a[_0xa2c0('0x15')],'active':_0x13369a[_0xa2c0('0x16')],'Imap':{'status':_0x13369a[_0xa2c0('0x17')][_0xa2c0('0x18')],'messageStatus':_0x13369a[_0xa2c0('0x17')]['messageStatus'],'total':_0x13369a[_0xa2c0('0x17')][_0xa2c0('0x19')]}};if(_[_0xa2c0('0x1a')](md5Accounts[_0x8900c5])){md5Accounts[_0x8900c5]={};}var _0x2cc679=md5(JSON['stringify'](_0x50cb8c));if(md5Accounts[_0x8900c5]['md5']!==_0x2cc679){md5Accounts[_0x8900c5][_0xa2c0('0x1b')]=_0x2cc679;io['emit'](_0xa2c0('0x1c'),_0x50cb8c);}};EmailChannel[_0xa2c0('0x12')][_0xa2c0('0x1d')]=function(_0x123cbf){for(var _0x528fac in _0x123cbf){if(_0x123cbf[_0xa2c0('0x1e')](_0x528fac)){if(_0x123cbf[_0x528fac][_0xa2c0('0x17')]&&_0x123cbf[_0x528fac][_0xa2c0('0x16')]){var _0xa7cef1=_0x123cbf[_0x528fac]['Imap'];if(!this['imapAccounts'][_0xa2c0('0x1e')](_0xa7cef1['id'])){this[_0xa2c0('0x10')][_0xa7cef1['id']]=new EmailImap(_0xa7cef1,_0x123cbf[_0x528fac][_0xa2c0('0x5')]);this[_0xa2c0('0x10')][_0xa7cef1['id']]['on'](_0xa2c0('0x1f'),this['handleImapBox'][_0xa2c0('0x20')](this,_0x528fac));this[_0xa2c0('0x10')][_0xa7cef1['id']]['on'](_0xa2c0('0x5'),this[_0xa2c0('0x21')][_0xa2c0('0x20')](this,_0x528fac));this['imapAccounts'][_0xa7cef1['id']]['on'](_0xa2c0('0x22'),this['handleImapEnd'][_0xa2c0('0x20')](this,_0x528fac));this['imapAccounts'][_0xa7cef1['id']]['on'](_0xa2c0('0x23'),this[_0xa2c0('0x24')][_0xa2c0('0x20')](this,_0x528fac));}}else{if(_['isNil'](this[_0xa2c0('0xf')][_0x528fac][_0xa2c0('0x17')])){this['mailAccounts'][_0x528fac][_0xa2c0('0x17')]={};}this['mailAccounts'][_0x528fac]['Imap'][_0xa2c0('0x18')]='disabled';this['mailAccounts'][_0x528fac][_0xa2c0('0x17')]['messageStatus']='';this[_0xa2c0('0x13')](_0x528fac);}}}};EmailChannel['prototype']['handleImapBox']=function(_0x22197e,_0x47627b){if(this[_0xa2c0('0xf')][_0x22197e]){this[_0xa2c0('0xf')][_0x22197e][_0xa2c0('0x17')][_0xa2c0('0x18')]=_0xa2c0('0x25');this[_0xa2c0('0xf')][_0x22197e]['Imap'][_0xa2c0('0x26')]=_0x47627b[_0xa2c0('0x14')];if(_0x47627b[_0xa2c0('0x27')]){this[_0xa2c0('0xf')][_0x22197e][_0xa2c0('0x17')]['total']=_0x47627b[_0xa2c0('0x27')]['total'];}return this[_0xa2c0('0x13')](_0x22197e);}};EmailChannel[_0xa2c0('0x12')][_0xa2c0('0x21')]=function(_0x566092,_0x149c4c){logger[_0xa2c0('0xd')](_0xa2c0('0x28'),_0x149c4c[_0xa2c0('0x29')],_0x149c4c[_0xa2c0('0x2a')],_0x149c4c[_0xa2c0('0x2b')],_0x566092);if(this[_0xa2c0('0xf')][_0x566092]){return mailAccount['notify'](_0x566092,_0x149c4c)[_0xa2c0('0x2c')](function(_0x119b64){if(_0x119b64){logger[_0xa2c0('0xd')]('Mail\x20accepted\x20from\x20routing');}})[_0xa2c0('0x2d')](function(_0x2db129){logger['error']('Mail\x20not\x20accepted\x20from\x20routing\x20%s',JSON[_0xa2c0('0x2e')](_0x2db129));});}};EmailChannel[_0xa2c0('0x12')]['handleImapEnd']=function(_0x55d60a){if(this['mailAccounts'][_0x55d60a]&&this[_0xa2c0('0xf')][_0x55d60a][_0xa2c0('0x17')]){if(this[_0xa2c0('0x10')][this[_0xa2c0('0xf')][_0x55d60a][_0xa2c0('0x17')]['id']]){delete this[_0xa2c0('0x10')][this[_0xa2c0('0xf')][_0x55d60a][_0xa2c0('0x17')]['id']];}this[_0xa2c0('0xf')][_0x55d60a]['Imap']['lastSync']=moment();this[_0xa2c0('0x13')](_0x55d60a);}};EmailChannel[_0xa2c0('0x12')][_0xa2c0('0x24')]=function(_0x43838c,_0x49249c){if(this[_0xa2c0('0xf')][_0x43838c]){this[_0xa2c0('0xf')][_0x43838c][_0xa2c0('0x17')][_0xa2c0('0x18')]=_0xa2c0('0x23');this[_0xa2c0('0xf')][_0x43838c]['Imap'][_0xa2c0('0x26')]=_0x49249c;return this[_0xa2c0('0x13')](_0x43838c);}};EmailChannel[_0xa2c0('0x12')][_0xa2c0('0x2f')]=function(_0xeb746b,_0x2aef55){return this['smtp']['send'](_0xeb746b,_0x2aef55);};EmailChannel['prototype']['verifySmtp']=function(_0x5b9f74){var _0x1d2b5f=this;if(this['mailAccounts'][_0x5b9f74['id']]){if(_[_0xa2c0('0x1a')](this['mailAccounts'][_0x5b9f74['id']][_0xa2c0('0x30')])){this[_0xa2c0('0xf')][_0x5b9f74['id']]['Smtp']={};}this[_0xa2c0('0xf')][_0x5b9f74['id']][_0xa2c0('0x30')][_0xa2c0('0x31')]=moment();return new BPromise(function(_0x493376,_0x13a45a){return _0x1d2b5f[_0xa2c0('0x11')][_0xa2c0('0x32')](_0x5b9f74)[_0xa2c0('0x2c')](function(_0x5e983f){_0x1d2b5f[_0xa2c0('0xf')][_0x5b9f74['id']][_0xa2c0('0x30')][_0xa2c0('0x18')]=_0xa2c0('0x33');_0x1d2b5f[_0xa2c0('0xf')][_0x5b9f74['id']][_0xa2c0('0x30')]['messageStatus']='';_0x1d2b5f[_0xa2c0('0x13')](_0x5b9f74['id']);_0x493376(_0x5e983f);})[_0xa2c0('0x2d')](function(_0x12d866){_0x1d2b5f['mailAccounts'][_0x5b9f74['id']][_0xa2c0('0x30')][_0xa2c0('0x18')]=_0xa2c0('0x23');_0x1d2b5f['mailAccounts'][_0x5b9f74['id']][_0xa2c0('0x30')][_0xa2c0('0x26')]=_0x12d866;_0x1d2b5f[_0xa2c0('0x13')](_0x5b9f74['id']);_0x13a45a(_0x12d866);});});}return this[_0xa2c0('0x11')]['verify'](_0x5b9f74);};module['exports']=EmailChannel;