Built motion from commit 863c3375.|2.6.28
[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 _0x8434=['verified','verify','exports','moment','bluebird','md5','ioredis','../../../config/environment','email','defaults','redis','localhost','socket.io-emitter','./smtp','info','mailAccounts','imapAccounts','smtp','prototype','name','key','active','status','Imap','messageStatus','isNil','stringify','emit','mailAccount:update','refresh','service','custom','provider','box','handleImapBox','end','handleImapEnd','bind','disabled','connected','messages','total','handleImapEmail','firstName','lastName','from','notify','Mail\x20accepted\x20from\x20routing','catch','error','Mail\x20not\x20accepted\x20from\x20routing\x20%s','lastSync','handleImapError','sendMail','send','Smtp','then'];(function(_0x5d1867,_0x1aed97){var _0x43b515=function(_0x1b8e21){while(--_0x1b8e21){_0x5d1867['push'](_0x5d1867['shift']());}};_0x43b515(++_0x1aed97);}(_0x8434,0xae));var _0x4843=function(_0x115500,_0x5ca5d8){_0x115500=_0x115500-0x0;var _0x36f8a4=_0x8434[_0x115500];return _0x36f8a4;};'use strict';var _=require('lodash');var moment=require(_0x4843('0x0'));var BPromise=require(_0x4843('0x1'));var md5=require(_0x4843('0x2'));var Redis=require(_0x4843('0x3'));var config=require(_0x4843('0x4'));var logger=require('../../../config/logger')(_0x4843('0x5'));config['redis']=_[_0x4843('0x6')](config[_0x4843('0x7')],{'host':_0x4843('0x8'),'port':0x18eb});var io=require(_0x4843('0x9'))(new Redis(config[_0x4843('0x7')]));var EmailImap=require('./imap');var EmailSmtp=require(_0x4843('0xa'));var mailAccount=require('../rpc/mailAccount');var md5Accounts={};function EmailChannel(_0x5103da){logger[_0x4843('0xb')]('start\x20email\x20channel');this['mailAccounts']=_0x5103da[_0x4843('0xc')];this[_0x4843('0xd')]={};this[_0x4843('0xe')]=new EmailSmtp();}EmailChannel[_0x4843('0xf')]['emit']=function(_0x52666e){var _0x1a1240=this['mailAccounts'][_0x52666e];var _0x1170b5={'id':_0x1a1240['id'],'name':_0x1a1240[_0x4843('0x10')],'key':_0x1a1240[_0x4843('0x11')],'active':_0x1a1240[_0x4843('0x12')],'Imap':{'status':_0x1a1240['Imap'][_0x4843('0x13')],'messageStatus':_0x1a1240[_0x4843('0x14')][_0x4843('0x15')],'total':_0x1a1240[_0x4843('0x14')]['total']}};if(_[_0x4843('0x16')](md5Accounts[_0x52666e])){md5Accounts[_0x52666e]={};}var _0x27c1ab=md5(JSON[_0x4843('0x17')](_0x1170b5));if(md5Accounts[_0x52666e][_0x4843('0x2')]!==_0x27c1ab){md5Accounts[_0x52666e][_0x4843('0x2')]=_0x27c1ab;io[_0x4843('0x18')](_0x4843('0x19'),_0x1170b5);}};EmailChannel[_0x4843('0xf')][_0x4843('0x1a')]=function(_0x279c03){for(var _0x16f54b in _0x279c03){if(_0x279c03['hasOwnProperty'](_0x16f54b)){if(_0x279c03[_0x16f54b]['Imap']&&_0x279c03[_0x16f54b]['active']){var _0x576a79=_0x279c03[_0x16f54b][_0x4843('0x14')];if(!_0x576a79[_0x4843('0x1b')]&&_0x279c03[_0x16f54b]['provider']!==_0x4843('0x1c'))_0x576a79['service']=_0x279c03[_0x16f54b][_0x4843('0x1d')];if(!this[_0x4843('0xd')]['hasOwnProperty'](_0x576a79['id'])){this['imapAccounts'][_0x576a79['id']]=new EmailImap(_0x576a79,_0x279c03[_0x16f54b][_0x4843('0x5')]);this[_0x4843('0xd')][_0x576a79['id']]['on'](_0x4843('0x1e'),this[_0x4843('0x1f')]['bind'](this,_0x16f54b));this[_0x4843('0xd')][_0x576a79['id']]['on']('email',this['handleImapEmail']['bind'](this,_0x16f54b));this[_0x4843('0xd')][_0x576a79['id']]['on'](_0x4843('0x20'),this[_0x4843('0x21')][_0x4843('0x22')](this,_0x16f54b));this[_0x4843('0xd')][_0x576a79['id']]['on']('error',this['handleImapError'][_0x4843('0x22')](this,_0x16f54b));}}else{if(_['isNil'](this[_0x4843('0xc')][_0x16f54b][_0x4843('0x14')])){this[_0x4843('0xc')][_0x16f54b][_0x4843('0x14')]={};}this[_0x4843('0xc')][_0x16f54b][_0x4843('0x14')][_0x4843('0x13')]=_0x4843('0x23');this[_0x4843('0xc')][_0x16f54b][_0x4843('0x14')]['messageStatus']='';this['emit'](_0x16f54b);}}}};EmailChannel[_0x4843('0xf')][_0x4843('0x1f')]=function(_0x2b9d19,_0x2f869f){if(this[_0x4843('0xc')][_0x2b9d19]){this[_0x4843('0xc')][_0x2b9d19][_0x4843('0x14')][_0x4843('0x13')]=_0x4843('0x24');this[_0x4843('0xc')][_0x2b9d19][_0x4843('0x14')][_0x4843('0x15')]=_0x2f869f[_0x4843('0x10')];if(_0x2f869f[_0x4843('0x25')]){this[_0x4843('0xc')][_0x2b9d19][_0x4843('0x14')][_0x4843('0x26')]=_0x2f869f[_0x4843('0x25')][_0x4843('0x26')];}return this[_0x4843('0x18')](_0x2b9d19);}};EmailChannel[_0x4843('0xf')][_0x4843('0x27')]=function(_0x4cde23,_0xa7d684){logger[_0x4843('0xb')]('New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s',_0xa7d684[_0x4843('0x28')],_0xa7d684[_0x4843('0x29')],_0xa7d684[_0x4843('0x2a')],_0x4cde23);if(this[_0x4843('0xc')][_0x4cde23]){return mailAccount[_0x4843('0x2b')](_0x4cde23,_0xa7d684)['then'](function(_0x1f23bd){if(_0x1f23bd){logger['info'](_0x4843('0x2c'));}})[_0x4843('0x2d')](function(_0x5ce0e6){logger[_0x4843('0x2e')](_0x4843('0x2f'),JSON[_0x4843('0x17')](_0x5ce0e6));});}};EmailChannel['prototype']['handleImapEnd']=function(_0x19e676){if(this[_0x4843('0xc')][_0x19e676]&&this[_0x4843('0xc')][_0x19e676][_0x4843('0x14')]){if(this[_0x4843('0xd')][this['mailAccounts'][_0x19e676][_0x4843('0x14')]['id']]){delete this[_0x4843('0xd')][this[_0x4843('0xc')][_0x19e676]['Imap']['id']];}this['mailAccounts'][_0x19e676][_0x4843('0x14')][_0x4843('0x30')]=moment();this['emit'](_0x19e676);}};EmailChannel[_0x4843('0xf')][_0x4843('0x31')]=function(_0x50599d,_0x388cbc){if(this[_0x4843('0xc')][_0x50599d]){this[_0x4843('0xc')][_0x50599d]['Imap'][_0x4843('0x13')]=_0x4843('0x2e');this[_0x4843('0xc')][_0x50599d]['Imap'][_0x4843('0x15')]=_0x388cbc;return this[_0x4843('0x18')](_0x50599d);}};EmailChannel[_0x4843('0xf')][_0x4843('0x32')]=function(_0x1da691,_0x1c9d04){return this[_0x4843('0xe')][_0x4843('0x33')](_0x1da691,_0x1c9d04);};EmailChannel[_0x4843('0xf')]['verifySmtp']=function(_0x39ae34){var _0x445bb8=this;if(this['mailAccounts'][_0x39ae34['id']]){if(_['isNil'](this[_0x4843('0xc')][_0x39ae34['id']][_0x4843('0x34')])){this[_0x4843('0xc')][_0x39ae34['id']][_0x4843('0x34')]={};}this[_0x4843('0xc')][_0x39ae34['id']][_0x4843('0x34')]['lastSync']=moment();return new BPromise(function(_0x5571de,_0x5bf821){return _0x445bb8[_0x4843('0xe')]['verify'](_0x39ae34)[_0x4843('0x35')](function(_0x442ae0){_0x445bb8[_0x4843('0xc')][_0x39ae34['id']]['Smtp']['status']=_0x4843('0x36');_0x445bb8[_0x4843('0xc')][_0x39ae34['id']]['Smtp'][_0x4843('0x15')]='';_0x445bb8['emit'](_0x39ae34['id']);_0x5571de(_0x442ae0);})[_0x4843('0x2d')](function(_0x178446){_0x445bb8[_0x4843('0xc')][_0x39ae34['id']][_0x4843('0x34')][_0x4843('0x13')]=_0x4843('0x2e');_0x445bb8[_0x4843('0xc')][_0x39ae34['id']][_0x4843('0x34')][_0x4843('0x15')]=_0x178446;_0x445bb8[_0x4843('0x18')](_0x39ae34['id']);_0x5bf821(_0x178446);});});}return this[_0x4843('0xe')][_0x4843('0x37')](_0x39ae34);};module[_0x4843('0x38')]=EmailChannel;