Built motion from commit 37e1ed56.|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 _0xae07=['from','notify','then','Mail\x20accepted\x20from\x20routing','catch','error','Mail\x20not\x20accepted\x20from\x20routing\x20%s','stringify','sendMail','send','verifySmtp','Smtp','lastSync','verify','verified','exports','bluebird','ioredis','../../../config/environment','email','defaults','redis','localhost','../rpc/mailAccount','info','start\x20email\x20channel','mailAccounts','imapAccounts','smtp','prototype','emit','name','key','Imap','status','total','isNil','md5','refresh','active','provider','service','box','handleImapBox','bind','handleImapEmail','handleImapEnd','handleImapError','disabled','messageStatus','connected','messages','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName'];(function(_0x22fc16,_0x53cf38){var _0x24e7b3=function(_0x27499b){while(--_0x27499b){_0x22fc16['push'](_0x22fc16['shift']());}};_0x24e7b3(++_0x53cf38);}(_0xae07,0xb5));var _0x7ae0=function(_0x6c993a,_0x5ea3ab){_0x6c993a=_0x6c993a-0x0;var _0x27f547=_0xae07[_0x6c993a];return _0x27f547;};'use strict';var _=require('lodash');var moment=require('moment');var BPromise=require(_0x7ae0('0x0'));var md5=require('md5');var Redis=require(_0x7ae0('0x1'));var config=require(_0x7ae0('0x2'));var logger=require('../../../config/logger')(_0x7ae0('0x3'));config['redis']=_[_0x7ae0('0x4')](config[_0x7ae0('0x5')],{'host':_0x7ae0('0x6'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x7ae0('0x5')]));var EmailImap=require('./imap');var EmailSmtp=require('./smtp');var mailAccount=require(_0x7ae0('0x7'));var md5Accounts={};function EmailChannel(_0x47710f){logger[_0x7ae0('0x8')](_0x7ae0('0x9'));this['mailAccounts']=_0x47710f[_0x7ae0('0xa')];this[_0x7ae0('0xb')]={};this[_0x7ae0('0xc')]=new EmailSmtp();}EmailChannel[_0x7ae0('0xd')][_0x7ae0('0xe')]=function(_0x394829){var _0x45de35=this['mailAccounts'][_0x394829];var _0x3174a3={'id':_0x45de35['id'],'name':_0x45de35[_0x7ae0('0xf')],'key':_0x45de35[_0x7ae0('0x10')],'active':_0x45de35['active'],'Imap':{'status':_0x45de35[_0x7ae0('0x11')][_0x7ae0('0x12')],'messageStatus':_0x45de35[_0x7ae0('0x11')]['messageStatus'],'total':_0x45de35[_0x7ae0('0x11')][_0x7ae0('0x13')]}};if(_[_0x7ae0('0x14')](md5Accounts[_0x394829])){md5Accounts[_0x394829]={};}var _0xe4ab4e=md5(JSON['stringify'](_0x3174a3));if(md5Accounts[_0x394829][_0x7ae0('0x15')]!==_0xe4ab4e){md5Accounts[_0x394829][_0x7ae0('0x15')]=_0xe4ab4e;io[_0x7ae0('0xe')]('mailAccount:update',_0x3174a3);}};EmailChannel[_0x7ae0('0xd')][_0x7ae0('0x16')]=function(_0x432bb7){for(var _0x25bf15 in _0x432bb7){if(_0x432bb7['hasOwnProperty'](_0x25bf15)){if(_0x432bb7[_0x25bf15][_0x7ae0('0x11')]&&_0x432bb7[_0x25bf15][_0x7ae0('0x17')]){var _0x2b613c=_0x432bb7[_0x25bf15][_0x7ae0('0x11')];if(!_0x2b613c['service']&&_0x432bb7[_0x25bf15][_0x7ae0('0x18')]!=='custom')_0x2b613c[_0x7ae0('0x19')]=_0x432bb7[_0x25bf15][_0x7ae0('0x18')];if(!this[_0x7ae0('0xb')]['hasOwnProperty'](_0x2b613c['id'])){this['imapAccounts'][_0x2b613c['id']]=new EmailImap(_0x2b613c,_0x432bb7[_0x25bf15][_0x7ae0('0x3')]);this['imapAccounts'][_0x2b613c['id']]['on'](_0x7ae0('0x1a'),this[_0x7ae0('0x1b')][_0x7ae0('0x1c')](this,_0x25bf15));this[_0x7ae0('0xb')][_0x2b613c['id']]['on'](_0x7ae0('0x3'),this[_0x7ae0('0x1d')][_0x7ae0('0x1c')](this,_0x25bf15));this['imapAccounts'][_0x2b613c['id']]['on']('end',this[_0x7ae0('0x1e')][_0x7ae0('0x1c')](this,_0x25bf15));this[_0x7ae0('0xb')][_0x2b613c['id']]['on']('error',this[_0x7ae0('0x1f')][_0x7ae0('0x1c')](this,_0x25bf15));}}else{if(_['isNil'](this[_0x7ae0('0xa')][_0x25bf15]['Imap'])){this['mailAccounts'][_0x25bf15][_0x7ae0('0x11')]={};}this[_0x7ae0('0xa')][_0x25bf15][_0x7ae0('0x11')][_0x7ae0('0x12')]=_0x7ae0('0x20');this[_0x7ae0('0xa')][_0x25bf15][_0x7ae0('0x11')][_0x7ae0('0x21')]='';this[_0x7ae0('0xe')](_0x25bf15);}}}};EmailChannel['prototype'][_0x7ae0('0x1b')]=function(_0x3d1037,_0xae0f20){if(this[_0x7ae0('0xa')][_0x3d1037]){this[_0x7ae0('0xa')][_0x3d1037][_0x7ae0('0x11')][_0x7ae0('0x12')]=_0x7ae0('0x22');this[_0x7ae0('0xa')][_0x3d1037][_0x7ae0('0x11')][_0x7ae0('0x21')]=_0xae0f20[_0x7ae0('0xf')];if(_0xae0f20[_0x7ae0('0x23')]){this[_0x7ae0('0xa')][_0x3d1037]['Imap']['total']=_0xae0f20[_0x7ae0('0x23')][_0x7ae0('0x13')];}return this[_0x7ae0('0xe')](_0x3d1037);}};EmailChannel[_0x7ae0('0xd')][_0x7ae0('0x1d')]=function(_0x135340,_0xb1039a){logger[_0x7ae0('0x8')](_0x7ae0('0x24'),_0xb1039a[_0x7ae0('0x25')],_0xb1039a[_0x7ae0('0x26')],_0xb1039a[_0x7ae0('0x27')],_0x135340);if(this[_0x7ae0('0xa')][_0x135340]){return mailAccount[_0x7ae0('0x28')](_0x135340,_0xb1039a)[_0x7ae0('0x29')](function(_0xea122f){if(_0xea122f){logger[_0x7ae0('0x8')](_0x7ae0('0x2a'));}})[_0x7ae0('0x2b')](function(_0x554f8d){logger[_0x7ae0('0x2c')](_0x7ae0('0x2d'),JSON[_0x7ae0('0x2e')](_0x554f8d));});}};EmailChannel[_0x7ae0('0xd')][_0x7ae0('0x1e')]=function(_0x55ac12){if(this[_0x7ae0('0xa')][_0x55ac12]&&this[_0x7ae0('0xa')][_0x55ac12]['Imap']){if(this['imapAccounts'][this[_0x7ae0('0xa')][_0x55ac12]['Imap']['id']]){delete this['imapAccounts'][this[_0x7ae0('0xa')][_0x55ac12][_0x7ae0('0x11')]['id']];}this[_0x7ae0('0xa')][_0x55ac12][_0x7ae0('0x11')]['lastSync']=moment();this[_0x7ae0('0xe')](_0x55ac12);}};EmailChannel[_0x7ae0('0xd')][_0x7ae0('0x1f')]=function(_0x21e570,_0x338cfc){if(this[_0x7ae0('0xa')][_0x21e570]){this['mailAccounts'][_0x21e570][_0x7ae0('0x11')][_0x7ae0('0x12')]=_0x7ae0('0x2c');this['mailAccounts'][_0x21e570][_0x7ae0('0x11')]['messageStatus']=_0x338cfc;return this['emit'](_0x21e570);}};EmailChannel['prototype'][_0x7ae0('0x2f')]=function(_0x222238,_0x453aff){return this[_0x7ae0('0xc')][_0x7ae0('0x30')](_0x222238,_0x453aff);};EmailChannel[_0x7ae0('0xd')][_0x7ae0('0x31')]=function(_0x1966a5){var _0x365ca1=this;if(this[_0x7ae0('0xa')][_0x1966a5['id']]){if(_[_0x7ae0('0x14')](this['mailAccounts'][_0x1966a5['id']][_0x7ae0('0x32')])){this[_0x7ae0('0xa')][_0x1966a5['id']][_0x7ae0('0x32')]={};}this['mailAccounts'][_0x1966a5['id']][_0x7ae0('0x32')][_0x7ae0('0x33')]=moment();return new BPromise(function(_0x1e80dd,_0x5bf86b){return _0x365ca1[_0x7ae0('0xc')][_0x7ae0('0x34')](_0x1966a5)[_0x7ae0('0x29')](function(_0x3134be){_0x365ca1['mailAccounts'][_0x1966a5['id']]['Smtp']['status']=_0x7ae0('0x35');_0x365ca1[_0x7ae0('0xa')][_0x1966a5['id']][_0x7ae0('0x32')][_0x7ae0('0x21')]='';_0x365ca1['emit'](_0x1966a5['id']);_0x1e80dd(_0x3134be);})[_0x7ae0('0x2b')](function(_0x1142e6){_0x365ca1[_0x7ae0('0xa')][_0x1966a5['id']][_0x7ae0('0x32')][_0x7ae0('0x12')]=_0x7ae0('0x2c');_0x365ca1[_0x7ae0('0xa')][_0x1966a5['id']]['Smtp'][_0x7ae0('0x21')]=_0x1142e6;_0x365ca1['emit'](_0x1966a5['id']);_0x5bf86b(_0x1142e6);});});}return this[_0x7ae0('0xc')][_0x7ae0('0x34')](_0x1966a5);};module[_0x7ae0('0x36')]=EmailChannel;