0852f215e8770d6bb5f16135b823ffdca083b1dc
[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 _0x4a03=['redis','localhost','socket.io-emitter','./imap','./smtp','../rpc/mailAccount','mailAccounts','imapAccounts','emit','active','Imap','status','total','isNil','stringify','mailAccount:update','prototype','hasOwnProperty','box','handleImapBox','bind','handleImapEmail','handleImapEnd','error','messageStatus','connected','name','messages','info','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName','from','then','Mail\x20accepted\x20from\x20routing','catch','Mail\x20not\x20accepted\x20from\x20routing\x20%s','handleImapError','sendMail','smtp','send','Smtp','lastSync','verified','verify','exports','lodash','moment','bluebird','md5','ioredis','../../../config/environment','email','defaults'];(function(_0x4565bf,_0x5ad9d6){var _0x5610ba=function(_0x170988){while(--_0x170988){_0x4565bf['push'](_0x4565bf['shift']());}};_0x5610ba(++_0x5ad9d6);}(_0x4a03,0x172));var _0x34a0=function(_0x5537e4,_0x39dd38){_0x5537e4=_0x5537e4-0x0;var _0xa24836=_0x4a03[_0x5537e4];return _0xa24836;};'use strict';var _=require(_0x34a0('0x0'));var moment=require(_0x34a0('0x1'));var BPromise=require(_0x34a0('0x2'));var md5=require(_0x34a0('0x3'));var Redis=require(_0x34a0('0x4'));var config=require(_0x34a0('0x5'));var logger=require('../../../config/logger')(_0x34a0('0x6'));config['redis']=_[_0x34a0('0x7')](config[_0x34a0('0x8')],{'host':_0x34a0('0x9'),'port':0x18eb});var io=require(_0x34a0('0xa'))(new Redis(config[_0x34a0('0x8')]));var EmailImap=require(_0x34a0('0xb'));var EmailSmtp=require(_0x34a0('0xc'));var mailAccount=require(_0x34a0('0xd'));var md5Accounts={};function EmailChannel(_0xe289cd){logger['info']('start\x20email\x20channel');this[_0x34a0('0xe')]=_0xe289cd[_0x34a0('0xe')];this[_0x34a0('0xf')]={};this['smtp']=new EmailSmtp();}EmailChannel['prototype'][_0x34a0('0x10')]=function(_0x2f4b62){var _0x4a9907=this[_0x34a0('0xe')][_0x2f4b62];var _0xd75462={'id':_0x4a9907['id'],'name':_0x4a9907['name'],'key':_0x4a9907['key'],'active':_0x4a9907[_0x34a0('0x11')],'Imap':{'status':_0x4a9907[_0x34a0('0x12')][_0x34a0('0x13')],'messageStatus':_0x4a9907['Imap']['messageStatus'],'total':_0x4a9907[_0x34a0('0x12')][_0x34a0('0x14')]}};if(_[_0x34a0('0x15')](md5Accounts[_0x2f4b62])){md5Accounts[_0x2f4b62]={};}var _0x10d88f=md5(JSON[_0x34a0('0x16')](_0xd75462));if(md5Accounts[_0x2f4b62]['md5']!==_0x10d88f){md5Accounts[_0x2f4b62][_0x34a0('0x3')]=_0x10d88f;io['emit'](_0x34a0('0x17'),_0xd75462);}};EmailChannel[_0x34a0('0x18')]['refresh']=function(_0x548c97){for(var _0xf79942 in _0x548c97){if(_0x548c97[_0x34a0('0x19')](_0xf79942)){if(_0x548c97[_0xf79942][_0x34a0('0x12')]&&_0x548c97[_0xf79942]['active']){var _0x215ddd=_0x548c97[_0xf79942][_0x34a0('0x12')];if(!this[_0x34a0('0xf')][_0x34a0('0x19')](_0x215ddd['id'])){this[_0x34a0('0xf')][_0x215ddd['id']]=new EmailImap(_0x215ddd,_0x548c97[_0xf79942][_0x34a0('0x6')]);this[_0x34a0('0xf')][_0x215ddd['id']]['on'](_0x34a0('0x1a'),this[_0x34a0('0x1b')][_0x34a0('0x1c')](this,_0xf79942));this[_0x34a0('0xf')][_0x215ddd['id']]['on']('email',this[_0x34a0('0x1d')][_0x34a0('0x1c')](this,_0xf79942));this[_0x34a0('0xf')][_0x215ddd['id']]['on']('end',this[_0x34a0('0x1e')]['bind'](this,_0xf79942));this[_0x34a0('0xf')][_0x215ddd['id']]['on'](_0x34a0('0x1f'),this['handleImapError']['bind'](this,_0xf79942));}}else{if(_[_0x34a0('0x15')](this[_0x34a0('0xe')][_0xf79942][_0x34a0('0x12')])){this[_0x34a0('0xe')][_0xf79942][_0x34a0('0x12')]={};}this[_0x34a0('0xe')][_0xf79942][_0x34a0('0x12')][_0x34a0('0x13')]='disabled';this['mailAccounts'][_0xf79942][_0x34a0('0x12')][_0x34a0('0x20')]='';this['emit'](_0xf79942);}}}};EmailChannel['prototype'][_0x34a0('0x1b')]=function(_0x395abf,_0x4a2f04){if(this['mailAccounts'][_0x395abf]){this[_0x34a0('0xe')][_0x395abf][_0x34a0('0x12')][_0x34a0('0x13')]=_0x34a0('0x21');this[_0x34a0('0xe')][_0x395abf][_0x34a0('0x12')][_0x34a0('0x20')]=_0x4a2f04[_0x34a0('0x22')];if(_0x4a2f04['messages']){this[_0x34a0('0xe')][_0x395abf][_0x34a0('0x12')]['total']=_0x4a2f04[_0x34a0('0x23')]['total'];}return this[_0x34a0('0x10')](_0x395abf);}};EmailChannel[_0x34a0('0x18')]['handleImapEmail']=function(_0x4fd245,_0x588252){logger[_0x34a0('0x24')](_0x34a0('0x25'),_0x588252[_0x34a0('0x26')],_0x588252[_0x34a0('0x27')],_0x588252[_0x34a0('0x28')],_0x4fd245);if(this['mailAccounts'][_0x4fd245]){return mailAccount['notify'](_0x4fd245,_0x588252)[_0x34a0('0x29')](function(_0x45688d){if(_0x45688d){logger[_0x34a0('0x24')](_0x34a0('0x2a'));}})[_0x34a0('0x2b')](function(_0x37bd9c){logger[_0x34a0('0x1f')](_0x34a0('0x2c'),JSON['stringify'](_0x37bd9c));});}};EmailChannel[_0x34a0('0x18')]['handleImapEnd']=function(_0x50b391){if(this[_0x34a0('0xe')][_0x50b391]&&this[_0x34a0('0xe')][_0x50b391][_0x34a0('0x12')]){if(this[_0x34a0('0xf')][this[_0x34a0('0xe')][_0x50b391][_0x34a0('0x12')]['id']]){delete this['imapAccounts'][this[_0x34a0('0xe')][_0x50b391][_0x34a0('0x12')]['id']];}this[_0x34a0('0xe')][_0x50b391]['Imap']['lastSync']=moment();this[_0x34a0('0x10')](_0x50b391);}};EmailChannel[_0x34a0('0x18')][_0x34a0('0x2d')]=function(_0x52a77c,_0x1d466d){if(this[_0x34a0('0xe')][_0x52a77c]){this[_0x34a0('0xe')][_0x52a77c][_0x34a0('0x12')][_0x34a0('0x13')]=_0x34a0('0x1f');this[_0x34a0('0xe')][_0x52a77c][_0x34a0('0x12')][_0x34a0('0x20')]=_0x1d466d;return this['emit'](_0x52a77c);}};EmailChannel[_0x34a0('0x18')][_0x34a0('0x2e')]=function(_0x224013,_0x2e9f2a){return this[_0x34a0('0x2f')][_0x34a0('0x30')](_0x224013,_0x2e9f2a);};EmailChannel[_0x34a0('0x18')]['verifySmtp']=function(_0x5204d8){var _0xeb433a=this;if(this[_0x34a0('0xe')][_0x5204d8['id']]){if(_[_0x34a0('0x15')](this[_0x34a0('0xe')][_0x5204d8['id']][_0x34a0('0x31')])){this[_0x34a0('0xe')][_0x5204d8['id']]['Smtp']={};}this['mailAccounts'][_0x5204d8['id']]['Smtp'][_0x34a0('0x32')]=moment();return new BPromise(function(_0x2a7794,_0x7247f5){return _0xeb433a[_0x34a0('0x2f')]['verify'](_0x5204d8)['then'](function(_0x17dbe2){_0xeb433a['mailAccounts'][_0x5204d8['id']][_0x34a0('0x31')][_0x34a0('0x13')]=_0x34a0('0x33');_0xeb433a['mailAccounts'][_0x5204d8['id']][_0x34a0('0x31')][_0x34a0('0x20')]='';_0xeb433a[_0x34a0('0x10')](_0x5204d8['id']);_0x2a7794(_0x17dbe2);})[_0x34a0('0x2b')](function(_0x112bde){_0xeb433a[_0x34a0('0xe')][_0x5204d8['id']][_0x34a0('0x31')][_0x34a0('0x13')]=_0x34a0('0x1f');_0xeb433a[_0x34a0('0xe')][_0x5204d8['id']]['Smtp'][_0x34a0('0x20')]=_0x112bde;_0xeb433a['emit'](_0x5204d8['id']);_0x7247f5(_0x112bde);});});}return this['smtp'][_0x34a0('0x34')](_0x5204d8);};module[_0x34a0('0x35')]=EmailChannel;