1c11da6171f74c0b637a00887e08c76d11478d64
[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 _0x3d5d=['from','then','catch','Mail\x20not\x20accepted\x20from\x20routing\x20%s','lastSync','sendMail','verifySmtp','isNil','Smtp','verify','exports','lodash','moment','bluebird','md5','../../../config/environment','email','redis','defaults','localhost','./imap','./smtp','../rpc/mailAccount','info','start\x20email\x20channel','mailAccounts','imapAccounts','smtp','prototype','emit','name','key','Imap','status','messageStatus','total','mailAccount:update','hasOwnProperty','active','box','handleImapEnd','error','handleImapError','bind','disabled','handleImapBox','connected','messages','handleImapEmail','New\x20email\x20from\x20%s\x20%s\x20%s\x20to\x20%s','firstName','lastName'];(function(_0x200a88,_0xe77d83){var _0x480a20=function(_0x5ebd21){while(--_0x5ebd21){_0x200a88['push'](_0x200a88['shift']());}};_0x480a20(++_0xe77d83);}(_0x3d5d,0xa7));var _0xd3d5=function(_0x3bef2c,_0x5a86fd){_0x3bef2c=_0x3bef2c-0x0;var _0x331503=_0x3d5d[_0x3bef2c];return _0x331503;};'use strict';var _=require(_0xd3d5('0x0'));var moment=require(_0xd3d5('0x1'));var BPromise=require(_0xd3d5('0x2'));var md5=require(_0xd3d5('0x3'));var Redis=require('ioredis');var config=require(_0xd3d5('0x4'));var logger=require('../../../config/logger')(_0xd3d5('0x5'));config[_0xd3d5('0x6')]=_[_0xd3d5('0x7')](config[_0xd3d5('0x6')],{'host':_0xd3d5('0x8'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0xd3d5('0x6')]));var EmailImap=require(_0xd3d5('0x9'));var EmailSmtp=require(_0xd3d5('0xa'));var mailAccount=require(_0xd3d5('0xb'));var md5Accounts={};function EmailChannel(_0x57eb16){logger[_0xd3d5('0xc')](_0xd3d5('0xd'));this['mailAccounts']=_0x57eb16[_0xd3d5('0xe')];this[_0xd3d5('0xf')]={};this[_0xd3d5('0x10')]=new EmailSmtp();}EmailChannel[_0xd3d5('0x11')][_0xd3d5('0x12')]=function(_0x493743){var _0x43b464=this[_0xd3d5('0xe')][_0x493743];var _0x829e0b={'id':_0x43b464['id'],'name':_0x43b464[_0xd3d5('0x13')],'key':_0x43b464[_0xd3d5('0x14')],'active':_0x43b464['active'],'Imap':{'status':_0x43b464[_0xd3d5('0x15')][_0xd3d5('0x16')],'messageStatus':_0x43b464[_0xd3d5('0x15')][_0xd3d5('0x17')],'total':_0x43b464['Imap'][_0xd3d5('0x18')]}};if(_['isNil'](md5Accounts[_0x493743])){md5Accounts[_0x493743]={};}var _0x77068e=md5(JSON['stringify'](_0x829e0b));if(md5Accounts[_0x493743][_0xd3d5('0x3')]!==_0x77068e){md5Accounts[_0x493743][_0xd3d5('0x3')]=_0x77068e;io[_0xd3d5('0x12')](_0xd3d5('0x19'),_0x829e0b);}};EmailChannel['prototype']['refresh']=function(_0x2b0119){for(var _0x196a0f in _0x2b0119){if(_0x2b0119[_0xd3d5('0x1a')](_0x196a0f)){if(_0x2b0119[_0x196a0f][_0xd3d5('0x15')]&&_0x2b0119[_0x196a0f][_0xd3d5('0x1b')]){var _0x282262=_0x2b0119[_0x196a0f][_0xd3d5('0x15')];if(!this[_0xd3d5('0xf')][_0xd3d5('0x1a')](_0x282262['id'])){this[_0xd3d5('0xf')][_0x282262['id']]=new EmailImap(_0x282262,_0x2b0119[_0x196a0f][_0xd3d5('0x5')]);this[_0xd3d5('0xf')][_0x282262['id']]['on'](_0xd3d5('0x1c'),this['handleImapBox']['bind'](this,_0x196a0f));this[_0xd3d5('0xf')][_0x282262['id']]['on']('email',this['handleImapEmail']['bind'](this,_0x196a0f));this[_0xd3d5('0xf')][_0x282262['id']]['on']('end',this[_0xd3d5('0x1d')]['bind'](this,_0x196a0f));this[_0xd3d5('0xf')][_0x282262['id']]['on'](_0xd3d5('0x1e'),this[_0xd3d5('0x1f')][_0xd3d5('0x20')](this,_0x196a0f));}}else{if(_['isNil'](this[_0xd3d5('0xe')][_0x196a0f][_0xd3d5('0x15')])){this[_0xd3d5('0xe')][_0x196a0f][_0xd3d5('0x15')]={};}this['mailAccounts'][_0x196a0f][_0xd3d5('0x15')][_0xd3d5('0x16')]=_0xd3d5('0x21');this[_0xd3d5('0xe')][_0x196a0f][_0xd3d5('0x15')]['messageStatus']='';this[_0xd3d5('0x12')](_0x196a0f);}}}};EmailChannel[_0xd3d5('0x11')][_0xd3d5('0x22')]=function(_0xc467f8,_0x2035c6){if(this[_0xd3d5('0xe')][_0xc467f8]){this['mailAccounts'][_0xc467f8][_0xd3d5('0x15')][_0xd3d5('0x16')]=_0xd3d5('0x23');this['mailAccounts'][_0xc467f8][_0xd3d5('0x15')]['messageStatus']=_0x2035c6[_0xd3d5('0x13')];if(_0x2035c6[_0xd3d5('0x24')]){this['mailAccounts'][_0xc467f8][_0xd3d5('0x15')][_0xd3d5('0x18')]=_0x2035c6[_0xd3d5('0x24')]['total'];}return this[_0xd3d5('0x12')](_0xc467f8);}};EmailChannel[_0xd3d5('0x11')][_0xd3d5('0x25')]=function(_0x54158d,_0x390413){logger[_0xd3d5('0xc')](_0xd3d5('0x26'),_0x390413[_0xd3d5('0x27')],_0x390413[_0xd3d5('0x28')],_0x390413[_0xd3d5('0x29')],_0x54158d);if(this[_0xd3d5('0xe')][_0x54158d]){return mailAccount['notify'](_0x54158d,_0x390413)[_0xd3d5('0x2a')](function(_0x3ca07c){if(_0x3ca07c){logger[_0xd3d5('0xc')]('Mail\x20accepted\x20from\x20routing');}})[_0xd3d5('0x2b')](function(_0x1ce434){logger[_0xd3d5('0x1e')](_0xd3d5('0x2c'),JSON['stringify'](_0x1ce434));});}};EmailChannel[_0xd3d5('0x11')][_0xd3d5('0x1d')]=function(_0x286f64){if(this[_0xd3d5('0xe')][_0x286f64]&&this[_0xd3d5('0xe')][_0x286f64][_0xd3d5('0x15')]){if(this['imapAccounts'][this['mailAccounts'][_0x286f64][_0xd3d5('0x15')]['id']]){delete this[_0xd3d5('0xf')][this['mailAccounts'][_0x286f64][_0xd3d5('0x15')]['id']];}this[_0xd3d5('0xe')][_0x286f64]['Imap'][_0xd3d5('0x2d')]=moment();this['emit'](_0x286f64);}};EmailChannel['prototype'][_0xd3d5('0x1f')]=function(_0x4b5c7d,_0x201d38){if(this['mailAccounts'][_0x4b5c7d]){this[_0xd3d5('0xe')][_0x4b5c7d][_0xd3d5('0x15')][_0xd3d5('0x16')]='error';this[_0xd3d5('0xe')][_0x4b5c7d][_0xd3d5('0x15')][_0xd3d5('0x17')]=_0x201d38;return this[_0xd3d5('0x12')](_0x4b5c7d);}};EmailChannel['prototype'][_0xd3d5('0x2e')]=function(_0x24adcd,_0x1bdd74){return this[_0xd3d5('0x10')]['send'](_0x24adcd,_0x1bdd74);};EmailChannel[_0xd3d5('0x11')][_0xd3d5('0x2f')]=function(_0x4dcaa2){var _0x172161=this;if(this['mailAccounts'][_0x4dcaa2['id']]){if(_[_0xd3d5('0x30')](this[_0xd3d5('0xe')][_0x4dcaa2['id']]['Smtp'])){this[_0xd3d5('0xe')][_0x4dcaa2['id']][_0xd3d5('0x31')]={};}this[_0xd3d5('0xe')][_0x4dcaa2['id']][_0xd3d5('0x31')][_0xd3d5('0x2d')]=moment();return new BPromise(function(_0x11c73d,_0x52914d){return _0x172161[_0xd3d5('0x10')][_0xd3d5('0x32')](_0x4dcaa2)[_0xd3d5('0x2a')](function(_0x413ea3){_0x172161[_0xd3d5('0xe')][_0x4dcaa2['id']][_0xd3d5('0x31')][_0xd3d5('0x16')]='verified';_0x172161[_0xd3d5('0xe')][_0x4dcaa2['id']][_0xd3d5('0x31')][_0xd3d5('0x17')]='';_0x172161[_0xd3d5('0x12')](_0x4dcaa2['id']);_0x11c73d(_0x413ea3);})[_0xd3d5('0x2b')](function(_0x5d194c){_0x172161['mailAccounts'][_0x4dcaa2['id']][_0xd3d5('0x31')][_0xd3d5('0x16')]=_0xd3d5('0x1e');_0x172161[_0xd3d5('0xe')][_0x4dcaa2['id']][_0xd3d5('0x31')][_0xd3d5('0x17')]=_0x5d194c;_0x172161[_0xd3d5('0x12')](_0x4dcaa2['id']);_0x52914d(_0x5d194c);});});}return this[_0xd3d5('0x10')][_0xd3d5('0x32')](_0x4dcaa2);};module[_0xd3d5('0x33')]=EmailChannel;